From 98968b45f3a6bf32babaf528c30466c25a56800a Mon Sep 17 00:00:00 2001 From: yellows8 Date: Mon, 14 Oct 2019 17:21:38 -0400 Subject: [PATCH] nfc: Replaced HidControllerID usage with struct NfcDeviceHandle. Updated params for nfpListDevices. Minor internal changes. --- nx/include/switch/services/nfc.h | 43 ++++---- nx/source/services/nfc.c | 175 +++++++++++++------------------ 2 files changed, 99 insertions(+), 119 deletions(-) diff --git a/nx/include/switch/services/nfc.h b/nx/include/switch/services/nfc.h index b7a1b51b..b07a87ed 100644 --- a/nx/include/switch/services/nfc.h +++ b/nx/include/switch/services/nfc.h @@ -142,6 +142,11 @@ typedef struct { u64 reserved[3]; } NfcRequiredMcuVersionData; +/// Nfc/Nfp DeviceHandle +typedef struct { + u64 handle; ///< Handle. +} NfcDeviceHandle; + /** * @brief Sets the \ref NfpServiceType for initialization. Call this function before \ref nfpInitialize, if needed. * @note By default ::NfpServiceType_NotInitialized will be used. @@ -178,39 +183,39 @@ Service* nfcGetServiceSession(void); /// Gets the Service object for the interface from nfc:*. Service* nfcGetServiceSession_Interface(void); -Result nfpListDevices(s32 *count, HidControllerID *out, size_t num_elements); -Result nfpStartDetection(HidControllerID id); -Result nfpStopDetection(HidControllerID id); -Result nfpMount(HidControllerID id, NfpDeviceType device_type, NfpMountTarget mount_target); -Result nfpUnmount(HidControllerID id); +Result nfpListDevices(s32 *total_out, NfcDeviceHandle *out, s32 count); +Result nfpStartDetection(const NfcDeviceHandle *handle); +Result nfpStopDetection(const NfcDeviceHandle *handle); +Result nfpMount(const NfcDeviceHandle *handle, NfpDeviceType device_type, NfpMountTarget mount_target); +Result nfpUnmount(const NfcDeviceHandle *handle); /// Not available with ::NfpServiceType_System. -Result nfpOpenApplicationArea(HidControllerID id, u32 app_id, u32 *npad_id); +Result nfpOpenApplicationArea(const NfcDeviceHandle *handle, u32 app_id, u32 *npad_id); /// Not available with ::NfpServiceType_System. -Result nfpGetApplicationArea(HidControllerID id, void* buf, size_t buf_size); +Result nfpGetApplicationArea(const NfcDeviceHandle *handle, void* buf, size_t buf_size); /// Not available with ::NfpServiceType_System. -Result nfpSetApplicationArea(HidControllerID id, const void* buf, size_t buf_size); -Result nfpFlush(HidControllerID id); -Result nfpRestore(HidControllerID id); +Result nfpSetApplicationArea(const NfcDeviceHandle *handle, const void* buf, size_t buf_size); +Result nfpFlush(const NfcDeviceHandle *handle); +Result nfpRestore(const NfcDeviceHandle *handle); /// Not available with ::NfpServiceType_System. -Result nfpCreateApplicationArea(HidControllerID id, u32 app_id, const void* buf, size_t buf_size); +Result nfpCreateApplicationArea(const NfcDeviceHandle *handle, u32 app_id, const void* buf, size_t buf_size); -Result nfpGetTagInfo(HidControllerID id, NfpTagInfo *out); -Result nfpGetRegisterInfo(HidControllerID id, NfpRegisterInfo *out); -Result nfpGetCommonInfo(HidControllerID id, NfpCommonInfo *out); -Result nfpGetModelInfo(HidControllerID id, NfpModelInfo *out); +Result nfpGetTagInfo(const NfcDeviceHandle *handle, NfpTagInfo *out); +Result nfpGetRegisterInfo(const NfcDeviceHandle *handle, NfpRegisterInfo *out); +Result nfpGetCommonInfo(const NfcDeviceHandle *handle, NfpCommonInfo *out); +Result nfpGetModelInfo(const NfcDeviceHandle *handle, NfpModelInfo *out); /// Returned event will have autoclear off. -Result nfpAttachActivateEvent(HidControllerID id, Event *out_event); +Result nfpAttachActivateEvent(const NfcDeviceHandle *handle, Event *out_event); /// Returned event will have autoclear off. -Result nfpAttachDeactivateEvent(HidControllerID id, Event *out_event); +Result nfpAttachDeactivateEvent(const NfcDeviceHandle *handle, Event *out_event); Result nfpGetState(NfpState *out); -Result nfpGetDeviceState(HidControllerID id, NfpDeviceState *out); -Result nfpGetNpadId(HidControllerID id, u32 *out); +Result nfpGetDeviceState(const NfcDeviceHandle *handle, NfpDeviceState *out); +Result nfpGetNpadId(const NfcDeviceHandle *handle, u32 *out); /// Returned event will have autoclear on. /// Only available with [3.0.0+]. diff --git a/nx/source/services/nfc.c b/nx/source/services/nfc.c index 0ca20662..d5c1bc1f 100644 --- a/nx/source/services/nfc.c +++ b/nx/source/services/nfc.c @@ -23,14 +23,13 @@ static const NfcRequiredMcuVersionData g_nfcVersionData[2] = { }, }; -static Result _nfpCreateInterface(Service* srv, Service* srv_out); -static Result _nfpInterfaceInitialize(Service* srv, u64 aruid, const NfcRequiredMcuVersionData *version, s32 count, u32 cmd_id); -static Result _nfpInterfaceFinalize(Service* srv, u32 cmd_id); +static Result _nfcCreateInterface(Service* srv, Service* srv_out); +static Result _nfcInterfaceInitialize(Service* srv, u64 aruid, const NfcRequiredMcuVersionData *version, s32 count, u32 cmd_id); -static Result _nfpCmdNoIO(Service* srv, u32 cmd_id); -static Result _nfpCmdInIdNoOut(Service* srv, HidControllerID id, u32 cmd_id); -static Result _nfpCmdInIdOutEvent(Service* srv, HidControllerID id, Event *out_event, u32 cmd_id); -static Result _nfpCmdInIdOutBuffer(Service* srv, HidControllerID id, void* buf, size_t buf_size, u32 cmd_id); +static Result _nfcCmdNoIO(Service* srv, u32 cmd_id); +static Result _nfcCmdInDevhandleNoOut(Service* srv, const NfcDeviceHandle *handle, u32 cmd_id); +static Result _nfcCmdInDevhandleOutEvent(Service* srv, const NfcDeviceHandle *handle, Event *out_event, u32 cmd_id); +static Result _nfcCmdInDevhandleOutBuffer(Service* srv, const NfcDeviceHandle *handle, void* buf, size_t buf_size, u32 cmd_id); NX_GENERATE_SERVICE_GUARD(nfp); @@ -67,16 +66,16 @@ Result _nfpInitialize(void) { rc = serviceConvertToDomain(&g_nfpSrv); if (R_SUCCEEDED(rc)) - rc = _nfpCreateInterface(&g_nfpSrv, &g_nfpInterface); + rc = _nfcCreateInterface(&g_nfpSrv, &g_nfpInterface); if (R_SUCCEEDED(rc)) - rc = _nfpInterfaceInitialize(&g_nfpInterface, aruid, g_nfcVersionData, 2, 0); + rc = _nfcInterfaceInitialize(&g_nfpInterface, aruid, g_nfcVersionData, 2, 0); return rc; } void _nfpCleanup(void) { - _nfpInterfaceFinalize(&g_nfpInterface, 1); + _nfcCmdNoIO(&g_nfpInterface, 1); // Finalize serviceClose(&g_nfpInterface); serviceClose(&g_nfpSrv); g_nfpServiceType = NfpServiceType_NotInitialized; @@ -106,16 +105,16 @@ Result _nfcInitialize(void) { rc = serviceConvertToDomain(&g_nfcSrv); if (R_SUCCEEDED(rc)) - rc = _nfpCreateInterface(&g_nfcSrv, &g_nfcInterface); + rc = _nfcCreateInterface(&g_nfcSrv, &g_nfcInterface); if (R_SUCCEEDED(rc)) - rc = _nfpInterfaceInitialize(&g_nfcInterface, aruid, g_nfcVersionData, 2, hosversionBefore(4,0,0) ? 0 : 400); + rc = _nfcInterfaceInitialize(&g_nfcInterface, aruid, g_nfcVersionData, 2, hosversionBefore(4,0,0) ? 0 : 400); return rc; } void _nfcCleanup(void) { - _nfpInterfaceFinalize(&g_nfcInterface, hosversionBefore(4,0,0) ? 1 : 401); + _nfcCmdNoIO(&g_nfcInterface, hosversionBefore(4,0,0) ? 1 : 401); // Finalize serviceClose(&g_nfcInterface); serviceClose(&g_nfcSrv); g_nfcServiceType = NfcServiceType_NotInitialized; @@ -137,7 +136,7 @@ Service* nfcGetServiceSession_Interface(void) { return &g_nfpInterface; } -static Result _nfpCreateInterface(Service* srv, Service* srv_out) { +static Result _nfcCreateInterface(Service* srv, Service* srv_out) { serviceAssumeDomain(srv); return serviceDispatch(srv, 0, .out_num_objects = 1, @@ -145,7 +144,7 @@ static Result _nfpCreateInterface(Service* srv, Service* srv_out) { ); } -static Result _nfpCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { +static Result _nfcCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatch(srv, cmd_id, .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, @@ -153,54 +152,51 @@ static Result _nfpCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { ); } -static Result _fpuCmdGetEvent(Service* srv, Event* out_event, bool autoclear, u32 cmd_id) { +static Result _nfcCmdGetEvent(Service* srv, Event* out_event, bool autoclear, u32 cmd_id) { Handle tmp_handle = INVALID_HANDLE; Result rc = 0; - rc = _nfpCmdGetHandle(srv, &tmp_handle, cmd_id); + rc = _nfcCmdGetHandle(srv, &tmp_handle, cmd_id); if (R_SUCCEEDED(rc)) eventLoadRemote(out_event, tmp_handle, autoclear); return rc; } -static Result _nfpCmdNoIO(Service* srv, u32 cmd_id) { +static Result _nfcCmdNoIO(Service* srv, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatch(srv, cmd_id); } -static Result _nfpCmdInIdNoOut(Service* srv, HidControllerID id, u32 cmd_id) { +static Result _nfcCmdInDevhandleNoOut(Service* srv, const NfcDeviceHandle *handle, u32 cmd_id) { serviceAssumeDomain(srv); - u64 tmp = hidControllerIDToOfficial(id); - return serviceDispatchIn(srv, cmd_id, tmp); + return serviceDispatchIn(srv, cmd_id, *handle); } -static Result _nfpCmdInIdOutU32(Service* srv, HidControllerID id, u32 *out, u32 cmd_id) { +static Result _nfcCmdInDevhandleOutU32(Service* srv, const NfcDeviceHandle *handle, u32 *out, u32 cmd_id) { serviceAssumeDomain(srv); - u64 tmp = hidControllerIDToOfficial(id); - return serviceDispatchInOut(srv, cmd_id, tmp, *out); + return serviceDispatchInOut(srv, cmd_id, *handle, *out); } -static Result _nfpCmdNoInOutU32(Service* srv, u32 *out, u32 cmd_id) { +static Result _nfcCmdNoInOutU32(Service* srv, u32 *out, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchOut(srv, cmd_id, *out); } -static Result _nfpCmdNoInOutU8(Service* srv, u8 *out, u32 cmd_id) { +static Result _nfcCmdNoInOutU8(Service* srv, u8 *out, u32 cmd_id) { serviceAssumeDomain(srv); return serviceDispatchOut(srv, cmd_id, *out); } -static Result _nfpCmdNoInOutBool(Service* srv, bool *out, u32 cmd_id) { +static Result _nfcCmdNoInOutBool(Service* srv, bool *out, u32 cmd_id) { u8 tmp=0; - Result rc = _nfpCmdNoInOutU8(srv, &tmp, cmd_id); + Result rc = _nfcCmdNoInOutU8(srv, &tmp, cmd_id); if (R_SUCCEEDED(rc) && out) *out = tmp!=0; return rc; } -static Result _nfpCmdInIdOutEvent(Service* srv, HidControllerID id, Event *out_event, u32 cmd_id) { +static Result _nfcCmdInDevhandleOutEvent(Service* srv, const NfcDeviceHandle *handle, Event *out_event, u32 cmd_id) { Handle tmp_handle = INVALID_HANDLE; - u64 tmp = hidControllerIDToOfficial(id); serviceAssumeDomain(srv); - Result rc = serviceDispatchIn(srv, cmd_id, tmp, + Result rc = serviceDispatchIn(srv, cmd_id, *handle, .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, .out_handles = &tmp_handle, ); @@ -208,16 +204,15 @@ static Result _nfpCmdInIdOutEvent(Service* srv, HidControllerID id, Event *out_e return rc; } -static Result _nfpCmdInIdOutBuffer(Service* srv, HidControllerID id, void* buf, size_t buf_size, u32 cmd_id) { - u64 tmp = hidControllerIDToOfficial(id); +static Result _nfcCmdInDevhandleOutBuffer(Service* srv, const NfcDeviceHandle *handle, void* buf, size_t buf_size, u32 cmd_id) { serviceAssumeDomain(srv); - return serviceDispatchIn(srv, cmd_id, tmp, + return serviceDispatchIn(srv, cmd_id, *handle, .buffer_attrs = { SfBufferAttr_FixedSize | SfBufferAttr_HipcPointer | SfBufferAttr_Out }, .buffers = { { buf, buf_size } }, ); } -static Result _nfpInterfaceInitialize(Service* srv, u64 aruid, const NfcRequiredMcuVersionData *version, s32 count, u32 cmd_id) { +static Result _nfcInterfaceInitialize(Service* srv, u64 aruid, const NfcRequiredMcuVersionData *version, s32 count, u32 cmd_id) { const struct { u64 aruid; u64 zero; @@ -231,108 +226,88 @@ static Result _nfpInterfaceInitialize(Service* srv, u64 aruid, const NfcRequired ); } -static Result _nfpInterfaceFinalize(Service* srv, u32 cmd_id) { - return _nfpCmdNoIO(srv, cmd_id); -} - -Result nfpListDevices(s32 *count, HidControllerID *out, size_t num_elements) { - // This is the maximum number of controllers that can be connected to a console at a time - // Incidentally, this is the biggest value official software (SSBU) was observed using - size_t max_controllers = 9; - if (num_elements > max_controllers) - num_elements = max_controllers; - - u64 buf[max_controllers]; - memset(buf, 0, sizeof(buf)); - +Result nfpListDevices(s32 *total_out, NfcDeviceHandle *out, s32 count) { serviceAssumeDomain(&g_nfpInterface); - Result rc = serviceDispatchOut(&g_nfpInterface, 2, *count, + return serviceDispatchOut(&g_nfpInterface, 2, *total_out, .buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_Out }, - .buffers = { { buf, max_controllers*sizeof(u64) } }, + .buffers = { { out, count*sizeof(NfcDeviceHandle) } }, ); - if (R_SUCCEEDED(rc) && out) { - for (size_t i=0; i