Skip to content

Commit 06ec4fa

Browse files
committed
nfc: Update example
1 parent fbbe905 commit 06ec4fa

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

nfc/source/main.c

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void print_hex(void *buf, size_t size) {
3333
consoleUpdate(NULL);
3434
}
3535

36-
Result process_amiibo(u32 app_id) {
36+
Result process_amiibo() {
3737
Result rc = 0;
3838

3939
// Get the handle of the first controller with NFC capabilities.
@@ -94,15 +94,34 @@ Result process_amiibo(u32 app_id) {
9494
rc = eventWaitLoop(&activate_event);
9595

9696
if (R_SUCCEEDED(rc)) {
97-
printf("A tag was detected, please do not remove it from the NFC spot.\n");
97+
printf("A tag was detected, please do not remove it from the NFC spot.\n\n");
9898
consoleUpdate(NULL);
9999
}
100100
}
101101

102+
// Retrieve the tag info data, which contains the protocol, type and uuid.
103+
if (R_SUCCEEDED(rc)) {
104+
NfpTagInfo tag_info = {0};
105+
rc = nfpGetTagInfo(&handle, &tag_info);
106+
107+
if (R_SUCCEEDED(rc)) {
108+
printf("Tag protocol: 0x%02x, type: 0x%02x, UUID: ", tag_info.protocol, tag_info.tag_type);
109+
print_hex(tag_info.uuid, tag_info.uuid_length);
110+
printf("\n");
111+
}
112+
}
113+
102114
// If a tag was successfully detected, load it into memory.
103-
if (R_SUCCEEDED(rc))
115+
if (R_SUCCEEDED(rc)) {
104116
rc = nfpMount(&handle, NfpDeviceType_Amiibo, NfpMountTarget_All);
105117

118+
if (rc == 0x11073) // 2115-0136
119+
printf("This tag is corrupted and has a backup in system.\n");
120+
121+
if (rc == 0x12073) // 2115-0144
122+
printf("This tag is corrupted.\n");
123+
}
124+
106125
// Retrieve the model info data, which contains the amiibo id.
107126
if (R_SUCCEEDED(rc)) {
108127
NfpModelInfo model_info = {0};
@@ -111,6 +130,7 @@ Result process_amiibo(u32 app_id) {
111130
if (R_SUCCEEDED(rc)) {
112131
printf("Amiibo ID: ");
113132
print_hex(model_info.amiibo_id, 8);
133+
printf("\n");
114134
}
115135
}
116136

@@ -120,8 +140,40 @@ Result process_amiibo(u32 app_id) {
120140
NfpCommonInfo common_info = {0};
121141
rc = nfpGetCommonInfo(&handle, &common_info);
122142

123-
if (R_SUCCEEDED(rc))
143+
if (R_SUCCEEDED(rc)) {
124144
app_area_size = common_info.application_area_size;
145+
printf("Write counter: %d, last write date %d/%d/%d\n\n", common_info.write_counter, common_info.last_write_day, common_info.last_write_month, common_info.last_write_year);
146+
}
147+
}
148+
149+
u32 app_id=0;
150+
// Retrieve the admin info data, which contains the app id.
151+
if (R_SUCCEEDED(rc)) {
152+
NfpAdminInfo admin_info = {0};
153+
rc = nfpGetAdminInfo(&handle, &admin_info);
154+
155+
if (R_SUCCEEDED(rc)) {
156+
app_id = admin_info.application_area_id;
157+
printf("App area: 0x%x, game ID: 0x%lx, console: ", app_id, admin_info.application_id);
158+
switch(admin_info.application_area_version){
159+
case NfpApplicationAreaVersion_3DS:
160+
printf("Old 3ds");
161+
break;
162+
case NfpApplicationAreaVersion_WiiU:
163+
printf("Wii U");
164+
break;
165+
case NfpApplicationAreaVersion_3DSv2:
166+
printf("New 3ds");
167+
break;
168+
case NfpApplicationAreaVersion_Switch:
169+
printf("Switch");
170+
break;
171+
case NfpApplicationAreaVersion_NotSet:
172+
printf("Not set");
173+
break;
174+
}
175+
printf("\n");
176+
}
125177
}
126178

127179
if (R_SUCCEEDED(rc)) {
@@ -134,13 +186,15 @@ Result process_amiibo(u32 app_id) {
134186
}
135187

136188
u8 app_area[0xd8] = {0}; // Maximum size of the application area.
189+
u32 app_area_read_size = 0; // Actual number of bytes set by nfpGetApplicationArea.
137190
if (app_area_size > sizeof(app_area)) app_area_size = sizeof(app_area);
138191
if (R_SUCCEEDED(rc)) {
139-
rc = nfpGetApplicationArea(&handle, app_area, app_area_size);
192+
rc = nfpGetApplicationArea(&handle, app_area, app_area_size, &app_area_read_size);
140193

141194
if (R_SUCCEEDED(rc)) {
142-
printf("App area:\n");
143-
print_hex(app_area, app_area_size);
195+
printf("App data:\n");
196+
print_hex(app_area, app_area_read_size);
197+
printf("\n");
144198
}
145199
}
146200

@@ -171,11 +225,7 @@ Result process_amiibo(u32 app_id) {
171225
int main(int argc, char* argv[])
172226
{
173227
Result rc = 0;
174-
175-
// Hardcoded for Super Smash Bros. Ultimate.
176-
// See also: https://switchbrew.org/wiki/NFC_services#Application_IDs
177-
u32 app_id = 0x34f80200;
178-
228+
179229
// This example uses a text console, as a simple way to output text to the screen.
180230
// If you want to write a software-rendered graphics application,
181231
// take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
@@ -194,7 +244,7 @@ int main(int argc, char* argv[])
194244
consoleUpdate(NULL);
195245

196246
// Initialize the nfp:* service.
197-
rc = nfpInitialize(NfpServiceType_User);
247+
rc = nfpInitialize(NfpServiceType_Debug);
198248

199249
// Check if NFC is enabled. If not, wait until it is.
200250
// Note that various official games don't use nfc*().
@@ -237,7 +287,7 @@ int main(int argc, char* argv[])
237287
// padGetButtonsDown returns the set of buttons that have been
238288
// newly pressed in this frame compared to the previous one
239289
if (padGetButtonsDown(&pad) & HidNpadButton_A) {
240-
rc = process_amiibo(app_id);
290+
rc = process_amiibo();
241291

242292
// If an error happened, print it.
243293
if (R_FAILED(rc))

0 commit comments

Comments
 (0)