diff --git a/nx/include/switch/services/fs.h b/nx/include/switch/services/fs.h index 9d5d4b0a..ea85665d 100644 --- a/nx/include/switch/services/fs.h +++ b/nx/include/switch/services/fs.h @@ -80,7 +80,7 @@ typedef enum Result fsInitialize(void); void fsExit(void); -Handle fsGetServiceSession(void); +Service* fsGetServiceSession(void); Result fsMountSdcard(FsFileSystem* out); Result fsMountSaveData(FsFileSystem* out, u8 inval, FsSave *save); diff --git a/nx/include/switch/services/hid.h b/nx/include/switch/services/hid.h index 9b2eacc2..69478171 100644 --- a/nx/include/switch/services/hid.h +++ b/nx/include/switch/services/hid.h @@ -514,7 +514,7 @@ Result hidInitialize(void); void hidExit(void); void hidReset(void); -Handle hidGetSessionService(void); +Service* hidGetSessionService(void); void* hidGetSharedmemAddr(void); void hidSetControllerLayout(HidControllerID id, HidControllerLayoutType layoutType); diff --git a/nx/include/switch/services/sm.h b/nx/include/switch/services/sm.h index d70451c1..6a5c30e4 100644 --- a/nx/include/switch/services/sm.h +++ b/nx/include/switch/services/sm.h @@ -1,6 +1,60 @@ +typedef enum { + ServiceType_Uninitialized, + ServiceType_Normal, + ServiceType_Override +} ServiceType; + +typedef struct { + Handle handle; + ServiceType type; +} Service; + +static inline bool serviceIsOverride(Service* s) { + return s->type == ServiceType_Override; +} + +static inline bool serviceIsActive(Service* s) { + return s->type != ServiceType_Uninitialized; +} + +static inline bool serviceIpcDispatch(Service* s) { + return ipcDispatch(s->handle); +} + +static inline void serviceCreate(Service* s, Handle h) { + s->handle = h; + s->type = ServiceType_Normal; +} + +static inline void serviceClose(Service* s) { + switch (s->type) { + + case ServiceType_Normal: + svcCloseHandle(s->handle); + break; + + case ServiceType_Override: + // Don't close because we don't own the overridden handle. + break; + + case ServiceType_Uninitialized: + break; + } + + s->type = ServiceType_Uninitialized; +} + Result smInitialize(void); -void smExit(void); -Result smGetService(Handle* handle_out, const char* name); +void smExit(void); +Result smGetService(Service* service_out, const char* name); +Result smGetServiceOriginal(Handle* handle_out, u64 name); +Handle smGetServiceOverride(u64 name); Result smRegisterService(Handle* handle_out, const char* name, bool is_light, int max_sessions); Result smUnregisterService(const char* name); -bool smHasInitialized(void); +bool smHasInitialized(void); +u64 smEncodeName(const char* name); +void smAddOverrideHandle(u64 name, Handle handle); + +bool serviceIsOverride(Service* s); +bool serviceIsInitialized(Service* s); +void serviceClose(Service* s); diff --git a/nx/include/switch/services/usb.h b/nx/include/switch/services/usb.h index 3bde52c2..8cb5e3e6 100644 --- a/nx/include/switch/services/usb.h +++ b/nx/include/switch/services/usb.h @@ -12,36 +12,23 @@ /// Imported from libusb, with some adjustments. struct usb_endpoint_descriptor { uint8_t bLength; - uint8_t bDescriptorType; /// Must match USB_DT_ENDPOINT. - uint8_t bEndpointAddress; /// Should be one of the usb_endpoint_direction values, the endpoint-number is automatically allocated. - uint8_t bmAttributes; - uint16_t wMaxPacketSize; - uint8_t bInterval; }; /// Imported from libusb, with some adjustments. struct usb_interface_descriptor { uint8_t bLength; - uint8_t bDescriptorType; /// Must match USB_DT_INTERFACE. - uint8_t bInterfaceNumber; /// See also USBDS_DEFAULT_InterfaceNumber. - uint8_t bAlternateSetting; /// Must match 0. - uint8_t bNumEndpoints; /// Ignored. - uint8_t bInterfaceClass; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - uint8_t iInterface; /// Ignored. }; @@ -69,7 +56,7 @@ typedef struct { typedef struct { bool initialized; u32 interface_index; - Handle h; + Service h; Handle SetupEvent; Handle CtrlInCompletionEvent; @@ -78,131 +65,90 @@ typedef struct { typedef struct { bool initialized; - Handle h; - + Service h; Handle CompletionEvent; } UsbDsEndpoint; -typedef enum -{ - USBCOMPLEXID_Default = 0x2 -} usbComplexId; +typedef enum { + UsbComplexId_Default = 0x2 +} UsbComplexId; /// Imported from libusb, with changed names. enum usb_class_code { USB_CLASS_PER_INTERFACE = 0, - USB_CLASS_AUDIO = 1, - USB_CLASS_COMM = 2, - USB_CLASS_HID = 3, - USB_CLASS_PHYSICAL = 5, - USB_CLASS_PRINTER = 7, - USB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */ USB_CLASS_IMAGE = 6, - USB_CLASS_MASS_STORAGE = 8, - USB_CLASS_HUB = 9, - USB_CLASS_DATA = 10, - USB_CLASS_SMART_CARD = 0x0b, - USB_CLASS_CONTENT_SECURITY = 0x0d, - USB_CLASS_VIDEO = 0x0e, - USB_CLASS_PERSONAL_HEALTHCARE = 0x0f, - USB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, - USB_CLASS_WIRELESS = 0xe0, - USB_CLASS_APPLICATION = 0xfe, - USB_CLASS_VENDOR_SPEC = 0xff }; /// Imported from libusb, with changed names. enum usb_descriptor_type { USB_DT_DEVICE = 0x01, - USB_DT_CONFIG = 0x02, - USB_DT_STRING = 0x03, - USB_DT_INTERFACE = 0x04, - USB_DT_ENDPOINT = 0x05, - USB_DT_BOS = 0x0f, - USB_DT_DEVICE_CAPABILITY = 0x10, - USB_DT_HID = 0x21, - USB_DT_REPORT = 0x22, - USB_DT_PHYSICAL = 0x23, - USB_DT_HUB = 0x29, - USB_DT_SUPERSPEED_HUB = 0x2a, - USB_DT_SS_ENDPOINT_COMPANION = 0x30 }; /// Imported from libusb, with changed names. enum usb_endpoint_direction { USB_ENDPOINT_IN = 0x80, - USB_ENDPOINT_OUT = 0x00 }; /// Imported from libusb, with changed names. enum usb_transfer_type { USB_TRANSFER_TYPE_CONTROL = 0, - USB_TRANSFER_TYPE_ISOCHRONOUS = 1, - USB_TRANSFER_TYPE_BULK = 2, - USB_TRANSFER_TYPE_INTERRUPT = 3, - USB_TRANSFER_TYPE_BULK_STREAM = 4, }; /// Imported from libusb, with changed names. enum usb_iso_sync_type { USB_ISO_SYNC_TYPE_NONE = 0, - USB_ISO_SYNC_TYPE_ASYNC = 1, - USB_ISO_SYNC_TYPE_ADAPTIVE = 2, - USB_ISO_SYNC_TYPE_SYNC = 3 }; /// Imported from libusb, with changed names. enum usb_iso_usage_type { USB_ISO_USAGE_TYPE_DATA = 0, - USB_ISO_USAGE_TYPE_FEEDBACK = 1, - USB_ISO_USAGE_TYPE_IMPLICIT = 2, }; -Result usbDsInitialize(usbComplexId complexId, const usbDsDeviceInfo* deviceinfo); +Result usbDsInitialize(UsbComplexId complexId, const usbDsDeviceInfo* deviceinfo); /// Exit usbDs. Any interfaces/endpoints which are left open are automatically closed, since otherwise usb-sysmodule won't fully reset usbds to defaults. void usbDsExit(void); -Handle usbDsGetServiceSession(void); +Service* usbDsGetServiceSession(void); Handle usbDsGetStateChangeEvent(void); Result usbDsGetState(u32 *out); diff --git a/nx/source/runtime/devices/usb_comms.c b/nx/source/runtime/devices/usb_comms.c index ae697229..6a7c9615 100644 --- a/nx/source/runtime/devices/usb_comms.c +++ b/nx/source/runtime/devices/usb_comms.c @@ -19,7 +19,7 @@ Result usbCommsInitialize(void) Result ret=0; - ret = usbDsInitialize(USBCOMPLEXID_Default, NULL); + ret = usbDsInitialize(UsbComplexId_Default, NULL); if (R_SUCCEEDED(ret)) { //The buffer for PostBufferAsync commands must be 0x1000-byte aligned. diff --git a/nx/source/services/acc.c b/nx/source/services/acc.c index c9e4f208..ee001013 100644 --- a/nx/source/services/acc.c +++ b/nx/source/services/acc.c @@ -1,36 +1,26 @@ #include #include -static Handle g_accountServiceSession = INVALID_HANDLE; +static Service g_accSrv; -Result accountInitialize(void) { - if (g_accountServiceSession != INVALID_HANDLE) return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); - - Result rc = 0; - - rc = smGetService(&g_accountServiceSession, "acc:u1"); - if (R_FAILED(rc)) return rc; - - return rc; -} - -void accountExit(void) +Result accountInitialize(void) { - if (g_accountServiceSession == INVALID_HANDLE) return; + if (serviceIsActive(&g_accSrv)) + return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); - if (g_accountServiceSession != INVALID_HANDLE) { - svcCloseHandle(g_accountServiceSession); - g_accountServiceSession = INVALID_HANDLE; - } + return smGetService(&g_accSrv, "acc:u1"); } -Handle accountGetSessionService(void) { - return g_accountServiceSession; +void accountExit(void) { + serviceClose(&g_accSrv); } -Result accountGetActiveUser(u128 *userID, bool *account_selected) { - if (g_accountServiceSession == INVALID_HANDLE) return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); +Service* accountGetService(void) { + return &g_accSrv; +} +Result accountGetActiveUser(u128 *userID, bool *account_selected) +{ IpcCommand c; ipcInitialize(&c); @@ -44,7 +34,7 @@ Result accountGetActiveUser(u128 *userID, bool *account_selected) { raw->magic = SFCI_MAGIC; raw->cmd_id = 4; - Result rc = ipcDispatch(g_accountServiceSession); + Result rc = serviceIpcDispatch(&g_accSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/apm.c b/nx/source/services/apm.c index f8674128..d53e3c8b 100644 --- a/nx/source/services/apm.c +++ b/nx/source/services/apm.c @@ -1,42 +1,41 @@ #include #include -static Handle g_apmServiceSession = INVALID_HANDLE; -static Handle g_apmISession = INVALID_HANDLE; +static Service g_apmSrv; +static Service g_apmISession; -static Result _apmGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id); +static Result _apmGetSession(Service* srv, Service* srv_out, u64 cmd_id); -Result apmInitialize(void) { - if (g_apmServiceSession != INVALID_HANDLE) return 0; +Result apmInitialize(void) +{ + if (serviceIsActive(&g_apmSrv)) + return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); Result rc = 0; - rc = smGetService(&g_apmServiceSession, "apm:p"); - if (R_FAILED(rc)) rc = smGetService(&g_apmServiceSession, "apm"); + rc = smGetService(&g_apmSrv, "apm:p"); - if (R_SUCCEEDED(rc)) rc = _apmGetSession(g_apmServiceSession, &g_apmISession, 0);//OpenSession. Official sw doesn't open this until using commands which need it, when it wasn't already opened. + if (R_FAILED(rc)) + rc = smGetService(&g_apmSrv, "apm"); - if (R_FAILED(rc)) apmExit(); + // OpenSession. + // Official sw doesn't open this until using commands which need it, when it wasn't already opened. + if (R_SUCCEEDED(rc)) + rc = _apmGetSession(&g_apmSrv, &g_apmISession, 0); + + if (R_FAILED(rc)) + apmExit(); return rc; } void apmExit(void) { - if (g_apmServiceSession == INVALID_HANDLE) return; - - if (g_apmServiceSession != INVALID_HANDLE) { - svcCloseHandle(g_apmServiceSession); - g_apmServiceSession = INVALID_HANDLE; - } - - if (g_apmISession != INVALID_HANDLE) { - svcCloseHandle(g_apmISession); - g_apmISession = INVALID_HANDLE; - } + serviceClose(&g_apmISession); + serviceClose(&g_apmSrv); } -static Result _apmGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id) { +static Result _apmGetSession(Service* srv, Service* srv_out, u64 cmd_id) { IpcCommand c; ipcInitialize(&c); @@ -50,7 +49,7 @@ static Result _apmGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_i raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -64,7 +63,7 @@ static Result _apmGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_i rc = resp->result; if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; + serviceCreate(srv_out, r.Handles[0]); } } @@ -89,7 +88,7 @@ Result apmSetPerformanceConfiguration(u32 PerformanceMode, u32 PerformanceConfig raw->PerformanceMode = PerformanceMode; raw->PerformanceConfiguration = PerformanceConfiguration; - Result rc = ipcDispatch(g_apmISession); + Result rc = serviceIpcDispatch(&g_apmISession); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -122,7 +121,7 @@ Result apmGetPerformanceConfiguration(u32 PerformanceMode, u32 *PerformanceConfi raw->cmd_id = 1; raw->PerformanceMode = PerformanceMode; - Result rc = ipcDispatch(g_apmISession); + Result rc = serviceIpcDispatch(&g_apmISession); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/applet.c b/nx/source/services/applet.c index 6b0d35a0..63d0e8eb 100644 --- a/nx/source/services/applet.c +++ b/nx/source/services/applet.c @@ -6,27 +6,26 @@ __attribute__((weak)) bool __nx_applet_auto_notifyrunning = true; __attribute__((weak)) u8 __nx_applet_AppletAttribute[0x80]; __attribute__((weak)) u32 __nx_applet_PerformanceConfiguration[2] = {/*0x92220008*//*0x20004*//*0x92220007*/0, 0}; -static Handle g_appletServiceSession = INVALID_HANDLE; -static Handle g_appletProxySession = INVALID_HANDLE; +static Service g_appletSrv; +static Service g_appletProxySession; // From Get*Functions, for ILibraryAppletProxy. This is GetLibraryAppletSelfAccessor -static Handle g_appletIFunctions = INVALID_HANDLE; +static Service g_appletIFunctions; -static Handle g_appletILibraryAppletCreator = INVALID_HANDLE; -static Handle g_appletICommonStateGetter = INVALID_HANDLE; -static Handle g_appletISelfController = INVALID_HANDLE; -static Handle g_appletIWindowController = INVALID_HANDLE; -static Handle g_appletIAudioController = INVALID_HANDLE; -static Handle g_appletIDisplayController = INVALID_HANDLE; -static Handle g_appletIDebugFunctions = INVALID_HANDLE; +static Service g_appletILibraryAppletCreator; +static Service g_appletICommonStateGetter; +static Service g_appletISelfController; +static Service g_appletIWindowController; +static Service g_appletIAudioController; +static Service g_appletIDisplayController; +static Service g_appletIDebugFunctions; static Handle g_appletMessageEventHandle = INVALID_HANDLE; static u64 g_appletResourceUserId = 0; - -static u8 g_appletOperationMode; +static u8 g_appletOperationMode; static u32 g_appletPerformanceMode; -static u8 g_appletFocusState; +static u8 g_appletFocusState; static bool g_appletNotifiedRunning = 0; @@ -34,19 +33,20 @@ static AppletHookCookie g_appletFirstHook; void appletExit(void); -static Result _appletGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id); -static Result _appletGetSessionProxy(Handle sessionhandle, Handle* handle_out, u64 cmd_id, Handle prochandle, u8 *AppletAttribute); +static Result _appletGetHandle(Service* srv, Handle* handle_out, u64 cmd_id); +static Result _appletGetSession(Service* srv, Service* srv_out, u64 cmd_id); +static Result _appletGetSessionProxy(Service* srv_out, u64 cmd_id, Handle prochandle, u8 *AppletAttribute); static Result _appletGetAppletResourceUserId(u64 *out); static Result appletSetFocusHandlingMode(u32 mode); - -static Result _appletReceiveMessage(u32 *out); static Result _appletGetCurrentFocusState(u8 *out); -static Result _appletAcquireForegroundRights(void); static Result _appletSetFocusHandlingMode(u8 inval0, u8 inval1, u8 inval2); static Result _appletSetOutOfFocusSuspendingEnabled(u8 inval); +static Result _appletReceiveMessage(u32 *out); +static Result _appletAcquireForegroundRights(void); + static Result _appletGetOperationMode(u8 *out); static Result _appletGetPerformanceMode(u32 *out); @@ -56,10 +56,10 @@ static Result _appletSetPerformanceModeChangedNotification(u8 flag); Result appletInitialize(void) { - if (g_appletServiceSession != INVALID_HANDLE) + if (serviceIsActive(&g_appletSrv)) return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); - if (__nx_applet_type==AppletType_None) + if (__nx_applet_type == AppletType_None) return 0; Result rc = 0; @@ -72,10 +72,10 @@ Result appletInitialize(void) __nx_applet_type = AppletType_Application; // Fallthrough. case AppletType_Application: - rc = smGetService(&g_appletServiceSession, "appletOE"); + rc = smGetService(&g_appletSrv, "appletOE"); break; default: - rc = smGetService(&g_appletServiceSession, "appletAE"); + rc = smGetService(&g_appletSrv, "appletAE"); break; } @@ -91,10 +91,11 @@ Result appletInitialize(void) case AppletType_LibraryApplet: cmd_id = 200; break; case AppletType_OverlayApplet: cmd_id = 300; break; case AppletType_SystemApplication: cmd_id = 350; break; + // TODO: Replace error code default: fatalSimple(MAKERESULT(MODULE_LIBNX, LIBNX_NOTFOUND)); } - rc = _appletGetSessionProxy(g_appletServiceSession, &g_appletProxySession, cmd_id, CUR_PROCESS_HANDLE, NULL); + rc = _appletGetSessionProxy(&g_appletProxySession, cmd_id, CUR_PROCESS_HANDLE, NULL); if (rc == APT_BUSY_ERROR) { svcSleepThread(10000000); @@ -105,39 +106,39 @@ Result appletInitialize(void) // Get*Functions, for ILibraryAppletProxy this is GetLibraryAppletSelfAccessor if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletIFunctions, 20); + rc = _appletGetSession(&g_appletSrv, &g_appletIFunctions, 20); // TODO: Add non-application type-specific session init here. // GetLibraryAppletCreator if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletILibraryAppletCreator, 11); + rc = _appletGetSession(&g_appletSrv, &g_appletILibraryAppletCreator, 11); // GetCommonStateGetter if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletICommonStateGetter, 0); + rc = _appletGetSession(&g_appletSrv, &g_appletICommonStateGetter, 0); // GetSelfController if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletISelfController, 1); + rc = _appletGetSession(&g_appletSrv, &g_appletISelfController, 1); // GetWindowController if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletIWindowController, 2); + rc = _appletGetSession(&g_appletSrv, &g_appletIWindowController, 2); // Get AppletResourceUserId. if (R_SUCCEEDED(rc)) rc = _appletGetAppletResourceUserId(&g_appletResourceUserId); // GetAudioController if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletIAudioController, 3); + rc = _appletGetSession(&g_appletSrv, &g_appletIAudioController, 3); // GetDisplayController if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletIDisplayController, 4); + rc = _appletGetSession(&g_appletSrv, &g_appletIDisplayController, 4); // GetDebugFunctions if (R_SUCCEEDED(rc)) - rc = _appletGetSession(g_appletProxySession, &g_appletIDebugFunctions, 1000); + rc = _appletGetSession(&g_appletSrv, &g_appletIDebugFunctions, 1000); if (R_SUCCEEDED(rc) && (__nx_applet_type == AppletType_Application)) { - // Reuse _appletGetSession since ipc input/output is the same. This is ICommonStateGetter::GetEventHandle. - rc = _appletGetSession(g_appletICommonStateGetter, &g_appletMessageEventHandle, 0); + // ICommonStateGetter::GetEventHandle + rc = _appletGetHandle(&g_appletICommonStateGetter, &g_appletMessageEventHandle, 0); if (R_SUCCEEDED(rc)) { do { @@ -214,9 +215,6 @@ Result appletInitialize(void) void appletExit(void) { - if (g_appletServiceSession == INVALID_HANDLE) - return; - apmExit(); if (g_appletMessageEventHandle != INVALID_HANDLE) { @@ -224,56 +222,16 @@ void appletExit(void) g_appletMessageEventHandle = INVALID_HANDLE; } - if (g_appletServiceSession != INVALID_HANDLE) { - svcCloseHandle(g_appletServiceSession); - g_appletServiceSession = INVALID_HANDLE; - } - - if (g_appletProxySession != INVALID_HANDLE) { - svcCloseHandle(g_appletProxySession); - g_appletProxySession = INVALID_HANDLE; - } - - if (g_appletIFunctions != INVALID_HANDLE) { - svcCloseHandle(g_appletIFunctions); - g_appletIFunctions = INVALID_HANDLE; - } - - if (g_appletILibraryAppletCreator != INVALID_HANDLE) { - svcCloseHandle(g_appletILibraryAppletCreator); - g_appletILibraryAppletCreator = INVALID_HANDLE; - } - - if (g_appletICommonStateGetter != INVALID_HANDLE) { - svcCloseHandle(g_appletICommonStateGetter); - g_appletICommonStateGetter = INVALID_HANDLE; - } - - if (g_appletISelfController != INVALID_HANDLE) { - svcCloseHandle(g_appletISelfController); - g_appletISelfController = INVALID_HANDLE; - } - - if (g_appletIWindowController != INVALID_HANDLE) { - svcCloseHandle(g_appletIWindowController); - g_appletIWindowController = INVALID_HANDLE; - } - - if (g_appletIAudioController != INVALID_HANDLE) { - svcCloseHandle(g_appletIAudioController); - g_appletIAudioController = INVALID_HANDLE; - } - - if (g_appletIDisplayController != INVALID_HANDLE) { - svcCloseHandle(g_appletIDisplayController); - g_appletIDisplayController = INVALID_HANDLE; - } - - if (g_appletIDebugFunctions != INVALID_HANDLE) { - svcCloseHandle(g_appletIDebugFunctions); - g_appletIDebugFunctions = INVALID_HANDLE; - } + serviceClose(&g_appletIFunctions); + serviceClose(&g_appletILibraryAppletCreator); + serviceClose(&g_appletICommonStateGetter); + serviceClose(&g_appletISelfController); + serviceClose(&g_appletIWindowController); + serviceClose(&g_appletIAudioController); + serviceClose(&g_appletIDisplayController); + serviceClose(&g_appletIDebugFunctions); + serviceClose(&g_appletSrv); g_appletResourceUserId = 0; } @@ -286,7 +244,7 @@ static void appletCallHook(AppletHookType hookType) void appletHook(AppletHookCookie* cookie, AppletHookFn callback, void* param) { - if (!callback) + if (callback == NULL) return; AppletHookCookie* hook = &g_appletFirstHook; @@ -350,7 +308,7 @@ static Result appletSetFocusHandlingMode(u32 mode) { return rc; } -static Result _appletGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id) { +static Result _appletGetHandle(Service* srv, Handle* handle_out, u64 cmd_id) { IpcCommand c; ipcInitialize(&c); @@ -364,7 +322,7 @@ static Result _appletGetSession(Handle sessionhandle, Handle* handle_out, u64 cm raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -385,7 +343,20 @@ static Result _appletGetSession(Handle sessionhandle, Handle* handle_out, u64 cm return rc; } -static Result _appletGetSessionProxy(Handle sessionhandle, Handle* handle_out, u64 cmd_id, Handle prochandle, u8 *AppletAttribute) { +static Result _appletGetSession(Service* srv, Service* srv_out, u64 cmd_id) { + Result rc; + Handle handle; + + rc = _appletGetHandle(srv, &handle, cmd_id); + + if (R_SUCCEEDED(rc)) { + serviceCreate(srv_out, handle); + } + + return rc; +} + +static Result _appletGetSessionProxy(Service* srv_out, u64 cmd_id, Handle prochandle, u8 *AppletAttribute) { IpcCommand c; ipcInitialize(&c); @@ -405,7 +376,7 @@ static Result _appletGetSessionProxy(Handle sessionhandle, Handle* handle_out, u raw->cmd_id = cmd_id; raw->reserved = 0; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(&g_appletSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -419,7 +390,7 @@ static Result _appletGetSessionProxy(Handle sessionhandle, Handle* handle_out, u rc = resp->result; if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; + serviceCreate(srv_out, r.Handles[0]); } } @@ -440,7 +411,7 @@ static Result _appletGetAppletResourceUserId(u64 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 1; - Result rc = ipcDispatch(g_appletIWindowController); + Result rc = serviceIpcDispatch(&g_appletIWindowController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -476,7 +447,7 @@ static Result _appletAcquireForegroundRights(void) { raw->magic = SFCI_MAGIC; raw->cmd_id = 10; - Result rc = ipcDispatch(g_appletIWindowController); + Result rc = serviceIpcDispatch(&g_appletIWindowController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -494,7 +465,8 @@ static Result _appletAcquireForegroundRights(void) { } Result appletGetAppletResourceUserId(u64 *out) { - if (g_appletServiceSession == INVALID_HANDLE) return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if (!serviceIsActive(&g_appletSrv)) + return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); *out = g_appletResourceUserId; return 0; @@ -517,7 +489,7 @@ void appletNotifyRunning(u8 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 40; - Result rc = ipcDispatch(g_appletIFunctions); + Result rc = serviceIpcDispatch(&g_appletIFunctions); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -553,7 +525,7 @@ static Result _appletReceiveMessage(u32 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 1; - Result rc = ipcDispatch(g_appletICommonStateGetter); + Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -589,7 +561,7 @@ static Result _appletGetOperationMode(u8 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 5; - Result rc = ipcDispatch(g_appletICommonStateGetter); + Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -624,7 +596,7 @@ static Result _appletGetPerformanceMode(u32 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 6; - Result rc = ipcDispatch(g_appletICommonStateGetter); + Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -660,7 +632,7 @@ static Result _appletGetCurrentFocusState(u8 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 9; - Result rc = ipcDispatch(g_appletICommonStateGetter); + Result rc = serviceIpcDispatch(&g_appletICommonStateGetter); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -698,7 +670,7 @@ static Result _appletSetOperationModeChangedNotification(u8 flag) { raw->cmd_id = 11; raw->flag = flag; - Result rc = ipcDispatch(g_appletISelfController); + Result rc = serviceIpcDispatch(&g_appletISelfController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -731,7 +703,7 @@ static Result _appletSetPerformanceModeChangedNotification(u8 flag) { raw->cmd_id = 12; raw->flag = flag; - Result rc = ipcDispatch(g_appletISelfController); + Result rc = serviceIpcDispatch(&g_appletISelfController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -768,7 +740,7 @@ static Result _appletSetFocusHandlingMode(u8 inval0, u8 inval1, u8 inval2) { raw->inval1 = inval1; raw->inval2 = inval2; - Result rc = ipcDispatch(g_appletISelfController); + Result rc = serviceIpcDispatch(&g_appletISelfController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -801,7 +773,7 @@ static Result _appletSetOutOfFocusSuspendingEnabled(u8 inval) { raw->cmd_id = 16; raw->inval = inval; - Result rc = ipcDispatch(g_appletISelfController); + Result rc = serviceIpcDispatch(&g_appletISelfController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -832,7 +804,7 @@ Result appletCreateManagedDisplayLayer(u64 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 40; - Result rc = ipcDispatch(g_appletISelfController); + Result rc = serviceIpcDispatch(&g_appletISelfController); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/bsd.c b/nx/source/services/bsd.c index eaa0ced8..fd5dcec1 100644 --- a/nx/source/services/bsd.c +++ b/nx/source/services/bsd.c @@ -1,14 +1,14 @@ // Copyright 2017 plutoo #include -static Handle g_bsdClientHandle = -1; -static Handle g_bsdMonitorHandle = -1; +static Service g_bsdSrv; +static Service g_bsdMonitor; static u64 g_bsdClientPid = -1; static int g_Errno = 0; #define EPIPE 32 -static Result _bsdRegisterClient(Handle h, TransferMemory* tmem, u64* pid_out) { +static Result _bsdRegisterClient(Service* srv, TransferMemory* tmem, u64* pid_out) { IpcCommand c; ipcInitialize(&c); ipcSendPid(&c); @@ -33,7 +33,7 @@ static Result _bsdRegisterClient(Handle h, TransferMemory* tmem, u64* pid_out) { raw->unk0[4] = 13; raw->tmem_sz = tmem->size; - Result rc = ipcDispatch(h); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -52,7 +52,7 @@ static Result _bsdRegisterClient(Handle h, TransferMemory* tmem, u64* pid_out) { return rc; } -static Result _bsdStartMonitor(Handle h, u64 pid) { +static Result _bsdStartMonitor(Service* srv, u64 pid) { IpcCommand c; ipcInitialize(&c); ipcSendPid(&c); @@ -69,7 +69,7 @@ static Result _bsdStartMonitor(Handle h, u64 pid) { raw->cmd_id = 1; raw->pid = pid; - Result rc = ipcDispatch(h); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -88,21 +88,21 @@ static Result _bsdStartMonitor(Handle h, u64 pid) { Result bsdInitialize(TransferMemory* tmem) { const char* bsd_srv = "bsd:s"; - Result rc = smGetService(&g_bsdClientHandle, bsd_srv); + Result rc = smGetService(&g_bsdSrv, bsd_srv); if (R_FAILED(rc)) { bsd_srv = "bsd:u"; - rc = smGetService(&g_bsdClientHandle, bsd_srv); + rc = smGetService(&g_bsdSrv, bsd_srv); } if (R_SUCCEEDED(rc)) { - rc = smGetService(&g_bsdMonitorHandle, bsd_srv); + rc = smGetService(&g_bsdMonitor, bsd_srv); if (R_SUCCEEDED(rc)) { - rc = _bsdRegisterClient(g_bsdClientHandle, tmem, &g_bsdClientPid); + rc = _bsdRegisterClient(&g_bsdSrv, tmem, &g_bsdClientPid); if (R_SUCCEEDED(rc)) { - rc = _bsdStartMonitor(g_bsdMonitorHandle, g_bsdClientPid); + rc = _bsdStartMonitor(&g_bsdMonitor, g_bsdClientPid); } } } @@ -135,7 +135,7 @@ int bsdSocket(int domain, int type, int protocol) { raw->type = type; raw->protocol = protocol; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int fd = -1; if (R_SUCCEEDED(rc)) { @@ -183,7 +183,7 @@ int bsdRecv(int sockfd, void* buffer, size_t length, int flags) { raw->sockfd = sockfd; raw->flags = flags; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -232,7 +232,7 @@ int bsdSend(int sockfd, void* buffer, size_t length, int flags) { raw->sockfd = sockfd; raw->flags = flags; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -284,7 +284,7 @@ int bsdSendTo(int sockfd, void* buffer, size_t length, int flags, const struct b raw->sockfd = sockfd; raw->flags = flags; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -332,7 +332,7 @@ int bsdConnect(int sockfd, void* addr, u32 addrlen) { raw->cmd_id = 14; raw->sockfd = sockfd; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int fd = -1; if (R_SUCCEEDED(rc)) { @@ -378,7 +378,7 @@ int bsdBind(int sockfd, void* addr, u32 addrlen) { raw->magic = SFCI_MAGIC; raw->cmd_id = 13; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -426,7 +426,7 @@ int bsdListen(int sockfd, int backlog) { raw->sockfd = sockfd; raw->backlog = backlog; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -478,7 +478,7 @@ int bsdSetSockOpt(int sockfd, int level, int option_name, const void *option_val raw->level = level; raw->option_name = option_name; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { @@ -525,7 +525,7 @@ int bsdWrite(int sockfd, void* buffer, size_t length) { raw->cmd_id = 24; raw->sockfd = sockfd; - Result rc = ipcDispatch(g_bsdClientHandle); + Result rc = serviceIpcDispatch(&g_bsdSrv); int ret = -1; if (R_SUCCEEDED(rc)) { diff --git a/nx/source/services/fatal.c b/nx/source/services/fatal.c index c7913de9..82bd17a6 100644 --- a/nx/source/services/fatal.c +++ b/nx/source/services/fatal.c @@ -14,7 +14,7 @@ void fatalSimple(Result err) { if (R_SUCCEEDED(rc)) { Handle srv; - rc = smGetService(&srv, "fatal:u"); + rc = smGetServiceOriginal(&srv, smEncodeName("fatal:u")); if (R_SUCCEEDED(rc)) { IpcCommand c; diff --git a/nx/source/services/fs.c b/nx/source/services/fs.c index 6b0a8db9..4ad29451 100644 --- a/nx/source/services/fs.c +++ b/nx/source/services/fs.c @@ -1,13 +1,13 @@ // Copyright 2017 plutoo #include -static Handle g_fsHandle = INVALID_HANDLE; +static Service g_fsSrv; Result fsInitialize(void) { - if (g_fsHandle != INVALID_HANDLE) + if (serviceIsActive(&g_fsSrv)) return 0; - Result rc = smGetService(&g_fsHandle, "fsp-srv"); + Result rc = smGetService(&g_fsSrv, "fsp-srv"); if (R_SUCCEEDED(rc)) { IpcCommand c; @@ -26,7 +26,7 @@ Result fsInitialize(void) { raw->cmd_id = 1; raw->unk = 0; - rc = ipcDispatch(g_fsHandle); + rc = serviceIpcDispatch(&g_fsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -45,13 +45,11 @@ Result fsInitialize(void) { } void fsExit(void) { - if(g_fsHandle == INVALID_HANDLE)return; - svcCloseHandle(g_fsHandle); - g_fsHandle = INVALID_HANDLE; + serviceClose(&g_fsSrv); } -Handle fsGetServiceSession(void) { - return g_fsHandle; +Service* fsGetServiceSession(void) { + return &g_fsSrv; } Result fsMountSdcard(FsFileSystem* out) { @@ -68,7 +66,7 @@ Result fsMountSdcard(FsFileSystem* out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 18; - Result rc = ipcDispatch(g_fsHandle); + Result rc = serviceIpcDispatch(&g_fsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -107,7 +105,7 @@ Result fsMountSaveData(FsFileSystem* out, u8 inval, FsSave *save) { raw->inval = (u64)inval; memcpy(&raw->save, save, sizeof(FsSave)); - Result rc = ipcDispatch(g_fsHandle); + Result rc = serviceIpcDispatch(&g_fsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/hid.c b/nx/source/services/hid.c index 5ad511fd..05678a51 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -1,8 +1,8 @@ #include #include -static Handle g_hidServiceSession = INVALID_HANDLE; -static Handle g_hidIAppletResource = INVALID_HANDLE; +static Service g_hidSrv; +static Service g_hidIAppletResource; static SharedMemory g_hidSharedmem; static HidTouchScreenEntry g_touchEntry; @@ -20,70 +20,49 @@ static u64 g_touchTimestamp, g_mouseTimestamp, g_keyboardTimestamp, g_controller static RwLock g_hidLock; -static Result _hidCreateAppletResource(Handle sessionhandle, Handle* handle_out, u64 AppletResourceUserId); -static Result _hidGetSharedMemoryHandle(Handle sessionhandle, Handle* handle_out); +static Result _hidCreateAppletResource(Service* srv, Service* srv_out, u64 AppletResourceUserId); +static Result _hidGetSharedMemoryHandle(Service* srv, Handle* handle_out); Result hidInitialize(void) { - if (g_hidServiceSession != INVALID_HANDLE) + if (serviceIsActive(&g_hidSrv)) return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); - Result rc = 0; - u64 AppletResourceUserId = 0; - Handle sharedmem_handle=0; + Result rc; + Handle sharedmem_handle; + u64 AppletResourceUserId; rc = appletGetAppletResourceUserId(&AppletResourceUserId); if (R_FAILED(rc)) return rc; - rc = smGetService(&g_hidServiceSession, "hid"); + rc = smGetService(&g_hidSrv, "hid"); if (R_FAILED(rc)) return rc; - rc = _hidCreateAppletResource(g_hidServiceSession, &g_hidIAppletResource, AppletResourceUserId); + rc = _hidCreateAppletResource(&g_hidSrv, &g_hidIAppletResource, AppletResourceUserId); if (R_SUCCEEDED(rc)) - rc = _hidGetSharedMemoryHandle(g_hidIAppletResource, &sharedmem_handle); + rc = _hidGetSharedMemoryHandle(&g_hidIAppletResource, &sharedmem_handle); - if (R_SUCCEEDED(rc)) { + if (R_SUCCEEDED(rc)) + { shmemLoadRemote(&g_hidSharedmem, sharedmem_handle, 0x40000, PERM_R); rc = shmemMap(&g_hidSharedmem); - - if (R_FAILED(rc)) svcCloseHandle(sharedmem_handle); } - if (R_FAILED(rc)) { - if (g_hidServiceSession != INVALID_HANDLE) - svcCloseHandle(g_hidServiceSession); - - if (g_hidIAppletResource != INVALID_HANDLE) - svcCloseHandle(g_hidIAppletResource); - - g_hidServiceSession = INVALID_HANDLE; - g_hidIAppletResource = INVALID_HANDLE; - } + if (R_FAILED(rc)) + hidExit(); hidReset(); - return rc; } void hidExit(void) { - if (g_hidServiceSession == INVALID_HANDLE) - return; - - if (g_hidServiceSession != INVALID_HANDLE) { - svcCloseHandle(g_hidServiceSession); - g_hidServiceSession = INVALID_HANDLE; - } - - if (g_hidIAppletResource != INVALID_HANDLE) { - svcCloseHandle(g_hidIAppletResource); - g_hidIAppletResource = INVALID_HANDLE; - } - + serviceClose(&g_hidIAppletResource); + serviceClose(&g_hidSrv); shmemClose(&g_hidSharedmem); } @@ -114,8 +93,8 @@ void hidReset(void) rwlockWriteUnlock(&g_hidLock); } -Handle hidGetSessionService(void) { - return g_hidServiceSession; +Service* hidGetSessionService(void) { + return &g_hidSrv; } void* hidGetSharedmemAddr(void) { @@ -137,9 +116,6 @@ HidControllerLayoutType hidGetControllerLayout(HidControllerID id) { } void hidScanInput(void) { - if (g_hidServiceSession == INVALID_HANDLE) - return; - rwlockWriteLock(&g_hidLock); HidSharedMemory *sharedMem = (HidSharedMemory*)hidGetSharedmemAddr(); @@ -336,7 +312,7 @@ void hidTouchRead(touchPosition *pos, u32 point_id) { } } -static Result _hidCreateAppletResource(Handle sessionhandle, Handle* handle_out, u64 AppletResourceUserId) { +static Result _hidCreateAppletResource(Service* srv, Service* srv_out, u64 AppletResourceUserId) { IpcCommand c; ipcInitialize(&c); @@ -354,7 +330,7 @@ static Result _hidCreateAppletResource(Handle sessionhandle, Handle* handle_out, raw->cmd_id = 0; raw->AppletResourceUserId = AppletResourceUserId; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -368,14 +344,14 @@ static Result _hidCreateAppletResource(Handle sessionhandle, Handle* handle_out, rc = resp->result; if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; + serviceCreate(srv_out, r.Handles[0]); } } return rc; } -static Result _hidGetSharedMemoryHandle(Handle sessionhandle, Handle* handle_out) { +static Result _hidGetSharedMemoryHandle(Service* srv, Handle* handle_out) { IpcCommand c; ipcInitialize(&c); @@ -389,7 +365,7 @@ static Result _hidGetSharedMemoryHandle(Handle sessionhandle, Handle* handle_out raw->magic = SFCI_MAGIC; raw->cmd_id = 0; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/nv.c b/nx/source/services/nv.c index 3675cb07..51f0ae60 100644 --- a/nx/source/services/nv.c +++ b/nx/source/services/nv.c @@ -1,7 +1,7 @@ #include #include -static Handle g_nvServiceSession = INVALID_HANDLE; +static Service g_nvSrv; static size_t g_nvIpcBufferSize = 0; static u32 g_nvServiceType = -1; static TransferMemory g_nvTransfermem; @@ -16,30 +16,30 @@ Result nvInitialize(nvServiceType servicetype, size_t transfermem_size) { u64 AppletResourceUserId = 0; if (servicetype==NVSERVTYPE_Default || servicetype==NVSERVTYPE_Application) { - rc = smGetService(&g_nvServiceSession, "nvdrv"); + rc = smGetService(&g_nvSrv, "nvdrv"); g_nvServiceType = 0; } if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_Applet) { - rc = smGetService(&g_nvServiceSession, "nvdrv:a"); + rc = smGetService(&g_nvSrv, "nvdrv:a"); g_nvServiceType = 1; } if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_Sysmodule) { - rc = smGetService(&g_nvServiceSession, "nvdrv:s"); + rc = smGetService(&g_nvSrv, "nvdrv:s"); g_nvServiceType = 2; } if ((servicetype==NVSERVTYPE_Default && R_FAILED(rc)) || servicetype==NVSERVTYPE_T) { - rc = smGetService(&g_nvServiceSession, "nvdrv:t"); + rc = smGetService(&g_nvSrv, "nvdrv:t"); g_nvServiceType = 3; } if (R_SUCCEEDED(rc)) { g_nvIpcBufferSize = 0; - rc = ipcQueryPointerBufferSize(g_nvServiceSession, &g_nvIpcBufferSize); + rc = ipcQueryPointerBufferSize(g_nvSrv.handle, &g_nvIpcBufferSize); if (R_SUCCEEDED(rc)) rc = tmemCreate(&g_nvTransfermem, transfermem_size, PERM_NONE); @@ -53,15 +53,7 @@ Result nvInitialize(nvServiceType servicetype, size_t transfermem_size) { } if (R_FAILED(rc)) { - g_nvServiceType = -1; - - if(g_nvServiceSession != INVALID_HANDLE) - { - svcCloseHandle(g_nvServiceSession); - g_nvServiceSession = INVALID_HANDLE; - } - - tmemClose(&g_nvTransfermem); + nvExit(); } return rc; @@ -69,16 +61,12 @@ Result nvInitialize(nvServiceType servicetype, size_t transfermem_size) { void nvExit(void) { - if(g_nvServiceType==-1)return; + if(g_nvServiceType == -1) + return; g_nvServiceType = -1; - if(g_nvServiceSession != INVALID_HANDLE) - { - svcCloseHandle(g_nvServiceSession); - g_nvServiceSession = INVALID_HANDLE; - } - + serviceClose(&g_nvSrv); tmemClose(&g_nvTransfermem); } @@ -100,7 +88,7 @@ static Result _nvInitialize(Handle proc, Handle sharedmem, u32 transfermem_size) raw->cmd_id = 3; raw->transfermem_size = transfermem_size; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -134,7 +122,7 @@ static Result _nvSetClientPID(u64 AppletResourceUserId) { raw->cmd_id = 8; raw->AppletResourceUserId = AppletResourceUserId; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -166,7 +154,7 @@ Result nvOpen(u32 *fd, const char *devicepath) { raw->magic = SFCI_MAGIC; raw->cmd_id = 0; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -244,7 +232,7 @@ Result nvIoctl(u32 fd, u32 request, void* argp) { raw->fd = fd; raw->request = request; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -278,7 +266,7 @@ Result nvClose(u32 fd) { raw->cmd_id = 2; raw->fd = fd; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -314,7 +302,7 @@ Result nvQueryEvent(u32 fd, u32 event_id, Handle *handle_out) { raw->fd = fd; raw->event_id = event_id; - Result rc = ipcDispatch(g_nvServiceSession); + Result rc = serviceIpcDispatch(&g_nvSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/pm.c b/nx/source/services/pm.c index d0e1c018..546cf709 100644 --- a/nx/source/services/pm.c +++ b/nx/source/services/pm.c @@ -1,16 +1,10 @@ // Copyright 2017 plutoo #include -static Handle g_pmdmntHandle = INVALID_HANDLE; +static Service g_pmdmntSrv; Result pmdmntInitialize(void) { - Result rc = smGetService(&g_pmdmntHandle, "pm:dmnt"); - - if (R_FAILED(rc)) { - g_pmdmntHandle = INVALID_HANDLE; - } - - return rc; + return smGetService(&g_pmdmntSrv, "pm:dmnt"); } Result pmdmntStartProcess(u64 pid) { @@ -29,7 +23,7 @@ Result pmdmntStartProcess(u64 pid) { raw->cmd_id = 2; raw->pid = pid; - Result rc = ipcDispatch(g_pmdmntHandle); + Result rc = serviceIpcDispatch(&g_pmdmntSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -62,7 +56,7 @@ Result pmdmntGetTitlePid(u64* pid_out, u64 title_id) { raw->cmd_id = 3; raw->title_id = title_id; - Result rc = ipcDispatch(g_pmdmntHandle); + Result rc = serviceIpcDispatch(&g_pmdmntSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -100,7 +94,7 @@ Result pmdmntEnableDebugForTitleId(Handle* handle_out, u64 title_id) { raw->cmd_id = 4; raw->title_id = title_id; - Result rc = ipcDispatch(g_pmdmntHandle); + Result rc = serviceIpcDispatch(&g_pmdmntSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -135,7 +129,7 @@ Result pmdmntGetApplicationPid(u64* pid_out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 5; - Result rc = ipcDispatch(g_pmdmntHandle); + Result rc = serviceIpcDispatch(&g_pmdmntSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -171,7 +165,7 @@ Result pmdmntEnableDebugForApplication(Handle* handle_out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 6; - Result rc = ipcDispatch(g_pmdmntHandle); + Result rc = serviceIpcDispatch(&g_pmdmntSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; diff --git a/nx/source/services/sm.c b/nx/source/services/sm.c index 9c7d0e16..cce036ea 100644 --- a/nx/source/services/sm.c +++ b/nx/source/services/sm.c @@ -1,20 +1,56 @@ // Copyright 2017 plutoo #include -static Handle g_smHandle = -1; +static Handle g_smHandle = INVALID_HANDLE; -bool smHasInitialized(void) { - return g_smHandle != -1; +#define MAX_OVERRIDES 32 + +static struct { + u64 name; + Handle handle; +} g_smOverrides[MAX_OVERRIDES]; + +static size_t g_smOverridesNum = 0; + +void smAddOverrideHandle(u64 name, Handle handle) +{ + if (g_smOverridesNum == MAX_OVERRIDES) + fatalSimple(1); + + size_t i = g_smOverridesNum; + + g_smOverrides[i].name = name; + g_smOverrides[i].handle = handle; + + g_smOverridesNum++; } -Result smInitialize(void) { +Handle smGetServiceOverride(u64 name) +{ + size_t i; + + for (i=0; itype = ServiceType_Override; + service_out->handle = handle; + rc = 0; + } + else + { + rc = smGetServiceOriginal(&handle, name_encoded); + + if (R_SUCCEEDED(rc)) + { + service_out->type = ServiceType_Normal; + service_out->handle = handle; + } + } + + return rc; +} + +Result smGetServiceOriginal(Handle* handle_out, u64 name) +{ IpcCommand c; ipcInitialize(&c); @@ -86,7 +152,7 @@ Result smGetService(Handle* handle_out, const char* name) { raw->magic = SFCI_MAGIC; raw->cmd_id = 1; - raw->service_name = _EncodeName(name); + raw->service_name = name; Result rc = ipcDispatch(g_smHandle); @@ -125,7 +191,7 @@ Result smRegisterService(Handle* handle_out, const char* name, bool is_light, in raw->magic = SFCI_MAGIC; raw->cmd_id = 2; - raw->service_name = _EncodeName(name); + raw->service_name = smEncodeName(name); raw->is_light = !!is_light; raw->max_sessions = max_sessions; @@ -165,7 +231,7 @@ Result smUnregisterService(const char* name) { raw->magic = SFCI_MAGIC; raw->cmd_id = 3; - raw->service_name = _EncodeName(name); + raw->service_name = smEncodeName(name); Result rc = ipcDispatch(g_smHandle); diff --git a/nx/source/services/usb.c b/nx/source/services/usb.c index 232e71b4..864640a1 100644 --- a/nx/source/services/usb.c +++ b/nx/source/services/usb.c @@ -4,46 +4,50 @@ #define TOTAL_INTERFACES 4 #define TOTAL_ENDPOINTS 15*2 -static Handle g_usbDsServiceSession = 0; -static Handle g_usbDsStateChangeEvent = 0; +static Service g_usbDsSrv; +static Handle g_usbDsStateChangeEvent = INVALID_HANDLE; static UsbDsInterface g_usbDsInterfaceTable[TOTAL_INTERFACES]; static UsbDsEndpoint g_usbDsEndpointTable[TOTAL_INTERFACES*TOTAL_ENDPOINTS]; static void _usbDsFreeTables(void); -static Result _usbDsBindDevice(usbComplexId complexId); +static Result _usbDsBindDevice(UsbComplexId complexId); static Result _usbDsBindClientProcess(Handle prochandle); -static Result _usbDsGetEvent(Handle sessionhandle, Handle* handle_out, u64 cmd_id); +static Result _usbDsGetEvent(Service* srv, Handle* handle_out, u64 cmd_id); static Result _usbDsSetVidPidBcd(const usbDsDeviceInfo* deviceinfo); -static Result _usbDsGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id, const void* buf0, size_t buf0size, const void* buf1, size_t buf1size); +static Result _usbDsGetSession(Service* srv, Service* srv_out, u64 cmd_id, const void* buf0, size_t buf0size, const void* buf1, size_t buf1size); -Result usbDsInitialize(usbComplexId complexId, const usbDsDeviceInfo* deviceinfo) { - if(g_usbDsServiceSession!=0)return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); +Result usbDsInitialize(UsbComplexId complexId, const usbDsDeviceInfo* deviceinfo) { + if (serviceIsActive(&g_usbDsSrv)) + return MAKERESULT(MODULE_LIBNX, LIBNX_ALREADYINITIALIZED); Result rc = 0; - rc = smGetService(&g_usbDsServiceSession, "usb:ds"); + rc = smGetService(&g_usbDsSrv, "usb:ds"); - if (R_SUCCEEDED(rc))rc = _usbDsBindDevice(complexId); - if (R_SUCCEEDED(rc))rc = _usbDsBindClientProcess(CUR_PROCESS_HANDLE); - if (R_SUCCEEDED(rc))rc = _usbDsGetEvent(g_usbDsServiceSession, &g_usbDsStateChangeEvent, 3);// GetStateChangeEvent + if (R_SUCCEEDED(rc)) + rc = _usbDsBindDevice(complexId); + if (R_SUCCEEDED(rc)) + rc = _usbDsBindClientProcess(CUR_PROCESS_HANDLE); + + // GetStateChangeEvent + if (R_SUCCEEDED(rc)) + rc = _usbDsGetEvent(&g_usbDsSrv, &g_usbDsStateChangeEvent, 3); if (R_SUCCEEDED(rc) && deviceinfo && kernelAbove200()) { rc = _usbDsSetVidPidBcd(deviceinfo); } - if (R_FAILED(rc)) { + if (R_FAILED(rc)) + { if(g_usbDsStateChangeEvent) { svcCloseHandle(g_usbDsStateChangeEvent); - g_usbDsStateChangeEvent = 0; + g_usbDsStateChangeEvent = INVALID_HANDLE; } - if(g_usbDsServiceSession) { - svcCloseHandle(g_usbDsServiceSession); - g_usbDsServiceSession = 0; - } + serviceClose(&g_usbDsSrv); } return rc; @@ -51,29 +55,26 @@ Result usbDsInitialize(usbComplexId complexId, const usbDsDeviceInfo* deviceinfo void usbDsExit(void) { - if(g_usbDsServiceSession==0)return; + if (!serviceIsActive(&g_usbDsSrv)) + return; _usbDsFreeTables(); - if(g_usbDsStateChangeEvent) { + if (g_usbDsStateChangeEvent) { svcCloseHandle(g_usbDsStateChangeEvent); g_usbDsStateChangeEvent = 0; } - if(g_usbDsServiceSession) { - svcCloseHandle(g_usbDsServiceSession); - g_usbDsServiceSession = 0; - } + serviceClose(&g_usbDsSrv); } -Handle usbDsGetServiceSession(void) -{ - return g_usbDsServiceSession; +Service* usbDsGetServiceSession(void) { + return &g_usbDsSrv; } Handle usbDsGetStateChangeEvent(void) { - return g_usbDsStateChangeEvent; + return g_usbDsStateChangeEvent; } static UsbDsInterface* _usbDsAllocateInterface(void) @@ -115,44 +116,40 @@ static UsbDsEndpoint* _usbDsAllocateEndpoint(UsbDsInterface* interface) static void _usbDsFreeInterface(UsbDsInterface* interface) { - if(!interface->initialized)return; + if (!interface->initialized) + return; - if(interface->CtrlOutCompletionEvent) { + if (interface->CtrlOutCompletionEvent) { svcCloseHandle(interface->CtrlOutCompletionEvent); interface->CtrlOutCompletionEvent = 0; } - if(interface->CtrlInCompletionEvent) { + if (interface->CtrlInCompletionEvent) { svcCloseHandle(interface->CtrlInCompletionEvent); interface->CtrlInCompletionEvent = 0; } - if(interface->SetupEvent) { + if (interface->SetupEvent) { svcCloseHandle(interface->SetupEvent); interface->SetupEvent = 0; } - if(interface->h) { - svcCloseHandle(interface->h); - interface->h = 0; - } + serviceClose(&interface->h); interface->initialized = false; } static void _usbDsFreeEndpoint(UsbDsEndpoint* endpoint) { - if(!endpoint->initialized)return; + if (!endpoint->initialized) + return; - if(endpoint->CompletionEvent) { + if (endpoint->CompletionEvent) { svcCloseHandle(endpoint->CompletionEvent); endpoint->CompletionEvent = 0; } - if(endpoint->h) { - svcCloseHandle(endpoint->h); - endpoint->h = 0; - } + serviceClose(&endpoint->h); endpoint->initialized = false; } @@ -167,9 +164,7 @@ static void _usbDsFreeTables(void) } } -static Result _usbDsBindDevice(usbComplexId complexId) { - if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsBindDevice(UsbComplexId complexId) { IpcCommand c; ipcInitialize(&c); @@ -185,7 +180,7 @@ static Result _usbDsBindDevice(usbComplexId complexId) { raw->cmd_id = 0; raw->complexId = complexId; - Result rc = ipcDispatch(g_usbDsServiceSession); + Result rc = serviceIpcDispatch(&g_usbDsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -203,8 +198,6 @@ static Result _usbDsBindDevice(usbComplexId complexId) { } static Result _usbDsBindClientProcess(Handle prochandle) { - if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - IpcCommand c; ipcInitialize(&c); @@ -220,7 +213,7 @@ static Result _usbDsBindClientProcess(Handle prochandle) { raw->magic = SFCI_MAGIC; raw->cmd_id = 1; - Result rc = ipcDispatch(g_usbDsServiceSession); + Result rc = serviceIpcDispatch(&g_usbDsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -237,9 +230,7 @@ static Result _usbDsBindClientProcess(Handle prochandle) { return rc; } -static Result _usbDsGetEvent(Handle sessionhandle, Handle* handle_out, u64 cmd_id) { - if(sessionhandle==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsGetEvent(Service* srv, Handle* handle_out, u64 cmd_id) { IpcCommand c; ipcInitialize(&c); @@ -253,7 +244,7 @@ static Result _usbDsGetEvent(Handle sessionhandle, Handle* handle_out, u64 cmd_i raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -275,8 +266,6 @@ static Result _usbDsGetEvent(Handle sessionhandle, Handle* handle_out, u64 cmd_i } Result usbDsGetState(u32 *out) { - if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - IpcCommand c; ipcInitialize(&c); @@ -290,7 +279,7 @@ Result usbDsGetState(u32 *out) { raw->magic = SFCI_MAGIC; raw->cmd_id = 4; - Result rc = ipcDispatch(g_usbDsServiceSession); + Result rc = serviceIpcDispatch(&g_usbDsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -368,8 +357,6 @@ Result usbDsParseReportData(usbDsReportData *reportdata, u32 urbId, u32 *request } static Result _usbDsSetVidPidBcd(const usbDsDeviceInfo* deviceinfo) { - if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - IpcCommand c; ipcInitialize(&c); @@ -385,7 +372,7 @@ static Result _usbDsSetVidPidBcd(const usbDsDeviceInfo* deviceinfo) { raw->magic = SFCI_MAGIC; raw->cmd_id = 5; - Result rc = ipcDispatch(g_usbDsServiceSession); + Result rc = serviceIpcDispatch(&g_usbDsSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -402,9 +389,7 @@ static Result _usbDsSetVidPidBcd(const usbDsDeviceInfo* deviceinfo) { return rc; } -static Result _usbDsGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd_id, const void* buf0, size_t buf0size, const void* buf1, size_t buf1size) { - if(sessionhandle==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsGetSession(Service* srv, Service* srv_out, u64 cmd_id, const void* buf0, size_t buf0size, const void* buf1, size_t buf1size) { IpcCommand c; ipcInitialize(&c); @@ -413,15 +398,17 @@ static Result _usbDsGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd u64 cmd_id; } *raw; - if(buf0 && buf0size)ipcAddSendBuffer(&c, buf0, buf0size, 0); - if(buf1 && buf1size)ipcAddSendBuffer(&c, buf1, buf1size, 0); + if (buf0 && buf0size) + ipcAddSendBuffer(&c, buf0, buf0size, 0); + if (buf1 && buf1size) + ipcAddSendBuffer(&c, buf1, buf1size, 0); raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -435,16 +422,14 @@ static Result _usbDsGetSession(Handle sessionhandle, Handle* handle_out, u64 cmd rc = resp->result; if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; + serviceCreate(srv_out, r.Handles[0]); } } return rc; } -static Result _usbDsCmdNoParams(Handle sessionhandle, u64 cmd_id) { - if(sessionhandle==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsCmdNoParams(Service* srv, u64 cmd_id) { IpcCommand c; ipcInitialize(&c); @@ -458,7 +443,7 @@ static Result _usbDsCmdNoParams(Handle sessionhandle, u64 cmd_id) { raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -475,9 +460,7 @@ static Result _usbDsCmdNoParams(Handle sessionhandle, u64 cmd_id) { return rc; } -static Result _usbDsPostBuffer(Handle sessionhandle, u64 cmd_id, void* buffer, size_t size, u32 *urbId) { - if(sessionhandle==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsPostBuffer(Service* srv, u64 cmd_id, void* buffer, size_t size, u32 *urbId) { armDCacheFlush(buffer, size); IpcCommand c; @@ -499,7 +482,7 @@ static Result _usbDsPostBuffer(Handle sessionhandle, u64 cmd_id, void* buffer, s raw->padding = 0; raw->buffer = (u64)buffer; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -518,9 +501,7 @@ static Result _usbDsPostBuffer(Handle sessionhandle, u64 cmd_id, void* buffer, s return rc; } -static Result _usbDsGetReport(Handle sessionhandle, u64 cmd_id, usbDsReportData *out) { - if(sessionhandle==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - +static Result _usbDsGetReport(Service* srv, u64 cmd_id, usbDsReportData *out) { IpcCommand c; ipcInitialize(&c); @@ -534,7 +515,7 @@ static Result _usbDsGetReport(Handle sessionhandle, u64 cmd_id, usbDsReportData raw->magic = SFCI_MAGIC; raw->cmd_id = cmd_id; - Result rc = ipcDispatch(sessionhandle); + Result rc = serviceIpcDispatch(srv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -547,7 +528,9 @@ static Result _usbDsGetReport(Handle sessionhandle, u64 cmd_id, usbDsReportData } *resp = r.Raw; rc = resp->result; - if (R_SUCCEEDED(rc) && out)memcpy(out, &resp->out, sizeof(resp->out)); + + if (R_SUCCEEDED(rc) && out) + memcpy(out, &resp->out, sizeof(resp->out)); } return rc; @@ -555,119 +538,129 @@ static Result _usbDsGetReport(Handle sessionhandle, u64 cmd_id, usbDsReportData Result usbDsGetDsInterface(UsbDsInterface** interface, struct usb_interface_descriptor* descriptor, const char *interface_name) { - UsbDsInterface* ptr = _usbDsAllocateInterface(); - if(ptr==NULL)return MAKERESULT(MODULE_LIBNX, LIBNX_OUTOFMEM); + UsbDsInterface* ptr = _usbDsAllocateInterface(); + if(ptr == NULL) + return MAKERESULT(MODULE_LIBNX, LIBNX_OUTOFMEM); - Result rc = _usbDsGetSession(g_usbDsServiceSession, &ptr->h, 2, descriptor, sizeof(struct usb_interface_descriptor), interface_name, strlen(interface_name)+1); + Result rc = _usbDsGetSession(&g_usbDsSrv, &ptr->h, 2, descriptor, sizeof(struct usb_interface_descriptor), interface_name, strlen(interface_name)+1); - if (R_SUCCEEDED(rc)) rc = _usbDsGetEvent(ptr->h, &ptr->SetupEvent, 1);//GetSetupEvent - if (R_SUCCEEDED(rc)) rc = _usbDsGetEvent(ptr->h, &ptr->CtrlInCompletionEvent, 7);//GetCtrlInCompletionEvent - if (R_SUCCEEDED(rc)) rc = _usbDsGetEvent(ptr->h, &ptr->CtrlOutCompletionEvent, 9);//GetCtrlOutCompletionEvent + // GetSetupEvent + if (R_SUCCEEDED(rc)) + rc = _usbDsGetEvent(&ptr->h, &ptr->SetupEvent, 1); + // GetCtrlInCompletionEvent + if (R_SUCCEEDED(rc)) + rc = _usbDsGetEvent(&ptr->h, &ptr->CtrlInCompletionEvent, 7); + // GetCtrlOutCompletionEvent + if (R_SUCCEEDED(rc)) + rc = _usbDsGetEvent(&ptr->h, &ptr->CtrlOutCompletionEvent, 9); - if (R_FAILED(rc)) _usbDsFreeInterface(ptr); + if (R_FAILED(rc)) + _usbDsFreeInterface(ptr); - if (R_SUCCEEDED(rc)) *interface = ptr; - return rc; + if (R_SUCCEEDED(rc)) + *interface = ptr; + + return rc; } //IDsInterface void usbDsInterface_Close(UsbDsInterface* interface) { - _usbDsFreeInterface(interface); + _usbDsFreeInterface(interface); } Result usbDsInterface_GetDsEndpoint(UsbDsInterface* interface, UsbDsEndpoint** endpoint, struct usb_endpoint_descriptor* descriptor) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - UsbDsEndpoint* ptr = _usbDsAllocateEndpoint(interface); - if(ptr==NULL)return MAKERESULT(MODULE_LIBNX, LIBNX_OUTOFMEM); + UsbDsEndpoint* ptr = _usbDsAllocateEndpoint(interface); + if(ptr==NULL)return MAKERESULT(MODULE_LIBNX, LIBNX_OUTOFMEM); - Result rc = _usbDsGetSession(interface->h, &ptr->h, 0, descriptor, sizeof(struct usb_endpoint_descriptor), NULL, 0); + Result rc = _usbDsGetSession(&interface->h, &ptr->h, 0, descriptor, sizeof(struct usb_endpoint_descriptor), NULL, 0); - if (R_SUCCEEDED(rc)) rc = _usbDsGetEvent(ptr->h, &ptr->CompletionEvent, 2);//GetCompletionEvent + if (R_SUCCEEDED(rc)) rc = _usbDsGetEvent(&ptr->h, &ptr->CompletionEvent, 2);//GetCompletionEvent - if (R_FAILED(rc)) _usbDsFreeEndpoint(ptr); + if (R_FAILED(rc)) _usbDsFreeEndpoint(ptr); - if (R_SUCCEEDED(rc)) *endpoint = ptr; - return rc; + if (R_SUCCEEDED(rc)) *endpoint = ptr; + return rc; } Result usbDsInterface_EnableInterface(UsbDsInterface* interface) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsCmdNoParams(interface->h, 3); + return _usbDsCmdNoParams(&interface->h, 3); } Result usbDsInterface_DisableInterface(UsbDsInterface* interface) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsCmdNoParams(interface->h, 4); + return _usbDsCmdNoParams(&interface->h, 4); } Result usbDsInterface_CtrlInPostBufferAsync(UsbDsInterface* interface, void* buffer, size_t size, u32 *urbId) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsPostBuffer(interface->h, 5, buffer, size, urbId); + return _usbDsPostBuffer(&interface->h, 5, buffer, size, urbId); } Result usbDsInterface_CtrlOutPostBufferAsync(UsbDsInterface* interface, void* buffer, size_t size, u32 *urbId) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsPostBuffer(interface->h, 6, buffer, size, urbId); + return _usbDsPostBuffer(&interface->h, 6, buffer, size, urbId); } Result usbDsInterface_GetCtrlInReportData(UsbDsInterface* interface, usbDsReportData *out) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsGetReport(interface->h, 8, out); + return _usbDsGetReport(&interface->h, 8, out); } Result usbDsInterface_GetCtrlOutReportData(UsbDsInterface* interface, usbDsReportData *out) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsGetReport(interface->h, 10, out); + return _usbDsGetReport(&interface->h, 10, out); } Result usbDsInterface_StallCtrl(UsbDsInterface* interface) { - if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!interface->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsCmdNoParams(interface->h, 11); + return _usbDsCmdNoParams(&interface->h, 11); } //IDsEndpoint void usbDsEndpoint_Close(UsbDsEndpoint* endpoint) { - _usbDsFreeEndpoint(endpoint); + _usbDsFreeEndpoint(endpoint); } Result usbDsEndpoint_PostBufferAsync(UsbDsEndpoint* endpoint, void* buffer, size_t size, u32 *urbId) { - if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsPostBuffer(endpoint->h, 0, buffer, size, urbId); + return _usbDsPostBuffer(&endpoint->h, 0, buffer, size, urbId); } Result usbDsEndpoint_GetReportData(UsbDsEndpoint* endpoint, usbDsReportData *out) { - if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsGetReport(endpoint->h, 3, out); + return _usbDsGetReport(&endpoint->h, 3, out); } Result usbDsEndpoint_StallCtrl(UsbDsEndpoint* endpoint) { - if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + if(!endpoint->initialized)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); - return _usbDsCmdNoParams(endpoint->h, 4); + return _usbDsCmdNoParams(&endpoint->h, 4); } diff --git a/nx/source/services/vi.c b/nx/source/services/vi.c index 9efa360f..ff4063bb 100644 --- a/nx/source/services/vi.c +++ b/nx/source/services/vi.c @@ -1,7 +1,7 @@ #include #include -static Handle g_viServiceSession = INVALID_HANDLE; +static Service g_viServiceSession; static u32 g_viServiceType = -1; static Handle g_viIApplicationDisplayService = INVALID_HANDLE; static Handle g_viIHOSBinderDriverRelay = INVALID_HANDLE; @@ -19,17 +19,17 @@ Result viInitialize(ViServiceType servicetype) Result rc = 0; - if (servicetype==ViServiceType_Default || servicetype==ViServiceType_Manager) { + if (servicetype == ViServiceType_Default || servicetype == ViServiceType_Manager) { rc = smGetService(&g_viServiceSession, "vi:m"); g_viServiceType = 2; } - if ((servicetype==ViServiceType_Default && R_FAILED(rc)) || servicetype==ViServiceType_System) { + if ((servicetype == ViServiceType_Default && R_FAILED(rc)) || servicetype == ViServiceType_System) { rc = smGetService(&g_viServiceSession, "vi:s"); g_viServiceType = 1; } - if ((servicetype==ViServiceType_Default && R_FAILED(rc)) || servicetype==ViServiceType_Application) + if ((servicetype == ViServiceType_Default && R_FAILED(rc)) || servicetype == ViServiceType_Application) { rc = smGetService(&g_viServiceSession, "vi:u"); g_viServiceType = 0; @@ -46,19 +46,19 @@ Result viInitialize(ViServiceType servicetype) raw.cmd_id = g_viServiceType; raw.inval0 = 0; - rc = _viGetSession(g_viServiceSession, &g_viIApplicationDisplayService, &raw, sizeof(raw)); + rc = _viGetSession(g_viServiceSession.handle, &g_viIApplicationDisplayService, &raw, sizeof(raw)); } if (R_SUCCEEDED(rc)) rc = _viGetSessionNoParams(g_viIApplicationDisplayService, &g_viIHOSBinderDriverRelay, 100); - if (g_viServiceType>=ViServiceType_System && R_SUCCEEDED(rc)) + if (g_viServiceType >= ViServiceType_System && R_SUCCEEDED(rc)) rc = _viGetSessionNoParams(g_viIApplicationDisplayService, &g_viISystemDisplayService, 101); - if (g_viServiceType>=ViServiceType_Manager && R_SUCCEEDED(rc)) + if (g_viServiceType >= ViServiceType_Manager && R_SUCCEEDED(rc)) rc = _viGetSessionNoParams(g_viIApplicationDisplayService, &g_viIManagerDisplayService, 102); - if (g_viServiceType>=ViServiceType_System && R_SUCCEEDED(rc) && kernelAbove200()) + if (g_viServiceType >= ViServiceType_System && R_SUCCEEDED(rc) && kernelAbove200()) rc = _viGetSessionNoParams(g_viIApplicationDisplayService, &g_viIHOSBinderDriverIndirect, 103); if (R_FAILED(rc)) { @@ -76,11 +76,6 @@ void viExit(void) g_viServiceType = -1; - if(g_viServiceSession != INVALID_HANDLE) { - svcCloseHandle(g_viServiceSession); - g_viServiceSession = 0; - } - if(g_viIApplicationDisplayService != INVALID_HANDLE) { svcCloseHandle(g_viIApplicationDisplayService); g_viIApplicationDisplayService = 0; @@ -100,35 +95,31 @@ void viExit(void) svcCloseHandle(g_viIHOSBinderDriverIndirect); g_viIHOSBinderDriverIndirect = 0; } + + serviceClose(&g_viServiceSession); } -Handle viGetSessionService(void) -{ - return g_viServiceSession; +Handle viGetSessionService(void) { + return g_viServiceSession.handle; } -Handle viGetSession_IApplicationDisplayService(void) -{ +Handle viGetSession_IApplicationDisplayService(void) { return g_viIApplicationDisplayService; } -Handle viGetSession_IHOSBinderDriverRelay(void) -{ +Handle viGetSession_IHOSBinderDriverRelay(void) { return g_viIHOSBinderDriverRelay; } -Handle viGetSession_ISystemDisplayService(void) -{ +Handle viGetSession_ISystemDisplayService(void) { return g_viISystemDisplayService; } -Handle viGetSession_IManagerDisplayService(void) -{ +Handle viGetSession_IManagerDisplayService(void) { return g_viIManagerDisplayService; } -Handle viGetSession_IHOSBinderDriverIndirect(void) -{ +Handle viGetSession_IHOSBinderDriverIndirect(void) { return g_viIHOSBinderDriverIndirect; }