ARM mbed M2X API Client: The ARM mbed client library is used to send/receive data to/from AT&T's M2X service from mbed LPC1768 microcontrollers.

Dependents:   m2x-demo-all M2X_MTS_ACCEL_DEMO M2X_MTS_Accel M2X_K64F_ACCEL ... more

Committer:
citrusbyte
Date:
Mon Dec 28 12:48:19 2015 +0000
Revision:
16:7903152de19f
Parent:
15:2610823f7f2e
Child:
18:510895884768
Add TimeService implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jb8414 0:f479e4f4db0e 1 #ifndef M2XStreamClient_h
jb8414 0:f479e4f4db0e 2 #define M2XStreamClient_h
jb8414 0:f479e4f4db0e 3
jb8414 0:f479e4f4db0e 4 #define MIN(a, b) (((a) > (b))?(b):(a))
jb8414 0:f479e4f4db0e 5
jb8414 0:f479e4f4db0e 6 #define MBED_PLATFORM
jb8414 0:f479e4f4db0e 7
jb8414 0:f479e4f4db0e 8 #ifdef ARDUINO_PLATFORM
jb8414 0:f479e4f4db0e 9 #include "Arduino.h"
jb8414 0:f479e4f4db0e 10
citrusbyte 15:2610823f7f2e 11 #define USER_AGENT "User-Agent: M2X Arduino Client/2.0.2"
jb8414 0:f479e4f4db0e 12 #endif
jb8414 0:f479e4f4db0e 13
jb8414 0:f479e4f4db0e 14 #ifdef MBED_PLATFORM
jb8414 0:f479e4f4db0e 15 #include "mbed.h"
jb8414 0:f479e4f4db0e 16
citrusbyte 13:0d574742208f 17 #define USER_AGENT "User-Agent: M2X Mbed Client/2.0.0"
jb8414 0:f479e4f4db0e 18 #endif
jb8414 0:f479e4f4db0e 19
jb8414 0:f479e4f4db0e 20 #include "Client.h"
jb8414 0:f479e4f4db0e 21 #include "NullPrint.h"
jb8414 0:f479e4f4db0e 22
jb8414 0:f479e4f4db0e 23 #ifdef DEBUG
jb8414 0:f479e4f4db0e 24 #ifdef ARDUINO_PLATFORM
jb8414 0:f479e4f4db0e 25 #define DBG(fmt_, data_) Serial.print(data_)
jb8414 0:f479e4f4db0e 26 #define DBGLN(fmt_, data_) Serial.println(data_)
jb8414 0:f479e4f4db0e 27 #define DBGLNEND Serial.println()
jb8414 0:f479e4f4db0e 28 #endif // ARDUINO_PLATFORM
jb8414 0:f479e4f4db0e 29
jb8414 0:f479e4f4db0e 30 #ifdef MBED_PLATFORM
jb8414 0:f479e4f4db0e 31 #define DBG(fmt_, data_) printf((fmt_), (data_))
jb8414 0:f479e4f4db0e 32 #define DBGLN(fmt_, data_) printf((fmt_), (data_)); printf("\n")
jb8414 0:f479e4f4db0e 33 #define DBGLNEND printf("\n")
jb8414 0:f479e4f4db0e 34 #endif // MBED_PLATFORM
jb8414 0:f479e4f4db0e 35 #else
jb8414 0:f479e4f4db0e 36 #define DBG(fmt_, data_)
jb8414 0:f479e4f4db0e 37 #define DBGLN(fmt_, data_)
jb8414 0:f479e4f4db0e 38 #define DBGLNEND
jb8414 0:f479e4f4db0e 39 #endif // DEBUG
jb8414 0:f479e4f4db0e 40
jb8414 0:f479e4f4db0e 41 #define HEX(t_) ((char) (((t_) > 9) ? ((t_) - 10 + 'A') : ((t_) + '0')))
jb8414 0:f479e4f4db0e 42 #define MAX_DOUBLE_DIGITS 7
jb8414 0:f479e4f4db0e 43
jb8414 0:f479e4f4db0e 44 static const int E_OK = 0;
jb8414 0:f479e4f4db0e 45 static const int E_NOCONNECTION = -1;
jb8414 0:f479e4f4db0e 46 static const int E_DISCONNECTED = -2;
jb8414 0:f479e4f4db0e 47 static const int E_NOTREACHABLE = -3;
jb8414 0:f479e4f4db0e 48 static const int E_INVALID = -4;
jb8414 0:f479e4f4db0e 49 static const int E_JSON_INVALID = -5;
citrusbyte 16:7903152de19f 50 static const int E_BUFFER_TOO_SMALL = -6;
citrusbyte 16:7903152de19f 51 static const int E_TIMESTAMP_ERROR = -8;
citrusbyte 16:7903152de19f 52
citrusbyte 16:7903152de19f 53 static inline bool m2x_status_is_success(int status) {
citrusbyte 16:7903152de19f 54 return (status == E_OK) || (status >= 200 && status <= 299);
citrusbyte 16:7903152de19f 55 }
citrusbyte 16:7903152de19f 56
citrusbyte 16:7903152de19f 57 static inline bool m2x_status_is_client_error(int status) {
citrusbyte 16:7903152de19f 58 return status >= 400 && status <= 499;
citrusbyte 16:7903152de19f 59 }
citrusbyte 16:7903152de19f 60
citrusbyte 16:7903152de19f 61 static inline bool m2x_status_is_server_error(int status) {
citrusbyte 16:7903152de19f 62 return status >= 500 && status <= 599;
citrusbyte 16:7903152de19f 63 }
citrusbyte 16:7903152de19f 64
citrusbyte 16:7903152de19f 65 static inline bool m2x_status_is_error(int status) {
citrusbyte 16:7903152de19f 66 return m2x_status_is_client_error(status) ||
citrusbyte 16:7903152de19f 67 m2x_status_is_server_error(status);
citrusbyte 16:7903152de19f 68 }
jb8414 0:f479e4f4db0e 69
citrusbyte 10:4ce9eba38dbe 70 /*
citrusbyte 10:4ce9eba38dbe 71 * +type+ indicates the value type: 1 for string, 2 for number
citrusbyte 10:4ce9eba38dbe 72 * NOTE that the value type here only contains a hint on how
citrusbyte 10:4ce9eba38dbe 73 * you can use the value. Even though 2 is returned, the value
citrusbyte 10:4ce9eba38dbe 74 * is still stored in (const char *), and atoi/atof is needed to
citrusbyte 10:4ce9eba38dbe 75 * get the actual value
citrusbyte 10:4ce9eba38dbe 76 */
jb8414 0:f479e4f4db0e 77 typedef void (*stream_value_read_callback)(const char* at,
jb8414 0:f479e4f4db0e 78 const char* value,
jb8414 0:f479e4f4db0e 79 int index,
citrusbyte 10:4ce9eba38dbe 80 void* context,
citrusbyte 10:4ce9eba38dbe 81 int type);
jb8414 0:f479e4f4db0e 82
jb8414 0:f479e4f4db0e 83 typedef void (*location_read_callback)(const char* name,
jb8414 0:f479e4f4db0e 84 double latitude,
jb8414 0:f479e4f4db0e 85 double longitude,
jb8414 0:f479e4f4db0e 86 double elevation,
jb8414 0:f479e4f4db0e 87 const char* timestamp,
jb8414 0:f479e4f4db0e 88 int index,
jb8414 0:f479e4f4db0e 89 void* context);
jb8414 0:f479e4f4db0e 90
jb8414 0:f479e4f4db0e 91 class M2XStreamClient {
jb8414 0:f479e4f4db0e 92 public:
jb8414 0:f479e4f4db0e 93 static const char* kDefaultM2XHost;
jb8414 0:f479e4f4db0e 94 static const int kDefaultM2XPort = 80;
jb8414 0:f479e4f4db0e 95
jb8414 0:f479e4f4db0e 96 M2XStreamClient(Client* client,
jb8414 0:f479e4f4db0e 97 const char* key,
jb8414 0:f479e4f4db0e 98 int case_insensitive = 1,
jb8414 0:f479e4f4db0e 99 const char* host = kDefaultM2XHost,
NetArc 14:205076b587fe 100 int port = kDefaultM2XPort,
NetArc 14:205076b587fe 101 const char* path_prefix = NULL);
jb8414 0:f479e4f4db0e 102
citrusbyte 10:4ce9eba38dbe 103 // Push data stream value using PUT request, returns the HTTP status code
jb8414 0:f479e4f4db0e 104 template <class T>
citrusbyte 13:0d574742208f 105 int updateStreamValue(const char* deviceId, const char* streamName, T value);
jb8414 0:f479e4f4db0e 106
jb8414 0:f479e4f4db0e 107 // Post multiple values to M2X all at once.
citrusbyte 13:0d574742208f 108 // +deviceId+ - id of the device to post values
jb8414 0:f479e4f4db0e 109 // +streamNum+ - Number of streams to post
jb8414 0:f479e4f4db0e 110 // +names+ - Array of stream names, the length of the array should
jb8414 0:f479e4f4db0e 111 // be exactly +streamNum+
jb8414 0:f479e4f4db0e 112 // +counts+ - Array of +streamNum+ length, each item in this array
jb8414 0:f479e4f4db0e 113 // containing the number of values we want to post for each stream
jb8414 0:f479e4f4db0e 114 // +ats+ - Timestamps for each value, the length of this array should
jb8414 0:f479e4f4db0e 115 // be the some of all values in +counts+, for the first +counts[0]+
jb8414 0:f479e4f4db0e 116 // items, the values belong to the first stream, for the following
jb8414 0:f479e4f4db0e 117 // +counts[1]+ number of items, the values belong to the second stream,
citrusbyte 10:4ce9eba38dbe 118 // etc. Notice that timestamps are required here: you must provide
citrusbyte 10:4ce9eba38dbe 119 // a timestamp for each value posted.
jb8414 0:f479e4f4db0e 120 // +values+ - Values to post. This works the same way as +ats+, the
jb8414 0:f479e4f4db0e 121 // first +counts[0]+ number of items contain values to post to the first
jb8414 0:f479e4f4db0e 122 // stream, the succeeding +counts[1]+ number of items contain values
jb8414 0:f479e4f4db0e 123 // for the second stream, etc. The length of this array should be
jb8414 0:f479e4f4db0e 124 // the sum of all values in +counts+ array.
jb8414 0:f479e4f4db0e 125 template <class T>
citrusbyte 13:0d574742208f 126 int postDeviceUpdates(const char* deviceId, int streamNum,
citrusbyte 13:0d574742208f 127 const char* names[], const int counts[],
citrusbyte 13:0d574742208f 128 const char* ats[], T values[]);
jb8414 0:f479e4f4db0e 129
jb8414 0:f479e4f4db0e 130 // Fetch values for a particular data stream. Since memory is
jb8414 0:f479e4f4db0e 131 // very limited on an Arduino, we cannot parse and get all the
jb8414 0:f479e4f4db0e 132 // data points in memory. Instead, we use callbacks here: whenever
jb8414 0:f479e4f4db0e 133 // a new data point is parsed, we call the callback using the values,
jb8414 0:f479e4f4db0e 134 // after that, the values will be thrown away to make space for new
jb8414 0:f479e4f4db0e 135 // values.
jb8414 0:f479e4f4db0e 136 // Note that you can also pass in a user-specified context in this
jb8414 0:f479e4f4db0e 137 // function, this context will be passed to the callback function
jb8414 0:f479e4f4db0e 138 // each time we get a data point.
jb8414 0:f479e4f4db0e 139 // For each data point, the callback will be called once. The HTTP
jb8414 0:f479e4f4db0e 140 // status code will be returned. And the content is only parsed when
jb8414 0:f479e4f4db0e 141 // the status code is 200.
citrusbyte 13:0d574742208f 142 int listStreamValues(const char* deviceId, const char* streamName,
citrusbyte 13:0d574742208f 143 stream_value_read_callback callback, void* context,
citrusbyte 13:0d574742208f 144 const char* query = NULL);
jb8414 0:f479e4f4db0e 145
jb8414 0:f479e4f4db0e 146 // Update datasource location
jb8414 0:f479e4f4db0e 147 // NOTE: On an Arduino Uno and other ATMEGA based boards, double has
jb8414 0:f479e4f4db0e 148 // 4-byte (32 bits) precision, which is the same as float. So there's
jb8414 0:f479e4f4db0e 149 // no natural double-precision floating number on these boards. With
jb8414 0:f479e4f4db0e 150 // a float value, we have a precision of roughly 7 digits, that means
jb8414 0:f479e4f4db0e 151 // either 5 or 6 digits after the floating point. According to wikipedia,
jb8414 0:f479e4f4db0e 152 // a difference of 0.00001 will give us ~1.1132m distance. If this
jb8414 0:f479e4f4db0e 153 // precision is good for you, you can use the double-version we provided
jb8414 0:f479e4f4db0e 154 // here. Otherwise, you may need to use the string-version and do the
jb8414 0:f479e4f4db0e 155 // actual conversion by yourselves.
jb8414 0:f479e4f4db0e 156 // However, with an Arduino Due board, double has 8-bytes (64 bits)
jb8414 0:f479e4f4db0e 157 // precision, which means you are free to use the double-version only
jb8414 0:f479e4f4db0e 158 // without any precision problems.
jb8414 0:f479e4f4db0e 159 // Returned value is the http status code.
jb8414 0:f479e4f4db0e 160 template <class T>
citrusbyte 13:0d574742208f 161 int updateLocation(const char* deviceId, const char* name,
jb8414 0:f479e4f4db0e 162 T latitude, T longitude, T elevation);
jb8414 0:f479e4f4db0e 163
citrusbyte 13:0d574742208f 164 // Read location information for a device. Also used callback to process
jb8414 0:f479e4f4db0e 165 // data points for memory reasons. The HTTP status code is returned,
jb8414 0:f479e4f4db0e 166 // response is only parsed when the HTTP status code is 200
citrusbyte 13:0d574742208f 167 int readLocation(const char* deviceId, location_read_callback callback,
jb8414 0:f479e4f4db0e 168 void* context);
citrusbyte 10:4ce9eba38dbe 169
citrusbyte 10:4ce9eba38dbe 170 // Delete values from a data stream
citrusbyte 10:4ce9eba38dbe 171 // You will need to provide from and end date/time strings in the ISO8601
citrusbyte 10:4ce9eba38dbe 172 // format "yyyy-mm-ddTHH:MM:SS.SSSZ" where
citrusbyte 10:4ce9eba38dbe 173 // yyyy: the year
citrusbyte 10:4ce9eba38dbe 174 // mm: the month
citrusbyte 10:4ce9eba38dbe 175 // dd: the day
citrusbyte 10:4ce9eba38dbe 176 // HH: the hour (24 hour format)
citrusbyte 10:4ce9eba38dbe 177 // MM: the minute
citrusbyte 10:4ce9eba38dbe 178 // SS.SSS: the seconds (to the millisecond)
citrusbyte 10:4ce9eba38dbe 179 // NOTE: the time is given in Zulu (GMT)
citrusbyte 10:4ce9eba38dbe 180 // M2X will delete all values within the from to end date/time range.
citrusbyte 10:4ce9eba38dbe 181 // The status code is 204 on success and 400 on a bad request (e.g. the
citrusbyte 10:4ce9eba38dbe 182 // timestamp is not in ISO8601 format or the from timestamp is not less than
citrusbyte 10:4ce9eba38dbe 183 // or equal to the end timestamp.
citrusbyte 13:0d574742208f 184 int deleteValues(const char* deviceId, const char* streamName,
citrusbyte 10:4ce9eba38dbe 185 const char* from, const char* end);
citrusbyte 16:7903152de19f 186
citrusbyte 16:7903152de19f 187 // Fetches current timestamp in seconds from M2X server. Since we
citrusbyte 16:7903152de19f 188 // are using signed 32-bit integer as return value, this will only
citrusbyte 16:7903152de19f 189 // return valid results before 03:14:07 UTC on 19 January 2038. If
citrusbyte 16:7903152de19f 190 // the device is supposed to work after that, this function should
citrusbyte 16:7903152de19f 191 // not be used.
citrusbyte 16:7903152de19f 192 //
citrusbyte 16:7903152de19f 193 // The returned value will contain the status code(positive values)
citrusbyte 16:7903152de19f 194 // or the error code(negative values).
citrusbyte 16:7903152de19f 195 // In case of success, the current timestamp will be filled in the
citrusbyte 16:7903152de19f 196 // +ts+ pointer passed in as argument.
citrusbyte 16:7903152de19f 197 //
citrusbyte 16:7903152de19f 198 // NOTE: although returning uint32_t can give us a larger space,
citrusbyte 16:7903152de19f 199 // we prefer to cope with the unix convention here.
citrusbyte 16:7903152de19f 200 int getTimestamp32(int32_t* ts);
citrusbyte 16:7903152de19f 201
citrusbyte 16:7903152de19f 202 // Fetches current timestamp in seconds from M2X server.
citrusbyte 16:7903152de19f 203 // This function will return the timestamp as an integer literal
citrusbyte 16:7903152de19f 204 // in the provided buffer. Hence there's no problem working after
citrusbyte 16:7903152de19f 205 // 03:14:07 UTC on 19 January 2038. The drawback part here, is that
citrusbyte 16:7903152de19f 206 // you will have to work with 64-bit integer, which is not available
citrusbyte 16:7903152de19f 207 // on certain platform(such as Arduino), a bignum library or alike
citrusbyte 16:7903152de19f 208 // is needed in this case.
citrusbyte 16:7903152de19f 209 //
citrusbyte 16:7903152de19f 210 // Notice +bufferLength+ is supposed to contain the length of the
citrusbyte 16:7903152de19f 211 // buffer when calling this function. It is also the caller's
citrusbyte 16:7903152de19f 212 // responsibility to ensure the buffer is big enough, otherwise
citrusbyte 16:7903152de19f 213 // the library will return an error indicating the buffer is too
citrusbyte 16:7903152de19f 214 // small.
citrusbyte 16:7903152de19f 215 // While this is not accurate all the time, one trick here is to
citrusbyte 16:7903152de19f 216 // pass in 0 as the bufferLength, in which case we will always return
citrusbyte 16:7903152de19f 217 // the buffer-too-small error. However, the correct buffer length
citrusbyte 16:7903152de19f 218 // can be found this way so a secound execution is most likely to work
citrusbyte 16:7903152de19f 219 // (unless we are at the edge of the buffer length increasing, for
citrusbyte 16:7903152de19f 220 // example, when the timestamp jumps from 9999999999 to 10000000000,
citrusbyte 16:7903152de19f 221 // which is highly unlikely to happend). However, given that the
citrusbyte 16:7903152de19f 222 // maximum 64-bit integer can be stored in 19 bytes, there's not
citrusbyte 16:7903152de19f 223 // much need to use this trick.)
citrusbyte 16:7903152de19f 224 //
citrusbyte 16:7903152de19f 225 // The returned value will contain the status code(positive values)
citrusbyte 16:7903152de19f 226 // or the error code(negative values).
citrusbyte 16:7903152de19f 227 // In case of success, the current timestamp will be filled in the
citrusbyte 16:7903152de19f 228 // passed +buffer+ pointer, and the actual used buffer length will
citrusbyte 16:7903152de19f 229 // be returned in +bufferLength+ pointer.
citrusbyte 16:7903152de19f 230 // NOTE: as long as we can read the returned buffer length, it will
citrusbyte 16:7903152de19f 231 // be used to fill in the +bufferLength+ variable even though other
citrusbyte 16:7903152de19f 232 // errors occur(buffer is not enough, network is shutdown before
citrusbyte 16:7903152de19f 233 // reading the whole buffer, etc.)
citrusbyte 16:7903152de19f 234 int getTimestamp(char* buffer, int* bufferLength);
jb8414 0:f479e4f4db0e 235 private:
jb8414 0:f479e4f4db0e 236 Client* _client;
jb8414 0:f479e4f4db0e 237 const char* _key;
jb8414 0:f479e4f4db0e 238 int _case_insensitive;
jb8414 0:f479e4f4db0e 239 const char* _host;
jb8414 0:f479e4f4db0e 240 int _port;
NetArc 14:205076b587fe 241 const char* _path_prefix;
jb8414 0:f479e4f4db0e 242 NullPrint _null_print;
jb8414 0:f479e4f4db0e 243
jb8414 0:f479e4f4db0e 244 // Writes the HTTP header part for updating a stream value
citrusbyte 13:0d574742208f 245 void writePutHeader(const char* deviceId,
citrusbyte 10:4ce9eba38dbe 246 const char* streamName,
citrusbyte 10:4ce9eba38dbe 247 int contentLength);
citrusbyte 10:4ce9eba38dbe 248 // Writes the HTTP header part for deleting stream values
citrusbyte 13:0d574742208f 249 void writeDeleteHeader(const char* deviceId,
citrusbyte 10:4ce9eba38dbe 250 const char* streamName,
citrusbyte 10:4ce9eba38dbe 251 int contentLength);
jb8414 0:f479e4f4db0e 252 // Writes HTTP header lines including M2X API Key, host, content
jb8414 0:f479e4f4db0e 253 // type and content length(if the body exists)
jb8414 0:f479e4f4db0e 254 void writeHttpHeader(int contentLength);
jb8414 0:f479e4f4db0e 255 // Parses HTTP response header and return the content length.
jb8414 0:f479e4f4db0e 256 // Note that this function does not parse all http headers, as long
jb8414 0:f479e4f4db0e 257 // as the content length is found, this function will return
jb8414 0:f479e4f4db0e 258 int readContentLength();
jb8414 0:f479e4f4db0e 259 // Skips all HTTP response header part. Return minus value in case
jb8414 0:f479e4f4db0e 260 // the connection is closed before we got all headers
jb8414 0:f479e4f4db0e 261 int skipHttpHeader();
jb8414 0:f479e4f4db0e 262 // Parses and returns the HTTP status code, note this function will
jb8414 0:f479e4f4db0e 263 // return immediately once it gets the status code
jb8414 0:f479e4f4db0e 264 int readStatusCode(bool closeClient);
jb8414 0:f479e4f4db0e 265 // Waits for a certain string pattern in the HTTP header, and returns
jb8414 0:f479e4f4db0e 266 // once the pattern is found. In the pattern, you can use '*' to denote
jb8414 0:f479e4f4db0e 267 // any character
jb8414 0:f479e4f4db0e 268 int waitForString(const char* str);
jb8414 0:f479e4f4db0e 269 // Closes the connection
jb8414 0:f479e4f4db0e 270 void close();
jb8414 0:f479e4f4db0e 271 // Parses JSON response of stream value API, and calls callback function
jb8414 0:f479e4f4db0e 272 // once we get a data point
jb8414 0:f479e4f4db0e 273 int readStreamValue(stream_value_read_callback callback, void* context);
jb8414 0:f479e4f4db0e 274 // Parses JSON response of location API, and calls callback function once
jb8414 0:f479e4f4db0e 275 // we get a data point
jb8414 0:f479e4f4db0e 276 int readLocation(location_read_callback callback, void* context);
jb8414 0:f479e4f4db0e 277 };
jb8414 0:f479e4f4db0e 278
jb8414 0:f479e4f4db0e 279 #include "M2XStreamClient_template.h"
citrusbyte 16:7903152de19f 280 #include "TimeService.h"
jb8414 0:f479e4f4db0e 281
jb8414 0:f479e4f4db0e 282 #endif /* M2XStreamClient_h */