Skip to content

Commit 7d74c0a

Browse files
committed
Fixed writing. Cleanup debug messages.
1 parent 0676af4 commit 7d74c0a

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

source/main.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void writeToTag() {
124124
printf("No tag loaded\n");
125125
return;
126126
}
127-
printf("Place tag on scanner...\n");
127+
printf("Place tag on scanner, or press B to cancel.\n");
128128
u8 firstPages[NTAG_BLOCK_SIZE];
129129

130130
int res = nfc_readBlock(0, firstPages, sizeof(firstPages));
@@ -176,7 +176,7 @@ void writeToTag() {
176176
}
177177

178178
void dumpTagToFile() {
179-
printf("Place tag on scanner...\n");
179+
printf("Place tag on scanner, or press B to cancel\n");
180180
u8 data[AMIIBO_MAX_SIZE];
181181
int res = nfc_readFull(data, sizeof(data));
182182
if (res != 0) {
@@ -209,23 +209,33 @@ void dumpTagToFile() {
209209
}
210210

211211
void menu() {
212-
printf("X - Load tag dump.\n");
213-
printf("A - Write/Restore to tag.\n");
214-
printf("Y - Dump tag to file.\n");
215-
printf("B - Quit\n");
212+
int refreshMenu = 1;
216213

217214
while (aptMainLoop()) {
218215
gspWaitForVBlank();
216+
217+
if (refreshMenu) {
218+
printf("X - Load tag dump.\n");
219+
if (tag_isLoaded())
220+
printf("A - Write to tag.\n");
221+
printf("Y - Dump tag to file.\n");
222+
printf("B - Quit\n\n");
223+
refreshMenu = 0;
224+
}
225+
219226
hidScanInput();
220227
u32 kDown = hidKeysDown();
221228

222-
if (kDown & KEY_X)
229+
if (kDown & KEY_X) {
223230
loadDump();
224-
else if (kDown & KEY_A)
231+
refreshMenu = 1;
232+
} else if (kDown & KEY_A) {
225233
writeToTag();
226-
else if (kDown & KEY_Y)
234+
refreshMenu = 1;
235+
} else if (kDown & KEY_Y) {
227236
dumpTagToFile();
228-
else if (kDown & KEY_B)
237+
refreshMenu = 1;
238+
} else if (kDown & KEY_B)
229239
break;
230240
}
231241
}

source/nfc.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static void printbuf(char *prefix, u8* data, size_t len) {
4343
#define DnfcStopScanning nfcStopScanning
4444

4545
#else
46+
47+
//emulate the calls for running in citra
4648
static Result DnfcStartOtherTagScanning(int a, int b) {
4749
return 0;
4850
}
@@ -89,7 +91,11 @@ Result nfc_readFull(u8 *data, int datalen) {
8991

9092
u32 kDown = hidKeysDown();
9193

92-
if(kDown & KEY_B) break;
94+
if(kDown & KEY_B) {
95+
ret = -1;
96+
printf("Cancelled.\n");
97+
break;
98+
}
9399

94100
ret = DnfcGetTagState(&curstate);
95101
if(R_FAILED(ret)) {
@@ -104,15 +110,17 @@ Result nfc_readFull(u8 *data, int datalen) {
104110
u8 tagdata[AMIIBO_MAX_SIZE];
105111
memset(tagdata, 0, sizeof(tagdata));
106112

113+
printf("Reading tag");
114+
107115
for(int i=0x00; i<=NTAG_215_LAST_PAGE ;i+=NTAG_FAST_READ_PAGE_COUNT) {
108-
printf("Page %d\n", i);
116+
printf(".");
109117
u8 cmd[] = CMD_FAST_READ(i, NTAG_FAST_READ_PAGE_COUNT);
110118
u8 cmdresult[NTAG_FAST_READ_PAGE_COUNT*NTAG_PAGE_SIZE];
111-
printbuf("CMD ", cmd, sizeof(cmd));
119+
//printbuf("CMD ", cmd, sizeof(cmd));
112120
memset(cmdresult, 0, sizeof(cmdresult));
113121
size_t resultsize = 0;
114122

115-
printf("page start %x end %x\n", cmd[1], cmd[2]);
123+
//printf("page start %x end %x\n", cmd[1], cmd[2]);
116124
resultsize = NTAG_FAST_READ_PAGE_COUNT*NTAG_PAGE_SIZE;
117125

118126
ret = DnfcSendTagCommand(cmd, sizeof(cmd), cmdresult, sizeof(cmdresult), &resultsize, NFC_TIMEOUT);
@@ -129,9 +137,8 @@ Result nfc_readFull(u8 *data, int datalen) {
129137
int copycount = sizeof(cmdresult);
130138
if (((i * NTAG_PAGE_SIZE) + sizeof(cmdresult)) > sizeof(tagdata))
131139
copycount = ((i * NTAG_PAGE_SIZE) + sizeof(cmdresult)) - sizeof(tagdata);
132-
printf(">%d ", copycount);
133140
memcpy(&tagdata[i * NTAG_PAGE_SIZE], cmdresult, copycount);
134-
printbuf("result", cmdresult, resultsize);
141+
//printbuf("result", cmdresult, resultsize);
135142
}
136143
if (ret == 0) {
137144
memcpy(data, tagdata, sizeof(tagdata));
@@ -142,6 +149,7 @@ Result nfc_readFull(u8 *data, int datalen) {
142149
}
143150
}
144151

152+
printf("\n");
145153
DnfcStopScanning();
146154
return ret;
147155
}
@@ -172,7 +180,11 @@ Result nfc_readBlock(int pageId, u8 *data, int datalen) {
172180

173181
u32 kDown = hidKeysDown();
174182

175-
if(kDown & KEY_B) break;
183+
if(kDown & KEY_B) {
184+
ret = -1;
185+
printf("Cancelled.\n");
186+
break;
187+
}
176188

177189
ret = DnfcGetTagState(&curstate);
178190
if(R_FAILED(ret))
@@ -222,13 +234,6 @@ static Result writeTag(u8 *data, u8 *PWD, u8 *PACK) {
222234
//write normal pages
223235
int ret = 0;
224236

225-
ret = nfcCmd21(); //seems to put the NFC reader into a continious mode allowing all the requests to go through one session without powering the tag down.
226-
227-
if(R_FAILED(ret)) {
228-
printf("nfcCmd21 failed : %d\n", R_DESCRIPTION(ret));
229-
return ret;
230-
}
231-
232237
printf("Writing normal pages\n");
233238
for(int pageId = 0x04; pageId <= 0x81; pageId++) {
234239
printf(".");
@@ -244,6 +249,12 @@ static Result writeTag(u8 *data, u8 *PWD, u8 *PACK) {
244249
if (R_FAILED(ret))
245250
return ret;
246251

252+
ret = nfcCmd21(); //seems to put the NFC reader into a continious mode allowing all the requests to go through one session without powering the tag down.
253+
if(R_FAILED(ret)) {
254+
printf("nfcCmd21 failed : %d\n", R_DESCRIPTION(ret));
255+
return ret;
256+
}
257+
247258
//write PACK
248259
printf("Writing PACK\n");
249260
ret = writePage(0x86, PACK);
@@ -292,7 +303,7 @@ Result nfc_auth(u8 *PWD) {
292303
size_t resultsize = 0;
293304
u8 packres[2];
294305
int ret = DnfcSendTagCommand(pwdcmd, sizeof(pwdcmd), packres, sizeof(packres), &resultsize, NFC_TIMEOUT);
295-
printbuf("pack ", packres, sizeof(packres));
306+
//printbuf("pack ", packres, sizeof(packres));
296307
if(R_FAILED(ret)) {
297308
printf("PWD command failed failed : %d\n", R_DESCRIPTION(ret));
298309
return ret;
@@ -327,7 +338,11 @@ Result nfc_write(u8 *data, int datalen, u8 *PWD, int PWDLength) {
327338

328339
u32 kDown = hidKeysDown();
329340

330-
if(kDown & KEY_B) break;
341+
if(kDown & KEY_B) {
342+
ret = -1;
343+
printf("Cancelled.\n");
344+
break;
345+
}
331346

332347
ret = DnfcGetTagState(&curstate);
333348
if(R_FAILED(ret)) {

0 commit comments

Comments
 (0)