PageRenderTime 73ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/card/readcard.c

https://bitbucket.org/hkhateb/v1_libfunc_interface_s.c
C | 895 lines | 637 code | 93 blank | 165 comment | 104 complexity | 7d53d51880ddc29393dc73de3195e196 MD5 | raw file
  1. #include<ctype.h>
  2. #include "readcard.h"
  3. #include "../public/libserial.h"
  4. #include "../public/initcom.h"
  5. #include "../public/public.h"
  6. #include "../weigand/weigand.h"
  7. struct card_INFO cardinfo; //card-reader info
  8. struct R_card_INFO ReadCardInfo; //card-reader info
  9. struct W_card_INFO WriteCardInfo; //card-reader info
  10. /*--------------------------------------------------------------------------*
  11. @Function :InitCard - initialize card-reader parameters
  12. @Include :"readcard.h"
  13. @Description :void
  14. @Return value : Failure FALSE; Success TRUE
  15. *---------------------------------------------------------------------------*/
  16. int InitCard(struct card_INFO *card_info)
  17. {
  18. cardinfo=*card_info;
  19. return TRUE;
  20. }
  21. /*--------------------------------------------------------------------------*
  22. @Function :InitCard_Read - initialize card-reader parameters
  23. @Include :"readcard.h"
  24. @Description :void
  25. @Return value : Failure FALSE; Success TRUE
  26. *---------------------------------------------------------------------------*/
  27. int InitCard_Read(struct R_card_INFO *R_cardinfo)
  28. {
  29. ReadCardInfo=*R_cardinfo;
  30. return TRUE;
  31. }
  32. /*--------------------------------------------------------------------------*
  33. @Function :InitCard_Write - initialize card-reader parameters
  34. @Include :"readcard.h"
  35. @Description :void
  36. @Return value : Failure FALSE; Success TRUE
  37. *---------------------------------------------------------------------------*/
  38. int InitCard_Write(struct W_card_INFO *W_cardinfo)
  39. {
  40. WriteCardInfo=*W_cardinfo;
  41. return TRUE;
  42. }
  43. /*--------------------------------------------------------------------------*
  44. @Function :ReadCard - get data
  45. @Include :"readcard.h"
  46. @Description :
  47. char *cardnum:card No.
  48. judge what type of data was received form com1
  49. (as data form keypad and all card-reader are all transferred through com1,so it's necessary to distinguish)
  50. @Return value :
  51. Failure 0;
  52. read magnetic failure -1;
  53. read barcode crypt failure -2;
  54. read barcode failure -3;
  55. Success card type >0
  56. *---------------------------------------------------------------------------*/
  57. int ReadCardOld(char *cardnum)
  58. {
  59. unsigned char *card=NULL,buf[32];
  60. static char sno[128];
  61. unsigned int wgdata=0;
  62. int i=0,len=0;
  63. memset(sno, 0, sizeof(sno));
  64. switch(com1value.instruction)
  65. {
  66. case READHID:
  67. card = com1value.user_data;
  68. len=com1value.nbytes;
  69. cardnum[0]=len;
  70. memcpy(cardnum+1,com1value.user_data,len);
  71. return com1value.instruction;
  72. case READEMCARD:
  73. card = com1value.user_data;
  74. sprintf(sno, "%02X%02X%02X%02X%02X",*card++,*card++,*card++,*card++,*card++);
  75. // p=card_handle(sno);
  76. break;
  77. case READMF1CARD:
  78. card = com1value.user_data;
  79. printf("com1value.user_data = %02X%02X%02X%02X\n",*card,*card+1,*card+2,*card+3);
  80. memset(buf,0,sizeof(buf));
  81. printf("buf1 = %s\n",buf);
  82. sprintf(buf, "%02X%02X%02X%02X",*card++,*card++,*card++,*card++);
  83. printf("buf2 = %s\n",buf);
  84. sprintf(sno,"%lu",strtoul(buf,NULL,16));
  85. printf("buf3 = %s\n",buf);
  86. //
  87. break;
  88. case READMAGNETIC:
  89. if((int)com1value.address==cardinfo.track){
  90. if((int)com1value.itemnum==0x0)
  91. return -1;
  92. memset(sno, 0, sizeof(sno));
  93. // len=(int)com1value.nbytes;
  94. while(isxdigit(com1value.user_data[i]))
  95. {
  96. sno[i]=(int)com1value.user_data[i];
  97. i++;
  98. }
  99. }
  100. else return 0;
  101. break;
  102. case READBARCODE:
  103. if((int)com1value.user_data[0]==0x0&&(int)com1value.user_data[1]==0x0)
  104. return -2;
  105. else if((int)com1value.user_data[0]==0x0&&(int)com1value.user_data[1]==0x1)
  106. return -3;
  107. memset(sno, 0, sizeof(sno));
  108. len=(int)com1value.nbytes;
  109. i=0;
  110. len--;
  111. while(len--)
  112. {
  113. sno[i]=(int)com1value.user_data[i+1];
  114. i++;
  115. }
  116. default:
  117. /* wgdata=ReadWigan();
  118. if(wgdata<=0) break;
  119. sprintf(sno,"%06X",(wgdata<<7)>>8); //cut the first and the last bytes
  120. if(cardinfo.card_right!=0) //cut the card number according to setting
  121. {
  122. memset(sno,0,sizeof(sno));
  123. sprintf(sno,"%10X",(wgdata<<7)>>8);//cut the first and the last bytes
  124. right(sno,cardinfo.card_right);
  125. }
  126. com1value.instruction=WEIGANDCARD;
  127. */
  128. break;
  129. }
  130. if(sno==NULL||strlen(sno)==0) return com1value.instruction;
  131. // printf("p=%s\n",sno);
  132. memcpy(cardnum,sno,strlen(sno));
  133. return com1value.instruction;
  134. }
  135. /*--------------------------------------------------------------------------*
  136. @Function :ReadCard - get data
  137. @Include :"readcard.h"
  138. @Description :
  139. char *cardnum:card No.
  140. judge what type of data was received form com1
  141. (as data form keypad and all card-reader are all transferred through com1,so it's necessary to distinguish)
  142. @Return value :
  143. Failure 0;
  144. read magnetic failure -1;
  145. read barcode crypt failure -2;
  146. read barcode failure -3;
  147. read magnetic crypt failure -4;
  148. unknown error -5;
  149. read EM crypt failure -6;
  150. read EM failure -7;
  151. read MF1 crypt failure -8;
  152. read MF1 failure -9;
  153. read HID crypt failure -10;
  154. read HID failure -11;
  155. Success card type >0
  156. *---------------------------------------------------------------------------*/
  157. int ReadCard(char *cardnum)
  158. {
  159. unsigned char *card=NULL,buf[32];
  160. static char sno[128];
  161. unsigned int wgdata=0;
  162. int i=0,len=0;
  163. start:
  164. memset(sno, 0, sizeof(sno));
  165. switch(com1value.instruction)
  166. {
  167. case READHID:
  168. if((int)com1value.itemnum==0x0)
  169. {
  170. if(com1value.user_data[0]==0x00)
  171. return -10;
  172. else if(com1value.user_data[0]==0x01)
  173. return -11;
  174. else return -5;
  175. }
  176. card = com1value.user_data;
  177. len=com1value.nbytes;
  178. cardnum[0]=len;
  179. memcpy(cardnum+1,com1value.user_data,len);
  180. return com1value.instruction;
  181. case READEMCARD:
  182. if((int)com1value.itemnum==0x0)
  183. {
  184. if(com1value.user_data[0]==0x00)
  185. return -8;
  186. else if(com1value.user_data[0]==0x01)
  187. return -9;
  188. else return -5;
  189. }
  190. card = com1value.user_data;
  191. sprintf(sno, "%02X%02X%02X%02X%02X",*card++,*card++,*card++,*card++,*card++);
  192. // p=card_handle(sno);
  193. break;
  194. case READMF1CARD:
  195. if((int)com1value.itemnum==0x0)
  196. {
  197. if(com1value.user_data[0]==0x00)
  198. return -6;
  199. else if(com1value.user_data[0]==0x01)
  200. return -7;
  201. else return -5;
  202. }
  203. //printf("com1value.user_data = %02X%02X%02X%02X",card,card+1,card+2,card+3);
  204. card = com1value.user_data;
  205. memset(buf,0,sizeof(buf));
  206. sprintf(buf, "%02X%02X%02X%02X",*card++,*card++,*card++,*card++);
  207. printf("libfun buf = %s\n",buf);
  208. //convert to unsigned long int - need to fix this point in another versions we get in future from chineses
  209. sprintf(sno,"%lu",strtoul(buf,NULL,16));
  210. printf("libfun sno = %s\n",sno);
  211. //
  212. break;
  213. case READMAGNETIC:
  214. //printf("com1value.address = %d\n",com1value.address);
  215. //if((int)com1value.address > 0){
  216. /*if((int)com1value.address==cardinfo.track){
  217. if((int)com1value.itemnum==0x0)
  218. {
  219. if(com1value.user_data[0]==0x00)
  220. return -4;
  221. else if(com1value.user_data[0]==0x01)
  222. return -1;
  223. else return -5;
  224. }
  225. memset(sno, 0, sizeof(sno));
  226. // len=(int)com1value.nbytes;
  227. while(isxdigit(com1value.user_data[i]))
  228. {
  229. sno[i]=(int)com1value.user_data[i];
  230. //printf("com1value.user_data[i] = %d %c\n",com1value.user_data[i],com1value.user_data[i]);
  231. i++;
  232. }
  233. sno[i] = '\0';
  234. }
  235. else return 0;*/
  236. loop:
  237. if((int)com1value.address==cardinfo.track){
  238. if((int)com1value.itemnum==0x0)
  239. {
  240. if(com1value.user_data[0]==0x00)
  241. return -4;
  242. else if(com1value.user_data[0]==0x01)
  243. return -1;
  244. else return -5;
  245. }
  246. memset(sno, 0, sizeof(sno));
  247. // len=(int)com1value.nbytes;
  248. while(isxdigit(com1value.user_data[i]))
  249. {
  250. sno[i]=(int)com1value.user_data[i];
  251. i++;
  252. }
  253. }
  254. else
  255. {
  256. if(ReadCom1()==FALSE) return 0;
  257. if (com1value.instruction==READMAGNETIC)goto loop;
  258. else{
  259. goto start;
  260. }
  261. }
  262. break;
  263. case READBARCODE:
  264. if((int)com1value.user_data[0]==0x0&&(int)com1value.user_data[1]==0x0)
  265. return -2;
  266. else if((int)com1value.user_data[0]==0x0&&(int)com1value.user_data[1]==0x1)
  267. return -3;
  268. memset(sno, 0, sizeof(sno));
  269. len=(int)com1value.nbytes;
  270. i=0;
  271. len--;
  272. while(len--)
  273. {
  274. sno[i]=(int)com1value.user_data[i+1];
  275. i++;
  276. }
  277. default:
  278. /* wgdata=ReadWigan();
  279. if(wgdata<=0) break;
  280. sprintf(sno,"%06X",(wgdata<<7)>>8); //cut the first and the last bytes
  281. if(cardinfo.card_right!=0) //cut the card number according to setting
  282. {
  283. memset(sno,0,sizeof(sno));
  284. sprintf(sno,"%10X",(wgdata<<7)>>8);//cut the first and the last bytes
  285. right(sno,cardinfo.card_right);
  286. }
  287. com1value.instruction=WEIGANDCARD;
  288. */
  289. break;
  290. }
  291. len = strlen(sno);
  292. if(sno==NULL||len==0) return com1value.instruction;
  293. // printf("p=%s\n",sno);
  294. memcpy(cardnum,sno,len);
  295. cardnum[len] = '\0';
  296. return com1value.instruction;
  297. }
  298. //8+6 bits -->3+5 Bytes
  299. int ReadWigan26(struct mem_inout* wg_data)
  300. {
  301. unsigned int count1=0,count2=0;
  302. unsigned int i,begin=0,end=0,c=0;
  303. //read
  304. if(ReadWigan(26,wg_data)!=0)return -1;
  305. //printf("wg26 success\n");
  306. //even-odd check
  307. for(i=0;i<26;i++)
  308. {
  309. c=1<<i;
  310. if(c&wg_data->d[0]){
  311. if(i==0)begin=1;
  312. else if(i<13)count1++;
  313. else if(i<25)count2++;
  314. else if(i==25)end=1;
  315. }
  316. }
  317. //cut Hight and Low bit
  318. wg_data->d[0]=(wg_data->d[0]&0X1FFFFFE)>>1;
  319. if(count1%2!=begin&&count2%2==end)return 0;
  320. else return -1;
  321. }
  322. int ReadWigan27(struct mem_inout* wg_data)
  323. {
  324. //read
  325. if(ReadWigan(27,wg_data)!=0)return -1;
  326. return -1;
  327. //check
  328. }
  329. int ReadWigan32(struct mem_inout* wg_data)
  330. {
  331. //read
  332. if(ReadWigan(32,wg_data)!=0)return -1;
  333. return -1;
  334. //check
  335. }
  336. int ReadWigan33(struct mem_inout* wg_data)
  337. {
  338. //read
  339. if(ReadWigan(33,wg_data)!=0)return -1;
  340. return -1;
  341. //check
  342. }
  343. int ReadWigan34(struct mem_inout* wg_data)
  344. {
  345. //read
  346. if(ReadWigan(34,wg_data)!=0)return -1;
  347. return -1;
  348. //check
  349. }
  350. int ReadWigan36(struct mem_inout* wg_data)
  351. {
  352. //read
  353. if(ReadWigan(36,wg_data)!=0)return -1;
  354. return -1;
  355. //check
  356. }
  357. int ReadWigan37(struct mem_inout* wg_data)
  358. {
  359. //read
  360. if(ReadWigan(37,wg_data)!=0)return -1;
  361. return -1;
  362. //check
  363. }
  364. int ReadWigan40(struct mem_inout* wg_data)
  365. {
  366. //read
  367. if(ReadWigan(40,wg_data)!=0)return -1;
  368. return -1;
  369. //check
  370. }
  371. int ReadWigan86(struct mem_inout* wg_data)
  372. {
  373. //read
  374. if(ReadWigan(86,wg_data)!=0)return -1;
  375. return -1;
  376. //check
  377. }
  378. char * wg26_0()
  379. {
  380. static char buf[9];
  381. struct mem_inout data;
  382. if(ReadWigan26(&data)!=0)return NULL;
  383. memset(buf,0,sizeof(buf));
  384. data.d[0]=data.d[0]&0xFFFFFF;
  385. sprintf((char *)buf,"%03d%05d",(data.d[0]&0XFF0000)>>16,data.d[0]&0XFFFF);
  386. return buf;
  387. }
  388. //3+3+18 bits -->1+1+6 Byte
  389. char * wg26_1()
  390. {
  391. static char buf[9];
  392. struct mem_inout data;
  393. if(ReadWigan26(&data)!=0)return NULL;
  394. memset(buf,0,sizeof(buf));
  395. data.d[0]=data.d[0]&0xFFFFFF;
  396. sprintf(buf,"%d%d%06d",(data.d[0]&0XE00000)>>21,(data.d[0]&0X1C0000)>>18,data.d[0]&0X3FFFF);
  397. return buf;
  398. }
  399. //4+20 bits --> 1+7 Byte ?? 4bits ->1111 -->15(2Byte)
  400. char * wg26_2()
  401. {
  402. static char buf[9];
  403. struct mem_inout data;
  404. memset(buf,0,sizeof(buf));
  405. if(ReadWigan26(&data)!=0)return NULL;
  406. memset(buf,0,sizeof(buf));
  407. data.d[0]=data.d[0]&0xFFFFFF;
  408. sprintf(buf,"%02u%07u",(data.d[0]&0xF00000)>>20,data.d[0]&0xFFFFF);
  409. return buf;
  410. }
  411. //24bits -->24Byte
  412. char * wg26_3()
  413. {
  414. static char buf[9];
  415. struct mem_inout data;
  416. if(ReadWigan26(&data)!=0)return NULL;
  417. memset(buf,0,sizeof(buf));
  418. data.d[0]=data.d[0]&0xFFFFFF;
  419. sprintf(buf,"%08d",data.d[0]);
  420. return buf;
  421. }
  422. char * wg27_0()
  423. {
  424. static char buf[10];
  425. struct mem_inout data;
  426. if(ReadWigan27(&data)!=0)return NULL;
  427. memset(buf,0,sizeof(buf));
  428. data.d[0]=data.d[0]&0x1FFFFFF;
  429. sprintf(buf,"%1d%03d%05d",(data.d[0]&0X1000000)>>24,(data.d[0]&0xFF0000)>>16,data.d[0]&0XFFFF);
  430. return buf;
  431. }
  432. //15+15 bits -->5+5=10 Bytes
  433. char * wg32_0()
  434. {
  435. static char buf[12];
  436. struct mem_inout data;
  437. if(ReadWigan27(&data)!=0)return NULL;
  438. memset(buf,0,sizeof(buf));
  439. data.d[0]=data.d[0]&0x3FFFFFFF;
  440. sprintf(buf,"%05d%05d",data.d[0]>>15,data.d[0]&0x7FFF);
  441. return buf;
  442. }
  443. //9+21 bits --->3+7=10 Bytes
  444. char * wg32_1()
  445. {
  446. static char buf[12];
  447. struct mem_inout data;
  448. if(ReadWigan27(&data)!=0)return NULL;
  449. memset(buf,0,sizeof(buf));
  450. data.d[0]=data.d[0]&0x1FFFFFF;
  451. sprintf(buf,"%03d%07d",data.d[0]>>21,data.d[0]&0X1FFFFF);
  452. return buf;
  453. }
  454. //7+24 bits --> 2+8 Byte
  455. char * wg33_0()
  456. {
  457. static char buf[16];
  458. struct mem_inout data;
  459. if(ReadWigan33(&data)!=0)return NULL;
  460. memset(buf,0,sizeof(buf));
  461. data.d[0]=data.d[0]&0x7FFFFFFF;
  462. sprintf(buf,"%02d%08d",data.d[0]>>24,data.d[0]&0X7F);
  463. return buf;
  464. }
  465. //10+21 bits -->4+7 Byte
  466. char * wg33_1()
  467. {
  468. static char buf[16];
  469. struct mem_inout data;
  470. if(ReadWigan33(&data)!=0)return NULL;
  471. memset(buf,0,sizeof(buf));
  472. data.d[0]=data.d[0]&0x7FFFFFFF;
  473. sprintf(buf,"%04d%07d",data.d[0]>>21,data.d[0]&0X3FF);
  474. return buf;
  475. }
  476. //32 bits --> 10 Byte
  477. char * wg34_0()
  478. {
  479. static char buf[16];
  480. struct mem_inout data;
  481. if(ReadWigan34(&data)!=0)return NULL;
  482. memset(buf,0,sizeof(buf));
  483. sprintf(buf,"%010d",data.d[0]);
  484. return buf;
  485. }
  486. //16+16bits --> 5+5 Byte
  487. char * wg34_1()
  488. {
  489. static char buf[16];
  490. struct mem_inout data;
  491. if(ReadWigan34(&data)!=0)return NULL;
  492. memset(buf,0,sizeof(buf));
  493. sprintf(buf,"%05d%05d",data.d[0]>>16,data.d[0]&0XFFFF);
  494. return buf;
  495. }
  496. //8+4+20 bits -->3+2+7 Byte
  497. char * wg34_2()
  498. {
  499. static char buf[16];
  500. struct mem_inout data;
  501. if(ReadWigan34(&data)!=0)return NULL;
  502. memset(buf,0,sizeof(buf));
  503. sprintf(buf,"%03d%02d%07d",data.d[0]>>24,(data.d[0]>>20)&0XF,data.d[0]&0XFFFFF);
  504. return buf;
  505. }
  506. //
  507. char * wg36_0()
  508. {
  509. static char buf[16];
  510. struct mem_inout data;
  511. if(ReadWigan36(&data)!=0)return NULL;
  512. return NULL;
  513. return buf;
  514. }
  515. //16+16+2 bits -->5+5+1=11 Byte
  516. char * wg36_1()
  517. {
  518. static char buf[16];
  519. struct mem_inout data;
  520. unsigned int tmp=0;
  521. if(ReadWigan36(&data)!=0)return NULL;
  522. data.d[1]=data.d[1]&0x3;
  523. tmp=(data.d[1]<<14)&(data.d[0]>>18);
  524. memset(buf,0,sizeof(buf));
  525. sprintf(buf,"%05d%05d%01d",tmp,(data.d[0]>>2)&0XFFFF,data.d[0]&0X3);
  526. return buf;
  527. }
  528. //14+20 bits --> 5+7 =12 Byte
  529. char * wg36_2()
  530. {
  531. static char buf[16];
  532. struct mem_inout data;
  533. unsigned int tmp;
  534. if(ReadWigan36(&data)!=0)return NULL;
  535. data.d[1]=data.d[1]&0x3;
  536. tmp=(data.d[1]<<12)&(data.d[0]>>20);
  537. memset(buf,0,sizeof(buf));
  538. sprintf(buf,"%05d%07d",tmp,data.d[0]&0XFFFFF);
  539. return buf;
  540. }
  541. char *wg37_0()
  542. {
  543. return NULL;
  544. }
  545. char *wg37_1()
  546. {
  547. return NULL;
  548. }
  549. char *wg37_2()
  550. {
  551. return NULL;
  552. }
  553. char *wg37_3()
  554. {
  555. return NULL;
  556. }
  557. char *wg37_4()
  558. {
  559. return NULL;
  560. }
  561. char *wg37_5()
  562. {
  563. return NULL;
  564. }
  565. char *wg37_6()
  566. {
  567. return NULL;
  568. }
  569. char *wg37_7()
  570. {
  571. return NULL;
  572. }
  573. char *wg40_0()
  574. {
  575. return NULL;
  576. }
  577. char *wg40_1()
  578. {
  579. return NULL;
  580. }
  581. char *wg40_2()
  582. {
  583. return NULL;
  584. }
  585. char *wg86_0()
  586. {
  587. return NULL;
  588. }
  589. //linear(13 Decimal char)
  590. int card_linearhandle(char *cardsno,char*param)
  591. {
  592. // char sno[128]
  593. int len=0;
  594. char tmpsno[128];
  595. unsigned long long cardno=0,cardno1;
  596. if(cardsno==NULL||strlen(cardsno)==0) return FALSE;
  597. len=strlen(cardsno);
  598. memcpy(tmpsno,cardsno,2);
  599. cardno1=strtoull(tmpsno,NULL,16);
  600. cardno=strtoull(&cardsno[2],NULL,16);
  601. sprintf(param,"%03llu%010llu",cardno1,cardno);
  602. // memset(sno,0,sizeof(sno));
  603. // sprintf(sno,"%014llo",cardno);
  604. // printf("tmp=%s\n",sno);
  605. return TRUE;
  606. }
  607. //syner card no.(14 Decimal char)
  608. int card_synelhandle(char *cardsno,char*param)
  609. {
  610. char sno[128];
  611. int i;
  612. // char tmpsno[128];
  613. // int len=0;
  614. unsigned long long cardno=0;
  615. if(cardsno==NULL||strlen(cardsno)==0) return FALSE;
  616. cardno=strtoull(cardsno,NULL,16);
  617. memset(sno,0,sizeof(sno));
  618. sprintf(sno,"%014llo",cardno);
  619. for(i=0;i<14;i++)
  620. {
  621. param[i]=sno[13-i];
  622. }
  623. return TRUE;
  624. }
  625. //ReadOnly-card
  626. char *card_handle(char *p)
  627. {
  628. static char sno[126];
  629. char tmpsno[126];
  630. unsigned long cardno=0;
  631. if(p==NULL||strlen(p)==0) return NULL;
  632. memset(sno, 0, sizeof(sno));
  633. strcpy(sno,p);
  634. // sprintf(sno, "%02X%02X%02X%02X",*p++,*p++,*p++,*p++);
  635. if(cardinfo.card_fs==0) //first cut ,then convert (disable)
  636. {
  637. if(cardinfo.card_10!=0) //decimal
  638. {
  639. memset(tmpsno,0,sizeof(tmpsno));
  640. right(sno,8);
  641. strcpy(tmpsno,sno);
  642. memset(sno,0,sizeof(sno));
  643. sprintf(sno,"%lu",strtoul(tmpsno,NULL,16));
  644. }
  645. if(cardinfo.card_right!=0) //cut the card number according to setting
  646. {
  647. if(cardinfo.card_10)
  648. {
  649. cardno=strtoul(sno,NULL,10);
  650. memset(sno,0,sizeof(sno));
  651. sprintf(sno,"%0*lu",cardinfo.card_right,cardno);
  652. }
  653. else
  654. {
  655. cardno=strtoul(sno,NULL,16);
  656. memset(sno,0,sizeof(sno));
  657. sprintf(sno,"%0*lX",cardinfo.card_right,cardno);
  658. }
  659. right(sno,cardinfo.card_right);
  660. }
  661. }
  662. else // enable
  663. {
  664. if(cardinfo.card_right!=0) //cut the card number according to setting
  665. {
  666. right(sno,cardinfo.card_right);
  667. }
  668. if(cardinfo.card_10!=0) // convert to decimal
  669. {
  670. memset(tmpsno,0,sizeof(tmpsno));
  671. right(sno,8);
  672. strcpy(tmpsno,sno);
  673. memset(sno,0,sizeof(sno));
  674. sprintf(sno,"%lu",strtoul(tmpsno,NULL,16));
  675. if(cardinfo.card_right10!=0) // decimal bits num
  676. {
  677. right(sno,cardinfo.card_right10);
  678. cutzero(sno);
  679. }
  680. }
  681. }
  682. return sno;
  683. }
  684. /*
  685. sno:card number
  686. rightnum:numbers of bits to cut
  687. */
  688. void right(char * sno,int rightnum)
  689. {
  690. char tmpsno[1024];
  691. int len,j;
  692. char *p1;
  693. if(sno==NULL||rightnum<0) return;
  694. memset(tmpsno,0,sizeof(tmpsno));
  695. len=strlen(sno);
  696. snprintf(tmpsno,"%s",sno,1024);
  697. //strncpy(tmpsno,sno,1024);
  698. p1=tmpsno;
  699. j=len-rightnum;
  700. if(j<0)
  701. return;
  702. p1+=j;
  703. if(len>1024)len=1024;
  704. memset(sno,0,len);
  705. snprintf(sno,len,"%s",p1);
  706. //strncpy(sno,p1,len);
  707. return;
  708. }
  709. //cut '0' character
  710. void cutzero(char *src)
  711. {
  712. char *ptr,*str;
  713. int flag=0;
  714. ptr=str=src;
  715. while(*str)
  716. {
  717. if((flag)||(*str!='0'))
  718. {
  719. *ptr++=*str++;
  720. flag=1;
  721. }
  722. else str++;
  723. }
  724. *ptr='\0';
  725. }
  726. //read sector value
  727. int MF1Read(char *cardsno,char *param)
  728. {
  729. int i,n,j,flag=1,len=0;
  730. unsigned char buf[1024];
  731. if(ReadCardInfo.enable==0) return -1;
  732. if(ReadCardInfo.beg_sectors<0||ReadCardInfo.end_sectors>15||ReadCardInfo.beg_block<0||ReadCardInfo.end_block>2)
  733. return -1;
  734. memset(buf,0,sizeof(buf));
  735. for(n=ReadCardInfo.beg_sectors;n<=ReadCardInfo.end_sectors;n++)
  736. {
  737. for(j=ReadCardInfo.beg_block;j<=ReadCardInfo.end_block;)
  738. {
  739. if(n==0&&j==0) {j++;continue;}
  740. // usleep(overtime);
  741. if(flag)
  742. {
  743. if(mifs_read(cardsno,n,j,&buf[len],ReadCardInfo.keymode,ReadCardInfo.Bkey)==FALSE)
  744. return -1;
  745. flag=0;
  746. j++;
  747. }
  748. else
  749. {
  750. if(mifs_read(cardsno,n,j,&buf[len],ReadCardInfo.keymode,NULL)==FALSE) return -1;
  751. flag=0;
  752. j++;
  753. }
  754. len=len+16;
  755. }
  756. }
  757. for(i=0;i<len;i++)
  758. {
  759. param[i]=(int)buf[i];
  760. }
  761. // gsmBytes2String(buf,sno,len);
  762. return len;
  763. }
  764. //write data to sector
  765. int MF1Write(char *cardsno, char*value)
  766. {
  767. int n,j,flag=1,len=0;
  768. char tmp[2048];
  769. unsigned char buf[26];
  770. if(WriteCardInfo.enable==0) return FALSE;
  771. if(WriteCardInfo.beg_sectors<0||WriteCardInfo.end_sectors>15||WriteCardInfo.beg_block<0||WriteCardInfo.end_block>2)
  772. return FALSE;
  773. snprintf(tmp,1024,"%s",value);
  774. //strncpy(tmp,value,1024);
  775. for(n=WriteCardInfo.beg_sectors;n<=WriteCardInfo.end_sectors;n++)
  776. {
  777. for(j=WriteCardInfo.beg_block;j<=WriteCardInfo.end_block;)
  778. {
  779. if(n==0&&j==0) {j++;continue;}
  780. memset(buf,0,sizeof(buf));
  781. memcpy(buf,&tmp[len],16);
  782. if(flag)
  783. {
  784. if(mifs_write(cardsno,n,j,buf,WriteCardInfo.keymode,WriteCardInfo.Bkey)==1)
  785. return FALSE;
  786. printf("\nwrite rigth\n");
  787. flag=0;
  788. j++;
  789. }
  790. else
  791. {
  792. if(mifs_write(cardsno,n,j,buf,WriteCardInfo.keymode,NULL)==FALSE) return FALSE;
  793. printf("\nwrite rigth\n");
  794. flag=0;
  795. j++;
  796. }
  797. len=len+16;
  798. }
  799. }
  800. return TRUE;
  801. }