tag_stat read_tag_str(uint8_t addr, char * str) { tag_stat tmp; char *p; uint8_t buff[MAXRLEN]; int i = 16; tmp=PcdRead(addr,buff); p=str; if (tmp==TAG_OK) { for (i=16;i>=0;i--) { sprintf(p,"%02x",(char)buff[i]); p+=2; } }else if (tmp==TAG_ERRCRC){ sprintf(p,"CRC Error"); }else{ sprintf(p,"Unknown error"); } return tmp; }
/**************************************************************************************************** *函数名称: rc522_read *函数参数: filp: 文件指针 * buf: 数据读取存储数组 * size: 需要读取的数据长度 * pos: 文件偏移 *函数返回值: 成功返回0 错误返回-1 *函数描述: spi读取数据函数 ****************************************************************************************************/ static ssize_t rc522_read(struct file *filp, char __user * buf, size_t size, loff_t *pos) { int ret = 0; #if 0 uint8_t read_buf[20] = {0}; /* 读取数据暂存数组 */ switch (g_read_cmd) { case RC522_Request_CMD: ret = PcdRequest(PICC_REQALL, read_buf); /* 寻找卡 */ printk("PcdRequest = %d\r\n",ret); if (ret < 0) printk(KERN_ERR "PcdRequest error!\r\n"); break; case RC522_Anticoll_CMD: ret = PcdAnticoll(read_buf);//防冲撞 if (ret != MI_OK) printk(KERN_ERR "PcdAnticoll error!\r\n"); copy_to_user( buf, read_buf, size ); break; case RC522_Read_CMD: ret = PcdRead( read_block_num, read_buf );//读块 if (ret < 0) printk(KERN_ERR "PcdRead error!\r\n"); copy_to_user( buf, read_buf, size ); default: printk(KERN_ERR "cmd is Invalid!\r\n"); } #endif return ret; }
/**************************************************************************************** ** 函数原型: void M1ReadVal(void) ** 函数功能: 读取钱包值 ** 入口参数: - ** 出口参数: - ** 返 回 值: - ** 描 述: ****************************************************************************************/ void M1ReadVal(void) { char status; status = PcdAuthentication(c_RecvBuf[4], c_RecvBuf[5], &c_RecvBuf[6], c_CardSrlNum); if (MI_OK == status) //验证密钥 { if (MI_OK == PcdRead(c_RecvBuf[5],&c_RecvBuf[14])) //读块数据 { status = 0; } else { status = 0x03; } } else { status = 0x02; } COM_Send(&c_RecvBuf[14],0x04,status); }
/**************************************************************************************** ** 函数原型: void M1Read(void) ** 函数功能: 读卡块值 ** 入口参数: - ** 出口参数: - ** 返 回 值: - ** 描 述: ****************************************************************************************/ void M1Read(void) { char status; status = PcdAuthentication(c_RecvBuf[4], c_RecvBuf[5], &c_RecvBuf[6], c_CardSrlNum); if (status == MI_OK) { //密钥验证成功 if (MI_OK == PcdRead(c_RecvBuf[5],&c_RecvBuf[14])) { status = 0; //读卡成功 } else { status = 0x03; //读卡失败 } } else { status = 0x02; //密钥验证失败 } COM_Send(&c_RecvBuf[14],0x10,status); }
/******************************************************************************* * 描 述 : 对寻卡、防冲撞、选卡、发送卡号、读卡、写卡、修改密码进行操作 成功则LED1灯亮 * 输 入 : 无 * 输 出 : 无 * 返 回 : 无 *******************************************************************************/ void ctrlprocess(void) { unsigned char ii; char status; PcdReset(); status=PcdRequest(PICC_REQIDL,&RevBuffer[0]); //寻天线区内未进入休眠状态的卡,返回卡片类型 2字节 if(status!=MI_OK) { return; } status=PcdAnticoll(&RevBuffer[2]); //防冲撞,返回卡的序列号 4字节 if(status!=MI_OK) return; memcpy(MLastSelectedSnr,&RevBuffer[2],4); status=PcdSelect(MLastSelectedSnr); //选卡 if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } if(oprationcard==KEYCARD) //修改密码 { oprationcard=0; status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr); if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } status=PcdWrite(KuaiN,&NewKey[0]); if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } PcdHalt(); InfoPrintf("modify password success\r\n"); } else if(oprationcard==READCARD) //读卡 { oprationcard=0; status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr); //验证A密匙 if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } status=PcdRead(KuaiN,Read_Data); if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } DebugPrintf(OTHER_DBG,"read card key: "); debug_printf(OTHER_DBG,(unsigned char*)Read_Data,16); PcdHalt(); port_data[port_id*5 + 0] = port_id + 1 +'0'; port_data[port_id*5 + 1] = (Read_Data[0] / 10) % 10 + '0' ; port_data[port_id*5 + 2] = (Read_Data[0] % 10) + '0'; port_data[port_id*5 + 3] = 0x0d; port_data[port_id*5 + 4] = 0x0a; } else if(oprationcard==WRITECARD) //写卡 { oprationcard=0; status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr);// if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } status=PcdWrite(KuaiN,&WriteData[0]); if(status!=MI_OK) { InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__); return; } PcdHalt(); InfoPrintf("write card success\r\n"); } else if(oprationcard==SENDID) //发送卡号 { oprationcard=0; for(ii=0;ii<4;ii++) cardID[ii] = MLastSelectedSnr[ii]; InfoPrintf("send car id: "); debug_printf(NORM_DBG,(unsigned char*)cardID,4); } }
int main(int argc, char *argv[]) { uid_t uid; uint8_t SN[10]; uint16_t CType=0; uint8_t SN_len=0; char status; int tmp,i; char str[255]; char *p; char sn_str[23]; pid_t child; int max_page=0; uint8_t page_step=0; FILE * fmem_str; char save_mem=0; char fmem_path[255]; uint8_t use_gpio=0; uint8_t gpio=255; uint32_t spi_speed=10000000L; int fd = -1 ; //by myself unsigned char blockaddr = (unsigned char)atoi(argv[1]); unsigned char key_passwd = (unsigned char)atoi(argv[2]) ; int n = 0; unsigned char statu = 0; unsigned char data_test[16]; for(n = 0; n < 6; n++) { data_test[n] = 0x00; } data_test[6] = 0xff ; data_test[7] = 0x07; data_test[8] = 0x80 ; data_test[9] = 0x69 ; for(n=10;n<16;n++){ data_test[n] = 0xFF ; } #if 1 if(argc < 3) { printf("command error and try again like this : ./a.out 1 \n"); return -1; } #endif if( (fd = spi_open() )< 0) return -1 ; /* * spi mode */ unsigned char mode = 3; // unsigned char mode = 1; unsigned char mode_t ; unsigned char bits = 8 ; unsigned int speed = 120000000 ; int ret = -1 ; ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) printf("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode_t); if (ret == -1) printf("can't get spi mode"); /* * bits per word */ ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) printf("can't set bits per word"); unsigned char bits_t ; ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits_t); if (ret == -1) printf("can't get bits per word"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) printf("can't set max speed hz"); unsigned int speed_t ; ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed_t); if (ret == -1) printf("can't get max speed hz"); printf("spidev hw info : mode:%d bits:%d speed:%d\n",mode_t,bits_t,speed_t) ; //many changes in InitRc522 function, please check it. printf("we will read block [%d] value !\n",blockaddr); printf("rc522 is soft_rest ....\n"); PcdReset(); #if 1 printf("rc522 open antenna ... \n"); usleep(100*1000); PcdAntennaOn(); memset(Tmpbuff,0,20); printf("now find tag .....\n"); while(1) { statu = PcdRequest(PICC_REQALL,Tmpbuff); if(statu == TAG_OK) { printf("find the tag and the tag type is :"); for(n = 0; n < (sizeof(Tmpbuff) / sizeof(Tmpbuff[0])); n++) { printf("0x%x ",Tmpbuff[n]); } printf("\n"); }else { continue ; } printf("Now is running Anticoll ....\n"); statu = PcdAnticoll(PICC_ANTICOLL1, Tmpbuff); if(statu == TAG_OK) { printf("Anticoll success and the tag sn is "); for(n = 0; n < (sizeof(Tmpbuff) / sizeof(Tmpbuff[0])); n++) { printf("0x%x ",Tmpbuff[n]); } printf("\n"); }else { printf("Anticoll faild !\n"); continue ; } printf("Now start select an tag .... \n"); statu = PcdSelect(PICC_ANTICOLL1, Tmpbuff); if(statu == TAG_OK) { printf("select tag is success !\n"); }else { printf("sorry,select tag faild !\n"); continue ; } printf("Now start authstate ... \n"); statu = PcdAuthState(PICC_AUTHENT1A, blockaddr, key, Tmpbuff); // statu = PcdAuthState(PICC_AUTHENT1B, blockaddr, key, Tmpbuff); if(statu == TAG_OK) { printf("authstate success !\n"); }else { printf("authstate faild and continue find tag....\n"); continue ; } #if 1 if(key_passwd == 3 ) { printf("Now start write [%d] blockaddr... \n",key_passwd); statu = PcdWrite(key_passwd, data_test); if(statu == TAG_OK) { printf("write success !\n"); }else { printf("write faild !\n"); continue ; } statu = PcdRead(key_passwd,Tmpbuff); if(statu == TAG_OK) { printf("read success and the data is "); for(n = 0; n < 16; n++) { printf("0x%x ",Tmpbuff[n]); } printf("\n"); }else { printf("read faild and continue find tag ....\n"); continue ; } }else { printf("[%d] address is only read !\n",blockaddr); } #endif printf("Now start read [%d] addresss ... \n",blockaddr); statu = PcdRead(blockaddr,Tmpbuff); if(statu == TAG_OK) { printf("read success and the data is "); for(n = 0; n < 16; n++) { printf("0x%x ",Tmpbuff[n]); } printf("\n"); }else { printf("read faild and continue find tag ....\n"); continue ; } printf("Now sleep the picc \n"); PcdHalt(); break ; } // InitRc522(); #if 0 while(1) { status= find_tag(&CType); printf("status is [%d] :Ctype [%x] \n",status,CType) ; if (status==TAG_NOTAG) { printf("find tag again !\n") ; usleep(200000); continue; }else if ((status!=TAG_OK)&&(status!=TAG_COLLISION)) { printf("status!=TAG_OK)&&(status!=TAG_COLLISION again \n") ; continue; } printf("Ctype is %d\n",CType); if (select_tag_sn(SN,&SN_len)!=TAG_OK) { printf("select_tag_sn(SN,&SN_len)!=TAG_OK) again \n") ; continue; } read_tag_str(0,str); printf("str:%s\n",str) ; PcdHalt(); break ; } #endif #endif printf("now start close spi orpe.....\n"); spi_close() ; return 0; }