2018-04-18 16:57:06 +02:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <type_traits>
|
2018-04-19 00:24:40 +02:00
|
|
|
#include <cstdio>
|
2018-04-18 16:57:06 +02:00
|
|
|
|
|
|
|
#include "iserviceobject.hpp"
|
2018-04-18 19:41:17 +02:00
|
|
|
#include "iwaitable.hpp"
|
2018-04-18 16:57:06 +02:00
|
|
|
#include "serviceserver.hpp"
|
|
|
|
|
2018-04-20 00:40:05 +02:00
|
|
|
enum IpcControlCommand {
|
|
|
|
IpcCtrl_Cmd_ConvertCurrentObjectToDomain = 0,
|
|
|
|
IpcCtrl_Cmd_CopyFromCurrentDomain = 1,
|
|
|
|
IpcCtrl_Cmd_CloneCurrentObject = 2,
|
|
|
|
IpcCtrl_Cmd_QueryPointerBufferSize = 3,
|
2018-04-20 00:41:51 +02:00
|
|
|
IpcCtrl_Cmd_CloneCurrentObjectEx = 4
|
2018-04-20 00:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define POINTER_BUFFER_SIZE_MAX 0xFFFF
|
|
|
|
|
2018-04-18 16:57:06 +02:00
|
|
|
template <typename T>
|
|
|
|
class ServiceServer;
|
|
|
|
|
|
|
|
template <typename T>
|
2018-04-18 20:10:45 +02:00
|
|
|
class ServiceSession : public IWaitable {
|
2018-04-18 16:57:06 +02:00
|
|
|
static_assert(std::is_base_of<IServiceObject, T>::value, "Service Objects must derive from IServiceObject");
|
|
|
|
|
|
|
|
T *service_object;
|
|
|
|
ServiceServer<T> *server;
|
|
|
|
Handle server_handle;
|
|
|
|
Handle client_handle;
|
2018-04-19 05:12:09 +02:00
|
|
|
char pointer_buffer[0x400];
|
2018-04-20 00:40:05 +02:00
|
|
|
|
|
|
|
static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!");
|
|
|
|
|
2018-04-18 16:57:06 +02:00
|
|
|
public:
|
2018-04-18 20:10:45 +02:00
|
|
|
ServiceSession<T>(ServiceServer<T> *s, Handle s_h, Handle c_h) : server(s), server_handle(s_h), client_handle(c_h) {
|
2018-04-18 16:57:06 +02:00
|
|
|
this->service_object = new T();
|
|
|
|
}
|
|
|
|
|
2018-04-18 19:41:17 +02:00
|
|
|
virtual ~ServiceSession() {
|
2018-04-18 16:57:06 +02:00
|
|
|
delete this->service_object;
|
|
|
|
if (server_handle) {
|
|
|
|
svcCloseHandle(server_handle);
|
|
|
|
}
|
|
|
|
if (client_handle) {
|
|
|
|
svcCloseHandle(client_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T *get_service_object() { return this->service_object; }
|
|
|
|
Handle get_server_handle() { return this->server_handle; }
|
|
|
|
Handle get_client_handle() { return this->client_handle; }
|
2018-04-18 19:41:17 +02:00
|
|
|
|
|
|
|
/* IWaitable */
|
2018-04-18 20:10:45 +02:00
|
|
|
virtual unsigned int get_num_waitables() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void get_waitables(IWaitable **dst) {
|
|
|
|
dst[0] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void delete_child(IWaitable *child) {
|
|
|
|
/* TODO: Panic, because we can never have any children. */
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Handle get_handle() {
|
|
|
|
return this->server_handle;
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
virtual Result handle_signaled(u64 timeout) {
|
|
|
|
Result rc;
|
|
|
|
int handle_index;
|
2018-04-19 05:12:09 +02:00
|
|
|
|
|
|
|
/* Prepare pointer buffer... */
|
|
|
|
IpcCommand c_for_reply;
|
|
|
|
ipcInitialize(&c_for_reply);
|
|
|
|
ipcAddRecvStatic(&c_for_reply, this->pointer_buffer, sizeof(this->pointer_buffer), 0);
|
|
|
|
ipcPrepareHeader(&c_for_reply, 0);
|
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, timeout))) {
|
|
|
|
if (handle_index != 0) {
|
|
|
|
/* TODO: Panic? */
|
|
|
|
}
|
|
|
|
u32 *cmdbuf = (u32 *)armGetTls();
|
|
|
|
u32 out_words = 4;
|
|
|
|
u32 extra_rawdata_count = 0;
|
|
|
|
u32 wordcount = 0;
|
|
|
|
Result retval = 0;
|
|
|
|
u32 *rawdata_start = cmdbuf;
|
2018-04-19 05:12:09 +02:00
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
IpcParsedCommand r;
|
|
|
|
IpcCommand c;
|
|
|
|
|
2018-04-19 00:24:40 +02:00
|
|
|
fprintf(stderr, "Doing ServiceSession parse...\n");
|
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
ipcInitialize(&c);
|
|
|
|
|
|
|
|
retval = ipcParse(&r);
|
2018-04-19 05:12:09 +02:00
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
if (R_SUCCEEDED(retval)) {
|
|
|
|
rawdata_start = (u32 *)r.Raw;
|
|
|
|
wordcount = r.RawSize;
|
2018-04-20 00:28:19 +02:00
|
|
|
switch (r.CommandType) {
|
|
|
|
case IpcCommandType_Close:
|
|
|
|
/* TODO: This should close the session and clean up its resources. */
|
|
|
|
retval = 0xF601;
|
|
|
|
break;
|
|
|
|
case IpcCommandType_LegacyControl:
|
|
|
|
/* TODO: What does this allow one to do? */
|
|
|
|
retval = 0xF601;
|
|
|
|
break;
|
|
|
|
case IpcCommandType_LegacyRequest:
|
|
|
|
/* TODO: What does this allow one to do? */
|
|
|
|
retval = 0xF601;
|
|
|
|
break;
|
|
|
|
case IpcCommandType_Request:
|
|
|
|
case IpcCommandType_RequestWithContext:
|
|
|
|
retval = this->service_object->dispatch(&r, &c, cmdbuf, rawdata_start[2], &rawdata_start[4], wordcount - 6, &cmdbuf[8], &extra_rawdata_count);
|
2018-04-20 00:29:44 +02:00
|
|
|
out_words += extra_rawdata_count;
|
2018-04-20 00:28:19 +02:00
|
|
|
break;
|
2018-04-20 00:40:05 +02:00
|
|
|
case IpcCommandType_Control:
|
|
|
|
case IpcCommandType_ControlWithContext:
|
|
|
|
retval = this->dispatch_control_command(&r, &c, cmdbuf, rawdata_start[2], &rawdata_start[4], wordcount - 6, &cmdbuf[8], &extra_rawdata_count);
|
|
|
|
out_words += extra_rawdata_count;
|
|
|
|
break;
|
2018-04-20 00:28:19 +02:00
|
|
|
case IpcCommandType_Invalid:
|
|
|
|
default:
|
|
|
|
retval = 0xF601;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:24:40 +02:00
|
|
|
if (retval != 0xF601) {
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 retval;
|
|
|
|
} *raw;
|
2018-04-18 20:53:04 +02:00
|
|
|
|
2018-04-19 00:24:40 +02:00
|
|
|
raw = (decltype(raw))ipcPrepareHeader(&c, out_words);
|
2018-04-18 20:53:04 +02:00
|
|
|
|
2018-04-19 00:24:40 +02:00
|
|
|
raw->magic = SFCO_MAGIC;
|
|
|
|
raw->retval = retval;
|
|
|
|
|
|
|
|
rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, this->server_handle, 0);
|
|
|
|
} else {
|
|
|
|
rc = retval;
|
|
|
|
}
|
2018-04-18 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-04-18 20:10:45 +02:00
|
|
|
}
|
2018-04-20 00:40:05 +02:00
|
|
|
|
|
|
|
Result dispatch_control_command(IpcParsedCommand *r, IpcCommand *out_c, u32 *cmd_buf, u32 cmd_id, u32 *in_rawdata, u32 in_rawdata_size, u32 *out_rawdata, u32 *out_raw_data_count) {
|
|
|
|
Result rc = 0xF601;
|
|
|
|
|
|
|
|
/* TODO: Implement. */
|
|
|
|
switch ((IpcControlCommand)cmd_id) {
|
|
|
|
case IpcCtrl_Cmd_ConvertCurrentObjectToDomain:
|
|
|
|
/* TODO */
|
|
|
|
break;
|
|
|
|
case IpcCtrl_Cmd_CopyFromCurrentDomain:
|
|
|
|
/* TODO */
|
|
|
|
break;
|
|
|
|
case IpcCtrl_Cmd_CloneCurrentObject:
|
|
|
|
/* TODO */
|
|
|
|
break;
|
|
|
|
case IpcCtrl_Cmd_QueryPointerBufferSize:
|
|
|
|
if (r->HasPid || r->NumHandles != 0 || r->NumBuffers != 0 || r->NumStatics != 0 || r->NumStaticsOut != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*out_rawdata = sizeof(this->pointer_buffer);
|
|
|
|
*out_raw_data_count = 1;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
2018-04-20 00:41:51 +02:00
|
|
|
case IpcCtrl_Cmd_CloneCurrentObjectEx:
|
2018-04-20 00:40:05 +02:00
|
|
|
/* TODO */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2018-04-18 16:57:06 +02:00
|
|
|
};
|