PageRenderTime 66ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/card/readcard-old.c

https://bitbucket.org/hkhateb/v1_libfunc_interface_s.c
C | 709 lines | 522 code | 91 blank | 96 comment | 72 complexity | 65c0834f0edcdc07155a8c34707aaaaa 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 : Failure 0; Success card type
  51. *---------------------------------------------------------------------------*/
  52. int ReadCard(char *cardnum)
  53. {
  54. unsigned char *card=NULL,buf[32];
  55. static char sno[128];
  56. unsigned int wgdata=0;
  57. int i=0,len=0;
  58. memset(sno, 0, sizeof(sno));
  59. switch(com1value.instruction)
  60. {
  61. case READEMCARD:
  62. card = com1value.user_data;
  63. sprintf(sno, "%02X%02X%02X%02X%02X",*card++,*card++,*card++,*card++,*card++);
  64. // p=card_handle(sno);
  65. break;
  66. case READMF1CARD:
  67. card = com1value.user_data;
  68. memset(buf,0,sizeof(buf));
  69. sprintf(buf, "%02X%02X%02X%02X",*card++,*card++,*card++,*card++);
  70. sprintf(sno,"%lu",strtoul(buf,NULL,16));
  71. //
  72. break;
  73. case READMAGNETIC:
  74. if((int)com1value.address==cardinfo.track){
  75. memset(sno, 0, sizeof(sno));
  76. // len=(int)com1value.nbytes;
  77. while(isxdigit(com1value.user_data[i]))
  78. {
  79. sno[i]=(int)com1value.user_data[i];
  80. i++;
  81. }
  82. sno[i] = '\0';
  83. }
  84. else return 0;
  85. break;
  86. case READBARCODE:
  87. memset(sno, 0, sizeof(sno));
  88. len=(int)com1value.nbytes;
  89. i=0;
  90. len--;
  91. while(len--)
  92. {
  93. sno[i]=(int)com1value.user_data[i+1];
  94. i++;
  95. }
  96. sno[i] = '\0';
  97. default:
  98. /* wgdata=ReadWigan();
  99. if(wgdata<=0) break;
  100. sprintf(sno,"%06X",(wgdata<<7)>>8); //cut the first and the last bytes
  101. if(cardinfo.card_right!=0) //cut the card number according to setting
  102. {
  103. memset(sno,0,sizeof(sno));
  104. sprintf(sno,"%10X",(wgdata<<7)>>8);//cut the first and the last bytes
  105. right(sno,cardinfo.card_right);
  106. }
  107. com1value.instruction=WEIGANDCARD;
  108. */
  109. break;
  110. }
  111. if(sno==NULL||strlen(sno)==0) return com1value.instruction;
  112. // printf("p=%s\n",sno);
  113. //memcpy(cardnum,sno,strlen(sno));
  114. strcpy(cardnum,sno);
  115. return com1value.instruction;
  116. }
  117. //8+6 bits -->3+5 Bytes
  118. int ReadWigan26(struct mem_inout* wg_data)
  119. {
  120. unsigned int count1=0,count2=0;
  121. unsigned int i,begin=0,end=0,c=0;
  122. //read
  123. if(ReadWigan(26,wg_data)!=0)return -1;
  124. //printf("wg26 success\n");
  125. //even-odd check
  126. for(i=0;i<26;i++)
  127. {
  128. c=1<<i;
  129. if(c&wg_data->d[0]){
  130. if(i==0)begin=1;
  131. else if(i<13)count1++;
  132. else if(i<25)count2++;
  133. else if(i==25)end=1;
  134. }
  135. }
  136. //cut Hight and Low bit
  137. wg_data->d[0]=(wg_data->d[0]&0X1FFFFFE)>>1;
  138. if(count1%2!=begin&&count2%2==end)return 0;
  139. else return -1;
  140. }
  141. int ReadWigan27(struct mem_inout* wg_data)
  142. {
  143. //read
  144. if(ReadWigan(27,wg_data)!=0)return -1;
  145. return -1;
  146. //check
  147. }
  148. int ReadWigan32(struct mem_inout* wg_data)
  149. {
  150. //read
  151. if(ReadWigan(32,wg_data)!=0)return -1;
  152. return -1;
  153. //check
  154. }
  155. int ReadWigan33(struct mem_inout* wg_data)
  156. {
  157. //read
  158. if(ReadWigan(33,wg_data)!=0)return -1;
  159. return -1;
  160. //check
  161. }
  162. int ReadWigan34(struct mem_inout* wg_data)
  163. {
  164. //read
  165. if(ReadWigan(34,wg_data)!=0)return -1;
  166. return -1;
  167. //check
  168. }
  169. int ReadWigan36(struct mem_inout* wg_data)
  170. {
  171. //read
  172. if(ReadWigan(36,wg_data)!=0)return -1;
  173. return -1;
  174. //check
  175. }
  176. int ReadWigan37(struct mem_inout* wg_data)
  177. {
  178. //read
  179. if(ReadWigan(37,wg_data)!=0)return -1;
  180. return -1;
  181. //check
  182. }
  183. int ReadWigan40(struct mem_inout* wg_data)
  184. {
  185. //read
  186. if(ReadWigan(40,wg_data)!=0)return -1;
  187. return -1;
  188. //check
  189. }
  190. int ReadWigan86(struct mem_inout* wg_data)
  191. {
  192. //read
  193. if(ReadWigan(86,wg_data)!=0)return -1;
  194. return -1;
  195. //check
  196. }
  197. char * wg26_0()
  198. {
  199. static char buf[9];
  200. struct mem_inout data;
  201. if(ReadWigan26(&data)!=0)return NULL;
  202. memset(buf,0,sizeof(buf));
  203. data.d[0]=data.d[0]&0xFFFFFF;
  204. sprintf((char *)buf,"%03d%05d",(data.d[0]&0XFF0000)>>16,data.d[0]&0XFFFF);
  205. return buf;
  206. }
  207. //3+3+18 bits -->1+1+6 Byte
  208. char * wg26_1()
  209. {
  210. static char buf[9];
  211. struct mem_inout data;
  212. if(ReadWigan26(&data)!=0)return NULL;
  213. memset(buf,0,sizeof(buf));
  214. data.d[0]=data.d[0]&0xFFFFFF;
  215. sprintf(buf,"%d%d%06d",(data.d[0]&0XE00000)>>21,(data.d[0]&0X1C0000)>>18,data.d[0]&0X3FFFF);
  216. return buf;
  217. }
  218. //4+20 bits --> 1+7 Byte ?? 4bits ->1111 -->15(2Byte)
  219. char * wg26_2()
  220. {
  221. static char buf[9];
  222. struct mem_inout data;
  223. memset(buf,0,sizeof(buf));
  224. if(ReadWigan26(&data)!=0)return NULL;
  225. memset(buf,0,sizeof(buf));
  226. data.d[0]=data.d[0]&0xFFFFFF;
  227. sprintf(buf,"%02u%07u",(data.d[0]&0xF00000)>>20,data.d[0]&0xFFFFF);
  228. return buf;
  229. }
  230. //24bits -->24Byte
  231. char * wg26_3()
  232. {
  233. static char buf[9];
  234. struct mem_inout data;
  235. if(ReadWigan26(&data)!=0)return NULL;
  236. memset(buf,0,sizeof(buf));
  237. data.d[0]=data.d[0]&0xFFFFFF;
  238. sprintf(buf,"%08d",data.d[0]);
  239. return buf;
  240. }
  241. char * wg27_0()
  242. {
  243. static char buf[10];
  244. struct mem_inout data;
  245. if(ReadWigan27(&data)!=0)return NULL;
  246. memset(buf,0,sizeof(buf));
  247. data.d[0]=data.d[0]&0x1FFFFFF;
  248. sprintf(buf,"%1d%03d%05d",(data.d[0]&0X1000000)>>24,(data.d[0]&0xFF0000)>>16,data.d[0]&0XFFFF);
  249. return buf;
  250. }
  251. //15+15 bits -->5+5=10 Bytes
  252. char * wg32_0()
  253. {
  254. static char buf[12];
  255. struct mem_inout data;
  256. if(ReadWigan27(&data)!=0)return NULL;
  257. memset(buf,0,sizeof(buf));
  258. data.d[0]=data.d[0]&0x3FFFFFFF;
  259. sprintf(buf,"%05d%05d",data.d[0]>>15,data.d[0]&0x7FFF);
  260. return buf;
  261. }
  262. //9+21 bits --->3+7=10 Bytes
  263. char * wg32_1()
  264. {
  265. static char buf[12];
  266. struct mem_inout data;
  267. if(ReadWigan27(&data)!=0)return NULL;
  268. memset(buf,0,sizeof(buf));
  269. data.d[0]=data.d[0]&0x1FFFFFF;
  270. sprintf(buf,"%03d%07d",data.d[0]>>21,data.d[0]&0X1FFFFF);
  271. return buf;
  272. }
  273. //7+24 bits --> 2+8 Byte
  274. char * wg33_0()
  275. {
  276. static char buf[16];
  277. struct mem_inout data;
  278. if(ReadWigan33(&data)!=0)return NULL;
  279. memset(buf,0,sizeof(buf));
  280. data.d[0]=data.d[0]&0x7FFFFFFF;
  281. sprintf(buf,"%02d%08d",data.d[0]>>24,data.d[0]&0X7F);
  282. return buf;
  283. }
  284. //10+21 bits -->4+7 Byte
  285. char * wg33_1()
  286. {
  287. static char buf[16];
  288. struct mem_inout data;
  289. if(ReadWigan33(&data)!=0)return NULL;
  290. memset(buf,0,sizeof(buf));
  291. data.d[0]=data.d[0]&0x7FFFFFFF;
  292. sprintf(buf,"%04d%07d",data.d[0]>>21,data.d[0]&0X3FF);
  293. return buf;
  294. }
  295. //32 bits --> 10 Byte
  296. char * wg34_0()
  297. {
  298. static char buf[16];
  299. struct mem_inout data;
  300. if(ReadWigan34(&data)!=0)return NULL;
  301. memset(buf,0,sizeof(buf));
  302. sprintf(buf,"%010d",data.d[0]);
  303. return buf;
  304. }
  305. //16+16bits --> 5+5 Byte
  306. char * wg34_1()
  307. {
  308. static char buf[16];
  309. struct mem_inout data;
  310. if(ReadWigan34(&data)!=0)return NULL;
  311. memset(buf,0,sizeof(buf));
  312. sprintf(buf,"%05d%05d",data.d[0]>>16,data.d[0]&0XFFFF);
  313. return buf;
  314. }
  315. //8+4+20 bits -->3+2+7 Byte
  316. char * wg34_2()
  317. {
  318. static char buf[16];
  319. struct mem_inout data;
  320. if(ReadWigan34(&data)!=0)return NULL;
  321. memset(buf,0,sizeof(buf));
  322. sprintf(buf,"%03d%02d%07d",data.d[0]>>24,(data.d[0]>>20)&0XF,data.d[0]&0XFFFFF);
  323. return buf;
  324. }
  325. //
  326. char * wg36_0()
  327. {
  328. static char buf[16];
  329. struct mem_inout data;
  330. if(ReadWigan36(&data)!=0)return NULL;
  331. return NULL;
  332. return buf;
  333. }
  334. //16+16+2 bits -->5+5+1=11 Byte
  335. char * wg36_1()
  336. {
  337. static char buf[16];
  338. struct mem_inout data;
  339. unsigned int tmp=0;
  340. if(ReadWigan36(&data)!=0)return NULL;
  341. data.d[1]=data.d[1]&0x3;
  342. tmp=(data.d[1]<<14)&(data.d[0]>>18);
  343. memset(buf,0,sizeof(buf));
  344. sprintf(buf,"%05d%05d%01d",tmp,(data.d[0]>>2)&0XFFFF,data.d[0]&0X3);
  345. return buf;
  346. }
  347. //14+20 bits --> 5+7 =12 Byte
  348. char * wg36_2()
  349. {
  350. static char buf[16];
  351. struct mem_inout data;
  352. unsigned int tmp;
  353. if(ReadWigan36(&data)!=0)return NULL;
  354. data.d[1]=data.d[1]&0x3;
  355. tmp=(data.d[1]<<12)&(data.d[0]>>20);
  356. memset(buf,0,sizeof(buf));
  357. sprintf(buf,"%05d%07d",tmp,data.d[0]&0XFFFFF);
  358. return buf;
  359. }
  360. char *wg37_0()
  361. {
  362. return NULL;
  363. }
  364. char *wg37_1()
  365. {
  366. return NULL;
  367. }
  368. char *wg37_2()
  369. {
  370. return NULL;
  371. }
  372. char *wg37_3()
  373. {
  374. return NULL;
  375. }
  376. char *wg37_4()
  377. {
  378. return NULL;
  379. }
  380. char *wg37_5()
  381. {
  382. return NULL;
  383. }
  384. char *wg37_6()
  385. {
  386. return NULL;
  387. }
  388. char *wg37_7()
  389. {
  390. return NULL;
  391. }
  392. char *wg40_0()
  393. {
  394. return NULL;
  395. }
  396. char *wg40_1()
  397. {
  398. return NULL;
  399. }
  400. char *wg40_2()
  401. {
  402. return NULL;
  403. }
  404. char *wg86_0()
  405. {
  406. return NULL;
  407. }
  408. //linear(13 Decimal char)
  409. int card_linearhandle(char *cardsno,char*param)
  410. {
  411. // char sno[128]
  412. int len=0;
  413. char tmpsno[128];
  414. unsigned long long cardno=0,cardno1;
  415. if(cardsno==NULL||strlen(cardsno)==0) return FALSE;
  416. len=strlen(cardsno);
  417. memcpy(tmpsno,cardsno,2);
  418. cardno1=strtoull(tmpsno,NULL,16);
  419. cardno=strtoull(&cardsno[2],NULL,16);
  420. sprintf(param,"%03llu%010llu",cardno1,cardno);
  421. // memset(sno,0,sizeof(sno));
  422. // sprintf(sno,"%014llo",cardno);
  423. // printf("tmp=%s\n",sno);
  424. return TRUE;
  425. }
  426. //syner card no.(14 Decimal char)
  427. int card_synelhandle(char *cardsno,char*param)
  428. {
  429. char sno[128];
  430. int i;
  431. // char tmpsno[128];
  432. // int len=0;
  433. unsigned long long cardno=0;
  434. if(cardsno==NULL||strlen(cardsno)==0) return FALSE;
  435. cardno=strtoull(cardsno,NULL,16);
  436. memset(sno,0,sizeof(sno));
  437. sprintf(sno,"%014llo",cardno);
  438. for(i=0;i<14;i++)
  439. {
  440. param[i]=sno[13-i];
  441. }
  442. param[i] = '\0';
  443. return TRUE;
  444. }
  445. //ReadOnly-card
  446. char *card_handle(char *p)
  447. {
  448. static char sno[126];
  449. char tmpsno[126];
  450. unsigned long cardno=0;
  451. if(p==NULL||strlen(p)==0) return NULL;
  452. memset(sno, 0, sizeof(sno));
  453. strcpy(sno,p);
  454. // sprintf(sno, "%02X%02X%02X%02X",*p++,*p++,*p++,*p++);
  455. if(cardinfo.card_fs==0) //first cut ,then convert (disable)
  456. {
  457. if(cardinfo.card_10!=0) //decimal
  458. {
  459. memset(tmpsno,0,sizeof(tmpsno));
  460. right(sno,8);
  461. strcpy(tmpsno,sno);
  462. memset(sno,0,sizeof(sno));
  463. sprintf(sno,"%lu",strtoul(tmpsno,NULL,16));
  464. }
  465. if(cardinfo.card_right!=0) //cut the card number according to setting
  466. {
  467. if(cardinfo.card_10)
  468. {
  469. cardno=strtoul(sno,NULL,10);
  470. memset(sno,0,sizeof(sno));
  471. sprintf(sno,"%0*lu",cardinfo.card_right,cardno);
  472. }
  473. else
  474. {
  475. cardno=strtoul(sno,NULL,16);
  476. memset(sno,0,sizeof(sno));
  477. sprintf(sno,"%0*lX",cardinfo.card_right,cardno);
  478. }
  479. right(sno,cardinfo.card_right);
  480. }
  481. }
  482. else // enable
  483. {
  484. if(cardinfo.card_right!=0) //cut the card number according to setting
  485. {
  486. right(sno,cardinfo.card_right);
  487. }
  488. if(cardinfo.card_10!=0) // convert to decimal
  489. {
  490. memset(tmpsno,0,sizeof(tmpsno));
  491. right(sno,8);
  492. strcpy(tmpsno,sno);
  493. memset(sno,0,sizeof(sno));
  494. sprintf(sno,"%lu",strtoul(tmpsno,NULL,16));
  495. if(cardinfo.card_right10!=0) // decimal bits num
  496. {
  497. right(sno,cardinfo.card_right10);
  498. cutzero(sno);
  499. }
  500. }
  501. }
  502. return sno;
  503. }
  504. /*
  505. sno:card number
  506. rightnum:numbers of bits to cut
  507. */
  508. void right(char * sno,int rightnum)
  509. {
  510. char tmpsno[1024];
  511. int len,j;
  512. char *p1;
  513. if(sno==NULL||rightnum<0) return;
  514. memset(tmpsno,0,sizeof(tmpsno));
  515. len=strlen(sno);
  516. strncpy(tmpsno,sno,1024);
  517. p1=tmpsno;
  518. j=len-rightnum;
  519. if(j<0)
  520. return;
  521. p1+=j;
  522. if(len>1024)len=1024;
  523. memset(sno,0,len);
  524. strncpy(sno,p1,len);
  525. return;
  526. }
  527. //cut '0' character
  528. void cutzero(char *src)
  529. {
  530. char *ptr,*str;
  531. int flag=0;
  532. ptr=str=src;
  533. while(*str)
  534. {
  535. if((flag)||(*str!='0'))
  536. {
  537. *ptr++=*str++;
  538. flag=1;
  539. }
  540. else str++;
  541. }
  542. *ptr='\0';
  543. }
  544. //read sector value
  545. int MF1Read(char *cardsno,char *param)
  546. {
  547. int i,n,j,flag=1,len=0;
  548. unsigned char buf[1024];
  549. if(ReadCardInfo.enable==0) return -1;
  550. if(ReadCardInfo.beg_sectors<0||ReadCardInfo.end_sectors>15||ReadCardInfo.beg_block<0||ReadCardInfo.end_block>2)
  551. return -1;
  552. memset(buf,0,sizeof(buf));
  553. for(n=ReadCardInfo.beg_sectors;n<=ReadCardInfo.end_sectors;n++)
  554. {
  555. for(j=ReadCardInfo.beg_block;j<=ReadCardInfo.end_block;)
  556. {
  557. if(n==0&&j==0) {j++;continue;}
  558. // usleep(overtime);
  559. if(flag)
  560. {
  561. if(mifs_read(cardsno,n,j,&buf[len],ReadCardInfo.keymode,ReadCardInfo.Bkey)==FALSE)
  562. return -1;
  563. flag=0;
  564. j++;
  565. }
  566. else
  567. {
  568. if(mifs_read(cardsno,n,j,&buf[len],ReadCardInfo.keymode,NULL)==FALSE) return -1;
  569. flag=0;
  570. j++;
  571. }
  572. len=len+16;
  573. }
  574. }
  575. for(i=0;i<len;i++)
  576. {
  577. param[i]=(int)buf[i];
  578. }
  579. // gsmBytes2String(buf,sno,len);
  580. return len;
  581. }
  582. //write data to sector
  583. int MF1Write(char *cardsno, char*value)
  584. {
  585. int n,j,flag=1,len=0;
  586. char tmp[2048];
  587. unsigned char buf[26];
  588. if(WriteCardInfo.enable==0) return FALSE;
  589. if(WriteCardInfo.beg_sectors<0||WriteCardInfo.end_sectors>15||WriteCardInfo.beg_block<0||WriteCardInfo.end_block>2)
  590. return FALSE;
  591. strncpy(tmp,value,1024);
  592. for(n=WriteCardInfo.beg_sectors;n<=WriteCardInfo.end_sectors;n++)
  593. {
  594. for(j=WriteCardInfo.beg_block;j<=WriteCardInfo.end_block;)
  595. {
  596. if(n==0&&j==0) {j++;continue;}
  597. memset(buf,0,sizeof(buf));
  598. memcpy(buf,&tmp[len],16);
  599. if(flag)
  600. {
  601. if(mifs_write(cardsno,n,j,buf,WriteCardInfo.keymode,WriteCardInfo.Bkey)==1)
  602. return FALSE;
  603. printf("\nwrite rigth\n");
  604. flag=0;
  605. j++;
  606. }
  607. else
  608. {
  609. if(mifs_write(cardsno,n,j,buf,WriteCardInfo.keymode,NULL)==FALSE) return FALSE;
  610. printf("\nwrite rigth\n");
  611. flag=0;
  612. j++;
  613. }
  614. len=len+16;
  615. }
  616. }
  617. return TRUE;
  618. }