/**************************************************************************************************** *函数名称: rc522_write *函数参数: filp: 文件指针 * buf: 数据写入的数组 * size: 需要读取的数据长度 * pos: 文件偏移 *函数返回值: 成功返回0 错误返回-1 *函数描述: spi写入数据函数 ****************************************************************************************************/ static ssize_t rc522_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { int ret = 0; #if 0 uint8_t write_buf[20] = {0}; /* 验证卡片密码 */ g_Auth_State authstate = (g_Auth_State)kmalloc(sizeof(struct AUTHSTATE), GFP_KERNEL); /* 分配结构 */ /* 写块 */ g_Write_Addr write_addr = (g_Write_Addr)kmalloc(sizeof(struct WRITEDAAR), GFP_KERNEL); /* 分配结构 */ /* 块备份 */ g_Bak_Value bak_value = (g_Bak_Value)kmalloc(sizeof(struct BAKVALUE), GFP_KERNEL); memset(authstate, 0, sizeof(struct AUTHSTATE)); /* 初始化结构体 */ memset(write_addr, 0 ,sizeof(struct WRITEDAAR)); /* 初始化结构体 */ memset(bak_value, 0 ,sizeof(struct BAKVALUE)); /* 初始化结构体 */ switch (g_write_cmd) { case RC522_Select_CMD: /* 选定卡片 */ copy_from_user( write_buf, buf, size ); ret = PcdSelect(write_buf);//选定卡片 if (ret != MI_OK) printk(KERN_ERR "PcdSelect error!\r\n"); break; case RC522_AuthState_CMD: /* 验证卡片密码 */ copy_from_user( authstate, buf, size ); ret = PcdAuthState(authstate->cmd, authstate->addr, authstate->key, authstate->car_str);//验证卡片密码 if (ret != MI_OK) printk(KERN_ERR "PcdSelect error!\r\n"); break; case RC522_Write_CMD: /* 写块 */ copy_from_user( write_addr, buf, size ); ret = PcdWrite(write_addr->addr, write_addr->data);//写块 if (ret != MI_OK) printk(KERN_ERR "PcdSelect error!\r\n"); break; case RC522_BakValue_CMD: /* 块备份 */ copy_from_user( bak_value, buf, size ); ret = PcdBakValue(bak_value->s_addr, bak_value->d_addr);//块备份 if (ret != MI_OK) printk(KERN_ERR "PcdSelect error!\r\n"); break; default: printk(KERN_ERR "cmd is Invalid!\r\n"); } kfree(authstate); kfree(write_addr); kfree(bak_value); #endif return ret; }
/**************************************************************************************** ** 函数原型: void PcdAuthentication(void) ** 函数功能: 密钥验证 ** 入口参数: - ** 出口参数: - ** 返 回 值: - ** 描 述: ****************************************************************************************/ char PcdAuthentication(unsigned char keyType, unsigned char blockaddr, unsigned char *pKey, unsigned char *pCardSnr) { char status = MI_COM_ERR; unsigned char AuthCmd, c_TempData[12]; if (keyType == 0x0A) { AuthCmd = PICC_AUTHENT1A; } if (keyType == 0x0B) { AuthCmd = PICC_AUTHENT1B; } ChangeCodeKey(pKey,c_TempData); //转换密钥格式 PcdAuthKey(c_TempData); //写入FIFO status = PcdAuthState(AuthCmd, blockaddr, pCardSnr); //密钥验证 return 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; }