Interplan IM920 library, 920MHz module

Dependents:   IM920_sample IM920_SDlog IM920_sample IM920_sample3 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IM920_msg.cpp Source File

IM920_msg.cpp

00001 #include "IM920.h"
00002 
00003 void IM920::recvData (char c) {
00004     static int sub, len, count;
00005     static char chr;
00006 
00007 #ifdef DEBUG_DUMP
00008     if (c < 0x20 || c >= 0x7f) {
00009         std::printf("_%02x", c);
00010     } else {
00011         std::printf("_%c", c);
00012     }
00013 #endif
00014     switch (_state.mode) {
00015     case MODE_COMMAND:
00016         switch (c) {
00017         case 0:
00018         case 0x0a: // LF
00019         case 0x0d: // CR
00020             _state.buf[len] = 0;
00021             len = 0;
00022             parseMessage();
00023             break;
00024         case ':':
00025             if (_state.buf[2] == ',' && _state.buf[7] == ',' && len == 10) {
00026                 sub = 0;
00027                 _state.mode = MODE_DATA_RX;
00028                 break;
00029             }
00030             /* FALLTHROUGH */
00031         default:
00032             if (len < sizeof(_state.buf) - 1) {
00033                 _state.buf[len] = c;
00034                 len ++;
00035             }
00036             break;
00037         }
00038         break;
00039 
00040     case MODE_DATA_RX:
00041         if (c == '\r' || c == '\n') {
00042             DBG("recv %d/%d\r\n", count, len);
00043             _state.received = true;
00044             _state.mode = MODE_COMMAND;
00045             len = 0;
00046             break;
00047         }
00048         switch (sub) {
00049         case 0:
00050             chr = x2i(c) << 4;
00051             sub ++;
00052             break;
00053         case 1:
00054             chr |= x2i(c);
00055             sub ++;
00056             if (_state.data!= NULL) {
00057                 _state.data->queue(chr);
00058                 if (_state.data->available() >= CFG_DATA_SIZE) {
00059                     _state.received = true;
00060                     WARN("buf full");
00061                 }
00062             }
00063             count ++;
00064             break;
00065         case 2:
00066             if (c == ',') {
00067                 sub = 0;
00068             }
00069             break;
00070         }
00071     }
00072 }
00073 
00074 #define RES_TABLE_NUM 4
00075 int IM920::parseMessage () {
00076     int i;
00077     static const struct RES_TABLE {
00078         const Response res;
00079         void (IM920::*func)(const char*);
00080     } res_table[RES_TABLE_NUM] = {
00081       {RES_NULL,        NULL},
00082       {RES_RDID,        &IM920::resRDID},
00083       {RES_RDNN,        &IM920::resRDNN},
00084       {RES_RDRS,        &IM920::resRDRS},
00085     };
00086 
00087     if (_state.res != RES_NULL) {
00088       for (i = 0; i < RES_TABLE_NUM; i ++) {
00089         if (res_table[i].res == _state.res) {
00090             DBG("parse res %d '%s'\r\n", i, _state.buf);
00091             if (res_table[i].func != NULL) {
00092                 (this->*(res_table[i].func))(_state.buf);
00093             }
00094         }
00095       }
00096     }
00097 
00098     if (strncmp(_state.buf, "OK", 2) == 0) {
00099         _state.ok = true;
00100         if (_state.status == STAT_SLEEP) {
00101             _state.status = STAT_NONE;
00102         }
00103         return 0;
00104     } else
00105     if (strncmp(_state.buf, "NG", 2) == 0) {
00106         _state.failure = true;
00107         return 0;
00108     }
00109 
00110     return -1;
00111 }
00112 
00113 void IM920::resRDID (const char *buf) {
00114 
00115     if (buf[0] < '0' || buf[0] > 'F') return;
00116 
00117     _state.id = strtol(buf, NULL, 16);
00118     _state.res = RES_NULL;
00119 }
00120 
00121 void IM920::resRDNN (const char *buf) {
00122 
00123     if (buf[0] < '0' || buf[0] > 'F') return;
00124 
00125     _state.node = strtol(buf, NULL, 16);
00126     _state.res = RES_NULL;
00127 }
00128 
00129 void IM920::resRDRS (const char *buf) {
00130 
00131     if (buf[0] < '0' || buf[0] > 'F') return;
00132 
00133     _state.rssi = strtol(buf, NULL, 16);
00134     _state.res = RES_NULL;
00135 }
00136