-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.cpp
More file actions
302 lines (263 loc) · 6.04 KB
/
tag.cpp
File metadata and controls
302 lines (263 loc) · 6.04 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "tag.h"
void Tag_Actions::checksum()
{
byte s=packet[2];
for(int i=3;i<packet[2]+2;i++)
s^=packet[i];
packet[packet[2]+2]=s;
}
bool Tag_Actions::isChecksum()
{
byte s=packet_received[2];
for(int i=3;i<packet_received[2]+2;i++)
s^=packet_received[i];
if(s==packet_received[2]+2)
return true;
else
return false;
}
void Tag_Actions::Read_rfid()
{
int count=0;
byte temp_packet_read;
int i=0;
while(1)
{
if(serial.Read(&temp_packet_read,1)>0)
{
//printf("%x\n",temp_packet_read);
packet_received[i]=temp_packet_read;
i++;
if(i>2 && i==packet_received[2]+3)
break;
count++;
if(count==100)
break;
}
}
//checksum...
if(isChecksum())
{
for(int i=0;i<packet_received[2]+3;i++)
printf("%x\t",packet_received[i]);
}
else
cout<<"\nchecksum not match\n";
cout<<"\n";
if(count==100)
cout<<"Nothing to Read...\n";
}
void Tag_Actions::packet_reset()
{
for(int i=0;i<30;i++)
packet[i]=0;
packet[0]=0xAA;
packet[1]=0xBB;
}
void Tag_Actions::control_rf_transmit(bool state_switch)
{
packet_reset();
packet[2]=0x03; //length
packet[3]=0x01; //command for RF transmit
if(state_switch==true)
packet[4]=0x01;
else
packet[4]=0x00;
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
}
void Tag_Actions::select_mifare_card()
{
packet_reset();
packet[2]=0x02;
packet[3]=0x10;
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
cout<<"Serial Number: ";
for(int i=5;i<9;i++)
printf("%x\t",packet_received[i]);
cout<<endl;
cout<<"Type: ";
if(packet_received[9]==0x00)
cout<<"Mifare Standard 1K(S50) card\n";
else if(packet_received[9]==0x01)
cout<<"Mifare Standard 4K(S70) card\n";
else if(packet_received[9]==0x02)
cout<<"Mifare ProX card\n";
}
void Tag_Actions::read_data_block(char key_type, byte block, byte *key) //key: 'a' or 'b'
{
packet_reset();
packet[2]=0x0A; // length=10
packet[3]=0x11; // command
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...";
Read_rfid();
cout<<"Thus the data in the block is: \n";
for(int i=5;i<21;i++)
printf("%x\n",packet_received[i]);
}
void Tag_Actions::write_data_block(char key_type, byte block, byte *key, byte *data_write)
{
packet_reset();
packet[2]=0x1A;
packet[3]=0x12;
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
for(int i=12;i<28;i++)
packet[i]=data_write[i-12];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
if(packet_received[4]==0x00)
cout<<"Writing success!!";
else
cout<<"Oops!! Error in writing.";
}
void Tag_Actions::init_value_block(char key_type, byte block, byte *key, byte *value)
{
packet_reset();
packet[2]=0x0E;
packet[3]=0x13;
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
for(int i=12;i<16;i++)
packet[i]=value[i-12];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
if(packet_received[4]==0x00)
cout<<"Init as value block success!!";
else
cout<<"Oops!! Error in writing.";
}
void Tag_Actions::read_value_block(char key_type, byte block, byte *key)
{
packet_reset();
packet[2]=0x0A;
packet[3]=0x14;
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
cout<<"Thus the data in the block is: \n";
for(int i=5;i<9;i++)
printf("%x\n",packet_received[i]);
}
void Tag_Actions::increment_value(char key_type, byte block, byte *key, byte *value)
{
packet_reset();
packet[2]=0x0E;
packet[3]=0x15;
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
for(int i=12;i<16;i++)
packet[i]=value[i-12];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
if(packet_received[4]==0x00)
cout<<"Increment value block success!!";
else
cout<<"Oops!! Error in writing.";
}
void Tag_Actions::decrement_value(char key_type, byte block, byte *key, byte *value)
{
packet_reset();
packet[2]=0x0E;
packet[3]=0x16;
if(key_type=='a' || 'A')
packet[4]=0x00;
else if(key_type=='b' || 'B')
packet[4]=0x01;
packet[5]=block;
for(int i=6;i<12;i++)
packet[i]=key[i-6];
for(int i=12;i<16;i++)
packet[i]=value[i-12];
checksum();
cout<<"Sending the packet: \n";
for(int i=0;i<packet[2]+3;i++)
{
printf("%x\n",packet[i]);
serial.WriteByte((char)packet[i]);
}
cout<<"\nReading...\n";
Read_rfid();
if(packet_received[4]==0x00)
cout<<"Decrement value block success!!";
else
cout<<"Oops!! Error in writing.";
}