forked from adeept/Adeept_RFID_RC522_C_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
150 lines (138 loc) · 3.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdbool.h>
#include "mfrc522.h"
#define DISP_COMMANDLINE() printf("RC522>")
int scan_loop(uint8_t *CardID);
int tag_select(uint8_t *CardID);
int main(int argc, char **argv) {
MFRC522_Status_t ret;
//Recognized card ID
uint8_t CardID[5] = { 0x00, };
static char command_buffer[1024];
bool autodump_mode = false;
if (argc <= 1)
{
puts("Please specify device!");
exit(-1);
}
if ((argc > 2 && strcmp(argv[2], "autodump") == 0))
{
puts("Autodump mode enabled!");
autodump_mode = true;
}
ret = MFRC522_Init('A', argv[1]);
if (ret < 0) {
perror("Failed to initialize");
exit(-1);
}
printf("User Space RC522 Application\r\n");
if (!autodump_mode) {
while (1) {
/*Main Loop Start*/
DISP_COMMANDLINE();
scanf("%s", command_buffer);
if (strcmp(command_buffer, "scan") == 0) {
puts("Scanning");
while (1) {
ret = MFRC522_Check(CardID);
if (ret != MI_OK) {
printf(".");
fflush(stdout);
continue;
}
ret |= tag_select(CardID);
if (ret == MI_OK) {
ret = scan_loop(CardID);
if (ret < 0) {
goto END_SCAN;
} else if (ret == 1) {
goto HALT;
}
}
}
END_SCAN: printf("Card error...");
HALT: puts("Halt");
} else if (strcmp(command_buffer, "quit") == 0
|| strcmp(command_buffer, "exit") == 0) {
return 0;
} else {
puts("Unknown command");
puts("scan:scan card and dump");
puts("quit:exit program");
}
/*Main Loop End*/
}
} else {
while (true) {
ret = MFRC522_Check(CardID);
if (ret != MI_OK) {
continue;
}
ret |= tag_select(CardID);
if (ret == MI_OK) {
MFRC522_Debug_DumpSector(CardID, 0, false);
}
fflush(stdout);
}
}
}
int scan_loop(uint8_t *CardID) {
while (1) {
char input[32];
int block_start;
DISP_COMMANDLINE();
printf("%02X%02X%02X%02X>", CardID[0], CardID[1], CardID[2], CardID[3]);
scanf("%s", input);
puts((char*)input);
if (strcmp(input, "halt") == 0) {
return 1;
} else if (strcmp(input, "dump") == 0) {
if (MFRC522_Debug_CardDump(CardID) < 0)
return -1;
} else if (strcmp(input, "read") == 0) {
scanf("%d", &block_start);
if (MFRC522_Debug_DumpSector(CardID, block_start, true) < 0) {
return -1;
}
} else if (strcmp(input, "writestr") == 0) {
char write_buffer[256], c;
size_t len = 0;
scanf("%d", &block_start);
while ((c = getchar()) != '\n' && c != EOF)
;
printf(">");
if (len >= 0) {
if (MFRC522_Debug_Write(block_start, write_buffer,
strlen(write_buffer)) < 0) {
return -1;
}
}
} else {
printf(
"Usage:\r\n" "\tread <blockstart>\r\n" "\tdump\r\n" "\twritestr <blockaddr>\r\n");
return 0;
}
}
return 0;
}
int tag_select(uint8_t *CardID) {
int ret_int;
printf(
"Card detected 0x%02X 0x%02X 0x%02X 0x%02X, Check Sum = 0x%02X\r\n",
CardID[0], CardID[1], CardID[2], CardID[3], CardID[4]);
ret_int = MFRC522_SelectTag(CardID);
if (ret_int == 0) {
printf("Card Select Failed\r\n");
return -1;
} else {
printf("Card Selected, Type:%s\r\n",
MFRC522_TypeToString(MFRC522_ParseType(ret_int)));
}
ret_int = 0;
return ret_int;
}