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@5:ea68c8980ad8, 2013-10-24 (annotated)
- Committer:
- citrusbyte
- Date:
- Thu Oct 24 12:22:33 2013 +0000
- Revision:
- 5:ea68c8980ad8
- Child:
- 7:e64d9e1a800a
Initial commit for M2X mbed client library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
citrusbyte | 5:ea68c8980ad8 | 1 | #ifndef StreamParseFunctions_h |
citrusbyte | 5:ea68c8980ad8 | 2 | #define StreamParseFunctions_h |
citrusbyte | 5:ea68c8980ad8 | 3 | |
citrusbyte | 5:ea68c8980ad8 | 4 | // Data structures and functions used to parse stream values |
citrusbyte | 5:ea68c8980ad8 | 5 | |
citrusbyte | 5:ea68c8980ad8 | 6 | #define STREAM_BUF_LEN 20 |
citrusbyte | 5:ea68c8980ad8 | 7 | |
citrusbyte | 5:ea68c8980ad8 | 8 | typedef struct { |
citrusbyte | 5:ea68c8980ad8 | 9 | uint8_t state; |
citrusbyte | 5:ea68c8980ad8 | 10 | char at_str[STREAM_BUF_LEN + 1]; |
citrusbyte | 5:ea68c8980ad8 | 11 | char value_str[STREAM_BUF_LEN + 1]; |
citrusbyte | 5:ea68c8980ad8 | 12 | int index; |
citrusbyte | 5:ea68c8980ad8 | 13 | |
citrusbyte | 5:ea68c8980ad8 | 14 | stream_value_read_callback callback; |
citrusbyte | 5:ea68c8980ad8 | 15 | void* context; |
citrusbyte | 5:ea68c8980ad8 | 16 | } stream_parsing_context_state; |
citrusbyte | 5:ea68c8980ad8 | 17 | |
citrusbyte | 5:ea68c8980ad8 | 18 | #define WAITING_AT 0x1 |
citrusbyte | 5:ea68c8980ad8 | 19 | #define GOT_AT 0x2 |
citrusbyte | 5:ea68c8980ad8 | 20 | #define WAITING_VALUE 0x4 |
citrusbyte | 5:ea68c8980ad8 | 21 | #define GOT_VALUE 0x8 |
citrusbyte | 5:ea68c8980ad8 | 22 | |
citrusbyte | 5:ea68c8980ad8 | 23 | #define GOT_STREAM (GOT_AT | GOT_VALUE) |
citrusbyte | 5:ea68c8980ad8 | 24 | #define TEST_GOT_STREAM(state_) (((state_) & GOT_STREAM) == GOT_STREAM) |
citrusbyte | 5:ea68c8980ad8 | 25 | |
citrusbyte | 5:ea68c8980ad8 | 26 | #define TEST_IS_AT(state_) (((state_) & (WAITING_AT | GOT_AT)) == WAITING_AT) |
citrusbyte | 5:ea68c8980ad8 | 27 | #define TEST_IS_VALUE(state_) (((state_) & (WAITING_VALUE | GOT_VALUE)) == \ |
citrusbyte | 5:ea68c8980ad8 | 28 | WAITING_VALUE) |
citrusbyte | 5:ea68c8980ad8 | 29 | |
citrusbyte | 5:ea68c8980ad8 | 30 | static void on_stream_key_found(jsonlite_callback_context* context, |
citrusbyte | 5:ea68c8980ad8 | 31 | jsonlite_token* token) |
citrusbyte | 5:ea68c8980ad8 | 32 | { |
citrusbyte | 5:ea68c8980ad8 | 33 | stream_parsing_context_state* state = |
citrusbyte | 5:ea68c8980ad8 | 34 | (stream_parsing_context_state*) context->client_state; |
citrusbyte | 5:ea68c8980ad8 | 35 | if (strncmp((const char*) token->start, "at", 2) == 0) { |
citrusbyte | 5:ea68c8980ad8 | 36 | state->state |= WAITING_AT; |
citrusbyte | 5:ea68c8980ad8 | 37 | } else if ((strncmp((const char*) token->start, "value", 5) == 0) && |
citrusbyte | 5:ea68c8980ad8 | 38 | (token->start[5] != 's')) { // get rid of "values" |
citrusbyte | 5:ea68c8980ad8 | 39 | state->state |= WAITING_VALUE; |
citrusbyte | 5:ea68c8980ad8 | 40 | } |
citrusbyte | 5:ea68c8980ad8 | 41 | } |
citrusbyte | 5:ea68c8980ad8 | 42 | |
citrusbyte | 5:ea68c8980ad8 | 43 | static void on_stream_string_found(jsonlite_callback_context* context, |
citrusbyte | 5:ea68c8980ad8 | 44 | jsonlite_token* token) |
citrusbyte | 5:ea68c8980ad8 | 45 | { |
citrusbyte | 5:ea68c8980ad8 | 46 | stream_parsing_context_state* state = |
citrusbyte | 5:ea68c8980ad8 | 47 | (stream_parsing_context_state*) context->client_state; |
citrusbyte | 5:ea68c8980ad8 | 48 | |
citrusbyte | 5:ea68c8980ad8 | 49 | if (TEST_IS_AT(state->state)) { |
citrusbyte | 5:ea68c8980ad8 | 50 | strncpy(state->at_str, (const char*) token->start, |
citrusbyte | 5:ea68c8980ad8 | 51 | MIN(token->end - token->start, STREAM_BUF_LEN)); |
citrusbyte | 5:ea68c8980ad8 | 52 | state->at_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0'; |
citrusbyte | 5:ea68c8980ad8 | 53 | state->state |= GOT_AT; |
citrusbyte | 5:ea68c8980ad8 | 54 | } else if (TEST_IS_VALUE(state->state)) { |
citrusbyte | 5:ea68c8980ad8 | 55 | strncpy(state->value_str, (const char*) token->start, |
citrusbyte | 5:ea68c8980ad8 | 56 | MIN(token->end - token->start, STREAM_BUF_LEN)); |
citrusbyte | 5:ea68c8980ad8 | 57 | state->value_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0'; |
citrusbyte | 5:ea68c8980ad8 | 58 | state->state |= GOT_VALUE; |
citrusbyte | 5:ea68c8980ad8 | 59 | } |
citrusbyte | 5:ea68c8980ad8 | 60 | |
citrusbyte | 5:ea68c8980ad8 | 61 | if (TEST_GOT_STREAM(state->state)) { |
citrusbyte | 5:ea68c8980ad8 | 62 | state->callback(state->at_str, state->value_str, |
citrusbyte | 5:ea68c8980ad8 | 63 | state->index++, state->context); |
citrusbyte | 5:ea68c8980ad8 | 64 | state->state = 0; |
citrusbyte | 5:ea68c8980ad8 | 65 | } |
citrusbyte | 5:ea68c8980ad8 | 66 | } |
citrusbyte | 5:ea68c8980ad8 | 67 | |
citrusbyte | 5:ea68c8980ad8 | 68 | #endif /* StreamParseFunctions_h */ |
citrusbyte | 5:ea68c8980ad8 | 69 | |
citrusbyte | 5:ea68c8980ad8 | 70 |