Add btm-mitm files

This commit is contained in:
ndeadly 2020-09-08 16:17:07 +02:00
parent ee211be8f8
commit 80f63dfaf7
5 changed files with 192 additions and 0 deletions

View file

@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 ndeadly
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
namespace ams::mitm::btm {
struct DeviceCondition : sf::LargeData {
BtmDeviceCondition condition;
};
struct DeviceInfo : sf::LargeData {
BtmDeviceInfo info;
};
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 ndeadly
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "btm_mitm_service.hpp"
#include "btm_shim.h"
#include <cstring>
namespace ams::mitm::btm {
Result BtmMitmService::GetDeviceCondition(sf::Out<btm::DeviceCondition> out) {
auto device_condition = reinterpret_cast<BtmDeviceCondition *>(out.GetPointer());
R_TRY(btmGetDeviceConditionFwd(this->forward_service.get(), device_condition));
for (unsigned int i = 0; i < device_condition->connected_count; ++i) {
auto device = &device_condition->devices[i];
if (!IsOfficialSwitchControllerName(device->name, sizeof(device->name))) {
std::strncpy(device->name, controller::pro_controller_name, sizeof(device->name) - 1);
}
}
return ams::ResultSuccess();
}
Result BtmMitmService::GetDeviceInfo(sf::Out<btm::DeviceInfo> out) {
auto device_info = reinterpret_cast<BtmDeviceInfo *>(out.GetPointer());
R_TRY(btmGetDeviceInfoFwd(this->forward_service.get(), device_info));
for (unsigned int i = 0; i < device_info->count; ++i) {
auto device = &device_info->devices[i];
if (!IsOfficialSwitchControllerName(device->name, sizeof(device->name))) {
std::strncpy(device->name, controller::pro_controller_name, sizeof(device->name) - 1);
}
}
return ams::ResultSuccess();
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 ndeadly
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "btm_types.hpp"
namespace ams::mitm::btm {
namespace {
#define AMS_BTM_MITM_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 3, Result, GetDeviceCondition, (sf::Out<btm::DeviceCondition>)) \
AMS_SF_METHOD_INFO(C, H, 9, Result, GetDeviceInfo, (sf::Out<btm::DeviceInfo>)) \
AMS_SF_DEFINE_MITM_INTERFACE(IBtmMitmInterface, AMS_BTM_MITM_INTERFACE_INFO)
}
class BtmMitmService : public sf::MitmServiceImplBase {
public:
using MitmServiceImplBase::MitmServiceImplBase;
public:
static bool ShouldMitm(const sm::MitmProcessInfo &client_info) {
return true;
}
public:
Result GetDeviceCondition(sf::Out<btm::DeviceCondition> out);
Result GetDeviceInfo(sf::Out<btm::DeviceInfo> out);
};
static_assert(IsIBtmMitmInterface<BtmMitmService>);
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2020 ndeadly
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "btm_shim.h"
#include <stratosphere/sf/sf_mitm_dispatch.h>
Result btmGetDeviceConditionFwd(Service* s, BtmDeviceCondition *condition) {
return serviceMitmDispatch(s, 3,
.buffer_attrs = { SfBufferAttr_FixedSize | SfBufferAttr_HipcPointer | SfBufferAttr_Out },
.buffers = { {condition, sizeof(BtmDeviceCondition)} }
);
}
Result btmGetDeviceInfoFwd(Service* s, BtmDeviceInfo *devices) {
return serviceMitmDispatch(s, 9,
.buffer_attrs = { SfBufferAttr_FixedSize | SfBufferAttr_HipcPointer | SfBufferAttr_Out },
.buffers = { {devices, sizeof(BtmDeviceInfo)} }
);
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2020 ndeadly
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#ifdef __cplusplus
extern "C" {
#endif
Result btmGetDeviceConditionFwd(Service* s, BtmDeviceCondition *condition);
Result btmGetDeviceInfoFwd(Service* s, BtmDeviceInfo *devices);
#ifdef __cplusplus
}
#endif