Project:
Ingenico JavaPOS driver
Code Location:
http://ingenico-jpos-driver.googlecode.com/svn/trunk/src/com/blitz/jpos/trunk/src/com/blitz/jpos
Outline
-
>
C
IngenicoSerialThread
- F busy
- F errorMessages
- M getBusy()
- C SpecialCar
- F pistes
- F inputStream
- F readThread
- F serialPort
- F messageString
- F outputStream
- F outputBufferEmptyFlag
- M initwritetoport()
- M writetoport()
- M writeEnq()
- M IngenicoSerialThread()
- M IngenicoSerialThread(String)
-
E
E210_STATE
- F Repos
- F RcRecuENQ
- F RcAttenteSTX
- F EmEmetENQ
- F EmAttenteACKaENQ
- F EmData
- F EmAttenteAckData
- F MASTER_SEND_COMMAND
- F SLAVE_IDE
- F SLAVE_RECEIVE
- F RcAttenteLRC
- F RcAttenteEOT
- F RcReceptionData
- F Delai
- F EmErreurData
- F state
- F m_fonctionEL210
- F timeOut
- F m_strData
- M run()
- M SendMessage(RXTXPort , byte [ ])
- M EnvoiChar(RXTXPort , byte)
- M RecoiChar(RXTXPort)
- M abort()
- F readBuffer
- M setFunction(byte [ ])
- M getState()
IngenicoSerialThread.java
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
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
// // This file is part of Blitz B.S. JavaPOS library. // // Blitz B.S. JavaPOS library is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Blitz B.S. JavaPOS library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Blitz B.S. JavaPOS library. If not, see <http://www.gnu.org/licenses/>. // package com.blitz.jpos; //derived from SUN's examples in the javax.comm package import gnu.io.PortInUseException; import gnu.io.RXTXPort; import gnu.io.SerialPort; import gnu.io.UnsupportedCommOperationException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import jpos.JposConst; import jpos.JposException; public class IngenicoSerialThread implements Runnable//, EventCallbacks// , SerialPortEventListener { private boolean busy; public ArrayList<byte[]> errorMessages = new ArrayList<byte[]>(); boolean getBusy() { return this.busy; } class SpecialCar { public static final byte STX = 2; public static final byte ETX = 3; public static final byte EOT = 4; public static final byte ENQ = 5; public static final byte ACK = 6; public static final byte NAK = 0x15; } public ArrayList<byte[]> pistes = new ArrayList<byte[]>(); /* * public static CommPortIdentifier portId; public static CommPortIdentifier * saveportId; static Enumeration portList; */ InputStream inputStream; /* * SerialPort serialPort; */ Thread readThread; RXTXPort serialPort; static String messageString = "Hello, world!"; static OutputStream outputStream; static boolean outputBufferEmptyFlag = false; /* * public static void main(String[] args) { boolean portFound = false; * String defaultPort; * * // determine the name of the serial port on several operating systems * String osname = System.getProperty("os.name","").toLowerCase(); if ( * osname.startsWith("windows") ) { // windows defaultPort = "COM1"; } else * if (osname.startsWith("linux")) { // linux defaultPort = "/dev/ttyS0"; } * else if ( osname.startsWith("mac") ) { // mac defaultPort = "????"; } * else { * System.out.println("Sorry, your operating system is not supported"); * return; } * * if (args.length > 0) { defaultPort = args[0]; } * * System.out.println("Set default port to "+defaultPort); * * // parse ports and if the default port is found, initialized the reader * portList = CommPortIdentifier.getPortIdentifiers(); while * (portList.hasMoreElements()) { portId = (CommPortIdentifier) * portList.nextElement(); if (portId.getPortType() == * CommPortIdentifier.PORT_SERIAL) { if * (portId.getName().equals(defaultPort)) { * System.out.println("Found port: "+defaultPort); portFound = true; // init * reader thread E210SerialThread reader = new E210SerialThread(); } } * * } if (!portFound) { System.out.println("port " + defaultPort + * " not found."); } * * } */ public void initwritetoport() { // initwritetoport() assumes that the port has already been opened and // initialized by "public nulltest()" // try { // get the outputstream outputStream = serialPort.getOutputStream(); // } catch (IOException e) {} try { // activate the OUTPUT_BUFFER_EMPTY notifier serialPort.notifyOnOutputEmpty(true); } catch (Exception e) { System.out.println("Error setting event notification"); System.out.println(e.toString()); System.exit(-1); } } public void writetoport() { System.out.println("Writing \"" + messageString + "\" to " + serialPort.getName()); try { // write string to serial port outputStream.write(messageString.getBytes()); } catch (IOException e) { } } public void writeEnq() { // Write ENQ to serial port try { outputStream.write(5); } catch (IOException e) { } } private IngenicoSerialThread() { } public IngenicoSerialThread(String commName) throws PortInUseException, UnsupportedCommOperationException { this(); // portId = CommPortIdentifier.getPortIdentifier("COM3"); // initalize serial port //try { serialPort = new RXTXPort(commName);// (SerialPort) // portId.open("SimpleReadApp", // 2000); //} catch (PortInUseException e) { //} // try { inputStream = serialPort.getInputStream(); // } catch (IOException e) {} // try { // serialPort.addEventListener(this); // } catch (TooManyListenersException e) {} // activate the DATA_AVAILABLE notifier serialPort.notifyOnDataAvailable(true); // set port parameters serialPort.setSerialPortParams(1200, SerialPort.DATABITS_7, SerialPort.STOPBITS_2, SerialPort.PARITY_EVEN); this.busy = true; // start the read thread readThread = new Thread(this); readThread.start(); } public enum E210_STATE { Repos, RcRecuENQ, RcAttenteSTX, EmEmetENQ, EmAttenteACKaENQ, EmData, EmAttenteAckData, MASTER_SEND_COMMAND, SLAVE_IDE, SLAVE_RECEIVE, RcAttenteLRC, RcAttenteEOT, RcReceptionData, Delai, EmErreurData } private E210_STATE state = E210_STATE.Repos; private byte[] m_fonctionEL210 = new byte[0]; private int timeOut; private byte[] m_strData = new byte[0]; //private ArrayList<byte[]> m_trame = new ArrayList<byte[]>(); //private String m_strCMC7 = ""; //private int m_Erreurs = 0; public void run() { // first thing in the thread, we initialize the write operation initwritetoport(); try { /* * while (true) { * * * // write string to port, the serialEvent will read it * //writetoport(); writeEnq(); Thread.sleep(1000); * * } */ int LRC_Calcul = 0; int busyCount = 0; while (true) { //System.out.println("busy=" + busy); // Attente de 1s (1000 ms) // Thread.Sleep(1000); //if (state != E210_STATE.Repos) //System.out.println("etape=" + state.toString()); //if (m_strData.length >= 48 && m_strData[0] == 51)// 48 //{//System.out.println("new_data size=" + Integer.toString(m_strData.length)); //m_strCMC7 = "C" + (new String(m_strData)).substring(14, 26); //this.pistes.clear(); //System.out.println("etape=" + state.toString()); //this.pistes.add(m_strData.clone()); //} /*System.out.println("new_data"); if (m_strData.length >= 40 && m_strData[0] == 0x51)//48 { m_strCMC7 = "C" + (new String(m_strData)).substring(14, 34); this.pistes.add(m_strCMC7.getBytes()); }*/ switch (state) { // Repos case Repos: if (this.inputStream.available() > 0) { busyCount = 0; busy = true; if (RecoiChar(serialPort) == SpecialCar.ENQ) { state = E210_STATE.RcRecuENQ; } } else { if (m_fonctionEL210.length != 0) { busyCount = 0; busy = true; state = E210_STATE.EmEmetENQ; } else { Thread.sleep(100); busyCount++; if (busyCount > 6) { this.busy = false; } } } break; // Emits ENQ case EmEmetENQ: EnvoiChar(serialPort, SpecialCar.ENQ); timeOut = 0; state = E210_STATE.EmAttenteACKaENQ; break; case EmAttenteACKaENQ: if (serialPort.getInputStream().available() > 0) { timeOut = 0; byte car = RecoiChar(serialPort); if (car == SpecialCar.ACK) { state = E210_STATE.EmData; } else { if (car == SpecialCar.ENQ) { state = E210_STATE.RcRecuENQ; } else state = E210_STATE.Repos; } } else { if (timeOut > 10) { state = E210_STATE.Repos; } else { Thread.sleep(100); timeOut++; } } break; case EmData: // Erase error messages this.errorMessages.clear(); byte[] tab = m_fonctionEL210.clone(); SendMessage(serialPort, tab); state = E210_STATE.EmAttenteAckData; timeOut = 0; break; case EmAttenteAckData: // Si on a recus quelque chose if (serialPort.getInputStream().available() > 0) { // Si on nous repond positivement (ACK) if (RecoiChar(serialPort) == SpecialCar.ACK) { // On envoi fin de transmition EnvoiChar(serialPort, SpecialCar.EOT); m_fonctionEL210 = new byte[0]; state = E210_STATE.Repos; } else { // Pas bon //state = E210_STATE.EmErreurData; m_fonctionEL210 = new byte[0]; state = E210_STATE.Repos; } timeOut = 0; } else { if (timeOut == 4) { state = E210_STATE.Repos; } else { Thread.sleep(100); timeOut++; } } break; // Recois ENQ case RcRecuENQ: EnvoiChar(serialPort, SpecialCar.ACK); state = E210_STATE.RcAttenteSTX; m_strData = new byte[0]; timeOut = 0; break; // Attente STX case RcAttenteSTX: if (serialPort.getInputStream().available() > 0) { if (RecoiChar(serialPort) == SpecialCar.STX) { state = E210_STATE.RcReceptionData; m_strData = new byte[0]; } } else { if (timeOut == 4) { state = E210_STATE.Repos; } else { Thread.sleep(100); timeOut++; } } break; case RcReceptionData: if (serialPort.getInputStream().available() > 0) { byte car = RecoiChar(serialPort); if (car == SpecialCar.ETX) { LRC_Calcul = LRC_Calcul ^ SpecialCar.ETX; state = E210_STATE.RcAttenteLRC; timeOut = 0; } else { LRC_Calcul = LRC_Calcul ^ car; byte[] new_data = new byte[m_strData.length + 1]; for (int i = 0; i < m_strData.length; i++) { new_data[i] = m_strData[i]; } new_data[new_data.length - 1] = car; m_strData = new_data; } timeOut = 0; } else { if (timeOut > 4) { state = E210_STATE.RcAttenteSTX; timeOut = 0; } else { Thread.sleep(100); timeOut++; } } break; case RcAttenteLRC: if (serialPort.getInputStream().available() > 0) { byte LRC_Recu = RecoiChar(serialPort); if (LRC_Recu == LRC_Calcul) { EnvoiChar(serialPort, SpecialCar.ACK); LRC_Calcul = 0; // (TEST) Remise à zéro du calcul LRC // Add check signature if (m_strData.length == 48 && m_strData[0] == 51) { this.pistes.add(m_strData.clone()); } else { // ERROR information if (m_strData.length > 2 ){ String thirdCara = new String(m_strData.clone()).substring(0, 2); if (thirdCara.charAt(1) != '0') { this.errorMessages.add(m_strData.clone()); } } } timeOut = 0; state = E210_STATE.RcAttenteEOT; } else { EnvoiChar(serialPort, SpecialCar.NAK); timeOut = 0; state = E210_STATE.RcAttenteSTX; } } else { if (timeOut > 4) { state = E210_STATE.RcAttenteSTX; timeOut = 0; } else { Thread.sleep(100); timeOut++; } } break; case RcAttenteEOT: if (serialPort.getInputStream().available() > 0) { int car = RecoiChar(serialPort); if (car == SpecialCar.EOT) { /* System.out.println("CHECK DATA ???"); if (m_strData.length == 48 && m_strData[0] == 51) { this.pistes.add(m_strData.clone()); System.out.println("DATA !!!"); }*/ //m_Erreurs = m_strData[2] & 0xF; if (m_strData[2] == 0x31) { //m_Erreurs = 0; // Annulation effectuee state = E210_STATE.Repos; // m_fonctionEL210 = EL210_FONCTION_LECTURECMC7; } else { /* * if (m_strData.Length >= 48 && m_strData[0] == * 0x03) { m_strCMC7 = "C" + * ASCIIEncoding.ASCII.GetString(m_strData, 14, * 34); } */ state = E210_STATE.Delai; } } else if (car == SpecialCar.STX) // Il a encore quelque chose a dire ! { timeOut = 0; state = E210_STATE.RcReceptionData; } else { state = E210_STATE.Repos; } } else { if (timeOut > 4) { state = E210_STATE.Repos; timeOut = 0; } else { Thread.sleep(100); timeOut++; } } break; case Delai: //Thread.sleep(2000); // Attends 2 secondes Thread.sleep(100); // Attends 2 secondes // m_fonctionEL210= EL210_FONCTION_LECTURECMC7; state = E210_STATE.Repos; break; default: System.err.print("ERROR UNEXCPECTED STATE" + state.toString()); // Attente de 500 ms // Thread.Sleep(500); throw new JposException(JposConst.JPOS_E_FAILURE, "erreur interne"); } } } catch (InterruptedException e) { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JposException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Send a message to the device * * @param serialPort2 * serial port to use * @param tab * data to send */ private void SendMessage(RXTXPort serialPort2, byte[] tab) { byte[] tab_to_send = new byte[tab.length + 3]; // Write STX tab_to_send[0] = SpecialCar.STX; // Write Data byte lrc = 0; for (int i = 0; i < tab.length; i++) { tab_to_send[i + 1] = tab[i]; lrc = (byte) (lrc ^ tab[i]); } // Write ETX tab_to_send[tab.length + 1] = SpecialCar.ETX; lrc = (byte) (lrc ^ SpecialCar.ETX); // Write the lrc; tab_to_send[tab.length + 2] = lrc; // Send to serial buffer try { serialPort2.getOutputStream().write(tab_to_send); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Send one byte to the device * * @param serialPort2 * serial port to use * @param character * data to send */ private void EnvoiChar(RXTXPort serialPort2, byte character) { try { serialPort2.getOutputStream().write(character); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private byte RecoiChar(RXTXPort serialPort2) { while (true) { int res = -1; try { res = serialPort2.getInputStream().read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (res != -1) return (byte) res; } } public void abort() { if (this.readThread != null) { this.readThread.interrupt(); this.serialPort.close(); } } byte[] readBuffer = null; public void setFunction(byte[] ingenicoCheckFunction) { this.busy = true; this.m_fonctionEL210 = ingenicoCheckFunction; } public E210_STATE getState() { return this.state; } /* * public void serialEvent(SerialPortEvent event) { switch * (event.getEventType()) { case SerialPortEvent.BI: case * SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: * case SerialPortEvent.CD: case SerialPortEvent.CTS: case * SerialPortEvent.DSR: case SerialPortEvent.RI: case * SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case * SerialPortEvent.DATA_AVAILABLE: // we get here if data has been received * //byte[] readBuffer = new byte[20]; try { // read data while * (inputStream.available() > 0) { int numBytes = * inputStream..read(readBuffer); } // print data String result = new * String(readBuffer); System.out.println("Read: "+result); } catch * (IOException e) {} * * break; } } */ }
