| 1 | // TODO(schwehr): check for reference counting leaks |
| 2 | // TODO(schwehr): better error handling for all messages |
| 3 | |
| 4 | #include <Python.h> |
| 5 | |
| 6 | #include "ais.h" |
| 7 | |
| 8 | namespace libais { |
| 9 | |
| 10 | PyObject *ais_py_exception; |
| 11 | const char exception_short[] = "DecodeError"; |
| 12 | const char exception_name[] = "_ais.DecodeError"; |
| 13 | |
| 14 | // TODO(schwehr): Write a full module doc string. |
| 15 | |
| 16 | // Functional Identifiers (FI) are individual messages within a |
| 17 | // specific DAC. An FI in one DAC has nothing to do with an FI in |
| 18 | // another DAC. |
| 19 | enum AIS_FI { |
| 20 | AIS_FI_6_1_0_TEXT = 0, |
| 21 | AIS_FI_6_1_1_ACK = 1, |
| 22 | AIS_FI_6_1_2_FI_INTERROGATE = 2, |
| 23 | AIS_FI_6_1_3_CAPABILITY_INTERROGATE = 3, |
| 24 | AIS_FI_6_1_4_CAPABILITY_REPLY = 4, |
| 25 | AIS_FI_6_1_5_ACK = 5, |
| 26 | AIS_FI_6_1_12_DANGEROUS_CARGO = 12, |
| 27 | AIS_FI_6_1_14_TIDAL_WINDOW = 14, |
| 28 | AIS_FI_6_1_16_VTS_TARGET = 16, |
| 29 | AIS_FI_6_1_18_ENTRY_TIME = 18, |
| 30 | AIS_FI_6_1_20_BERTHING = 20, |
| 31 | AIS_FI_6_1_25_DANGEROUS_CARGO = 25, |
| 32 | AIS_FI_6_1_28_ROUTE = 28, |
| 33 | AIS_FI_6_1_30_TEXT = 30, |
| 34 | AIS_FI_6_1_32_TIDAL_WINDOW = 32, |
| 35 | AIS_FI_6_1_40_PERSONS_ON_BOARD = 40, |
| 36 | AIS_FI_6_200_21_RIS_VTS_ETA = 21, |
| 37 | AIS_FI_6_200_22_RIS_VTS_RTA = 22, |
| 38 | AIS_FI_6_200_55_RIS_VTS_SAR = 55, |
| 39 | |
| 40 | AIS_FI_8_1_0_TEXT = 0, |
| 41 | AIS_FI_8_1_11_MET_HYDRO = 11, |
| 42 | AIS_FI_8_1_13_FAIRWAY_CLOSED = 13, |
| 43 | AIS_FI_8_1_15_SHIP_AND_VOYAGE = 15, |
| 44 | AIS_FI_8_1_16_PERSONS_ON_BOARD = 16, |
| 45 | AIS_FI_8_1_17_VTS_TARGET = 17, |
| 46 | AIS_FI_8_1_19_TRAFFIC_SIGNAL = 19, |
| 47 | AIS_FI_8_1_21_WEATHER_OBS = 21, |
| 48 | AIS_FI_8_1_22_AREA_NOTICE = 22, |
| 49 | AIS_FI_8_1_24_SHIP_AND_VOYAGE = 24, |
| 50 | AIS_FI_8_1_26_SENSOR = 26, |
| 51 | AIS_FI_8_1_27_ROUTE = 27, |
| 52 | AIS_FI_8_1_29_TEXT = 29, |
| 53 | AIS_FI_8_1_31_MET_HYDRO = 31, |
| 54 | AIS_FI_8_1_40_PERSONS_ON_BOARD = 40, |
| 55 | AIS_FI_8_200_10_RIS_SHIP_AND_VOYAGE = 10, |
| 56 | AIS_FI_8_200_21_RIS_ETA_AT_LOCK_BRIDGE_TERMINAL = 21, |
| 57 | AIS_FI_8_200_22_RIS_RTA_AT_LOCK_BRIDGE_TERMINAL = 22, |
| 58 | AIS_FI_8_200_23_RIS_EMMA_WARNING = 23, |
| 59 | AIS_FI_8_200_24_RIS_WATERLEVEL = 24, |
| 60 | AIS_FI_8_200_40_RIS_ATON_SIGNAL_STATUS = 40, |
| 61 | AIS_FI_8_200_55_RIS_PERSONS_ON_BOARD = 50, |
| 62 | AIS_FI_8_366_22_AREA_NOTICE = 22, // USCG. |
| 63 | AIS_FI_8_367_22_AREA_NOTICE = 22, // USCG. |
| 64 | }; |
| 65 | |
| 66 | void |
| 67 | DictSafeSetItem(PyObject *dict, const string &key, const long val) { // NOLINT |
| 68 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 69 | PyObject *val_obj = PyLong_FromLong(val); |
| 70 | assert(key_obj); |
| 71 | assert(val_obj); |
| 72 | PyDict_SetItem(dict, key_obj, val_obj); |
| 73 | Py_DECREF(key_obj); |
| 74 | Py_DECREF(val_obj); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | DictSafeSetItem(PyObject *dict, const string &key, const int val) { |
| 79 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 80 | PyObject *val_obj = PyLong_FromLong(val); |
| 81 | assert(key_obj); |
| 82 | assert(val_obj); |
| 83 | PyDict_SetItem(dict, key_obj, val_obj); |
| 84 | Py_DECREF(key_obj); |
| 85 | Py_DECREF(val_obj); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | DictSafeSetItem(PyObject *dict, const string &key, const unsigned int val) { |
| 90 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 91 | PyObject *val_obj = PyLong_FromLong(val); |
| 92 | assert(key_obj); |
| 93 | assert(val_obj); |
| 94 | PyDict_SetItem(dict, key_obj, val_obj); |
| 95 | Py_DECREF(key_obj); |
| 96 | Py_DECREF(val_obj); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | DictSafeSetItem(PyObject *dict, const string &key, const string &val) { |
| 101 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 102 | PyObject *val_obj = PyUnicode_FromString(val.c_str()); |
| 103 | assert(key_obj); |
| 104 | assert(val_obj); |
| 105 | PyDict_SetItem(dict, key_obj, val_obj); |
| 106 | Py_DECREF(key_obj); |
| 107 | Py_DECREF(val_obj); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void |
| 112 | DictSafeSetItem(PyObject *dict, const string &key, const char *val) { |
| 113 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 114 | PyObject *val_obj = PyUnicode_FromString(val); |
| 115 | assert(key_obj); |
| 116 | assert(val_obj); |
| 117 | PyDict_SetItem(dict, key_obj, val_obj); |
| 118 | Py_DECREF(key_obj); |
| 119 | Py_DECREF(val_obj); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | #if 0 |
| 124 | void |
| 125 | DictSafeSetItem(PyObject *dict, const string &key, const bool val) { |
| 126 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 127 | PyObject *val_obj = PyBool_FromLong(val); |
| 128 | assert(key_obj); |
| 129 | assert(val_obj); |
| 130 | PyDict_SetItem(dict, key_obj, val_obj); |
| 131 | Py_DECREF(key_obj); |
| 132 | Py_DECREF(val_obj); |
| 133 | } |
| 134 | #else |
| 135 | void |
| 136 | DictSafeSetItem(PyObject *dict, const string &key, const bool val) { |
| 137 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 138 | assert(key_obj); |
| 139 | if (val) { |
| 140 | PyDict_SetItem(dict, key_obj, Py_True); |
| 141 | } else { |
| 142 | PyDict_SetItem(dict, key_obj, Py_False); |
| 143 | } |
| 144 | Py_DECREF(key_obj); |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | void |
| 149 | DictSafeSetItem(PyObject *dict, const string &key, const float val) { |
| 150 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 151 | PyObject *val_obj = PyFloat_FromDouble(val); |
| 152 | assert(key_obj); |
| 153 | assert(val_obj); |
| 154 | PyDict_SetItem(dict, key_obj, val_obj); |
| 155 | Py_DECREF(key_obj); |
| 156 | Py_DECREF(val_obj); |
| 157 | } |
| 158 | |
| 159 | // Python Floats are IEE-754 double precision. |
| 160 | void |
| 161 | DictSafeSetItem(PyObject *dict, const string &key, const double val) { |
| 162 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 163 | PyObject *val_obj = PyFloat_FromDouble(val); |
| 164 | assert(key_obj); |
| 165 | assert(val_obj); |
| 166 | PyDict_SetItem(dict, key_obj, val_obj); |
| 167 | Py_DECREF(key_obj); |
| 168 | Py_DECREF(val_obj); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | DictSafeSetItem(PyObject *dict, const string &x_key, const string& y_key, |
| 173 | const AisPoint& position) { |
| 174 | DictSafeSetItem(dict, x_key, position.lng_deg); |
| 175 | DictSafeSetItem(dict, y_key, position.lat_deg); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | DictSafeSetItem(PyObject *dict, const string &key, PyObject *val_obj) { |
| 180 | // When we need to add dictionaries and such to a dictionary |
| 181 | assert(dict); |
| 182 | assert(val_obj); |
| 183 | PyObject *key_obj = PyUnicode_FromString(key.c_str()); |
| 184 | assert(key_obj); |
| 185 | PyDict_SetItem(dict, key_obj, val_obj); |
| 186 | Py_DECREF(key_obj); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | PyObject * |
| 191 | ais_msg_to_pydict(const AisMsg* msg) { |
| 192 | assert(msg); |
| 193 | |
| 194 | PyObject *dict = PyDict_New(); |
| 195 | DictSafeSetItem(dict, "id", msg->message_id); |
| 196 | DictSafeSetItem(dict, "repeat_indicator", msg->repeat_indicator); |
| 197 | DictSafeSetItem(dict, "mmsi", msg->mmsi); |
| 198 | |
| 199 | return dict; |
| 200 | } |
| 201 | |
| 202 | // Class A position report |
| 203 | PyObject * |
| 204 | ais1_2_3_to_pydict(const char *nmea_payload, const size_t pad) { |
| 205 | assert(nmea_payload); |
| 206 | |
| 207 | Ais1_2_3 msg(nmea_payload, pad); |
| 208 | if (msg.had_error()) { |
| 209 | PyErr_Format(ais_py_exception, "Ais1_2_3: %s", |
| 210 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 215 | |
| 216 | DictSafeSetItem(dict, "nav_status", msg.nav_status); |
| 217 | DictSafeSetItem(dict, "rot_over_range", msg.rot_over_range); |
| 218 | DictSafeSetItem(dict, "rot", msg.rot); |
| 219 | DictSafeSetItem(dict, "sog", msg.sog); |
| 220 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 221 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 222 | DictSafeSetItem(dict, "cog", msg.cog); |
| 223 | DictSafeSetItem(dict, "true_heading", msg.true_heading); |
| 224 | DictSafeSetItem(dict, "timestamp", msg.timestamp); |
| 225 | DictSafeSetItem(dict, "special_manoeuvre", msg.special_manoeuvre); |
| 226 | DictSafeSetItem(dict, "spare", msg.spare); |
| 227 | DictSafeSetItem(dict, "raim", msg.raim); |
| 228 | |
| 229 | // COMM States |
| 230 | DictSafeSetItem(dict, "sync_state", msg.sync_state); // Both SOTDMA & ITDMA |
| 231 | |
| 232 | // SOTDMA |
| 233 | if (msg.message_id == 1 || msg.message_id == 2) { |
| 234 | if (msg.slot_timeout_valid) { |
| 235 | DictSafeSetItem(dict, "slot_timeout", msg.slot_timeout); |
| 236 | } |
| 237 | |
| 238 | if (msg.received_stations_valid) { |
| 239 | DictSafeSetItem(dict, "received_stations", msg.received_stations); |
| 240 | } |
| 241 | if (msg.slot_number_valid) { |
| 242 | DictSafeSetItem(dict, "slot_number", msg.slot_number); |
| 243 | } |
| 244 | if (msg.utc_valid) { |
| 245 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 246 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 247 | DictSafeSetItem(dict, "utc_spare", msg.utc_spare); |
| 248 | } |
| 249 | |
| 250 | if (msg.slot_offset_valid) { |
| 251 | DictSafeSetItem(dict, "slot_offset", msg.slot_offset); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // ITDMA |
| 256 | if (msg.slot_increment_valid) { |
| 257 | DictSafeSetItem(dict, "slot_increment", msg.slot_increment); |
| 258 | DictSafeSetItem(dict, "slots_to_allocate", msg.slots_to_allocate); |
| 259 | DictSafeSetItem(dict, "keep_flag", msg.keep_flag); |
| 260 | } |
| 261 | |
| 262 | return dict; |
| 263 | } |
| 264 | |
| 265 | // Basestation report and ';' time report |
| 266 | PyObject * |
| 267 | ais4_11_to_pydict(const char *nmea_payload, const size_t pad) { |
| 268 | assert(nmea_payload); |
| 269 | |
| 270 | Ais4_11 msg(nmea_payload, pad); |
| 271 | if (msg.had_error()) { |
| 272 | PyErr_Format(ais_py_exception, "Ais4_11: %s", |
| 273 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 274 | return nullptr; |
| 275 | } |
| 276 | |
| 277 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 278 | |
| 279 | DictSafeSetItem(dict, "year", msg.year); |
| 280 | DictSafeSetItem(dict, "month", msg.month); |
| 281 | DictSafeSetItem(dict, "day", msg.day); |
| 282 | DictSafeSetItem(dict, "hour", msg.hour); |
| 283 | DictSafeSetItem(dict, "minute", msg.minute); |
| 284 | DictSafeSetItem(dict, "second", msg.second); |
| 285 | |
| 286 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 287 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 288 | |
| 289 | DictSafeSetItem(dict, "fix_type", msg.fix_type); |
| 290 | DictSafeSetItem(dict, "transmission_ctl", msg.transmission_ctl); |
| 291 | DictSafeSetItem(dict, "spare", msg.spare); |
| 292 | DictSafeSetItem(dict, "raim", msg.raim); |
| 293 | |
| 294 | // SOTDMA |
| 295 | DictSafeSetItem(dict, "sync_state", msg.sync_state); |
| 296 | DictSafeSetItem(dict, "slot_timeout", msg.slot_timeout); |
| 297 | |
| 298 | if (msg.received_stations_valid) |
| 299 | DictSafeSetItem(dict, "received_stations", msg.received_stations); |
| 300 | if (msg.slot_number_valid) |
| 301 | DictSafeSetItem(dict, "slot_number", msg.slot_number); |
| 302 | if (msg.utc_valid) { |
| 303 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 304 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 305 | DictSafeSetItem(dict, "utc_spare", msg.utc_spare); |
| 306 | } |
| 307 | |
| 308 | if (msg.slot_offset_valid) |
| 309 | DictSafeSetItem(dict, "slot_offset", msg.slot_offset); |
| 310 | |
| 311 | return dict; |
| 312 | } |
| 313 | |
| 314 | // Class A ship data |
| 315 | PyObject * |
| 316 | ais5_to_pydict(const char *nmea_payload, const size_t pad) { |
| 317 | assert(nmea_payload); |
| 318 | Ais5 msg(nmea_payload, pad); |
| 319 | if (msg.had_error()) { |
| 320 | PyErr_Format(ais_py_exception, "Ais5: %s", |
| 321 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 322 | return nullptr; |
| 323 | } |
| 324 | |
| 325 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 326 | |
| 327 | DictSafeSetItem(dict, "ais_version", msg.ais_version); |
| 328 | DictSafeSetItem(dict, "imo_num", msg.imo_num); |
| 329 | DictSafeSetItem(dict, "callsign", msg.callsign); |
| 330 | DictSafeSetItem(dict, "name", msg.name); |
| 331 | DictSafeSetItem(dict, "type_and_cargo", msg.type_and_cargo); |
| 332 | DictSafeSetItem(dict, "dim_a", msg.dim_a); |
| 333 | DictSafeSetItem(dict, "dim_b", msg.dim_b); |
| 334 | DictSafeSetItem(dict, "dim_c", msg.dim_c); |
| 335 | DictSafeSetItem(dict, "dim_d", msg.dim_d); |
| 336 | DictSafeSetItem(dict, "fix_type", msg.fix_type); |
| 337 | DictSafeSetItem(dict, "eta_month", msg.eta_month); |
| 338 | DictSafeSetItem(dict, "eta_day", msg.eta_day); |
| 339 | DictSafeSetItem(dict, "eta_hour", msg.eta_hour); |
| 340 | DictSafeSetItem(dict, "eta_minute", msg.eta_minute); |
| 341 | DictSafeSetItem(dict, "draught", msg.draught); |
| 342 | DictSafeSetItem(dict, "destination", msg.destination); |
| 343 | DictSafeSetItem(dict, "dte", msg.dte); |
| 344 | DictSafeSetItem(dict, "spare", msg.spare); |
| 345 | |
| 346 | return dict; |
| 347 | } |
| 348 | |
| 349 | // Address binary message |
| 350 | AIS_STATUS |
| 351 | ais6_1_0_append_pydict(const char *nmea_payload, PyObject *dict, |
| 352 | const size_t pad) { |
| 353 | assert(nmea_payload); |
| 354 | assert(dict); |
| 355 | assert(pad < 6); |
| 356 | Ais6_1_0 msg(nmea_payload, pad); |
| 357 | if (msg.had_error()) { |
| 358 | return msg.get_error(); |
| 359 | } |
| 360 | |
| 361 | DictSafeSetItem(dict, "ack_required", msg.ack_required); |
| 362 | DictSafeSetItem(dict, "msg_seq", msg.msg_seq); |
| 363 | DictSafeSetItem(dict, "text", msg.text); |
| 364 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 365 | |
| 366 | return AIS_OK; |
| 367 | } |
| 368 | |
| 369 | AIS_STATUS |
| 370 | ais6_1_1_append_pydict(const char *nmea_payload, PyObject *dict, |
| 371 | const size_t pad) { |
| 372 | assert(nmea_payload); |
| 373 | assert(dict); |
| 374 | assert(pad < 6); |
| 375 | Ais6_1_1 msg(nmea_payload, pad); |
| 376 | if (msg.had_error()) { |
| 377 | return msg.get_error(); |
| 378 | } |
| 379 | |
| 380 | DictSafeSetItem(dict, "ack_dac", msg.ack_dac); |
| 381 | DictSafeSetItem(dict, "msg_seq", msg.msg_seq); |
| 382 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 383 | |
| 384 | return AIS_OK; |
| 385 | } |
| 386 | |
| 387 | AIS_STATUS |
| 388 | ais6_1_2_append_pydict(const char *nmea_payload, PyObject *dict, |
| 389 | const size_t pad) { |
| 390 | assert(nmea_payload); |
| 391 | assert(dict); |
| 392 | assert(pad < 6); |
| 393 | Ais6_1_2 msg(nmea_payload, pad); |
| 394 | if (msg.had_error()) { |
| 395 | return msg.get_error(); |
| 396 | } |
| 397 | |
| 398 | DictSafeSetItem(dict, "req_dac", msg.req_dac); |
| 399 | DictSafeSetItem(dict, "req_fi", msg.req_fi); |
| 400 | |
| 401 | return AIS_OK; |
| 402 | } |
| 403 | |
| 404 | AIS_STATUS |
| 405 | ais6_1_3_append_pydict(const char *nmea_payload, PyObject *dict, |
| 406 | const size_t pad) { |
| 407 | assert(nmea_payload); |
| 408 | assert(dict); |
| 409 | assert(pad < 6); |
| 410 | |
| 411 | Ais6_1_3 msg(nmea_payload, pad); |
| 412 | if (msg.had_error()) { |
| 413 | return msg.get_error(); |
| 414 | } |
| 415 | |
| 416 | DictSafeSetItem(dict, "req_dac", msg.req_dac); |
| 417 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 418 | DictSafeSetItem(dict, "spare3", msg.spare3); |
| 419 | DictSafeSetItem(dict, "spare4", msg.spare4); |
| 420 | |
| 421 | return AIS_OK; |
| 422 | } |
| 423 | |
| 424 | AIS_STATUS |
| 425 | ais6_1_4_append_pydict(const char *nmea_payload, PyObject *dict, |
| 426 | const size_t pad) { |
| 427 | assert(nmea_payload); |
| 428 | assert(dict); |
| 429 | assert(pad < 6); |
| 430 | Ais6_1_4 msg(nmea_payload, pad); |
| 431 | if (msg.had_error()) { |
| 432 | return msg.get_error(); |
| 433 | } |
| 434 | |
| 435 | DictSafeSetItem(dict, "ack_dac", msg.ack_dac); |
| 436 | constexpr int kNumFI = 64; |
| 437 | PyObject *cap_list = PyList_New(kNumFI); |
| 438 | PyObject *res_list = PyList_New(kNumFI); |
| 439 | for (size_t cap_num = 0; cap_num < kNumFI; cap_num++) { |
| 440 | // TODO(schwehr): memory leak? |
| 441 | PyObject *cap = PyLong_FromLong(long(msg.capabilities[cap_num])); // NOLINT |
| 442 | PyList_SetItem(cap_list, cap_num, cap); |
| 443 | |
| 444 | PyObject *res = PyLong_FromLong(long(msg.cap_reserved[cap_num])); // NOLINT |
| 445 | PyList_SetItem(res_list, cap_num, res); |
| 446 | } |
| 447 | DictSafeSetItem(dict, "capabilities", cap_list); |
| 448 | DictSafeSetItem(dict, "cap_reserved", res_list); |
| 449 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 450 | DictSafeSetItem(dict, "spare3", msg.spare2); |
| 451 | DictSafeSetItem(dict, "spare4", msg.spare2); |
| 452 | DictSafeSetItem(dict, "spare5", msg.spare2); |
| 453 | |
| 454 | return AIS_OK; |
| 455 | } |
| 456 | |
| 457 | AIS_STATUS |
| 458 | ais6_1_5_append_pydict(const char *nmea_payload, PyObject *dict, |
| 459 | const size_t pad) { |
| 460 | assert(nmea_payload); |
| 461 | assert(dict); |
| 462 | assert(pad < 6); |
| 463 | Ais6_1_5 msg(nmea_payload, pad); |
| 464 | if (msg.had_error()) { |
| 465 | return msg.get_error(); |
| 466 | } |
| 467 | |
| 468 | DictSafeSetItem(dict, "ack_dac", msg.ack_dac); |
| 469 | DictSafeSetItem(dict, "ack_fi", msg.ack_dac); |
| 470 | DictSafeSetItem(dict, "seq_num", msg.ack_dac); |
| 471 | DictSafeSetItem(dict, "ai_available", msg.ack_dac); |
| 472 | DictSafeSetItem(dict, "ai_response", msg.ack_dac); |
| 473 | DictSafeSetItem(dict, "spare", msg.ack_dac); |
| 474 | |
| 475 | return AIS_OK; |
| 476 | } |
| 477 | |
| 478 | AIS_STATUS |
| 479 | ais6_1_12_append_pydict(const char *nmea_payload, PyObject *dict, |
| 480 | const size_t pad) { |
| 481 | assert(nmea_payload); |
| 482 | assert(pad < 6); |
| 483 | Ais6_1_12 msg(nmea_payload, pad); |
| 484 | if (msg.had_error()) { |
| 485 | return msg.get_error(); |
| 486 | } |
| 487 | |
| 488 | DictSafeSetItem(dict, "last_port", msg.last_port); |
| 489 | DictSafeSetItem(dict, "utc_month_dep", msg.utc_month_dep); // actual |
| 490 | DictSafeSetItem(dict, "utc_day_dep", msg.utc_day_dep); |
| 491 | DictSafeSetItem(dict, "utc_hour_dep", msg.utc_hour_dep); |
| 492 | DictSafeSetItem(dict, "utc_min_dep", msg.utc_min_dep); |
| 493 | DictSafeSetItem(dict, "next_port", msg.next_port); |
| 494 | DictSafeSetItem(dict, "utc_month_next", msg.utc_month_next); // estimated |
| 495 | DictSafeSetItem(dict, "utc_day_next", msg.utc_day_next); |
| 496 | DictSafeSetItem(dict, "utc_hour_next", msg.utc_hour_next); |
| 497 | DictSafeSetItem(dict, "utc_min_next", msg.utc_min_next); |
| 498 | DictSafeSetItem(dict, "main_danger", msg.main_danger); |
| 499 | DictSafeSetItem(dict, "imo_cat", msg.imo_cat); |
| 500 | DictSafeSetItem(dict, "un", msg.un); |
| 501 | DictSafeSetItem(dict, "value", msg.value); // TODO(schwehr): units? |
| 502 | DictSafeSetItem(dict, "value_unit", msg.value_unit); |
| 503 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 504 | |
| 505 | return AIS_OK; |
| 506 | } |
| 507 | |
| 508 | // 6_1_13 does not exist |
| 509 | |
| 510 | // IMO Circ 289 - Tidal Window |
| 511 | AIS_STATUS |
| 512 | ais6_1_14_append_pydict(const char *nmea_payload, PyObject *dict, |
| 513 | const size_t pad) { |
| 514 | assert(nmea_payload); |
| 515 | assert(pad < 6); |
| 516 | Ais6_1_14 msg(nmea_payload, pad); |
| 517 | if (msg.had_error()) { |
| 518 | return msg.get_error(); |
| 519 | } |
| 520 | |
| 521 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 522 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 523 | |
| 524 | if (!msg.windows.size()) |
| 525 | return AIS_ERR_BAD_SUB_MSG; |
| 526 | |
| 527 | PyObject *window_list = PyList_New(msg.windows.size()); |
| 528 | for (size_t w_num = 0; w_num < msg.windows.size(); w_num++) { |
| 529 | PyObject *window = PyDict_New(); |
| 530 | DictSafeSetItem(window, "x", "y", msg.windows[w_num].position); |
| 531 | DictSafeSetItem(window, "utc_hour_from", msg.windows[w_num].utc_hour_from); |
| 532 | DictSafeSetItem(window, "utc_min_from", msg.windows[w_num].utc_min_from); |
| 533 | DictSafeSetItem(window, "utc_hour_to", msg.windows[w_num].utc_hour_to); |
| 534 | DictSafeSetItem(window, "utc_min_to", msg.windows[w_num].utc_min_to); |
| 535 | DictSafeSetItem(window, "cur_dir", msg.windows[w_num].cur_dir); |
| 536 | DictSafeSetItem(window, "cur_speed", msg.windows[w_num].cur_speed); |
| 537 | PyList_SetItem(window_list, w_num, window); |
| 538 | } |
| 539 | PyDict_SetItem(dict, PyUnicode_FromString("windows"), window_list); |
| 540 | |
| 541 | return AIS_OK; |
| 542 | } |
| 543 | |
| 544 | // 6_1_15, 6_1_16, and 6_1_17 do not exist |
| 545 | |
| 546 | // IMO Circ 289 - Clearance time to enter port |
| 547 | AIS_STATUS |
| 548 | ais6_1_18_append_pydict(const char *nmea_payload, PyObject *dict, |
| 549 | const size_t pad) { |
| 550 | assert(nmea_payload); |
| 551 | assert(pad < 6); |
| 552 | Ais6_1_18 msg(nmea_payload, pad); |
| 553 | if (msg.had_error()) { |
| 554 | return msg.get_error(); |
| 555 | } |
| 556 | |
| 557 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 558 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 559 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 560 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 561 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 562 | DictSafeSetItem(dict, "port_berth", msg.port_berth); |
| 563 | DictSafeSetItem(dict, "dest", msg.dest); |
| 564 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 565 | DictSafeSetItem(dict, "spare2_0", msg.spare2[0]); |
| 566 | DictSafeSetItem(dict, "spare2_1", msg.spare2[1]); |
| 567 | |
| 568 | return AIS_OK; |
| 569 | } |
| 570 | |
| 571 | // 6_1_19 does not exist |
| 572 | |
| 573 | // IMO Circ 289 - Berthing data |
| 574 | AIS_STATUS |
| 575 | ais6_1_20_append_pydict(const char *nmea_payload, PyObject *dict, |
| 576 | const size_t pad) { |
| 577 | assert(nmea_payload); |
| 578 | assert(pad < 6); |
| 579 | Ais6_1_20 msg(nmea_payload, pad); |
| 580 | if (msg.had_error()) { |
| 581 | return msg.get_error(); |
| 582 | } |
| 583 | |
| 584 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 585 | DictSafeSetItem(dict, "length", msg.length); |
| 586 | DictSafeSetItem(dict, "depth", msg.depth); |
| 587 | DictSafeSetItem(dict, "position", msg.mooring_position); |
| 588 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 589 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 590 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 591 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 592 | if (msg.services_known) { |
| 593 | PyObject *serv_list = PyList_New(26); |
| 594 | for (size_t serv_num = 0; serv_num < 26; serv_num++) { |
| 595 | PyObject *serv = PyLong_FromLong(long(msg.services[serv_num])); // NOLINT |
| 596 | PyList_SetItem(serv_list, serv_num, serv); |
| 597 | } |
| 598 | DictSafeSetItem(dict, "services", serv_list); |
| 599 | } |
| 600 | DictSafeSetItem(dict, "name", msg.name); |
| 601 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 602 | |
| 603 | return AIS_OK; |
| 604 | } |
| 605 | |
| 606 | // 6_1_21, 6_1_22, 6_1_23 and 6_1_24 Do not exist (yet?) |
| 607 | |
| 608 | AIS_STATUS |
| 609 | ais6_1_25_append_pydict(const char *nmea_payload, PyObject *dict, |
| 610 | const size_t pad) { |
| 611 | assert(nmea_payload); |
| 612 | assert(dict); |
| 613 | assert(pad < 6); |
| 614 | Ais6_1_25 msg(nmea_payload, pad); |
| 615 | if (msg.had_error()) { |
| 616 | return msg.get_error(); |
| 617 | } |
| 618 | |
| 619 | DictSafeSetItem(dict, "amount_unit", msg.amount_unit); |
| 620 | DictSafeSetItem(dict, "amount", msg.amount); |
| 621 | |
| 622 | if (0 == msg.cargos.size()) |
| 623 | return AIS_ERR_BAD_SUB_MSG; |
| 624 | |
| 625 | PyObject *cargo_list = PyList_New(msg.cargos.size()); |
| 626 | for (size_t cargo_num = 0; cargo_num < msg.cargos.size(); cargo_num++) { |
| 627 | PyObject *cargo = PyDict_New(); |
| 628 | if (msg.cargos[cargo_num].imdg_valid) |
| 629 | DictSafeSetItem(cargo, "imdg", msg.cargos[cargo_num].imdg); |
| 630 | if (msg.cargos[cargo_num].spare_valid) |
| 631 | DictSafeSetItem(cargo, "spare", msg.cargos[cargo_num].spare); |
| 632 | if (msg.cargos[cargo_num].un_valid) |
| 633 | DictSafeSetItem(cargo, "un", msg.cargos[cargo_num].un); |
| 634 | if (msg.cargos[cargo_num].bc_valid) |
| 635 | DictSafeSetItem(cargo, "bc", msg.cargos[cargo_num].bc); |
| 636 | if (msg.cargos[cargo_num].marpol_oil_valid) |
| 637 | DictSafeSetItem(cargo, "marpol_oil", msg.cargos[cargo_num].marpol_oil); |
| 638 | if (msg.cargos[cargo_num].marpol_cat_valid) |
| 639 | DictSafeSetItem(cargo, "marpol_cat", msg.cargos[cargo_num].marpol_cat); |
| 640 | PyList_SetItem(cargo_list, cargo_num, cargo); |
| 641 | } |
| 642 | PyDict_SetItem(dict, PyUnicode_FromString("cargos"), cargo_list); |
| 643 | |
| 644 | return AIS_OK; |
| 645 | } |
| 646 | |
| 647 | // 6_1_26 |
| 648 | // 6_1_27 |
| 649 | // 6_1_28 - TODO(schwehr): Route Addressed - clone from 8_1_27 |
| 650 | // 6_1_29 |
| 651 | // 6_1_30 - TODO(schwehr): Text Addressed |
| 652 | // 6_1_31 |
| 653 | |
| 654 | AIS_STATUS |
| 655 | ais6_1_32_append_pydict(const char *nmea_payload, PyObject *dict, |
| 656 | const size_t pad) { |
| 657 | assert(nmea_payload); |
| 658 | assert(dict); |
| 659 | assert(pad < 6); |
| 660 | Ais6_1_32 msg(nmea_payload, pad); |
| 661 | if (msg.had_error()) { |
| 662 | return msg.get_error(); |
| 663 | } |
| 664 | |
| 665 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 666 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 667 | |
| 668 | PyObject *window_list = PyList_New(msg.windows.size()); |
| 669 | for (size_t win_num = 0; win_num < msg.windows.size(); win_num++) { |
| 670 | PyObject *win = PyDict_New(); |
| 671 | DictSafeSetItem(win, "x", "y", msg.windows[win_num].position); |
| 672 | DictSafeSetItem(win, "from_utc_hour", msg.windows[win_num].from_utc_hour); |
| 673 | DictSafeSetItem(win, "from_utc_min", msg.windows[win_num].from_utc_min); |
| 674 | DictSafeSetItem(win, "to_utc_hour", msg.windows[win_num].to_utc_hour); |
| 675 | DictSafeSetItem(win, "to_utc_min", msg.windows[win_num].to_utc_min); |
| 676 | DictSafeSetItem(win, "cur_dir", msg.windows[win_num].cur_dir); |
| 677 | DictSafeSetItem(win, "cur_speed", msg.windows[win_num].cur_speed); |
| 678 | PyList_SetItem(window_list, win_num, win); |
| 679 | } |
| 680 | PyDict_SetItem(dict, PyUnicode_FromString("windows"), window_list); |
| 681 | |
| 682 | return AIS_OK; |
| 683 | } |
| 684 | |
| 685 | AIS_STATUS |
| 686 | ais6_1_40_append_pydict(const char *nmea_payload, PyObject *dict, |
| 687 | const size_t pad) { |
| 688 | assert(nmea_payload); |
| 689 | assert(dict); |
| 690 | assert(pad < 6); |
| 691 | Ais6_1_40 msg(nmea_payload, pad); |
| 692 | if (msg.had_error()) { |
| 693 | return msg.get_error(); |
| 694 | } |
| 695 | DictSafeSetItem(dict, "persons", msg.persons); |
| 696 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 697 | |
| 698 | return AIS_OK; |
| 699 | } |
| 700 | |
| 701 | PyObject* |
| 702 | ais6_to_pydict(const char *nmea_payload, const size_t pad) { |
| 703 | assert(nmea_payload); |
| 704 | Ais6 msg(nmea_payload, pad); |
| 705 | if (msg.had_error() && msg.get_error() != AIS_UNINITIALIZED) { |
| 706 | PyErr_Format(ais_py_exception, "Ais6: %s", |
| 707 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 708 | return nullptr; |
| 709 | } |
| 710 | |
| 711 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 712 | |
| 713 | DictSafeSetItem(dict, "seq", msg.seq); |
| 714 | DictSafeSetItem(dict, "mmsi_dest", msg.mmsi_dest); |
| 715 | DictSafeSetItem(dict, "retransmit", msg.retransmit); |
| 716 | DictSafeSetItem(dict, "spare", msg.spare); |
| 717 | DictSafeSetItem(dict, "dac", msg.dac); |
| 718 | DictSafeSetItem(dict, "fi", msg.fi); |
| 719 | |
| 720 | // TODO(schwehr): manage all the submessage types |
| 721 | |
| 722 | AIS_STATUS status = AIS_UNINITIALIZED; |
| 723 | |
| 724 | switch (msg.dac) { |
| 725 | case AIS_DAC_1_INTERNATIONAL: // IMO. |
| 726 | switch (msg.fi) { |
| 727 | case AIS_FI_6_1_0_TEXT: // OLD ITU 1371-1. |
| 728 | status = ais6_1_0_append_pydict(nmea_payload, dict, pad); |
| 729 | break; |
| 730 | case AIS_FI_6_1_1_ACK: // OLD ITU 1371-1. |
| 731 | status = ais6_1_1_append_pydict(nmea_payload, dict, pad); |
| 732 | break; |
| 733 | case AIS_FI_6_1_2_FI_INTERROGATE: // OLD ITU 1371-1. |
| 734 | status = ais6_1_2_append_pydict(nmea_payload, dict, pad); |
| 735 | break; |
| 736 | case AIS_FI_6_1_3_CAPABILITY_INTERROGATE: // OLD ITU 1371-1. |
| 737 | status = ais6_1_3_append_pydict(nmea_payload, dict, pad); |
| 738 | break; |
| 739 | case AIS_FI_6_1_4_CAPABILITY_REPLY: // OLD ITU 1371-1. |
| 740 | status = ais6_1_4_append_pydict(nmea_payload, dict, pad); |
| 741 | break; |
| 742 | case AIS_FI_6_1_5_ACK: // ITU 1371-5. |
| 743 | status = ais6_1_5_append_pydict(nmea_payload, dict, pad); |
| 744 | break; |
| 745 | case AIS_FI_6_1_12_DANGEROUS_CARGO: // Not to be used after 1 Jan 2013. |
| 746 | status = ais6_1_12_append_pydict(nmea_payload, dict, pad); |
| 747 | break; |
| 748 | case AIS_FI_6_1_14_TIDAL_WINDOW: // Not to be used after 1 Jan 2013. |
| 749 | status = ais6_1_14_append_pydict(nmea_payload, dict, pad); |
| 750 | break; |
| 751 | case AIS_FI_6_1_18_ENTRY_TIME: |
| 752 | status = ais6_1_18_append_pydict(nmea_payload, dict, pad); |
| 753 | break; |
| 754 | case AIS_FI_6_1_20_BERTHING: |
| 755 | status = ais6_1_20_append_pydict(nmea_payload, dict, pad); |
| 756 | break; |
| 757 | case AIS_FI_6_1_25_DANGEROUS_CARGO: |
| 758 | status = ais6_1_25_append_pydict(nmea_payload, dict, pad); |
| 759 | break; |
| 760 | // TODO(schwehr): AIS_FI_6_1_28_ROUTE. |
| 761 | // TODO(schwehr): AIS_FI_6_1_30_TEXT. |
| 762 | case AIS_FI_6_1_32_TIDAL_WINDOW: // IMO Circ 289 |
| 763 | status = ais6_1_32_append_pydict(nmea_payload, dict, pad); |
| 764 | break; |
| 765 | case AIS_FI_6_1_40_PERSONS_ON_BOARD: // OLD ITU 1371-1. |
| 766 | status = ais6_1_40_append_pydict(nmea_payload, dict, pad); |
| 767 | break; |
| 768 | default: |
| 769 | // TODO(schwehr): Raise an exception? |
| 770 | DictSafeSetItem(dict, "not_parsed", true); |
| 771 | } |
| 772 | break; |
| 773 | |
| 774 | default: |
| 775 | // TODO(schwehr): Raise an exception? |
| 776 | DictSafeSetItem(dict, "not_parsed", true); |
| 777 | } |
| 778 | |
| 779 | if (status != AIS_OK) { |
| 780 | PyErr_Format(ais_py_exception, |
| 781 | "Ais6: DAC:FI not known. 6:%d:%d %s", |
| 782 | msg.dac, |
| 783 | msg.fi, |
| 784 | AIS_STATUS_STRINGS[status]); |
| 785 | return nullptr; |
| 786 | } |
| 787 | |
| 788 | return dict; |
| 789 | } |
| 790 | |
| 791 | // Acknowledgement |
| 792 | PyObject* |
| 793 | ais7_13_to_pydict(const char *nmea_payload, const size_t pad) { |
| 794 | assert(nmea_payload); |
| 795 | Ais7_13 msg(nmea_payload, pad); |
| 796 | if (msg.had_error()) { |
| 797 | PyErr_Format(ais_py_exception, "Ais7_13: %s", |
| 798 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 799 | return nullptr; |
| 800 | } |
| 801 | |
| 802 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 803 | |
| 804 | PyObject *list = PyList_New(msg.dest_mmsi.size()); |
| 805 | for (size_t i = 0; i < msg.dest_mmsi.size(); i++) { |
| 806 | PyObject *tuple = PyTuple_New(2); |
| 807 | PyTuple_SetItem(tuple, 0, PyLong_FromLong(msg.dest_mmsi[i])); // Steals ref |
| 808 | PyTuple_SetItem(tuple, 1, PyLong_FromLong(msg.seq_num[i])); // Steals ref |
| 809 | PyList_SetItem(list, i, tuple); // Steals ref |
| 810 | } |
| 811 | PyDict_SetItem(dict, PyUnicode_FromString("acks"), list); |
| 812 | Py_DECREF(list); |
| 813 | return dict; |
| 814 | } |
| 815 | |
| 816 | AIS_STATUS |
| 817 | ais8_1_0_append_pydict(const char *nmea_payload, PyObject *dict, |
| 818 | const size_t pad) { |
| 819 | assert(nmea_payload); |
| 820 | assert(dict); |
| 821 | assert(pad < 6); |
| 822 | Ais8_1_0 msg(nmea_payload, pad); |
| 823 | if (msg.had_error()) { |
| 824 | return msg.get_error(); |
| 825 | } |
| 826 | |
| 827 | DictSafeSetItem(dict, "ack_required", msg.ack_required); |
| 828 | DictSafeSetItem(dict, "msg_seq", msg.msg_seq); |
| 829 | DictSafeSetItem(dict, "text", msg.text); |
| 830 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 831 | |
| 832 | return AIS_OK; |
| 833 | } |
| 834 | |
| 835 | // ais 8_1_[1..10] do not exist |
| 836 | |
| 837 | AIS_STATUS |
| 838 | ais8_1_11_append_pydict(const char *nmea_payload, PyObject *dict, |
| 839 | const size_t pad) { |
| 840 | assert(nmea_payload); |
| 841 | assert(dict); |
| 842 | assert(pad < 6); |
| 843 | Ais8_1_11 msg(nmea_payload, pad); |
| 844 | if (msg.had_error()) { |
| 845 | return msg.get_error(); |
| 846 | } |
| 847 | |
| 848 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 849 | |
| 850 | DictSafeSetItem(dict, "wind_ave", msg.wind_ave); |
| 851 | DictSafeSetItem(dict, "wind_gust", msg.wind_gust); |
| 852 | DictSafeSetItem(dict, "wind_dir", msg.wind_dir); |
| 853 | DictSafeSetItem(dict, "wind_gust_dir", msg.wind_gust); |
| 854 | |
| 855 | DictSafeSetItem(dict, "air_temp", msg.air_temp); |
| 856 | DictSafeSetItem(dict, "rel_humid", msg.rel_humid); |
| 857 | DictSafeSetItem(dict, "dew_point", msg.dew_point); |
| 858 | DictSafeSetItem(dict, "air_pres", msg.air_pres); |
| 859 | DictSafeSetItem(dict, "air_pres_trend", msg.air_pres_trend); |
| 860 | DictSafeSetItem(dict, "horz_vis", msg.horz_vis); |
| 861 | |
| 862 | DictSafeSetItem(dict, "water_level", msg.water_level); |
| 863 | DictSafeSetItem(dict, "water_level_trend", msg.water_level_trend); |
| 864 | |
| 865 | DictSafeSetItem(dict, "surf_cur_speed", msg.surf_cur_speed); |
| 866 | DictSafeSetItem(dict, "surf_cur_dir", msg.surf_cur_dir); |
| 867 | |
| 868 | DictSafeSetItem(dict, "cur_speed_2", msg.cur_speed_2); |
| 869 | DictSafeSetItem(dict, "cur_dir_2", msg.cur_dir_2); |
| 870 | DictSafeSetItem(dict, "cur_depth_2", msg.cur_depth_2); |
| 871 | |
| 872 | DictSafeSetItem(dict, "cur_speed_3", msg.cur_speed_3); |
| 873 | DictSafeSetItem(dict, "cur_dir_3", msg.cur_dir_3); |
| 874 | DictSafeSetItem(dict, "cur_depth_3", msg.cur_depth_3); |
| 875 | |
| 876 | DictSafeSetItem(dict, "wave_height", msg.wave_height); |
| 877 | DictSafeSetItem(dict, "wave_period", msg.wave_period); |
| 878 | DictSafeSetItem(dict, "wave_dir", msg.wave_dir); |
| 879 | |
| 880 | DictSafeSetItem(dict, "swell_height", msg.swell_height); |
| 881 | DictSafeSetItem(dict, "swell_period", msg.swell_period); |
| 882 | DictSafeSetItem(dict, "swell_dir", msg.swell_dir); |
| 883 | |
| 884 | DictSafeSetItem(dict, "sea_state", msg.sea_state); |
| 885 | DictSafeSetItem(dict, "water_temp", msg.water_temp); |
| 886 | DictSafeSetItem(dict, "precip_type", msg.precip_type); |
| 887 | DictSafeSetItem(dict, "ice", msg.ice); // Grr... ice |
| 888 | |
| 889 | // Or could be spare |
| 890 | DictSafeSetItem(dict, "ext_water_level", msg.extended_water_level); |
| 891 | DictSafeSetItem(dict, "spare2", msg.extended_water_level); |
| 892 | |
| 893 | return AIS_OK; |
| 894 | } |
| 895 | |
| 896 | // 12 is addressed |
| 897 | |
| 898 | AIS_STATUS |
| 899 | ais8_1_13_append_pydict(const char *nmea_payload, PyObject *dict, |
| 900 | const size_t pad) { |
| 901 | assert(nmea_payload); |
| 902 | assert(dict); |
| 903 | assert(pad < 6); |
| 904 | Ais8_1_13 msg(nmea_payload, pad); |
| 905 | if (msg.had_error()) { |
| 906 | return msg.get_error(); |
| 907 | } |
| 908 | |
| 909 | DictSafeSetItem(dict, "reason", msg.reason); |
| 910 | DictSafeSetItem(dict, "location_from", msg.location_from); |
| 911 | DictSafeSetItem(dict, "location_to", msg.location_to); |
| 912 | DictSafeSetItem(dict, "radius", msg.radius); |
| 913 | DictSafeSetItem(dict, "units", msg.units); |
| 914 | DictSafeSetItem(dict, "day_from", msg.day_from); |
| 915 | DictSafeSetItem(dict, "month_from", msg.month_from); |
| 916 | DictSafeSetItem(dict, "hour_from", msg.hour_from); |
| 917 | DictSafeSetItem(dict, "minute_from", msg.minute_from); |
| 918 | DictSafeSetItem(dict, "day_to", msg.day_to); |
| 919 | DictSafeSetItem(dict, "month_to", msg.month_to); |
| 920 | DictSafeSetItem(dict, "hour_to", msg.hour_to); |
| 921 | DictSafeSetItem(dict, "minute_to", msg.minute_to); |
| 922 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 923 | |
| 924 | return AIS_OK; |
| 925 | } |
| 926 | |
| 927 | // 14 is addressed |
| 928 | |
| 929 | AIS_STATUS |
| 930 | ais8_1_15_append_pydict(const char *nmea_payload, PyObject *dict, |
| 931 | const size_t pad) { |
| 932 | assert(nmea_payload); |
| 933 | assert(dict); |
| 934 | assert(pad < 6); |
| 935 | Ais8_1_15 msg(nmea_payload, pad); |
| 936 | if (msg.had_error()) { |
| 937 | return msg.get_error(); |
| 938 | } |
| 939 | |
| 940 | DictSafeSetItem(dict, "air_draught", msg.air_draught); |
| 941 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 942 | return AIS_OK; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | AIS_STATUS |
| 947 | ais8_1_16_append_pydict(const char *nmea_payload, PyObject *dict, |
| 948 | const size_t pad) { |
| 949 | assert(nmea_payload); |
| 950 | assert(dict); |
| 951 | assert(pad < 6); |
| 952 | Ais8_1_16 msg(nmea_payload, pad); |
| 953 | if (msg.had_error()) { |
| 954 | return msg.get_error(); |
| 955 | } |
| 956 | |
| 957 | DictSafeSetItem(dict, "persons", msg.persons); |
| 958 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 959 | return AIS_OK; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | AIS_STATUS |
| 964 | ais8_1_17_append_pydict(const char *nmea_payload, PyObject *dict, |
| 965 | const size_t pad) { |
| 966 | assert(nmea_payload); |
| 967 | assert(dict); |
| 968 | assert(pad < 6); |
| 969 | Ais8_1_17 msg(nmea_payload, pad); |
| 970 | if (msg.had_error()) { |
| 971 | return msg.get_error(); |
| 972 | } |
| 973 | |
| 974 | PyObject *target_list = PyList_New(msg.targets.size()); |
| 975 | for (size_t target_num = 0; target_num < msg.targets.size(); target_num++) { |
| 976 | PyObject *target = PyDict_New(); |
| 977 | DictSafeSetItem(target, "type", msg.targets[target_num].type); |
| 978 | DictSafeSetItem(target, "id", msg.targets[target_num].id); |
| 979 | DictSafeSetItem(target, "spare", msg.targets[target_num].spare); |
| 980 | DictSafeSetItem(target, "x", "y", msg.targets[target_num].position); |
| 981 | DictSafeSetItem(target, "cog", msg.targets[target_num].cog); |
| 982 | DictSafeSetItem(target, "timestamp", msg.targets[target_num].timestamp); |
| 983 | DictSafeSetItem(target, "sog", msg.targets[target_num].sog); |
| 984 | PyList_SetItem(target_list, target_num, target); |
| 985 | } |
| 986 | PyDict_SetItem(dict, PyUnicode_FromString("targets"), target_list); |
| 987 | |
| 988 | return AIS_OK; |
| 989 | } |
| 990 | |
| 991 | // 18 is addressed only |
| 992 | |
| 993 | AIS_STATUS |
| 994 | ais8_1_19_append_pydict(const char *nmea_payload, PyObject *dict, |
| 995 | const size_t pad) { |
| 996 | assert(nmea_payload); |
| 997 | assert(dict); |
| 998 | assert(pad < 6); |
| 999 | Ais8_1_19 msg(nmea_payload, pad); |
| 1000 | if (msg.had_error()) { |
| 1001 | return msg.get_error(); |
| 1002 | } |
| 1003 | |
| 1004 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1005 | DictSafeSetItem(dict, "name", msg.name); |
| 1006 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 1007 | DictSafeSetItem(dict, "status", msg.status); |
| 1008 | DictSafeSetItem(dict, "signal", msg.signal); |
| 1009 | DictSafeSetItem(dict, "utc_hour_next", msg.utc_hour_next); |
| 1010 | DictSafeSetItem(dict, "utc_min_next", msg.utc_min_next); |
| 1011 | DictSafeSetItem(dict, "next_signal", msg.next_signal); |
| 1012 | DictSafeSetItem(dict, "spare2_0", msg.spare2[0]); |
| 1013 | DictSafeSetItem(dict, "spare2_1", msg.spare2[1]); |
| 1014 | DictSafeSetItem(dict, "spare2_2", msg.spare2[2]); |
| 1015 | DictSafeSetItem(dict, "spare2_3", msg.spare2[3]); |
| 1016 | |
| 1017 | return AIS_OK; |
| 1018 | } |
| 1019 | |
| 1020 | // 20 is addressed |
| 1021 | |
| 1022 | AIS_STATUS |
| 1023 | ais8_1_21_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1024 | const size_t pad) { |
| 1025 | assert(nmea_payload); |
| 1026 | assert(dict); |
| 1027 | assert(pad < 6); |
| 1028 | Ais8_1_21 msg(nmea_payload, pad); |
| 1029 | if (msg.had_error()) { |
| 1030 | return msg.get_error(); |
| 1031 | } |
| 1032 | |
| 1033 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 1034 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 1035 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 1036 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 1037 | |
| 1038 | if (0 == msg.type_wx_report) { |
| 1039 | // WX obs from ship |
| 1040 | DictSafeSetItem(dict, "location", msg.location); |
| 1041 | DictSafeSetItem(dict, "wx", msg.wx[0]); // TODO(schwehr) Rename present? |
| 1042 | DictSafeSetItem(dict, "horz_viz", msg.horz_viz); |
| 1043 | DictSafeSetItem(dict, "humidity", msg.humidity); |
| 1044 | DictSafeSetItem(dict, "wind_speed", msg.wind_speed); |
| 1045 | DictSafeSetItem(dict, "wind_dir", msg.wind_dir); |
| 1046 | DictSafeSetItem(dict, "pressure", msg.pressure); |
| 1047 | DictSafeSetItem(dict, "pressure_tendency", msg.pressure_tendency); |
| 1048 | DictSafeSetItem(dict, "air_temp", msg.air_temp); |
| 1049 | DictSafeSetItem(dict, "water_temp", msg.water_temp); |
| 1050 | DictSafeSetItem(dict, "wave_period", msg.wave_period); |
| 1051 | DictSafeSetItem(dict, "wave_height", msg.wave_height); |
| 1052 | DictSafeSetItem(dict, "wave_dir", msg.wave_dir); |
| 1053 | DictSafeSetItem(dict, "swell_height", msg.swell_height); |
| 1054 | DictSafeSetItem(dict, "swell_dir", msg.swell_dir); |
| 1055 | DictSafeSetItem(dict, "swell_period", msg.swell_period); |
| 1056 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1057 | } else { |
| 1058 | // type == 1 |
| 1059 | // WMO OBS from ship |
| 1060 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 1061 | DictSafeSetItem(dict, "cog", msg.cog); |
| 1062 | DictSafeSetItem(dict, "sog", msg.sog); |
| 1063 | DictSafeSetItem(dict, "heading", msg.heading); |
| 1064 | DictSafeSetItem(dict, "pressure", msg.pressure); |
| 1065 | DictSafeSetItem(dict, "rel_pressure", msg.rel_pressure); |
| 1066 | DictSafeSetItem(dict, "pressure_tendency", msg.pressure_tendency); |
| 1067 | DictSafeSetItem(dict, "wind_dir", msg.wind_dir); |
| 1068 | DictSafeSetItem(dict, "wind_speed_ms", msg.wind_speed_ms); |
| 1069 | DictSafeSetItem(dict, "wind_dir_rel", msg.wind_dir_rel); |
| 1070 | DictSafeSetItem(dict, "wind_speed_rel", msg.wind_speed_rel); |
| 1071 | DictSafeSetItem(dict, "wind_gust_speed", msg.wind_gust_speed); |
| 1072 | DictSafeSetItem(dict, "wind_gust_dir", msg.wind_gust_dir); |
| 1073 | DictSafeSetItem(dict, "air_temp_raw", msg.air_temp_raw); |
| 1074 | DictSafeSetItem(dict, "humidity", msg.humidity); |
| 1075 | DictSafeSetItem(dict, "water_temp_raw", msg.water_temp_raw); |
| 1076 | DictSafeSetItem(dict, "horz_viz", msg.horz_viz); |
| 1077 | // TODO(schwehr): list? |
| 1078 | DictSafeSetItem(dict, "wx", msg.wx[0]); |
| 1079 | DictSafeSetItem(dict, "wx_next1", msg.wx[1]); |
| 1080 | DictSafeSetItem(dict, "wx_next2", msg.wx[2]); |
| 1081 | DictSafeSetItem(dict, "cloud_total", msg.cloud_total); |
| 1082 | DictSafeSetItem(dict, "cloud_low", msg.cloud_low); |
| 1083 | DictSafeSetItem(dict, "cloud_low_type", msg.cloud_low_type); |
| 1084 | DictSafeSetItem(dict, "cloud_middle_type", msg.cloud_middle_type); |
| 1085 | DictSafeSetItem(dict, "cloud_high_type", msg.cloud_high_type); |
| 1086 | DictSafeSetItem(dict, "alt_lowest_cloud_base", msg.alt_lowest_cloud_base); |
| 1087 | DictSafeSetItem(dict, "wave_period", msg.wave_period); |
| 1088 | DictSafeSetItem(dict, "wave_height", msg.wave_height); |
| 1089 | DictSafeSetItem(dict, "swell_dir", msg.swell_dir); |
| 1090 | DictSafeSetItem(dict, "swell_period", msg.swell_period); |
| 1091 | DictSafeSetItem(dict, "swell_height", msg.swell_height); |
| 1092 | DictSafeSetItem(dict, "swell_dir_2", msg.swell_dir_2); |
| 1093 | DictSafeSetItem(dict, "swell_period_2", msg.swell_period_2); |
| 1094 | DictSafeSetItem(dict, "swell_height_2", msg.swell_height_2); |
| 1095 | DictSafeSetItem(dict, "ice_thickness", msg.ice_thickness); |
| 1096 | DictSafeSetItem(dict, "ice_accretion", msg.ice_accretion); |
| 1097 | DictSafeSetItem(dict, "ice_accretion_cause", msg.ice_accretion_cause); |
| 1098 | DictSafeSetItem(dict, "sea_ice_concentration", msg.sea_ice_concentration); |
| 1099 | DictSafeSetItem(dict, "amt_type_ice", msg.amt_type_ice); |
| 1100 | DictSafeSetItem(dict, "ice_situation", msg.ice_situation); |
| 1101 | DictSafeSetItem(dict, "ice_devel", msg.ice_devel); |
| 1102 | DictSafeSetItem(dict, "bearing_ice_edge", msg.bearing_ice_edge); |
| 1103 | } |
| 1104 | |
| 1105 | return AIS_OK; |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | AIS_STATUS |
| 1110 | ais8_1_22_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1111 | const size_t pad) { |
| 1112 | Ais8_1_22 msg(nmea_payload, pad); |
| 1113 | if (msg.had_error()) { |
| 1114 | return msg.get_error(); |
| 1115 | } |
| 1116 | |
| 1117 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1118 | DictSafeSetItem(dict, "notice_type", msg.notice_type); |
| 1119 | DictSafeSetItem(dict, "notice_type_str", |
| 1120 | ais8_1_22_notice_names[msg.notice_type]); |
| 1121 | |
| 1122 | DictSafeSetItem(dict, "month", msg.month); |
| 1123 | DictSafeSetItem(dict, "day", msg.day); |
| 1124 | DictSafeSetItem(dict, "hour", msg.hour); |
| 1125 | DictSafeSetItem(dict, "minute", msg.minute); |
| 1126 | DictSafeSetItem(dict, "duration_minutes", msg.minute); |
| 1127 | |
| 1128 | PyObject *sub_area_list = PyList_New(msg.sub_areas.size()); |
| 1129 | |
| 1130 | // Loop over sub_areas |
| 1131 | for (size_t i = 0; i < msg.sub_areas.size(); i++) { |
| 1132 | switch (msg.sub_areas[i]->getType()) { |
| 1133 | case AIS8_1_22_SHAPE_CIRCLE: // or point |
| 1134 | { |
| 1135 | PyObject *sub_area = PyDict_New(); |
| 1136 | Ais8_1_22_Circle *c = |
| 1137 | reinterpret_cast<Ais8_1_22_Circle*>(msg.sub_areas[i]); |
| 1138 | |
| 1139 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_CIRCLE); |
| 1140 | if (c->radius_m == 0) |
| 1141 | DictSafeSetItem(sub_area, "sub_area_type_str", "point"); |
| 1142 | else |
| 1143 | DictSafeSetItem(sub_area, "sub_area_type_str", "circle"); |
| 1144 | |
| 1145 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1146 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1147 | DictSafeSetItem(sub_area, "radius", c->radius_m); |
| 1148 | // TODO(schwehr): spare? |
| 1149 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1150 | } |
| 1151 | break; |
| 1152 | case AIS8_1_22_SHAPE_RECT: |
| 1153 | { |
| 1154 | PyObject *sub_area = PyDict_New(); |
| 1155 | Ais8_1_22_Rect *c = |
| 1156 | reinterpret_cast<Ais8_1_22_Rect*>(msg.sub_areas[i]); |
| 1157 | |
| 1158 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_RECT); |
| 1159 | DictSafeSetItem(sub_area, "sub_area_type_str", "rect"); |
| 1160 | |
| 1161 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1162 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1163 | DictSafeSetItem(sub_area, "e_dim_m", c->e_dim_m); |
| 1164 | DictSafeSetItem(sub_area, "n_dim_m", c->n_dim_m); |
| 1165 | DictSafeSetItem(sub_area, "orient_deg", c->orient_deg); |
| 1166 | // TODO(schwehr): spare? |
| 1167 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1168 | } |
| 1169 | break; |
| 1170 | case AIS8_1_22_SHAPE_SECTOR: |
| 1171 | { |
| 1172 | PyObject *sub_area = PyDict_New(); |
| 1173 | Ais8_1_22_Sector *c = |
| 1174 | reinterpret_cast<Ais8_1_22_Sector*>(msg.sub_areas[i]); |
| 1175 | |
| 1176 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_SECTOR); |
| 1177 | DictSafeSetItem(sub_area, "sub_area_type_str", "sector"); |
| 1178 | |
| 1179 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1180 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1181 | DictSafeSetItem(sub_area, "radius", c->radius_m); |
| 1182 | DictSafeSetItem(sub_area, "left_bound_deg", c->left_bound_deg); |
| 1183 | DictSafeSetItem(sub_area, "right_bound_deg", c->right_bound_deg); |
| 1184 | // TODO(schwehr): spare? |
| 1185 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1186 | } |
| 1187 | break; |
| 1188 | case AIS8_1_22_SHAPE_POLYLINE: |
| 1189 | { |
| 1190 | PyObject *sub_area = PyDict_New(); |
| 1191 | Ais8_1_22_Polyline *polyline = |
| 1192 | reinterpret_cast<Ais8_1_22_Polyline*>(msg.sub_areas[i]); |
| 1193 | |
| 1194 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_POLYLINE); |
| 1195 | DictSafeSetItem(sub_area, "sub_area_type_str", "polyline"); |
| 1196 | assert(polyline->angles.size() == polyline->dists_m.size()); |
| 1197 | PyObject *angle_list = PyList_New(polyline->angles.size()); |
| 1198 | PyObject *dist_list = PyList_New(polyline->angles.size()); |
| 1199 | |
| 1200 | for (size_t pt_num = 0; pt_num < polyline->angles.size(); pt_num++) { |
| 1201 | PyList_SetItem(angle_list, pt_num, |
| 1202 | PyFloat_FromDouble(polyline->angles[pt_num])); |
| 1203 | PyList_SetItem(dist_list, pt_num, |
| 1204 | PyFloat_FromDouble(polyline->dists_m[pt_num])); |
| 1205 | } |
| 1206 | |
| 1207 | DictSafeSetItem(sub_area, "angles", angle_list); |
| 1208 | DictSafeSetItem(sub_area, "dists_m", dist_list); |
| 1209 | |
| 1210 | // TODO(schwehr): spare? |
| 1211 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1212 | } |
| 1213 | break; |
| 1214 | case AIS8_1_22_SHAPE_POLYGON: |
| 1215 | { |
| 1216 | PyObject *sub_area = PyDict_New(); |
| 1217 | Ais8_1_22_Polygon *polygon = |
| 1218 | reinterpret_cast<Ais8_1_22_Polygon*>(msg.sub_areas[i]); |
| 1219 | |
| 1220 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_POLYGON); |
| 1221 | DictSafeSetItem(sub_area, "sub_area_type_str", "polygon"); |
| 1222 | assert(polygon->angles.size() == polygon->dists_m.size()); |
| 1223 | PyObject *angle_list = PyList_New(polygon->angles.size()); |
| 1224 | PyObject *dist_list = PyList_New(polygon->angles.size()); |
| 1225 | |
| 1226 | for (size_t pt_num = 0; pt_num < polygon->angles.size(); pt_num++) { |
| 1227 | PyList_SetItem(angle_list, pt_num, |
| 1228 | PyFloat_FromDouble(polygon->angles[pt_num])); |
| 1229 | PyList_SetItem(dist_list, pt_num, |
| 1230 | PyFloat_FromDouble(polygon->dists_m[pt_num])); |
| 1231 | } |
| 1232 | |
| 1233 | DictSafeSetItem(sub_area, "angles", angle_list); |
| 1234 | DictSafeSetItem(sub_area, "dists_m", dist_list); |
| 1235 | |
| 1236 | // TODO(schwehr): spare? |
| 1237 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1238 | } |
| 1239 | break; |
| 1240 | case AIS8_1_22_SHAPE_TEXT: |
| 1241 | { |
| 1242 | PyObject *sub_area = PyDict_New(); |
| 1243 | |
| 1244 | Ais8_1_22_Text *text = |
| 1245 | reinterpret_cast<Ais8_1_22_Text*>(msg.sub_areas[i]); |
| 1246 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_1_22_SHAPE_TEXT); |
| 1247 | DictSafeSetItem(sub_area, "sub_area_type_str", "text"); |
| 1248 | |
| 1249 | DictSafeSetItem(sub_area, "text", text->text); |
| 1250 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1251 | } |
| 1252 | break; |
| 1253 | |
| 1254 | default: |
| 1255 | {} // TODO(schwehr): Mark an unknown subarea or raise an exception. |
| 1256 | } |
| 1257 | } |
| 1258 | DictSafeSetItem(dict, "sub_areas", sub_area_list); |
| 1259 | |
| 1260 | return AIS_OK; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | // No 23 broadcast |
| 1265 | |
| 1266 | // IMO Circ 289 - Extended ship static and voyage-related |
| 1267 | AIS_STATUS |
| 1268 | ais8_1_24_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1269 | const size_t pad) { |
| 1270 | assert(nmea_payload); |
| 1271 | assert(dict); |
| 1272 | assert(pad < 6); |
| 1273 | Ais8_1_24 msg(nmea_payload, pad); |
| 1274 | if (msg.had_error()) { |
| 1275 | return msg.get_error(); |
| 1276 | } |
| 1277 | |
| 1278 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1279 | DictSafeSetItem(dict, "air_draught", msg.air_draught); |
| 1280 | DictSafeSetItem(dict, "last_port", msg.last_port); |
| 1281 | |
| 1282 | PyObject *port_list = PyList_New(2); |
| 1283 | PyObject *port0 = PyUnicode_FromString(msg.next_ports[0].c_str()); |
| 1284 | PyObject *port1 = PyUnicode_FromString(msg.next_ports[0].c_str()); |
| 1285 | PyList_SetItem(port_list, 0, port0); Py_DECREF(port0); |
| 1286 | PyList_SetItem(port_list, 1, port1); Py_DECREF(port1); |
| 1287 | |
| 1288 | PyObject *solas_list = PyList_New(26); |
| 1289 | for (size_t solas_num = 0; solas_num < 26; solas_num++) { |
| 1290 | PyObject *solas = PyLong_FromLong(msg.solas_status[solas_num]); |
| 1291 | PyList_SetItem(solas_list, solas_num, solas); |
| 1292 | } |
| 1293 | DictSafeSetItem(dict, "solas", solas_list); |
| 1294 | DictSafeSetItem(dict, "ice_class", msg.ice_class); |
| 1295 | DictSafeSetItem(dict, "shaft_power", msg.shaft_power); |
| 1296 | DictSafeSetItem(dict, "vhf", msg.vhf); |
| 1297 | DictSafeSetItem(dict, "lloyds_ship_type", msg.lloyds_ship_type); |
| 1298 | DictSafeSetItem(dict, "gross_tonnage", msg.gross_tonnage); |
| 1299 | DictSafeSetItem(dict, "laden_ballast", msg.laden_ballast); |
| 1300 | DictSafeSetItem(dict, "heavy_oil", msg.heavy_oil); |
| 1301 | DictSafeSetItem(dict, "light_oil", msg.light_oil); |
| 1302 | DictSafeSetItem(dict, "diesel", msg.diesel); |
| 1303 | DictSafeSetItem(dict, "bunker_oil", msg.bunker_oil); |
| 1304 | DictSafeSetItem(dict, "persons", msg.persons); |
| 1305 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1306 | |
| 1307 | return AIS_OK; |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | // No 25 broadcast |
| 1312 | |
| 1313 | AIS_STATUS |
| 1314 | ais8_1_26_append_pydict_sensor_hdr(PyObject *dict, |
| 1315 | Ais8_1_26_SensorReport* rpt) { |
| 1316 | assert(dict); |
| 1317 | assert(rpt); |
| 1318 | DictSafeSetItem(dict, "report_type", rpt->report_type); |
| 1319 | DictSafeSetItem(dict, "utc_day", rpt->utc_day); |
| 1320 | DictSafeSetItem(dict, "utc_hr", rpt->utc_hr); |
| 1321 | DictSafeSetItem(dict, "utc_min", rpt->utc_min); |
| 1322 | DictSafeSetItem(dict, "site_id", rpt->site_id); |
| 1323 | |
| 1324 | return AIS_OK; |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | // IMO Circ 289 - Environmental |
| 1329 | AIS_STATUS |
| 1330 | ais8_1_26_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1331 | const size_t pad) { |
| 1332 | assert(nmea_payload); |
| 1333 | assert(dict); |
| 1334 | assert(pad < 6); |
| 1335 | Ais8_1_26 msg(nmea_payload, pad); |
| 1336 | if (msg.had_error()) { |
| 1337 | return msg.get_error(); |
| 1338 | } |
| 1339 | |
| 1340 | PyObject *rpt_list = PyList_New(msg.reports.size()); |
| 1341 | DictSafeSetItem(dict, "reports", rpt_list); |
| 1342 | |
| 1343 | for (size_t rpt_num = 0; rpt_num < msg.reports.size(); rpt_num++) { |
| 1344 | PyObject *rpt_dict = PyDict_New(); |
| 1345 | PyList_SetItem(rpt_list, rpt_num, rpt_dict); |
| 1346 | |
| 1347 | switch (msg.reports[rpt_num]->report_type) { |
| 1348 | // case AIS8_1_26_SENSOR_ERROR: |
| 1349 | case AIS8_1_26_SENSOR_LOCATION: |
| 1350 | { |
| 1351 | Ais8_1_26_Location *rpt = |
| 1352 | reinterpret_cast<Ais8_1_26_Location *>(msg.reports[rpt_num]); |
| 1353 | ais8_1_26_append_pydict_sensor_hdr(rpt_dict, rpt); |
| 1354 | DictSafeSetItem(rpt_dict, "x", "y", rpt->position); |
| 1355 | DictSafeSetItem(rpt_dict, "z", rpt->z); |
| 1356 | DictSafeSetItem(rpt_dict, "owner", rpt->owner); |
| 1357 | DictSafeSetItem(rpt_dict, "timeout", rpt->timeout); |
| 1358 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1359 | } |
| 1360 | break; |
| 1361 | case AIS8_1_26_SENSOR_STATION: |
| 1362 | { |
| 1363 | Ais8_1_26_Station *rpt = |
| 1364 | reinterpret_cast<Ais8_1_26_Station *>(msg.reports[rpt_num]); |
| 1365 | DictSafeSetItem(rpt_dict, "name", rpt->name); |
| 1366 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1367 | } |
| 1368 | break; |
| 1369 | case AIS8_1_26_SENSOR_WIND: |
| 1370 | { |
| 1371 | Ais8_1_26_Wind *rpt = |
| 1372 | reinterpret_cast<Ais8_1_26_Wind *>(msg.reports[rpt_num]); |
| 1373 | DictSafeSetItem(rpt_dict, "wind_speed", rpt->wind_speed); |
| 1374 | DictSafeSetItem(rpt_dict, "wind_gust", rpt->wind_gust); |
| 1375 | DictSafeSetItem(rpt_dict, "wind_dir", rpt->wind_dir); |
| 1376 | DictSafeSetItem(rpt_dict, "wind_gust_dir", rpt->wind_gust_dir); |
| 1377 | DictSafeSetItem(rpt_dict, "sensor_type", rpt->sensor_type); |
| 1378 | DictSafeSetItem(rpt_dict, "wind_forecast", rpt->wind_forecast); |
| 1379 | DictSafeSetItem( |
| 1380 | rpt_dict, "wind_gust_forecast", rpt->wind_gust_forecast); |
| 1381 | DictSafeSetItem(rpt_dict, "wind_dir_forecast", rpt->wind_dir_forecast); |
| 1382 | DictSafeSetItem(rpt_dict, "utc_day_forecast", rpt->utc_day_forecast); |
| 1383 | DictSafeSetItem(rpt_dict, "utc_hour_forecast", rpt->utc_hour_forecast); |
| 1384 | DictSafeSetItem(rpt_dict, "utc_min_forecast", rpt->utc_min_forecast); |
| 1385 | DictSafeSetItem(rpt_dict, "duration", rpt->duration); |
| 1386 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1387 | } |
| 1388 | break; |
| 1389 | case AIS8_1_26_SENSOR_WATER_LEVEL: |
| 1390 | { |
| 1391 | Ais8_1_26_WaterLevel *rpt = |
| 1392 | reinterpret_cast<Ais8_1_26_WaterLevel *>(msg.reports[rpt_num]); |
| 1393 | DictSafeSetItem(rpt_dict, "type", rpt->type); |
| 1394 | DictSafeSetItem(rpt_dict, "level", rpt->level); |
| 1395 | DictSafeSetItem(rpt_dict, "trend", rpt->trend); |
| 1396 | DictSafeSetItem(rpt_dict, "vdatum", rpt->vdatum); |
| 1397 | DictSafeSetItem(rpt_dict, "sensor_type", rpt->sensor_type); |
| 1398 | DictSafeSetItem(rpt_dict, "forecast_type", rpt->forecast_type); |
| 1399 | DictSafeSetItem(rpt_dict, "level_forecast", rpt->level_forecast); |
| 1400 | DictSafeSetItem(rpt_dict, "utc_day_forecast", rpt->utc_day_forecast); |
| 1401 | DictSafeSetItem(rpt_dict, "utc_hour_forecast", rpt->utc_hour_forecast); |
| 1402 | DictSafeSetItem(rpt_dict, "utc_min_forecast", rpt->utc_min_forecast); |
| 1403 | DictSafeSetItem(rpt_dict, "duration", rpt->duration); |
| 1404 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1405 | } |
| 1406 | break; |
| 1407 | case AIS8_1_26_SENSOR_CURR_2D: |
| 1408 | { |
| 1409 | Ais8_1_26_Curr2D *rpt = |
| 1410 | reinterpret_cast<Ais8_1_26_Curr2D *>(msg.reports[rpt_num]); |
| 1411 | DictSafeSetItem(rpt_dict, "type", rpt->type); |
| 1412 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1413 | |
| 1414 | PyObject *curr_list = PyList_New(3); |
| 1415 | DictSafeSetItem(dict, "currents", curr_list); |
| 1416 | for (size_t idx = 0; idx < 3; idx++) { |
| 1417 | PyObject *curr_dict = PyDict_New(); |
| 1418 | DictSafeSetItem(curr_dict, "speed", rpt->currents[idx].speed); |
| 1419 | DictSafeSetItem(curr_dict, "dir", rpt->currents[idx].dir); |
| 1420 | DictSafeSetItem(curr_dict, "depth", rpt->currents[idx].depth); |
| 1421 | PyList_SetItem(curr_list, idx, curr_dict); |
| 1422 | } |
| 1423 | } |
| 1424 | break; |
| 1425 | case AIS8_1_26_SENSOR_CURR_3D: |
| 1426 | { |
| 1427 | Ais8_1_26_Curr3D *rpt = |
| 1428 | reinterpret_cast<Ais8_1_26_Curr3D *>(msg.reports[rpt_num]); |
| 1429 | DictSafeSetItem(rpt_dict, "type", rpt->type); |
| 1430 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1431 | |
| 1432 | PyObject *curr_list = PyList_New(3); |
| 1433 | DictSafeSetItem(dict, "currents", curr_list); |
| 1434 | for (size_t idx = 0; idx < 2; idx++) { |
| 1435 | // ERROR: no way to specify negative direction |
| 1436 | PyObject *curr_dict = PyDict_New(); |
| 1437 | PyList_SetItem(curr_list, idx, curr_dict); |
| 1438 | DictSafeSetItem(curr_dict, "north", rpt->currents[idx].north); |
| 1439 | DictSafeSetItem(curr_dict, "east", rpt->currents[idx].east); |
| 1440 | DictSafeSetItem(curr_dict, "up", rpt->currents[idx].up); |
| 1441 | DictSafeSetItem(curr_dict, "depth", rpt->currents[idx].depth); |
| 1442 | } |
| 1443 | } |
| 1444 | break; |
| 1445 | case AIS8_1_26_SENSOR_HORZ_FLOW: |
| 1446 | { |
| 1447 | Ais8_1_26_HorzFlow *rpt = |
| 1448 | reinterpret_cast<Ais8_1_26_HorzFlow *>(msg.reports[rpt_num]); |
| 1449 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1450 | |
| 1451 | PyObject *curr_list = PyList_New(3); |
| 1452 | DictSafeSetItem(dict, "currents", curr_list); |
| 1453 | for (size_t idx = 0; idx < 2; idx++) { |
| 1454 | PyObject *curr_dict = PyDict_New(); |
| 1455 | PyList_SetItem(curr_list, idx, curr_dict); |
| 1456 | DictSafeSetItem(curr_dict, "bearing", rpt->currents[idx].bearing); |
| 1457 | DictSafeSetItem(curr_dict, "dist", rpt->currents[idx].dist); |
| 1458 | DictSafeSetItem(curr_dict, "speed", rpt->currents[idx].speed); |
| 1459 | DictSafeSetItem(curr_dict, "dir", rpt->currents[idx].dir); |
| 1460 | DictSafeSetItem(curr_dict, "level", rpt->currents[idx].level); |
| 1461 | } |
| 1462 | } |
| 1463 | break; |
| 1464 | case AIS8_1_26_SENSOR_SEA_STATE: |
| 1465 | { |
| 1466 | Ais8_1_26_SeaState *rpt = |
| 1467 | reinterpret_cast<Ais8_1_26_SeaState *>(msg.reports[rpt_num]); |
| 1468 | DictSafeSetItem(rpt_dict, "swell_height", rpt->swell_height); |
| 1469 | DictSafeSetItem(rpt_dict, "swell_period", rpt->swell_period); |
| 1470 | DictSafeSetItem(rpt_dict, "swell_dir", rpt->swell_dir); |
| 1471 | DictSafeSetItem(rpt_dict, "sea_state", rpt->sea_state); |
| 1472 | DictSafeSetItem(rpt_dict, "swell_sensor_type", rpt->swell_sensor_type); |
| 1473 | DictSafeSetItem(rpt_dict, "water_temp", rpt->water_temp); |
| 1474 | DictSafeSetItem(rpt_dict, "water_temp_depth", rpt->water_temp_depth); |
| 1475 | DictSafeSetItem(rpt_dict, "water_sensor_type", rpt->water_sensor_type); |
| 1476 | DictSafeSetItem(rpt_dict, "wave_height", rpt->wave_height); |
| 1477 | DictSafeSetItem(rpt_dict, "wave_period", rpt->wave_period); |
| 1478 | DictSafeSetItem(rpt_dict, "wave_dir", rpt->wave_dir); |
| 1479 | DictSafeSetItem(rpt_dict, "wave_sensor_type", rpt->wave_sensor_type); |
| 1480 | DictSafeSetItem(rpt_dict, "salinity", rpt->salinity); |
| 1481 | } |
| 1482 | break; |
| 1483 | case AIS8_1_26_SENSOR_SALINITY: |
| 1484 | { |
| 1485 | Ais8_1_26_Salinity *rpt = |
| 1486 | reinterpret_cast<Ais8_1_26_Salinity *>(msg.reports[rpt_num]); |
| 1487 | DictSafeSetItem(rpt_dict, "water_temp", rpt->water_temp); |
| 1488 | DictSafeSetItem(rpt_dict, "conductivity", rpt->conductivity); |
| 1489 | DictSafeSetItem(rpt_dict, "pressure", rpt->pressure); |
| 1490 | DictSafeSetItem(rpt_dict, "salinity", rpt->salinity); |
| 1491 | DictSafeSetItem(rpt_dict, "salinity_type", rpt->salinity_type); |
| 1492 | DictSafeSetItem(rpt_dict, "sensor_type", rpt->sensor_type); |
| 1493 | DictSafeSetItem(rpt_dict, "spare0", rpt->spare[0]); |
| 1494 | DictSafeSetItem(rpt_dict, "spare1", rpt->spare[1]); |
| 1495 | } |
| 1496 | break; |
| 1497 | case AIS8_1_26_SENSOR_WX: |
| 1498 | { |
| 1499 | Ais8_1_26_Wx *rpt = |
| 1500 | reinterpret_cast<Ais8_1_26_Wx *>(msg.reports[rpt_num]); |
| 1501 | DictSafeSetItem(rpt_dict, "air_temp", rpt->air_temp); |
| 1502 | DictSafeSetItem(rpt_dict, "air_temp_sensor_type", |
| 1503 | rpt->air_temp_sensor_type); |
| 1504 | DictSafeSetItem(rpt_dict, "precip", rpt->precip); |
| 1505 | DictSafeSetItem(rpt_dict, "horz_vis", rpt->horz_vis); |
| 1506 | DictSafeSetItem(rpt_dict, "dew_point", rpt->dew_point); |
| 1507 | DictSafeSetItem(rpt_dict, "dew_point_type", rpt->dew_point_type); |
| 1508 | DictSafeSetItem(rpt_dict, "air_pressure", rpt->air_pressure); |
| 1509 | DictSafeSetItem(rpt_dict, "air_pressure_trend", |
| 1510 | rpt->air_pressure_trend); |
| 1511 | DictSafeSetItem(rpt_dict, "air_pressor_type", rpt->air_pressor_type); |
| 1512 | DictSafeSetItem(rpt_dict, "salinity", rpt->salinity); |
| 1513 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1514 | } |
| 1515 | break; |
| 1516 | case AIS8_1_26_SENSOR_AIR_DRAUGHT: |
| 1517 | { |
| 1518 | Ais8_1_26_AirDraught *rpt = |
| 1519 | reinterpret_cast<Ais8_1_26_AirDraught *>(msg.reports[rpt_num]); |
| 1520 | DictSafeSetItem(rpt_dict, "draught", rpt->draught); |
| 1521 | DictSafeSetItem(rpt_dict, "gap", rpt->gap); |
| 1522 | DictSafeSetItem(rpt_dict, "forecast_gap", rpt->forecast_gap); |
| 1523 | DictSafeSetItem(rpt_dict, "int trend", rpt->trend); |
| 1524 | DictSafeSetItem( |
| 1525 | rpt_dict, "int utc_day_forecast", rpt->utc_day_forecast); |
| 1526 | DictSafeSetItem(rpt_dict, "utc_hour_forecast", rpt->utc_hour_forecast); |
| 1527 | DictSafeSetItem(rpt_dict, "utc_min_forecast", rpt->utc_min_forecast); |
| 1528 | DictSafeSetItem(rpt_dict, "spare", rpt->spare); |
| 1529 | } |
| 1530 | break; |
| 1531 | case AIS8_1_26_SENSOR_RESERVED_11: // FALLTHROUGH |
| 1532 | case AIS8_1_26_SENSOR_RESERVED_12: |
| 1533 | case AIS8_1_26_SENSOR_RESERVED_13: |
| 1534 | case AIS8_1_26_SENSOR_RESERVED_14: |
| 1535 | case AIS8_1_26_SENSOR_RESERVED_15: |
| 1536 | default: |
| 1537 | {} // TODO(schwehr): mark a bad sensor type or raise exception |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | return AIS_OK; |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | // IMO Circ 289 - Route information |
| 1546 | AIS_STATUS |
| 1547 | ais8_1_27_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1548 | const size_t pad) { |
| 1549 | assert(nmea_payload); |
| 1550 | assert(dict); |
| 1551 | assert(pad < 6); |
| 1552 | Ais8_1_27 msg(nmea_payload, pad); |
| 1553 | if (msg.had_error()) { |
| 1554 | return msg.get_error(); |
| 1555 | } |
| 1556 | |
| 1557 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1558 | DictSafeSetItem(dict, "sender_type", msg.sender_type); |
| 1559 | DictSafeSetItem(dict, "route_type", msg.route_type); |
| 1560 | DictSafeSetItem(dict, "utc_month", msg.utc_month); |
| 1561 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 1562 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 1563 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 1564 | DictSafeSetItem(dict, "duration", msg.duration); |
| 1565 | |
| 1566 | PyObject *waypoint_list = PyList_New(msg.waypoints.size()); |
| 1567 | for (size_t point_num = 0; point_num < msg.waypoints.size(); point_num++) { |
| 1568 | PyObject *waypoint = PyList_New(2); |
| 1569 | // TODO(schwehr): Py_DECREF(); ? |
| 1570 | PyList_SetItem( |
| 1571 | waypoint, 0, PyFloat_FromDouble(msg.waypoints[point_num].lng_deg)); |
| 1572 | PyList_SetItem( |
| 1573 | waypoint, 1, PyFloat_FromDouble(msg.waypoints[point_num].lat_deg)); |
| 1574 | PyList_SetItem(waypoint_list, point_num, waypoint); |
| 1575 | } |
| 1576 | PyDict_SetItem(dict, PyUnicode_FromString("waypoints"), waypoint_list); |
| 1577 | |
| 1578 | return AIS_OK; |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | // no 28 broadcast |
| 1583 | |
| 1584 | // IMO Circ 289 - Text description |
| 1585 | AIS_STATUS |
| 1586 | ais8_1_29_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1587 | const size_t pad) { |
| 1588 | assert(nmea_payload); |
| 1589 | assert(dict); |
| 1590 | assert(pad < 6); |
| 1591 | Ais8_1_29 msg(nmea_payload, pad); |
| 1592 | if (msg.had_error()) { |
| 1593 | return msg.get_error(); |
| 1594 | } |
| 1595 | |
| 1596 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1597 | DictSafeSetItem(dict, "text", msg.text); |
| 1598 | |
| 1599 | return AIS_OK; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | // no 30 broadcast |
| 1604 | |
| 1605 | AIS_STATUS |
| 1606 | ais8_1_31_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1607 | const size_t pad) { |
| 1608 | assert(nmea_payload); |
| 1609 | assert(dict); |
| 1610 | assert(pad < 6); |
| 1611 | Ais8_1_31 msg(nmea_payload, pad); |
| 1612 | if (msg.had_error()) { |
| 1613 | return msg.get_error(); |
| 1614 | } |
| 1615 | |
| 1616 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 1617 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 1618 | DictSafeSetItem(dict, "utc_day", msg.utc_day); |
| 1619 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 1620 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 1621 | DictSafeSetItem(dict, "wind_ave", msg.wind_ave); |
| 1622 | DictSafeSetItem(dict, "wind_gust", msg.wind_gust); |
| 1623 | DictSafeSetItem(dict, "wind_dir", msg.wind_dir); |
| 1624 | DictSafeSetItem(dict, "wind_gust_dir", msg.wind_gust_dir); |
| 1625 | DictSafeSetItem(dict, "air_temp", msg.air_temp); |
| 1626 | DictSafeSetItem(dict, "rel_humid", msg.rel_humid); |
| 1627 | DictSafeSetItem(dict, "dew_point", msg.dew_point); |
| 1628 | DictSafeSetItem(dict, "air_pres", msg.air_pres); |
| 1629 | DictSafeSetItem(dict, "air_pres_trend", msg.air_pres_trend); |
| 1630 | DictSafeSetItem(dict, "horz_vis", msg.horz_vis); |
| 1631 | DictSafeSetItem(dict, "water_level", msg.water_level); |
| 1632 | DictSafeSetItem(dict, "water_level_trend", msg.water_level_trend); |
| 1633 | |
| 1634 | // TODO(schwehr): make this a list of dicts |
| 1635 | DictSafeSetItem(dict, "surf_cur_speed", msg.surf_cur_speed); |
| 1636 | DictSafeSetItem(dict, "surf_cur_dir", msg.surf_cur_dir); |
| 1637 | DictSafeSetItem(dict, "cur_speed_2", msg.cur_speed_2); |
| 1638 | DictSafeSetItem(dict, "cur_dir_2", msg.cur_dir_2); |
| 1639 | DictSafeSetItem(dict, "cur_depth_2", msg.cur_depth_2); |
| 1640 | DictSafeSetItem(dict, "cur_speed_3", msg.cur_speed_3); |
| 1641 | DictSafeSetItem(dict, "cur_dir_3", msg.cur_dir_3); |
| 1642 | DictSafeSetItem(dict, "cur_depth_3", msg.cur_depth_3); |
| 1643 | |
| 1644 | DictSafeSetItem(dict, "wave_height", msg.wave_height); |
| 1645 | DictSafeSetItem(dict, "wave_period", msg.wave_period); |
| 1646 | DictSafeSetItem(dict, "wave_dir", msg.wave_dir); |
| 1647 | DictSafeSetItem(dict, "swell_height", msg.swell_height); |
| 1648 | DictSafeSetItem(dict, "swell_period", msg.swell_period); |
| 1649 | DictSafeSetItem(dict, "swell_dir", msg.swell_dir); |
| 1650 | DictSafeSetItem(dict, "sea_state", msg.sea_state); |
| 1651 | DictSafeSetItem(dict, "water_temp", msg.water_temp); |
| 1652 | DictSafeSetItem(dict, "precip_type", msg.precip_type); |
| 1653 | DictSafeSetItem(dict, "salinity", msg.salinity); |
| 1654 | DictSafeSetItem(dict, "ice", msg.ice); |
| 1655 | |
| 1656 | return AIS_OK; |
| 1657 | } |
| 1658 | |
| 1659 | |
| 1660 | // no 32 broadcast |
| 1661 | |
| 1662 | // DAC 200 - River Information System |
| 1663 | // Inland ship static and voyage related data |
| 1664 | AIS_STATUS |
| 1665 | ais8_200_10_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1666 | const size_t pad) { |
| 1667 | assert(nmea_payload); |
| 1668 | assert(dict); |
| 1669 | assert(pad < 6); |
| 1670 | Ais8_200_10 msg(nmea_payload, pad); |
| 1671 | if (msg.had_error()) { |
| 1672 | return msg.get_error(); |
| 1673 | } |
| 1674 | |
| 1675 | DictSafeSetItem(dict, "eu_id", msg.eu_id); |
| 1676 | DictSafeSetItem(dict, "length", msg.length); |
| 1677 | DictSafeSetItem(dict, "beam", msg.beam); |
| 1678 | DictSafeSetItem(dict, "ship_type", msg.ship_type); |
| 1679 | DictSafeSetItem(dict, "haz_cargo", msg.haz_cargo); |
| 1680 | DictSafeSetItem(dict, "draught", msg.draught); |
| 1681 | DictSafeSetItem(dict, "loaded", msg.loaded); |
| 1682 | DictSafeSetItem(dict, "speed_qual", msg.speed_qual); |
| 1683 | DictSafeSetItem(dict, "course_qual", msg.course_qual); |
| 1684 | DictSafeSetItem(dict, "heading_qual", msg.heading_qual); |
| 1685 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1686 | |
| 1687 | return AIS_OK; |
| 1688 | } |
| 1689 | |
| 1690 | // River Information System |
| 1691 | // ETA report |
| 1692 | AIS_STATUS |
| 1693 | ais8_200_21_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1694 | const size_t pad) { |
| 1695 | assert(nmea_payload); |
| 1696 | assert(dict); |
| 1697 | assert(pad < 6); |
| 1698 | Ais8_200_21 msg(nmea_payload, pad); |
| 1699 | if (msg.had_error()) { |
| 1700 | return msg.get_error(); |
| 1701 | } |
| 1702 | |
| 1703 | DictSafeSetItem(dict, "country", msg.country); |
| 1704 | DictSafeSetItem(dict, "location", msg.location); |
| 1705 | DictSafeSetItem(dict, "section", msg.section); |
| 1706 | DictSafeSetItem(dict, "terminal", msg.terminal); |
| 1707 | DictSafeSetItem(dict, "hectometre", msg.hectometre); |
| 1708 | DictSafeSetItem(dict, "eta_month", msg.eta_month); |
| 1709 | DictSafeSetItem(dict, "eta_day", msg.eta_day); |
| 1710 | DictSafeSetItem(dict, "eta_hour", msg.eta_hour); |
| 1711 | DictSafeSetItem(dict, "eta_minute", msg.eta_minute); |
| 1712 | DictSafeSetItem(dict, "tugboats", msg.tugboats); |
| 1713 | DictSafeSetItem(dict, "air_draught", msg.air_draught); |
| 1714 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1715 | |
| 1716 | return AIS_OK; |
| 1717 | } |
| 1718 | |
| 1719 | // River Information System |
| 1720 | // RTA report |
| 1721 | AIS_STATUS |
| 1722 | ais8_200_22_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1723 | const size_t pad) { |
| 1724 | assert(nmea_payload); |
| 1725 | assert(dict); |
| 1726 | assert(pad < 6); |
| 1727 | Ais8_200_22 msg(nmea_payload, pad); |
| 1728 | if (msg.had_error()) { |
| 1729 | return msg.get_error(); |
| 1730 | } |
| 1731 | |
| 1732 | DictSafeSetItem(dict, "country", msg.country); |
| 1733 | DictSafeSetItem(dict, "location", msg.location); |
| 1734 | DictSafeSetItem(dict, "section", msg.section); |
| 1735 | DictSafeSetItem(dict, "terminal", msg.terminal); |
| 1736 | DictSafeSetItem(dict, "hectometre", msg.hectometre); |
| 1737 | DictSafeSetItem(dict, "rta_month", msg.rta_month); |
| 1738 | DictSafeSetItem(dict, "rta_day", msg.rta_day); |
| 1739 | DictSafeSetItem(dict, "rta_hour", msg.rta_hour); |
| 1740 | DictSafeSetItem(dict, "rta_minute", msg.rta_minute); |
| 1741 | DictSafeSetItem(dict, "lock_status", msg.lock_status); |
| 1742 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1743 | |
| 1744 | return AIS_OK; |
| 1745 | } |
| 1746 | |
| 1747 | // River Information System |
| 1748 | // EMMA warning |
| 1749 | AIS_STATUS |
| 1750 | ais8_200_23_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1751 | const size_t pad) { |
| 1752 | assert(nmea_payload); |
| 1753 | assert(dict); |
| 1754 | assert(pad < 6); |
| 1755 | Ais8_200_23 msg(nmea_payload, pad); |
| 1756 | if (msg.had_error()) { |
| 1757 | return msg.get_error(); |
| 1758 | } |
| 1759 | |
| 1760 | DictSafeSetItem(dict, "utc_year_start", msg.utc_year_start); |
| 1761 | DictSafeSetItem(dict, "utc_month_start", msg.utc_month_start); |
| 1762 | DictSafeSetItem(dict, "utc_day_start", msg.utc_day_start); |
| 1763 | DictSafeSetItem(dict, "utc_year_end", msg.utc_year_end); |
| 1764 | DictSafeSetItem(dict, "utc_month_end", msg.utc_month_end); |
| 1765 | DictSafeSetItem(dict, "utc_day_end", msg.utc_day_end); |
| 1766 | DictSafeSetItem(dict, "utc_hour_start", msg.utc_hour_start); |
| 1767 | DictSafeSetItem(dict, "utc_min_start", msg.utc_min_start); |
| 1768 | DictSafeSetItem(dict, "utc_hour_end", msg.utc_hour_end); |
| 1769 | DictSafeSetItem(dict, "utc_min_end", msg.utc_min_end); |
| 1770 | DictSafeSetItem(dict, "x1", "y1", msg.position1); |
| 1771 | DictSafeSetItem(dict, "x2", "y2", msg.position2); |
| 1772 | DictSafeSetItem(dict, "type", msg.type); |
| 1773 | DictSafeSetItem(dict, "min", msg.min); |
| 1774 | DictSafeSetItem(dict, "max", msg.max); |
| 1775 | DictSafeSetItem(dict, "classification", msg.classification); |
| 1776 | DictSafeSetItem(dict, "wind_dir", msg.wind_dir); |
| 1777 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1778 | |
| 1779 | return AIS_OK; |
| 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | // EU River Information System (RIS) |
| 1784 | // Water level |
| 1785 | AIS_STATUS |
| 1786 | ais8_200_24_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1787 | const size_t pad) { |
| 1788 | assert(nmea_payload); |
| 1789 | assert(dict); |
| 1790 | assert(pad < 6); |
| 1791 | Ais8_200_24 msg(nmea_payload, pad); |
| 1792 | if (msg.had_error()) { |
| 1793 | return msg.get_error(); |
| 1794 | } |
| 1795 | |
| 1796 | DictSafeSetItem(dict, "country", msg.country); |
| 1797 | |
| 1798 | PyObject *id_list = PyList_New(4); |
| 1799 | for (size_t i = 0; i < 4; i++) |
| 1800 | PyList_SetItem(id_list, i, PyLong_FromLong(msg.gauge_ids[i])); |
| 1801 | DictSafeSetItem(dict, "gauge_ids", id_list); |
| 1802 | |
| 1803 | PyObject *level_list = PyList_New(4); |
| 1804 | for (size_t i = 0; i < 4; i++) |
| 1805 | PyList_SetItem(level_list, i, PyFloat_FromDouble(msg.levels[i])); |
| 1806 | DictSafeSetItem(dict, "levels", level_list); |
| 1807 | |
| 1808 | return AIS_OK; |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | // River Information System |
| 1813 | // Signal status |
| 1814 | AIS_STATUS |
| 1815 | ais8_200_40_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1816 | const size_t pad) { |
| 1817 | assert(nmea_payload); |
| 1818 | assert(dict); |
| 1819 | assert(pad < 6); |
| 1820 | Ais8_200_40 msg(nmea_payload, pad); |
| 1821 | if (msg.had_error()) { |
| 1822 | return msg.get_error(); |
| 1823 | } |
| 1824 | |
| 1825 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 1826 | DictSafeSetItem(dict, "form", msg.form); |
| 1827 | DictSafeSetItem(dict, "dir", msg.dir); |
| 1828 | DictSafeSetItem(dict, "stream_dir", msg.stream_dir); |
| 1829 | DictSafeSetItem(dict, "status_raw", msg.status_raw); |
| 1830 | // TODO(schwehr): extract status components |
| 1831 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 1832 | |
| 1833 | return AIS_OK; |
| 1834 | } |
| 1835 | |
| 1836 | |
| 1837 | // River Information System |
| 1838 | // Number of persons on board |
| 1839 | AIS_STATUS |
| 1840 | ais8_200_55_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1841 | const size_t pad) { |
| 1842 | assert(nmea_payload); |
| 1843 | assert(dict); |
| 1844 | assert(pad < 6); |
| 1845 | Ais8_200_55 msg(nmea_payload, pad); |
| 1846 | if (msg.had_error()) { |
| 1847 | return msg.get_error(); |
| 1848 | } |
| 1849 | |
| 1850 | DictSafeSetItem(dict, "crew", msg.crew); |
| 1851 | DictSafeSetItem(dict, "passengers", msg.passengers); |
| 1852 | DictSafeSetItem(dict, "yet_more_personnel", msg.yet_more_personnel); |
| 1853 | |
| 1854 | PyObject *spare2_list = PyList_New(3); |
| 1855 | for (size_t i = 0; i < 3; i++) |
| 1856 | PyList_SetItem(spare2_list, 0, PyLong_FromLong(msg.spare2[i])); |
| 1857 | DictSafeSetItem(dict, "spare2", spare2_list); |
| 1858 | |
| 1859 | return AIS_OK; |
| 1860 | } |
| 1861 | |
| 1862 | void |
| 1863 | ais8_367_22_append_pydict(const char *nmea_payload, PyObject *dict, |
| 1864 | const size_t pad) { |
| 1865 | Ais8_367_22 msg(nmea_payload, pad); // TODO(schwehr): check for errors |
| 1866 | |
| 1867 | DictSafeSetItem(dict, "version", msg.version); |
| 1868 | DictSafeSetItem(dict, "link_id", msg.link_id); |
| 1869 | DictSafeSetItem(dict, "notice_type", msg.notice_type); |
| 1870 | // TODO(schwehr): are 8:1:22 and 8:367:22 tables the same? |
| 1871 | DictSafeSetItem(dict, "notice_type_str", |
| 1872 | ais8_1_22_notice_names[msg.notice_type]); |
| 1873 | |
| 1874 | DictSafeSetItem(dict, "month", msg.month); // This is UTC, not local time. |
| 1875 | DictSafeSetItem(dict, "day", msg.day); |
| 1876 | DictSafeSetItem(dict, "hour", msg.hour); |
| 1877 | DictSafeSetItem(dict, "minute", msg.minute); |
| 1878 | |
| 1879 | DictSafeSetItem(dict, "durations_minutes", msg.duration_minutes); |
| 1880 | |
| 1881 | PyObject *sub_area_list = PyList_New(msg.sub_areas.size()); |
| 1882 | |
| 1883 | // Loop over sub_areas |
| 1884 | for (size_t i = 0; i < msg.sub_areas.size(); i++) { |
| 1885 | switch (msg.sub_areas[i]->getType()) { |
| 1886 | case AIS8_366_22_SHAPE_CIRCLE: // or point |
| 1887 | { |
| 1888 | PyObject *sub_area = PyDict_New(); |
| 1889 | Ais8_367_22_Circle *c = |
| 1890 | reinterpret_cast<Ais8_367_22_Circle*>(msg.sub_areas[i]); |
| 1891 | |
| 1892 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_366_22_SHAPE_CIRCLE); |
| 1893 | if (c->radius_m == 0) |
| 1894 | DictSafeSetItem(sub_area, "sub_area_type_str", "point"); |
| 1895 | else |
| 1896 | DictSafeSetItem(sub_area, "sub_area_type_str", "circle"); |
| 1897 | |
| 1898 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1899 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1900 | DictSafeSetItem(sub_area, "radius", c->radius_m); |
| 1901 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1902 | } |
| 1903 | break; |
| 1904 | case AIS8_366_22_SHAPE_RECT: |
| 1905 | { |
| 1906 | PyObject *sub_area = PyDict_New(); |
| 1907 | Ais8_367_22_Rect *c = |
| 1908 | reinterpret_cast<Ais8_367_22_Rect*>(msg.sub_areas[i]); |
| 1909 | |
| 1910 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_366_22_SHAPE_RECT); |
| 1911 | DictSafeSetItem(sub_area, "sub_area_type_str", "rect"); |
| 1912 | |
| 1913 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1914 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1915 | DictSafeSetItem(sub_area, "e_dim_m", c->e_dim_m); |
| 1916 | DictSafeSetItem(sub_area, "n_dim_m", c->n_dim_m); |
| 1917 | DictSafeSetItem(sub_area, "orient_deg", c->orient_deg); |
| 1918 | |
| 1919 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1920 | } |
| 1921 | break; |
| 1922 | case AIS8_366_22_SHAPE_SECTOR: |
| 1923 | { |
| 1924 | PyObject *sub_area = PyDict_New(); |
| 1925 | Ais8_367_22_Sector *c = |
| 1926 | reinterpret_cast<Ais8_367_22_Sector*>(msg.sub_areas[i]); |
| 1927 | |
| 1928 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_366_22_SHAPE_SECTOR); |
| 1929 | DictSafeSetItem(sub_area, "sub_area_type_str", "sector"); |
| 1930 | |
| 1931 | DictSafeSetItem(sub_area, "x", "y", c->position); |
| 1932 | DictSafeSetItem(sub_area, "precision", c->precision); |
| 1933 | DictSafeSetItem(sub_area, "radius", c->radius_m); |
| 1934 | DictSafeSetItem(sub_area, "left_bound_deg", c->left_bound_deg); |
| 1935 | DictSafeSetItem(sub_area, "right_bound_deg", c->right_bound_deg); |
| 1936 | |
| 1937 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1938 | } |
| 1939 | break; |
| 1940 | case AIS8_366_22_SHAPE_POLYLINE: // FALLTHROUGH |
| 1941 | case AIS8_366_22_SHAPE_POLYGON: |
| 1942 | { |
| 1943 | PyObject *sub_area = PyDict_New(); |
| 1944 | Ais8_367_22_Poly *poly = |
| 1945 | reinterpret_cast<Ais8_367_22_Poly*>(msg.sub_areas[i]); |
| 1946 | |
| 1947 | DictSafeSetItem(sub_area, "sub_area_type", msg.sub_areas[i]->getType()); |
| 1948 | if (msg.sub_areas[i]->getType() == AIS8_366_22_SHAPE_POLYLINE) |
| 1949 | DictSafeSetItem(sub_area, "sub_area_type_str", "polyline"); |
| 1950 | else |
| 1951 | DictSafeSetItem(sub_area, "sub_area_type_str", "polygon"); |
| 1952 | assert(poly->angles.size() == poly->dists_m.size()); |
| 1953 | PyObject *angle_list = PyList_New(poly->angles.size()); |
| 1954 | PyObject *dist_list = PyList_New(poly->angles.size()); |
| 1955 | |
| 1956 | for (size_t pt_num = 0; pt_num < poly->angles.size(); pt_num++) { |
| 1957 | PyList_SetItem(angle_list, pt_num, |
| 1958 | PyFloat_FromDouble(poly->angles[pt_num])); |
| 1959 | PyList_SetItem(dist_list, pt_num, |
| 1960 | PyFloat_FromDouble(poly->dists_m[pt_num])); |
| 1961 | } |
| 1962 | |
| 1963 | DictSafeSetItem(sub_area, "angles", angle_list); |
| 1964 | DictSafeSetItem(sub_area, "dists_m", dist_list); |
| 1965 | |
| 1966 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1967 | } |
| 1968 | break; |
| 1969 | case AIS8_366_22_SHAPE_TEXT: |
| 1970 | { |
| 1971 | PyObject *sub_area = PyDict_New(); |
| 1972 | |
| 1973 | Ais8_367_22_Text *text = |
| 1974 | reinterpret_cast<Ais8_367_22_Text*>(msg.sub_areas[i]); |
| 1975 | DictSafeSetItem(sub_area, "sub_area_type", AIS8_366_22_SHAPE_TEXT); |
| 1976 | DictSafeSetItem(sub_area, "sub_area_type_str", "text"); |
| 1977 | |
| 1978 | DictSafeSetItem(sub_area, "text", text->text); |
| 1979 | |
| 1980 | PyList_SetItem(sub_area_list, i, sub_area); |
| 1981 | } |
| 1982 | break; |
| 1983 | |
| 1984 | default: |
| 1985 | {} // TODO(schwehr): Mark an unknown subarea or raise an exception. |
| 1986 | } |
| 1987 | } |
| 1988 | DictSafeSetItem(dict, "sub_areas", sub_area_list); |
| 1989 | } |
| 1990 | |
| 1991 | // AIS Binary broadcast messages. There will be a huge number of subtypes |
| 1992 | // If we don't know how to decode it, just return the dac, fi |
| 1993 | PyObject* |
| 1994 | ais8_to_pydict(const char *nmea_payload, const size_t pad) { |
| 1995 | assert(nmea_payload); |
| 1996 | assert(pad < 6); |
| 1997 | |
| 1998 | Ais8 msg(nmea_payload, pad); |
| 1999 | if (msg.had_error() && msg.get_error() != AIS_UNINITIALIZED) { |
| 2000 | PyErr_Format(ais_py_exception, "Ais8: %s", |
| 2001 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2002 | return nullptr; |
| 2003 | } |
| 2004 | |
| 2005 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2006 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2007 | DictSafeSetItem(dict, "dac", msg.dac); |
| 2008 | DictSafeSetItem(dict, "fi", msg.fi); |
| 2009 | |
| 2010 | AIS_STATUS status = AIS_UNINITIALIZED; |
| 2011 | |
| 2012 | switch (msg.dac) { |
| 2013 | case AIS_DAC_1_INTERNATIONAL: // IMO. |
| 2014 | switch (msg.fi) { |
| 2015 | // See: ITU-R.M.1371-3 IFM messages Annex 5, Section 5 and IMO Circ 289 |
| 2016 | case AIS_FI_8_1_0_TEXT: |
| 2017 | status = ais8_1_0_append_pydict(nmea_payload, dict, pad); |
| 2018 | break; |
| 2019 | case AIS_FI_8_1_11_MET_HYDRO: // Not to be used after 1 Jan 2013. |
| 2020 | status = ais8_1_11_append_pydict(nmea_payload, dict, pad); |
| 2021 | break; |
| 2022 | case AIS_FI_8_1_13_FAIRWAY_CLOSED: // Not to be used after 1 Jan 2013. |
| 2023 | // TODO(schwehr): untested - no messages found |
| 2024 | status = ais8_1_13_append_pydict(nmea_payload, dict, pad); |
| 2025 | break; |
| 2026 | case AIS_FI_8_1_15_SHIP_AND_VOYAGE: // Not after 1 Jan 2013. |
| 2027 | // TODO(schwehr): untested - no messages found |
| 2028 | status = ais8_1_15_append_pydict(nmea_payload, dict, pad); |
| 2029 | break; |
| 2030 | // 16 has conflicting definition in the old 1371-1: VTS targets. |
| 2031 | case AIS_FI_8_1_16_PERSONS_ON_BOARD: // Not to be used after 1 Jan 2013. |
| 2032 | status = ais8_1_16_append_pydict(nmea_payload, dict, pad); |
| 2033 | break; |
| 2034 | // 17 has conflicting definitions in 1371-1: IFM 17: Ship waypoints |
| 2035 | // and/or route plan report. |
| 2036 | case AIS_FI_8_1_17_VTS_TARGET: // Not to be used after Jan 2013. |
| 2037 | // TODO(schwehr): Untested. no messages found. |
| 2038 | status = ais8_1_17_append_pydict(nmea_payload, dict, pad); |
| 2039 | break; |
| 2040 | // ITU 1371-1 conflict: IFM 19: Extended ship static and voyage. |
| 2041 | case AIS_FI_8_1_19_TRAFFIC_SIGNAL: |
| 2042 | // TODO(schwehr): Untested. No messages found. |
| 2043 | status = ais8_1_19_append_pydict(nmea_payload, dict, pad); |
| 2044 | DictSafeSetItem(dict, "parsed", true); |
| 2045 | break; |
| 2046 | // 20: Berthing data. |
| 2047 | case AIS_FI_8_1_21_WEATHER_OBS: |
| 2048 | // TODO(schwehr): untested - no messages found |
| 2049 | status = ais8_1_21_append_pydict(nmea_payload, dict, pad); |
| 2050 | break; |
| 2051 | case AIS_FI_8_1_22_AREA_NOTICE: |
| 2052 | status = ais8_1_22_append_pydict(nmea_payload, dict, pad); |
| 2053 | break; |
| 2054 | case AIS_FI_8_1_24_SHIP_AND_VOYAGE: |
| 2055 | status = ais8_1_24_append_pydict(nmea_payload, dict, pad); |
| 2056 | break; |
| 2057 | case AIS_FI_8_1_26_SENSOR: |
| 2058 | status = ais8_1_26_append_pydict(nmea_payload, dict, pad); |
| 2059 | break; |
| 2060 | case AIS_FI_8_1_27_ROUTE: |
| 2061 | status = ais8_1_27_append_pydict(nmea_payload, dict, pad); |
| 2062 | break; |
| 2063 | case AIS_FI_8_1_29_TEXT: |
| 2064 | status = ais8_1_29_append_pydict(nmea_payload, dict, pad); |
| 2065 | break; |
| 2066 | case AIS_FI_8_1_31_MET_HYDRO: |
| 2067 | status = ais8_1_31_append_pydict(nmea_payload, dict, pad); |
| 2068 | break; |
| 2069 | // ITU 1371-1 only: 3.10 - IFM 40: Number of persons on board. |
| 2070 | default: |
| 2071 | DictSafeSetItem(dict, "parsed", false); |
| 2072 | break; |
| 2073 | } |
| 2074 | break; |
| 2075 | case AIS_DAC_200_RIS: |
| 2076 | switch (msg.fi) { |
| 2077 | case AIS_FI_8_200_10_RIS_SHIP_AND_VOYAGE: |
| 2078 | status = ais8_200_10_append_pydict(nmea_payload, dict, pad); |
| 2079 | break; |
| 2080 | case AIS_FI_8_200_21_RIS_ETA_AT_LOCK_BRIDGE_TERMINAL: |
| 2081 | status = ais8_200_21_append_pydict(nmea_payload, dict, pad); |
| 2082 | break; |
| 2083 | case AIS_FI_8_200_22_RIS_RTA_AT_LOCK_BRIDGE_TERMINAL: |
| 2084 | status = ais8_200_22_append_pydict(nmea_payload, dict, pad); |
| 2085 | break; |
| 2086 | case AIS_FI_8_200_23_RIS_EMMA_WARNING: |
| 2087 | status = ais8_200_23_append_pydict(nmea_payload, dict, pad); |
| 2088 | break; |
| 2089 | case AIS_FI_8_1_24_SHIP_AND_VOYAGE: |
| 2090 | status = ais8_200_24_append_pydict(nmea_payload, dict, pad); |
| 2091 | break; |
| 2092 | case AIS_FI_8_200_40_RIS_ATON_SIGNAL_STATUS: |
| 2093 | status = ais8_200_40_append_pydict(nmea_payload, dict, pad); |
| 2094 | break; |
| 2095 | case AIS_FI_8_200_55_RIS_PERSONS_ON_BOARD: |
| 2096 | status = ais8_200_55_append_pydict(nmea_payload, dict, pad); |
| 2097 | break; |
| 2098 | default: |
| 2099 | DictSafeSetItem(dict, "parsed", false); |
| 2100 | break; |
| 2101 | } |
| 2102 | break; |
| 2103 | // TODO(schwehr): AIS_FI_8_366_22_AREA_NOTICE. |
| 2104 | case 367: // United states. |
| 2105 | switch (msg.fi) { |
| 2106 | case 22: // USCG Area Notice 2012 (v5?). |
| 2107 | ais8_367_22_append_pydict(nmea_payload, dict, pad); |
| 2108 | break; |
| 2109 | default: |
| 2110 | DictSafeSetItem(dict, "parsed", false); |
| 2111 | break; |
| 2112 | } |
| 2113 | break; |
| 2114 | default: |
| 2115 | DictSafeSetItem(dict, "parsed", false); |
| 2116 | // TODO(schwehr): raise exception or return standin? |
| 2117 | } |
| 2118 | |
| 2119 | if (status != AIS_OK) { |
| 2120 | PyErr_Format(ais_py_exception, "Ais8: %s", |
| 2121 | AIS_STATUS_STRINGS[status]); |
| 2122 | return nullptr; |
| 2123 | } |
| 2124 | |
| 2125 | return dict; |
| 2126 | } |
| 2127 | |
| 2128 | // Aircraft position report |
| 2129 | PyObject* |
| 2130 | ais9_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2131 | assert(nmea_payload); |
| 2132 | Ais9 msg(nmea_payload, pad); |
| 2133 | if (msg.had_error()) { |
| 2134 | PyErr_Format(ais_py_exception, "Ais9: %s", |
| 2135 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2136 | return nullptr; |
| 2137 | } |
| 2138 | |
| 2139 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2140 | |
| 2141 | DictSafeSetItem(dict, "alt", msg.alt); |
| 2142 | DictSafeSetItem(dict, "sog", msg.sog); |
| 2143 | |
| 2144 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 2145 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2146 | |
| 2147 | DictSafeSetItem(dict, "cog", msg.cog); |
| 2148 | DictSafeSetItem(dict, "timestamp", msg.timestamp); |
| 2149 | DictSafeSetItem(dict, "alt_sensor", msg.alt_sensor); |
| 2150 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2151 | DictSafeSetItem(dict, "dte", msg.dte); |
| 2152 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2153 | DictSafeSetItem(dict, "assigned_mode", msg.assigned_mode); |
| 2154 | DictSafeSetItem(dict, "raim", msg.raim); |
| 2155 | |
| 2156 | DictSafeSetItem(dict, "sync_state", msg.sync_state); |
| 2157 | |
| 2158 | if (0 == msg.commstate_flag) { |
| 2159 | // SOTMDA |
| 2160 | DictSafeSetItem(dict, "slot_timeout", msg.slot_timeout); |
| 2161 | |
| 2162 | if (msg.received_stations_valid) |
| 2163 | DictSafeSetItem(dict, "received_stations", msg.received_stations); |
| 2164 | if (msg.slot_number_valid) |
| 2165 | DictSafeSetItem(dict, "slot_number", msg.slot_number); |
| 2166 | if (msg.utc_valid) { |
| 2167 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 2168 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 2169 | DictSafeSetItem(dict, "utc_spare", msg.utc_spare); |
| 2170 | } |
| 2171 | if (msg.slot_offset_valid) |
| 2172 | DictSafeSetItem(dict, "slot_offset", msg.slot_offset); |
| 2173 | } else { |
| 2174 | // ITDMA |
| 2175 | DictSafeSetItem(dict, "slot_increment", msg.slot_increment); |
| 2176 | DictSafeSetItem(dict, "slots_to_allocate", msg.slots_to_allocate); |
| 2177 | DictSafeSetItem(dict, "keep_flag", msg.keep_flag); |
| 2178 | } |
| 2179 | |
| 2180 | return dict; |
| 2181 | } |
| 2182 | |
| 2183 | // 10 - ':' - UTC and date inquiry |
| 2184 | PyObject* |
| 2185 | ais10_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2186 | assert(nmea_payload); |
| 2187 | assert(pad < 6); |
| 2188 | |
| 2189 | Ais10 msg(nmea_payload, pad); |
| 2190 | if (msg.had_error()) { |
| 2191 | PyErr_Format(ais_py_exception, "Ais10: %s", |
| 2192 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2193 | return nullptr; |
| 2194 | } |
| 2195 | |
| 2196 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2197 | |
| 2198 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2199 | DictSafeSetItem(dict, "dest_mmsi", msg.dest_mmsi); |
| 2200 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2201 | |
| 2202 | return dict; |
| 2203 | } |
| 2204 | |
| 2205 | // msg 11 ';' - See msg 4_11 |
| 2206 | |
| 2207 | // 12 - '<' - Addressed safety related text |
| 2208 | PyObject* |
| 2209 | ais12_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2210 | assert(nmea_payload); |
| 2211 | Ais12 msg(nmea_payload, pad); |
| 2212 | if (msg.had_error()) { |
| 2213 | PyErr_Format(ais_py_exception, "Ais12: %s", |
| 2214 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2215 | return nullptr; |
| 2216 | } |
| 2217 | |
| 2218 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2219 | |
| 2220 | DictSafeSetItem(dict, "seq_num", msg.seq_num); |
| 2221 | DictSafeSetItem(dict, "dest_mmsi", msg.dest_mmsi); |
| 2222 | DictSafeSetItem(dict, "retransmitted", msg.retransmitted); |
| 2223 | |
| 2224 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2225 | DictSafeSetItem(dict, "text", msg.text); |
| 2226 | |
| 2227 | return dict; |
| 2228 | } |
| 2229 | |
| 2230 | // msg 13 - See msg 7 |
| 2231 | |
| 2232 | // 14 - '>' - Safety broadcast text |
| 2233 | PyObject* |
| 2234 | ais14_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2235 | assert(nmea_payload); |
| 2236 | Ais14 msg(nmea_payload, pad); |
| 2237 | |
| 2238 | if (msg.had_error()) { |
| 2239 | PyErr_Format(ais_py_exception, "Ais14: %s", |
| 2240 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2241 | return nullptr; |
| 2242 | } |
| 2243 | |
| 2244 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2245 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2246 | DictSafeSetItem(dict, "text", msg.text); |
| 2247 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2248 | |
| 2249 | return dict; |
| 2250 | } |
| 2251 | |
| 2252 | // 15 - '?' - Interrogation |
| 2253 | PyObject* |
| 2254 | ais15_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2255 | assert(nmea_payload); |
| 2256 | Ais15 msg(nmea_payload, pad); |
| 2257 | |
| 2258 | if (msg.had_error()) { |
| 2259 | PyErr_Format(ais_py_exception, "Ais15: %s", |
| 2260 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2261 | return nullptr; |
| 2262 | } |
| 2263 | |
| 2264 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2265 | |
| 2266 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2267 | DictSafeSetItem(dict, "mmsi_1", msg.mmsi_1); |
| 2268 | DictSafeSetItem(dict, "msg_1_1", msg.msg_1_1); |
| 2269 | DictSafeSetItem(dict, "slot_offset_1_1", msg.slot_offset_1_1); |
| 2270 | |
| 2271 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2272 | DictSafeSetItem(dict, "dest_msg_1_2", msg.dest_msg_1_2); |
| 2273 | DictSafeSetItem(dict, "slot_offset_1_2", msg.slot_offset_1_2); |
| 2274 | |
| 2275 | DictSafeSetItem(dict, "spare3", msg.spare3); |
| 2276 | DictSafeSetItem(dict, "mmsi_2", msg.mmsi_2); |
| 2277 | DictSafeSetItem(dict, "msg_2", msg.msg_2); |
| 2278 | DictSafeSetItem(dict, "slot_offset_2", msg.slot_offset_2); |
| 2279 | DictSafeSetItem(dict, "spare4", msg.spare4); |
| 2280 | |
| 2281 | return dict; |
| 2282 | } |
| 2283 | |
| 2284 | // 16 - '@' - Assigned mode command |
| 2285 | PyObject* |
| 2286 | ais16_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2287 | assert(nmea_payload); |
| 2288 | Ais16 msg(nmea_payload, pad); |
| 2289 | |
| 2290 | if (msg.had_error()) { |
| 2291 | PyErr_Format(ais_py_exception, "Ais16: %s", |
| 2292 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2293 | return nullptr; |
| 2294 | } |
| 2295 | |
| 2296 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2297 | |
| 2298 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2299 | DictSafeSetItem(dict, "dest_mmsi_a", msg.dest_mmsi_a); |
| 2300 | DictSafeSetItem(dict, "offset_a", msg.offset_a); |
| 2301 | DictSafeSetItem(dict, "inc_a", msg.inc_a); |
| 2302 | |
| 2303 | if (-1 != msg.spare2) DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2304 | if (-1 != msg.dest_mmsi_b) { |
| 2305 | DictSafeSetItem(dict, "dest_mmsi_b", msg.dest_mmsi_b); |
| 2306 | DictSafeSetItem(dict, "offset_b", msg.offset_b); |
| 2307 | DictSafeSetItem(dict, "inc_b", msg.inc_b); |
| 2308 | } |
| 2309 | |
| 2310 | return dict; |
| 2311 | } |
| 2312 | |
| 2313 | // 17 - 'A' - GNSS differential - TODO(schwehr): incomplete |
| 2314 | PyObject* |
| 2315 | ais17_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2316 | assert(nmea_payload); |
| 2317 | Ais17 msg(nmea_payload, pad); |
| 2318 | |
| 2319 | if (msg.had_error()) { |
| 2320 | PyErr_Format(ais_py_exception, "Ais17: %s", |
| 2321 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2322 | return nullptr; |
| 2323 | } |
| 2324 | |
| 2325 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2326 | |
| 2327 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2328 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2329 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2330 | |
| 2331 | return dict; |
| 2332 | } |
| 2333 | |
| 2334 | // 18 - 'B' - Class B position report. |
| 2335 | PyObject* |
| 2336 | ais18_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2337 | assert(nmea_payload); |
| 2338 | Ais18 msg(nmea_payload, pad); |
| 2339 | if (msg.had_error()) { |
| 2340 | PyErr_Format(ais_py_exception, "Ais18: %s", |
| 2341 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2342 | return nullptr; |
| 2343 | } |
| 2344 | |
| 2345 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2346 | |
| 2347 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2348 | DictSafeSetItem(dict, "sog", msg.sog); |
| 2349 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 2350 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2351 | DictSafeSetItem(dict, "cog", msg.cog); |
| 2352 | DictSafeSetItem(dict, "true_heading", msg.true_heading); |
| 2353 | DictSafeSetItem(dict, "timestamp", msg.timestamp); |
| 2354 | |
| 2355 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2356 | |
| 2357 | DictSafeSetItem(dict, "unit_flag", msg.unit_flag); |
| 2358 | DictSafeSetItem(dict, "display_flag", msg.display_flag); |
| 2359 | DictSafeSetItem(dict, "dsc_flag", msg.dsc_flag); |
| 2360 | DictSafeSetItem(dict, "band_flag", msg.band_flag); |
| 2361 | DictSafeSetItem(dict, "m22_flag", msg.m22_flag); |
| 2362 | DictSafeSetItem(dict, "mode_flag", msg.mode_flag); |
| 2363 | |
| 2364 | DictSafeSetItem(dict, "raim", msg.raim); |
| 2365 | |
| 2366 | DictSafeSetItem(dict, "commstate_flag", msg.commstate_flag); |
| 2367 | if (msg.slot_timeout_valid) { |
| 2368 | DictSafeSetItem(dict, "slot_timeout", msg.slot_timeout); |
| 2369 | } |
| 2370 | if (msg.slot_offset_valid) { |
| 2371 | DictSafeSetItem(dict, "slot_offset", msg.slot_offset); |
| 2372 | } |
| 2373 | if (msg.utc_valid) { |
| 2374 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 2375 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 2376 | DictSafeSetItem(dict, "utc_spare", msg.utc_spare); |
| 2377 | } |
| 2378 | if (msg.slot_number_valid) { |
| 2379 | DictSafeSetItem(dict, "slot_number", msg.slot_number); |
| 2380 | } |
| 2381 | if (msg.received_stations_valid) { |
| 2382 | DictSafeSetItem(dict, "received_stations", msg.received_stations); |
| 2383 | } |
| 2384 | |
| 2385 | // ITDMA |
| 2386 | if (msg.slot_increment_valid) { |
| 2387 | DictSafeSetItem(dict, "slot_increment", msg.slot_increment); |
| 2388 | DictSafeSetItem(dict, "slots_to_allocate", msg.slots_to_allocate); |
| 2389 | DictSafeSetItem(dict, "keep_flag", msg.keep_flag); |
| 2390 | } |
| 2391 | |
| 2392 | if (msg.commstate_cs_fill_valid) { |
| 2393 | DictSafeSetItem(dict, "commstate_cs_fill", msg.commstate_cs_fill); |
| 2394 | } |
| 2395 | |
| 2396 | return dict; |
| 2397 | } |
| 2398 | |
| 2399 | // 19 - 'C' - Class B combined position report and ship data |
| 2400 | PyObject* |
| 2401 | ais19_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2402 | assert(nmea_payload); |
| 2403 | Ais19 msg(nmea_payload, pad); |
| 2404 | if (msg.had_error()) { |
| 2405 | PyErr_Format(ais_py_exception, "Ais19: %s", |
| 2406 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2407 | return nullptr; |
| 2408 | } |
| 2409 | |
| 2410 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2411 | |
| 2412 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2413 | DictSafeSetItem(dict, "sog", msg.sog); |
| 2414 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 2415 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2416 | DictSafeSetItem(dict, "cog", msg.cog); |
| 2417 | DictSafeSetItem(dict, "true_heading", msg.true_heading); |
| 2418 | DictSafeSetItem(dict, "timestamp", msg.timestamp); |
| 2419 | |
| 2420 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2421 | |
| 2422 | DictSafeSetItem(dict, "name", msg.name); |
| 2423 | DictSafeSetItem(dict, "type_and_cargo", msg.type_and_cargo); |
| 2424 | DictSafeSetItem(dict, "dim_a", msg.dim_a); |
| 2425 | DictSafeSetItem(dict, "dim_b", msg.dim_b); |
| 2426 | DictSafeSetItem(dict, "dim_c", msg.dim_c); |
| 2427 | DictSafeSetItem(dict, "dim_d", msg.dim_d); |
| 2428 | DictSafeSetItem(dict, "fix_type", msg.fix_type); |
| 2429 | |
| 2430 | DictSafeSetItem(dict, "raim", msg.raim); |
| 2431 | |
| 2432 | DictSafeSetItem(dict, "dte", msg.dte); |
| 2433 | DictSafeSetItem(dict, "assigned_mode", msg.assigned_mode); |
| 2434 | DictSafeSetItem(dict, "spare3", msg.spare3); |
| 2435 | |
| 2436 | return dict; |
| 2437 | } |
| 2438 | |
| 2439 | // 20 - 'D' - data link management |
| 2440 | PyObject* |
| 2441 | ais20_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2442 | assert(nmea_payload); |
| 2443 | Ais20 msg(nmea_payload, pad); |
| 2444 | if (msg.had_error()) { |
| 2445 | PyErr_Format(ais_py_exception, "Ais20: %s", |
| 2446 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2447 | return nullptr; |
| 2448 | } |
| 2449 | |
| 2450 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2451 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2452 | |
| 2453 | int list_size = 1; |
| 2454 | if (msg.group_valid_4) list_size = 4; |
| 2455 | else if (msg.group_valid_3) list_size = 3; |
| 2456 | else if (msg.group_valid_2) list_size = 2; |
| 2457 | |
| 2458 | PyObject *list = PyList_New(list_size); |
| 2459 | |
| 2460 | { |
| 2461 | PyObject *reservation = PyDict_New(); |
| 2462 | DictSafeSetItem(reservation, "offset", msg.offset_1); |
| 2463 | DictSafeSetItem(reservation, "num_slots", msg.num_slots_1); |
| 2464 | DictSafeSetItem(reservation, "timeout", msg.timeout_1); |
| 2465 | DictSafeSetItem(reservation, "incr", msg.incr_1); |
| 2466 | PyList_SetItem(list, 0, reservation); |
| 2467 | } |
| 2468 | |
| 2469 | if (msg.group_valid_2) { |
| 2470 | PyObject *reservation = PyDict_New(); |
| 2471 | DictSafeSetItem(reservation, "offset", msg.offset_2); |
| 2472 | DictSafeSetItem(reservation, "num_slots", msg.num_slots_2); |
| 2473 | DictSafeSetItem(reservation, "timeout", msg.timeout_2); |
| 2474 | DictSafeSetItem(reservation, "incr", msg.incr_2); |
| 2475 | PyList_SetItem(list, 1, reservation); |
| 2476 | } |
| 2477 | |
| 2478 | if (msg.group_valid_3) { |
| 2479 | PyObject *reservation = PyDict_New(); |
| 2480 | DictSafeSetItem(reservation, "offset", msg.offset_3); |
| 2481 | DictSafeSetItem(reservation, "num_slots", msg.num_slots_3); |
| 2482 | DictSafeSetItem(reservation, "timeout", msg.timeout_3); |
| 2483 | DictSafeSetItem(reservation, "incr", msg.incr_3); |
| 2484 | PyList_SetItem(list, 2, reservation); |
| 2485 | } |
| 2486 | |
| 2487 | if (msg.group_valid_4) { |
| 2488 | PyObject *reservation = PyDict_New(); |
| 2489 | DictSafeSetItem(reservation, "offset", msg.offset_4); |
| 2490 | DictSafeSetItem(reservation, "num_slots", msg.num_slots_4); |
| 2491 | DictSafeSetItem(reservation, "timeout", msg.timeout_4); |
| 2492 | DictSafeSetItem(reservation, "incr", msg.incr_4); |
| 2493 | PyList_SetItem(list, 3, reservation); |
| 2494 | } |
| 2495 | |
| 2496 | PyObject * reservations = PyUnicode_FromString("reservations"); |
| 2497 | PyDict_SetItem(dict, reservations, list); |
| 2498 | Py_DECREF(reservations); |
| 2499 | Py_DECREF(list); |
| 2500 | |
| 2501 | return dict; |
| 2502 | } |
| 2503 | |
| 2504 | |
| 2505 | // 21 - 'E' - ATON Aid to Navigation |
| 2506 | PyObject* |
| 2507 | ais21_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2508 | assert(nmea_payload); |
| 2509 | Ais21 msg(nmea_payload, pad); |
| 2510 | if (msg.had_error()) { |
| 2511 | PyErr_Format(ais_py_exception, "Ais21: %s", |
| 2512 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2513 | return nullptr; |
| 2514 | } |
| 2515 | |
| 2516 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2517 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2518 | |
| 2519 | DictSafeSetItem(dict, "aton_type", msg.aton_type); |
| 2520 | DictSafeSetItem(dict, "name", msg.name); |
| 2521 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 2522 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2523 | DictSafeSetItem(dict, "dim_a", msg.dim_a); |
| 2524 | DictSafeSetItem(dict, "dim_b", msg.dim_b); |
| 2525 | DictSafeSetItem(dict, "dim_c", msg.dim_c); |
| 2526 | DictSafeSetItem(dict, "dim_d", msg.dim_d); |
| 2527 | DictSafeSetItem(dict, "fix_type", msg.fix_type); |
| 2528 | DictSafeSetItem(dict, "timestamp", msg.timestamp); |
| 2529 | DictSafeSetItem(dict, "off_pos", msg.off_pos); |
| 2530 | DictSafeSetItem(dict, "aton_status", msg.aton_status); |
| 2531 | DictSafeSetItem(dict, "raim", msg.raim); |
| 2532 | DictSafeSetItem(dict, "virtual_aton", msg.virtual_aton); |
| 2533 | DictSafeSetItem(dict, "assigned_mode", msg.assigned_mode); |
| 2534 | |
| 2535 | return dict; |
| 2536 | } |
| 2537 | |
| 2538 | // 22 - 'F' - Channel mangement |
| 2539 | PyObject* |
| 2540 | ais22_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2541 | assert(nmea_payload); |
| 2542 | Ais22 msg(nmea_payload, pad); |
| 2543 | if (msg.had_error()) { |
| 2544 | PyErr_Format(ais_py_exception, "Ais22: %s", |
| 2545 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2546 | return nullptr; |
| 2547 | } |
| 2548 | |
| 2549 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2550 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2551 | |
| 2552 | DictSafeSetItem(dict, "chan_a", msg.chan_a); |
| 2553 | DictSafeSetItem(dict, "chan_b", msg.chan_b); |
| 2554 | DictSafeSetItem(dict, "txrx_mode", msg.txrx_mode); |
| 2555 | DictSafeSetItem(dict, "power_low", msg.power_low); |
| 2556 | |
| 2557 | if (msg.pos_valid) { |
| 2558 | DictSafeSetItem(dict, "x1", "y1", msg.position1); |
| 2559 | DictSafeSetItem(dict, "x2", "y2", msg.position2); |
| 2560 | } else { |
| 2561 | DictSafeSetItem(dict, "dest_mmsi_1", msg.dest_mmsi_1); |
| 2562 | DictSafeSetItem(dict, "dest_mmsi_2", msg.dest_mmsi_2); |
| 2563 | } |
| 2564 | |
| 2565 | DictSafeSetItem(dict, "chan_a_bandwidth", msg.chan_a_bandwidth); |
| 2566 | DictSafeSetItem(dict, "chan_b_bandwidth", msg.chan_b_bandwidth); |
| 2567 | DictSafeSetItem(dict, "zone_size", msg.zone_size); |
| 2568 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2569 | |
| 2570 | return dict; |
| 2571 | } |
| 2572 | |
| 2573 | |
| 2574 | // 23 - 'F' - Group assignment command |
| 2575 | PyObject* |
| 2576 | ais23_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2577 | assert(nmea_payload); |
| 2578 | Ais23 msg(nmea_payload, pad); |
| 2579 | if (msg.had_error()) { |
| 2580 | PyErr_Format(ais_py_exception, "Ais23: %s", |
| 2581 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2582 | return nullptr; |
| 2583 | } |
| 2584 | |
| 2585 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2586 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2587 | |
| 2588 | DictSafeSetItem(dict, "x1", "y1", msg.position1); |
| 2589 | DictSafeSetItem(dict, "x2", "y2", msg.position2); |
| 2590 | |
| 2591 | DictSafeSetItem(dict, "station_type", msg.station_type); |
| 2592 | DictSafeSetItem(dict, "type_and_cargo", msg.type_and_cargo); |
| 2593 | DictSafeSetItem(dict, "spare2", msg.spare2); |
| 2594 | |
| 2595 | DictSafeSetItem(dict, "txrx_mode", msg.txrx_mode); |
| 2596 | DictSafeSetItem(dict, "interval_raw", msg.interval_raw); |
| 2597 | DictSafeSetItem(dict, "quiet", msg.quiet); |
| 2598 | DictSafeSetItem(dict, "spare3", msg.spare3); |
| 2599 | |
| 2600 | return dict; |
| 2601 | } |
| 2602 | |
| 2603 | // 24 - 'H' - Static data report |
| 2604 | PyObject* |
| 2605 | ais24_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2606 | assert(nmea_payload); |
| 2607 | Ais24 msg(nmea_payload, pad); |
| 2608 | if (msg.had_error()) { |
| 2609 | PyErr_Format(ais_py_exception, "Ais24: %s", |
| 2610 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2611 | return nullptr; |
| 2612 | } |
| 2613 | |
| 2614 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2615 | |
| 2616 | DictSafeSetItem(dict, "part_num", msg.part_num); |
| 2617 | |
| 2618 | switch (msg.part_num) { |
| 2619 | case 0: // Part A |
| 2620 | DictSafeSetItem(dict, "name", msg.name); |
| 2621 | break; |
| 2622 | case 1: // Part B |
| 2623 | DictSafeSetItem(dict, "type_and_cargo", msg.type_and_cargo); |
| 2624 | DictSafeSetItem(dict, "vendor_id", msg.vendor_id); |
| 2625 | DictSafeSetItem(dict, "callsign", msg.callsign); |
| 2626 | DictSafeSetItem(dict, "dim_a", msg.dim_a); |
| 2627 | DictSafeSetItem(dict, "dim_b", msg.dim_b); |
| 2628 | DictSafeSetItem(dict, "dim_c", msg.dim_c); |
| 2629 | DictSafeSetItem(dict, "dim_d", msg.dim_d); |
| 2630 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2631 | break; |
| 2632 | case 2: // FALLTHROUGH - not yet defined by ITU |
| 2633 | case 3: // FALLTHROUGH - not yet defined by ITU |
| 2634 | default: |
| 2635 | // status = AIS_ERR_BAD_MSG_CONTENT; |
| 2636 | // TODO(schwehr): setup python exception |
| 2637 | return nullptr; |
| 2638 | } |
| 2639 | |
| 2640 | return dict; |
| 2641 | } |
| 2642 | |
| 2643 | // 25 - 'I' - Single slot binary message |
| 2644 | PyObject* |
| 2645 | ais25_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2646 | assert(nmea_payload); |
| 2647 | Ais25 msg(nmea_payload, pad); |
| 2648 | if (msg.had_error()) { |
| 2649 | PyErr_Format(ais_py_exception, "Ais25: %s", |
| 2650 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2651 | return nullptr; |
| 2652 | } |
| 2653 | |
| 2654 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2655 | |
| 2656 | // TODO(schwehr) use_app_id |
| 2657 | if (msg.dest_mmsi_valid) DictSafeSetItem(dict, "dest_mmsi", msg.dest_mmsi); |
| 2658 | if (msg.use_app_id) { |
| 2659 | DictSafeSetItem(dict, "dac", msg.dac); |
| 2660 | DictSafeSetItem(dict, "fi", msg.fi); |
| 2661 | } |
| 2662 | |
| 2663 | // TODO(schwehr): handle payload |
| 2664 | |
| 2665 | return dict; |
| 2666 | } |
| 2667 | |
| 2668 | // 26 - 'J' - Multi-slot binary message with commstate |
| 2669 | PyObject* |
| 2670 | ais26_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2671 | Ais26 msg(nmea_payload, pad); |
| 2672 | if (msg.had_error()) { |
| 2673 | PyErr_Format(ais_py_exception, "Ais26: %s", |
| 2674 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2675 | return nullptr; |
| 2676 | } |
| 2677 | |
| 2678 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2679 | |
| 2680 | if (msg.dest_mmsi_valid) DictSafeSetItem(dict, "dest_mmsi", msg.dest_mmsi); |
| 2681 | if (msg.use_app_id) { |
| 2682 | DictSafeSetItem(dict, "dac", msg.dac); |
| 2683 | DictSafeSetItem(dict, "fi", msg.fi); |
| 2684 | } |
| 2685 | |
| 2686 | // TODO(schwehr): handle payload |
| 2687 | |
| 2688 | DictSafeSetItem(dict, "sync_state", msg.sync_state); |
| 2689 | if (0 == msg.commstate_flag) { |
| 2690 | // SOTDMA |
| 2691 | if (msg.received_stations_valid) |
| 2692 | DictSafeSetItem(dict, "received_stations", msg.received_stations); |
| 2693 | if (msg.slot_number_valid) |
| 2694 | DictSafeSetItem(dict, "slot_number", msg.slot_number); |
| 2695 | if (msg.utc_valid) { |
| 2696 | DictSafeSetItem(dict, "utc_hour", msg.utc_hour); |
| 2697 | DictSafeSetItem(dict, "utc_min", msg.utc_min); |
| 2698 | DictSafeSetItem(dict, "utc_spare", msg.utc_spare); |
| 2699 | } |
| 2700 | if (msg.slot_offset_valid) |
| 2701 | DictSafeSetItem(dict, "slot_offset", msg.slot_offset); |
| 2702 | } else { |
| 2703 | // ITDMA |
| 2704 | DictSafeSetItem(dict, "slot_increment", msg.slot_increment); |
| 2705 | DictSafeSetItem(dict, "slots_to_allocate", msg.slots_to_allocate); |
| 2706 | DictSafeSetItem(dict, "keep_flag", msg.keep_flag); |
| 2707 | } |
| 2708 | return dict; |
| 2709 | } |
| 2710 | |
| 2711 | |
| 2712 | // 27 - 'K' - Short position report for satellite reception |
| 2713 | PyObject* |
| 2714 | ais27_to_pydict(const char *nmea_payload, const size_t pad) { |
| 2715 | Ais27 msg(nmea_payload, pad); |
| 2716 | if (msg.had_error()) { |
| 2717 | PyErr_Format(ais_py_exception, "Ais27: %s", |
| 2718 | AIS_STATUS_STRINGS[msg.get_error()]); |
| 2719 | return nullptr; |
| 2720 | } |
| 2721 | |
| 2722 | PyObject *dict = ais_msg_to_pydict(&msg); |
| 2723 | |
| 2724 | DictSafeSetItem(dict, "position_accuracy", msg.position_accuracy); |
| 2725 | DictSafeSetItem(dict, "raim", msg.raim); |
| 2726 | DictSafeSetItem(dict, "nav_status", msg.nav_status); |
| 2727 | DictSafeSetItem(dict, "x", "y", msg.position); |
| 2728 | DictSafeSetItem(dict, "sog", msg.sog); |
| 2729 | DictSafeSetItem(dict, "cog", msg.cog); |
| 2730 | DictSafeSetItem(dict, "gnss", msg.gnss); |
| 2731 | DictSafeSetItem(dict, "spare", msg.spare); |
| 2732 | return dict; |
| 2733 | } |
| 2734 | |
| 2735 | |
| 2736 | extern "C" { |
| 2737 | |
| 2738 | static PyObject * |
| 2739 | decode(PyObject *self, PyObject *args) { |
| 2740 | int _pad; |
| 2741 | const char *nmea_payload; |
| 2742 | // TODO(schwehr): what to do about if no pad bits? Maybe warn and set to 0? |
| 2743 | if (!PyArg_ParseTuple(args, "si", &nmea_payload, &_pad)) { |
| 2744 | _pad = 0; |
| 2745 | if (!PyArg_ParseTuple(args, "s", &nmea_payload)) { |
| 2746 | PyErr_Format(ais_py_exception, "ais.decode: expected (str, int)"); |
| 2747 | return nullptr; |
| 2748 | } |
| 2749 | } |
| 2750 | const size_t pad = _pad; |
| 2751 | |
| 2752 | // The grand dispatcher |
| 2753 | switch (nmea_payload[0]) { |
| 2754 | case '1': // FALLTHROUGH - Class A Position |
| 2755 | case '2': // FALLTHROUGH |
| 2756 | case '3': |
| 2757 | return ais1_2_3_to_pydict(nmea_payload, pad); |
| 2758 | |
| 2759 | case '4': // FALLTHROUGH - 4 - Basestation report |
| 2760 | case ';': // 11 - UTC date response |
| 2761 | return ais4_11_to_pydict(nmea_payload, pad); |
| 2762 | |
| 2763 | case '5': // 5 - Ship and Cargo |
| 2764 | return ais5_to_pydict(nmea_payload, pad); |
| 2765 | |
| 2766 | case '6': // 6 - Addressed binary message |
| 2767 | return ais6_to_pydict(nmea_payload, pad); |
| 2768 | |
| 2769 | case '7': // FALLTHROUGH - 7 - ACK for addressed binary message |
| 2770 | case '=': // 13 - ASRM Ack (safety message) |
| 2771 | return ais7_13_to_pydict(nmea_payload, pad); |
| 2772 | |
| 2773 | case '8': // 8 - Binary broadcast message (BBM) |
| 2774 | return ais8_to_pydict(nmea_payload, pad); |
| 2775 | |
| 2776 | case '9': // 9 - SAR Position |
| 2777 | return ais9_to_pydict(nmea_payload, pad); |
| 2778 | |
| 2779 | case ':': // 10 - UTC Query |
| 2780 | return ais10_to_pydict(nmea_payload, pad); |
| 2781 | |
| 2782 | // ';' 11 - See 4 |
| 2783 | |
| 2784 | case '<': // 12 - ASRM |
| 2785 | return ais12_to_pydict(nmea_payload, pad); |
| 2786 | |
| 2787 | // 13 - See 7 |
| 2788 | |
| 2789 | case '>': // 14 - SRBM - Safety broadcast |
| 2790 | return ais14_to_pydict(nmea_payload, pad); |
| 2791 | |
| 2792 | case '?': // 15 - Interrogation |
| 2793 | return ais15_to_pydict(nmea_payload, pad); |
| 2794 | |
| 2795 | case '@': // 16 - Assigned mode command |
| 2796 | return ais16_to_pydict(nmea_payload, pad); |
| 2797 | |
| 2798 | case 'A': // 17 - GNSS broadcast |
| 2799 | return ais17_to_pydict(nmea_payload, pad); |
| 2800 | |
| 2801 | case 'B': // 18 - Position, Class B |
| 2802 | return ais18_to_pydict(nmea_payload, pad); |
| 2803 | |
| 2804 | case 'C': // 19 - Position and ship, Class B |
| 2805 | return ais19_to_pydict(nmea_payload, pad); |
| 2806 | |
| 2807 | case 'D': // 20 - Data link management |
| 2808 | return ais20_to_pydict(nmea_payload, pad); |
| 2809 | |
| 2810 | case 'E': // 21 - Aids to navigation report |
| 2811 | return ais21_to_pydict(nmea_payload, pad); |
| 2812 | |
| 2813 | case 'F': // 22 - Channel Management |
| 2814 | return ais22_to_pydict(nmea_payload, pad); |
| 2815 | |
| 2816 | case 'G': // 23 - Group Assignment Command |
| 2817 | return ais23_to_pydict(nmea_payload, pad); |
| 2818 | |
| 2819 | case 'H': // 24 - Static data report |
| 2820 | return ais24_to_pydict(nmea_payload, pad); |
| 2821 | |
| 2822 | case 'I': // 25 - Single slot binary message - addressed or broadcast |
| 2823 | // TODO(schwehr): handle payloads |
| 2824 | return ais25_to_pydict(nmea_payload, pad); |
| 2825 | |
| 2826 | case 'J': // 26 - Multi slot binary message with comm state |
| 2827 | return ais26_to_pydict(nmea_payload, pad); // TODO(schwehr): payloads |
| 2828 | |
| 2829 | case 'K': // 27 - Long-range AIS broadcast message |
| 2830 | return ais27_to_pydict(nmea_payload, pad); |
| 2831 | |
| 2832 | case 'L': // 28 - UNKNOWN |
| 2833 | PyErr_Format(ais_py_exception, "ais.decode: message 28 (L) not handled"); |
| 2834 | break; |
| 2835 | |
| 2836 | default: |
| 2837 | PyErr_Format(ais_py_exception, "ais.decode: unknown message - %c", |
| 2838 | nmea_payload[0]); |
| 2839 | } |
| 2840 | |
| 2841 | return nullptr; |
| 2842 | } |
| 2843 | |
| 2844 | static PyMethodDef ais_methods[] = { |
| 2845 | {"decode", decode, METH_VARARGS, "Return a dictionary for a NMEA string"}, |
| 2846 | {nullptr, nullptr, 0, nullptr}, // Sentinel |
| 2847 | }; |
| 2848 | |
| 2849 | // Python module initialization |
| 2850 | |
| 2851 | static struct PyModuleDef aismodule = { |
| 2852 | PyModuleDef_HEAD_INIT, |
| 2853 | "_ais", // Module name. |
| 2854 | nullptr, // Module documentation. |
| 2855 | -1, // Says the module keeps state in global variables. |
| 2856 | ais_methods |
| 2857 | }; |
| 2858 | |
| 2859 | PyMODINIT_FUNC PyInit__ais(void) { |
| 2860 | PyObject *module = PyModule_Create(&aismodule); |
| 2861 | |
| 2862 | if (module == nullptr) |
| 2863 | return nullptr; |
| 2864 | |
| 2865 | ais_py_exception = PyErr_NewException(exception_name, nullptr, nullptr); |
| 2866 | Py_INCREF(ais_py_exception); |
| 2867 | PyModule_AddObject(module, exception_short, ais_py_exception); |
| 2868 | return module; |
| 2869 | } |
| 2870 | |
| 2871 | } // extern "C" |
| 2872 | |
| 2873 | } // namespace libais |