Baseline for testing
Diff: EventLog.cpp
- Revision:
- 1:aaddf80105fe
- Parent:
- 0:929bb73983c5
- Child:
- 2:486d068deff9
diff -r 929bb73983c5 -r aaddf80105fe EventLog.cpp --- a/EventLog.cpp Thu Jul 25 00:43:28 2019 +0000 +++ b/EventLog.cpp Thu Sep 12 11:28:11 2019 +0000 @@ -1,6 +1,49 @@ #include "mbed.h" #include "EventLog.h" +#include "stdio.h" + + + + +/*------------------------------------------------------------------- + * define local file system + *-------------------------------------------------------------------*/ +LocalFileSystem local("local"); + +const char * filename = "/local/events.bin"; + + +ESCM_Event::ESCM_Event(): address(0),port(0xFF) +{ + +} + +ESCM_Event::ESCM_Event(uint16_t address): address(address),port(0xFF) +{ + setTimeNow(); +} + +void ESCM_Event::setTimeNow(void) +{ + time_t rawtime; + struct tm * timeinfo; + + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); timeinfo = localtime (&rawtime); + + hours = timeinfo->tm_hour; + mins = timeinfo->tm_min; + secs = timeinfo->tm_sec; + year = timeinfo->tm_year+1900; + month = timeinfo->tm_mon + 1; + day = timeinfo->tm_mday; +} + +void ESCM_Event::getTime (char * buffer) { + sprintf(buffer,"%02d:%02d %02d/%02d/%04d", hours,mins,month,day,year); +} + ESCM_EventLog::ESCM_EventLog() { @@ -13,7 +56,7 @@ ESCM_EventLog::~ESCM_EventLog() { } -void ESCM_EventLog::init() +int ESCM_EventLog::init() { full_=false; head_=0; @@ -21,67 +64,115 @@ printf("Initializing Event Log\n\r"); - memset ( events,0,sizeof(ESCM_Event)*MAX_EVENTS); + if ( !load() ) + { + memset ( events,0,sizeof(ESCM_Event)*MAX_EVENTS); + // for testing + for ( int i=0;i<MAX_EVENTS;i++) + { + add(i*2); + } + save(); + + } + + return 1; } int ESCM_EventLog::load() { #if 1 - char * filename = "/local/event_log.bin"; + + printf("Reading %s ...",filename ); FILE *input = fopen(filename, "rb"); - if(input){ + if(input!= NULL){ + + uint16_t code; + uint16_t size; - printf("Reading %s\n\r",filename ); - int size = MAX_EVENTS; + fread(&code, sizeof(uint16_t),1,input); + fread(&size, sizeof(uint16_t),1,input); - fread(&size, sizeof(uint32_t),1,input); + if (code != 0xFAFA && size > 50 ) { + return 0; + } + + fread(&head_, sizeof(uint16_t),1,input); + fread(&tail_, sizeof(uint16_t),1,input); for (int i=0;i<MAX_EVENTS;i++) { printf("." ); - fread( &events[i].address, sizeof(uint32_t),1 , input); - fread( events[i].timeStr, sizeof(char) ,40, input); + fread( &events[i].port, sizeof(uint16_t),1 , input); + fread( &events[i].address, sizeof(uint16_t),1 , input); + fread( &events[i].hours, sizeof(uint16_t),1 , input); + fread( &events[i].mins, sizeof(uint16_t),1 , input); + fread( &events[i].secs, sizeof(uint16_t),1 , input); + fread( &events[i].day, sizeof(uint16_t),1 , input); + fread( &events[i].month, sizeof(uint16_t),1 , input); + fread( &events[i].year, sizeof(uint16_t),1 , input); + + //fread( events[i].timeStr, sizeof(char) ,40, input); } fclose(input); - return 1; } else { printf("Could not Read %s\n\r",filename ); return 0; } -#endif +#endif + return 1; } -void ESCM_EventLog::save() +int ESCM_EventLog::save() { #if 1 - char * filename = "/local/event_log.bin"; - FILE *output = fopen(filename, "wb"); - - printf("Saving %s\n\r",filename ); - int size = MAX_EVENTS; - - fwrite(&size, sizeof(uint32_t),1,output); + printf("Saving %s ...",filename ); - for (int i=0;i<MAX_EVENTS;i++) + FILE *output = fopen(filename, "wb"); + if (output != NULL) { + + uint16_t code = 0xFAFA; + uint16_t size = MAX_EVENTS; + + fwrite(&code, sizeof(uint16_t),1,output); + fwrite(&size, sizeof(uint16_t),1,output); + fwrite(&head_, sizeof(uint16_t),1,output); + fwrite(&tail_, sizeof(uint16_t),1,output); + + for (int i=0;i<MAX_EVENTS;i++) + { + //printf("." ); + fwrite( &events[i].port, sizeof(uint16_t),1 , output); + fwrite( &events[i].address, sizeof(uint16_t),1 , output); + fwrite( &events[i].hours, sizeof(uint16_t),1 , output); + fwrite( &events[i].mins, sizeof(uint16_t),1 , output); + fwrite( &events[i].secs, sizeof(uint16_t),1 , output); + fwrite( &events[i].day, sizeof(uint16_t),1 , output); + fwrite( &events[i].month, sizeof(uint16_t),1 , output); + fwrite( &events[i].year, sizeof(uint16_t),1 , output); + + } + fflush(output); + fclose(output); + } + else { - printf("." ); - fwrite( &events[i].address, sizeof(uint32_t),1 , output); - fwrite( events[i].timeStr, sizeof(char) ,40, output); + printf("Could not save %s\n\r",filename ); + return 0; } - fflush(output); - fclose(output); #endif printf("Done \n\r" ); + return 1; } void ESCM_EventLog::display(Serial *pc) { int j = tail_; - int i = head_; + int i = head_ ; int count = 0 ; pc->printf("\n\r"); @@ -93,30 +184,83 @@ while ( count < size() ) { - pc->printf("%02d > %02d :: %s\n\r", count, events[i].address, events[i].timeStr ); - if ( --i <= 0) { + if (--i < 0 ) { i =( max_size_ - 1 ); } + + pc->printf("%02d <%02d> %02d :: %02d:%02d:%02d %02d/%02d/%04d \n\r", + count, + events[i].port, + events[i].address, + events[i].hours, + events[i].mins, + events[i].secs, + events[i].month, + events[i].day, + events[i].year + ); + count++; } pc->printf("-----------------------------\n\r"); } +void ESCM_EventLog::add(uint16_t address, uint16_t port) +{ + + + ESCM_Event newEvent; + newEvent.address = address; + newEvent.port = port; + newEvent.setTimeNow(); + //sprintf ( newEvent.timeStr , "%s", "==="); + + put( newEvent ); + +} -void ESCM_EventLog::add(int address) +void ESCM_EventLog::add(uint16_t address) +{ + + + ESCM_Event newEvent; + newEvent.address = address; + newEvent.port = 0; + newEvent.setTimeNow(); + //sprintf ( newEvent.timeStr , "%s", "==="); + + put( newEvent ); + +} + +void ESCM_EventLog::add(uint16_t address, char* timeStamp) { ESCM_Event newEvent; newEvent.address = address; - sprintf ( newEvent.timeStr , "%s", "==="); + newEvent.setTimeNow(); + //sprintf ( newEvent.timeStr , "%-16s", timeStamp); put( newEvent ); + } -void ESCM_EventLog::add(int address, char* timeStamp) -{ - ESCM_Event newEvent; - newEvent.address = address; - sprintf ( newEvent.timeStr , "%-16s", timeStamp); +ESCM_Event * ESCM_EventLog::index (int pos ){ + + + ESCM_Event * result = NULL; - put( newEvent ); -} - + if (!empty()) + { + if (pos < size() || pos >= 0 ) { + + int index = (head_ - 1 - pos ); + + if (index < 0 ) + index += max_size_; + + result = &(events[index]); + } + } + + return result; + +} \ No newline at end of file