You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "objectPathfinderBinaryLog.h"
  2. #define LOG(X) m_pLog->Log("BILO", this, X)
  3. #define LOG_ERROR(X) m_pLog->Log("BILO", this, _LOG_ERROR, X)
  4. #define PACKAGE_NAME "PATHFINDER_BIN_LOG"
  5. objectPathfinderBinaryLog * objectPathfinderBinaryLog::_instance = NULL;
  6. objectPathfinderBinaryLog * objectPathfinderBinaryLog::getInstance()
  7. {
  8. if (_instance == NULL)
  9. {
  10. _instance = new objectPathfinderBinaryLog();
  11. };
  12. return _instance;
  13. }
  14. objectPathfinderBinaryLog::objectPathfinderBinaryLog()
  15. {
  16. m_pLog = basicLogCollection::getInstance();
  17. m_pFID = NULL;
  18. m_nDBID = 0;
  19. m_pDBC = NULL;
  20. m_bActive = false;
  21. // uuidgen();
  22. }
  23. objectPathfinderBinaryLog::~objectPathfinderBinaryLog()
  24. {
  25. finish();
  26. }
  27. void objectPathfinderBinaryLog::attachDBC(PATHFINDER_DBC * DBC)
  28. {
  29. m_pDBC = DBC;
  30. }
  31. bool objectPathfinderBinaryLog::activate()
  32. {
  33. LOG("ACTIVATING");
  34. if (m_bActive)
  35. {
  36. if (! finish())
  37. {
  38. return false;
  39. }
  40. }
  41. if (m_pDBC == NULL)
  42. {
  43. return false;
  44. }
  45. uuidgen();
  46. char tBuff[256];
  47. sprintf(tBuff, "data/%s.bin", m_sUUID);
  48. m_pFID = fopen(tBuff, "wb+");
  49. if (m_pFID != NULL)
  50. {
  51. PF_TIMESTAMP ts;
  52. getPFTS(&ts);
  53. m_nDBID = m_pDBC->registerBinaryLog(ts.seconds, ts.milliseconds, m_sUUID);
  54. if (m_nDBID)
  55. {
  56. LOG("SUCCESS");
  57. m_bActive = true;
  58. }
  59. else
  60. {
  61. LOG("FAILURE");
  62. fclose(m_pFID);
  63. unlink(tBuff);
  64. };
  65. };
  66. return m_bActive;
  67. }
  68. bool objectPathfinderBinaryLog::deleteDataFile(CSTR name)
  69. {
  70. if (strcmp(name, m_sUUID) == 0)
  71. {
  72. if (m_bActive)
  73. {
  74. if (! finish())
  75. {
  76. return false;
  77. }
  78. }
  79. };
  80. char tBuff[256];
  81. sprintf(tBuff, "data/%s.bin", name);
  82. return (0 == unlink(tBuff));
  83. }
  84. bool objectPathfinderBinaryLog::finish()
  85. {
  86. LOG("DEACTIVATE");
  87. if (! m_bActive)
  88. {
  89. return true;
  90. }
  91. if (m_pFID == NULL)
  92. {
  93. return false;
  94. }
  95. m_bActive = false;
  96. m_mProtector.lock();
  97. fclose(m_pFID);
  98. m_pFID = NULL;
  99. m_mProtector.unlock();
  100. if (m_pDBC != NULL)
  101. {
  102. PF_TIMESTAMP ts;
  103. getPFTS(&ts);
  104. m_pDBC->unregisterBinaryLog(m_nDBID, ts.seconds, ts.milliseconds);
  105. };
  106. m_nDBID = 0;
  107. LOG("SUCCESS");
  108. return true;
  109. }
  110. void objectPathfinderBinaryLog::registerGPS(GPS_EVTREC * ptr)
  111. {
  112. if (! m_bActive)
  113. {
  114. return;
  115. }
  116. if (ptr == NULL)
  117. {
  118. return;
  119. }
  120. if (m_pFID == NULL)
  121. {
  122. LOG_ERROR("Data File Not Opened!");
  123. return;
  124. };
  125. uint16_t recSize;
  126. uint8_t recType = PF_EVT_REC_GPS;
  127. m_mProtector.lock();
  128. recSize = sizeof(GPS_EVTREC);
  129. fwrite(&recType, 1, 1, m_pFID);
  130. fwrite(&recSize, 2, 1, m_pFID);
  131. // -----------------------------------------------
  132. fwrite(ptr, recSize, 1, m_pFID);
  133. if (m_nDBID)
  134. {
  135. m_pDBC->updateBLOG_DATA(m_nDBID, recType, recSize + 3);
  136. };
  137. m_mProtector.unlock();
  138. }
  139. void objectPathfinderBinaryLog::registerBNO(BNO_EVTREC * ptr)
  140. {
  141. if (! m_bActive)
  142. {
  143. return;
  144. }
  145. if (ptr == NULL)
  146. {
  147. return;
  148. }
  149. if (m_pFID == NULL)
  150. {
  151. LOG_ERROR("Data File Not Opened!");
  152. return;
  153. };
  154. uint16_t recSize;
  155. uint8_t recType = PF_EVT_REC_BNO;
  156. m_mProtector.lock();
  157. recSize = sizeof(BNO_EVTREC);
  158. fwrite(&recType, 1, 1, m_pFID);
  159. fwrite(&recSize, 2, 1, m_pFID);
  160. // -----------------------------------------------
  161. fwrite(ptr, recSize, 1, m_pFID);
  162. if (m_nDBID)
  163. {
  164. m_pDBC->updateBLOG_DATA(m_nDBID, recType, recSize + 3);
  165. };
  166. m_mProtector.unlock();
  167. }
  168. void objectPathfinderBinaryLog::registerOPF(OPF_EVTREC * ptr)
  169. {
  170. if (! m_bActive)
  171. {
  172. return;
  173. }
  174. if (ptr == NULL)
  175. {
  176. return;
  177. }
  178. if (m_pFID == NULL)
  179. {
  180. LOG_ERROR("Data File Not Opened!");
  181. return;
  182. };
  183. uint16_t recSize;
  184. uint8_t recType = PF_EVT_REC_OPF;
  185. m_mProtector.lock();
  186. recSize = sizeof(OPF_EVTREC);
  187. fwrite(&recType, 1, 1, m_pFID);
  188. fwrite(&recSize, 2, 1, m_pFID);
  189. // -----------------------------------------------
  190. fwrite(ptr, recSize, 1, m_pFID);
  191. if (m_nDBID)
  192. {
  193. m_pDBC->updateBLOG_DATA(m_nDBID, recType, recSize + 3);
  194. };
  195. m_mProtector.unlock();
  196. }
  197. void objectPathfinderBinaryLog::registerPIC(PF_TIMESTAMP * ts, CSTR name)
  198. {
  199. if (! m_bActive)
  200. {
  201. return;
  202. }
  203. if (ts == NULL)
  204. {
  205. return;
  206. }
  207. if (name == NULL)
  208. {
  209. return;
  210. }
  211. if (m_pFID == NULL)
  212. {
  213. LOG_ERROR("Data File Not Opened!");
  214. return;
  215. };
  216. uint16_t recSize;
  217. uint8_t recType = PF_EVT_REC_PIC;
  218. m_mProtector.lock();
  219. recSize = 7 + strlen(name);
  220. fwrite(&recType, 1, 1, m_pFID);
  221. fwrite(&recSize, 2, 1, m_pFID);
  222. // -----------------------------------------------
  223. fwrite(&ts->seconds, 4, 1, m_pFID);
  224. fwrite(&ts->milliseconds, 2, 1, m_pFID);
  225. fwrite(name, 1, recSize - 6, m_pFID);
  226. LOG(name);
  227. if (m_nDBID)
  228. {
  229. m_pDBC->updateBLOG_DATA(m_nDBID, recType, recSize + 3);
  230. };
  231. m_mProtector.unlock();
  232. }
  233. void objectPathfinderBinaryLog::registerLFS(LRF_EVTREC * ptr)
  234. {
  235. if (! m_bActive)
  236. {
  237. return;
  238. }
  239. if (ptr == NULL)
  240. {
  241. return;
  242. }
  243. if (ptr->samples == NULL)
  244. {
  245. LOG_ERROR("EVT Data Corrupted!");
  246. return;
  247. };
  248. if (m_pFID == NULL)
  249. {
  250. LOG_ERROR("Data File Not Opened!");
  251. return;
  252. };
  253. uint16_t recSize;
  254. uint8_t recType = PF_EVT_REC_LRF;
  255. m_mProtector.lock();
  256. recSize = 15 + (ptr->samples_count << 1);
  257. fwrite(&recType, 1, 1, m_pFID);
  258. fwrite(&recSize, 2, 1, m_pFID);
  259. // -----------------------------------------------
  260. fwrite(&ptr->begin_seconds, 4, 1, m_pFID);
  261. fwrite(&ptr->begin_milliseconds, 2, 1, m_pFID);
  262. fwrite(&ptr->end_seconds, 4, 1, m_pFID);
  263. fwrite(&ptr->end_milliseconds, 2, 1, m_pFID);
  264. fwrite(&ptr->sps, 1, 1, m_pFID);
  265. fwrite(&ptr->samples_count, 2, 1, m_pFID);
  266. fwrite(ptr->samples, 2, ptr->samples_count, m_pFID);
  267. if (m_nDBID)
  268. {
  269. m_pDBC->updateBLOG_DATA(m_nDBID, recType, recSize + 3);
  270. };
  271. m_mProtector.unlock();
  272. }
  273. void objectPathfinderBinaryLog::uuidgen(bool gen)
  274. {
  275. FILE * fp;
  276. char buf[CTEXT];
  277. #ifdef DEBUG
  278. char tBuf[CTEXT];
  279. #endif
  280. if (! gen)
  281. {
  282. const char * file = _PATH_VARDB PACKAGE_NAME ".cache";
  283. fp = fopen(file, "r");
  284. if (! fp)
  285. {
  286. fp = fopen(file, "w");
  287. if (! fp)
  288. {
  289. sprintf(buf, "Cannot create UUID cache, %s: %s", file, strerror(errno));
  290. LOG_ERROR(buf);
  291. };
  292. generate:
  293. srand(time(NULL));
  294. snprintf(buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x", rand() & 0xFFFFFFFF, rand() & 0xFFFF, (rand() & 0x0FFF) | 0x4000, /* M 4 MSB version => version 4 */
  295. (rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */
  296. rand() & 0xFFFFFF, rand() & 0xFFFFFF);
  297. if (fp)
  298. {
  299. #ifdef DEBUG
  300. sprintf(tBuf, "Creating new UUID cache file, %s", file);
  301. LOG(tBuf);
  302. #endif
  303. fprintf(fp, "%s\n", buf);
  304. fclose(fp);
  305. };
  306. }
  307. else
  308. {
  309. #ifdef DEBUG
  310. sprintf(tBuf, "Cache file [%s] is Ok", file);
  311. LOG(tBuf);
  312. #endif
  313. if (! fgets(buf, sizeof(buf), fp))
  314. {
  315. fclose(fp);
  316. goto generate;
  317. }
  318. buf[strlen(buf) - 1] = 0;
  319. fclose(fp);
  320. };
  321. }
  322. else
  323. {
  324. srand(time(NULL));
  325. snprintf(buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x", rand() & 0xFFFFFFFF, rand() & 0xFFFF, (rand() & 0x0FFF) | 0x4000, /* M 4 MSB version => version 4 */
  326. (rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */
  327. rand() & 0xFFFFFF, rand() & 0xFFFFFF);
  328. };
  329. strcpy(m_sUUID, buf);
  330. #ifdef DEBUG
  331. sprintf(tBuf, "URN: %s", m_sUUID);
  332. LOG(tBuf);
  333. #endif
  334. }
  335. #undef LOG
  336. #undef LOG_ERROR