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
StreamParseFunctions.h@21:6878944d2ce2, 2016-01-02 (annotated)
- Committer:
- citrusbyte
- Date:
- Sat Jan 02 02:29:43 2016 +0000
- Revision:
- 21:6878944d2ce2
- Parent:
- 13:0d574742208f
Update library version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:f479e4f4db0e | 1 | #ifndef StreamParseFunctions_h |
jb8414 | 0:f479e4f4db0e | 2 | #define StreamParseFunctions_h |
jb8414 | 0:f479e4f4db0e | 3 | |
jb8414 | 0:f479e4f4db0e | 4 | // Data structures and functions used to parse stream values |
jb8414 | 0:f479e4f4db0e | 5 | |
citrusbyte | 10:4ce9eba38dbe | 6 | #define STREAM_BUF_LEN 32 |
jb8414 | 0:f479e4f4db0e | 7 | |
jb8414 | 0:f479e4f4db0e | 8 | typedef struct { |
jb8414 | 0:f479e4f4db0e | 9 | uint8_t state; |
jb8414 | 0:f479e4f4db0e | 10 | char at_str[STREAM_BUF_LEN + 1]; |
jb8414 | 0:f479e4f4db0e | 11 | char value_str[STREAM_BUF_LEN + 1]; |
jb8414 | 0:f479e4f4db0e | 12 | int index; |
jb8414 | 0:f479e4f4db0e | 13 | |
jb8414 | 0:f479e4f4db0e | 14 | stream_value_read_callback callback; |
jb8414 | 0:f479e4f4db0e | 15 | void* context; |
jb8414 | 0:f479e4f4db0e | 16 | } stream_parsing_context_state; |
jb8414 | 0:f479e4f4db0e | 17 | |
jb8414 | 0:f479e4f4db0e | 18 | #define WAITING_AT 0x1 |
jb8414 | 0:f479e4f4db0e | 19 | #define GOT_AT 0x2 |
jb8414 | 0:f479e4f4db0e | 20 | #define WAITING_VALUE 0x4 |
jb8414 | 0:f479e4f4db0e | 21 | #define GOT_VALUE 0x8 |
jb8414 | 0:f479e4f4db0e | 22 | |
jb8414 | 0:f479e4f4db0e | 23 | #define GOT_STREAM (GOT_AT | GOT_VALUE) |
jb8414 | 0:f479e4f4db0e | 24 | #define TEST_GOT_STREAM(state_) (((state_) & GOT_STREAM) == GOT_STREAM) |
jb8414 | 0:f479e4f4db0e | 25 | |
jb8414 | 0:f479e4f4db0e | 26 | #define TEST_IS_AT(state_) (((state_) & (WAITING_AT | GOT_AT)) == WAITING_AT) |
jb8414 | 0:f479e4f4db0e | 27 | #define TEST_IS_VALUE(state_) (((state_) & (WAITING_VALUE | GOT_VALUE)) == \ |
jb8414 | 0:f479e4f4db0e | 28 | WAITING_VALUE) |
jb8414 | 0:f479e4f4db0e | 29 | |
jb8414 | 0:f479e4f4db0e | 30 | static void on_stream_key_found(jsonlite_callback_context* context, |
jb8414 | 0:f479e4f4db0e | 31 | jsonlite_token* token) |
jb8414 | 0:f479e4f4db0e | 32 | { |
jb8414 | 0:f479e4f4db0e | 33 | stream_parsing_context_state* state = |
jb8414 | 0:f479e4f4db0e | 34 | (stream_parsing_context_state*) context->client_state; |
citrusbyte | 13:0d574742208f | 35 | if (strncmp((const char*) token->start, "timestamp", 2) == 0) { |
jb8414 | 0:f479e4f4db0e | 36 | state->state |= WAITING_AT; |
jb8414 | 0:f479e4f4db0e | 37 | } else if ((strncmp((const char*) token->start, "value", 5) == 0) && |
jb8414 | 0:f479e4f4db0e | 38 | (token->start[5] != 's')) { // get rid of "values" |
jb8414 | 0:f479e4f4db0e | 39 | state->state |= WAITING_VALUE; |
jb8414 | 0:f479e4f4db0e | 40 | } |
jb8414 | 0:f479e4f4db0e | 41 | } |
jb8414 | 0:f479e4f4db0e | 42 | |
citrusbyte | 10:4ce9eba38dbe | 43 | static void on_stream_value_found(jsonlite_callback_context* context, |
citrusbyte | 10:4ce9eba38dbe | 44 | jsonlite_token* token, |
citrusbyte | 10:4ce9eba38dbe | 45 | int type) |
jb8414 | 0:f479e4f4db0e | 46 | { |
jb8414 | 0:f479e4f4db0e | 47 | stream_parsing_context_state* state = |
jb8414 | 0:f479e4f4db0e | 48 | (stream_parsing_context_state*) context->client_state; |
citrusbyte | 10:4ce9eba38dbe | 49 | |
jb8414 | 0:f479e4f4db0e | 50 | if (TEST_IS_AT(state->state)) { |
jb8414 | 0:f479e4f4db0e | 51 | strncpy(state->at_str, (const char*) token->start, |
jb8414 | 0:f479e4f4db0e | 52 | MIN(token->end - token->start, STREAM_BUF_LEN)); |
jb8414 | 0:f479e4f4db0e | 53 | state->at_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0'; |
jb8414 | 0:f479e4f4db0e | 54 | state->state |= GOT_AT; |
jb8414 | 0:f479e4f4db0e | 55 | } else if (TEST_IS_VALUE(state->state)) { |
jb8414 | 0:f479e4f4db0e | 56 | strncpy(state->value_str, (const char*) token->start, |
jb8414 | 0:f479e4f4db0e | 57 | MIN(token->end - token->start, STREAM_BUF_LEN)); |
jb8414 | 0:f479e4f4db0e | 58 | state->value_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0'; |
jb8414 | 0:f479e4f4db0e | 59 | state->state |= GOT_VALUE; |
jb8414 | 0:f479e4f4db0e | 60 | } |
jb8414 | 0:f479e4f4db0e | 61 | |
jb8414 | 0:f479e4f4db0e | 62 | if (TEST_GOT_STREAM(state->state)) { |
jb8414 | 0:f479e4f4db0e | 63 | state->callback(state->at_str, state->value_str, |
citrusbyte | 10:4ce9eba38dbe | 64 | state->index++, state->context, type); |
jb8414 | 0:f479e4f4db0e | 65 | state->state = 0; |
jb8414 | 0:f479e4f4db0e | 66 | } |
jb8414 | 0:f479e4f4db0e | 67 | } |
jb8414 | 0:f479e4f4db0e | 68 | |
citrusbyte | 10:4ce9eba38dbe | 69 | static void on_stream_string_found(jsonlite_callback_context* context, |
citrusbyte | 10:4ce9eba38dbe | 70 | jsonlite_token* token) |
citrusbyte | 10:4ce9eba38dbe | 71 | { |
citrusbyte | 10:4ce9eba38dbe | 72 | on_stream_value_found(context, token, 1); |
citrusbyte | 10:4ce9eba38dbe | 73 | } |
citrusbyte | 10:4ce9eba38dbe | 74 | |
citrusbyte | 10:4ce9eba38dbe | 75 | static void on_stream_number_found(jsonlite_callback_context* context, |
citrusbyte | 10:4ce9eba38dbe | 76 | jsonlite_token* token) |
citrusbyte | 10:4ce9eba38dbe | 77 | { |
citrusbyte | 10:4ce9eba38dbe | 78 | on_stream_value_found(context, token, 2); |
citrusbyte | 10:4ce9eba38dbe | 79 | } |
citrusbyte | 10:4ce9eba38dbe | 80 | |
jb8414 | 0:f479e4f4db0e | 81 | #endif /* StreamParseFunctions_h */ |