forked from skjolber/external-nfc-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acr1283LReader.java
385 lines (309 loc) · 10 KB
/
Acr1283LReader.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
package com.skjolberg.nfc.acs;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Typeface;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import com.skjolberg.nfc.acs.remote.IAcr1283LReaderControl;
public class Acr1283LReader extends AcrReader {
private static final String TAG = Acr1283LReader.class.getName();
private static final int LED_GREEN = 1;
private static final int LED_BLUE = 1 << 1;
private static final int LED_ORANGE = 1 << 2;
private static final int LED_RED = 1 << 3;
// behaviour bits
// bit 0 RFU
private static final int PICC_POLLING_STATUS_LED = 1 << 1;
private static final int PICC_ACTIVATION_STATUS_LED = 1 << 2;
private static final int CARD_INSERTION_AND_REMOVAL_EVENTS_BUZZER = 1 << 3;
// bit 4 RFU
// bit 5 RFU
// bit 6 RFU
private static final int LED_CARD_OPERATION_BLINK = 1 << 7;
private static final int POLL_ISO14443_TYPE_B = 1 << 1;
private static final int POLL_ISO14443_TYPE_A = 1;
public static int serializeBehaviour(AcrDefaultLEDAndBuzzerBehaviour... types) {
int operation = 0;
for(AcrDefaultLEDAndBuzzerBehaviour type : types) {
if(type == AcrDefaultLEDAndBuzzerBehaviour.PICC_POLLING_STATUS_LED) {
operation |= PICC_POLLING_STATUS_LED;
} else if(type == AcrDefaultLEDAndBuzzerBehaviour.PICC_ACTIVATION_STATUS_LED) {
operation |= PICC_ACTIVATION_STATUS_LED;
} else if(type == AcrDefaultLEDAndBuzzerBehaviour.CARD_INSERTION_AND_REMOVAL_EVENTS_BUZZER) {
operation |= CARD_INSERTION_AND_REMOVAL_EVENTS_BUZZER;
} else if(type == AcrDefaultLEDAndBuzzerBehaviour.CARD_OPERATION_BLINK_LED) {
operation |= LED_CARD_OPERATION_BLINK;
} else {
throw new IllegalArgumentException("Behaviour " + type + " not supported");
}
}
return operation;
}
public static List<AcrDefaultLEDAndBuzzerBehaviour> parseBehaviour(int operation) {
List<AcrDefaultLEDAndBuzzerBehaviour> behaviours = new ArrayList<AcrDefaultLEDAndBuzzerBehaviour>();
if((operation & PICC_ACTIVATION_STATUS_LED) != 0) {
behaviours.add(AcrDefaultLEDAndBuzzerBehaviour.PICC_POLLING_STATUS_LED);
}
if((operation & PICC_ACTIVATION_STATUS_LED) != 0) {
behaviours.add(AcrDefaultLEDAndBuzzerBehaviour.PICC_ACTIVATION_STATUS_LED);
}
if((operation & CARD_INSERTION_AND_REMOVAL_EVENTS_BUZZER) != 0) {
behaviours.add(AcrDefaultLEDAndBuzzerBehaviour.CARD_INSERTION_AND_REMOVAL_EVENTS_BUZZER);
}
if((operation & LED_CARD_OPERATION_BLINK) != 0) {
behaviours.add(AcrDefaultLEDAndBuzzerBehaviour.CARD_OPERATION_BLINK_LED);
}
return behaviours;
}
public static int serializeLEDs(AcrLED... types) {
int operation = 0;
for(AcrLED type : types) {
if(type == AcrLED.GREEN) {
operation |= LED_GREEN;
} else if(type == AcrLED.RED) {
operation |= LED_RED;
} else if(type == AcrLED.BLUE) {
operation |= LED_BLUE;
} else if(type == AcrLED.ORANGE) {
operation |= LED_ORANGE;
} else {
throw new IllegalArgumentException("LED " + type + " not supported");
}
}
return operation;
}
public static List<AcrLED> parseLEDs(int operation) {
List<AcrLED> leds = new ArrayList<AcrLED>();
if((operation & LED_RED) != 0) {
leds.add(AcrLED.RED);
}
if((operation & LED_GREEN) != 0) {
leds.add(AcrLED.GREEN);
}
if((operation & LED_BLUE) != 0) {
leds.add(AcrLED.BLUE);
}
if((operation & LED_ORANGE) != 0) {
leds.add(AcrLED.ORANGE);
}
return leds;
}
protected IAcr1283LReaderControl readerControl;
public Acr1283LReader(String name, IAcr1283LReaderControl readerControl) {
this.name = name;
this.readerControl = readerControl;
}
public String getFirmware() throws AcrReaderException {
byte[] response;
try {
response = readerControl.getFirmware();
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readString(response);
}
public List<AcrPICC> getPICC() {
byte[] response;
try {
response = readerControl.getPICC();
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
int operation = readInteger(response);
ArrayList<AcrPICC> values = new ArrayList<AcrPICC>();
if((operation & POLL_ISO14443_TYPE_B) != 0) {
values.add(AcrPICC.POLL_ISO14443_TYPE_B);
}
if((operation & POLL_ISO14443_TYPE_A) != 0) {
values.add(AcrPICC.POLL_ISO14443_TYPE_A);
}
return values;
}
public boolean setPICC(AcrPICC ... types) {
int picc = 0;
for(AcrPICC type : types) {
switch(type) {
case POLL_ISO14443_TYPE_A:{
picc |= POLL_ISO14443_TYPE_A;
break;
}
case POLL_ISO14443_TYPE_B: {
picc |= POLL_ISO14443_TYPE_B;
break;
}
default : {
throw new IllegalArgumentException("Unexpected PICC " + type);
}
}
}
byte[] response;
try {
response = readerControl.setPICC(picc);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeStrongBinder(readerControl.asBinder());
}
public static final Parcelable.Creator<Acr1283LReader> CREATOR =
new Parcelable.Creator<Acr1283LReader>() {
@Override
public Acr1283LReader createFromParcel(Parcel in) {
String name = in.readString();
IBinder binder = in.readStrongBinder();
IAcr1283LReaderControl iin = IAcr1283LReaderControl.Stub.asInterface(binder);
return new Acr1283LReader(name, iin);
}
@Override
public Acr1283LReader[] newArray(int size) {
return new Acr1283LReader[size];
}
};
@Override
public byte[] control(int slotNum, int controlCode, byte[] command) {
byte[] response;
try {
response = readerControl.control(slotNum, controlCode, command);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readByteArray(response);
}
@Override
public byte[] transmit(int slotNum, byte[] command) {
byte[] response;
try {
response = readerControl.transmit(slotNum, command);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readByteArray(response);
}
public boolean setLEDs(AcrLED ... types) {
byte[] response;
try {
int operation = serializeLEDs(types);
response = readerControl.setLEDs(operation);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public boolean lightLED(boolean ready, boolean progress, boolean complete, boolean error) {
List<AcrLED> types = new ArrayList<AcrLED>();
if(ready) {
types.add(AcrLED.GREEN);
}
if(progress) {
types.add(AcrLED.BLUE);
}
if(complete) {
types.add(AcrLED.ORANGE);
}
if(error) {
types.add(AcrLED.RED);
}
return setLEDs(types.toArray(new AcrLED[types.size()]));
}
public List<AcrAutomaticPICCPolling> getAutomaticPICCPolling() {
byte[] response;
try {
response = readerControl.getAutomaticPICCPolling();
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return AcrAutomaticPICCPolling.parse(readInteger(response));
}
public boolean setAutomaticPICCPolling(AcrAutomaticPICCPolling ... types) {
byte[] response;
try {
response = readerControl.setAutomaticPICCPolling(AcrAutomaticPICCPolling.serialize(types));
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public List<AcrDefaultLEDAndBuzzerBehaviour> getDefaultLEDAndBuzzerBehaviour() {
byte[] response;
try {
response = readerControl.getDefaultLEDAndBuzzerBehaviour();
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return parseBehaviour(readInteger(response));
}
public boolean setDefaultLEDAndBuzzerBehaviour(AcrDefaultLEDAndBuzzerBehaviour ... types) {
byte[] response;
try {
int operation = serializeBehaviour(types);
response = readerControl.setDefaultLEDAndBuzzerBehaviour(operation);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public boolean displayText(AcrFont font, int style, int line, int position, String message) throws AcrReaderException {
return displayText(font, style, line, position, font.mapString(message));
}
public boolean displayText(AcrFont font, int style, int line, int position, byte[] message) throws AcrReaderException {
if(line < 0) {
throw new IllegalArgumentException("Expected non-negative line index");
}
if(position < 0) {
throw new IllegalArgumentException("Expected non-negative position index");
}
if(line >= font.getLines()) {
throw new IllegalArgumentException("Font " + font + " supports " + font.getLines() + " lines");
}
if(position + message.length > font.getLineLength()) {
throw new IllegalArgumentException("Font " + font + " supports " + font.getLineLength() + " chars per line");
}
if(style != Typeface.BOLD && style != Typeface.NORMAL) {
throw new IllegalArgumentException("Only font styles " + Typeface.NORMAL +" and " + Typeface.BOLD + " supported");
}
byte[] response;
try {
response = readerControl.displayText(font.getId(), style == Typeface.BOLD, line, position, message);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public boolean lightDisplayBacklight(boolean value) {
byte[] response;
try {
response = readerControl.lightDisplayBacklight(value);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public boolean clearDisplay() {
byte[] response;
try {
response = readerControl.clearDisplay();
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
public boolean setDisplayContrast(int contrast) {
byte[] response;
try {
response = readerControl.setDisplayContrast(contrast);
} catch (RemoteException e) {
throw new AcrReaderException(e);
}
return readBoolean(response);
}
}