Started irs IR-sensor support.

This commit is contained in:
yellows8 2018-01-29 23:16:06 -05:00
parent 993071f67e
commit ca6d017be9
3 changed files with 210 additions and 0 deletions

View File

@ -35,6 +35,7 @@ extern "C" {
#include "switch/services/fatal.h" #include "switch/services/fatal.h"
#include "switch/services/usb.h" #include "switch/services/usb.h"
#include "switch/services/hid.h" #include "switch/services/hid.h"
#include "switch/services/irs.h"
#include "switch/services/vi.h" #include "switch/services/vi.h"
#include "switch/services/nv.h" #include "switch/services/nv.h"
#include "switch/services/pm.h" #include "switch/services/pm.h"

View File

@ -0,0 +1,22 @@
/**
* @file irs.h
* @brief HID IR sensor service.
* @author yellows8
* @copyright libnx Authors
*/
#pragma once
#include "../types.h"
#include "../services/sm.h"
#include "../services/hid.h"
Result irsInitialize(void);
void irsExit(void);
Service* irsGetSessionService(void);
void* irsGetSharedmemAddr(void);
/// (De)activate the IR sensor, this is automatically used by irsExit(). Must be called after irsInitialize() to activate the IR sensor.
Result irsActivateIrsensor(bool activate);
Result irsGetIrCameraHandle(u32 *IrCameraHandle, HidControllerID id);

187
nx/source/services/irs.c Normal file
View File

@ -0,0 +1,187 @@
#include <string.h>
#include "types.h"
#include "result.h"
#include "ipc.h"
#include "services/applet.h"
#include "services/irs.h"
#include "services/hid.h"
#include "services/sm.h"
#include "kernel/shmem.h"
static Service g_irsSrv;
static SharedMemory g_irsSharedmem;
bool g_irsSensorActivated;
static Result _irsGetIrsensorSharedMemoryHandle(Handle* handle_out, u64 AppletResourceUserId);
Result irsInitialize(void)
{
if (serviceIsActive(&g_irsSrv))
return MAKERESULT(Module_Libnx, LibnxError_AlreadyInitialized);
Result rc;
Handle sharedmem_handle;
u64 AppletResourceUserId=0;
rc = appletGetAppletResourceUserId(&AppletResourceUserId);
if (R_FAILED(rc))
return rc;
rc = smGetService(&g_irsSrv, "irs");
if (R_FAILED(rc))
return rc;
rc = _irsGetIrsensorSharedMemoryHandle(&sharedmem_handle, AppletResourceUserId);
if (R_SUCCEEDED(rc))
{
shmemLoadRemote(&g_irsSharedmem, sharedmem_handle, 0x8000, PERM_R);
rc = shmemMap(&g_irsSharedmem);
}
if (R_FAILED(rc))
irsExit();
return rc;
}
void irsExit(void)
{
irsActivateIrsensor(0);
serviceClose(&g_irsSrv);
shmemClose(&g_irsSharedmem);
}
Service* irsGetSessionService(void) {
return &g_irsSrv;
}
void* irsGetSharedmemAddr(void) {
return shmemGetAddr(&g_irsSharedmem);
}
Result irsActivateIrsensor(bool activate) {
if (g_irsSensorActivated==activate) return 0;
Result rc=0;
u64 AppletResourceUserId=0;
rc = appletGetAppletResourceUserId(&AppletResourceUserId);
if (R_FAILED(rc))
return rc;
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
u64 AppletResourceUserId;
} *raw;
ipcSendPid(&c);
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = activate ? 302 : 303;
raw->AppletResourceUserId = AppletResourceUserId;
rc = serviceIpcDispatch(&g_irsSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) g_irsSensorActivated = activate;
}
return rc;
}
static Result _irsGetIrsensorSharedMemoryHandle(Handle* handle_out, u64 AppletResourceUserId) {
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
u64 AppletResourceUserId;
} *raw;
ipcSendPid(&c);
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 304;
raw->AppletResourceUserId = AppletResourceUserId;
Result rc = serviceIpcDispatch(&g_irsSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*handle_out = r.Handles[0];
}
}
return rc;
}
Result irsGetIrCameraHandle(u32 *IrCameraHandle, HidControllerID id) {
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
u32 id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 311;
raw->id = id;
Result rc = serviceIpcDispatch(&g_irsSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u32 IrCameraHandle;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc) && IrCameraHandle) {
*IrCameraHandle = resp->IrCameraHandle;
}
}
return rc;
}