-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSLibrary.cs
More file actions
executable file
·557 lines (456 loc) · 18.5 KB
/
Copy pathCSLibrary.cs
File metadata and controls
executable file
·557 lines (456 loc) · 18.5 KB
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
/*
Copyright (c) 2018 Convergence Systems Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using CSLibrary.Constants;
using CSLibrary.NetFinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSLibrary
{
using static RFIDDEVICE;
using Constants;
using Plugin.BLE.Abstractions.Contracts;
public partial class HighLevelInterface
{
#region Constant
// CS108 State
public enum READERSTATE
{
DISCONNECT,
IDLE,
BUSY,
READYFORDISCONNECT
}
// CS108 Command
private class DOWNLINKCMD
{
// RFID event code
public static readonly byte[] RFIDPOWERON = { 0x80, 0x00 };
public static readonly byte[] RFIDPOWEROFF = { 0x80, 0x01 };
public static readonly byte[] RFIDCMD = { 0x80, 0x02 };
// Barcode event code
public static readonly byte[] BARCODEPOWERON = { 0x90, 0x00 };
public static readonly byte[] BARCODEPOWEROFF = { 0x90, 0x01 };
public static readonly byte[] BARCODESCANTRIGGER = { 0x90, 0x02 };
public static readonly byte[] BARCODERAWDATA = { 0x90, 0x03 };
// CS108 status event code
public static readonly byte[] GETVOLTAGE = { 0xA0, 0x00 };
public static readonly byte[] GETTRIGGERSTATE = { 0xA0, 0x01 };
public static readonly byte[] STARTAUTOREPORTING = { 0xA0, 0x02 };
public static readonly byte[] STOPAUTOREPORTING = { 0xA0, 0x03 };
public static readonly byte[] RFIDABORT = { 0xA0, 0x04 };
public static readonly byte[] GETTRIGGER = { 0xA0, 0x05 };
public static readonly byte[] SETFASTMODE = { 0xA0, 0x06 };
public static readonly byte[] GETFASTMODE = { 0xA0, 0x07 };
public static readonly byte[] STARTAUTOREIGGERREPORTING = { 0xA0, 0x08 };
public static readonly byte[] STOPAUTOREIGGERREPORTING = { 0xA0, 0x09 };
// Silicon Lab IC event code
public static readonly byte[] GETSILICONVER = { 0xB0, 0x00 };
public static readonly byte[] SLIMAGERAWDATA = { 0xB0, 0x01 };
public static readonly byte[] SLBOOTLOADERRAWDATA = { 0xB0, 0x02 };
// BlueTooth event code
public static readonly byte[] GETVLUETOOTHVER = { 0xB0, 0x00 };
public static readonly byte[] BTIMAGERAWDATA = { 0xB0, 0x01 };
public static readonly byte[] BTBOOTLOADERRAWDATA = { 0xB0, 0x02 };
public static readonly byte[] SETDEVICENAME = { 0xB0, 0x03 };
public static readonly byte[] GETDEVICENAME = { 0xB0, 0x04 };
}
private class UPLINKCMD
{
public static readonly byte[] RFIDDATA = { 0x81, 0x00 };
public static readonly byte[] BARCODEDATA = { 0x90, 0x00 };
public static readonly byte[] BARCODEVALID = { 0x90, 0x01 };
public static readonly byte[] BATTERYFAILED = { 0xA1, 0x00 };
public static readonly byte[] ERRORCODE = { 0xA1, 0x01 };
public static readonly byte[] TRIGGERPUSHED = { 0xA1, 0x02 };
public static readonly byte[] TRIGGERRELEASED = { 0xA1, 0x03 };
}
// Constant
#endregion
#region internal variable
private READERSTATE _readerState = READERSTATE.DISCONNECT;
public READERSTATE Status { get { return _readerState; } }
/// <summary>
/// Device type — set by the active HAL's ConnectAsync before HardwareInit.
/// </summary>
internal MODEL _deviceType = MODEL.UNKNOWN;
/// <summary>
/// Reader Operation State Event
/// </summary>
public event EventHandler<CSLibrary.Events.OnReaderStateChangedEventArgs> OnReaderStateChanged;
#endregion
#region public variable
private SYSTEMPARAMETERS _sp = null;
private SiliconLabIC _handleSiliconLabIC = null;
private RFIDReader _handlerRFIDReader = null;
private BarcodeReader _handleBarCodeReader = null;
private Notification _handleNotification = null;
private BluetoothIC _handleBluetoothIC = null;
internal Battery _handleBattery = null;
private ITransport _transport = null;
public SiliconLabIC siliconlabIC
{
get { return _handleSiliconLabIC; }
}
public RFIDReader rfid
{
get { return _handlerRFIDReader; }
//set { _handlerRFIDReader = value; }
}
public BarcodeReader barcode
{
get { return _handleBarCodeReader; }
//set { _handleBarCodeReader = value; }
}
public Notification notification
{
get { return _handleNotification; }
//set { _handleNotification = value; }
}
public BluetoothIC bluetoothIC
{
get { return _handleBluetoothIC; }
//set { _handleBluetoothIC = value; }
}
public Battery battery
{
get { return _handleBattery; }
}
public string ReaderName
{
get
{
var value = _handleBluetoothIC.GetDeviceName();
if (value == null)
return _sp.getDeviceName();
else
return value;
}
}
internal string _MacAdd = null;
public string GetMacAddress()
{
return _MacAdd;
}
#endregion
public HighLevelInterface()
{
// Basic Module
_sp = new SYSTEMPARAMETERS();
_handleSiliconLabIC = new SiliconLabIC(this);
_handlerRFIDReader = new RFIDReader(this);
_handleBarCodeReader = new BarcodeReader(this);
_handleNotification = new Notification(this);
_handleBluetoothIC = new BluetoothIC(this);
_handleBattery = new Battery(this);
}
~HighLevelInterface()
{
DisconnectAsync();
}
public void CancelEventOnReaderStateChanged()
{
OnReaderStateChanged = null;
}
/// <summary>
/// Common disconnect cleanup — stops timer, clears buffers, resets state.
/// HAL-specific disconnect (BLE cleanup, barcode power) should be done
/// by the HAL's DisconnectAsync before calling this.
/// </summary>
internal void DisconnectCommon()
{
if (BTTimer != null)
{
BTTimer.Cancel();
BTTimer = null;
}
_readerState = READERSTATE.DISCONNECT;
_sendBuffer.Clear();
_NeedCommandResponseType = BTWAITCOMMANDRESPONSETYPE.NOWAIT;
}
/// <summary>
/// Unified disconnect — routes to BLE or TCP implementation based on current connection mode.
/// </summary>
public void DisconnectAsync()
{
if (_sp != null && _sp.getConnectionMode() == RFIDDEVICE.CONNECTIONMODE.TCP)
TCP_DisconnectAsync();
else
BLE_DisconnectAsync();
}
public Version GetVersion ()
{
Version ver = new Version(2, 0, 10, 1);
return ver;
}
CSLibrary.Timer BTTimer;
void HardwareInit()
{
BARCODEPowerOn();
RFIDPowerOff();
RFIDPowerOn();
// Set Low Power mode
rfid.SetDeviceType(_deviceType);
rfid.StopOperation();
_handleBluetoothIC.Connect();
_handleSiliconLabIC.Connect(_deviceType);
if (_sp.getConnectionMode() != RFIDDEVICE.CONNECTIONMODE.TCP)
_handleBarCodeReader.CheckHWValid();
//rfid.StopOperation();
//rfid.Connect(MODEL.CS710S);
rfid.Connect(_deviceType);
//rfid.ResetToDefaultPowerMode();
//rfid.SetReaderPowerMode(false);
//rfid.SetToStandbyMode();
notification.SetAutoReport(true);
//notification.SetAutoTriggerReport(0); // off auto trigger report // cancel SetAutoTriggerReport commnad to compatible with old BT firmware
FireReaderStateChangedEvent(new Events.OnReaderStateChangedEventArgs(_sendBuffer[0], Constants.ReaderCallbackType.CONNECT_SUCESS));
}
Func <Task> _afterFinishBLETask = null;
void WhenBLEFinish(Func<Task> nextTask)
{
_afterFinishBLETask = nextTask;
}
void ExecuteFinishBLETask()
{
if (_afterFinishBLETask == null)
return;
_afterFinishBLETask();
_afterFinishBLETask = null;
}
/// <summary>
///
/// </summary>
/// <param name="packetData"></param>
public bool ProcessAPIPacket (byte [] recData)
{
byte [] data = (byte [])recData.Clone();
//CSLibrary.Debug.WriteBytes("Routine : ProcessAPIPacket", recData);
switch (data[3])
{
case 0xc2:
// check packet running number
if (data[5] == 0x9e && data[8] == 0x81 && data[9] == 0x00)
{
_blePacketRunningNumber++;
if (data[4] != _blePacketRunningNumber)
{
if (data[4] > _blePacketRunningNumber)
InventoryDebug.InventorySkipPackerAdd((uint)(data[4] - _blePacketRunningNumber));
else
InventoryDebug.InventorySkipPackerAdd((uint)((256U + data[4]) - _blePacketRunningNumber));
_blePacketRunningNumber = data[4];
}
}
_currentCommandResponse |= RecvRFIDPacket(data);
break;
case 0x6a:
switch (RecvBarcodePacket(data))
{
case BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND:
_currentCommandResponse |= BTWAITCOMMANDRESPONSETYPE.BTAPIRESPONSE;
break;
case BARCODECOMMANDRESPONSETYPE.DATA:
_currentCommandResponse |= BTWAITCOMMANDRESPONSETYPE.DATA1;
break;
case BARCODECOMMANDRESPONSETYPE.NOTIFICATION:
case BARCODECOMMANDRESPONSETYPE.ERROR:
default:
return true;
}
break;
case 0xd9:
if (RecvNofigicationPacket (data))
_currentCommandResponse |= BTWAITCOMMANDRESPONSETYPE.BTAPIRESPONSE;
break;
//return false;
case 0xe8:
_currentCommandResponse |= _handleSiliconLabIC.ProcessDataPacket(data);
break;
case 0x5f:
if (_handleBluetoothIC.BluetoothICPacket(data))
_currentCommandResponse |= BTWAITCOMMANDRESPONSETYPE.BTAPIRESPONSE;
break;
}
BLERWEngineTimer(); // send next packet immediately
return true;
}
void TimerFunc(object o)
{
if (_readerState == READERSTATE.READYFORDISCONNECT || _readerState == READERSTATE.DISCONNECT)
{
BTTimer.Cancel();
_sendBuffer.Clear();
_NeedCommandResponseType = BTWAITCOMMANDRESPONSETYPE.NOWAIT;
_readerState = READERSTATE.DISCONNECT;
return;
}
BLERWEngineTimer();
return;
}
internal CSLibrary.HighLevelInterface.BTWAITCOMMANDRESPONSETYPE RecvRFIDPacket(byte[] recvData)
{
CSLibrary.Debug.WriteLine("Routine : RecvRFIDPacket");
UInt16 eventCode = (UInt16)((UInt16)recvData[8] << 8 | (UInt16)recvData[9]);
switch (eventCode)
{
case 0x8000:
{
// power on
var result = recvData[10];
}
break;
case 0x8001:
{
// power off
var result = recvData[10];
}
break;
case 0x8002:
{
var result = recvData[10];
}
break;
case 0x8100:
//return _handlerRFIDReader.DeviceRecvData (recvData, _currentCommandResponse);
return rfid.DeviceRecvData(recvData, _currentCommandResponse);
default:
// packet data error
return BTWAITCOMMANDRESPONSETYPE.NOWAIT;
}
return BTWAITCOMMANDRESPONSETYPE.BTAPIRESPONSE;
}
enum BARCODECOMMANDRESPONSETYPE
{
CONTROLCOMMAND,
DATA,
NOTIFICATION,
ERROR
}
BARCODECOMMANDRESPONSETYPE RecvBarcodePacket (byte[] recvData)
{
UInt16 eventCode = (UInt16)((UInt16)recvData[8] << 8 | (UInt16)recvData[9]);
switch (eventCode)
{
case 0x9000:
{
// power on
//_handleBarCodeReader.DeviceRecvState(1);
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
}
case 0x9001:
{
// power off
//_handleBarCodeReader.DeviceRecvState(0);
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
}
case 0x9002:
//_handlerRFIDReader.DeviceRecvData(recvData, 10, recvData.Length - 10);
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
case 0x9003:
//_handlerRFIDReader.DeviceRecvData(recvData, 10, recvData.Length - 10);
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
case 0x9004:
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
case 0x9005:
return BARCODECOMMANDRESPONSETYPE.CONTROLCOMMAND;
case 0x9100: // (barcode reader data)
if (_handleBarCodeReader.DeviceRecvData(recvData))
return BARCODECOMMANDRESPONSETYPE.DATA;
break;
case 0x9101: // Good read (ignore)
_handleBarCodeReader.DeviceRecvGoodRead();
return BARCODECOMMANDRESPONSETYPE.NOTIFICATION;
}
return BARCODECOMMANDRESPONSETYPE.ERROR;
}
internal bool RecvNofigicationPacket(byte[] recvData)
{
UInt16 eventCode = (UInt16)((UInt16)recvData[8] << 8 | (UInt16)recvData[9]);
switch (eventCode)
{
case 0xa000: // Current battery voltage
{
UInt16 voltage = (UInt16)((UInt16)recvData[10] << 8 | (UInt16)recvData[11]);
// buf fix CS710S report wrong voltage
if (_deviceType == RFIDDEVICE.MODEL.CS710S)
{
if (_handleSiliconLabIC.GetFirmwareVersion() <= 0x00020104) // <= version 2.1.4
if (voltage >= 4450) // > 4.45v
voltage -= 430; // - 0.43v
else
voltage -= 350; // - 0.35v
}
_handleNotification.DeviceRecvVoltage(voltage);
}
return false;
break;
case 0xa001: // Button Status
switch (recvData[10])
{
case 0x00:
_handleNotification.DeviceRecvState(1); // Send event to application
break;
case 0x01:
_handleNotification.DeviceRecvState(0); // Send event to application
break;
}
return false;
break;
case 0xa002:
break;
case 0xa006:
break;
case 0xa101: // Error Code
CSLibrary.Debug.WriteLine("Error : CS108 Error Code : {0}", (UInt16)((UInt16)recvData[10] << 8 | (UInt16)recvData[11]));
return false;
break;
case 0xa102: // Button On
_handleNotification.DeviceRecvState(0); // Send event to application
return false;
break;
case 0xa103: // Button Off
_handleNotification.DeviceRecvState(1); // Send event to application
return false;
break;
}
return true;
}
// public RFID function
internal void RFIDPowerOn()
{
SendAsync(0, 0, DOWNLINKCMD.RFIDPOWERON, null, BTWAITCOMMANDRESPONSETYPE.WAIT_BTAPIRESPONSE);
}
internal void RFIDPowerOff()
{
SendAsync(0, 0, DOWNLINKCMD.RFIDPOWEROFF, null, BTWAITCOMMANDRESPONSETYPE.WAIT_BTAPIRESPONSE);
}
// public barcode function
internal void BARCODEPowerOn()
{
SendAsync(0, 1, DOWNLINKCMD.BARCODEPOWERON, null, BTWAITCOMMANDRESPONSETYPE.WAIT_BTAPIRESPONSE);
}
internal void BARCODEPowerOff()
{
SendAsync(0, 1, DOWNLINKCMD.BARCODEPOWEROFF, null, BTWAITCOMMANDRESPONSETYPE.WAIT_BTAPIRESPONSE);
}
}
}