Converted some tab to spaces

This commit is contained in:
Charles 2015-09-12 16:28:46 +02:00
parent 2972d9aae7
commit e3d3054a92
2 changed files with 151 additions and 148 deletions

View File

@ -17,6 +17,8 @@
// //
// All text above must be included in any redistribution. // All text above must be included in any redistribution.
// //
// Edit : Tab size set to 2 but I converted tab to sapces
//
// ********************************************************************************** // **********************************************************************************
#include "LibTeleinfo.h" #include "LibTeleinfo.h"
@ -38,9 +40,9 @@ TInfo::TInfo()
// callback // callback
_fn_ADPS = NULL; _fn_ADPS = NULL;
_fn_data = NULL; _fn_data = NULL;
_fn_new_frame = NULL; _fn_new_frame = NULL;
_fn_updated_frame = NULL; _fn_updated_frame = NULL;
} }
/* ====================================================================== /* ======================================================================
@ -56,10 +58,10 @@ void TInfo::init()
listDelete(); listDelete();
// clear our receive buffer // clear our receive buffer
clearBuffer(); clearBuffer();
// We're in INIT in term of receive data // We're in INIT in term of receive data
_state = TINFO_INIT; _state = TINFO_INIT;
} }
/* ====================================================================== /* ======================================================================
@ -71,8 +73,8 @@ Comments: -
====================================================================== */ ====================================================================== */
void TInfo::attachADPS(void (*fn_ADPS)(uint8_t phase)) void TInfo::attachADPS(void (*fn_ADPS)(uint8_t phase))
{ {
// indicate the user callback // indicate the user callback
_fn_ADPS = fn_ADPS; _fn_ADPS = fn_ADPS;
} }
/* ====================================================================== /* ======================================================================
@ -84,8 +86,8 @@ Comments: -
====================================================================== */ ====================================================================== */
void TInfo::attachData(void (*fn_data)(ValueList * valueslist, uint8_t state)) void TInfo::attachData(void (*fn_data)(ValueList * valueslist, uint8_t state))
{ {
// indicate the user callback // indicate the user callback
_fn_data = fn_data; _fn_data = fn_data;
} }
/* ====================================================================== /* ======================================================================
@ -97,22 +99,22 @@ Comments: -
====================================================================== */ ====================================================================== */
void TInfo::attachNewFrame(void (*fn_new_frame)(ValueList * valueslist)) void TInfo::attachNewFrame(void (*fn_new_frame)(ValueList * valueslist))
{ {
// indicate the user callback // indicate the user callback
_fn_new_frame = fn_new_frame; _fn_new_frame = fn_new_frame;
} }
/* ====================================================================== /* ======================================================================
Function: attachChangedFrame Function: attachChangedFrame
Purpose : attach a callback when we received a full frame where data Purpose : attach a callback when we received a full frame where data
has changed since the last frame (cool to update data) has changed since the last frame (cool to update data)
Input : callback function Input : callback function
Output : - Output : -
Comments: - Comments: -
====================================================================== */ ====================================================================== */
void TInfo::attachUpdatedFrame(void (*fn_updated_frame)(ValueList * valueslist)) void TInfo::attachUpdatedFrame(void (*fn_updated_frame)(ValueList * valueslist))
{ {
// indicate the user callback // indicate the user callback
_fn_updated_frame = fn_updated_frame; _fn_updated_frame = fn_updated_frame;
} }
/* ====================================================================== /* ======================================================================
@ -124,9 +126,9 @@ Comments: -
====================================================================== */ ====================================================================== */
uint8_t TInfo::clearBuffer() uint8_t TInfo::clearBuffer()
{ {
// Clear our buffer, set index to 0 // Clear our buffer, set index to 0
memset(_recv_buff, 0, TINFO_BUFSIZE); memset(_recv_buff, 0, TINFO_BUFSIZE);
_recv_idx = 0; _recv_idx = 0;
} }
@ -142,8 +144,7 @@ Comments: checksum is calculated before adding, no need to bother with
ValueList * TInfo::addCustomValue(char * name, char * value, uint8_t * flags) ValueList * TInfo::addCustomValue(char * name, char * value, uint8_t * flags)
{ {
// Little check // Little check
if (name && *name && value && *value) if (name && *name && value && *value) {
{
ValueList * me; ValueList * me;
// Same as if we really received this line // Same as if we really received this line
@ -176,11 +177,11 @@ Comments: - state of the label changed by the function
====================================================================== */ ====================================================================== */
ValueList * TInfo::valueAdd(char * name, char * value, uint8_t checksum, uint8_t * flags) ValueList * TInfo::valueAdd(char * name, char * value, uint8_t checksum, uint8_t * flags)
{ {
// Get our linked list // Get our linked list
ValueList * me = &_valueslist; ValueList * me = &_valueslist;
uint8_t lgname = strlen(name); uint8_t lgname = strlen(name);
uint8_t lgvalue = strlen(value); uint8_t lgvalue = strlen(value);
uint8_t thischeck = calcChecksum(name,value); uint8_t thischeck = calcChecksum(name,value);
// just some paranoia // just some paranoia
@ -246,12 +247,12 @@ ValueList * TInfo::valueAdd(char * name, char * value, uint8_t checksum, uint8_t
// Our linked list structure sizeof(ValueList) // Our linked list structure sizeof(ValueList)
// + Name + '\0' // + Name + '\0'
// + Value + '\0' // + Value + '\0'
size_t size = sizeof(ValueList) + lgname + 1 + lgvalue + 1 ; size_t size = sizeof(ValueList) + lgname + 1 + lgvalue + 1 ;
// Create new node with size to store strings // Create new node with size to store strings
if ((newNode = (ValueList *) malloc(size) ) == NULL) if ((newNode = (ValueList *) malloc(size) ) == NULL)
return ( (ValueList *) NULL ); return ( (ValueList *) NULL );
else else
// get our buffer Safe // get our buffer Safe
memset(newNode, 0, size); memset(newNode, 0, size);
// Put the new node on the list // Put the new node on the list
@ -426,8 +427,8 @@ Output : Pointer
====================================================================== */ ====================================================================== */
ValueList * TInfo::getList(void) ValueList * TInfo::getList(void)
{ {
// Get our linked list // Get our linked list
return &_valueslist; return &_valueslist;
} }
/* ====================================================================== /* ======================================================================
@ -438,9 +439,9 @@ Output : total number of values
====================================================================== */ ====================================================================== */
uint8_t TInfo::valuesDump(void) uint8_t TInfo::valuesDump(void)
{ {
// Get our linked list // Get our linked list
ValueList * me = &_valueslist; ValueList * me = &_valueslist;
uint8_t index = 0; uint8_t index = 0;
// Got one ? // Got one ?
if (me) { if (me) {
@ -454,16 +455,16 @@ uint8_t TInfo::valuesDump(void)
TI_Debug(F(") ")) ; TI_Debug(F(") ")) ;
if (me->name) if (me->name)
TI_Debug(me->name) ; TI_Debug(me->name) ;
else else
TI_Debug(F("NULL")) ; TI_Debug(F("NULL")) ;
TI_Debug(F("=")) ; TI_Debug(F("=")) ;
if (me->value) if (me->value)
TI_Debug(me->value) ; TI_Debug(me->value) ;
else else
TI_Debug(F("NULL")) ; TI_Debug(F("NULL")) ;
TI_Debug(F(" '")) ; TI_Debug(F(" '")) ;
TI_Debug(me->checksum) ; TI_Debug(me->checksum) ;
@ -498,8 +499,8 @@ int TInfo::labelCount()
{ {
int count = 0; int count = 0;
// Get our linked list // Get our linked list
ValueList * me = &_valueslist; ValueList * me = &_valueslist;
if (me) if (me)
while ((me = me->next)) while ((me = me->next))
@ -516,8 +517,8 @@ Output : True if Ok False Otherwise
====================================================================== */ ====================================================================== */
boolean TInfo::listDelete() boolean TInfo::listDelete()
{ {
// Get our linked list // Get our linked list
ValueList * me = &_valueslist; ValueList * me = &_valueslist;
// Got a pointer // Got a pointer
if (me) { if (me) {
@ -546,7 +547,7 @@ boolean TInfo::listDelete()
Function: checksum Function: checksum
Purpose : calculate the checksum based on data/value fields Purpose : calculate the checksum based on data/value fields
Input : label name Input : label name
label value label value
Output : checksum Output : checksum
Comments: return '\0' in case of error Comments: return '\0' in case of error
====================================================================== */ ====================================================================== */
@ -557,16 +558,16 @@ unsigned char TInfo::calcChecksum(char *etiquette, char *valeur)
// avoid dead loop, always check all is fine // avoid dead loop, always check all is fine
if (etiquette && valeur) { if (etiquette && valeur) {
// this will not hurt and may save our life ;-) // this will not hurt and may save our life ;-)
if (strlen(etiquette) && strlen(valeur)) { if (strlen(etiquette) && strlen(valeur)) {
while (*etiquette) while (*etiquette)
sum += *etiquette++ ; sum += *etiquette++ ;
while(*valeur) while(*valeur)
sum += *valeur++ ; sum += *valeur++ ;
return ( (sum & 63) + ' ' ) ; return ( (sum & 63) + ' ' ) ;
} }
} }
return 0; return 0;
} }
@ -593,10 +594,10 @@ void TInfo::customLabel( char * plabel, char * pvalue, uint8_t * pflags)
// *pflags |= TINFO_FLAGS_ALERT; // *pflags |= TINFO_FLAGS_ALERT;
//} //}
// triphasé c'est ADIR + Num Phase // triphasé c'est ADIR + Num Phase
if (plabel[0]=='A' && plabel[1]=='D' && plabel[2]=='I' && plabel[3]=='R' ) if (plabel[0]=='A' && plabel[1]=='D' && plabel[2]=='I' && plabel[3]=='R' && plabel[4]>='1' && plabel[4]<='3') {
if (plabel[4]>='1' && plabel[4]<='3') phase = plabel[4]-'0';
phase = plabel[4]-'0'; }
// Nous avons un ADPS ? // Nous avons un ADPS ?
if (phase>=0 && phase <=3) { if (phase>=0 && phase <=3) {
@ -625,24 +626,24 @@ ValueList * TInfo::checkLine(char * pline)
char checksum; char checksum;
char buff[TINFO_BUFSIZE]; char buff[TINFO_BUFSIZE];
uint8_t flags = TINFO_FLAGS_NONE; uint8_t flags = TINFO_FLAGS_NONE;
boolean err = true ; // Assume error boolean err = true ; // Assume error
int len ; // Group len int len ; // Group len
if (pline==NULL) if (pline==NULL)
return NULL; return NULL;
len = strlen(pline); len = strlen(pline);
// a line should be at least 7 Char // a line should be at least 7 Char
// 2 Label + Space + 1 etiquette + space + checksum + \r // 2 Label + Space + 1 etiquette + space + checksum + \r
if ( len < 7 ) if ( len < 7 )
return NULL; return NULL;
// Get our own working copy // Get our own working copy
strncpy( buff, _recv_buff, len+1); strncpy( buff, _recv_buff, len+1);
p = &buff[0]; p = &buff[0];
ptok = p; // for sure we start with token name ptok = p; // for sure we start with token name
pend = p + len; // max size pend = p + len; // max size
// Init values // Init values
@ -665,7 +666,7 @@ ValueList * TInfo::checkLine(char * pline)
if (!pvalue) if (!pvalue)
pvalue = p; pvalue = p;
else else
// 2nd space, so it's the checksum // 2nd space, so it's the checksum
checksum = *p; checksum = *p;
} }
// new line ? ok we got all we need ? // new line ? ok we got all we need ?
@ -675,30 +676,30 @@ ValueList * TInfo::checkLine(char * pline)
// Good format ? // Good format ?
if ( ptok && pvalue && checksum ) { if ( ptok && pvalue && checksum ) {
// Always check to avoid bad behavior // Always check to avoid bad behavior
if(strlen(ptok) && strlen(pvalue)) { if(strlen(ptok) && strlen(pvalue)) {
// Is checksum is OK // Is checksum is OK
if ( calcChecksum(ptok,pvalue) == checksum) { if ( calcChecksum(ptok,pvalue) == checksum) {
// In case we need to do things on specific labels // In case we need to do things on specific labels
customLabel(ptok, pvalue, &flags); customLabel(ptok, pvalue, &flags);
// Add value to linked lists of values // Add value to linked lists of values
ValueList * me = valueAdd(ptok, pvalue, checksum, &flags); ValueList * me = valueAdd(ptok, pvalue, checksum, &flags);
// value correctly added/changed // value correctly added/changed
if ( me ) { if ( me ) {
// something to do with new datas // something to do with new datas
if (flags & (TINFO_FLAGS_UPDATED | TINFO_FLAGS_ADDED | TINFO_FLAGS_ALERT) ) { if (flags & (TINFO_FLAGS_UPDATED | TINFO_FLAGS_ADDED | TINFO_FLAGS_ALERT) ) {
// this frame will for sure be updated // this frame will for sure be updated
_frame_updated = true; _frame_updated = true;
// Do we need to advertise user callback // Do we need to advertise user callback
if (_fn_data) if (_fn_data)
_fn_data(me, flags); _fn_data(me, flags);
} }
} }
} }
} }
} }
} }
// Next char // Next char
@ -721,37 +722,37 @@ _State_e TInfo::process(char c)
// be sure 7 bits only // be sure 7 bits only
c &= 0x7F; c &= 0x7F;
// What we received ? // What we received ?
switch (c) { switch (c) {
// start of transmission ??? // start of transmission ???
case TINFO_STX: case TINFO_STX:
// Clear buffer, begin to store in it // Clear buffer, begin to store in it
clearBuffer(); clearBuffer();
// by default frame is not "updated" // by default frame is not "updated"
// if data change we'll set this flag // if data change we'll set this flag
_frame_updated = false; _frame_updated = false;
// We were waiting fo this one ? // We were waiting fo this one ?
if (_state == TINFO_INIT || _state == TINFO_WAIT_STX ) { if (_state == TINFO_INIT || _state == TINFO_WAIT_STX ) {
TI_Debugln(F("TINFO_WAIT_ETX")); TI_Debugln(F("TINFO_WAIT_ETX"));
_state = TINFO_WAIT_ETX; _state = TINFO_WAIT_ETX;
} }
break; break;
// End of transmission ? // End of transmission ?
case TINFO_ETX: case TINFO_ETX:
// Normal working mode ? // Normal working mode ?
if (_state == TINFO_READY) { if (_state == TINFO_READY) {
// Get on top of our linked list // Get on top of our linked list
ValueList * me = &_valueslist; ValueList * me = &_valueslist;
// Call user callback if any // Call user callback if any
if (_frame_updated && _fn_updated_frame) if (_frame_updated && _fn_updated_frame)
_fn_updated_frame(me); _fn_updated_frame(me);
else if (_fn_new_frame) else if (_fn_new_frame)
_fn_new_frame(me); _fn_new_frame(me);
#ifdef TI_Debug #ifdef TI_Debug
valuesDump(); valuesDump();
@ -761,59 +762,59 @@ _State_e TInfo::process(char c)
// to remove the alert flags from table (ADPS for example) // to remove the alert flags from table (ADPS for example)
// it will be put back again next time if any // it will be put back again next time if any
valueRemoveFlagged(TINFO_FLAGS_ALERT); valueRemoveFlagged(TINFO_FLAGS_ALERT);
} }
// We were waiting fo this one ? // We were waiting fo this one ?
if (_state == TINFO_WAIT_ETX) { if (_state == TINFO_WAIT_ETX) {
TI_Debugln(F("TINFO_READY")); TI_Debugln(F("TINFO_READY"));
_state = TINFO_READY; _state = TINFO_READY;
} }
else if ( _state == TINFO_INIT) { else if ( _state == TINFO_INIT) {
TI_Debugln(F("TINFO_WAIT_STX")); TI_Debugln(F("TINFO_WAIT_STX"));
_state = TINFO_WAIT_STX ; _state = TINFO_WAIT_STX ;
} }
break; break;
// Start of group \n ? // Start of group \n ?
case TINFO_SGR: case TINFO_SGR:
// Do nothing we'll work at end of group // Do nothing we'll work at end of group
// we can safely ignore this char // we can safely ignore this char
break; break;
// End of group \r ? // End of group \r ?
case TINFO_EGR: case TINFO_EGR:
// Are we ready to process ? // Are we ready to process ?
if (_state == TINFO_READY) { if (_state == TINFO_READY) {
// Store data recceived (we'll need it) // Store data recceived (we'll need it)
if ( _recv_idx < TINFO_BUFSIZE) if ( _recv_idx < TINFO_BUFSIZE)
_recv_buff[_recv_idx++]=c; _recv_buff[_recv_idx++]=c;
// clear the end of buffer (paranoia inside) // clear the end of buffer (paranoia inside)
memset(&_recv_buff[_recv_idx], 0, TINFO_BUFSIZE-_recv_idx); memset(&_recv_buff[_recv_idx], 0, TINFO_BUFSIZE-_recv_idx);
// check the group we've just received // check the group we've just received
checkLine(_recv_buff) ; checkLine(_recv_buff) ;
// Whatever error or not, we done // Whatever error or not, we done
clearBuffer(); clearBuffer();
} }
break; break;
// other char ? // other char ?
default: default:
{ {
// Only in a ready state of course // Only in a ready state of course
if (_state == TINFO_READY) { if (_state == TINFO_READY) {
// If buffer is not full, Store data // If buffer is not full, Store data
if ( _recv_idx < TINFO_BUFSIZE) if ( _recv_idx < TINFO_BUFSIZE)
_recv_buff[_recv_idx++]=c; _recv_buff[_recv_idx++]=c;
else else
clearBuffer(); clearBuffer();
} }
} }
break; break;
} }
} }

View File

@ -17,6 +17,8 @@
// //
// All text above must be included in any redistribution. // All text above must be included in any redistribution.
// //
// Edit : Tab size set to 2 but I converted tab to sapces
//
// ********************************************************************************** // **********************************************************************************
#ifndef LibTeleinfo_h #ifndef LibTeleinfo_h