diff --git a/btdrv-mitm/source/btm_mitm/btm/btm_types.hpp b/btdrv-mitm/source/btm_mitm/btm/btm_types.hpp new file mode 100644 index 0000000..b04fbb8 --- /dev/null +++ b/btdrv-mitm/source/btm_mitm/btm/btm_types.hpp @@ -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 . + */ +#pragma once +#include +#include + +namespace ams::mitm::btm { + + struct DeviceCondition : sf::LargeData { + BtmDeviceCondition condition; + }; + + struct DeviceInfo : sf::LargeData { + BtmDeviceInfo info; + }; + +} diff --git a/btdrv-mitm/source/btm_mitm/btm_mitm_service.cpp b/btdrv-mitm/source/btm_mitm/btm_mitm_service.cpp new file mode 100644 index 0000000..2ead8e8 --- /dev/null +++ b/btdrv-mitm/source/btm_mitm/btm_mitm_service.cpp @@ -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 . + */ +#include "btm_mitm_service.hpp" +#include "btm_shim.h" +#include + +namespace ams::mitm::btm { + + Result BtmMitmService::GetDeviceCondition(sf::Out out) { + auto device_condition = reinterpret_cast(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 out) { + auto device_info = reinterpret_cast(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(); + } + +} diff --git a/btdrv-mitm/source/btm_mitm/btm_mitm_service.hpp b/btdrv-mitm/source/btm_mitm/btm_mitm_service.hpp new file mode 100644 index 0000000..0d35f1d --- /dev/null +++ b/btdrv-mitm/source/btm_mitm/btm_mitm_service.hpp @@ -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 . + */ +#pragma once +#include +#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)) \ + AMS_SF_METHOD_INFO(C, H, 9, Result, GetDeviceInfo, (sf::Out)) \ + + 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 out); + Result GetDeviceInfo(sf::Out out); + }; + static_assert(IsIBtmMitmInterface); + +} diff --git a/btdrv-mitm/source/btm_mitm/btm_shim.c b/btdrv-mitm/source/btm_mitm/btm_shim.c new file mode 100644 index 0000000..bb70c54 --- /dev/null +++ b/btdrv-mitm/source/btm_mitm/btm_shim.c @@ -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 . + */ +#include "btm_shim.h" +#include + +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)} } + ); +} diff --git a/btdrv-mitm/source/btm_mitm/btm_shim.h b/btdrv-mitm/source/btm_mitm/btm_shim.h new file mode 100644 index 0000000..a8d8842 --- /dev/null +++ b/btdrv-mitm/source/btm_mitm/btm_shim.h @@ -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 . + */ +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +Result btmGetDeviceConditionFwd(Service* s, BtmDeviceCondition *condition); +Result btmGetDeviceInfoFwd(Service* s, BtmDeviceInfo *devices); + +#ifdef __cplusplus +} +#endif