Blame view

src/android_serialport_pos_id_api/SerialIdCardPort.java 10.5 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  /*
   * 
   * Copyright 2009 Cedric Priscal  
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");  
   * you may not use this file except in compliance with the License.  
   * You may obtain a copy of the License at  
   * 
   * http://www.apache.org/licenses/LICENSE-2.0  
   * 
   * Unless required by applicable law or agreed to in writing, software  
   * distributed under the License is distributed on an "AS IS" BASIS,  
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
   * See the License for the specific language governing permissions and  
   * limitations under the License.   
   */
  
  package android_serialport_pos_id_api;
  
  import java.io.File;
  import java.io.FileDescriptor;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.UnsupportedEncodingException;
  
  import com.synjones.sdt.IDCard;
  
  //import android.util.Log;
  
  //SerialIDPort
  public class SerialIdCardPort{
  
  	//private static final String TAG = "SerialPort";          
  
  	/*
       * Do not remove or rename the field mFd: it is used by native method close();          
       */
  	private FileDescriptor mFd;
  	private FileInputStream mFileInputStream;
  	private FileOutputStream mFileOutputStream;
  	private final long Tresponse = 1000;
  	private IDCard idcard = new IDCard();
  	private byte[] basemsg = null;
  	public SerialIdCardPort(File device, int baudrate, int flags) throws SecurityException, IOException {
  		
  		/* Check access permission */
  		if (!device.canRead() || !device.canWrite()) {
  			try {
  				/* Missing read/write permission, trying to chmod the file */
  				Process su;
  				su = Runtime.getRuntime().exec("/system/bin/su");
  				String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
  						+ "exit\n";
  				su.getOutputStream().write(cmd.getBytes());
  				if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
  					throw new SecurityException();
  				}
  			}
  			catch (Exception e) {
  				e.printStackTrace();
  				throw new SecurityException();
  			}
  		}
  
  		mFd = open(device.getAbsolutePath(), baudrate, flags);
  		if (mFd == null) {
  			//Log.e(TAG, "native open returns null");
  			throw new IOException();
  		}
  
  		mFileInputStream = new FileInputStream(mFd);
  		mFileOutputStream = new FileOutputStream(mFd);
  	}
  
  	// Getters and setters         
  	public FileInputStream getInputStream() {
  		return mFileInputStream;
  	}
  
  	public OutputStream getOutputStream() {
  		return mFileOutputStream;
  	}
  
  	private byte xorchk(byte[] b, int offset, int length) {
  		byte chk = 0;
  		int i;
  		for (i = 0; i < length; i++) {
  			chk ^= b[offset + i];
  		}
  		return chk;
  	}
  
  	private byte[] commandReader(byte[] cmd, long timeout) {
  		byte[] recv = new byte[7], recvl = null;
  		long TickCount;
  		int recvlen = 0;
  
  		try {
  			//Log.i("串口调试", "Enter commandReader");
  			mFileOutputStream.write(cmd);
  			TickCount = System.currentTimeMillis();
  			while (mFileInputStream.available()<7 && (System.currentTimeMillis()-TickCount)<timeout)
  				Thread.sleep(10);
  			//Log.i("串口调试", "available="+mFileInputStream.available());
  			if (mFileInputStream.available()<10) {
  				while (mFileInputStream.available()>0)
  					mFileInputStream.read();
  				return null;
  			}
  			//Log.i("串口调试", "available="+mFileInputStream.available());
  			if (mFileInputStream.read(recv) != recv.length
  					|| recv[0] != (byte)0xAA
  					|| recv[1] != (byte)0xAA
  					|| recv[2] != (byte)0xAA
  					|| recv[3] != (byte)0x96
  					|| recv[4] != (byte)0x69) {
  				while (mFileInputStream.available()>0)
  					mFileInputStream.read();
  				return null;
  			}
  
  			recvlen = recv[5] * 256 + recv[6];
  			//Log.i("串口调试", "bytes are availabled! recvlen="+recvlen);
  			while (mFileInputStream.available()<recvlen && (System.currentTimeMillis()-TickCount)<timeout)
  				Thread.sleep(10);
  			if (mFileInputStream.available()<recvlen || recvlen < 4) {
  				while (mFileInputStream.available()>0)
  					mFileInputStream.read();
  				return null;
  			}
  			recvl = new byte[recv.length+recvlen];
  			System.arraycopy(recv, 0, recvl, 0, recv.length);
  			if (mFileInputStream.read(recvl, recv.length, recvlen) != recvlen) {
  				while (mFileInputStream.available()>0)
  					mFileInputStream.read();
  				return null;
  			}
  
  			/*
  			if (recvlen <64) {
  				String dbgmsg = "cmd=" + Integer.toHexString(cmd[7]) + " " + Integer.toHexString(cmd[8]) + ",";
  				for (int i=0;i<recvl.length;i++)
  					dbgmsg += Integer.toHexString(recvl[i]&0x00FF) + " ";
  				Log.i("串口调试", dbgmsg);
  			}
  			*/
  
  
  			while (mFileInputStream.available()>0)
  				mFileInputStream.read();
  
  			if (xorchk(recvl, 5, recvl.length-5)!=0)
  				return null;
  		} catch (IOException ioe) {
  			recvl = null;
  		} catch (NullPointerException npe) {
  			recvl = null;
  		} catch (InterruptedException ie) {
  			recvl = null;
  		}
  
  		return recvl;
  	}
  
  	public byte resetSAM() {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x10,(byte)0xFF,(byte)0xEC};
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || recvl[9] != 0x90)
  			Status = 2;
  		return Status;
  	}
  
  	public byte setMaxRFByte(byte max) {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x04,(byte)0x61,(byte)0xFF,(byte)0x50,(byte)0xCA};
  		cmd[9] = max;
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || recvl[9] != 0x90)
  			Status = 2;
  		return Status;
  	}
  
  	public byte getSAMStatus() {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x11,(byte)0xFF,(byte)0xED};
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || recvl[9] != 0x90)
  			Status = 2;
  		return Status;
  	}
  
  	public byte getSAMID(byte[] samid) {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x12,(byte)0xFF,(byte)0xFE};
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || recvl[9] != 0x90)
  			Status = 2;
  		else if (recvl.length < 27)
  			Status = 3;
  		else {
  			samid = new byte[16];
  			System.arraycopy(recvl, 10, samid, 0, samid.length);
  		}
  		return Status;
  	}
  
  	public byte startFindIDCard() {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x20,(byte)0x01,(byte)0x22};
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || (recvl[9] != (byte)0x9F && recvl[9] != (byte)0x80)) {
  			Status = 2;
  			IDCard.SW1 = recvl[7];
  			IDCard.SW2 = recvl[8];
  			IDCard.SW3 = recvl[9];
  		}
  		//Log.i("串口调试", "startFindCard="+Status);
  		return Status;
  	}
  
  	public byte selectIDCard() {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x20,(byte)0x02,(byte)0x21};
  		byte[] recvl = commandReader(cmd, Tresponse);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || (recvl[9] != (byte)0x90 && recvl[9] != (byte)0x81)) {
  			Status = 2;
  			IDCard.SW1 = recvl[7];
  			IDCard.SW2 = recvl[8];
  			IDCard.SW3 = recvl[9];
  		}
  		//Log.i("串口调试", "selectIDCard="+Status);
  		return Status;
  	}
  
  	public byte readBaseMsg() {
  		byte Status = 0;
  		byte[] cmd = {(byte)0xAA,(byte)0xAA,(byte)0xAA,(byte)0x96,(byte)0x69,(byte)0x00,(byte)0x03,(byte)0x30,(byte)0x01,(byte)0x32};
  		byte[] recvl = commandReader(cmd, Tresponse*10);
  		if (recvl == null)
  			Status = 1;
  		else if (recvl[7] != 0x00 || recvl[8] != 0x00 || recvl[9] != (byte)0x90) {
  			Status = 2;
  			IDCard.SW1 = recvl[7];
  			IDCard.SW2 = recvl[8];
  			IDCard.SW3 = recvl[9];
  		}
  		else if (recvl.length < 1295)
  			Status = 3;
  		else {
  			basemsg = new byte[4+256+1024];
  			System.arraycopy(recvl, 10, basemsg, 0, basemsg.length);
  			//Log.i("串口调试", "basemsg="+basemsg.length);
  		}
  		//Log.i("串口调试", "readBaseMsg="+Status+" recvl="+(recvl==null?0:recvl.length));
  
  		return Status;
  	}
  
  	public IDCard getIDCard() {
  		short textlen, wltlen;
  		String dbgmsg = "";
  		if (startFindIDCard() == 0x00 && selectIDCard()==0x00 && readBaseMsg()==0) {
  			try {
  				textlen = (short) (basemsg[0] * 256 + basemsg[1]);
  				wltlen = (short) (basemsg[2] * 256 + basemsg[3]);
  				byte[] name = new byte[30];
  				System.arraycopy(basemsg, 4, name, 0, name.length);
  				idcard.setName(new String(name, "UTF-16LE").trim());
  				byte[] sex = new byte[2];
  				System.arraycopy(basemsg, 34, sex, 0, sex.length);
  				idcard.setSex(new String(sex, "UTF-16LE"));
  				if (idcard.getSex().equalsIgnoreCase("1"))
  					idcard.setSex("男");
  				else
  					idcard.setSex("女");
  				byte[] nation = new byte[4];
  				System.arraycopy(basemsg, 36, nation, 0, nation.length);
  				idcard.setNation(idcard.getNationName(new String(nation, "UTF-16LE")));
  				byte[] birthday = new byte[16];
  				System.arraycopy(basemsg, 40, birthday, 0, birthday.length);
  				idcard.setBirthday(new String(birthday, "UTF-16LE"));
  				byte[] address = new byte[70];
  				System.arraycopy(basemsg, 56, address, 0, address.length);
  				idcard.setAddress(new String(address, "UTF-16LE").trim());
  				byte[] idcardno = new byte[36];
  				System.arraycopy(basemsg, 126, idcardno, 0, idcardno.length);
  				idcard.setIDCardNo(new String(idcardno, "UTF-16LE"));
  				byte[] grantdept = new byte[30];
  				System.arraycopy(basemsg, 162, grantdept, 0, grantdept.length);
  				idcard.setGrantDept(new String(grantdept, "UTF-16LE").trim());
  				byte[] userlifebegin = new byte[16];
  				System.arraycopy(basemsg, 192, userlifebegin, 0, userlifebegin.length);
  				idcard.setUserLifeBegin(new String(userlifebegin, "UTF-16LE"));
  				byte[] userlifeend = new byte[16];
  				System.arraycopy(basemsg, 208, userlifeend, 0, userlifeend.length);
  				idcard.setUserLifeEnd(new String(userlifeend, "UTF-16LE").trim());
  				byte[] wlt = new byte[1024];
  				System.arraycopy(basemsg, textlen+4, wlt, 0, wlt.length);
  				idcard.setWlt(wlt);
  			} catch (UnsupportedEncodingException e) {
  				// TODO Auto-generated catch block
  				e.printStackTrace();
  			}
  		} else {
  			return null;
  		}
  		return idcard;
  	}
  
  	// JNI
  	private native static FileDescriptor open(String path, int baudrate, int flags);
  	public native void close();
  	public native int readCard();
  	public native int getField(int fieldId);
  
  
  	static {//serial_idcard_port
  		System.loadLibrary("serial_IdCard");
  	}
  }