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.

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