Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
parser.c
00001 /* 00002 * 00003 */ 00004 00005 #include "mbed.h" 00006 #include "parser.h" 00007 00008 int wrap_protocol(char *c, string *message, list<string> *messages, char header, char footer, char dle ) { 00009 00010 static int state = 0; 00011 static int dle_state = 0; 00012 static int index; 00013 static int size; 00014 00015 00016 switch (state) { 00017 case 0: // start sending a header if there is a valid message available 00018 if (!messages->empty()) { 00019 *message = messages->front(); 00020 messages->pop_front(); // now that we have the message, delete it from the list 00021 index = 0; // initialize sending the actual message 00022 size = message->size(); 00023 state = 1; 00024 *c = header; 00025 return WRAP_SEND; 00026 } 00027 return WRAP_ABORT; 00028 00029 case 1: 00030 if ((size == 0) || (index==size)) { 00031 *c = footer; 00032 state = 0; 00033 return WRAP_FINISHED; 00034 } 00035 00036 *c = (*message)[index]; 00037 00038 if ( dle_state == 1 ) { 00039 dle_state = 0; 00040 index++; 00041 return WRAP_SEND; 00042 } 00043 00044 if ( (*c == header) || (*c == footer) || (*c == dle) ) { 00045 dle_state = 1; 00046 *c = dle; 00047 return WRAP_SEND; 00048 } 00049 00050 index++; 00051 return WRAP_SEND; 00052 } 00053 00054 // if we reach this point, something went wrong! 00055 return WRAP_ABORT; 00056 } 00057 00058 void unwrap_protocol(char *c, string *buffer, list<string> *messages, unsigned int max_message_size, 00059 unsigned int max_messages, char header, char footer, char dle ) { 00060 00061 // these are needed for the internal state of the function 00062 // they are also the reaseon, why this function can only be used in exactly one location 00063 // i.e. the interrupt routine for receiving characters from the RS232 00064 static int state = 0; 00065 static int dle_state = 0; 00066 00067 switch (state) { 00068 case 0: 00069 if (*c == header) { 00070 message_led=!message_led; 00071 state = 1; 00072 } 00073 return; 00074 00075 case 1: 00076 if (*c== header && dle_state==0) { 00077 // new header that is not escaped will set e new message start 00078 buffer->clear(); 00079 return; 00080 } 00081 if (*c == footer && dle_state==0 ) { 00082 // a frame end is only a frame end, if the footer is not escaped 00083 messages->push_back( *buffer ); 00084 buffer->clear(); 00085 state = 0; 00086 message_led=!message_led; 00087 return; 00088 } 00089 00090 // check length of buffer; clear, if too large; restart search for header! 00091 if (buffer->length() > max_message_size) { 00092 // prevent buffer overflows if someone is flooding us with characters 00093 buffer->clear(); 00094 state=0; 00095 return; 00096 } 00097 00098 if (*c == dle && dle_state==0) { 00099 // the next character is "escaped" and is part of the message 00100 dle_state = 1; 00101 return; 00102 } 00103 00104 // probably the easiest and quickest way of resetting the dle_state 00105 dle_state = 0; 00106 00107 // in all other cases, we found a new character and append it to the buffer 00108 *buffer += *c; 00109 } 00110 } 00111 00112 00113
Generated on Tue Jul 12 2022 23:17:07 by
1.7.2