Michael Ammann / M2XStreamClient

Fork of M2XStreamClient by AT&T M2X Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StreamParseFunctions.h Source File

StreamParseFunctions.h

00001 #ifndef StreamParseFunctions_h
00002 #define StreamParseFunctions_h
00003 
00004 // Data structures and functions used to parse stream values
00005 
00006 #define STREAM_BUF_LEN 32
00007 
00008 typedef struct {
00009   uint8_t state;
00010   char at_str[STREAM_BUF_LEN + 1];
00011   char value_str[STREAM_BUF_LEN + 1];
00012   int index;
00013 
00014   stream_value_read_callback callback;
00015   void* context;
00016 } stream_parsing_context_state;
00017 
00018 #define WAITING_AT 0x1
00019 #define GOT_AT 0x2
00020 #define WAITING_VALUE 0x4
00021 #define GOT_VALUE 0x8
00022 
00023 #define GOT_STREAM (GOT_AT | GOT_VALUE)
00024 #define TEST_GOT_STREAM(state_) (((state_) & GOT_STREAM) == GOT_STREAM)
00025 
00026 #define TEST_IS_AT(state_) (((state_) & (WAITING_AT | GOT_AT)) == WAITING_AT)
00027 #define TEST_IS_VALUE(state_) (((state_) & (WAITING_VALUE | GOT_VALUE)) == \
00028                                WAITING_VALUE)
00029 
00030 static void on_stream_key_found(jsonlite_callback_context* context,
00031                                 jsonlite_token* token)
00032 {
00033   stream_parsing_context_state* state =
00034       (stream_parsing_context_state*) context->client_state;
00035   if (strncmp((const char*) token->start, "at", 2) == 0) {
00036     state->state |= WAITING_AT;
00037   } else if ((strncmp((const char*) token->start, "value", 5) == 0) &&
00038              (token->start[5] != 's')) { // get rid of "values"
00039     state->state |= WAITING_VALUE;
00040   }
00041 }
00042 
00043 static void on_stream_value_found(jsonlite_callback_context* context,
00044                                   jsonlite_token* token,
00045                                   int type)
00046 {
00047   stream_parsing_context_state* state =
00048       (stream_parsing_context_state*) context->client_state;
00049 
00050   if (TEST_IS_AT(state->state)) {
00051     strncpy(state->at_str, (const char*) token->start,
00052             MIN(token->end - token->start, STREAM_BUF_LEN));
00053     state->at_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0';
00054     state->state |= GOT_AT;
00055   } else if (TEST_IS_VALUE(state->state)) {
00056     strncpy(state->value_str, (const char*) token->start,
00057             MIN(token->end - token->start, STREAM_BUF_LEN));
00058     state->value_str[MIN(token->end - token->start, STREAM_BUF_LEN)] = '\0';
00059     state->state |= GOT_VALUE;
00060   }
00061 
00062   if (TEST_GOT_STREAM(state->state)) {
00063     state->callback(state->at_str, state->value_str,
00064                     state->index++, state->context, type);
00065     state->state = 0;
00066   }
00067 }
00068 
00069 static void on_stream_string_found(jsonlite_callback_context* context,
00070                                    jsonlite_token* token)
00071 {
00072   on_stream_value_found(context, token, 1);
00073 }
00074 
00075 static void on_stream_number_found(jsonlite_callback_context* context,
00076                                    jsonlite_token* token)
00077 {
00078   on_stream_value_found(context, token, 2);
00079 }
00080 
00081 #endif  /* StreamParseFunctions_h */