at2000.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdint.h>
  6. #include <syslog.h>
  7. #include <stdarg.h>
  8. //#include <unistd.h>
  9. #include "tiny-AES128-C-master/aes.h"
  10. #include "../../status.h"
  11. #include "../../feeder.h"
  12. #define FM4_BUFFER_SIZE 2048
  13. #define AT2000_SAP_ID_ACK 0x00
  14. #define AT2000_SAP_ID_DEVICE_ID 0x01
  15. #define AT2000_SAP_ID_CONF_REQ 0x82
  16. #define AT2000_SAP_ID_CONF_REQ_RES 0x83
  17. #define AT2000_SAP_ID_REMOTE_CONF 0x84
  18. #define AT2000_SAP_ID_FIRMWARE_DATA 0x85
  19. #define AT2000_SAP_ID_IO_ELEMENTS_REQ 0x86
  20. #define AT2000_SAP_ID_IO_ELEMENTS_RES 0x87
  21. #define AT2000_SAP_ID_TRACK_RECORDS_REQ 0x88
  22. #define AT2000_SAP_ID_TRACK_RECORDS_RES 0x89
  23. #define AT2000_SAP_ID_LOG_REQ 0x8A
  24. #define AT2000_SAP_ID_LOG_RES 0x8B
  25. #define AT2000_SAP_ID_SESSION_END_REQ 0x0C
  26. #define AT2000_SAP_ID_FIRWARE_UPDATE_STATUS_REQ 0x8D
  27. #define AT2000_SAP_ID_FIRWARE_UPDATE_STATUS_RES 0x8E
  28. #define AT2000_SAP_ID_NONE 0xFF
  29. #define AT2000_SAP_ID_NO_REQ 0xFF
  30. #define AT2000_STATUS_IDLE 0x01
  31. #define AT2000_STATUS_INIT 0x02
  32. #define AT2000_STATUS_TRACK 0x03
  33. #define AT2000_STATUS_TRACK_WAIT 0x04
  34. #define AT2000_STATUS_SESSION_END 0x05
  35. #define AT2000_STATUS_CLOSE 0x06
  36. #define AT2000_IO_EVENTS_COORDINATE_RELIABILITY 11
  37. static uint8_t at2000_aes_key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  38. //static unsigned char * int_buffer;
  39. //static unsigned int int_buffer_pos = 0, int_buffer_pos_wr = 0;
  40. //static int imei_flag = 0;
  41. //char imei_str[16]; //todo: tohle nejak predavat
  42. unsigned char reply_buffer[32];
  43. unsigned int reply_len;
  44. //static double values_out[64];
  45. static uint64_t values_sensor[64];
  46. static uint8_t values_type[64];
  47. static time_t values_time;
  48. //static uint8_t values_type;
  49. static uint8_t values_len;
  50. typedef struct unit_t
  51. {
  52. int fd;
  53. char imei[16];
  54. struct unit_t * next;
  55. unsigned char * int_buffer;
  56. unsigned int int_buffer_pos;
  57. unsigned int int_buffer_pos_wr;
  58. // unsigned int int_buffer_size;
  59. uint8_t IV[16];
  60. uint8_t tstV[16];
  61. uint8_t status;
  62. uint8_t sapid;
  63. uint32_t data_len;
  64. uint8_t pos_obs;
  65. } unit_t;
  66. static struct unit_t * units;
  67. static struct unit_t * aunit;
  68. static void (* feederLog)(int priority, const char * fmt, ...);
  69. int setLog(void * func)
  70. {
  71. feederLog = func;
  72. return 0;
  73. }
  74. typedef struct avl_header_t
  75. {
  76. uint16_t dummy __attribute__ ((packed));
  77. char imei[15];
  78. } avl_header_t;
  79. typedef struct at2000_packet_t
  80. {
  81. // uint8_t codec_id;
  82. uint8_t sap;
  83. uint8_t len[3];
  84. uint8_t reserv[12];
  85. } __attribute__((packed)) at2000_packet_t;
  86. typedef struct at2000_track_t
  87. {
  88. uint16_t recID;
  89. uint8_t reserv[2];
  90. uint32_t timestamp;
  91. uint32_t timestamp_frac;
  92. float lat;
  93. float lon;
  94. uint32_t alt;
  95. uint32_t speed;
  96. uint32_t course;
  97. uint32_t geozone_events;
  98. uint32_t io_events;
  99. uint32_t geozone_val;
  100. uint32_t io_val_bits;
  101. uint16_t operator;
  102. uint16_t ain1;
  103. uint16_t ain2;
  104. uint16_t ext_power;
  105. uint16_t cellid;
  106. uint8_t gsm_level;
  107. uint8_t current_profile;
  108. uint8_t battery_level;
  109. uint8_t temperature;
  110. uint8_t gps_sats;
  111. } __attribute__((packed)) at2000_track_t;
  112. uint16_t byteSwap16(uint16_t value)
  113. {
  114. uint16_t swapped;
  115. swapped = (((0x00FF) & (value >> 8)) |
  116. ((0xFF00) & (value << 8)));
  117. return swapped;
  118. }
  119. uint32_t byteSwap32(uint32_t value)
  120. {
  121. uint32_t swapped;
  122. swapped = (((0x000000FF) & (value >> 24)) |
  123. ((0x0000FF00) & (value >> 8)) |
  124. ((0x00FF0000) & (value << 8)) |
  125. ((0xFF000000) & (value << 24)));
  126. return swapped;
  127. }
  128. uint64_t byteSwap64(uint64_t value)
  129. {
  130. uint64_t swapped;
  131. swapped = (((0x00000000000000FFULL) & (value >> 56)) |
  132. ((0x000000000000FF00ULL) & (value >> 40)) |
  133. ((0x0000000000FF0000ULL) & (value >> 24)) |
  134. ((0x00000000FF000000ULL) & (value >> 8)) |
  135. ((0x000000FF00000000ULL) & (value << 8)) |
  136. ((0x0000FF0000000000ULL) & (value << 24)) |
  137. ((0x00FF000000000000ULL) & (value << 40)) |
  138. ((0xFF00000000000000ULL) & (value << 56)));
  139. return swapped;
  140. }
  141. void arraySwap4(uint8_t * array)
  142. {
  143. uint8_t temp;
  144. temp = array[0];
  145. array[0] = array[3];
  146. array[3] = temp;
  147. temp = array[1];
  148. array[1] = array[2];
  149. array[2] = temp;
  150. }
  151. /*
  152. enum states {STATE_IDLE = 0, STATE_DATA_LENGTH, STATE_CODEC_ID, STATE_NO_OF_REC,
  153. STATE_TIMESTAMP, STATE_PRIORITY,
  154. STATE_LON, STATE_LAT, STATE_ALT, STATE_ANGLE, STATE_SATTS, STATE_SPEED,
  155. STATE_EVENT_IO_ID, STATE_TOTAL_IO,
  156. STATE_BYTE1_IO, STATE_BYTE1_ID, STATE_BYTE1_VAL,
  157. STATE_BYTE2_IO, STATE_BYTE2_ID, STATE_BYTE2_VAL,
  158. STATE_BYTE4_IO, STATE_BYTE4_ID, STATE_BYTE4_VAL,
  159. STATE_BYTE8_IO, STATE_BYTE8_ID, STATE_BYTE8_VAL,
  160. STATE_NO_OF_REC_END, STATE_CRC
  161. };
  162. static unsigned char state = STATE_IDLE; //state of status machine, must be global to be accesible by process function when determining if this packet could be imei packet (state is IDLE)
  163. */
  164. unsigned char counts[] =
  165. {
  166. 4, 4, 1, 1,
  167. 8, 1,
  168. 4, 4, 2, 2, 1, 2,
  169. 1, 1,
  170. 1, 1, 1,
  171. 1, 1, 2,
  172. 1, 1, 4,
  173. 1, 1, 8,
  174. 1, 4
  175. };
  176. #define PRIO_LOW 0
  177. #define PRIO_HIGHT 1
  178. #define PRIO_PANIC 2
  179. #define PRIO_SECURITY 3
  180. #define VAL_DI1 1
  181. #define VAL_DI2 2
  182. void valuesCopyPos(double lat, double lon, double alt, time_t t, uint8_t type, double * values)
  183. {
  184. values[0] = lat;
  185. values[1] = lon;
  186. values[2] = alt;
  187. values_time = t;
  188. values_type[0] = type;
  189. values_type[1] = type;
  190. values_type[2] = type;
  191. }
  192. //todo: nasledujici dve pole do pole struktur, tedy vcetne multiplu
  193. /* {FM4 type, FEEDER sensor type id, sensor id, alertable (if prio is 1(high))} */
  194. uint8_t sensorTypes[][4] =
  195. {
  196. {1, TYPE_PERCENTAGE, 2, 0},
  197. {9, TYPE_ANALOG_INPUT, 7, 0},
  198. {21, TYPE_SIGNAL_STRENGTH, 7, 0},
  199. {22, TYPE_PROFILE, 1, 0},
  200. {53, TYPE_DIGITAL_INPUT, 14, 0},
  201. {72, TYPE_THERMOMETER, 28, 0},
  202. {101, TYPE_DIGITAL_INPUT, 15, 0},
  203. {102, TYPE_DIGITAL_INPUT, 15, 0},
  204. {103, TYPE_RESISTANCE, 1, 0},
  205. {200, TYPE_LINK, 8, 0},
  206. {210, TYPE_SPEED, 8, 0},
  207. {238, TYPE_DIGITAL_INPUT, 17, 0},
  208. {239, TYPE_LINK, 9, 0},
  209. {240, TYPE_MOVE, 2, 0},
  210. {0, 0, 0, 0}
  211. };
  212. /*
  213. uint8_t sensorTypes[][4] =
  214. {
  215. {1, TYPE_DIGITAL_INPUT, 5, 0},
  216. {2, TYPE_DIGITAL_INPUT, 6, 1},
  217. {3, TYPE_DIGITAL_INPUT, 7, 0},
  218. {4, TYPE_DIGITAL_INPUT, 8, 0},
  219. {9, TYPE_ANALOG_INPUT, 1, 0},
  220. {10, TYPE_ANALOG_INPUT, 2, 0},
  221. {11, TYPE_ANALOG_INPUT, 3, 0},
  222. {19, TYPE_ANALOG_INPUT, 4, 0},
  223. {21, TYPE_SIGNAL_STRENGTH, 3, 0},
  224. {24, TYPE_SPEED, 2, 0},
  225. {66, TYPE_VOLTAGE, 11, 0},
  226. {67, TYPE_VOLTAGE, 10, 0},
  227. {68, TYPE_CURRENT, 2, 0},
  228. {69, TYPE_SIGNAL_STRENGTH, 4, 0},
  229. {70, TYPE_THERMOMETER, 11, 0},
  230. {72, TYPE_THERMOMETER, 12, 0},
  231. {73, TYPE_THERMOMETER, 13, 0},
  232. {74, TYPE_THERMOMETER, 14, 0},
  233. {79, TYPE_ACCELERATION, 2, 0},
  234. {80, TYPE_SPEED, 3, 0},
  235. {81, TYPE_CRUISE, 1, 0},
  236. {82, TYPE_CRUISE, 2, 0},
  237. {83, TYPE_CRUISE, 3, 0},
  238. {84, TYPE_ENGINE, 1, 0},
  239. {85, TYPE_ENGINE, 2, 0},
  240. {86, TYPE_ENGINE, 3, 0},
  241. {87, TYPE_LEVEL, 4, 0},
  242. {88, TYPE_FREQUENCY, 1, 0},
  243. {104, TYPE_TIME, 1, 0},
  244. {127, TYPE_TEMP, 20, 0},
  245. {128, TYPE_TEMP, 21, 0},
  246. {135, TYPE_FLOW, 6, 0},
  247. {136, TYPE_FLOW, 7, 0},
  248. {207, TYPE_ID, 1, 0},
  249. {249, TYPE_SIGNAL_STRENGTH, 5, 0},
  250. {0, 0, 0, 0}
  251. };
  252. */
  253. double sensorMulti[] =
  254. {
  255. 1.0,
  256. 1.0,
  257. 1.0,
  258. 1.0,
  259. 1.0,
  260. 0.1,
  261. 1.0,
  262. 1.0,
  263. 1.0,
  264. 1.0,
  265. 1.0,
  266. 1.0,
  267. 1.0,
  268. 1.0,
  269. };
  270. /*
  271. double sensorMulti[] =
  272. {
  273. 1.0,
  274. 1.0,
  275. 1.0,
  276. 1.0,
  277. 1.0,
  278. 1.0,
  279. 1.0,
  280. 1.0,
  281. 1.0,
  282. 1.0,
  283. 0.001,
  284. 0.001,
  285. 0.001,
  286. 1.0,
  287. 0.1,
  288. 0.1,
  289. 0.1,
  290. 0.1,
  291. 1.0,
  292. 1.0,
  293. 1.0,
  294. 1.0,
  295. 1.0,
  296. 1.0,
  297. 1.0,
  298. 1.0,
  299. 1.0,
  300. 1.0,
  301. 1.0,
  302. 1.0,
  303. 1.0,
  304. 1.0,
  305. 1.0,
  306. 1.0,
  307. 1.0
  308. };
  309. */
  310. int getSensorType(uint8_t sensor)
  311. {
  312. int i;
  313. i = 0;
  314. while (sensorTypes[i][0] != 0)
  315. {
  316. if (sensorTypes[i][0] == sensor)
  317. return i;
  318. i++;
  319. }
  320. return -1;
  321. }
  322. void valuesCopyVal(double value, uint8_t sensor, time_t t, uint8_t type, double * values)
  323. {
  324. uint8_t sensor_index;
  325. sensor_index = getSensorType(sensor); //todo: check return value
  326. values[values_len] = value * sensorMulti[sensor_index];
  327. values_sensor[values_len] = sensorTypes[sensor_index][2] * 10000 + sensorTypes[sensor_index][1] * 10000000;
  328. values_time = t;
  329. if ((type == VALUES_TYPE_ALERT) && (sensorTypes[sensor_index][3] == 1))
  330. values_type[values_len] = VALUES_TYPE_ALERT;
  331. else
  332. values_type[values_len] = VALUES_TYPE_OBS;
  333. feederLog(LOG_DEBUG, "fm4: Observation type %s\n", (values_type[values_len] == VALUES_TYPE_ALERT) ? "alert" : "obs");
  334. values_len++;
  335. }
  336. //----------------------------------------------------------------------------------------------
  337. struct unit_t * unitFind(int fd)
  338. {
  339. struct unit_t * unit;
  340. unit = units;
  341. while (unit != NULL)
  342. {
  343. if (unit->fd == fd)
  344. {
  345. return unit;
  346. }
  347. unit = unit->next;
  348. }
  349. return NULL;
  350. }
  351. struct unit_t * unitCreate(int fd)
  352. {
  353. struct unit_t * unit, * unit_new;
  354. unit_new = malloc(sizeof(struct unit_t));
  355. if (unit_new == NULL)
  356. return NULL;
  357. unit_new->fd = fd;
  358. unit_new->imei[0] = 0;
  359. unit_new->next = NULL;
  360. unit_new->int_buffer = malloc(FM4_BUFFER_SIZE);
  361. if (unit_new->int_buffer == NULL)
  362. {
  363. free(unit_new);
  364. return NULL;
  365. }
  366. // unit_new->int_buffer_size = FM4_BUFFER_SIZE;
  367. unit_new->int_buffer_pos = 0;
  368. unit_new->int_buffer_pos_wr = 0;
  369. unit_new->status = AT2000_STATUS_IDLE;
  370. unit_new->sapid = AT2000_SAP_ID_NONE;
  371. if (units == NULL)
  372. units = unit_new;
  373. else
  374. {
  375. unit = units;
  376. while (unit->next != NULL)
  377. {
  378. unit = unit->next;
  379. }
  380. unit->next = unit_new;
  381. }
  382. return unit_new;
  383. }
  384. int imei(struct unit_t * unit, unsigned char * data)
  385. {
  386. char imei_str[16];
  387. memcpy(imei_str, data, 15);
  388. imei_str[15] = 0;
  389. feederLog(LOG_DEBUG, "at2000: Imei is %s\n", imei_str);
  390. strcpy(unit->imei, imei_str);
  391. return 1;
  392. }
  393. int init(void * param)
  394. {
  395. param = param;
  396. bzero(reply_buffer, 32);
  397. reply_len = 0;
  398. units = aunit = NULL;
  399. return 0;
  400. }
  401. void logHex(char * buffer, unsigned char * data, unsigned int length)
  402. {
  403. unsigned int i;
  404. buffer[0] = 0;
  405. for (i = 0; i < length; i++)
  406. {
  407. sprintf(buffer + strlen(buffer), "%02X ", data[i]);
  408. }
  409. }
  410. unsigned int
  411. process(void *lib_data, int socket, unsigned char *data,
  412. unsigned int length, unsigned long long int *id, time_t * tm,
  413. double *result_array, uint64_t * sensors, unsigned int *type)
  414. {
  415. unsigned int ret = 0;
  416. struct unit_t * unit;
  417. char buffer[32768];
  418. unsigned int l;
  419. struct at2000_packet_t * packet;
  420. uint8_t buffer_dec[512];
  421. (void)lib_data;
  422. if (data != NULL)
  423. {
  424. feederLog(LOG_DEBUG, "at2000: Incoming data: len %d: \n", length);
  425. l = length;
  426. while (l > 0)
  427. {
  428. logHex(buffer, data + length - l, (l > 16) ? 16:l);
  429. feederLog(LOG_DEBUG, "%s\n", buffer);
  430. l -= (l > 16) ? 16:l;
  431. }
  432. }
  433. unit = unitFind(socket);
  434. if (unit == NULL)
  435. {
  436. feederLog(LOG_DEBUG, "at2000: new unit for socket %d\n", socket);
  437. unit = unitCreate(socket); //todo: check if scucsefull
  438. }
  439. else
  440. {
  441. feederLog(LOG_DEBUG, "at2000: data from known unit on socket %d\n", socket);
  442. }
  443. aunit = unit;
  444. if (aunit->status == AT2000_STATUS_IDLE)
  445. {
  446. if (data[0] == 1)
  447. {
  448. packet = (struct at2000_packet_t *)(data + 1);
  449. aunit->data_len = ((uint32_t)(packet->len[0])) + ((uint32_t)(packet->len[1]) << 8) + ((uint32_t)(packet->len[2]) << 16);
  450. feederLog(LOG_DEBUG, "at2000: data size %d\n", aunit->data_len);
  451. if (packet->sap == AT2000_SAP_ID_DEVICE_ID)
  452. {
  453. imei(aunit, data + 1 + sizeof(struct at2000_packet_t));
  454. memcpy(aunit->IV, data + 1 + sizeof(struct at2000_packet_t) + 15, 16);
  455. memcpy(aunit->tstV, data + 1 + sizeof(struct at2000_packet_t) + 15 + 16, 16);
  456. feederLog(LOG_DEBUG, "at2000: tstV decrypt\n");
  457. AES128_CBC_decrypt_buffer(buffer_dec, aunit->tstV, 16, at2000_aes_key, aunit->IV);
  458. logHex(buffer, buffer_dec, 16);
  459. feederLog(LOG_DEBUG, "%s\n", buffer);
  460. if (((aunit->imei[14] ^ aunit->IV[0]) != buffer_dec[0]) ||
  461. ((aunit->imei[13] ^ aunit->IV[1]) != buffer_dec[1]) ||
  462. ((aunit->imei[12] ^ aunit->IV[2]) != buffer_dec[2]) ||
  463. ((aunit->imei[11] ^ aunit->IV[3]) != buffer_dec[3]))
  464. {
  465. feederLog(LOG_ERR, "at2000: tstV not passed\n");
  466. ret = -1;
  467. }
  468. }
  469. }
  470. else
  471. {
  472. feederLog(LOG_ERR, "at2000: bad codec id\n");
  473. ret = -1;
  474. }
  475. }
  476. else
  477. {
  478. if (data != NULL)
  479. {
  480. aunit->pos_obs = VALUES_TYPE_POS;
  481. if (aunit->sapid == AT2000_SAP_ID_NONE)
  482. {
  483. packet = (struct at2000_packet_t *)(data);
  484. aunit->data_len = ((uint32_t)(packet->len[0])) + ((uint32_t)(packet->len[1]) << 8) + ((uint32_t)(packet->len[2]) << 16);
  485. feederLog(LOG_DEBUG, "at2000: data size %d\n", aunit->data_len);
  486. if (packet->sap == AT2000_SAP_ID_TRACK_RECORDS_RES)
  487. {
  488. feederLog(LOG_DEBUG, "at2000: track log in\n");
  489. aunit->sapid = packet->sap;
  490. /*
  491. if (data_len < aunit->int_buffer_size)
  492. {
  493. aunit->int_buffer = realloc(aunit->int_buffer, data_len);
  494. if (aunit->int_buffer == NULL)
  495. {
  496. feederLog(LOG_ERR, "at2000: no more memory for buffer\n");//todo: puvodni pamet je uvolnena nebo jen zustava nerealokovana?
  497. unit->int_buffer_pos_wr = unit->int_buffer_pos = unit->int_buffer_size = 0;
  498. ret = -1;
  499. return ret;
  500. }
  501. }
  502. */
  503. // memcpy(aunit->int_buffer + aunit->int_buffer_pos_wr, data + 16, length - 16);
  504. memcpy(data, aunit->tstV, 16);
  505. AES128_CBC_decrypt_buffer(aunit->int_buffer + aunit->int_buffer_pos_wr, data, length, at2000_aes_key, aunit->IV);
  506. aunit->int_buffer_pos_wr += (length);
  507. aunit->int_buffer_pos = 16;
  508. /*
  509. AES128_CBC_decrypt_buffer(aunit->int_buffer + aunit->int_buffer_pos_wr, data + 16, length - 16, at2000_aes_key, aunit->IV);
  510. aunit->int_buffer_pos_wr += (length - 16);*/
  511. }
  512. }
  513. else if (aunit->sapid == AT2000_SAP_ID_TRACK_RECORDS_RES)
  514. {
  515. AES128_CBC_decrypt_buffer(aunit->int_buffer + aunit->int_buffer_pos_wr, data, length, at2000_aes_key, aunit->IV);
  516. aunit->int_buffer_pos_wr += (length);
  517. }
  518. }
  519. if (aunit->sapid == AT2000_SAP_ID_TRACK_RECORDS_RES)
  520. {
  521. //while (aunit->int_buffer_pos + sizeof(struct at2000_track_t) <= aunit->int_buffer_pos_wr)
  522. if (aunit->int_buffer_pos + sizeof(struct at2000_track_t) <= aunit->int_buffer_pos_wr)
  523. {
  524. //AES128_CBC_decrypt_buffer(buffer_dec, aunit->int_buffer + aunit->int_buffer_pos, sizeof(struct at2000_track_t), at2000_aes_key, aunit->IV);
  525. struct at2000_track_t * track;
  526. track = (struct at2000_track_t *)(aunit->int_buffer + aunit->int_buffer_pos);
  527. switch (aunit->pos_obs)
  528. {
  529. case VALUES_TYPE_POS:
  530. {
  531. logHex(buffer, aunit->int_buffer + aunit->int_buffer_pos, sizeof(struct at2000_track_t));
  532. feederLog(LOG_DEBUG, "%s\n", buffer);
  533. /* feederLog(LOG_DEBUG, "track id %d\n", track->recID);
  534. feederLog(LOG_DEBUG, "timestamp %ld\n", track->timestamp);
  535. feederLog(LOG_DEBUG, "track lat %f\n", (float)track->lat);
  536. feederLog(LOG_DEBUG, "track lon %f\n", (float)track->lon);
  537. */
  538. result_array[0] = (float)track->lat;
  539. result_array[1] = (float)track->lon;
  540. sensors[0] = sensors[1] = 0x10;
  541. *type = VALUES_TYPE_POS;
  542. *id = atol(aunit->imei);
  543. *tm = track->timestamp;
  544. if ((track->gps_sats != 0xFF) && ((float)track->lat != 0.0) && ((float)track->lon != 0.0) && ((track->io_events & (1 << AT2000_IO_EVENTS_COORDINATE_RELIABILITY)) != 0))
  545. {
  546. ret = 2;
  547. aunit->pos_obs = VALUES_TYPE_OBS;
  548. }
  549. else
  550. {
  551. feederLog(LOG_DEBUG, "at2000: invalid fix\n");
  552. ret = 0;
  553. aunit->int_buffer_pos += sizeof(struct at2000_track_t);
  554. aunit->data_len -= sizeof(struct at2000_track_t);
  555. }
  556. break;
  557. }
  558. case VALUES_TYPE_OBS:
  559. {
  560. result_array[0] = (float)track->battery_level;
  561. sensors[0] = 540070000;
  562. *type = VALUES_TYPE_OBS;
  563. *id = atol(aunit->imei);
  564. *tm = track->timestamp;
  565. ret = 1;
  566. aunit->pos_obs = VALUES_TYPE_POS;
  567. aunit->int_buffer_pos += sizeof(struct at2000_track_t);
  568. aunit->data_len -= sizeof(struct at2000_track_t);
  569. break;
  570. }
  571. }
  572. }
  573. else
  574. {
  575. memcpy(aunit->int_buffer, aunit->int_buffer + aunit->int_buffer_pos, aunit->int_buffer_pos_wr - aunit->int_buffer_pos);
  576. aunit->int_buffer_pos_wr = aunit->int_buffer_pos_wr - aunit->int_buffer_pos;
  577. aunit->int_buffer_pos = 0;
  578. }
  579. }
  580. }
  581. return ret;
  582. }
  583. void replyBuild(uint8_t * buffer, uint8_t id, uint8_t * payload, uint8_t payload_len)
  584. {
  585. struct at2000_packet_t * packet;
  586. packet = (at2000_packet_t *)buffer;
  587. bzero(buffer, 32);
  588. packet->sap = id;
  589. packet->len[0] = payload_len;
  590. packet->len[1] = packet->len[2] = 0;
  591. if ((payload != NULL) && (payload_len > 0))
  592. memcpy(packet->reserv, payload, payload_len);
  593. }
  594. void __sleep__(unsigned int sec)
  595. {
  596. struct timespec tv;
  597. tv.tv_sec = sec;
  598. tv.tv_nsec = 0;
  599. while (nanosleep(&tv, &tv) == -1) ;
  600. }
  601. int reply(void *lib_data, int socket, unsigned char *data)
  602. {
  603. unsigned int temp_reply_len = 0;
  604. uint8_t payload[16];
  605. (void)lib_data;
  606. (void)socket;
  607. feederLog(LOG_DEBUG, "at2000: replying\n");
  608. if (aunit->status == AT2000_STATUS_IDLE)
  609. {
  610. __sleep__(1);
  611. payload[0] = 0x00;
  612. replyBuild(data, AT2000_SAP_ID_ACK, payload, 1);
  613. temp_reply_len = 32;
  614. aunit->status = AT2000_STATUS_INIT;
  615. }
  616. else if (aunit->status == AT2000_STATUS_INIT)
  617. {
  618. __sleep__(1);
  619. replyBuild(data, AT2000_SAP_ID_TRACK_RECORDS_REQ, NULL, 0);
  620. temp_reply_len = 16;
  621. aunit->status = AT2000_STATUS_TRACK_WAIT;
  622. }
  623. else if (aunit->status == AT2000_STATUS_TRACK)
  624. {
  625. __sleep__(1);
  626. payload[0] = 0x00;
  627. replyBuild(data, AT2000_SAP_ID_ACK, payload, 1);
  628. temp_reply_len = 32;
  629. if (aunit->data_len == 0)
  630. aunit->status = AT2000_STATUS_SESSION_END;
  631. else
  632. aunit->status = AT2000_STATUS_TRACK_WAIT;
  633. }
  634. else if (aunit->status == AT2000_STATUS_TRACK_WAIT)
  635. {
  636. temp_reply_len = 0;
  637. aunit->status = AT2000_STATUS_TRACK;
  638. }
  639. else if (aunit->status == AT2000_STATUS_SESSION_END)
  640. {
  641. __sleep__(1);
  642. replyBuild(data, AT2000_SAP_ID_SESSION_END_REQ, NULL, 0);
  643. temp_reply_len = 16;
  644. aunit->status = AT2000_STATUS_CLOSE;
  645. }
  646. else if (aunit->status == AT2000_STATUS_CLOSE)
  647. {
  648. __sleep__(1);
  649. temp_reply_len = -1;
  650. }
  651. /*
  652. memcpy(data, reply_buffer, reply_len);
  653. temp_reply_len = reply_len;
  654. reply_len = 0;
  655. */
  656. return temp_reply_len;
  657. }
  658. int open(void *lib_data, int socket)
  659. {
  660. (void)lib_data;
  661. (void)socket;
  662. feederLog(LOG_DEBUG, "at2000: socket %d opened\n", socket);
  663. return 0;
  664. }
  665. int close(void *lib_data, int socket)
  666. {
  667. struct unit_t * unit, * unit_parent;
  668. (void)lib_data;
  669. (void)socket;
  670. feederLog(LOG_DEBUG, "at2000: socket %d closed\n", socket);
  671. unit = unitFind(socket);
  672. if (unit != NULL)
  673. {
  674. free(unit->int_buffer);
  675. unit->imei[0] = 0; //todo: nebo mozna lepe, zrusit celou jednotku - coz bacha na next
  676. unit->fd = -1;
  677. if (unit == units)
  678. units = unit->next;
  679. for (unit_parent = units; (unit_parent != NULL); unit_parent = unit_parent->next)
  680. {
  681. if (unit_parent->next == unit)
  682. {
  683. unit_parent->next = unit->next;
  684. }
  685. }
  686. free(unit);
  687. unit = NULL;
  688. }
  689. // unit->int_buffer_pos_wr = unit->int_buffer_pos = unit->int_buffer_size = 0;
  690. // unit->int_buffer_pos_wr = unit->int_buffer_pos = 0;
  691. return 0;
  692. }