From a405bc4827800b1c29b8deac97cd53e2ecabef59 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Wed, 29 Nov 2017 23:31:18 -0500 Subject: [PATCH] Added account. Added u128 type to types.h. --- nx/include/switch.h | 1 + nx/include/switch/services/account.h | 6 +++ nx/include/switch/types.h | 9 ++-- nx/source/services/account.c | 72 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 nx/include/switch/services/account.h create mode 100644 nx/source/services/account.c diff --git a/nx/include/switch.h b/nx/include/switch.h index 448f1513..704b2118 100644 --- a/nx/include/switch.h +++ b/nx/include/switch.h @@ -25,6 +25,7 @@ extern "C" { #include #include +#include #include #include #include diff --git a/nx/include/switch/services/account.h b/nx/include/switch/services/account.h new file mode 100644 index 00000000..822fa7a0 --- /dev/null +++ b/nx/include/switch/services/account.h @@ -0,0 +1,6 @@ +Result accountInitialize(void); +void accountExit(void); +Handle accountGetSessionService(void); + +/// Get the userID for the currently active user. The output userID is only valid when the output account_selected==1, otherwise no user is currently selected. +Result accountGetActiveUser(u128 *userID, bool *account_selected); diff --git a/nx/include/switch/types.h b/nx/include/switch/types.h index b31bfa38..2ad6cbbd 100644 --- a/nx/include/switch/types.h +++ b/nx/include/switch/types.h @@ -18,10 +18,11 @@ #endif #endif -typedef uint8_t u8; ///< 8-bit unsigned integer -typedef uint16_t u16; ///< 16-bit unsigned integer -typedef uint32_t u32; ///< 32-bit unsigned integer -typedef uint64_t u64; ///< 64-bit unsigned integer +typedef uint8_t u8; ///< 8-bit unsigned integer +typedef uint16_t u16; ///< 16-bit unsigned integer +typedef uint32_t u32; ///< 32-bit unsigned integer +typedef uint64_t u64; ///< 64-bit unsigned integer +typedef __uint128_t u128; ///< 128-bit unsigned integer typedef int8_t s8; ///< 8-bit signed integer typedef int16_t s16; ///< 16-bit signed integer diff --git a/nx/source/services/account.c b/nx/source/services/account.c new file mode 100644 index 00000000..ad0f33ca --- /dev/null +++ b/nx/source/services/account.c @@ -0,0 +1,72 @@ +#include +#include + +static Handle g_accountServiceSession = INVALID_HANDLE; + +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) +{ + if (g_accountServiceSession == INVALID_HANDLE) return; + + if (g_accountServiceSession != INVALID_HANDLE) { + svcCloseHandle(g_accountServiceSession); + g_accountServiceSession = INVALID_HANDLE; + } +} + +Handle accountGetSessionService(void) { + return g_accountServiceSession; +} + +Result accountGetActiveUser(u128 *userID, bool *account_selected) { + if (g_accountServiceSession == INVALID_HANDLE) return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 4; + + Result rc = ipcDispatch(g_accountServiceSession); + + if (R_SUCCEEDED(rc)) { + IpcCommandResponse r; + ipcParseResponse(&r); + + struct { + u64 magic; + u64 result; + u128 userID; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc) && userID) { + *userID = resp->userID; + if (account_selected) { + *account_selected = 0; + if (*userID != 0) *account_selected = 1; + } + } + } + + return rc; +} +