M2XStreamClient fork with a workaround in M2XStreamClient.cpp for the MTS_Wifi_Connect_M2X example.
Dependents: STM32_MTS_Wifi_Connect_M2X M2X_STM32_MTS_Temp MTS_WiFi_Connect_M2X_Example
Fork of M2XStreamClient by
LocationParseFunctions.h@2:6fbc104d1321, 2014-08-26 (annotated)
- Committer:
- joe_tijerina
- Date:
- Tue Aug 26 15:20:40 2014 +0000
- Revision:
- 2:6fbc104d1321
- Parent:
- 0:f479e4f4db0e
Fixed compiler warning (added ifndef MIN to avoid defining duplicate MIN define)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:f479e4f4db0e | 1 | #ifndef LocationParseFunctions_h |
jb8414 | 0:f479e4f4db0e | 2 | #define LocationParseFunctions_h |
jb8414 | 0:f479e4f4db0e | 3 | |
jb8414 | 0:f479e4f4db0e | 4 | // Data structures and functions used to parse locations |
jb8414 | 0:f479e4f4db0e | 5 | |
jb8414 | 0:f479e4f4db0e | 6 | #define LOCATION_BUF_LEN 20 |
jb8414 | 0:f479e4f4db0e | 7 | |
jb8414 | 0:f479e4f4db0e | 8 | typedef struct { |
jb8414 | 0:f479e4f4db0e | 9 | uint16_t state; |
jb8414 | 0:f479e4f4db0e | 10 | char name_str[LOCATION_BUF_LEN + 1]; |
jb8414 | 0:f479e4f4db0e | 11 | double latitude; |
jb8414 | 0:f479e4f4db0e | 12 | double longitude; |
jb8414 | 0:f479e4f4db0e | 13 | double elevation; |
jb8414 | 0:f479e4f4db0e | 14 | char timestamp_str[LOCATION_BUF_LEN + 1]; |
jb8414 | 0:f479e4f4db0e | 15 | int index; |
jb8414 | 0:f479e4f4db0e | 16 | |
jb8414 | 0:f479e4f4db0e | 17 | location_read_callback callback; |
jb8414 | 0:f479e4f4db0e | 18 | void* context; |
jb8414 | 0:f479e4f4db0e | 19 | } location_parsing_context_state; |
jb8414 | 0:f479e4f4db0e | 20 | |
jb8414 | 0:f479e4f4db0e | 21 | #define WAITING_NAME 0x1 |
jb8414 | 0:f479e4f4db0e | 22 | #define WAITING_LATITUDE 0x2 |
jb8414 | 0:f479e4f4db0e | 23 | #define WAITING_LONGITUDE 0x4 |
jb8414 | 0:f479e4f4db0e | 24 | #define WAITING_ELEVATION 0x8 |
jb8414 | 0:f479e4f4db0e | 25 | #define WAITING_TIMESTAMP 0x10 |
jb8414 | 0:f479e4f4db0e | 26 | |
jb8414 | 0:f479e4f4db0e | 27 | #define GOT_NAME 0x20 |
jb8414 | 0:f479e4f4db0e | 28 | #define GOT_LATITUDE 0x40 |
jb8414 | 0:f479e4f4db0e | 29 | #define GOT_LONGITUDE 0x80 |
jb8414 | 0:f479e4f4db0e | 30 | #define GOT_ELEVATION 0x100 |
jb8414 | 0:f479e4f4db0e | 31 | #define GOT_TIMESTAMP 0x200 |
jb8414 | 0:f479e4f4db0e | 32 | |
jb8414 | 0:f479e4f4db0e | 33 | #define GOT_LOCATION (GOT_NAME | GOT_LATITUDE | GOT_LONGITUDE | GOT_ELEVATION | GOT_TIMESTAMP) |
jb8414 | 0:f479e4f4db0e | 34 | #define TEST_GOT_LOCATION(state_) (((state_) & GOT_LOCATION) == GOT_LOCATION) |
jb8414 | 0:f479e4f4db0e | 35 | |
jb8414 | 0:f479e4f4db0e | 36 | #define TEST_IS_NAME(state_) (((state_) & (WAITING_NAME | GOT_NAME)) == WAITING_NAME) |
jb8414 | 0:f479e4f4db0e | 37 | #define TEST_IS_LATITUDE(state_) (((state_) & (WAITING_LATITUDE | GOT_LATITUDE)) \ |
jb8414 | 0:f479e4f4db0e | 38 | == WAITING_LATITUDE) |
jb8414 | 0:f479e4f4db0e | 39 | #define TEST_IS_LONGITUDE(state_) (((state_) & (WAITING_LONGITUDE | GOT_LONGITUDE)) \ |
jb8414 | 0:f479e4f4db0e | 40 | == WAITING_LONGITUDE) |
jb8414 | 0:f479e4f4db0e | 41 | #define TEST_IS_ELEVATION(state_) (((state_) & (WAITING_ELEVATION | GOT_ELEVATION)) \ |
jb8414 | 0:f479e4f4db0e | 42 | == WAITING_ELEVATION) |
jb8414 | 0:f479e4f4db0e | 43 | #define TEST_IS_TIMESTAMP(state_) (((state_) & (WAITING_TIMESTAMP | GOT_TIMESTAMP)) \ |
jb8414 | 0:f479e4f4db0e | 44 | == WAITING_TIMESTAMP) |
jb8414 | 0:f479e4f4db0e | 45 | |
jb8414 | 0:f479e4f4db0e | 46 | static void on_location_key_found(jsonlite_callback_context* context, |
jb8414 | 0:f479e4f4db0e | 47 | jsonlite_token* token) { |
jb8414 | 0:f479e4f4db0e | 48 | location_parsing_context_state* state = |
jb8414 | 0:f479e4f4db0e | 49 | (location_parsing_context_state*) context->client_state; |
jb8414 | 0:f479e4f4db0e | 50 | if (strncmp((const char*) token->start, "waypoints", 9) == 0) { |
jb8414 | 0:f479e4f4db0e | 51 | // only parses those locations in waypoints, skip the outer one |
jb8414 | 0:f479e4f4db0e | 52 | state->state = 0; |
jb8414 | 0:f479e4f4db0e | 53 | } else if (strncmp((const char*) token->start, "name", 4) == 0) { |
jb8414 | 0:f479e4f4db0e | 54 | state->state |= WAITING_NAME; |
jb8414 | 0:f479e4f4db0e | 55 | } else if (strncmp((const char*) token->start, "latitude", 8) == 0) { |
jb8414 | 0:f479e4f4db0e | 56 | state->state |= WAITING_LATITUDE; |
jb8414 | 0:f479e4f4db0e | 57 | } else if (strncmp((const char*) token->start, "longitude", 9) == 0) { |
jb8414 | 0:f479e4f4db0e | 58 | state->state |= WAITING_LONGITUDE; |
jb8414 | 0:f479e4f4db0e | 59 | } else if (strncmp((const char*) token->start, "elevation", 9) == 0) { |
jb8414 | 0:f479e4f4db0e | 60 | state->state |= WAITING_ELEVATION; |
jb8414 | 0:f479e4f4db0e | 61 | } else if (strncmp((const char*) token->start, "timestamp", 9) == 0) { |
jb8414 | 0:f479e4f4db0e | 62 | state->state |= WAITING_TIMESTAMP; |
jb8414 | 0:f479e4f4db0e | 63 | } |
jb8414 | 0:f479e4f4db0e | 64 | } |
jb8414 | 0:f479e4f4db0e | 65 | |
jb8414 | 0:f479e4f4db0e | 66 | static void on_location_string_found(jsonlite_callback_context* context, |
jb8414 | 0:f479e4f4db0e | 67 | jsonlite_token* token) { |
jb8414 | 0:f479e4f4db0e | 68 | location_parsing_context_state* state = |
jb8414 | 0:f479e4f4db0e | 69 | (location_parsing_context_state*) context->client_state; |
jb8414 | 0:f479e4f4db0e | 70 | |
jb8414 | 0:f479e4f4db0e | 71 | if (TEST_IS_NAME(state->state)) { |
jb8414 | 0:f479e4f4db0e | 72 | strncpy(state->name_str, (const char*) token->start, |
jb8414 | 0:f479e4f4db0e | 73 | MIN(token->end - token->start, LOCATION_BUF_LEN)); |
jb8414 | 0:f479e4f4db0e | 74 | state->name_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0'; |
jb8414 | 0:f479e4f4db0e | 75 | state->state |= GOT_NAME; |
jb8414 | 0:f479e4f4db0e | 76 | } else if (TEST_IS_LATITUDE(state->state)) { |
jb8414 | 0:f479e4f4db0e | 77 | state->latitude = atof((const char*) token->start); |
jb8414 | 0:f479e4f4db0e | 78 | state->state |= GOT_LATITUDE; |
jb8414 | 0:f479e4f4db0e | 79 | } else if (TEST_IS_LONGITUDE(state->state)) { |
jb8414 | 0:f479e4f4db0e | 80 | state->longitude = atof((const char*) token->start); |
jb8414 | 0:f479e4f4db0e | 81 | state->state |= GOT_LONGITUDE; |
jb8414 | 0:f479e4f4db0e | 82 | } else if (TEST_IS_ELEVATION(state->state)) { |
jb8414 | 0:f479e4f4db0e | 83 | state->elevation = atof((const char*) token->start); |
jb8414 | 0:f479e4f4db0e | 84 | state->state |= GOT_ELEVATION; |
jb8414 | 0:f479e4f4db0e | 85 | } else if (TEST_IS_TIMESTAMP(state->state)) { |
jb8414 | 0:f479e4f4db0e | 86 | strncpy(state->timestamp_str, (const char*) token->start, |
jb8414 | 0:f479e4f4db0e | 87 | MIN(token->end - token->start, LOCATION_BUF_LEN)); |
jb8414 | 0:f479e4f4db0e | 88 | state->timestamp_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0'; |
jb8414 | 0:f479e4f4db0e | 89 | state->state |= GOT_TIMESTAMP; |
jb8414 | 0:f479e4f4db0e | 90 | } |
jb8414 | 0:f479e4f4db0e | 91 | |
jb8414 | 0:f479e4f4db0e | 92 | if (TEST_GOT_LOCATION(state->state)) { |
jb8414 | 0:f479e4f4db0e | 93 | state->callback(state->name_str, state->latitude, state->longitude, |
jb8414 | 0:f479e4f4db0e | 94 | state->elevation, state->timestamp_str, state->index++, |
jb8414 | 0:f479e4f4db0e | 95 | state->context); |
jb8414 | 0:f479e4f4db0e | 96 | state->state = 0; |
jb8414 | 0:f479e4f4db0e | 97 | } |
jb8414 | 0:f479e4f4db0e | 98 | } |
jb8414 | 0:f479e4f4db0e | 99 | |
jb8414 | 0:f479e4f4db0e | 100 | #endif /* LocationParseFunctions_h */ |