demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
IothubSerial.cpp@8:d98e2dec0f40, 2015-12-31 (annotated)
- Committer:
- henryrawas
- Date:
- Thu Dec 31 17:47:55 2015 +0000
- Revision:
- 8:d98e2dec0f40
- Parent:
- 7:6723f6887d00
- Child:
- 13:ffeff9b5e513
add taps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryrawas | 4:36a4eceb1b7f | 1 | #include "mbed.h" |
henryrawas | 4:36a4eceb1b7f | 2 | #include "IothubSerial.h" |
henryrawas | 4:36a4eceb1b7f | 3 | #include "crt_abstractions.h" |
henryrawas | 4:36a4eceb1b7f | 4 | |
henryrawas | 4:36a4eceb1b7f | 5 | char* nametemp = "temp"; |
henryrawas | 4:36a4eceb1b7f | 6 | char* namevolt = "volt"; |
henryrawas | 4:36a4eceb1b7f | 7 | char* namedeg = "rot"; |
henryrawas | 8:d98e2dec0f40 | 8 | char* nameload = "load"; |
henryrawas | 4:36a4eceb1b7f | 9 | |
henryrawas | 4:36a4eceb1b7f | 10 | IothubSerial::IothubSerial() |
henryrawas | 4:36a4eceb1b7f | 11 | { |
henryrawas | 4:36a4eceb1b7f | 12 | _hasPending = false; |
henryrawas | 4:36a4eceb1b7f | 13 | } |
henryrawas | 4:36a4eceb1b7f | 14 | |
henryrawas | 4:36a4eceb1b7f | 15 | // try to serialize the measurements into the buffer |
henryrawas | 4:36a4eceb1b7f | 16 | // return bytes used or -1 if buffer too small or other error |
henryrawas | 4:36a4eceb1b7f | 17 | // current serialization is a json array with ',' at end |
henryrawas | 4:36a4eceb1b7f | 18 | // eg: temp: [22.0, 23.1, 22.3], |
henryrawas | 4:36a4eceb1b7f | 19 | int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize) |
henryrawas | 4:36a4eceb1b7f | 20 | { |
henryrawas | 4:36a4eceb1b7f | 21 | char* name; |
henryrawas | 4:36a4eceb1b7f | 22 | int slen; |
henryrawas | 4:36a4eceb1b7f | 23 | int startlen = bufsize; |
henryrawas | 4:36a4eceb1b7f | 24 | |
henryrawas | 4:36a4eceb1b7f | 25 | switch (mg.MeasId) |
henryrawas | 4:36a4eceb1b7f | 26 | { |
henryrawas | 4:36a4eceb1b7f | 27 | case NM_Temperature: |
henryrawas | 4:36a4eceb1b7f | 28 | name = nametemp; |
henryrawas | 4:36a4eceb1b7f | 29 | break; |
henryrawas | 4:36a4eceb1b7f | 30 | |
henryrawas | 4:36a4eceb1b7f | 31 | case NM_Voltage: |
henryrawas | 4:36a4eceb1b7f | 32 | name = namevolt; |
henryrawas | 4:36a4eceb1b7f | 33 | break; |
henryrawas | 4:36a4eceb1b7f | 34 | |
henryrawas | 4:36a4eceb1b7f | 35 | case NM_Degrees: |
henryrawas | 4:36a4eceb1b7f | 36 | name = namedeg; |
henryrawas | 4:36a4eceb1b7f | 37 | break; |
henryrawas | 4:36a4eceb1b7f | 38 | |
henryrawas | 8:d98e2dec0f40 | 39 | case NM_Load: |
henryrawas | 8:d98e2dec0f40 | 40 | name = nameload; |
henryrawas | 8:d98e2dec0f40 | 41 | break; |
henryrawas | 8:d98e2dec0f40 | 42 | |
henryrawas | 4:36a4eceb1b7f | 43 | default: |
henryrawas | 4:36a4eceb1b7f | 44 | return -1; |
henryrawas | 4:36a4eceb1b7f | 45 | } |
henryrawas | 4:36a4eceb1b7f | 46 | |
henryrawas | 4:36a4eceb1b7f | 47 | if (bufsize > strlen(name) + 5) |
henryrawas | 4:36a4eceb1b7f | 48 | { |
henryrawas | 4:36a4eceb1b7f | 49 | slen = sprintf_s(buf, bufsize, "\"%s\": [", name); |
henryrawas | 4:36a4eceb1b7f | 50 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 51 | { |
henryrawas | 4:36a4eceb1b7f | 52 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 53 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 54 | } |
henryrawas | 4:36a4eceb1b7f | 55 | else |
henryrawas | 4:36a4eceb1b7f | 56 | return -1; |
henryrawas | 4:36a4eceb1b7f | 57 | } |
henryrawas | 4:36a4eceb1b7f | 58 | else |
henryrawas | 4:36a4eceb1b7f | 59 | return -1; |
henryrawas | 4:36a4eceb1b7f | 60 | |
henryrawas | 4:36a4eceb1b7f | 61 | for (int i = 0; i < mg.NumVals; i++) |
henryrawas | 4:36a4eceb1b7f | 62 | { |
henryrawas | 4:36a4eceb1b7f | 63 | if (bufsize > 9) |
henryrawas | 4:36a4eceb1b7f | 64 | { |
henryrawas | 4:36a4eceb1b7f | 65 | if (i < mg.NumVals - 1) |
henryrawas | 4:36a4eceb1b7f | 66 | slen = sprintf_s(buf, bufsize, "%7.2f, ", mg.MeasVals[i]); |
henryrawas | 4:36a4eceb1b7f | 67 | else |
henryrawas | 4:36a4eceb1b7f | 68 | slen = sprintf_s(buf, bufsize, "%7.2f ", mg.MeasVals[i]); |
henryrawas | 4:36a4eceb1b7f | 69 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 70 | { |
henryrawas | 4:36a4eceb1b7f | 71 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 72 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 73 | } |
henryrawas | 4:36a4eceb1b7f | 74 | else |
henryrawas | 4:36a4eceb1b7f | 75 | return -1; |
henryrawas | 4:36a4eceb1b7f | 76 | } |
henryrawas | 4:36a4eceb1b7f | 77 | else |
henryrawas | 4:36a4eceb1b7f | 78 | return -1; |
henryrawas | 4:36a4eceb1b7f | 79 | } |
henryrawas | 4:36a4eceb1b7f | 80 | |
henryrawas | 4:36a4eceb1b7f | 81 | if (bufsize > 2) |
henryrawas | 4:36a4eceb1b7f | 82 | { |
henryrawas | 4:36a4eceb1b7f | 83 | slen = sprintf_s(buf, bufsize, "],"); |
henryrawas | 4:36a4eceb1b7f | 84 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 85 | { |
henryrawas | 4:36a4eceb1b7f | 86 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 87 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 88 | } |
henryrawas | 4:36a4eceb1b7f | 89 | else |
henryrawas | 4:36a4eceb1b7f | 90 | return -1; |
henryrawas | 4:36a4eceb1b7f | 91 | } |
henryrawas | 4:36a4eceb1b7f | 92 | else |
henryrawas | 4:36a4eceb1b7f | 93 | return -1; |
henryrawas | 4:36a4eceb1b7f | 94 | |
henryrawas | 4:36a4eceb1b7f | 95 | return startlen - bufsize; |
henryrawas | 4:36a4eceb1b7f | 96 | } |
henryrawas | 4:36a4eceb1b7f | 97 | |
henryrawas | 7:6723f6887d00 | 98 | int AddTime(time_t seconds, char* buf, int bufsize) |
henryrawas | 7:6723f6887d00 | 99 | { |
henryrawas | 7:6723f6887d00 | 100 | if (bufsize > 32) |
henryrawas | 7:6723f6887d00 | 101 | { |
henryrawas | 7:6723f6887d00 | 102 | int slen = strftime(buf, 32, "\"time\": \"%FT%T\",", localtime(&seconds)); |
henryrawas | 7:6723f6887d00 | 103 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 104 | { |
henryrawas | 7:6723f6887d00 | 105 | return slen; |
henryrawas | 7:6723f6887d00 | 106 | } |
henryrawas | 7:6723f6887d00 | 107 | else |
henryrawas | 7:6723f6887d00 | 108 | return -1; |
henryrawas | 7:6723f6887d00 | 109 | } |
henryrawas | 7:6723f6887d00 | 110 | else |
henryrawas | 7:6723f6887d00 | 111 | return -1; |
henryrawas | 7:6723f6887d00 | 112 | } |
henryrawas | 7:6723f6887d00 | 113 | |
henryrawas | 4:36a4eceb1b7f | 114 | // try to serialize one or more measurements into the buffer |
henryrawas | 4:36a4eceb1b7f | 115 | // return bytes used or -1 if buffer too small or other error. 0 if no data |
henryrawas | 4:36a4eceb1b7f | 116 | // current serialization is a json object with entry per measure |
henryrawas | 7:6723f6887d00 | 117 | // eg: { "temp": [1, 2], "volt": [12.1, 12.2] } |
henryrawas | 4:36a4eceb1b7f | 118 | int IothubSerial::MeasureBufToString(char* buf, int bufsize) |
henryrawas | 4:36a4eceb1b7f | 119 | { |
henryrawas | 4:36a4eceb1b7f | 120 | int slen; |
henryrawas | 4:36a4eceb1b7f | 121 | bool hasdata = false; |
henryrawas | 4:36a4eceb1b7f | 122 | bool copydata = false; |
henryrawas | 7:6723f6887d00 | 123 | char* startbuf = buf; |
henryrawas | 7:6723f6887d00 | 124 | bool settime = false; |
henryrawas | 7:6723f6887d00 | 125 | char* lastcomma = NULL; |
henryrawas | 7:6723f6887d00 | 126 | |
henryrawas | 7:6723f6887d00 | 127 | time_t secs = 0; |
henryrawas | 4:36a4eceb1b7f | 128 | |
henryrawas | 4:36a4eceb1b7f | 129 | if (bufsize > 1) |
henryrawas | 4:36a4eceb1b7f | 130 | { |
henryrawas | 4:36a4eceb1b7f | 131 | slen = sprintf_s(buf, bufsize, "{"); |
henryrawas | 4:36a4eceb1b7f | 132 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 133 | { |
henryrawas | 4:36a4eceb1b7f | 134 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 135 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 136 | } |
henryrawas | 4:36a4eceb1b7f | 137 | else |
henryrawas | 4:36a4eceb1b7f | 138 | return -1; |
henryrawas | 4:36a4eceb1b7f | 139 | } |
henryrawas | 4:36a4eceb1b7f | 140 | else |
henryrawas | 4:36a4eceb1b7f | 141 | return -1; |
henryrawas | 4:36a4eceb1b7f | 142 | |
henryrawas | 4:36a4eceb1b7f | 143 | if (_hasPending) |
henryrawas | 4:36a4eceb1b7f | 144 | { |
henryrawas | 4:36a4eceb1b7f | 145 | hasdata = true; |
henryrawas | 7:6723f6887d00 | 146 | secs = _pending.Created; |
henryrawas | 7:6723f6887d00 | 147 | slen = AddTime(secs, buf, bufsize); |
henryrawas | 7:6723f6887d00 | 148 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 149 | { |
henryrawas | 7:6723f6887d00 | 150 | bufsize -= slen; |
henryrawas | 7:6723f6887d00 | 151 | buf += slen; |
henryrawas | 7:6723f6887d00 | 152 | settime = true; |
henryrawas | 7:6723f6887d00 | 153 | } |
henryrawas | 7:6723f6887d00 | 154 | else |
henryrawas | 7:6723f6887d00 | 155 | return -1; // no room for pending record |
henryrawas | 7:6723f6887d00 | 156 | |
henryrawas | 4:36a4eceb1b7f | 157 | slen = MeasureGroupToString(_pending, buf, bufsize); |
henryrawas | 4:36a4eceb1b7f | 158 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 159 | { |
henryrawas | 4:36a4eceb1b7f | 160 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 161 | buf += slen; |
henryrawas | 7:6723f6887d00 | 162 | lastcomma = buf; |
henryrawas | 4:36a4eceb1b7f | 163 | _hasPending = false; |
henryrawas | 4:36a4eceb1b7f | 164 | copydata = true; |
henryrawas | 4:36a4eceb1b7f | 165 | } |
henryrawas | 4:36a4eceb1b7f | 166 | else |
henryrawas | 4:36a4eceb1b7f | 167 | return -1; // no room for pending record |
henryrawas | 4:36a4eceb1b7f | 168 | } |
henryrawas | 4:36a4eceb1b7f | 169 | |
henryrawas | 4:36a4eceb1b7f | 170 | while (!MeasureBuf.empty()) |
henryrawas | 4:36a4eceb1b7f | 171 | { |
henryrawas | 4:36a4eceb1b7f | 172 | if (!MeasureBuf.pop(_pending)) |
henryrawas | 4:36a4eceb1b7f | 173 | { |
henryrawas | 4:36a4eceb1b7f | 174 | break; |
henryrawas | 4:36a4eceb1b7f | 175 | } |
henryrawas | 4:36a4eceb1b7f | 176 | hasdata = true; |
henryrawas | 4:36a4eceb1b7f | 177 | _hasPending = true; |
henryrawas | 7:6723f6887d00 | 178 | |
henryrawas | 7:6723f6887d00 | 179 | if (secs != _pending.Created) |
henryrawas | 7:6723f6887d00 | 180 | { |
henryrawas | 7:6723f6887d00 | 181 | if (settime && lastcomma != NULL) |
henryrawas | 7:6723f6887d00 | 182 | { |
henryrawas | 7:6723f6887d00 | 183 | *(lastcomma - 1) = '}'; |
henryrawas | 7:6723f6887d00 | 184 | if (bufsize > 2) |
henryrawas | 7:6723f6887d00 | 185 | { |
henryrawas | 7:6723f6887d00 | 186 | strcpy(buf, ",{"); |
henryrawas | 7:6723f6887d00 | 187 | buf += 2; |
henryrawas | 7:6723f6887d00 | 188 | bufsize -= 2; |
henryrawas | 7:6723f6887d00 | 189 | } |
henryrawas | 7:6723f6887d00 | 190 | } |
henryrawas | 7:6723f6887d00 | 191 | secs = _pending.Created; |
henryrawas | 7:6723f6887d00 | 192 | slen = AddTime(secs, buf, bufsize); |
henryrawas | 7:6723f6887d00 | 193 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 194 | { |
henryrawas | 7:6723f6887d00 | 195 | bufsize -= slen; |
henryrawas | 7:6723f6887d00 | 196 | buf += slen; |
henryrawas | 7:6723f6887d00 | 197 | settime = true; |
henryrawas | 7:6723f6887d00 | 198 | } |
henryrawas | 7:6723f6887d00 | 199 | else |
henryrawas | 7:6723f6887d00 | 200 | break; |
henryrawas | 7:6723f6887d00 | 201 | } |
henryrawas | 7:6723f6887d00 | 202 | |
henryrawas | 4:36a4eceb1b7f | 203 | slen = MeasureGroupToString(_pending, buf, bufsize); |
henryrawas | 4:36a4eceb1b7f | 204 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 205 | { |
henryrawas | 4:36a4eceb1b7f | 206 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 207 | buf += slen; |
henryrawas | 7:6723f6887d00 | 208 | lastcomma = buf; |
henryrawas | 4:36a4eceb1b7f | 209 | _hasPending = false; |
henryrawas | 4:36a4eceb1b7f | 210 | copydata = true; |
henryrawas | 4:36a4eceb1b7f | 211 | } |
henryrawas | 4:36a4eceb1b7f | 212 | else |
henryrawas | 4:36a4eceb1b7f | 213 | break; // no room to serialize, leave pending for mext message |
henryrawas | 4:36a4eceb1b7f | 214 | } |
henryrawas | 4:36a4eceb1b7f | 215 | |
henryrawas | 4:36a4eceb1b7f | 216 | if (!hasdata) |
henryrawas | 4:36a4eceb1b7f | 217 | return 0; // no data |
henryrawas | 4:36a4eceb1b7f | 218 | |
henryrawas | 4:36a4eceb1b7f | 219 | if (!copydata) |
henryrawas | 4:36a4eceb1b7f | 220 | return -1; // have data but buffer too small |
henryrawas | 4:36a4eceb1b7f | 221 | |
henryrawas | 4:36a4eceb1b7f | 222 | // replace final ',' with '}' |
henryrawas | 7:6723f6887d00 | 223 | *(lastcomma - 1) = '}'; |
henryrawas | 7:6723f6887d00 | 224 | |
henryrawas | 7:6723f6887d00 | 225 | return lastcomma - startbuf; |
henryrawas | 7:6723f6887d00 | 226 | } |
henryrawas | 7:6723f6887d00 | 227 | |
henryrawas | 7:6723f6887d00 | 228 | |
henryrawas | 7:6723f6887d00 | 229 | // try to serialize one or more alerts into the buffer |
henryrawas | 7:6723f6887d00 | 230 | // return bytes used or -1 if buffer too small or other error. 0 if no data |
henryrawas | 7:6723f6887d00 | 231 | // current serialization is a json object |
henryrawas | 7:6723f6887d00 | 232 | // eg: { "alert": "msg", "sev": n } |
henryrawas | 7:6723f6887d00 | 233 | int IothubSerial::AlertBufToString(char* buf, int bufsize) |
henryrawas | 7:6723f6887d00 | 234 | { |
henryrawas | 7:6723f6887d00 | 235 | int slen; |
henryrawas | 7:6723f6887d00 | 236 | bool hasdata = false; |
henryrawas | 7:6723f6887d00 | 237 | bool copydata = false; |
henryrawas | 7:6723f6887d00 | 238 | int startlen = bufsize; |
henryrawas | 7:6723f6887d00 | 239 | |
henryrawas | 7:6723f6887d00 | 240 | if (AlertBuf.pop(_pendAlert)) |
henryrawas | 7:6723f6887d00 | 241 | { |
henryrawas | 7:6723f6887d00 | 242 | hasdata = true; |
henryrawas | 7:6723f6887d00 | 243 | slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"alert\": \"%s\", \"sev\": %d }", _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.Sev); |
henryrawas | 7:6723f6887d00 | 244 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 245 | { |
henryrawas | 7:6723f6887d00 | 246 | bufsize -= slen; |
henryrawas | 7:6723f6887d00 | 247 | buf += slen; |
henryrawas | 7:6723f6887d00 | 248 | copydata = true; |
henryrawas | 7:6723f6887d00 | 249 | } |
henryrawas | 7:6723f6887d00 | 250 | } |
henryrawas | 7:6723f6887d00 | 251 | |
henryrawas | 7:6723f6887d00 | 252 | if (!hasdata) |
henryrawas | 7:6723f6887d00 | 253 | return 0; // no data |
henryrawas | 7:6723f6887d00 | 254 | |
henryrawas | 7:6723f6887d00 | 255 | if (!copydata) |
henryrawas | 7:6723f6887d00 | 256 | return -1; // have data but buffer too small |
henryrawas | 7:6723f6887d00 | 257 | |
henryrawas | 4:36a4eceb1b7f | 258 | |
henryrawas | 4:36a4eceb1b7f | 259 | return startlen - bufsize; |
henryrawas | 4:36a4eceb1b7f | 260 | } |
henryrawas | 4:36a4eceb1b7f | 261 | |
henryrawas | 4:36a4eceb1b7f | 262 | bool IothubSerial::HasMeasureGroup() |
henryrawas | 4:36a4eceb1b7f | 263 | { |
henryrawas | 4:36a4eceb1b7f | 264 | return _hasPending; |
henryrawas | 4:36a4eceb1b7f | 265 | } |