Urvish Patel / M2XStreamClient

Fork of M2XStreamClient by AT&T M2X Team

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?

UserRevisionLine numberNew contents of line
citrusbyte 5:ea68c8980ad8 1 #ifndef LocationParseFunctions_h
citrusbyte 5:ea68c8980ad8 2 #define LocationParseFunctions_h
citrusbyte 5:ea68c8980ad8 3
citrusbyte 5:ea68c8980ad8 4 // Data structures and functions used to parse locations
citrusbyte 5:ea68c8980ad8 5
citrusbyte 5:ea68c8980ad8 6 #define LOCATION_BUF_LEN 20
citrusbyte 5:ea68c8980ad8 7
citrusbyte 5:ea68c8980ad8 8 typedef struct {
citrusbyte 5:ea68c8980ad8 9 uint16_t state;
citrusbyte 5:ea68c8980ad8 10 char name_str[LOCATION_BUF_LEN + 1];
citrusbyte 5:ea68c8980ad8 11 double latitude;
citrusbyte 5:ea68c8980ad8 12 double longitude;
citrusbyte 5:ea68c8980ad8 13 double elevation;
citrusbyte 5:ea68c8980ad8 14 char timestamp_str[LOCATION_BUF_LEN + 1];
citrusbyte 5:ea68c8980ad8 15 int index;
citrusbyte 5:ea68c8980ad8 16
citrusbyte 5:ea68c8980ad8 17 location_read_callback callback;
citrusbyte 5:ea68c8980ad8 18 void* context;
citrusbyte 5:ea68c8980ad8 19 } location_parsing_context_state;
citrusbyte 5:ea68c8980ad8 20
citrusbyte 5:ea68c8980ad8 21 #define WAITING_NAME 0x1
citrusbyte 5:ea68c8980ad8 22 #define WAITING_LATITUDE 0x2
citrusbyte 5:ea68c8980ad8 23 #define WAITING_LONGITUDE 0x4
citrusbyte 5:ea68c8980ad8 24 #define WAITING_ELEVATION 0x8
citrusbyte 5:ea68c8980ad8 25 #define WAITING_TIMESTAMP 0x10
citrusbyte 5:ea68c8980ad8 26
citrusbyte 5:ea68c8980ad8 27 #define GOT_NAME 0x20
citrusbyte 5:ea68c8980ad8 28 #define GOT_LATITUDE 0x40
citrusbyte 5:ea68c8980ad8 29 #define GOT_LONGITUDE 0x80
citrusbyte 5:ea68c8980ad8 30 #define GOT_ELEVATION 0x100
citrusbyte 5:ea68c8980ad8 31 #define GOT_TIMESTAMP 0x200
citrusbyte 5:ea68c8980ad8 32
citrusbyte 5:ea68c8980ad8 33 #define GOT_LOCATION (GOT_NAME | GOT_LATITUDE | GOT_LONGITUDE | GOT_ELEVATION | GOT_TIMESTAMP)
citrusbyte 5:ea68c8980ad8 34 #define TEST_GOT_LOCATION(state_) (((state_) & GOT_LOCATION) == GOT_LOCATION)
citrusbyte 5:ea68c8980ad8 35
citrusbyte 5:ea68c8980ad8 36 #define TEST_IS_NAME(state_) (((state_) & (WAITING_NAME | GOT_NAME)) == WAITING_NAME)
citrusbyte 5:ea68c8980ad8 37 #define TEST_IS_LATITUDE(state_) (((state_) & (WAITING_LATITUDE | GOT_LATITUDE)) \
citrusbyte 5:ea68c8980ad8 38 == WAITING_LATITUDE)
citrusbyte 5:ea68c8980ad8 39 #define TEST_IS_LONGITUDE(state_) (((state_) & (WAITING_LONGITUDE | GOT_LONGITUDE)) \
citrusbyte 5:ea68c8980ad8 40 == WAITING_LONGITUDE)
citrusbyte 5:ea68c8980ad8 41 #define TEST_IS_ELEVATION(state_) (((state_) & (WAITING_ELEVATION | GOT_ELEVATION)) \
citrusbyte 5:ea68c8980ad8 42 == WAITING_ELEVATION)
citrusbyte 5:ea68c8980ad8 43 #define TEST_IS_TIMESTAMP(state_) (((state_) & (WAITING_TIMESTAMP | GOT_TIMESTAMP)) \
citrusbyte 5:ea68c8980ad8 44 == WAITING_TIMESTAMP)
citrusbyte 5:ea68c8980ad8 45
citrusbyte 5:ea68c8980ad8 46 static void on_location_key_found(jsonlite_callback_context* context,
citrusbyte 5:ea68c8980ad8 47 jsonlite_token* token) {
citrusbyte 5:ea68c8980ad8 48 location_parsing_context_state* state =
citrusbyte 5:ea68c8980ad8 49 (location_parsing_context_state*) context->client_state;
citrusbyte 5:ea68c8980ad8 50 if (strncmp((const char*) token->start, "waypoints", 9) == 0) {
citrusbyte 5:ea68c8980ad8 51 // only parses those locations in waypoints, skip the outer one
citrusbyte 5:ea68c8980ad8 52 state->state = 0;
citrusbyte 5:ea68c8980ad8 53 } else if (strncmp((const char*) token->start, "name", 4) == 0) {
citrusbyte 5:ea68c8980ad8 54 state->state |= WAITING_NAME;
citrusbyte 5:ea68c8980ad8 55 } else if (strncmp((const char*) token->start, "latitude", 8) == 0) {
citrusbyte 5:ea68c8980ad8 56 state->state |= WAITING_LATITUDE;
citrusbyte 5:ea68c8980ad8 57 } else if (strncmp((const char*) token->start, "longitude", 9) == 0) {
citrusbyte 5:ea68c8980ad8 58 state->state |= WAITING_LONGITUDE;
citrusbyte 5:ea68c8980ad8 59 } else if (strncmp((const char*) token->start, "elevation", 9) == 0) {
citrusbyte 5:ea68c8980ad8 60 state->state |= WAITING_ELEVATION;
citrusbyte 5:ea68c8980ad8 61 } else if (strncmp((const char*) token->start, "timestamp", 9) == 0) {
citrusbyte 5:ea68c8980ad8 62 state->state |= WAITING_TIMESTAMP;
citrusbyte 5:ea68c8980ad8 63 }
citrusbyte 5:ea68c8980ad8 64 }
citrusbyte 5:ea68c8980ad8 65
citrusbyte 5:ea68c8980ad8 66 static void on_location_string_found(jsonlite_callback_context* context,
citrusbyte 5:ea68c8980ad8 67 jsonlite_token* token) {
citrusbyte 5:ea68c8980ad8 68 location_parsing_context_state* state =
citrusbyte 5:ea68c8980ad8 69 (location_parsing_context_state*) context->client_state;
citrusbyte 5:ea68c8980ad8 70
citrusbyte 5:ea68c8980ad8 71 if (TEST_IS_NAME(state->state)) {
citrusbyte 5:ea68c8980ad8 72 strncpy(state->name_str, (const char*) token->start,
citrusbyte 5:ea68c8980ad8 73 MIN(token->end - token->start, LOCATION_BUF_LEN));
citrusbyte 5:ea68c8980ad8 74 state->name_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0';
citrusbyte 5:ea68c8980ad8 75 state->state |= GOT_NAME;
citrusbyte 5:ea68c8980ad8 76 } else if (TEST_IS_LATITUDE(state->state)) {
citrusbyte 5:ea68c8980ad8 77 state->latitude = atof((const char*) token->start);
citrusbyte 5:ea68c8980ad8 78 state->state |= GOT_LATITUDE;
citrusbyte 5:ea68c8980ad8 79 } else if (TEST_IS_LONGITUDE(state->state)) {
citrusbyte 5:ea68c8980ad8 80 state->longitude = atof((const char*) token->start);
citrusbyte 5:ea68c8980ad8 81 state->state |= GOT_LONGITUDE;
citrusbyte 5:ea68c8980ad8 82 } else if (TEST_IS_ELEVATION(state->state)) {
citrusbyte 5:ea68c8980ad8 83 state->elevation = atof((const char*) token->start);
citrusbyte 5:ea68c8980ad8 84 state->state |= GOT_ELEVATION;
citrusbyte 5:ea68c8980ad8 85 } else if (TEST_IS_TIMESTAMP(state->state)) {
citrusbyte 5:ea68c8980ad8 86 strncpy(state->timestamp_str, (const char*) token->start,
citrusbyte 5:ea68c8980ad8 87 MIN(token->end - token->start, LOCATION_BUF_LEN));
citrusbyte 5:ea68c8980ad8 88 state->timestamp_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0';
citrusbyte 5:ea68c8980ad8 89 state->state |= GOT_TIMESTAMP;
citrusbyte 5:ea68c8980ad8 90 }
citrusbyte 5:ea68c8980ad8 91
citrusbyte 5:ea68c8980ad8 92 if (TEST_GOT_LOCATION(state->state)) {
citrusbyte 5:ea68c8980ad8 93 state->callback(state->name_str, state->latitude, state->longitude,
citrusbyte 5:ea68c8980ad8 94 state->elevation, state->timestamp_str, state->index++,
citrusbyte 5:ea68c8980ad8 95 state->context);
citrusbyte 5:ea68c8980ad8 96 state->state = 0;
citrusbyte 5:ea68c8980ad8 97 }
citrusbyte 5:ea68c8980ad8 98 }
citrusbyte 5:ea68c8980ad8 99
citrusbyte 5:ea68c8980ad8 100 #endif /* LocationParseFunctions_h */
citrusbyte 5:ea68c8980ad8 101
citrusbyte 5:ea68c8980ad8 102