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:
Wed Dec 23 18:34:06 2015 +0000
Revision:
4:36a4eceb1b7f
Child:
7:6723f6887d00
RobotArm plus publish IoTHub status

Who changed what in which revision?

UserRevisionLine numberNew 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 4:36a4eceb1b7f 8
henryrawas 4:36a4eceb1b7f 9 IothubSerial::IothubSerial()
henryrawas 4:36a4eceb1b7f 10 {
henryrawas 4:36a4eceb1b7f 11 _hasPending = false;
henryrawas 4:36a4eceb1b7f 12 }
henryrawas 4:36a4eceb1b7f 13
henryrawas 4:36a4eceb1b7f 14 // try to serialize the measurements into the buffer
henryrawas 4:36a4eceb1b7f 15 // return bytes used or -1 if buffer too small or other error
henryrawas 4:36a4eceb1b7f 16 // current serialization is a json array with ',' at end
henryrawas 4:36a4eceb1b7f 17 // eg: temp: [22.0, 23.1, 22.3],
henryrawas 4:36a4eceb1b7f 18 int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize)
henryrawas 4:36a4eceb1b7f 19 {
henryrawas 4:36a4eceb1b7f 20 char* name;
henryrawas 4:36a4eceb1b7f 21 int slen;
henryrawas 4:36a4eceb1b7f 22 int startlen = bufsize;
henryrawas 4:36a4eceb1b7f 23
henryrawas 4:36a4eceb1b7f 24 switch (mg.MeasId)
henryrawas 4:36a4eceb1b7f 25 {
henryrawas 4:36a4eceb1b7f 26 case NM_Temperature:
henryrawas 4:36a4eceb1b7f 27 name = nametemp;
henryrawas 4:36a4eceb1b7f 28 break;
henryrawas 4:36a4eceb1b7f 29
henryrawas 4:36a4eceb1b7f 30 case NM_Voltage:
henryrawas 4:36a4eceb1b7f 31 name = namevolt;
henryrawas 4:36a4eceb1b7f 32 break;
henryrawas 4:36a4eceb1b7f 33
henryrawas 4:36a4eceb1b7f 34 case NM_Degrees:
henryrawas 4:36a4eceb1b7f 35 name = namedeg;
henryrawas 4:36a4eceb1b7f 36 break;
henryrawas 4:36a4eceb1b7f 37
henryrawas 4:36a4eceb1b7f 38 default:
henryrawas 4:36a4eceb1b7f 39 return -1;
henryrawas 4:36a4eceb1b7f 40 }
henryrawas 4:36a4eceb1b7f 41
henryrawas 4:36a4eceb1b7f 42 if (bufsize > strlen(name) + 5)
henryrawas 4:36a4eceb1b7f 43 {
henryrawas 4:36a4eceb1b7f 44 slen = sprintf_s(buf, bufsize, "\"%s\": [", name);
henryrawas 4:36a4eceb1b7f 45 if (slen > 0)
henryrawas 4:36a4eceb1b7f 46 {
henryrawas 4:36a4eceb1b7f 47 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 48 buf += slen;
henryrawas 4:36a4eceb1b7f 49 }
henryrawas 4:36a4eceb1b7f 50 else
henryrawas 4:36a4eceb1b7f 51 return -1;
henryrawas 4:36a4eceb1b7f 52 }
henryrawas 4:36a4eceb1b7f 53 else
henryrawas 4:36a4eceb1b7f 54 return -1;
henryrawas 4:36a4eceb1b7f 55
henryrawas 4:36a4eceb1b7f 56 for (int i = 0; i < mg.NumVals; i++)
henryrawas 4:36a4eceb1b7f 57 {
henryrawas 4:36a4eceb1b7f 58 if (bufsize > 9)
henryrawas 4:36a4eceb1b7f 59 {
henryrawas 4:36a4eceb1b7f 60 if (i < mg.NumVals - 1)
henryrawas 4:36a4eceb1b7f 61 slen = sprintf_s(buf, bufsize, "%7.2f, ", mg.MeasVals[i]);
henryrawas 4:36a4eceb1b7f 62 else
henryrawas 4:36a4eceb1b7f 63 slen = sprintf_s(buf, bufsize, "%7.2f ", mg.MeasVals[i]);
henryrawas 4:36a4eceb1b7f 64 if (slen > 0)
henryrawas 4:36a4eceb1b7f 65 {
henryrawas 4:36a4eceb1b7f 66 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 67 buf += slen;
henryrawas 4:36a4eceb1b7f 68 }
henryrawas 4:36a4eceb1b7f 69 else
henryrawas 4:36a4eceb1b7f 70 return -1;
henryrawas 4:36a4eceb1b7f 71 }
henryrawas 4:36a4eceb1b7f 72 else
henryrawas 4:36a4eceb1b7f 73 return -1;
henryrawas 4:36a4eceb1b7f 74 }
henryrawas 4:36a4eceb1b7f 75
henryrawas 4:36a4eceb1b7f 76 if (bufsize > 2)
henryrawas 4:36a4eceb1b7f 77 {
henryrawas 4:36a4eceb1b7f 78 slen = sprintf_s(buf, bufsize, "],");
henryrawas 4:36a4eceb1b7f 79 if (slen > 0)
henryrawas 4:36a4eceb1b7f 80 {
henryrawas 4:36a4eceb1b7f 81 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 82 buf += slen;
henryrawas 4:36a4eceb1b7f 83 }
henryrawas 4:36a4eceb1b7f 84 else
henryrawas 4:36a4eceb1b7f 85 return -1;
henryrawas 4:36a4eceb1b7f 86 }
henryrawas 4:36a4eceb1b7f 87 else
henryrawas 4:36a4eceb1b7f 88 return -1;
henryrawas 4:36a4eceb1b7f 89
henryrawas 4:36a4eceb1b7f 90 return startlen - bufsize;
henryrawas 4:36a4eceb1b7f 91 }
henryrawas 4:36a4eceb1b7f 92
henryrawas 4:36a4eceb1b7f 93 // try to serialize one or more measurements into the buffer
henryrawas 4:36a4eceb1b7f 94 // return bytes used or -1 if buffer too small or other error. 0 if no data
henryrawas 4:36a4eceb1b7f 95 // current serialization is a json object with entry per measure
henryrawas 4:36a4eceb1b7f 96 // eg: { temp: [1, 2], volt: [12.1, 12.2] }
henryrawas 4:36a4eceb1b7f 97 int IothubSerial::MeasureBufToString(char* buf, int bufsize)
henryrawas 4:36a4eceb1b7f 98 {
henryrawas 4:36a4eceb1b7f 99 int slen;
henryrawas 4:36a4eceb1b7f 100 bool hasdata = false;
henryrawas 4:36a4eceb1b7f 101 bool copydata = false;
henryrawas 4:36a4eceb1b7f 102 int startlen = bufsize;
henryrawas 4:36a4eceb1b7f 103
henryrawas 4:36a4eceb1b7f 104 if (bufsize > 1)
henryrawas 4:36a4eceb1b7f 105 {
henryrawas 4:36a4eceb1b7f 106 slen = sprintf_s(buf, bufsize, "{");
henryrawas 4:36a4eceb1b7f 107 if (slen > 0)
henryrawas 4:36a4eceb1b7f 108 {
henryrawas 4:36a4eceb1b7f 109 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 110 buf += slen;
henryrawas 4:36a4eceb1b7f 111 }
henryrawas 4:36a4eceb1b7f 112 else
henryrawas 4:36a4eceb1b7f 113 return -1;
henryrawas 4:36a4eceb1b7f 114 }
henryrawas 4:36a4eceb1b7f 115 else
henryrawas 4:36a4eceb1b7f 116 return -1;
henryrawas 4:36a4eceb1b7f 117
henryrawas 4:36a4eceb1b7f 118 if (_hasPending)
henryrawas 4:36a4eceb1b7f 119 {
henryrawas 4:36a4eceb1b7f 120 hasdata = true;
henryrawas 4:36a4eceb1b7f 121 slen = MeasureGroupToString(_pending, buf, bufsize);
henryrawas 4:36a4eceb1b7f 122 if (slen > 0)
henryrawas 4:36a4eceb1b7f 123 {
henryrawas 4:36a4eceb1b7f 124 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 125 buf += slen;
henryrawas 4:36a4eceb1b7f 126 _hasPending = false;
henryrawas 4:36a4eceb1b7f 127 copydata = true;
henryrawas 4:36a4eceb1b7f 128 }
henryrawas 4:36a4eceb1b7f 129 else
henryrawas 4:36a4eceb1b7f 130 return -1; // no room for pending record
henryrawas 4:36a4eceb1b7f 131 }
henryrawas 4:36a4eceb1b7f 132
henryrawas 4:36a4eceb1b7f 133 while (!MeasureBuf.empty())
henryrawas 4:36a4eceb1b7f 134 {
henryrawas 4:36a4eceb1b7f 135 if (!MeasureBuf.pop(_pending))
henryrawas 4:36a4eceb1b7f 136 {
henryrawas 4:36a4eceb1b7f 137 break;
henryrawas 4:36a4eceb1b7f 138 }
henryrawas 4:36a4eceb1b7f 139 hasdata = true;
henryrawas 4:36a4eceb1b7f 140 _hasPending = true;
henryrawas 4:36a4eceb1b7f 141 slen = MeasureGroupToString(_pending, buf, bufsize);
henryrawas 4:36a4eceb1b7f 142 if (slen > 0)
henryrawas 4:36a4eceb1b7f 143 {
henryrawas 4:36a4eceb1b7f 144 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 145 buf += slen;
henryrawas 4:36a4eceb1b7f 146 _hasPending = false;
henryrawas 4:36a4eceb1b7f 147 copydata = true;
henryrawas 4:36a4eceb1b7f 148 }
henryrawas 4:36a4eceb1b7f 149 else
henryrawas 4:36a4eceb1b7f 150 break; // no room to serialize, leave pending for mext message
henryrawas 4:36a4eceb1b7f 151 }
henryrawas 4:36a4eceb1b7f 152
henryrawas 4:36a4eceb1b7f 153 if (!hasdata)
henryrawas 4:36a4eceb1b7f 154 return 0; // no data
henryrawas 4:36a4eceb1b7f 155
henryrawas 4:36a4eceb1b7f 156 if (!copydata)
henryrawas 4:36a4eceb1b7f 157 return -1; // have data but buffer too small
henryrawas 4:36a4eceb1b7f 158
henryrawas 4:36a4eceb1b7f 159 // replace final ',' with '}'
henryrawas 4:36a4eceb1b7f 160 *(buf - 1) = '}';
henryrawas 4:36a4eceb1b7f 161
henryrawas 4:36a4eceb1b7f 162 return startlen - bufsize;
henryrawas 4:36a4eceb1b7f 163 }
henryrawas 4:36a4eceb1b7f 164
henryrawas 4:36a4eceb1b7f 165 bool IothubSerial::HasMeasureGroup()
henryrawas 4:36a4eceb1b7f 166 {
henryrawas 4:36a4eceb1b7f 167 return _hasPending;
henryrawas 4:36a4eceb1b7f 168 }