forked from switchbrew/libnx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial time support, timezones are not handled yet.
- Loading branch information
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @file time.h | ||
* @brief Time services IPC wrapper. | ||
* @author yellows8 | ||
* @copyright libnx Authors | ||
*/ | ||
#pragma once | ||
|
||
#include "../types.h" | ||
#include "../services/sm.h" | ||
|
||
typedef enum { | ||
TimeType_UserSystemClock, | ||
TimeType_NetworkSystemClock, | ||
TimeType_LocalSystemClock, | ||
TimeType_Default = TimeType_NetworkSystemClock, | ||
} TimeType; | ||
|
||
Result timeInitialize(void); | ||
void timeExit(void); | ||
|
||
Service* timeGetSessionService(void); | ||
|
||
Result timeGetCurrentTime(TimeType type, u64 *timestamp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include <string.h> | ||
#include "types.h" | ||
#include "result.h" | ||
#include "ipc.h" | ||
#include "services/time.h" | ||
#include "services/sm.h" | ||
|
||
static Service g_timeSrv; | ||
static Service g_timeUserSystemClock; | ||
static Service g_timeNetworkSystemClock; | ||
static Service g_timeTimeZoneService; | ||
static Service g_timeLocalSystemClock; | ||
|
||
static Result _timeGetSession(Service* srv_out, u64 cmd_id); | ||
|
||
Result timeInitialize(void) | ||
{ | ||
if (serviceIsActive(&g_timeSrv)) | ||
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized); | ||
|
||
Result rc; | ||
|
||
rc = smGetService(&g_timeSrv, "time:u"); | ||
if (R_FAILED(rc)) | ||
return rc; | ||
|
||
rc = _timeGetSession(&g_timeUserSystemClock, 0); | ||
|
||
if (R_SUCCEEDED(rc)) | ||
rc = _timeGetSession(&g_timeNetworkSystemClock, 1); | ||
|
||
if (R_SUCCEEDED(rc)) | ||
rc = _timeGetSession(&g_timeTimeZoneService, 3); | ||
|
||
if (R_SUCCEEDED(rc)) | ||
rc = _timeGetSession(&g_timeLocalSystemClock, 4); | ||
|
||
if (R_FAILED(rc)) | ||
timeExit(); | ||
|
||
return rc; | ||
} | ||
|
||
void timeExit(void) | ||
{ | ||
if (!serviceIsActive(&g_timeSrv)) | ||
return; | ||
|
||
serviceClose(&g_timeLocalSystemClock); | ||
serviceClose(&g_timeTimeZoneService); | ||
serviceClose(&g_timeNetworkSystemClock); | ||
serviceClose(&g_timeUserSystemClock); | ||
serviceClose(&g_timeSrv); | ||
} | ||
|
||
Service* timeGetSessionService(void) { | ||
return &g_timeSrv; | ||
} | ||
|
||
static Result _timeGetSession(Service* srv_out, u64 cmd_id) { | ||
IpcCommand c; | ||
ipcInitialize(&c); | ||
|
||
struct { | ||
u64 magic; | ||
u64 cmd_id; | ||
} *raw; | ||
|
||
raw = ipcPrepareHeader(&c, sizeof(*raw)); | ||
|
||
raw->magic = SFCI_MAGIC; | ||
raw->cmd_id = cmd_id; | ||
|
||
Result rc = serviceIpcDispatch(&g_timeSrv); | ||
|
||
if (R_SUCCEEDED(rc)) { | ||
IpcParsedCommand r; | ||
ipcParse(&r); | ||
|
||
struct { | ||
u64 magic; | ||
u64 result; | ||
} *resp = r.Raw; | ||
|
||
rc = resp->result; | ||
|
||
if (R_SUCCEEDED(rc)) { | ||
serviceCreate(srv_out, r.Handles[0]); | ||
} | ||
} | ||
|
||
return rc; | ||
} | ||
|
||
Result timeGetCurrentTime(TimeType type, u64 *timestamp) { | ||
Service *srv = NULL; | ||
|
||
if (type==TimeType_UserSystemClock) { | ||
srv = &g_timeUserSystemClock; | ||
} | ||
else if (type==TimeType_NetworkSystemClock) { | ||
srv = &g_timeNetworkSystemClock; | ||
} | ||
else if (type==TimeType_LocalSystemClock) { | ||
srv = &g_timeLocalSystemClock; | ||
} | ||
else { | ||
return MAKERESULT(Module_Libnx, LibnxError_BadInput); | ||
} | ||
|
||
IpcCommand c; | ||
ipcInitialize(&c); | ||
|
||
struct { | ||
u64 magic; | ||
u64 cmd_id; | ||
} *raw; | ||
|
||
raw = ipcPrepareHeader(&c, sizeof(*raw)); | ||
|
||
raw->magic = SFCI_MAGIC; | ||
raw->cmd_id = 0; | ||
|
||
Result rc = serviceIpcDispatch(srv); | ||
|
||
if (R_SUCCEEDED(rc)) { | ||
IpcParsedCommand r; | ||
ipcParse(&r); | ||
|
||
struct { | ||
u64 magic; | ||
u64 result; | ||
u64 timestamp; | ||
} *resp = r.Raw; | ||
|
||
rc = resp->result; | ||
|
||
if (R_SUCCEEDED(rc) && timestamp) *timestamp = resp->timestamp; | ||
} | ||
|
||
return rc; | ||
} | ||
|