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