demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Committer:
henryrawas
Date:
Sat Jan 23 00:20:27 2016 +0000
Revision:
19:2f0ec9ac1238
Parent:
18:224289104fc0
Child:
20:891b5270845a
licenses

Who changed what in which revision?

UserRevisionLine numberNew 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 7:6723f6887d00 63 int AddTime(time_t seconds, char* buf, int bufsize)
henryrawas 7:6723f6887d00 64 {
henryrawas 7:6723f6887d00 65 if (bufsize > 32)
henryrawas 7:6723f6887d00 66 {
henryrawas 7:6723f6887d00 67 int slen = strftime(buf, 32, "\"time\": \"%FT%T\",", localtime(&seconds));
henryrawas 7:6723f6887d00 68 if (slen > 0)
henryrawas 7:6723f6887d00 69 {
henryrawas 7:6723f6887d00 70 return slen;
henryrawas 7:6723f6887d00 71 }
henryrawas 7:6723f6887d00 72 else
henryrawas 7:6723f6887d00 73 return -1;
henryrawas 7:6723f6887d00 74 }
henryrawas 7:6723f6887d00 75 else
henryrawas 7:6723f6887d00 76 return -1;
henryrawas 7:6723f6887d00 77 }
henryrawas 7:6723f6887d00 78
henryrawas 17:0dbcbd8587fd 79
henryrawas 17:0dbcbd8587fd 80 // try to serialize a snapshot into the buffer
henryrawas 17:0dbcbd8587fd 81 // return bytes used / -1 if buffer too small / 0 if no data
henryrawas 17:0dbcbd8587fd 82 // current serialization is a json object with time and array per measure
henryrawas 17:0dbcbd8587fd 83 // eg: { "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }
henryrawas 17:0dbcbd8587fd 84 int IothubSerial::MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize)
henryrawas 17:0dbcbd8587fd 85 {
henryrawas 17:0dbcbd8587fd 86 int slen;
henryrawas 17:0dbcbd8587fd 87 int startlen = bufsize;
henryrawas 17:0dbcbd8587fd 88
henryrawas 17:0dbcbd8587fd 89 slen = sprintf_s(buf, bufsize, "{");
henryrawas 17:0dbcbd8587fd 90 if (slen > 0)
henryrawas 17:0dbcbd8587fd 91 {
henryrawas 17:0dbcbd8587fd 92 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 93 buf += slen;
henryrawas 17:0dbcbd8587fd 94 }
henryrawas 17:0dbcbd8587fd 95 else
henryrawas 17:0dbcbd8587fd 96 return -1;
henryrawas 17:0dbcbd8587fd 97
henryrawas 17:0dbcbd8587fd 98 slen = AddTime(msnap.Created, buf, bufsize);
henryrawas 17:0dbcbd8587fd 99 if (slen > 0)
henryrawas 17:0dbcbd8587fd 100 {
henryrawas 17:0dbcbd8587fd 101 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 102 buf += slen;
henryrawas 17:0dbcbd8587fd 103 }
henryrawas 17:0dbcbd8587fd 104 else
henryrawas 17:0dbcbd8587fd 105 return -1;
henryrawas 17:0dbcbd8587fd 106
henryrawas 18:224289104fc0 107 slen = MeasureGroupToString(nametemp, msnap.Temps, buf, bufsize);
henryrawas 17:0dbcbd8587fd 108 if (slen > 0)
henryrawas 17:0dbcbd8587fd 109 {
henryrawas 17:0dbcbd8587fd 110 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 111 buf += slen;
henryrawas 17:0dbcbd8587fd 112 }
henryrawas 17:0dbcbd8587fd 113 else
henryrawas 17:0dbcbd8587fd 114 return -1;
henryrawas 17:0dbcbd8587fd 115
henryrawas 18:224289104fc0 116 slen = MeasureGroupToString(namedeg, msnap.Positions, buf, bufsize);
henryrawas 17:0dbcbd8587fd 117 if (slen > 0)
henryrawas 17:0dbcbd8587fd 118 {
henryrawas 17:0dbcbd8587fd 119 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 120 buf += slen;
henryrawas 17:0dbcbd8587fd 121 }
henryrawas 17:0dbcbd8587fd 122 else
henryrawas 17:0dbcbd8587fd 123 return -1;
henryrawas 17:0dbcbd8587fd 124
henryrawas 18:224289104fc0 125 slen = MeasureGroupToString(nameload, msnap.Loads, buf, bufsize);
henryrawas 17:0dbcbd8587fd 126 if (slen > 0)
henryrawas 17:0dbcbd8587fd 127 {
henryrawas 17:0dbcbd8587fd 128 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 129 buf += slen;
henryrawas 17:0dbcbd8587fd 130 }
henryrawas 17:0dbcbd8587fd 131 else
henryrawas 17:0dbcbd8587fd 132 return -1;
henryrawas 17:0dbcbd8587fd 133
henryrawas 18:224289104fc0 134 slen = MeasureGroupToString(namevolt, msnap.Volts, buf, bufsize);
henryrawas 17:0dbcbd8587fd 135 if (slen > 0)
henryrawas 17:0dbcbd8587fd 136 {
henryrawas 17:0dbcbd8587fd 137 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 138 buf += slen;
henryrawas 17:0dbcbd8587fd 139 }
henryrawas 17:0dbcbd8587fd 140 else
henryrawas 17:0dbcbd8587fd 141 return -1;
henryrawas 17:0dbcbd8587fd 142
henryrawas 17:0dbcbd8587fd 143 // replace final ',' with '}'
henryrawas 17:0dbcbd8587fd 144 *(buf - 1) = '}';
henryrawas 17:0dbcbd8587fd 145
henryrawas 17:0dbcbd8587fd 146 return startlen - bufsize;
henryrawas 17:0dbcbd8587fd 147 }
henryrawas 17:0dbcbd8587fd 148
henryrawas 17:0dbcbd8587fd 149 // try to serialize one or more measurement snapshots into the buffer
henryrawas 13:ffeff9b5e513 150 // return bytes used / -1 if buffer too small / 0 if no data
henryrawas 13:ffeff9b5e513 151 // current serialization is a json array of objects with time and array per measure
henryrawas 13:ffeff9b5e513 152 // eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }]
henryrawas 4:36a4eceb1b7f 153 int IothubSerial::MeasureBufToString(char* buf, int bufsize)
henryrawas 4:36a4eceb1b7f 154 {
henryrawas 4:36a4eceb1b7f 155 int slen;
henryrawas 4:36a4eceb1b7f 156 bool hasdata = false;
henryrawas 4:36a4eceb1b7f 157 bool copydata = false;
henryrawas 7:6723f6887d00 158 char* startbuf = buf;
henryrawas 7:6723f6887d00 159 char* lastcomma = NULL;
henryrawas 7:6723f6887d00 160
henryrawas 17:0dbcbd8587fd 161 slen = sprintf_s(buf, bufsize, "[");
henryrawas 13:ffeff9b5e513 162 if (slen > 0)
henryrawas 4:36a4eceb1b7f 163 {
henryrawas 13:ffeff9b5e513 164 bufsize -= slen;
henryrawas 13:ffeff9b5e513 165 buf += slen;
henryrawas 4:36a4eceb1b7f 166 }
henryrawas 4:36a4eceb1b7f 167 else
henryrawas 4:36a4eceb1b7f 168 return -1;
henryrawas 4:36a4eceb1b7f 169
henryrawas 4:36a4eceb1b7f 170 if (_hasPending)
henryrawas 4:36a4eceb1b7f 171 {
henryrawas 4:36a4eceb1b7f 172 hasdata = true;
henryrawas 17:0dbcbd8587fd 173 slen = MeasureSnapshotToString(_pending, buf, bufsize);
henryrawas 7:6723f6887d00 174 if (slen > 0)
henryrawas 7:6723f6887d00 175 {
henryrawas 7:6723f6887d00 176 bufsize -= slen;
henryrawas 7:6723f6887d00 177 buf += slen;
henryrawas 7:6723f6887d00 178 }
henryrawas 7:6723f6887d00 179 else
henryrawas 7:6723f6887d00 180 return -1; // no room for pending record
henryrawas 17:0dbcbd8587fd 181 // add comma
henryrawas 17:0dbcbd8587fd 182 slen = sprintf_s(buf, bufsize, ",");
henryrawas 4:36a4eceb1b7f 183 if (slen > 0)
henryrawas 4:36a4eceb1b7f 184 {
henryrawas 4:36a4eceb1b7f 185 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 186 buf += slen;
henryrawas 4:36a4eceb1b7f 187 }
henryrawas 4:36a4eceb1b7f 188 else
henryrawas 17:0dbcbd8587fd 189 return -1;
henryrawas 17:0dbcbd8587fd 190 lastcomma = buf;
henryrawas 17:0dbcbd8587fd 191 _hasPending = false;
henryrawas 17:0dbcbd8587fd 192 copydata = true;
henryrawas 4:36a4eceb1b7f 193 }
henryrawas 4:36a4eceb1b7f 194
henryrawas 4:36a4eceb1b7f 195 while (!MeasureBuf.empty())
henryrawas 4:36a4eceb1b7f 196 {
henryrawas 4:36a4eceb1b7f 197 if (!MeasureBuf.pop(_pending))
henryrawas 4:36a4eceb1b7f 198 {
henryrawas 4:36a4eceb1b7f 199 break;
henryrawas 4:36a4eceb1b7f 200 }
henryrawas 4:36a4eceb1b7f 201 hasdata = true;
henryrawas 4:36a4eceb1b7f 202 _hasPending = true;
henryrawas 7:6723f6887d00 203
henryrawas 17:0dbcbd8587fd 204 slen = MeasureSnapshotToString(_pending, buf, bufsize);
henryrawas 4:36a4eceb1b7f 205 if (slen > 0)
henryrawas 4:36a4eceb1b7f 206 {
henryrawas 4:36a4eceb1b7f 207 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 208 buf += slen;
henryrawas 4:36a4eceb1b7f 209 }
henryrawas 4:36a4eceb1b7f 210 else
henryrawas 13:ffeff9b5e513 211 break; // no room to serialize, leave pending for next message
henryrawas 17:0dbcbd8587fd 212 // add comma
henryrawas 17:0dbcbd8587fd 213 slen = sprintf_s(buf, bufsize, ",");
henryrawas 17:0dbcbd8587fd 214 if (slen > 0)
henryrawas 17:0dbcbd8587fd 215 {
henryrawas 17:0dbcbd8587fd 216 bufsize -= slen;
henryrawas 17:0dbcbd8587fd 217 buf += slen;
henryrawas 17:0dbcbd8587fd 218 }
henryrawas 17:0dbcbd8587fd 219 else
henryrawas 17:0dbcbd8587fd 220 break;
henryrawas 17:0dbcbd8587fd 221 _hasPending = false;
henryrawas 17:0dbcbd8587fd 222 lastcomma = buf;
henryrawas 17:0dbcbd8587fd 223 copydata = true;
henryrawas 4:36a4eceb1b7f 224 }
henryrawas 4:36a4eceb1b7f 225
henryrawas 4:36a4eceb1b7f 226 if (!hasdata)
henryrawas 4:36a4eceb1b7f 227 return 0; // no data
henryrawas 4:36a4eceb1b7f 228
henryrawas 4:36a4eceb1b7f 229 if (!copydata)
henryrawas 4:36a4eceb1b7f 230 return -1; // have data but buffer too small
henryrawas 4:36a4eceb1b7f 231
henryrawas 17:0dbcbd8587fd 232 // replace final ',' with ']'
henryrawas 17:0dbcbd8587fd 233 *(lastcomma - 1) = ']';
henryrawas 13:ffeff9b5e513 234
henryrawas 7:6723f6887d00 235 return lastcomma - startbuf;
henryrawas 7:6723f6887d00 236 }
henryrawas 7:6723f6887d00 237
henryrawas 7:6723f6887d00 238
henryrawas 7:6723f6887d00 239 // try to serialize one or more alerts into the buffer
henryrawas 7:6723f6887d00 240 // return bytes used or -1 if buffer too small or other error. 0 if no data
henryrawas 13:ffeff9b5e513 241 // Serialize to a json object with time, measurename, value, message, and index of joint
henryrawas 13:ffeff9b5e513 242 // eg: { "alerttype": "Temperature", "message": "too hot", "measurename", "temp", "index": 2, "value": 79.3, "time": "2016-01-23T14:55:02" }
henryrawas 7:6723f6887d00 243 int IothubSerial::AlertBufToString(char* buf, int bufsize)
henryrawas 7:6723f6887d00 244 {
henryrawas 7:6723f6887d00 245 int slen;
henryrawas 7:6723f6887d00 246 bool hasdata = false;
henryrawas 7:6723f6887d00 247 bool copydata = false;
henryrawas 7:6723f6887d00 248 int startlen = bufsize;
henryrawas 7:6723f6887d00 249
henryrawas 7:6723f6887d00 250 if (AlertBuf.pop(_pendAlert))
henryrawas 7:6723f6887d00 251 {
henryrawas 13:ffeff9b5e513 252 char tbuf[32];
henryrawas 13:ffeff9b5e513 253 (void)strftime(tbuf, 32, "%FT%T", localtime(&_pendAlert.Created));
henryrawas 7:6723f6887d00 254 hasdata = true;
henryrawas 13:ffeff9b5e513 255 slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"message\": \"%s\", \"measurename\": \"%s\", \"index\": %d , \"value\": %f, \"time\": \"%s\" }",
henryrawas 13:ffeff9b5e513 256 _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.MeasureName, _pendAlert.Index, _pendAlert.Value, tbuf);
henryrawas 7:6723f6887d00 257 if (slen > 0)
henryrawas 7:6723f6887d00 258 {
henryrawas 7:6723f6887d00 259 bufsize -= slen;
henryrawas 7:6723f6887d00 260 buf += slen;
henryrawas 7:6723f6887d00 261 copydata = true;
henryrawas 7:6723f6887d00 262 }
henryrawas 7:6723f6887d00 263 }
henryrawas 7:6723f6887d00 264
henryrawas 7:6723f6887d00 265 if (!hasdata)
henryrawas 7:6723f6887d00 266 return 0; // no data
henryrawas 7:6723f6887d00 267
henryrawas 7:6723f6887d00 268 if (!copydata)
henryrawas 7:6723f6887d00 269 return -1; // have data but buffer too small
henryrawas 7:6723f6887d00 270
henryrawas 4:36a4eceb1b7f 271
henryrawas 4:36a4eceb1b7f 272 return startlen - bufsize;
henryrawas 4:36a4eceb1b7f 273 }
henryrawas 4:36a4eceb1b7f 274