branch with improvemnts

Fork of M2XStreamClient by AT&T M2X Team

Committer:
citrusbyte
Date:
Thu Oct 24 12:22:33 2013 +0000
Revision:
5:ea68c8980ad8
Child:
6:e6d66d99dd6f
Initial commit for M2X mbed client library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
citrusbyte 5:ea68c8980ad8 1 #ifndef M2XStreamClient_h
citrusbyte 5:ea68c8980ad8 2 #define M2XStreamClient_h
citrusbyte 5:ea68c8980ad8 3
citrusbyte 5:ea68c8980ad8 4 #define MIN(a, b) (((a) > (b))?(b):(a))
citrusbyte 5:ea68c8980ad8 5
citrusbyte 5:ea68c8980ad8 6 #include "mbed.h"
citrusbyte 5:ea68c8980ad8 7 #include "Client.h"
citrusbyte 5:ea68c8980ad8 8 #include "Utility.h"
citrusbyte 5:ea68c8980ad8 9
citrusbyte 5:ea68c8980ad8 10 #include "NullPrint.h"
citrusbyte 5:ea68c8980ad8 11
citrusbyte 5:ea68c8980ad8 12 static const int E_OK = 0;
citrusbyte 5:ea68c8980ad8 13 static const int E_NOCONNECTION = -1;
citrusbyte 5:ea68c8980ad8 14 static const int E_DISCONNECTED = -2;
citrusbyte 5:ea68c8980ad8 15 static const int E_NOTREACHABLE = -3;
citrusbyte 5:ea68c8980ad8 16 static const int E_INVALID = -4;
citrusbyte 5:ea68c8980ad8 17 static const int E_JSON_INVALID = -5;
citrusbyte 5:ea68c8980ad8 18
citrusbyte 5:ea68c8980ad8 19 typedef void (*stream_value_read_callback)(const char* at,
citrusbyte 5:ea68c8980ad8 20 const char* value,
citrusbyte 5:ea68c8980ad8 21 int index,
citrusbyte 5:ea68c8980ad8 22 void* context);
citrusbyte 5:ea68c8980ad8 23
citrusbyte 5:ea68c8980ad8 24 typedef void (*location_read_callback)(const char* name,
citrusbyte 5:ea68c8980ad8 25 double latitude,
citrusbyte 5:ea68c8980ad8 26 double longitude,
citrusbyte 5:ea68c8980ad8 27 double elevation,
citrusbyte 5:ea68c8980ad8 28 const char* timestamp,
citrusbyte 5:ea68c8980ad8 29 int index,
citrusbyte 5:ea68c8980ad8 30 void* context);
citrusbyte 5:ea68c8980ad8 31
citrusbyte 5:ea68c8980ad8 32 class M2XStreamClient {
citrusbyte 5:ea68c8980ad8 33 public:
citrusbyte 5:ea68c8980ad8 34 static const char* kDefaultM2XHost;
citrusbyte 5:ea68c8980ad8 35 static const int kDefaultM2XPort = 80;
citrusbyte 5:ea68c8980ad8 36
citrusbyte 5:ea68c8980ad8 37 M2XStreamClient(Client* client,
citrusbyte 5:ea68c8980ad8 38 const char* key,
citrusbyte 5:ea68c8980ad8 39 const char* host = kDefaultM2XHost,
citrusbyte 5:ea68c8980ad8 40 int port = kDefaultM2XPort);
citrusbyte 5:ea68c8980ad8 41
citrusbyte 5:ea68c8980ad8 42 // Update data stream, returns the HTTP status code
citrusbyte 5:ea68c8980ad8 43 int send(const char* feedId, const char* streamName, double value);
citrusbyte 5:ea68c8980ad8 44 int send(const char* feedId, const char* streamName, long value);
citrusbyte 5:ea68c8980ad8 45 int send(const char* feedId, const char* streamName, int value);
citrusbyte 5:ea68c8980ad8 46 int send(const char* feedId, const char* streamName, const char* value);
citrusbyte 5:ea68c8980ad8 47
citrusbyte 5:ea68c8980ad8 48 // Receive values for a particular data stream. Since memory is
citrusbyte 5:ea68c8980ad8 49 // very limited on an Arduino, we cannot parse and get all the
citrusbyte 5:ea68c8980ad8 50 // data points in memory. Instead, we use callbacks here: whenever
citrusbyte 5:ea68c8980ad8 51 // a new data point is parsed, we call the callback using the values,
citrusbyte 5:ea68c8980ad8 52 // after that, the values will be thrown away to make space for new
citrusbyte 5:ea68c8980ad8 53 // values.
citrusbyte 5:ea68c8980ad8 54 // Note that you can also pass in a user-specified context in this
citrusbyte 5:ea68c8980ad8 55 // function, this context will be passed to the callback function
citrusbyte 5:ea68c8980ad8 56 // each time we get a data point.
citrusbyte 5:ea68c8980ad8 57 // For each data point, the callback will be called once. The HTTP
citrusbyte 5:ea68c8980ad8 58 // status code will be returned. And the content is only parsed when
citrusbyte 5:ea68c8980ad8 59 // the status code is 200.
citrusbyte 5:ea68c8980ad8 60 int receive(const char* feedId, const char* streamName,
citrusbyte 5:ea68c8980ad8 61 stream_value_read_callback callback, void* context);
citrusbyte 5:ea68c8980ad8 62
citrusbyte 5:ea68c8980ad8 63 // Update datasource location
citrusbyte 5:ea68c8980ad8 64 // NOTE: On an Arduino Uno and other ATMEGA based boards, double has
citrusbyte 5:ea68c8980ad8 65 // 4-byte (32 bits) precision, which is the same as float. So there's
citrusbyte 5:ea68c8980ad8 66 // no natural double-precision floating number on these boards. With
citrusbyte 5:ea68c8980ad8 67 // a float value, we have a precision of roughly 7 digits, that means
citrusbyte 5:ea68c8980ad8 68 // either 5 or 6 digits after the floating point. According to wikipedia,
citrusbyte 5:ea68c8980ad8 69 // a difference of 0.00001 will give us ~1.1132m distance. If this
citrusbyte 5:ea68c8980ad8 70 // precision is good for you, you can use the double-version we provided
citrusbyte 5:ea68c8980ad8 71 // here. Otherwise, you may need to use the string-version and do the
citrusbyte 5:ea68c8980ad8 72 // actual conversion by yourselves.
citrusbyte 5:ea68c8980ad8 73 // However, with an Arduino Due board, double has 8-bytes (64 bits)
citrusbyte 5:ea68c8980ad8 74 // precision, which means you are free to use the double-version only
citrusbyte 5:ea68c8980ad8 75 // without any precision problems.
citrusbyte 5:ea68c8980ad8 76 // Returned value is the http status code.
citrusbyte 5:ea68c8980ad8 77 int updateLocation(const char* feedId, const char* name,
citrusbyte 5:ea68c8980ad8 78 double latitude, double longitude, double elevation);
citrusbyte 5:ea68c8980ad8 79 int updateLocation(const char* feedId, const char* name,
citrusbyte 5:ea68c8980ad8 80 const char* latitude, const char* longitude,
citrusbyte 5:ea68c8980ad8 81 const char* elevation);
citrusbyte 5:ea68c8980ad8 82
citrusbyte 5:ea68c8980ad8 83 // Read location information for a feed. Also used callback to process
citrusbyte 5:ea68c8980ad8 84 // data points for memory reasons. The HTTP status code is returned,
citrusbyte 5:ea68c8980ad8 85 // response is only parsed when the HTTP status code is 200
citrusbyte 5:ea68c8980ad8 86 int readLocation(const char* feedId, location_read_callback callback,
citrusbyte 5:ea68c8980ad8 87 void* context);
citrusbyte 5:ea68c8980ad8 88 private:
citrusbyte 5:ea68c8980ad8 89 Client* _client;
citrusbyte 5:ea68c8980ad8 90 const char* _key;
citrusbyte 5:ea68c8980ad8 91 const char* _host;
citrusbyte 5:ea68c8980ad8 92 int _port;
citrusbyte 5:ea68c8980ad8 93 NullPrint _null_print;
citrusbyte 5:ea68c8980ad8 94
citrusbyte 5:ea68c8980ad8 95 // Writes the HTTP header part for updating a stream value
citrusbyte 5:ea68c8980ad8 96 void writeSendHeader(const char* feedId,
citrusbyte 5:ea68c8980ad8 97 const char* streamName,
citrusbyte 5:ea68c8980ad8 98 int contentLength);
citrusbyte 5:ea68c8980ad8 99 // Writes HTTP header lines including M2X key, host, content
citrusbyte 5:ea68c8980ad8 100 // type and content length(if the body exists)
citrusbyte 5:ea68c8980ad8 101 void writeHttpHeader(int contentLength);
citrusbyte 5:ea68c8980ad8 102 // Parses HTTP response header and return the content length.
citrusbyte 5:ea68c8980ad8 103 // Note that this function does not parse all http headers, as long
citrusbyte 5:ea68c8980ad8 104 // as the content length is found, this function will return
citrusbyte 5:ea68c8980ad8 105 int readContentLength();
citrusbyte 5:ea68c8980ad8 106 // Skips all HTTP response header part. Return minus value in case
citrusbyte 5:ea68c8980ad8 107 // the connection is closed before we got all headers
citrusbyte 5:ea68c8980ad8 108 int skipHttpHeader();
citrusbyte 5:ea68c8980ad8 109 // Parses and returns the HTTP status code, note this function will
citrusbyte 5:ea68c8980ad8 110 // return immediately once it gets the status code
citrusbyte 5:ea68c8980ad8 111 int readStatusCode(bool closeClient);
citrusbyte 5:ea68c8980ad8 112 // Waits for a certain string pattern in the HTTP header, and returns
citrusbyte 5:ea68c8980ad8 113 // once the pattern is found. In the pattern, you can use '*' to denote
citrusbyte 5:ea68c8980ad8 114 // any character
citrusbyte 5:ea68c8980ad8 115 int waitForString(const char* str);
citrusbyte 5:ea68c8980ad8 116 // Closes the connection
citrusbyte 5:ea68c8980ad8 117 void close();
citrusbyte 5:ea68c8980ad8 118 // Parses JSON response of stream value API, and calls callback function
citrusbyte 5:ea68c8980ad8 119 // once we get a data point
citrusbyte 5:ea68c8980ad8 120 int readStreamValue(stream_value_read_callback callback, void* context);
citrusbyte 5:ea68c8980ad8 121 // Parses JSON response of location API, and calls callback function once
citrusbyte 5:ea68c8980ad8 122 // we get a data point
citrusbyte 5:ea68c8980ad8 123 int readLocation(location_read_callback callback, void* context);
citrusbyte 5:ea68c8980ad8 124 };
citrusbyte 5:ea68c8980ad8 125
citrusbyte 5:ea68c8980ad8 126 #endif /* M2XStreamClient_h */
citrusbyte 5:ea68c8980ad8 127
citrusbyte 5:ea68c8980ad8 128