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.

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