Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
jsmn/RadioCfg.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | #include "RadioCfg.h" |
vpcola | 0:f1d3878b8dd9 | 2 | #include <string.h> |
vpcola | 0:f1d3878b8dd9 | 3 | #include <stdlib.h> |
vpcola | 0:f1d3878b8dd9 | 4 | |
vpcola | 0:f1d3878b8dd9 | 5 | static char tempstr[100]; |
vpcola | 0:f1d3878b8dd9 | 6 | |
vpcola | 0:f1d3878b8dd9 | 7 | static int jsoneq(const char * json, jsmntok_t * tok, const char * s) |
vpcola | 0:f1d3878b8dd9 | 8 | { |
vpcola | 0:f1d3878b8dd9 | 9 | if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && |
vpcola | 0:f1d3878b8dd9 | 10 | strncmp(json + tok->start, s, tok->end - tok->start) == 0) { |
vpcola | 0:f1d3878b8dd9 | 11 | return 0; |
vpcola | 0:f1d3878b8dd9 | 12 | } |
vpcola | 0:f1d3878b8dd9 | 13 | return -1; |
vpcola | 0:f1d3878b8dd9 | 14 | } |
vpcola | 0:f1d3878b8dd9 | 15 | |
vpcola | 0:f1d3878b8dd9 | 16 | int parseradioconfig(const char * jsonstring, RadioCfg & radiocfg) |
vpcola | 0:f1d3878b8dd9 | 17 | { |
vpcola | 0:f1d3878b8dd9 | 18 | int i, r; |
vpcola | 0:f1d3878b8dd9 | 19 | |
vpcola | 0:f1d3878b8dd9 | 20 | jsmn_parser p; |
vpcola | 0:f1d3878b8dd9 | 21 | jsmntok_t t[128]; |
vpcola | 0:f1d3878b8dd9 | 22 | |
vpcola | 0:f1d3878b8dd9 | 23 | jsmn_init(&p); |
vpcola | 0:f1d3878b8dd9 | 24 | r = jsmn_parse(&p, jsonstring, strlen(jsonstring), t, sizeof(t)/sizeof(t[0])); |
vpcola | 0:f1d3878b8dd9 | 25 | if ( r < 0 ) |
vpcola | 0:f1d3878b8dd9 | 26 | return -1; |
vpcola | 0:f1d3878b8dd9 | 27 | |
vpcola | 0:f1d3878b8dd9 | 28 | /* Top level element is an object */ |
vpcola | 0:f1d3878b8dd9 | 29 | if (r < 1 || t[0].type != JSMN_OBJECT) |
vpcola | 0:f1d3878b8dd9 | 30 | { |
vpcola | 0:f1d3878b8dd9 | 31 | return -1; |
vpcola | 0:f1d3878b8dd9 | 32 | } |
vpcola | 0:f1d3878b8dd9 | 33 | |
vpcola | 0:f1d3878b8dd9 | 34 | /* Loop over all tokens */ |
vpcola | 0:f1d3878b8dd9 | 35 | for (i = 1; i < r; i++) |
vpcola | 0:f1d3878b8dd9 | 36 | { |
vpcola | 0:f1d3878b8dd9 | 37 | if (jsoneq(jsonstring, &t[i], "error") == 0) |
vpcola | 0:f1d3878b8dd9 | 38 | { |
vpcola | 0:f1d3878b8dd9 | 39 | // Query returned an error ... |
vpcola | 0:f1d3878b8dd9 | 40 | return -1; |
vpcola | 0:f1d3878b8dd9 | 41 | } |
vpcola | 0:f1d3878b8dd9 | 42 | else if (jsoneq(jsonstring, &t[i], "id") == 0) |
vpcola | 0:f1d3878b8dd9 | 43 | { |
vpcola | 0:f1d3878b8dd9 | 44 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 45 | strncpy(&tempstr[0], jsonstring + t[i+1].start, (t[i+1].end - t[i+1].start)); |
vpcola | 0:f1d3878b8dd9 | 46 | // Store the radio id ... |
vpcola | 0:f1d3878b8dd9 | 47 | radiocfg.radioID = strtoull(tempstr, NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 48 | i++; |
vpcola | 0:f1d3878b8dd9 | 49 | } |
vpcola | 0:f1d3878b8dd9 | 50 | else if (jsoneq(jsonstring, &t[i], "name") == 0) |
vpcola | 0:f1d3878b8dd9 | 51 | { |
vpcola | 0:f1d3878b8dd9 | 52 | memset(&radiocfg.name[0], 0, RADIO_NAME_LEN); |
vpcola | 0:f1d3878b8dd9 | 53 | strncpy(&radiocfg.name[0], jsonstring + t[i+1].start, (t[i+1].end - t[i+1].start)); |
vpcola | 0:f1d3878b8dd9 | 54 | i++; |
vpcola | 0:f1d3878b8dd9 | 55 | } |
vpcola | 0:f1d3878b8dd9 | 56 | else if (jsoneq(jsonstring, &t[i], "country") == 0) |
vpcola | 0:f1d3878b8dd9 | 57 | { |
vpcola | 0:f1d3878b8dd9 | 58 | memset(&radiocfg.country[0], 0, RADIO_NAME_LEN); |
vpcola | 0:f1d3878b8dd9 | 59 | strncpy(&radiocfg.country[0], jsonstring + t[i+1].start, (t[i+1].end - t[i+1].start)); |
vpcola | 0:f1d3878b8dd9 | 60 | i++; |
vpcola | 0:f1d3878b8dd9 | 61 | } |
vpcola | 0:f1d3878b8dd9 | 62 | else if (jsoneq(jsonstring, &t[i], "utc_offset") == 0) |
vpcola | 0:f1d3878b8dd9 | 63 | { |
vpcola | 0:f1d3878b8dd9 | 64 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 65 | strncpy(&tempstr[0], jsonstring + t[i+1].start, (t[i+1].end - t[i+1].start)); |
vpcola | 0:f1d3878b8dd9 | 66 | // Store the radio id ... |
vpcola | 0:f1d3878b8dd9 | 67 | radiocfg.utc_offset = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 68 | i++; |
vpcola | 0:f1d3878b8dd9 | 69 | } |
vpcola | 0:f1d3878b8dd9 | 70 | else if (jsoneq(jsonstring, &t[i], "radios") == 0) |
vpcola | 0:f1d3878b8dd9 | 71 | { |
vpcola | 0:f1d3878b8dd9 | 72 | int j; |
vpcola | 0:f1d3878b8dd9 | 73 | // Radios is an array of radio objects |
vpcola | 0:f1d3878b8dd9 | 74 | if (t[i+1].type != JSMN_ARRAY){ |
vpcola | 0:f1d3878b8dd9 | 75 | continue; |
vpcola | 0:f1d3878b8dd9 | 76 | } |
vpcola | 0:f1d3878b8dd9 | 77 | |
vpcola | 0:f1d3878b8dd9 | 78 | int arrsize = t[i+1].size; |
vpcola | 0:f1d3878b8dd9 | 79 | // Move to the start of the array |
vpcola | 0:f1d3878b8dd9 | 80 | i += 2; |
vpcola | 0:f1d3878b8dd9 | 81 | for (j = 0; j < arrsize; j++) |
vpcola | 0:f1d3878b8dd9 | 82 | { |
vpcola | 0:f1d3878b8dd9 | 83 | RadioSlave slv; |
vpcola | 0:f1d3878b8dd9 | 84 | int r = t[i + j].size; |
vpcola | 0:f1d3878b8dd9 | 85 | //jsmntok_t * g = &t[i + j]; |
vpcola | 0:f1d3878b8dd9 | 86 | //printf(" * %.*s\n", g->end - g->start, jsonstring + g->start); |
vpcola | 0:f1d3878b8dd9 | 87 | // Enumerate objects within the array |
vpcola | 0:f1d3878b8dd9 | 88 | for (int z = 0; z < r ; z++) |
vpcola | 0:f1d3878b8dd9 | 89 | { |
vpcola | 0:f1d3878b8dd9 | 90 | |
vpcola | 0:f1d3878b8dd9 | 91 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "id") == 0) |
vpcola | 0:f1d3878b8dd9 | 92 | { |
vpcola | 0:f1d3878b8dd9 | 93 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 94 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 95 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 96 | slv.radioID = strtoull(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 97 | } |
vpcola | 0:f1d3878b8dd9 | 98 | else if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "name") == 0) |
vpcola | 0:f1d3878b8dd9 | 99 | { |
vpcola | 0:f1d3878b8dd9 | 100 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 101 | memset(&slv.name[0], 0, RADIO_NAME_LEN); |
vpcola | 0:f1d3878b8dd9 | 102 | strncpy(&slv.name[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 103 | } |
vpcola | 0:f1d3878b8dd9 | 104 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "temp_pin") == 0) |
vpcola | 0:f1d3878b8dd9 | 105 | { |
vpcola | 0:f1d3878b8dd9 | 106 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 107 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 108 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 109 | slv.temp_pin = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 110 | } |
vpcola | 0:f1d3878b8dd9 | 111 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "humid_pin") == 0) |
vpcola | 0:f1d3878b8dd9 | 112 | { |
vpcola | 0:f1d3878b8dd9 | 113 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 114 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 115 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 116 | slv.humid_pin = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 117 | } |
vpcola | 0:f1d3878b8dd9 | 118 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "lumin_pin") == 0) |
vpcola | 0:f1d3878b8dd9 | 119 | { |
vpcola | 0:f1d3878b8dd9 | 120 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 121 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 122 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 123 | slv.lumin_pin = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 124 | } |
vpcola | 0:f1d3878b8dd9 | 125 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "spinkler_pin") == 0) |
vpcola | 0:f1d3878b8dd9 | 126 | { |
vpcola | 0:f1d3878b8dd9 | 127 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 128 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 129 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 130 | slv.sprinkler_pin = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 131 | } |
vpcola | 0:f1d3878b8dd9 | 132 | if (jsoneq(jsonstring, &t[i+j+(z*2)+1], "hthreshold") == 0) |
vpcola | 0:f1d3878b8dd9 | 133 | { |
vpcola | 0:f1d3878b8dd9 | 134 | jsmntok_t * o = &t[i+j+(z*2)+2]; |
vpcola | 0:f1d3878b8dd9 | 135 | memset(&tempstr[0], 0, sizeof(tempstr)); |
vpcola | 0:f1d3878b8dd9 | 136 | strncpy(&tempstr[0], jsonstring + o->start, o->end - o->start); |
vpcola | 0:f1d3878b8dd9 | 137 | slv.hthreshold = strtoul(&tempstr[0], NULL, 0); |
vpcola | 0:f1d3878b8dd9 | 138 | } |
vpcola | 0:f1d3878b8dd9 | 139 | else |
vpcola | 0:f1d3878b8dd9 | 140 | { |
vpcola | 0:f1d3878b8dd9 | 141 | // TODO: |
vpcola | 0:f1d3878b8dd9 | 142 | } |
vpcola | 0:f1d3878b8dd9 | 143 | } |
vpcola | 0:f1d3878b8dd9 | 144 | // Add the radio slave to the vector |
vpcola | 0:f1d3878b8dd9 | 145 | radiocfg.radios.push_back(slv); |
vpcola | 0:f1d3878b8dd9 | 146 | i += (t[i+j].size) * 2; |
vpcola | 0:f1d3878b8dd9 | 147 | } |
vpcola | 0:f1d3878b8dd9 | 148 | // Move to the end of the object |
vpcola | 0:f1d3878b8dd9 | 149 | i += 4; |
vpcola | 0:f1d3878b8dd9 | 150 | }else |
vpcola | 0:f1d3878b8dd9 | 151 | { |
vpcola | 0:f1d3878b8dd9 | 152 | |
vpcola | 0:f1d3878b8dd9 | 153 | } |
vpcola | 0:f1d3878b8dd9 | 154 | } |
vpcola | 0:f1d3878b8dd9 | 155 | |
vpcola | 0:f1d3878b8dd9 | 156 | return 0; |
vpcola | 0:f1d3878b8dd9 | 157 | } |