branch with improvemnts

Fork of M2XStreamClient by AT&T M2X Team

Committer:
NetArc
Date:
Sun Sep 07 05:28:55 2014 +0000
Revision:
2:7ea7ab05f120
Parent:
0:f479e4f4db0e
Child:
10:4ce9eba38dbe
fix for parsing numeric stream values

Who changed what in which revision?

UserRevisionLine numberNew 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
NetArc 2:7ea7ab05f120 6 #define STREAM_BUF_LEN 64
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;
jb8414 0:f479e4f4db0e 35 if (strncmp((const char*) token->start, "at", 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
jb8414 0:f479e4f4db0e 43 static void on_stream_string_found(jsonlite_callback_context* context,
jb8414 0:f479e4f4db0e 44 jsonlite_token* token)
jb8414 0:f479e4f4db0e 45 {
jb8414 0:f479e4f4db0e 46 stream_parsing_context_state* state =
jb8414 0:f479e4f4db0e 47 (stream_parsing_context_state*) context->client_state;
jb8414 0:f479e4f4db0e 48 if (TEST_IS_AT(state->state)) {
jb8414 0:f479e4f4db0e 49 strncpy(state->at_str, (const char*) token->start,
jb8414 0:f479e4f4db0e 50 MIN(token->end - token->start, STREAM_BUF_LEN));
jb8414 0:f479e4f4db0e 51 state->at_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0';
jb8414 0:f479e4f4db0e 52 state->state |= GOT_AT;
jb8414 0:f479e4f4db0e 53 } else if (TEST_IS_VALUE(state->state)) {
jb8414 0:f479e4f4db0e 54 strncpy(state->value_str, (const char*) token->start,
jb8414 0:f479e4f4db0e 55 MIN(token->end - token->start, STREAM_BUF_LEN));
jb8414 0:f479e4f4db0e 56 state->value_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0';
jb8414 0:f479e4f4db0e 57 state->state |= GOT_VALUE;
jb8414 0:f479e4f4db0e 58 }
jb8414 0:f479e4f4db0e 59
jb8414 0:f479e4f4db0e 60 if (TEST_GOT_STREAM(state->state)) {
jb8414 0:f479e4f4db0e 61 state->callback(state->at_str, state->value_str,
jb8414 0:f479e4f4db0e 62 state->index++, state->context);
jb8414 0:f479e4f4db0e 63 state->state = 0;
jb8414 0:f479e4f4db0e 64 }
jb8414 0:f479e4f4db0e 65 }
jb8414 0:f479e4f4db0e 66
jb8414 0:f479e4f4db0e 67 #endif /* StreamParseFunctions_h */