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