#include "objectPathfinderBinaryLog.h" #define LOG( X ) m_pLog->Log( "BILO", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "BILO", this, _LOG_ERROR, X ) #define PACKAGE_NAME "PATHFINDER_BIN_LOG" objectPathfinderBinaryLog *objectPathfinderBinaryLog::_instance = NULL; objectPathfinderBinaryLog *objectPathfinderBinaryLog::getInstance(){ if( _instance == NULL ){ _instance = new objectPathfinderBinaryLog(); }; return _instance; }; objectPathfinderBinaryLog::objectPathfinderBinaryLog(){ m_pLog = basicLogCollection::getInstance(); m_pFID = NULL; m_nDBID = 0; m_pDBC = NULL; m_bActive = false; //uuidgen(); }; objectPathfinderBinaryLog::~objectPathfinderBinaryLog(){ finish(); }; void objectPathfinderBinaryLog::attachDBC( PATHFINDER_DBC *DBC ){ m_pDBC = DBC; }; bool objectPathfinderBinaryLog::activate(){ LOG("ACTIVATING"); if( m_bActive ) if( !finish() ) return false; if( m_pDBC == NULL ) return false; uuidgen(); char tBuff[256]; sprintf( tBuff, "data/%s.bin", m_sUUID ); m_pFID = fopen( tBuff, "wb+" ); if( m_pFID != NULL ) { PF_TIMESTAMP ts; getPFTS( &ts ); m_nDBID = m_pDBC->registerBinaryLog( ts.seconds, ts.milliseconds, m_sUUID ); if( m_nDBID ) { LOG("SUCCESS"); m_bActive = true; } else { LOG("FAILURE"); fclose( m_pFID ); unlink( tBuff ); }; }; return m_bActive; }; bool objectPathfinderBinaryLog::deleteDataFile( CSTR name ){ if( strcmp( name, m_sUUID ) == 0 ) { if( m_bActive ) if( !finish() ) return false; }; char tBuff[256]; sprintf( tBuff, "data/%s.bin", name ); return ( 0 == unlink( tBuff ) ); }; bool objectPathfinderBinaryLog::finish(){ LOG("DEACTIVATE"); if( !m_bActive ) return true; if( m_pFID == NULL ) return false; m_bActive = false; m_mProtector.lock(); fclose( m_pFID ); m_pFID = NULL; m_mProtector.unlock(); if( m_pDBC != NULL ) { PF_TIMESTAMP ts; getPFTS( &ts ); m_pDBC->unregisterBinaryLog( m_nDBID, ts.seconds, ts.milliseconds ); }; m_nDBID = 0; LOG("SUCCESS"); return true; }; void objectPathfinderBinaryLog::registerGPS( GPS_EVTREC *ptr ){ if( !m_bActive ) return; if( ptr == NULL ) return; if( m_pFID == NULL ){ LOG_ERROR("Data File Not Opened!"); return; }; uint16_t recSize; uint8_t recType = PF_EVT_REC_GPS; m_mProtector.lock(); recSize = sizeof(GPS_EVTREC); fwrite( &recType, 1, 1, m_pFID ); fwrite( &recSize, 2, 1, m_pFID ); // ----------------------------------------------- fwrite( ptr, recSize, 1, m_pFID ); if( m_nDBID ) { m_pDBC->updateBLOG_DATA( m_nDBID, recType, recSize + 3 ); }; m_mProtector.unlock(); }; void objectPathfinderBinaryLog::registerBNO( BNO_EVTREC *ptr ){ if( !m_bActive ) return; if( ptr == NULL ) return; if( m_pFID == NULL ){ LOG_ERROR("Data File Not Opened!"); return; }; uint16_t recSize; uint8_t recType = PF_EVT_REC_BNO; m_mProtector.lock(); recSize = sizeof(BNO_EVTREC); fwrite( &recType, 1, 1, m_pFID ); fwrite( &recSize, 2, 1, m_pFID ); // ----------------------------------------------- fwrite( ptr, recSize, 1, m_pFID ); if( m_nDBID ) { m_pDBC->updateBLOG_DATA( m_nDBID, recType, recSize + 3 ); }; m_mProtector.unlock(); }; void objectPathfinderBinaryLog::registerOPF( OPF_EVTREC *ptr ){ if( !m_bActive ) return; if( ptr == NULL ) return; if( m_pFID == NULL ){ LOG_ERROR("Data File Not Opened!"); return; }; uint16_t recSize; uint8_t recType = PF_EVT_REC_OPF; m_mProtector.lock(); recSize = sizeof(OPF_EVTREC); fwrite( &recType, 1, 1, m_pFID ); fwrite( &recSize, 2, 1, m_pFID ); // ----------------------------------------------- fwrite( ptr, recSize, 1, m_pFID ); if( m_nDBID ) { m_pDBC->updateBLOG_DATA( m_nDBID, recType, recSize + 3 ); }; m_mProtector.unlock(); }; void objectPathfinderBinaryLog::registerPIC( PF_TIMESTAMP *ts, CSTR name ){ if( !m_bActive ) return; if( ts == NULL ) return; if( name == NULL ) return; if( m_pFID == NULL ){ LOG_ERROR("Data File Not Opened!"); return; }; uint16_t recSize; uint8_t recType = PF_EVT_REC_PIC; m_mProtector.lock(); recSize = 7 + strlen(name); fwrite( &recType, 1, 1, m_pFID ); fwrite( &recSize, 2, 1, m_pFID ); // ----------------------------------------------- fwrite( &ts->seconds, 4, 1, m_pFID ); fwrite( &ts->milliseconds, 2, 1, m_pFID ); fwrite( name, 1, recSize - 6, m_pFID ); LOG( name ); if( m_nDBID ) { m_pDBC->updateBLOG_DATA( m_nDBID, recType, recSize + 3 ); }; m_mProtector.unlock(); }; void objectPathfinderBinaryLog::registerLFS( LRF_EVTREC *ptr ){ if( !m_bActive ) return; if( ptr == NULL ) return; if( ptr->samples == NULL ) { LOG_ERROR("EVT Data Corrupted!"); return; }; if( m_pFID == NULL ){ LOG_ERROR("Data File Not Opened!"); return; }; uint16_t recSize; uint8_t recType = PF_EVT_REC_LRF; m_mProtector.lock(); recSize = 15 + ( ptr->samples_count << 1 ); fwrite( &recType, 1, 1, m_pFID ); fwrite( &recSize, 2, 1, m_pFID ); // ----------------------------------------------- fwrite( &ptr->begin_seconds, 4, 1, m_pFID ); fwrite( &ptr->begin_milliseconds, 2, 1, m_pFID ); fwrite( &ptr->end_seconds, 4, 1, m_pFID ); fwrite( &ptr->end_milliseconds, 2, 1, m_pFID ); fwrite( &ptr->sps, 1, 1, m_pFID ); fwrite( &ptr->samples_count, 2, 1, m_pFID ); fwrite( ptr->samples, 2, ptr->samples_count, m_pFID ); if( m_nDBID ) { m_pDBC->updateBLOG_DATA( m_nDBID, recType, recSize + 3 ); }; m_mProtector.unlock(); }; void objectPathfinderBinaryLog::uuidgen( bool gen ){ FILE * fp; char buf[CTEXT]; #ifdef DEBUG char tBuf[CTEXT]; #endif if( !gen ) { const char * file = _PATH_VARDB PACKAGE_NAME ".cache"; fp = fopen(file, "r"); if (!fp) { fp = fopen(file, "w"); if (!fp){ sprintf( buf, "Cannot create UUID cache, %s: %s", file, strerror(errno)); LOG_ERROR( buf ); }; generate: srand(time(NULL)); 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 */ (rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */ rand() & 0xFFFFFF, rand() & 0xFFFFFF ); if (fp) { #ifdef DEBUG sprintf( tBuf, "Creating new UUID cache file, %s", file); LOG( tBuf ); #endif fprintf(fp, "%s\n", buf); fclose(fp); }; } else { #ifdef DEBUG sprintf( tBuf, "Cache file [%s] is Ok", file); LOG( tBuf ); #endif if (!fgets(buf, sizeof(buf), fp)) { fclose(fp); goto generate; } buf[strlen(buf) - 1] = 0; fclose(fp); }; } else { srand(time(NULL)); 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 */ (rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */ rand() & 0xFFFFFF, rand() & 0xFFFFFF ); }; strcpy( m_sUUID, buf ); #ifdef DEBUG sprintf( tBuf, "URN: %s", m_sUUID ); LOG( tBuf ); #endif }; #undef LOG #undef LOG_ERROR