| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "_modes.h" |
| 20 |
|
| 21 |
|
| 22 |
static PyObject *modesmessage_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 23 |
static void modesmessage_dealloc(modesmessage *self); |
| 24 |
static int modesmessage_init(modesmessage *self, PyObject *args, PyObject *kwds); |
| 25 |
static int modesmessage_bf_getbuffer(PyObject *self, Py_buffer *view, int flags); |
| 26 |
static Py_ssize_t modesmessage_sq_length(modesmessage *self); |
| 27 |
static PyObject *modesmessage_sq_item(modesmessage *self, Py_ssize_t i); |
| 28 |
static long modesmessage_hash(PyObject *self); |
| 29 |
static PyObject *modesmessage_richcompare(PyObject *self, PyObject *other, int op); |
| 30 |
static PyObject *modesmessage_repr(PyObject *self); |
| 31 |
static PyObject *modesmessage_str(PyObject *self); |
| 32 |
|
| 33 |
|
| 34 |
static PyObject *decode_ac13(unsigned ac13); |
| 35 |
static uint32_t crc_residual(uint8_t *message, int len); |
| 36 |
static int decode(modesmessage *self); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static PyMemberDef modesmessageMembers[] = { |
| 41 |
{ "timestamp", T_ULONGLONG, offsetof(modesmessage, timestamp), 0, "12MHz timestamp" }, |
| 42 |
{ "signal", T_UINT, offsetof(modesmessage, signal), READONLY, "signal level" }, |
| 43 |
{ "df", T_UINT, offsetof(modesmessage, df), READONLY, "downlink format or a special DF_* value" }, |
| 44 |
{ "nuc", T_UINT, offsetof(modesmessage, nuc), READONLY, "NUCp value" }, |
| 45 |
{ "even_cpr", T_BOOL, offsetof(modesmessage, even_cpr), READONLY, "CPR even-format flag" }, |
| 46 |
{ "odd_cpr", T_BOOL, offsetof(modesmessage, odd_cpr), READONLY, "CPR odd-format flag" }, |
| 47 |
{ "valid", T_BOOL, offsetof(modesmessage, valid), READONLY, "Does the message look OK?" }, |
| 48 |
{ "crc_residual", T_OBJECT, offsetof(modesmessage, crc), READONLY, "CRC residual" }, |
| 49 |
{ "address", T_OBJECT, offsetof(modesmessage, address), READONLY, "ICAO address" }, |
| 50 |
{ "altitude", T_OBJECT, offsetof(modesmessage, altitude), READONLY, "altitude" }, |
| 51 |
{ "eventdata", T_OBJECT, offsetof(modesmessage, eventdata), READONLY, "event data dictionary for special event messages" }, |
| 52 |
{ NULL, 0, 0, 0, NULL } |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
static PyBufferProcs modesmessageBufferProcs = { |
| 57 |
modesmessage_bf_getbuffer, |
| 58 |
NULL |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
static PySequenceMethods modesmessageSequenceMethods = { |
| 63 |
(lenfunc)modesmessage_sq_length, |
| 64 |
0, |
| 65 |
0, |
| 66 |
(ssizeargfunc)modesmessage_sq_item, |
| 67 |
0, |
| 68 |
0, |
| 69 |
0, |
| 70 |
0, |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
static PyTypeObject modesmessageType = { |
| 75 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 76 |
"_modes.Message", |
| 77 |
sizeof(modesmessage), |
| 78 |
0, |
| 79 |
(destructor)modesmessage_dealloc, |
| 80 |
0, |
| 81 |
0, |
| 82 |
0, |
| 83 |
0, |
| 84 |
(reprfunc)modesmessage_repr, |
| 85 |
0, |
| 86 |
&modesmessageSequenceMethods, |
| 87 |
0, |
| 88 |
(hashfunc)modesmessage_hash, |
| 89 |
0, |
| 90 |
(reprfunc)modesmessage_str, |
| 91 |
PyObject_GenericGetAttr, |
| 92 |
PyObject_GenericSetAttr, |
| 93 |
&modesmessageBufferProcs, |
| 94 |
Py_TPFLAGS_DEFAULT||Py_TPFLAGS_BASETYPE, |
| 95 |
"A ModeS message.", |
| 96 |
0, |
| 97 |
0, |
| 98 |
modesmessage_richcompare, |
| 99 |
0, |
| 100 |
0, |
| 101 |
0, |
| 102 |
0, |
| 103 |
modesmessageMembers, |
| 104 |
0, |
| 105 |
0, |
| 106 |
0, |
| 107 |
0, |
| 108 |
0, |
| 109 |
0, |
| 110 |
(initproc)modesmessage_init, |
| 111 |
0, |
| 112 |
modesmessage_new, |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
int modesmessage_module_init(PyObject *m) |
| 119 |
{ |
| 120 |
if (PyType_Ready(&modesmessageType) < 0) |
| 121 |
return -1; |
| 122 |
|
| 123 |
Py_INCREF(&modesmessageType); |
| 124 |
if (PyModule_AddObject(m, "Message", (PyObject *)&modesmessageType) < 0) { |
| 125 |
Py_DECREF(&modesmessageType); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
if (PyModule_AddIntMacro(m, DF_MODEAC) < 0) |
| 131 |
return -1; |
| 132 |
|
| 133 |
if (PyModule_AddIntMacro(m, DF_EVENT_TIMESTAMP_JUMP) < 0) |
| 134 |
return -1; |
| 135 |
|
| 136 |
if (PyModule_AddIntMacro(m, DF_EVENT_MODE_CHANGE) < 0) |
| 137 |
return -1; |
| 138 |
|
| 139 |
if (PyModule_AddIntMacro(m, DF_EVENT_EPOCH_ROLLOVER) < 0) |
| 140 |
return -1; |
| 141 |
|
| 142 |
if (PyModule_AddIntMacro(m, DF_EVENT_RADARCAPE_STATUS) < 0) |
| 143 |
return -1; |
| 144 |
|
| 145 |
if (PyModule_AddIntMacro(m, DF_EVENT_RADARCAPE_POSITION) < 0) |
| 146 |
return -1; |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
void modesmessage_module_free(PyObject *m) |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
static PyObject *modesmessage_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 156 |
{ |
| 157 |
modesmessage *self; |
| 158 |
|
| 159 |
self = (modesmessage *)type->tp_alloc(type, 0); |
| 160 |
if (!self) |
| 161 |
return NULL; |
| 162 |
|
| 163 |
|
| 164 |
self->timestamp = 0; |
| 165 |
self->signal = 0; |
| 166 |
self->df = 0; |
| 167 |
self->nuc = 0; |
| 168 |
self->even_cpr = self->odd_cpr = 0; |
| 169 |
self->valid = 0; |
| 170 |
self->crc = NULL; |
| 171 |
self->address = NULL; |
| 172 |
self->altitude = NULL; |
| 173 |
self->data = NULL; |
| 174 |
self->datalen = 0; |
| 175 |
self->eventdata = NULL; |
| 176 |
|
| 177 |
return (PyObject *)self; |
| 178 |
} |
| 179 |
|
| 180 |
static void modesmessage_dealloc(modesmessage *self) |
| 181 |
{ |
| 182 |
Py_XDECREF(self->crc); |
| 183 |
Py_XDECREF(self->address); |
| 184 |
Py_XDECREF(self->altitude); |
| 185 |
Py_XDECREF(self->eventdata); |
| 186 |
free(self->data); |
| 187 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
PyObject *modesmessage_from_buffer(unsigned long long timestamp, unsigned signal, uint8_t *data, int datalen) |
| 192 |
{ |
| 193 |
modesmessage *message; |
| 194 |
uint8_t *copydata; |
| 195 |
|
| 196 |
if (! (message = (modesmessage*)modesmessage_new(&modesmessageType, NULL, NULL))) |
| 197 |
goto err; |
| 198 |
|
| 199 |
|
| 200 |
message->data = NULL; |
| 201 |
|
| 202 |
copydata = malloc(datalen); |
| 203 |
if (!copydata) { |
| 204 |
PyErr_NoMemory(); |
| 205 |
goto err; |
| 206 |
} |
| 207 |
memcpy(copydata, data, datalen); |
| 208 |
|
| 209 |
message->timestamp = timestamp; |
| 210 |
message->signal = signal; |
| 211 |
message->data = copydata; |
| 212 |
message->datalen = datalen; |
| 213 |
|
| 214 |
if (decode(message) < 0) |
| 215 |
goto err; |
| 216 |
|
| 217 |
return (PyObject*)message; |
| 218 |
|
| 219 |
err: |
| 220 |
Py_XDECREF(message); |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
PyObject *modesmessage_new_eventmessage(int type, unsigned long long timestamp, PyObject *eventdata) |
| 228 |
{ |
| 229 |
modesmessage *message; |
| 230 |
|
| 231 |
if (! (message = (modesmessage*)modesmessage_new(&modesmessageType, NULL, NULL))) |
| 232 |
return NULL; |
| 233 |
|
| 234 |
message->df = type; |
| 235 |
message->timestamp = timestamp; |
| 236 |
message->eventdata = eventdata; |
| 237 |
return (PyObject *)message; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
PyObject *modesmessage_eventmessage(PyObject *self, PyObject *args, PyObject *kwds) |
| 242 |
{ |
| 243 |
static char *kwlist[] = { "type", "timestamp", "eventdata", NULL }; |
| 244 |
int type; |
| 245 |
unsigned long long timestamp; |
| 246 |
PyObject *eventdata = NULL; |
| 247 |
PyObject *rv = NULL; |
| 248 |
|
| 249 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iKO", kwlist, &type, ×tamp, &eventdata)) |
| 250 |
return NULL; |
| 251 |
|
| 252 |
Py_INCREF(eventdata); |
| 253 |
if (! (rv = modesmessage_new_eventmessage(type, timestamp, eventdata))) { |
| 254 |
Py_DECREF(eventdata); |
| 255 |
return NULL; |
| 256 |
} |
| 257 |
|
| 258 |
return rv; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
static int modesmessage_init(modesmessage *self, PyObject *args, PyObject *kwds) |
| 263 |
{ |
| 264 |
static char *kwlist[] = { "data", "timestamp", "signal", NULL }; |
| 265 |
Py_buffer data; |
| 266 |
int rv = -1; |
| 267 |
|
| 268 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s*|KI", kwlist, &data, &self->timestamp, &self->signal)) |
| 269 |
return -1; |
| 270 |
|
| 271 |
if (data.itemsize != 1) { |
| 272 |
PyErr_SetString(PyExc_ValueError, "buffer itemsize is not 1"); |
| 273 |
goto out; |
| 274 |
} |
| 275 |
|
| 276 |
if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 277 |
PyErr_SetString(PyExc_ValueError, "buffer is not contiguous"); |
| 278 |
goto out; |
| 279 |
} |
| 280 |
|
| 281 |
self->datalen = 0; |
| 282 |
free(self->data); |
| 283 |
|
| 284 |
self->data = malloc(data.len); |
| 285 |
if (!self->data) { |
| 286 |
PyErr_NoMemory(); |
| 287 |
goto out; |
| 288 |
} |
| 289 |
|
| 290 |
memcpy(self->data, data.buf, data.len); |
| 291 |
self->datalen = data.len; |
| 292 |
|
| 293 |
rv = decode(self); |
| 294 |
|
| 295 |
out: |
| 296 |
PyBuffer_Release(&data); |
| 297 |
return rv; |
| 298 |
} |
| 299 |
|
| 300 |
static PyObject *decode_ac13(unsigned ac13) |
| 301 |
{ |
| 302 |
int h, f, a; |
| 303 |
|
| 304 |
if (ac13 == 0) |
| 305 |
Py_RETURN_NONE; |
| 306 |
|
| 307 |
if (ac13 & 0x0040) |
| 308 |
Py_RETURN_NONE; |
| 309 |
|
| 310 |
if (ac13 & 0x0010) { |
| 311 |
int n = ((ac13 & 0x1f80) >> 2) | ((ac13 & 0x0020) >> 1) | (ac13 & 0x000f); |
| 312 |
return PyLong_FromLong(n * 25 - 1000); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
if (! ((ac13 & 0x1500))) { |
| 317 |
|
| 318 |
Py_RETURN_NONE; |
| 319 |
} |
| 320 |
|
| 321 |
h = 0; |
| 322 |
if (ac13 & 0x1000) h ^= 7; |
| 323 |
if (ac13 & 0x0400) h ^= 3; |
| 324 |
if (ac13 & 0x0100) h ^= 1; |
| 325 |
|
| 326 |
if (h & 5) |
| 327 |
h ^= 5; |
| 328 |
|
| 329 |
if (h > 5) |
| 330 |
Py_RETURN_NONE; |
| 331 |
|
| 332 |
f = 0; |
| 333 |
if (ac13 & 0x0010) f ^= 0x1ff; |
| 334 |
if (ac13 & 0x0004) f ^= 0x0ff; |
| 335 |
if (ac13 & 0x0001) f ^= 0x07f; |
| 336 |
if (ac13 & 0x0800) f ^= 0x03f; |
| 337 |
if (ac13 & 0x0200) f ^= 0x01f; |
| 338 |
if (ac13 & 0x0080) f ^= 0x00f; |
| 339 |
if (ac13 & 0x0020) f ^= 0x007; |
| 340 |
if (ac13 & 0x0008) f ^= 0x003; |
| 341 |
if (ac13 & 0x0002) f ^= 0x001; |
| 342 |
|
| 343 |
if (f & 1) |
| 344 |
h = (6 - h); |
| 345 |
|
| 346 |
a = 500 * f + 100 * h - 1300; |
| 347 |
if (a < -1200) |
| 348 |
Py_RETURN_NONE; |
| 349 |
|
| 350 |
return PyLong_FromLong(a); |
| 351 |
} |
| 352 |
|
| 353 |
static PyObject *decode_ac12(unsigned ac12) |
| 354 |
{ |
| 355 |
return decode_ac13(((ac12 & 0x0fc0) << 1) | (ac12 & 0x003f)); |
| 356 |
} |
| 357 |
|
| 358 |
static uint32_t crc_residual(uint8_t *message, int len) |
| 359 |
{ |
| 360 |
uint32_t crc; |
| 361 |
|
| 362 |
if (len < 3) |
| 363 |
return 0; |
| 364 |
|
| 365 |
crc = modescrc_buffer_crc(message, len - 3); |
| 366 |
crc = crc ^ (message[len-3] << 16); |
| 367 |
crc = crc ^ (message[len-2] << 8); |
| 368 |
crc = crc ^ (message[len-1]); |
| 369 |
return crc; |
| 370 |
} |
| 371 |
|
| 372 |
static int decode(modesmessage *self) |
| 373 |
{ |
| 374 |
uint32_t crc; |
| 375 |
|
| 376 |
|
| 377 |
self->valid = 0; |
| 378 |
self->nuc = 0; |
| 379 |
self->odd_cpr = self->even_cpr = 0; |
| 380 |
Py_CLEAR(self->crc); |
| 381 |
Py_CLEAR(self->address); |
| 382 |
Py_CLEAR(self->altitude); |
| 383 |
|
| 384 |
if (self->datalen == 2) { |
| 385 |
self->df = DF_MODEAC; |
| 386 |
self->address = PyLong_FromLong((self->data[0] << 8) | self->data[1]); |
| 387 |
self->valid = 1; |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
self->df = (self->data[0] >> 3) & 31; |
| 392 |
|
| 393 |
if ((self->df < 16 && self->datalen != 7) || (self->df >= 16 && self->datalen != 14)) { |
| 394 |
|
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
if (self->df != 0 && self->df != 4 && self->df != 5 && self->df != 11 && |
| 399 |
self->df != 16 && self->df != 17 && self->df != 20 && self->df != 21) { |
| 400 |
|
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
crc = crc_residual(self->data, self->datalen); |
| 405 |
if (!(self->crc = PyLong_FromLong(crc))) |
| 406 |
return -1; |
| 407 |
|
| 408 |
switch (self->df) { |
| 409 |
case 0: |
| 410 |
case 4: |
| 411 |
case 16: |
| 412 |
case 20: |
| 413 |
self->address = (Py_INCREF(self->crc), self->crc); |
| 414 |
if (! (self->altitude = decode_ac13((self->data[2] & 0x1f) << 8 | (self->data[3])))) |
| 415 |
return -1; |
| 416 |
self->valid = 1; |
| 417 |
break; |
| 418 |
|
| 419 |
case 5: |
| 420 |
case 21: |
| 421 |
case 24: |
| 422 |
self->address = (Py_INCREF(self->crc), self->crc); |
| 423 |
self->valid = 1; |
| 424 |
break; |
| 425 |
|
| 426 |
case 11: |
| 427 |
self->valid = ((crc & ~0x7f) == 0); |
| 428 |
if (self->valid) { |
| 429 |
if (! (self->address = PyLong_FromLong( (self->data[1] << 16) | (self->data[2] << 8) | (self->data[3]) ))) |
| 430 |
return -1; |
| 431 |
} |
| 432 |
break; |
| 433 |
|
| 434 |
case 17: |
| 435 |
self->valid = (crc == 0); |
| 436 |
if (self->valid) { |
| 437 |
unsigned metype; |
| 438 |
|
| 439 |
unsigned address = (self->data[1] << 16) | (self->data[2] << 8) | (self->data[3]); |
| 440 |
self->address = PyLong_FromLong(address); |
| 441 |
if (!self->address) |
| 442 |
return -1; |
| 443 |
|
| 444 |
metype = self->data[4] >> 3; |
| 445 |
if ((metype >= 9 && metype <= 18) || (metype >= 20 && metype < 22)) { |
| 446 |
if (metype == 22) |
| 447 |
self->nuc = 0; |
| 448 |
else if (metype <= 18) |
| 449 |
self->nuc = 18 - metype; |
| 450 |
else |
| 451 |
self->nuc = 29 - metype; |
| 452 |
|
| 453 |
if (0 && self->nuc <= 5) { |
| 454 |
fprintf(stderr, "%06x nuc: %d\n", address, self->nuc); |
| 455 |
} |
| 456 |
|
| 457 |
if (self->data[6] & 0x04) |
| 458 |
self->odd_cpr = 1; |
| 459 |
else |
| 460 |
self->even_cpr = 1; |
| 461 |
|
| 462 |
if (! (self->altitude = decode_ac12((self->data[5] << 4) | ((self->data[6] & 0xF0) >> 4)))) |
| 463 |
return -1; |
| 464 |
|
| 465 |
|
| 466 |
if ((self->data[7] == 0 && (self->data[8] & 0x7F) == 0) || (self->data[9] == 0 && self->data[10] == 0)) { |
| 467 |
self->valid = 0; |
| 468 |
if (0) { |
| 469 |
fprintf(stderr, "%06x %02x %02x %02x %02x\n", |
| 470 |
address, self->data[7], self->data[8], self->data[9], self->data[10]); |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
break; |
| 476 |
|
| 477 |
default: |
| 478 |
break; |
| 479 |
} |
| 480 |
|
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
static int modesmessage_bf_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 485 |
{ |
| 486 |
return PyBuffer_FillInfo(view, self, ((modesmessage*)self)->data, ((modesmessage*)self)->datalen, 1, flags); |
| 487 |
} |
| 488 |
|
| 489 |
static Py_ssize_t modesmessage_sq_length(modesmessage *self) |
| 490 |
{ |
| 491 |
return self->datalen; |
| 492 |
} |
| 493 |
|
| 494 |
static PyObject *modesmessage_sq_item(modesmessage *self, Py_ssize_t i) |
| 495 |
{ |
| 496 |
if (i < 0 || i >= self->datalen) { |
| 497 |
PyErr_SetString(PyExc_IndexError, "byte index out of range"); |
| 498 |
return NULL; |
| 499 |
} |
| 500 |
|
| 501 |
return PyLong_FromLong(self->data[i]); |
| 502 |
} |
| 503 |
|
| 504 |
static long modesmessage_hash(PyObject *self) |
| 505 |
{ |
| 506 |
modesmessage *msg = (modesmessage*)self; |
| 507 |
uint32_t hash = 0; |
| 508 |
int i; |
| 509 |
|
| 510 |
|
| 511 |
for (i = 0; i < 4 && i < msg->datalen; ++i) { |
| 512 |
hash += msg->data[i] & 0xff; |
| 513 |
hash += hash << 10; |
| 514 |
hash ^= hash >> 6; |
| 515 |
} |
| 516 |
|
| 517 |
hash += (hash << 3); |
| 518 |
hash ^= (hash >> 11); |
| 519 |
hash += (hash << 15); |
| 520 |
|
| 521 |
return (long)hash; |
| 522 |
} |
| 523 |
|
| 524 |
static PyObject *modesmessage_richcompare(PyObject *self, PyObject *other, int op) |
| 525 |
{ |
| 526 |
PyObject *result = NULL; |
| 527 |
|
| 528 |
if (! PyObject_TypeCheck(self, &modesmessageType) || |
| 529 |
! PyObject_TypeCheck(other, &modesmessageType)) { |
| 530 |
result = Py_NotImplemented; |
| 531 |
} else { |
| 532 |
modesmessage *message1 = (modesmessage*)self; |
| 533 |
modesmessage *message2 = (modesmessage*)other; |
| 534 |
int c = 0; |
| 535 |
|
| 536 |
switch (op) { |
| 537 |
case Py_EQ: |
| 538 |
c = (message1->datalen == message2->datalen) && (memcmp(message1->data, message2->data, message1->datalen) == 0); |
| 539 |
break; |
| 540 |
case Py_NE: |
| 541 |
c = (message1->datalen != message2->datalen) || (memcmp(message1->data, message2->data, message1->datalen) != 0); |
| 542 |
break; |
| 543 |
case Py_LT: |
| 544 |
c = (message1->datalen < message2->datalen) || |
| 545 |
(message1->datalen == message2->datalen && memcmp(message1->data, message2->data, message1->datalen) < 0); |
| 546 |
break; |
| 547 |
case Py_LE: |
| 548 |
c = (message1->datalen < message2->datalen) || |
| 549 |
(message1->datalen == message2->datalen && memcmp(message1->data, message2->data, message1->datalen) <= 0); |
| 550 |
break; |
| 551 |
case Py_GT: |
| 552 |
c = (message1->datalen > message2->datalen) || |
| 553 |
(message1->datalen == message2->datalen && memcmp(message1->data, message2->data, message1->datalen) > 0); |
| 554 |
break; |
| 555 |
case Py_GE: |
| 556 |
c = (message1->datalen > message2->datalen) || |
| 557 |
(message1->datalen == message2->datalen && memcmp(message1->data, message2->data, message1->datalen) >= 0); |
| 558 |
break; |
| 559 |
default: |
| 560 |
result = Py_NotImplemented; |
| 561 |
break; |
| 562 |
} |
| 563 |
|
| 564 |
if (!result) |
| 565 |
result = (c ? Py_True : Py_False); |
| 566 |
} |
| 567 |
|
| 568 |
Py_INCREF(result); |
| 569 |
return result; |
| 570 |
} |
| 571 |
|
| 572 |
static const char *df_event_name(int df) |
| 573 |
{ |
| 574 |
switch (df) { |
| 575 |
case DF_EVENT_TIMESTAMP_JUMP: |
| 576 |
return "DF_EVENT_TIMESTAMP_JUMP"; |
| 577 |
case DF_EVENT_MODE_CHANGE: |
| 578 |
return "DF_EVENT_MODE_CHANGE"; |
| 579 |
case DF_EVENT_EPOCH_ROLLOVER: |
| 580 |
return "DF_EVENT_EPOCH_ROLLOVER"; |
| 581 |
case DF_EVENT_RADARCAPE_STATUS: |
| 582 |
return "DF_EVENT_RADARCAPE_STATUS"; |
| 583 |
default: |
| 584 |
return NULL; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
static char hexdigit[16] = "0123456789abcdef"; |
| 589 |
static PyObject *modesmessage_repr(PyObject *self) |
| 590 |
{ |
| 591 |
modesmessage *message = (modesmessage *)self; |
| 592 |
if (message->data) { |
| 593 |
char buf[256]; |
| 594 |
char *p = buf; |
| 595 |
int i; |
| 596 |
|
| 597 |
for (i = 0; i < message->datalen; ++i) { |
| 598 |
*p++ = '\\'; |
| 599 |
*p++ = 'x'; |
| 600 |
*p++ = hexdigit[(message->data[i] >> 4) & 15]; |
| 601 |
*p++ = hexdigit[message->data[i] & 15]; |
| 602 |
} |
| 603 |
*p++ = 0; |
| 604 |
|
| 605 |
return PyUnicode_FromFormat("_modes.Message(b'%s',%llu,%u)", buf, (unsigned long long)message->timestamp, (unsigned)message->signal); |
| 606 |
} else { |
| 607 |
const char *eventname = df_event_name(message->df); |
| 608 |
if (eventname) { |
| 609 |
return PyUnicode_FromFormat("_modes.EventMessage(_modes.%s,%llu,%R)", |
| 610 |
df_event_name(message->df), |
| 611 |
(unsigned long long)message->timestamp, |
| 612 |
message->eventdata); |
| 613 |
} else { |
| 614 |
return PyUnicode_FromFormat("_modes.EventMessage(%d,%llu,%R)", |
| 615 |
message->df, |
| 616 |
(unsigned long long)message->timestamp, |
| 617 |
message->eventdata); |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
static PyObject *modesmessage_str(PyObject *self) |
| 623 |
{ |
| 624 |
modesmessage *message = (modesmessage *)self; |
| 625 |
if (message->data) { |
| 626 |
char buf[256]; |
| 627 |
char *p = buf; |
| 628 |
int i; |
| 629 |
|
| 630 |
for (i = 0; i < message->datalen; ++i) { |
| 631 |
*p++ = hexdigit[(message->data[i] >> 4) & 15]; |
| 632 |
*p++ = hexdigit[message->data[i] & 15]; |
| 633 |
} |
| 634 |
*p++ = 0; |
| 635 |
|
| 636 |
return PyUnicode_FromString(buf); |
| 637 |
} else { |
| 638 |
const char *eventname = df_event_name(message->df); |
| 639 |
if (eventname) { |
| 640 |
return PyUnicode_FromFormat("%s@%llu:%R", |
| 641 |
eventname, |
| 642 |
(unsigned long long)message->timestamp, |
| 643 |
message->eventdata); |
| 644 |
} else { |
| 645 |
return PyUnicode_FromFormat("DF%d@%llu:%R", |
| 646 |
message->df, |
| 647 |
(unsigned long long)message->timestamp, |
| 648 |
message->eventdata); |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|