📄 mfrc500uc.lst
字号:
992 4 }
993 3 }
994 2 while((status == MI_OK) // error status
995 2 && (*sak & 0x04) // no further cascade level
996 2 && (cascade_level < 3)); // highest cascade level is reached
997 2 }
998 1 if (status == MI_OK)
999 1 {
1000 2 //Exit function, if cascade level is triple and sak indicates another
1001 2 //cascase level.
1002 2 if ((cascade_level == 3) && (*sak & 0x04))
1003 2 {
1004 3 *uid_len = 0;
1005 3 status = MI_CASCLEVEX;
1006 3 }
1007 2 Mf500PcdSetAttrib(br,br);
1008 2 }
1009 1 return (status);
1010 1 }
1011
1012 ///////////////////////////////////////////////////////////////////////
1013 // M I F A R E P I C C A C T I V A T I O N S E Q E N C E
1014 ///////////////////////////////////////////////////////////////////////
1015 char Mf500PiccActivateWakeup(unsigned char br,
1016 unsigned char *atq,
1017 unsigned char *sak,
1018 unsigned char *uid,
1019 unsigned char uid_len)
1020 {
1021 1 unsigned char cascade_level;
1022 1 unsigned char uid_index;
1023 1 unsigned char tmpuid[4];
1024 1 unsigned char sel_code;
1025 1 unsigned char cmdASEL;
1026 1 signed char status;
1027 1
1028 1 //call activation with def. divs
1029 1 status = Mf500PcdSetDefaultAttrib();
1030 1 if (status == MI_OK)
1031 1 {
1032 2 status = Mf500PiccCommonRequest(PICC_REQALL,atq);
1033 2 }
1034 1 if (status == MI_OK)
1035 1 {
1036 2 if ((atq[0] & 0x1F) == 0x00) // check lower 5 bits, for tag-type
1037 2 // all tags within this 5 bits have to
1038 2 // provide a bitwise anticollision
1039 2 {
1040 3 status = MI_NOBITWISEANTICOLL;
1041 3 }
1042 2 }
1043 1 if (status == MI_OK)
1044 1 {
1045 2 //Get UID in 1 - 3 levels (standard, [double], [triple] )
1046 2 //-------
1047 2 switch(br)
C51 COMPILER V7.00 MFRC500UC 03/05/2004 15:25:52 PAGE 18
1048 2 {
1049 3 case 0: cmdASEL = PICC_ANTICOLL1; break;
1050 3 case 1: cmdASEL = PICC_ANTICOLL11; break;
1051 3 case 2: cmdASEL = PICC_ANTICOLL12; break;
1052 3 case 3: cmdASEL = PICC_ANTICOLL13; break;
1053 3 default:
1054 3 status = MI_BAUDRATE_NOT_SUPPORTED; break;
1055 3 }
1056 2 }
1057 1 if (status == MI_OK)
1058 1 {
1059 2 //Select UID in up to 3 cascade levels (standard, [double], [triple] )
1060 2 //------------------------------------
1061 2 cascade_level = 0;
1062 2 uid_index = 0;
1063 2 tmpuid[0] = 0x88; //first byte of cascaded UIDs is 0x88 (cascaded tag)
1064 2
1065 2 do
1066 2 {
1067 3 sel_code = cmdASEL + (2 * cascade_level);
1068 3 cmdASEL = PICC_ANTICOLL1; // reset anticollistion level for calculation
1069 3 //get the next UID part if we need to cascade
1070 3 if((uid_len - uid_index) > 4)
1071 3 {
1072 4 //ok, we need to cascade the UID
1073 4 memcpy(&tmpuid[1], &uid[uid_index], 3);
1074 4 uid_index += 3;
1075 4 }
1076 3 else
1077 3 {
1078 4 //ah, how nice. no need to cascade
1079 4 memcpy(tmpuid, &uid[uid_index], 4);
1080 4 uid_index += 4;
1081 4 }
1082 3
1083 3 status = Mf500PiccCascSelect(sel_code, tmpuid, sak);
1084 3
1085 3 if(status == MI_OK)
1086 3 {
1087 4 cascade_level++;
1088 4 }
1089 3 }
1090 2 while((status == MI_OK ) // error occured
1091 2 && (*sak & 0x04) // no further cascade level
1092 2 && ((uid_index + 1) < uid_len) // all bytes of snr sent
1093 2 && (cascade_level < 3)); // highest cascade level reached
1094 2 }
1095 1 if ( status == MI_OK)
1096 1 {
1097 2 //Exit function, if UID length is not of expected length
1098 2 if ((uid_index) != uid_len)
1099 2 {
1100 3 status = MI_SERNRERR ;
1101 3 }
1102 2 }
1103 1 if (status == MI_OK)
1104 1 {
1105 2 //Exit function, if cascade level is triple and sak indicates another
1106 2 //cascase level.
1107 2 if ((cascade_level == 3) && (*sak & 0x04))
1108 2 {
1109 3 status = MI_SERNRERR;
C51 COMPILER V7.00 MFRC500UC 03/05/2004 15:25:52 PAGE 19
1110 3 }
1111 2 }
1112 1 return status;
1113 1 }
1114
1115 ///////////////////////////////////////////////////////////////////////
1116 // M I F A R E A U T H E N T I C A T I O N
1117 // calling compatible version
1118 ///////////////////////////////////////////////////////////////////////
1119 char Mf500PiccAuth(unsigned char key_type, // PICC_AUTHENT1A or PICC_AUTHENT1B
1120 unsigned char key_addr, // key address in reader storage
1121 unsigned char block) // block number which should be
1122 // authenticated
1123 {
1124 1 char status = MI_OK;
1125 1
1126 1 status = Mf500PiccAuthE2( key_type,
1127 1 MLastSelectedSnr,
1128 1 key_addr,
1129 1 block);
1130 1 return status;
1131 1 }
1132
1133 ///////////////////////////////////////////////////////////////////////
1134 // A U T H E N T I C A T I O N
1135 // W I T H K E Y S F R O M E 2 P R O M
1136 ///////////////////////////////////////////////////////////////////////
1137 char Mf500PiccAuthE2( unsigned char auth_mode, // PICC_AUTHENT1A or PICC_AUTHENT1B
1138 unsigned char *snr, // 4 bytes card serial number
1139 unsigned char key_sector, // 0 <= key_sector <= 15
1140 unsigned char block) // 0 <= block <= 256
1141 {
1142 1 char status = MI_OK;
1143 1 // eeprom address calculation
1144 1 // 0x80 ... offset
1145 1 // key_sector ... sector
1146 1 // 0x18 ... 2 * 12 = 24 = 0x18
1147 1 unsigned short e2addr = 0x80 + key_sector * 0x18;
1148 1 unsigned char *e2addrbuf = (unsigned char*)&e2addr;
1149 1
1150 1 PcdSetTmo(106);
1151 1 if (auth_mode == PICC_AUTHENT1B)
1152 1 e2addr += 12; // key B offset
1153 1 FlushFIFO(); // empty FIFO
1154 1 ResetInfo(MInfo);
1155 1
1156 1 memcpy(MSndBuffer,e2addrbuf,2); // write low and high byte of address
1157 1 MInfo.nBytesToSend = 2;
1158 1 // write load command
1159 1 if ((status=PcdSingleResponseCmd(PCD_LOADKEYE2,MSndBuffer,MRcvBuffer,&MInfo)) == MI_OK)
1160 1 {
1161 2 // execute authentication
1162 2 status = Mf500PiccAuthState(auth_mode,snr,block);
1163 2 }
1164 1 return status;
1165 1 }
1166
1167 ///////////////////////////////////////////////////////////////////////
1168 // C O D E K E Y S
1169 ///////////////////////////////////////////////////////////////////////
1170 char Mf500HostCodeKey( unsigned char *uncoded, // 6 bytes key value uncoded
1171 unsigned char *coded) // 12 bytes key value coded
C51 COMPILER V7.00 MFRC500UC 03/05/2004 15:25:52 PAGE 20
1172 {
1173 1 char status = MI_OK;
1174 1 unsigned char cnt = 0;
1175 1 unsigned char ln = 0; // low nibble
1176 1 unsigned char hn = 0; // high nibble
1177 1
1178 1 for (cnt = 0; cnt < 6; cnt++)
1179 1 {
1180 2 ln = uncoded[cnt] & 0x0F;
1181 2 hn = uncoded[cnt] >> 4;
1182 2 coded[cnt * 2 + 1] = (~ln << 4) | ln;
1183 2 coded[cnt * 2 ] = (~hn << 4) | hn;
1184 2 }
1185 1 return MI_OK;
1186 1 }
1187
1188 ///////////////////////////////////////////////////////////////////////
1189 // A U T H E N T I C A T I O N
1190 // W I T H P R O V I D E D K E Y S
1191 ///////////////////////////////////////////////////////////////////////
1192 char Mf500PiccAuthKey( unsigned char auth_mode,
1193 unsigned char *snr,
1194 unsigned char *keys,
1195 unsigned char block)
1196 {
1197 1 char status = MI_OK;
1198 1 unsigned char i = 0;
1199 1
1200 1 PcdSetTmo(106);
1201 1 FlushFIFO(); // empty FIFO
1202 1 ResetInfo(MInfo);
1203 1 memcpy(MSndBuffer,keys,12); // write 12 bytes of the key
1204 1 MInfo.nBytesToSend = 12;
1205 1 // write load command
1206 1 if ((status=PcdSingleResponseCmd(PCD_LOADKEY,MSndBuffer,MRcvBuffer,&MInfo)) == MI_OK)
1207 1 {
1208 2 // execute authentication
1209 2 status = Mf500PiccAuthState(auth_mode,snr,block);
1210 2 }
1211 1 return status;
1212 1 }
1213
1214 ///////////////////////////////////////////////////////////////////////
1215 // S T O R E K E Y S I N E E P R O M
1216 ///////////////////////////////////////////////////////////////////////
1217 char Mf500PcdLoadKeyE2(unsigned char key_type,
1218 unsigned char sector,
1219 unsigned char *uncoded_keys)
1220 {
1221 1 // eeprom address calculation
1222 1 // 0x80 ... offset
1223 1 // key_sector ... sector
1224 1 // 0x18 ... 2 * 12 = 24 = 0x18
1225 1 signed char status = MI_OK;
1226 1 unsigned short e2addr = 0x80 + sector * 0x18;
1227 1 unsigned char coded_keys[12];
1228 1
1229 1 if (key_type == PICC_AUTHENT1B)
1230 1 e2addr += 12; // key B offset
1231 1 if ((status = Mf500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
1232 1 status = PcdWriteE2( e2addr,12,coded_keys);
1233 1 return status;
C51 COMPILER V7.00 MFRC500UC 03/05/2004 15:25:52 PAGE 21
1234 1 }
1235
1236 ///////////////////////////////////////////////////////////////////////
1237 // A U T H E N T I C A T I O N S T A T E S
1238 ///////////////////////////////////////////////////////////////////////
1239 char Mf500PiccAuthState( unsigned char auth_mode,
1240 unsigned char *snr,
1241
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -