demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
IothubSerial.cpp
- Committer:
- henryrawas
- Date:
- 2015-12-23
- Revision:
- 4:36a4eceb1b7f
- Child:
- 7:6723f6887d00
File content as of revision 4:36a4eceb1b7f:
#include "mbed.h" #include "IothubSerial.h" #include "crt_abstractions.h" char* nametemp = "temp"; char* namevolt = "volt"; char* namedeg = "rot"; IothubSerial::IothubSerial() { _hasPending = false; } // try to serialize the measurements into the buffer // return bytes used or -1 if buffer too small or other error // current serialization is a json array with ',' at end // eg: temp: [22.0, 23.1, 22.3], int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize) { char* name; int slen; int startlen = bufsize; switch (mg.MeasId) { case NM_Temperature: name = nametemp; break; case NM_Voltage: name = namevolt; break; case NM_Degrees: name = namedeg; break; default: return -1; } if (bufsize > strlen(name) + 5) { slen = sprintf_s(buf, bufsize, "\"%s\": [", name); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; } else return -1; for (int i = 0; i < mg.NumVals; i++) { if (bufsize > 9) { if (i < mg.NumVals - 1) slen = sprintf_s(buf, bufsize, "%7.2f, ", mg.MeasVals[i]); else slen = sprintf_s(buf, bufsize, "%7.2f ", mg.MeasVals[i]); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; } else return -1; } if (bufsize > 2) { slen = sprintf_s(buf, bufsize, "],"); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; } else return -1; return startlen - bufsize; } // try to serialize one or more measurements into the buffer // return bytes used or -1 if buffer too small or other error. 0 if no data // current serialization is a json object with entry per measure // eg: { temp: [1, 2], volt: [12.1, 12.2] } int IothubSerial::MeasureBufToString(char* buf, int bufsize) { int slen; bool hasdata = false; bool copydata = false; int startlen = bufsize; if (bufsize > 1) { slen = sprintf_s(buf, bufsize, "{"); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; } else return -1; if (_hasPending) { hasdata = true; slen = MeasureGroupToString(_pending, buf, bufsize); if (slen > 0) { bufsize -= slen; buf += slen; _hasPending = false; copydata = true; } else return -1; // no room for pending record } while (!MeasureBuf.empty()) { if (!MeasureBuf.pop(_pending)) { break; } hasdata = true; _hasPending = true; slen = MeasureGroupToString(_pending, buf, bufsize); if (slen > 0) { bufsize -= slen; buf += slen; _hasPending = false; copydata = true; } else break; // no room to serialize, leave pending for mext message } if (!hasdata) return 0; // no data if (!copydata) return -1; // have data but buffer too small // replace final ',' with '}' *(buf - 1) = '}'; return startlen - bufsize; } bool IothubSerial::HasMeasureGroup() { return _hasPending; }