Updated nfc example for latest libnx.

This commit is contained in:
yellows8 2019-10-13 19:40:03 -04:00
parent 5363a04d9b
commit a0b4ee1229
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43

View File

@ -38,7 +38,7 @@ Result process_amiibo(u32 app_id) {
HidControllerID controller = 0; HidControllerID controller = 0;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
s32 device_count; s32 device_count;
rc = nfpuListDevices(&device_count, &controller, 1); rc = nfpListDevices(&device_count, &controller, 1);
if (R_FAILED(rc)) if (R_FAILED(rc))
return rc; return rc;
@ -46,31 +46,31 @@ Result process_amiibo(u32 app_id) {
// Get the activation event. This is signaled when a tag is detected. // Get the activation event. This is signaled when a tag is detected.
Event activate_event = {0}; Event activate_event = {0};
if (R_FAILED(nfpuAttachActivateEvent(controller, &activate_event))) if (R_FAILED(nfpAttachActivateEvent(controller, &activate_event)))
goto fail_0; goto fail_0;
// Get the deactivation event. This is signaled when a tag is removed. // Get the deactivation event. This is signaled when a tag is removed.
Event deactivate_event = {0}; Event deactivate_event = {0};
if (R_FAILED(nfpuAttachDeactivateEvent(controller, &deactivate_event))) if (R_FAILED(nfpAttachDeactivateEvent(controller, &deactivate_event)))
goto fail_1; goto fail_1;
NfpuState state = 0; NfpState state = 0;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = nfpuGetState(&state); rc = nfpGetState(&state);
if (R_SUCCEEDED(rc) && state == NfpuState_NonInitialized) { if (R_SUCCEEDED(rc) && state == NfpState_NonInitialized) {
printf("Bad nfpu state: %u\n", state); printf("Bad nfp state: %u\n", state);
consoleUpdate(NULL); consoleUpdate(NULL);
rc = -1; rc = -1;
} }
} }
NfpuDeviceState device_state = 0; NfpDeviceState device_state = 0;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = nfpuGetDeviceState(controller, &device_state); rc = nfpGetDeviceState(controller, &device_state);
if (R_SUCCEEDED(rc) && device_state > NfpuDeviceState_TagFound) { if (R_SUCCEEDED(rc) && device_state > NfpDeviceState_TagFound) {
printf("Bad nfpu device state: %u\n", device_state); printf("Bad nfp device state: %u\n", device_state);
consoleUpdate(NULL); consoleUpdate(NULL);
rc = -1; rc = -1;
} }
@ -80,14 +80,14 @@ Result process_amiibo(u32 app_id) {
goto fail_1; goto fail_1;
// Start the detection of tags. // Start the detection of tags.
rc = nfpuStartDetection(controller); rc = nfpStartDetection(controller);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
printf("Scanning for a tag...\n"); printf("Scanning for a tag...\n");
consoleUpdate(NULL); consoleUpdate(NULL);
} }
// Wait until a tag is detected. // Wait until a tag is detected.
// You could also wait until nfpuGetDeviceState returns NfpuDeviceState_TagFound. // You could also wait until nfpGetDeviceState returns NfpDeviceState_TagFound.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = eventWaitLoop(&activate_event); rc = eventWaitLoop(&activate_event);
@ -99,12 +99,12 @@ Result process_amiibo(u32 app_id) {
// If a tag was successfully detected, load it into memory. // If a tag was successfully detected, load it into memory.
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
rc = nfpuMount(controller, NfpuDeviceType_Amiibo, NfpuMountTarget_All); rc = nfpMount(controller, NfpDeviceType_Amiibo, NfpMountTarget_All);
// Retrieve the model info data, which contains the amiibo id. // Retrieve the model info data, which contains the amiibo id.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
NfpuModelInfo model_info = {0}; NfpModelInfo model_info = {0};
rc = nfpuGetModelInfo(controller, &model_info); rc = nfpGetModelInfo(controller, &model_info);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
printf("Amiibo ID: "); printf("Amiibo ID: ");
@ -115,8 +115,8 @@ Result process_amiibo(u32 app_id) {
// Retrieve the common info data, which contains the application area size. // Retrieve the common info data, which contains the application area size.
u32 app_area_size = 0; u32 app_area_size = 0;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
NfpuCommonInfo common_info = {0}; NfpCommonInfo common_info = {0};
rc = nfpuGetCommonInfo(controller, &common_info); rc = nfpGetCommonInfo(controller, &common_info);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
app_area_size = common_info.application_area_size; app_area_size = common_info.application_area_size;
@ -124,7 +124,7 @@ Result process_amiibo(u32 app_id) {
u32 npad_id = 0; u32 npad_id = 0;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = nfpuOpenApplicationArea(controller, app_id, &npad_id); rc = nfpOpenApplicationArea(controller, app_id, &npad_id);
if (rc == 0x10073) // 2115-0128 if (rc == 0x10073) // 2115-0128
printf("This tag contains no application data.\n"); printf("This tag contains no application data.\n");
@ -135,7 +135,7 @@ Result process_amiibo(u32 app_id) {
u8 app_area[0xd8] = {0}; // Maximum size of the application area. u8 app_area[0xd8] = {0}; // Maximum size of the application area.
if (app_area_size > sizeof(app_area)) app_area_size = sizeof(app_area); if (app_area_size > sizeof(app_area)) app_area_size = sizeof(app_area);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = nfpuGetApplicationArea(controller, app_area, app_area_size); rc = nfpGetApplicationArea(controller, app_area, app_area_size);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
printf("App area:\n"); printf("App area:\n");
@ -144,7 +144,7 @@ Result process_amiibo(u32 app_id) {
} }
// Wait until the tag is removed. // Wait until the tag is removed.
// You could also wait until nfpuGetDeviceState returns NfpuDeviceState_TagRemoved. // You could also wait until nfpGetDeviceState returns NfpDeviceState_TagRemoved.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
printf("You can now remove the tag.\n"); printf("You can now remove the tag.\n");
consoleUpdate(NULL); consoleUpdate(NULL);
@ -152,10 +152,10 @@ Result process_amiibo(u32 app_id) {
} }
// Unmount the tag. // Unmount the tag.
nfpuUnmount(controller); nfpUnmount(controller);
// Stop the detection of tags. // Stop the detection of tags.
nfpuStopDetection(controller); nfpStopDetection(controller);
// Cleanup. // Cleanup.
fail_1: fail_1:
@ -186,18 +186,20 @@ int main(int argc, char* argv[])
printf("Scan an amiibo tag to display information about it.\n\n"); printf("Scan an amiibo tag to display information about it.\n\n");
consoleUpdate(NULL); consoleUpdate(NULL);
// Initialize the nfp:user and nfc:user services. // Initialize the nfp:* service.
rc = nfpuInitialize(NULL); rc = nfpInitialize();
// Check if NFC is enabled. If not, wait until it is. // Check if NFC is enabled. If not, wait until it is.
// Note that various official games don't use nfc*().
if (R_SUCCEEDED(rc)) rc = nfcInitialize();
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
bool nfc_enabled = false; bool nfc_enabled = false;
rc = nfpuIsNfcEnabled(&nfc_enabled); rc = nfcIsNfcEnabled(&nfc_enabled);
if (R_SUCCEEDED(rc) && !nfc_enabled) { if (R_SUCCEEDED(rc) && !nfc_enabled) {
// Get the availability change event. This is signaled when a change in NFC availability happens. See libnx nfc.h for the required sysver. // Get the availability change event. This is signaled when a change in NFC availability happens. See libnx nfc.h for the required sysver.
Event availability_change_event = {0}; Event availability_change_event = {0};
rc = nfpuAttachAvailabilityChangeEvent(&availability_change_event); rc = nfpAttachAvailabilityChangeEvent(&availability_change_event);
// Wait for a change in availability. // Wait for a change in availability.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
@ -208,6 +210,8 @@ int main(int argc, char* argv[])
eventClose(&availability_change_event); eventClose(&availability_change_event);
} }
nfcExit();
} }
if (R_FAILED(rc)) if (R_FAILED(rc))
@ -244,7 +248,7 @@ int main(int argc, char* argv[])
} }
fail_main: fail_main:
nfpuExit(); nfpExit();
// Deinitialize and clean up resources used by the console (important!) // Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL); consoleExit(NULL);