Interplan IM920 library, 920MHz module
Dependents: IM920_sample IM920_SDlog IM920_sample IM920_sample3 ... more
IM920 ライブラリ
データモード、低速、長距離 の設定で通信するライブラリです。
920MHz無線モジュール
- mbedとモジュールとは、シリアル(TX,RX)、BUSY、RESET端子を接続します。
- モジュールはあらかじめ、コマンドでノード番号などを設定しておきます。
IM920_msg.cpp@5:2fd9b1725283, 2015-04-24 (annotated)
- Committer:
- idealtechlab
- Date:
- Fri Apr 24 03:59:13 2015 +0000
- Revision:
- 5:2fd9b1725283
- Parent:
- 2:0b47f6b25cc4
fix buffer over run
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:d3ab05ed8142 | 1 | #include "IM920.h" |
okini3939 | 0:d3ab05ed8142 | 2 | |
okini3939 | 0:d3ab05ed8142 | 3 | void IM920::recvData (char c) { |
okini3939 | 0:d3ab05ed8142 | 4 | static int sub, len, count; |
okini3939 | 0:d3ab05ed8142 | 5 | static char chr; |
okini3939 | 0:d3ab05ed8142 | 6 | |
okini3939 | 0:d3ab05ed8142 | 7 | #ifdef DEBUG_DUMP |
okini3939 | 0:d3ab05ed8142 | 8 | if (c < 0x20 || c >= 0x7f) { |
okini3939 | 0:d3ab05ed8142 | 9 | std::printf("_%02x", c); |
okini3939 | 0:d3ab05ed8142 | 10 | } else { |
okini3939 | 0:d3ab05ed8142 | 11 | std::printf("_%c", c); |
okini3939 | 0:d3ab05ed8142 | 12 | } |
okini3939 | 0:d3ab05ed8142 | 13 | #endif |
okini3939 | 0:d3ab05ed8142 | 14 | switch (_state.mode) { |
okini3939 | 0:d3ab05ed8142 | 15 | case MODE_COMMAND: |
okini3939 | 0:d3ab05ed8142 | 16 | switch (c) { |
okini3939 | 0:d3ab05ed8142 | 17 | case 0: |
okini3939 | 0:d3ab05ed8142 | 18 | case 0x0a: // LF |
okini3939 | 0:d3ab05ed8142 | 19 | case 0x0d: // CR |
okini3939 | 0:d3ab05ed8142 | 20 | _state.buf[len] = 0; |
okini3939 | 0:d3ab05ed8142 | 21 | len = 0; |
okini3939 | 0:d3ab05ed8142 | 22 | parseMessage(); |
okini3939 | 0:d3ab05ed8142 | 23 | break; |
okini3939 | 0:d3ab05ed8142 | 24 | case ':': |
okini3939 | 0:d3ab05ed8142 | 25 | if (_state.buf[2] == ',' && _state.buf[7] == ',' && len == 10) { |
okini3939 | 0:d3ab05ed8142 | 26 | sub = 0; |
okini3939 | 0:d3ab05ed8142 | 27 | _state.mode = MODE_DATA_RX; |
okini3939 | 0:d3ab05ed8142 | 28 | break; |
okini3939 | 0:d3ab05ed8142 | 29 | } |
okini3939 | 0:d3ab05ed8142 | 30 | /* FALLTHROUGH */ |
okini3939 | 0:d3ab05ed8142 | 31 | default: |
okini3939 | 0:d3ab05ed8142 | 32 | if (len < sizeof(_state.buf) - 1) { |
okini3939 | 0:d3ab05ed8142 | 33 | _state.buf[len] = c; |
idealtechlab | 5:2fd9b1725283 | 34 | len ++; |
okini3939 | 0:d3ab05ed8142 | 35 | } |
okini3939 | 0:d3ab05ed8142 | 36 | break; |
okini3939 | 0:d3ab05ed8142 | 37 | } |
okini3939 | 0:d3ab05ed8142 | 38 | break; |
okini3939 | 0:d3ab05ed8142 | 39 | |
okini3939 | 0:d3ab05ed8142 | 40 | case MODE_DATA_RX: |
okini3939 | 0:d3ab05ed8142 | 41 | if (c == '\r' || c == '\n') { |
okini3939 | 0:d3ab05ed8142 | 42 | DBG("recv %d/%d\r\n", count, len); |
okini3939 | 0:d3ab05ed8142 | 43 | _state.received = true; |
okini3939 | 0:d3ab05ed8142 | 44 | _state.mode = MODE_COMMAND; |
okini3939 | 0:d3ab05ed8142 | 45 | len = 0; |
okini3939 | 0:d3ab05ed8142 | 46 | break; |
okini3939 | 0:d3ab05ed8142 | 47 | } |
okini3939 | 0:d3ab05ed8142 | 48 | switch (sub) { |
okini3939 | 0:d3ab05ed8142 | 49 | case 0: |
okini3939 | 0:d3ab05ed8142 | 50 | chr = x2i(c) << 4; |
okini3939 | 0:d3ab05ed8142 | 51 | sub ++; |
okini3939 | 0:d3ab05ed8142 | 52 | break; |
okini3939 | 0:d3ab05ed8142 | 53 | case 1: |
okini3939 | 0:d3ab05ed8142 | 54 | chr |= x2i(c); |
okini3939 | 0:d3ab05ed8142 | 55 | sub ++; |
okini3939 | 0:d3ab05ed8142 | 56 | if (_state.data!= NULL) { |
okini3939 | 0:d3ab05ed8142 | 57 | _state.data->queue(chr); |
idealtechlab | 2:0b47f6b25cc4 | 58 | if (_state.data->available() >= CFG_DATA_SIZE) { |
okini3939 | 0:d3ab05ed8142 | 59 | _state.received = true; |
okini3939 | 0:d3ab05ed8142 | 60 | WARN("buf full"); |
okini3939 | 0:d3ab05ed8142 | 61 | } |
okini3939 | 0:d3ab05ed8142 | 62 | } |
okini3939 | 0:d3ab05ed8142 | 63 | count ++; |
okini3939 | 0:d3ab05ed8142 | 64 | break; |
okini3939 | 0:d3ab05ed8142 | 65 | case 2: |
okini3939 | 0:d3ab05ed8142 | 66 | if (c == ',') { |
okini3939 | 0:d3ab05ed8142 | 67 | sub = 0; |
okini3939 | 0:d3ab05ed8142 | 68 | } |
okini3939 | 0:d3ab05ed8142 | 69 | break; |
okini3939 | 0:d3ab05ed8142 | 70 | } |
okini3939 | 0:d3ab05ed8142 | 71 | } |
okini3939 | 0:d3ab05ed8142 | 72 | } |
okini3939 | 0:d3ab05ed8142 | 73 | |
okini3939 | 0:d3ab05ed8142 | 74 | #define RES_TABLE_NUM 4 |
okini3939 | 0:d3ab05ed8142 | 75 | int IM920::parseMessage () { |
okini3939 | 0:d3ab05ed8142 | 76 | int i; |
okini3939 | 0:d3ab05ed8142 | 77 | static const struct RES_TABLE { |
okini3939 | 0:d3ab05ed8142 | 78 | const Response res; |
okini3939 | 0:d3ab05ed8142 | 79 | void (IM920::*func)(const char*); |
okini3939 | 0:d3ab05ed8142 | 80 | } res_table[RES_TABLE_NUM] = { |
okini3939 | 0:d3ab05ed8142 | 81 | {RES_NULL, NULL}, |
okini3939 | 0:d3ab05ed8142 | 82 | {RES_RDID, &IM920::resRDID}, |
okini3939 | 0:d3ab05ed8142 | 83 | {RES_RDNN, &IM920::resRDNN}, |
okini3939 | 0:d3ab05ed8142 | 84 | {RES_RDRS, &IM920::resRDRS}, |
okini3939 | 0:d3ab05ed8142 | 85 | }; |
okini3939 | 0:d3ab05ed8142 | 86 | |
okini3939 | 0:d3ab05ed8142 | 87 | if (_state.res != RES_NULL) { |
okini3939 | 0:d3ab05ed8142 | 88 | for (i = 0; i < RES_TABLE_NUM; i ++) { |
okini3939 | 0:d3ab05ed8142 | 89 | if (res_table[i].res == _state.res) { |
okini3939 | 0:d3ab05ed8142 | 90 | DBG("parse res %d '%s'\r\n", i, _state.buf); |
okini3939 | 0:d3ab05ed8142 | 91 | if (res_table[i].func != NULL) { |
okini3939 | 0:d3ab05ed8142 | 92 | (this->*(res_table[i].func))(_state.buf); |
okini3939 | 0:d3ab05ed8142 | 93 | } |
okini3939 | 0:d3ab05ed8142 | 94 | } |
okini3939 | 0:d3ab05ed8142 | 95 | } |
okini3939 | 0:d3ab05ed8142 | 96 | } |
okini3939 | 0:d3ab05ed8142 | 97 | |
okini3939 | 0:d3ab05ed8142 | 98 | if (strncmp(_state.buf, "OK", 2) == 0) { |
okini3939 | 0:d3ab05ed8142 | 99 | _state.ok = true; |
okini3939 | 0:d3ab05ed8142 | 100 | if (_state.status == STAT_SLEEP) { |
okini3939 | 0:d3ab05ed8142 | 101 | _state.status = STAT_NONE; |
okini3939 | 0:d3ab05ed8142 | 102 | } |
okini3939 | 0:d3ab05ed8142 | 103 | return 0; |
okini3939 | 0:d3ab05ed8142 | 104 | } else |
okini3939 | 0:d3ab05ed8142 | 105 | if (strncmp(_state.buf, "NG", 2) == 0) { |
okini3939 | 0:d3ab05ed8142 | 106 | _state.failure = true; |
okini3939 | 0:d3ab05ed8142 | 107 | return 0; |
okini3939 | 0:d3ab05ed8142 | 108 | } |
okini3939 | 0:d3ab05ed8142 | 109 | |
okini3939 | 0:d3ab05ed8142 | 110 | return -1; |
okini3939 | 0:d3ab05ed8142 | 111 | } |
okini3939 | 0:d3ab05ed8142 | 112 | |
okini3939 | 0:d3ab05ed8142 | 113 | void IM920::resRDID (const char *buf) { |
okini3939 | 0:d3ab05ed8142 | 114 | |
okini3939 | 0:d3ab05ed8142 | 115 | if (buf[0] < '0' || buf[0] > 'F') return; |
okini3939 | 0:d3ab05ed8142 | 116 | |
okini3939 | 0:d3ab05ed8142 | 117 | _state.id = strtol(buf, NULL, 16); |
okini3939 | 0:d3ab05ed8142 | 118 | _state.res = RES_NULL; |
okini3939 | 0:d3ab05ed8142 | 119 | } |
okini3939 | 0:d3ab05ed8142 | 120 | |
okini3939 | 0:d3ab05ed8142 | 121 | void IM920::resRDNN (const char *buf) { |
okini3939 | 0:d3ab05ed8142 | 122 | |
okini3939 | 0:d3ab05ed8142 | 123 | if (buf[0] < '0' || buf[0] > 'F') return; |
okini3939 | 0:d3ab05ed8142 | 124 | |
okini3939 | 0:d3ab05ed8142 | 125 | _state.node = strtol(buf, NULL, 16); |
okini3939 | 0:d3ab05ed8142 | 126 | _state.res = RES_NULL; |
okini3939 | 0:d3ab05ed8142 | 127 | } |
okini3939 | 0:d3ab05ed8142 | 128 | |
okini3939 | 0:d3ab05ed8142 | 129 | void IM920::resRDRS (const char *buf) { |
okini3939 | 0:d3ab05ed8142 | 130 | |
okini3939 | 0:d3ab05ed8142 | 131 | if (buf[0] < '0' || buf[0] > 'F') return; |
okini3939 | 0:d3ab05ed8142 | 132 | |
okini3939 | 0:d3ab05ed8142 | 133 | _state.rssi = strtol(buf, NULL, 16); |
okini3939 | 0:d3ab05ed8142 | 134 | _state.res = RES_NULL; |
okini3939 | 0:d3ab05ed8142 | 135 | } |
okini3939 | 0:d3ab05ed8142 | 136 |