forked from nceruchalu/easypay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
423 lines (379 loc) · 14 KB
/
data.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* -----------------------------------------------------------------------------
* ----- DATA.C -----
* ----- EASYPAY -----
* -----------------------------------------------------------------------------
*
* File Description:
* This is the library of functions for interfacing the PIC18F67K22 with the
* Http Server.
* Makes calls like:
* SimHttpGet("/test/", "p1=trust&p2=me", &http_response);
* SimHttpGet("/test/json/", NULL, &http_response);
* SimHttpPost("/test/", "p5=stay&p6=positive", &http_response);
*
* Table of Contents:
* (private)
* UidToString - convert int array UID to a char array
*
* (public)
* DataInit - initializes the data module and it's variables
* DataCardValidate - determine smartcard type server side
* DataPinValidate - validate pin on server side
* DataAcctBalance - get account balance (in kobos)
* DataAcctRecharge - recharge account with EasyTopup card
* DataParkDetails - get parking space & time if they exist
* DataParkPay - pay for/extend a parking space
* DataAlertPark - send notification Email for successful parking payment
*
* Assumptions:
* None
*
* Limitations:
* None
*
* Compiler:
* HI-TECH C Compiler for PIC18 MCUs (http://www.htsoft.com/)
*
* Revision History:
* May 14, 2013 Nnoduka Eruchalu Initial Revision
* Mar 30, 2014 Nnoduka Eruchalu Cleaned up comments
*/
#include "general.h"
#include <stdint.h>
#include "data.h"
#include "sim5218.h"
#include "mifare.h"
#include "eventproc.h"
/* shared variables have to be local to this file */
static http_data http_response; /* Http Response struct */
static sim_data module; /* SIM5218 module */
static const char *card_validate_url = "/card/validate/";
static const char *pin_validate_url = "/pin/validate/";
static const char *acct_balance_url = "/account/balance/";
static const char *acct_recharge_url = "/account/recharge/";
static const char *park_details_url = "/park/details/";
static const char *park_pay_url = "/park/pay/";
static const char *alert_park_url = "/alert/park/";
/* local functions that need not be public */
static void UidToString(uint8_t *uid, char *buffer);
/*
* UidToString
* Description: Save Mifare tag's UID in a string buffer array
*
* Arguments: uid: UID to convert to string
* buffer: array to save string in [must be at least 14 bytes long]
* Return: None
*
* Operation: Loop through UID bytes and save as hex string.
*
* Revision History:
* May 15, 2013 Nnoduka Eruchalu Initial Revision
* Mar 30, 2014 Nnoduka Eruchalu Cleaned up comments
*/
static void UidToString(uint8_t *uid, char *buffer)
{
char nibble, num;
size_t i;
for(i=0; i < 7;i++) {
num = uid[i];
/* extract high nibble and write out numeric or alpha */
nibble = (num & 0xF0) >> 4;
if (nibble < 10) buffer[2*i] = '0'+nibble;
else buffer[2*i] = nibble-10+'A';
/* extract low nibble and write out numeric or alpha */
nibble = (num & 0x0F);
if (nibble < 10) buffer[2*i+1] = '0'+nibble;
else buffer[2*i+1] = nibble-10+'A';
}
}
/*
* DataInit
* Description: This procedure initializes the shared variables, and clears the
* data transfer timer.
*
* Arguments: None
* Return: None
*
* Operation: Initialize the module representation.
*
* Assumptions: Called after setting up Serial channel 2 and interrupts are
* enabled
*
* Revision History:
* May 14, 2013 Nnoduka Eruchalu Initial Revision
* Mar 30, 2014 Nnoduka Eruchalu Cleaned up comments
*/
void DataInit(void)
{
SimStartTimer(0); /* reset SIMCOM module Timer */
SimDataInit(&module); /* initialize module object */
}
/*
* DataCardValidate
* Description: Determine smartcard type server side
*
* Arguments: tag: pointer to repr of MIFARE card to validate
* Return: smartcard type (SMARTCARD CODE) with following values:
* - CARD_TAP: EasyCard
* - CARD_TOPUP: EasyTopup
* - CARD_INVALID: Invalid Card
*
* Operation: Convert UID to a string.
* Do a HTTP POST with the string UID as a parameter. The smartcard
* type is returned from server
* The smartcard code is returned in the HTTP response's number
*
* Revision History:
* May 15, 2013 Nnoduka Eruchalu Initial Revision
* Mar 30, 2014 Nnoduka Eruchalu Cleaned up comments
*/
uint8_t DataCardValidate(mifare_tag *tag)
{
/* each uid hex num is 2 bytes [so 14], "uid=" is 4 bytes, & NULL-terminator
*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* NULL-terminator [1]
*/
char param_str[4+14+1]; /* allocate space for param */
strcpy(param_str, "uid="); /* load in UID key */
UidToString(tag->uid, ¶m_str[4]); /* load in UID string */
param_str[sizeof(param_str)-1] = '\0'; /* add NULL-terminator */
SimHttpPost(card_validate_url, param_str, &http_response);
return ((uint8_t) http_response.number);
}
/*
* DataPinValidate
* Description: Validate entered pin-code
*
* Arguments: uid: UID of EasyCard
* Return: TRUE: for valid pincode
* FALSE: for invalid pincode
*
* Operation: Convert UID to a string.
* Do a HTTP POST with the string UID and pin as a parameters.
* Note that a pin number of 0123 (base 10) is transmitted as
* pin=123
* The validity of the PIN will be in the HTTP response's boolean
*
* Assumption: Assumes complete pin number [all digits present]
*
* Revision History:
* May 15, 2013 Nnoduka Eruchalu Initial Revision
*/
uint8_t DataPinValidate(uint8_t *uid, uint16_t pin)
{
/*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* "&pin=" [5]
* pin number [NUM_PIN_DIGITS]
* NULL-terminator [1]
*/
char param_str[4+14+5+NUM_PIN_DIGITS+1]; /* allocate space for params */
strcpy(param_str, "uid="); /* load in UID key */
UidToString(uid, ¶m_str[4]); /* load in UID string */
strcpy(¶m_str[18], "&pin=");
sprintf(¶m_str[23], "%u", pin); /* automatically adds NULL-terminator */
param_str[sizeof(param_str)-1] = '\0'; /* add NULL-terminator: just because */
SimHttpPost(pin_validate_url, param_str, &http_response);
return http_response.boolean;
}
/*
* DataAcctBalance
* Description: Get account balance (in Kobos)
*
* Arguments: uid: UID of EasyCard
* Return: account balance (in kobos)
*
* Operation: Convert UID to a string.
* Do a HTTP GET with the string UID as a parameter.
* The balance will be in the HTTP response's number.
*
* Revision History:
* May 16, 2013 Nnoduka Eruchalu Initial Revision
*/
uint32_t DataAcctBalance(uint8_t *uid)
{
/*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* NULL-terminator [1]
*/
char param_str[4+14+1]; /* allocate space for params */
strcpy(param_str, "uid="); /* load in UID key */
UidToString(uid, ¶m_str[4]); /* load in UID string */
param_str[sizeof(param_str)-1] = '\0'; /* add NULL-terminator */
SimHttpGet(acct_balance_url, param_str, &http_response);
return http_response.number;
}
/*
* DataAcctRecharge
* Description: Recharge account with EasyTopup card
*
* Arguments: uid: UID of EasyCard
* topup_id: UID of EasyTopup card
* recharge_value: (ptr) to value of EasyTopup (in kobo) [modifed]
* Return: recharge success
*
* Operation: Do a HTTP POST with the UID and topup_id as parameters
* The value of the EasyTopup card will will be in the HTTP
* response's number.
* This will be saved in recharge_value
* The success of the recharge operation is recorded in the HTTP
* response's boolean. An unsuccessful recharge operation means the
* EasyTopup card has been used.
*
* Revision History:
* May 16, 2013 Nnoduka Eruchalu Initial Revision
*/
uint8_t DataAcctRecharge(uint8_t *uid, uint8_t *topup_id,
uint32_t *recharge_value)
{
/*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* "&tid=" [5]
* each topup id hex num is 1 bytes [14]
* NULL-terminator [1]
*/
char param_str[4+14+5+14+1]; /* allocate space for params */
strcpy(param_str, "uid="); /* load in UID key */
UidToString(uid, ¶m_str[4]); /* load in UID string */
strcpy(¶m_str[18], "&tid="); /* load in TopupID key */
UidToString(topup_id, ¶m_str[23]); /* load in TopupID string */
param_str[sizeof(param_str)-1] = '\0'; /* add NULL-terminator: just because */
SimHttpPost(acct_recharge_url, param_str, &http_response);
if (http_response.boolean) /* if recharge was successful, update */
*recharge_value = http_response.number; /* get value of EasyTopup card */
return http_response.boolean; /* return success of recharge */
}
/*
* DataParkDetails
* Description: Get parking details
*
* Arguments: uid: UID array for EasyCard
* space: (ptr) for storing space number [modified]
* time: (ptr) for storing time (in seconds) [modified]
*
* Return: TRUE: if user has currently running parking space
* FALSE: user doesn't currently have parking time left at a space
*
* Operation: Do a HTTP GET with the UID as a parameter
* The space number will be in the HTTP response's number
* The time left will be in the HTTP response's number2
* HTTP response's boolean will be the return value.
*
* Revision History:
* May 16, 2013 Nnoduka Eruchalu Initial Revision
*/
uint8_t DataParkDetails(uint8_t *uid, uint32_t *space, int32_t *time)
{
/*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* NULL-terminator [1]
*/
char param_str[4+14+1]; /* allocate space for params */
strcpy(param_str, "uid="); /* load in UID key */
UidToString(uid, ¶m_str[4]); /* load in UID string */
param_str[sizeof(param_str)-1] = '\0'; /* add NULL-terminator */
SimHttpGet(park_details_url, param_str, &http_response);
if(http_response.boolean) { /* if user has time left at a space */
*space = http_response.number; /* save those details */
*time = http_response.number2;
}
return http_response.boolean;
}
/*
* DataParkPay
* Description: Pay for/extend a parking space. Update the parking time if it is
* extended.
* If this is a time extension, time will be updated accordingly.
*
* Arguments: uid: UID array for EasyCard
* space: space number
* time: (ptr) for time (in seconds) [modified]
*
* Return: None
*
* Operation: Do a HTTP POST with the UID, space and time as parameters
* The possibly extended time left will be in HTTP response's
* number.
* If there is a time extension, it is recorded in HTTP response's
* boolean.
*
* Revision History:
* May 16, 2013 Nnoduka Eruchalu Initial Revision
*/
void DataParkPay(uint8_t *uid, uint32_t space, int32_t *time)
{
/*
* "uid=" [4]
* each uid hex num is 2 bytes [14],
* "&space=" [7]
* number of parking digits max out at NUM_PARK_SPACE_DIGITS
* "&time=" [6]
* number of time digits max out at NUM_PARK_TIME_DIGITS+2, with the +2 for
* converting from minutes to seconds
* NULL-terminator [1]
*/
/* allocate space for params */
char param_str[4+ 14 +7 +NUM_PARK_SPACE_DIGITS +6 +NUM_PARK_TIME_DIGITS+2 +1];
strcpy(param_str, "uid="); /* load in UID key, values */
UidToString(uid, ¶m_str[4]); /* load in UID string */
/*
* Load in key,value pairs utilizing the fact that strcpy and sprintf append
* NULLs and so strlen will be used to get the next index to start writing
* from. Start writing over last NULL character.
*/
strcpy(¶m_str[18], "&space="); /* load in space key and value */
sprintf(¶m_str[25], "%u", space);
strcpy(¶m_str[strlen(param_str)], "&time="); /* load time key and value */
sprintf(¶m_str[strlen(param_str)], "%u", *time);
SimHttpPost(park_pay_url, param_str, &http_response);
if (http_response.boolean) /* if time was extended, update it */
*time = http_response.number;
return;
}
/* ALERT ROUTINES */
/*
* DataAlertPark
* Description: Alert the user of successful parking payment for a given space
* at a given time.
*
* Arguments: space: space number
* time: time (in seconds)
* Return: None
*
* Operation: Do a HTTP POST with the space and time as parameters
*
* Revision History:
* May 16, 2013 Nnoduka Eruchalu Initial Revision
* Mar 30, 2014 Nnoduka Eruchalu Cleaned up comments
*/
void DataAlertPark(uint32_t space, int32_t time)
{
/*
* "&s=" [3]
* number of parking digits max out at NUM_PARK_SPACE_DIGITS
* "&t=" [3]
* number of time digits max out at NUM_PARK_TIME_DIGITS+2, with the +2 for
* converting from minutes to seconds
* NULL-terminator [1]
*/
/* allocate space for params */
char param_str[3 +NUM_PARK_SPACE_DIGITS +3 +NUM_PARK_TIME_DIGITS+2 +1];
/*
* Load in key,value pairs utilizing the fact that strcpy and sprintf append
* NULLs and so strlen will be used to get the next index to start writing
* from. Start writing over last NULL character.
*/
strcpy(param_str, "&s="); /* load in space key and value */
sprintf(¶m_str[strlen(param_str)], "%u", space);
strcpy(¶m_str[strlen(param_str)], "&t="); /* load time key and value */
sprintf(¶m_str[strlen(param_str)], "%u", time);
SimHttpPost(alert_park_url, param_str, &http_response);
return;
}