-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_handler.c
282 lines (238 loc) · 6.98 KB
/
req_handler.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cJSON.h>
#include "req_handler.h"
#ifdef UTFLAG
extern int call_by_test_svr_process_req3;
extern int call_by_test_query_term_info4;
#endif
/************************************************************************
* Called by POST, will assign a terminal ID, store an entry *
* in DB (in-memory data array), then return ID with in json format. *
* *
* Parameter examples: *
* input_data: {"TransactionType":"Credit","cardType":"Visa"} *
* resp_data: {15}, if a terminal ID assigned successfully *
* resp_data: {}, if no available terminal ID *
* *
* Return values: *
* if succeeds, return 0 *
* if fails, return 1 *
************************************************************************/
int
svr_process_req (char * input_data, char * resp_data)
{
int id=0;
int ret_val;
char id_char[TERMID_LENGTH] = "";
struct terminal_info_struct l_terminal_info;
/* For unit testing, intialize l_terminal_info so as to quickly compare with expected
Data, which has been initialized to 0 by compiler for unused char bytes.
*/
#ifdef UTFLAG
memset (&l_terminal_info,0,sizeof(l_terminal_info));
#endif
ret_val=parse_json(input_data, &l_terminal_info);
if (ret_val)
{
ERRLOG ("unexpected return value.");
return 1;
}
/* set id 0 if no spare */
ret_val=find_spare_id(&id);
if (ret_val)
{
ERRLOG ("unexpected return value.");
return 1;
}
if ( id != 0)
{
l_terminal_info.id = id;
l_terminal_info.flag = 1;
snprintf(id_char,TERMID_LENGTH,"{%d}",id);
if (insert_db(&l_terminal_info) != 0)
{
ERRLOG ("unexpected return value.");
return 1;
}
}
else
{
strncpy(id_char,"{}",3);
}
snprintf (resp_data, TERMID_LENGTH, "%s", id_char);
return 0;
}
/************************************************************************
* Query DB for the terminal id, return term_info in json format *
* *
* Parameter examples: *
* id: 15 *
* term_info: {"terminalID":15,"TransactionType":"Credit", *
* "cardType":"Visa"} *
* *
* Return values: *
* if succeeds, return 0 *
* if fails, return 1 *
************************************************************************/
int
query_term_info (int id, char *term_info)
{
struct terminal_info_struct term_db_entry;
struct terminal_info_struct *term_db_ptr;
char term_json_entry[MAXJSON_INFOSIZE] = "";
char *term_json_ptr;
int ret_val;
term_db_ptr = &term_db_entry;
term_json_ptr = term_json_entry;
ret_val=query_db(id, term_db_ptr);
if (ret_val == 0)
{
ret_val=struct2json((struct terminal_info_struct *)term_db_ptr, term_json_ptr);
if (ret_val)
{
ERRLOG ("unexpected return value.");
return 1;
}
}
else if (ret_val == 1)
{
/* if id no found in DB, let term_json_ptr pointed to "{}" */
strncpy(term_json_ptr, "{}",3);
}
else
{
ERRLOG ("unexpected return value.");
return 1;
}
snprintf(term_info, MAXJSON_INFOSIZE,"%s",term_json_ptr);
return 0;
}
/************************************************************************
* Respond all assigned terminal IDs in DB. *
* *
* Parameter examples: *
* if found, term_list populated, e.g. "{2 4 5 10}" *
* if not found, term_list populated as "{}" *
* *
* Return values: *
* if succeeds, return 0 *
* if abnormal error, return 1 (not used so far) *
************************************************************************/
int
query_term_list (char *term_list)
{
int id;
int first_flag = 0;
char id_str[ID_CHAR_LENGTH] = "";
strncpy (term_list,"{",2);
for (int i=0;i < MAXTERMID; i++)
{
if (gl_terminal_info[i].flag == 1)
{
if (first_flag == 0)
{
snprintf(id_str,ID_CHAR_LENGTH, "%d", gl_terminal_info[i].id);
strncat(term_list, id_str, strlen(id_str));
first_flag = 1;
}
else
{
snprintf(id_str,ID_CHAR_LENGTH, "%d", gl_terminal_info[i].id);
strncat(term_list, " ", 1);
strncat(term_list, id_str, strlen(id_str));
}
}
}
strncat(term_list, "}", 1);
return 0;
}
/************************************************************************
* Store data in json format to data structure provided. *
* *
* Parameter examples: *
* p_msg: {"TransactionType":"Credit","cardType":"Visa"} *
* term_info_ptr->card_type: "Visa" *
* term_info_ptr->transaction_type: "Credit" *
* *
* Return values: *
* if succeeds, return 0 *
* if fails, return 1 *
************************************************************************/
int parse_json(char * p_msg, struct terminal_info_struct *term_info_ptr)
{
cJSON *p_json;
cJSON *p_sub;
if(NULL == p_msg)
{
ERRLOG ("empty json message.");
return 1;
}
p_json = cJSON_Parse(p_msg);
if(NULL == p_json)
{
ERRLOG ("failure in cJSON_Parse().");
return 1;
}
/* Extract cardType */
p_sub = cJSON_GetObjectItem(p_json, "cardType");
if( p_sub == NULL )
{
ERRLOG ("failure in cJSON_Parse().");
return 1;
}
snprintf (term_info_ptr->card_type, CARD_NAME_LENGTH , "%s", cJSON_Print(p_sub));
/* Extract TransactionType */
p_sub = cJSON_GetObjectItem(p_json, "TransactionType");
if( p_sub == NULL )
{
ERRLOG ("failure in cJSON_Parse().");
return 1;
}
snprintf (term_info_ptr->transaction_type, TRANS_NAME_LENGTH , "%s", cJSON_Print(p_sub));
cJSON_Delete(p_json);
#ifdef UTFLAG
if ( call_by_test_svr_process_req3 )
/* This is for unit testing */
return 1;
else
return 0;
#else
return 0;
#endif
}
/************************************************************************
* Convert data structure in DB to string in json format. *
* *
* Parameter examples: *
* tm_db_ptr point to data like {15,"Visa","Credit",1} *
* tm_json[] contains string like: *
* {"terminalID":15,"transactions": *
* {"cardType":"Visa","TransactionType":"Credit"}} *
* Return values: *
* if succeeds, return 0 *
* if abnormal error, return 1 (not used so far) *
************************************************************************/
int
struct2json(struct terminal_info_struct *tm_db_ptr, char tm_json[])
{
char id_str[ID_CHAR_LENGTH] = "";
strncpy (tm_json,"{\"terminalID\":",15);
snprintf(id_str,ID_CHAR_LENGTH, "%d",tm_db_ptr->id);
strncat(tm_json, id_str, strlen(id_str));
strncat(tm_json, ",\"transactions\":{\"cardType\":",28);
strncat(tm_json, tm_db_ptr->card_type, strlen(tm_db_ptr->card_type));
strncat(tm_json, ",\"TransactionType\":",19);
strncat(tm_json, tm_db_ptr->transaction_type, strlen(tm_db_ptr->transaction_type));
strncat(tm_json, "}}",2);
#ifdef UTFLAG
if ( call_by_test_query_term_info4 )
/* This is for unit testing */
return 1;
else
return 0;
#else
return 0;
#endif
}