-
Notifications
You must be signed in to change notification settings - Fork 34
/
emv.cpp~
1006 lines (879 loc) · 22.5 KB
/
emv.cpp~
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "emv.h"
#define TRUE 1
#define MSG int
/*************************** GLOBALS *****************************/
byte GLOBAL_AID[7];
int GLOBAL_AID_LEN;
int OpenServiceCntrl (ServiceControl *sc);
int OpenServices();
void outputDataObject(int tag, const byte *value, int value_sz);
void releasePOS (POSControlVSDCImpl **ppPOSControl);
char *createDataStr(const char *data_name, int Tag);
void outputDataItems ();
//byte main_aid[] = {0x01}; //, {0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10};
byte main_aid[] = {0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10};
AccessManager AM(main_aid, 7);
UIControlImpl UI;
SCRControlImpl SCR;
//const byte zero_amount [6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ApplSelControlImpl Selector;
POSControlVSDCImpl *POSControl;
HANDLE hThreadExit;
unsigned int threadExitID;
HANDLE hThreadSelect;
unsigned int threadSelectID;
bool retreiveByteData(int Tag, byte **data, int *data_len, char format[]);
void PrintReceipt(long TermDes);
unsigned ThreadFunc_Exit( void* pArguments );
unsigned ThreadFunc_SelectTrans( void* pArguments );
int MessageLoop (HANDLE* lphObjects, int cObjects);
int getNumberOfCents (byte curCode[]);
byte Amount[6];
byte AmountOther[6];
byte CurCode [2];
byte TransType;
byte TransInfo;
bool IsTransInitialized;
bool IsAmountInitialized;
bool IsAmountOtherInitialized;
// Event handlers for the reader control
void onInsert();
void onRemove();
bool TransInProgress;
long TransactionToken;
HANDLE hEventOnInsertExit;
HANDLE hRemove;
int main()
{
long TransactionToken;
int res;
printf ("Starting the VipPOS Terminal Application...\n");
IsTransInitialized = false;
IsAmountInitialized = false;
IsAmountOtherInitialized = false;
memset(Amount, 0, 6);
memset(AmountOther, 0, 6);
CryptoControlImpl crypto;
if ((res = OpenServiceCntrl(&crypto)) != SUCCESS)
return res;
int KeysVerified;
if ((res = crypto.checkCAIntegrity (&KeysVerified)) != SUCCESS)
{
printf ("Certification Authority Public Key verification failed (%x, %d)\n",
res, KeysVerified);
return res;
}
else
{
printf ("Public keys integrity is successfully verified (%d)\n",
KeysVerified);
}
if ((res = OpenServices()) != SUCCESS)
return res;
Selector.AM = &AM;
if ((res = Selector.Initialize ()) != SUCCESS)
return res;
//hEventOnInsertExit = CreateEvent(NULL, TRUE, FALSE, "ON_INSERT_EXIT");
//hRemove = CreateEvent(NULL, TRUE, FALSE, "ON_REMOVE");
// Build a list of terminal supported applications (read from the registry)
ApplSelOperationEventImpl selOpEvent;
Selector.addOperationEvent (&selOpEvent);
res = Selector.BuildTerminalApplList ();
if (res != SUCCESS)
{
printf ("Failed building the Terminal Application list.\n");
printf (" Error: %x\n", selOpEvent.getError());
return selOpEvent.getError();
}
Selector.removeEvent ();
printf ("Terminal Application list is built successfully.\n");
TransInProgress = false;
SCR.RegisterEvent (ONINSERT, onInsert);
SCR.RegisterEvent (ONREMOVE, onRemove);
res = SCR.EstablishConnection();
if (res != SUCCESS)
{
printf ("Failed to establish connection with the card reader (%x)\n",
res);
Selector.CancelTransaction();
AM.close (UI);
AM.close (SCR);
return res;
}
/*
hThreadExit = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc_Exit,
0, 0, &threadExitID );
// Thread prompting a user to select transaction and/or enter Amount
hThreadSelect = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc_SelectTrans,
0, 0, &threadSelectID );
*/
printf ("Terminal is ready...\n");
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!HANDLE hArr[] = {hThreadExit, hThreadSelect, hEventOnInsertExit};
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!res = MessageLoop(hArr, 3);
/*
* MAIN LOOP
*/
while(1){
if(SCR.isConnected() == true){
printf("connected!!!!!!!!!!!!!!!!!!!!!!\n");
onInsert();
}
}
Selector.CancelTransaction();
AM.close (UI);
SCR.DestroyConnection ();
AM.close (SCR);
releasePOS(&POSControl);
//CloseHandle (hThreadExit);
//CloseHandle (hEventOnInsertExit);
//CloseHandle (hRemove);
printf("Terminal Application Finished.\n");
return 0;
}
// This is event handler for the 'OnCardInsert' event
void onInsert ()
{
if (TransInProgress) // Only one transaction allowed at a time
{
return;
}
TransInProgress = true;
//ResetEvent(hEventOnInsertExit);
releasePOS (&POSControl);
// Cancel Wait operation, so ThreadFunc_SelectTrans thread will terminate
UI.cancelWait ();
usleep(100);
Prompter prompt (&AM);
tlv_parser tlv_AIP;
TransactionToken = 0;
int res;
char str[80];
if (!IsTransInitialized)
{ // Transaction is not initialized -- prompt a user to select a transaction type
res = prompt.promptTransactionType(&TransType, &TransInfo, EMV_LANG_LATIN1);
if (res == SUCCESS)
{
IsTransInitialized = true;
}
else
{
// Exit transaction if transaction type cannot be retreived
UI.writeStatus ("(OnInsert) Failed getting a Transaction Code");
UI.setPrompt("(OnInsert) Remove the card");
TransInProgress = false;
//SetEvent(hEventOnInsertExit); // Notify that the OnInsert function is done
return;
}
}
// Start a new transaction
TransactionToken = 0;
res = SCR.BeginTransaction (TransactionToken);
if (res != SUCCESS && res != SCR_CARD_NOT_PRESENT)
{
UI.writeStatus ("(OnOnsert) Failed starting a new transaction");
UI.setPrompt("(OnInsert) Remove the card");
TransInProgress = false;
//SetEvent(hEventOnInsertExit); // Notify that the OnInsert function is done
return;
}
tlv_parser tlv_Appl;
ApplSelOperationEventImpl selOpEvent;
// Add Operation Event to Selector
if ((res = Selector.addOperationEvent (&selOpEvent)) != SUCCESS)
{
UI.writeStatus ("Failed to add Operation Event to the Application Select control");
TransInProgress = false;
SCR.EndTransaction (TransactionToken);
//SetEvent(hEventOnInsertExit); // Notify that the OnInsert function is done
return;
}
UI.setPrompt("Processing...");
// SELECT APPLICATION
res = Selector.EMV_ApplicationSelection(TransactionToken);
Selector.removeEvent ();
if (res != SUCCESS)
{
sprintf (str, "Select Application Error: %#X", selOpEvent.getError());
UI.writeStatus (str);
}
POSOperationEventImpl posOpEvent;
bool bApplicationSelected = false;
TransactionToken = selOpEvent.getTransactionToken();
while (res == SUCCESS)
{
int sel_data_len = selOpEvent.getLength();
if (sel_data_len <= 0)
{
UI.writeStatus ("(MAIN) Unexpected data (Application Selection)");
res = -1;
break;
}
byte *sel_data = 0;
selOpEvent.getByteString(&sel_data);
if ((res = tlv_Appl.parse (sel_data, sel_data_len)) != SUCCESS)
{
UI.writeStatus ("Failed to parse data (Application Selection)");
break;
}
// Start EMV transaction
// -----------------------------------
// Initiate Application Processing
//------------------------------------
// Extract Application Identifier (wich is stored in
// DF Name of FCI of a selected ADF (tag '84')
tlv_parser *parser_aid = tlv_Appl.Find (0x84);
if (!parser_aid)
{
UI.writeStatus ("(MAIN) Unexpected data (Cannot find DF Name)");
res = -1;
break;
}
// Create a POS module
//!!!!!!!!!!!!!!!!
POSControl = new POSControlVSDCImpl(parser_aid->GetRoot ()->GetValue (),
parser_aid->GetRoot ()->GetLengthVal ());
if (!POSControl)
{
UI.writeStatus ("(MAIN) Failed to create POS Control");
res = ERR_MEMORY_ALLOC;
break;
}
//POSControl->AM = new AccessManager(parser_aid->GetRoot ()->GetValue (),
// parser_aid->GetRoot ()->GetLengthVal ());
// Load appropriate POSControl
// AM.open((POSInterface*)POSControl,POSControl->aid,POSControl->aid_len);
// AM.open(&POSControl);
//AM.open((POSInterface*)POSControl);
/*
if ((res = AM.open((POSInterface*)POSControl,POSControl->aid,POSControl->aid_len)) != SUCCESS)
{
UI.writeStatus ("(MAIN) Failed to open POSControl");
delete POSControl;
POSControl = 0;
// Continue With the next application in the candidate list
//Attempting to select the next application in a list
selOpEvent.resetEvent(true);
Selector.addOperationEvent (&selOpEvent);
res = Selector.EMV_SelectNext();
Selector.removeEvent ();
if (res != SUCCESS)
{
sprintf (str, "Select Application Error: %#X", selOpEvent.getError());
UI.writeStatus (str);
break;
}
continue;
}
*/
TRANS_STARTUP_PARAMS params;
memset (¶ms, 0, sizeof(TRANS_STARTUP_PARAMS));
params.TransactionType = TransType;
params.TransactionInfo = TransInfo;
params.TransactionToken = TransactionToken;
if (IsAmountInitialized)
{
memcpy (params.AmountAuthorized, Amount, 6);
memcpy (params.CurrencyCode, CurCode, 2);
}
else
{
memset (params.AmountAuthorized, 0, 6);
memset (params.CurrencyCode, 0, 2);
}
if (IsAmountOtherInitialized)
memcpy (params.AmountOther, AmountOther, 6);
else
memset (params.AmountOther, 0, 6);
UI.setPrompt ("Processing...");
if ((res = POSControl->initPOS(¶ms)) != SUCCESS)
{
UI.writeStatus ("(MAIN) Failed to initialize POS");
AM.close(*POSControl);
delete POSControl;
POSControl = 0;
break;
}
// Add operation Event to POSControl
posOpEvent.resetEvent(true);
POSControl->addOperationEvent (&posOpEvent);
// Initiate Application Processing
res = POSControl->execPOS (tlv_Appl.GetRoot ()->GetDataObject (),
tlv_Appl.GetRoot ()->GetDataObjectLen ());
POSControl->removeEvent ();
if (res == SUCCESS)
{
break;
}
else
{
UI.writeStatus ("(MAIN) Application Selection FAILED");
if (posOpEvent.getError() == ERR_APPL_INIT_CONDITIONS_NOT_SATISFIED)
{
releasePOS(&POSControl);
// Continue With the next application in the candidate list
//Attempting to select the next application in a list
selOpEvent.resetEvent(true);
Selector.addOperationEvent (&selOpEvent);
res = Selector.EMV_SelectNext();
Selector.removeEvent ();
if (res != SUCCESS)
{
UI.writeStatus ("Failed to initialize Application");
UI.writeStatus ("No Application is selected");
break;
}
}
else
{
break;
}
}
} // End Of While Loop
if (res == SUCCESS && POSControl)
{
long TermDes;
posOpEvent.getLong(&TermDes);
PrintReceipt(TermDes);
outputDataItems();
//MessageBox (NULL, "After outputDataItems", "MAIN", MB_OK);
UI.writeStatus ("", false);
}
else
{
if (POSControl)
{
char bf[100];
sprintf (bf, "Error while executing POS. res = %#X, error = %#X, operation = %#X",
res, posOpEvent.getError(), posOpEvent.getOperation());
UI.writeStatus (bf);
}
}
// End Transaction
UI.setPrompt ("Remove Card");
SCR.EndTransaction(TransactionToken);
releasePOS (&POSControl);
TransInProgress = false;
//SetEvent(hEventOnInsertExit); // Notify that the OnInsert function is done
}
void onRemove ()
{
Selector.CancelTransaction();
SCR.EndTransaction (TransactionToken);
sleep(100);
SCR.DestroyConnection ();
UI.cancelWait();
UI.writeStatus ("", false);
//UI.resetOption (OPTIONLIST_TRANSACTIONS);
UI.resetAmount(PURCHASE_AMOUNT);
UI.resetAmount(CASHBACK_AMOUNT);
printf("OnRemove is done\n");
}
int OpenServices()
{
int res;
// Open UI service and start the application window
UI.setServiceName("UIControl");
if ((res = OpenServiceCntrl(&UI)) != SUCCESS)
return res;
// Open SCR service
SCR.setServiceName("SCRControl");
if ((res = OpenServiceCntrl(&SCR)) != SUCCESS)
return res;
// Open Application Selection Control
Selector.setServiceName("Selector");
if ((res = OpenServiceCntrl((ServiceControl*)&Selector)) != SUCCESS)
return res;
return SUCCESS;
}
int OpenServiceCntrl (ServiceControl *sc)
{
int res;
// Open UI service and start the application window
printf("Opening the Service: %s\n", sc->getServiceName());
if (sc->getInterfaceType () == INTERFACE_TYPE_POS)
res = AM.open((POSInterface*)sc);
else
res = AM.open(sc);
if (res != SUCCESS)
{
printf ("FAIED to open, ERROR: %x\n", res);
return res;
}
else
{
printf(" Opened successfully!\n");
}
return SUCCESS;
}
void outputDataObject(int tag, const byte *value, int value_sz)
{
char *buff;
char str [80];
sprintf (str, "Data Object Tag [%X]:", tag);
UI.writeStatus (str);
if (!value || value_sz <= 0)
{
UI.writeStatus ("Data is not found");
}
else
{
buff = DumpByteArr(value, value_sz );
UI.writeStatus (buff);
delete [] buff;
}
return;
}
unsigned ThreadFunc_Exit( void* pArguments )
{
// Wait for Cancel button to exit an application
int btn = 0;
int EventID;
int res;
int waitingEvent;
UI.setLanguage (EMV_LANG_LATIN1);
while (true)
{
//UI.setPrompt ("To exit the application close the window");
waitingEvent = BTN_EXIT;
res = UI.waitForEvent (&waitingEvent, 1, &EventID);
if (res == SUCCESS)
{
// Confirm exit
//btn = MessageBox (NULL, "Are you sure you want to leave an application?",
// "Conformation", MB_YESNO);
//if (btn == IDYES)
//{
// UI.cancelWait();
// Sleep(100);
// break;
//}
//else
// continue;
}
else
{
if (res == OPERATION_CANCELED_BY_USER &&
EventID == CANCEL_WAIT)
continue;
else
break;
}
}
//if (TransInProgress == false)
// SetEvent(hEventOnInsertExit);
printf ("***** Exiting ThreadFunc_Exit function (%x) ****\n", res);
//_endthreadex(0);
return 0;
}
unsigned ThreadFunc_SelectTrans( void* pArguments )
{
Prompter prompt (&AM);
IsTransInitialized = false;
IsAmountInitialized = false;
int res;
memset(Amount, 0, 6);
memset(CurCode, 0, 2);
memset(AmountOther, 0, 6);
res = prompt.promptTransactionType(&TransType, &TransInfo, EMV_LANG_LATIN1);
if (res == SUCCESS)
{
IsTransInitialized = true;
if (check_bit(TransInfo, 0x01)) // Amount required
{
res = prompt.promptAmount(true, Amount, CurCode, EMV_LANG_LATIN1);
if (res == SUCCESS)
{
if (memcmp(Amount, zero_amount, 6) != 0)
{
IsAmountInitialized = true;
if (check_bit(TransInfo, 0x02)) // Cashback -- AmountOther required
{
res = prompt.promptAmount (false, AmountOther, NULL, EMV_LANG_LATIN1);
if (res == SUCCESS)
{
IsAmountOtherInitialized = true;
}
}
}
}
}
}
if (res == SUCCESS && !TransInProgress)
UI.setPrompt ("Please insert the card");
printf ("***** Exiting ThreadFunc_SelectTrans function (%x) ****\n", res);
//_endthreadex(0);
return 0;
}
void releasePOS (POSControlVSDCImpl **ppPOSControl)
{
if (*ppPOSControl)
{
if ((*ppPOSControl)->opened ())
{
AM.close(**ppPOSControl);
delete *ppPOSControl;
*ppPOSControl = 0;
}
}
}
void outputDataItems ()
{
int res, btn, iTag;
char *cTag;
int initSize = 10;
byte *data_value = new byte [initSize];
UIOperationEventImpl opEvent;
UI.addOperationEvent (&opEvent);
res = UI.receiveString ("Enter a tag (decimal) of the Data Object to see the value", INFINITE);
while (res == SUCCESS)
{
opEvent.getButton (&btn);
if (btn != BTN_ENTER)
{
break;
}
else
{
opEvent.getString(&cTag);
iTag = atoi(cTag);
char *str_val = 0;
str_val = createDataStr("Data Object:", iTag);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
str_val = 0;
}
opEvent.resetEvent (true);
res = UI.receiveString ("Enter a tag (decimal) of the Data Object to see the value", INFINITE);
continue;
}
}
if (data_value)
delete [] data_value;
UI.removeEvent ();
}
void PrintReceipt(long TermDes)
{
if (!UI.opened())
return;
byte trans_results [1];
int size = 1;
char format[4];
if (!POSControl)
{
UI.writeStatus ("POS not initialized");
return;
}
if( !POSControl->opened ())
{
UI.writeStatus ("POS Control not opened");
return;
}
int res = POSControl->getTransData(0x50000004, trans_results, &size,
format, true);
if (res != SUCCESS || size != 1)
{
UI.writeStatus ("Cannot retreive transaction results");
return;
}
char *str_val;
UI.writeStatus ("");
UI.writeStatus ("---- Transaction Receipt ----");
str_val = createDataStr("Application:", 0x9f12);
if (strcmp(str_val, "Application: (0x9F12):") == 0)
{
delete [] str_val;
str_val = createDataStr("Application:", 0x50);
}
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
if (TermDes == TERMINAL_APPROVE)
UI.writeStatus (Language::getString (MSG_ID__APPROVED, DEFAULT_LANG));
else
UI.writeStatus(Language::getString (MSG_ID__DECLINED, DEFAULT_LANG));
str_val = createDataStr("Transaction Date", 0x9a);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("Transaction Time", 0x9f21);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("Transaction Amount", 0x9f02);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
byte data_value[6];
char frmt[4];
int data_size = 6;
if (POSControl->getTransData(0x9f03, data_value, &data_size, frmt, true) == SUCCESS)
{
str_val = createDataStr("Cashback Amount", 0x9f03);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
}
str_val = createDataStr("Application Identifier", 0x4f);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("Primary Account Number", 0x5a);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("Authorization Response Code", 0x8a);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("TVR", 0x95);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("TSI", 0x9b);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
if (check_bit(trans_results[0], 0x02))
{
UI.writeStatus ("Advice is required");
if (((trans_results[0]>>2) & 0x07) == 0x01)
UI.writeStatus ("Advice reason: Service not allowed");
else if (((trans_results[0]>>2) & 0x07) == 0x02)
UI.writeStatus ("Advice reason: PIN Try Limit exceeded");
else if (((trans_results[0]>>2) & 0x07) == 0x03)
UI.writeStatus ("Advice reason: Issuer authentication Failed");
}
if (check_bit(trans_results[0], 0x20))
{
str_val = createDataStr("Signature is required for", 0x5f20);
UI.writeStatus (str_val);
delete [] str_val;
}
str_val = createDataStr("Issuer Script Result:", 0x9F5B);
if (str_val)
{
UI.writeStatus (str_val);
delete [] str_val;
}
UI.writeStatus ("------------------------------------");
}
char *createDataStr(const char *data_name, int Tag)
{
byte *buff;
int buff_len;
char *full_str;
char data_format[4];
if (retreiveByteData (Tag, &buff, &buff_len, data_format))
{
if (buff && buff_len > 0)
{
char *str_val = 0;
bool am_failed = true;
if (Tag == 0x9f02 || Tag == 0x9f03)
{
// Convert Amount numeric to character string
str_val = new char [14];
char dlrAmount[13];
char ctsAmount[13];
byte curCode[2];
int res = getNumberOfCents (curCode);
if (res != SUCCESS)
{
am_failed = true;
}
else
{
res = numeric2asciAmount(dlrAmount, ctsAmount,
(curCode[0] >> 4), buff);
if (res == SUCCESS)
{
am_failed = false;
sprintf (str_val, "%s.%s", dlrAmount, ctsAmount);
}
else
am_failed = true;
}
}
if (am_failed)
{
if (strcmp(data_format, "an") == 0 ||
strcmp(data_format, "ans") == 0)
{
str_val = new char [buff_len + 1];
if (str_val)
{
for (int i = 0; i < buff_len; i++)
str_val[i] = (char)(buff[i]);
str_val[buff_len] = '\0';
}
}
else if (data_format[0] == 'd')
{
long lVal = LongFromByte(buff, buff_len);
str_val = new char [32];
if (str_val)
sprintf(str_val, "%d", lVal);
}
else
str_val = DumpByteArr (buff, buff_len);
}
if (str_val)
{
full_str = new char [strlen(str_val) + strlen(data_name) + 14];
sprintf (full_str, "%s (%X): %s", data_name, Tag, str_val);
delete [] str_val;
return full_str;
}
delete [] buff;
}
}
full_str = new char [strlen(data_name) + 13];
sprintf (full_str, "%s (%X):", data_name, Tag);
return full_str;
}
bool retreiveByteData(int Tag, byte **data, int *data_len, char format[])
{
*data_len = 0;
*data = 0;
int init_len = 10;
byte *data_val = new byte [init_len];
if (!POSControl && !POSControl->opened ())
{
return false;
}
int res = POSControl->getTransData(Tag, data_val, &init_len, format, false);
if (res == ERR_BUFFER_OVERFLOW)
{
delete [] data_val;
data_val = new byte [init_len];
res = POSControl->getTransData(Tag, data_val, &init_len, format, false);
}
if (res == SUCCESS)
{
*data_len = init_len;
*data = data_val;
return true;
}
else
{
delete [] data_val;
return false;
}
}
int getNumberOfCents (byte curCode[])
{
byte *data;
int data_len;
char format [5];
int res;
CnfgControlImpl Cnfg;
CnfgOperationEventImpl opEventKey;
CnfgOperationEventImpl opEventVal;
// Get Transaction Currency from the Context
if (retreiveByteData(0x5f2a, &data, &data_len, format))
{
if (data_len != 2)
{
delete [] data;
return -1;
}
if ((res = AM.open(&Cnfg)) != SUCCESS)
{
delete [] data;
return res;
}
Cnfg.addOperationEvent(&opEventKey);
res = Cnfg.enumKeys (CNFG_TERMINAL, "CurrencyCodes");
Cnfg.removeEvent();
if (res != SUCCESS)
{
delete [] data;
return opEventKey.getError ();
}
int numTypes;
if ((numTypes = opEventKey.getLength ()) <= 0)
{
delete [] data;
return -1;
}
char **trCode;
if ((res = opEventKey.getStringArray (&trCode)) != SUCCESS)
{
delete [] data;
return opEventKey.getError ();
}
Cnfg.addOperationEvent (&opEventVal);
res = SUCCESS;
int i = 0;
for (int k = 0; k < numTypes; k++)
{
opEventVal.resetEvent (true);
// Build a sub key for current currency
char *subKey = new char [15 + strlen (trCode[k])];
if (!subKey)
{
// Memory allocation error
delete [] data;
return ERR_MEMORY_ALLOC;
}
strcpy (subKey, "CurrencyCodes");
strcat(subKey, "\\");
strcat (subKey, trCode[k]);
res = Cnfg.getValue (CNFG_TERMINAL, "Data", subKey);
delete [] subKey;
if (res != SUCCESS)
{
delete [] data;
return res;
}
byte *curData;
opEventVal.getByteString (&curData);
int data_sz = opEventVal.getLength ();
if (data_sz != data_len)
{
delete []data;
return res;
}
if ((curData[0] & 0x0f) == data[0] &&
curData[1] == data[1])
{
// Found match
curCode[0] = curData[0];
curCode[1] = curData[1];
delete [] data;
return SUCCESS;
}
}
}
delete [] data;
return -1;
}
int MessageLoop (