-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsi_json.c
258 lines (221 loc) · 6.69 KB
/
si_json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* JSON formatter
*/
#include <json-c/json.h>
#include <string.h>
#include <stdio.h>
#include "si_base.h"
const char *stat_errdesc[] = {
"OK",
"Race not found",
"Bad authentication",
"No connection to server",
"Error in program",
"Unknown state"
};
const char *si_stat_errdesc(int err){
return stat_errdesc[err];
}
/* Make init JSON string */
char *si_init_json(void){
json_object *jo, *ji;
const char *result;
char *jsonstr;
jo = json_object_new_object();
ji = json_object_new_string("init");
json_object_object_add(jo, "init", ji);
result = json_object_to_json_string(jo);
if((jsonstr = (char *) malloc(strlen(result)+1)) == NULL){
json_object_put(jo);
return NULL;
}
strcpy(jsonstr, result);
json_object_put(jo);
return jsonstr;
}
/* Make punch details JSON - private helper */
json_object * make_json_punch(struct s_punch *punch){
json_object *jp, *ji;
jp = json_object_new_object();
ji = json_object_new_int(punch->cn);
json_object_object_add(jp, "cn", ji);
ji = json_object_new_int(punch->time);
json_object_object_add(jp, "time", ji);
ji = json_object_new_int(punch->timestat);
json_object_object_add(jp, "tst", ji);
return jp;
}
/* Make data JSON string from punch data */
char *si_data_json(struct s_sidata *card){
json_object *jo, *ji, *jp, *ja;
int i;
const char *result;
char *jsonstr;
jo = json_object_new_object();
ji = json_object_new_int(card->cardnum);
json_object_object_add(jo, "si_number", ji);
ji = json_object_new_int(card->cardtype);
json_object_object_add(jo, "si_type", ji);
if(card->lname){
ji = json_object_new_string(card->lname);
json_object_object_add(jo, "si_lname", ji);
}
if(card->fname){
ji = json_object_new_string(card->fname);
json_object_object_add(jo, "si_fname", ji);
}
jp = make_json_punch(&card->clear);
json_object_object_add(jo, "clear", jp);
jp = make_json_punch(&card->check);
json_object_object_add(jo, "check", jp);
jp = make_json_punch(&card->start);
json_object_object_add(jo, "start", jp);
jp = make_json_punch(&card->finish);
json_object_object_add(jo, "finish", jp);
ja = json_object_new_array();
for(i = 0; i < card->npunch; i++){
jp = make_json_punch(&card->punches[i]);
json_object_array_add(ja, jp);
}
json_object_object_add(jo, "punches", ja);
result = json_object_to_json_string(jo);
if((jsonstr = (char *) malloc(strlen(result)+1)) == NULL){
json_object_put(jo);
return NULL;
}
strcpy(jsonstr, result);
json_object_put(jo);
return jsonstr;
}
/* Get punch details - private helper */
void get_json_punch(struct s_punch *punch, json_object *jp){
enum json_type type;
json_object_object_foreach(jp, keyp, valp) {
type = json_object_get_type(valp);
if(strcmp(keyp, "time") == 0 && type == json_type_int){
punch->time = json_object_get_int(valp);
}
if(strcmp(keyp, "tst") == 0 && type == json_type_int){
punch->timestat = json_object_get_int(valp);
}
if(strcmp(keyp, "cn") == 0 && type == json_type_int){
punch->cn = json_object_get_int(valp);
}
}
json_object_put(jp);
}
/* Parse JSON string and return punch data */
struct s_sidata *si_json_data(struct s_sidata *card, char *jsonstr){
json_object *ja, *ji, *jp;
enum json_type type;
int i;
ji = json_tokener_parse(jsonstr);
if(! json_object_is_type(ji, json_type_object)){
json_object_put(ji);
return NULL;
}
json_object_object_foreach(ji, key, val) {
type = json_object_get_type(val);
if(strcmp(key, "si_number") == 0 && type == json_type_int){
card->cardnum = json_object_get_int(val);
}
if(strcmp(key, "si_type") == 0 && type == json_type_int){
card->cardtype = json_object_get_int(val);
}
if(strcmp(key, "si_lname") == 0 && type == json_type_string){
strcpy(card->lname, json_object_get_string(val));
}
if(strcmp(key, "si_fname") == 0 && type == json_type_string){
strcpy(card->fname, json_object_get_string(val));
}
if(strcmp(key, "tm_start") == 0 && type == json_type_int){
card->start.time = json_object_get_int(val);
}
if(strcmp(key, "tm_check") == 0 && type == json_type_int){
card->check.time = json_object_get_int(val);
}
if(strcmp(key, "tm_clear") == 0 && type == json_type_int){
card->clear.time = json_object_get_int(val);
card->clear.timestat = H24;
}
if(strcmp(key, "clear") == 0 && type == json_type_object){
json_object_object_get_ex(ji, key, &jp);
get_json_punch(&card->clear, jp);
}
if(strcmp(key, "check") == 0 && type == json_type_object){
json_object_object_get_ex(ji, key, &jp);
get_json_punch(&card->check, jp);
}
if(strcmp(key, "start") == 0 && type == json_type_object){
json_object_object_get_ex(ji, key, &jp);
get_json_punch(&card->start, jp);
}
if(strcmp(key, "finish") == 0 && type == json_type_object){
json_object_object_get_ex(ji, key, &jp);
get_json_punch(&card->finish, jp);
}
if(strcmp(key, "tm_finish") == 0 && type == json_type_int){
card->finish.time = json_object_get_int(val);
}
if(strcmp(key, "punches") == 0 && type == json_type_array){
json_object_object_get_ex(ji, key, &ja);
card->npunch = json_object_array_length(ja);
for(i = 0; i < card->npunch; i++) {
jp = json_object_array_get_idx(ja, i);
get_json_punch(&card->punches[i], jp);
}
}
}
return card;
}
/* Parse status response from server and return status */
int si_getstatus_json(char *jsonstr){
json_object *ji;
enum json_type type;
ji = json_tokener_parse(jsonstr);
if(! json_object_is_type(ji, json_type_object)){
json_object_put(ji);
return STAT_ERROR;
}
json_object_object_foreach(ji, key, val) { /*Passing through every array element*/
type = json_object_get_type(val);
if(strcmp(key, "status") == 0 && type == json_type_string){
if(strcmp(json_object_get_string(val), "OK") == 0){
return STAT_OK;
}
}
if(strcmp(key, "error") == 0 && type == json_type_string){
if(strcmp(json_object_get_string(val), "NoRace") == 0){
return STAT_NORACE;
}else if(strcmp(json_object_get_string(val), "NoAuth") == 0){
return STAT_NOAUTH;
}
}
}
return STAT_UNKNOWN;
}
/* Parse JSON string and return string for specific key */
char *si_getstring_json(char *jsonstr, char *retkey){
json_object *ji;
enum json_type type;
char *str;
ji = json_tokener_parse(jsonstr);
if(! json_object_is_type(ji, json_type_object)){
json_object_put(ji);
return NULL;
}
json_object_object_foreach(ji, key, val) { /*Passing through every array element*/
type = json_object_get_type(val);
if(strcmp(key, retkey) == 0 && type == json_type_string) {
if((str = (char *) malloc((strlen(json_object_get_string(val)) + 1) * sizeof(char))) == NULL) {
json_object_put(ji);
return NULL;
}else{
strcpy(str, json_object_get_string(val));
json_object_put(ji);
return str;
}
}
}
return NULL;
}