2020-05-22 10:29:36 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <switch.h>
|
2020-05-11 20:30:08 +00:00
|
|
|
#include "btdrv_mitm_service.hpp"
|
2020-06-13 00:08:43 +00:00
|
|
|
#include "btdrv_mitm_flags.hpp"
|
2020-05-11 20:30:08 +00:00
|
|
|
#include "btdrv_shim.h"
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
#include "bluetooth/bluetooth_events.hpp"
|
2020-06-11 18:34:14 +00:00
|
|
|
#include "controllermanager.hpp"
|
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
namespace ams::mitm::btdrv {
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeBluetooth(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeBluetooth");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::core::IsInitialized()) {
|
2020-05-22 10:29:36 +00:00
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-13 17:15:59 +00:00
|
|
|
R_TRY(btdrvInitializeBluetoothFwd(this->forward_service.get(), &handle));
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(bluetooth::core::Initialize(handle));
|
2020-06-15 12:13:17 +00:00
|
|
|
|
|
|
|
if (hos::GetVersion() >= hos::Version_7_0_0)
|
|
|
|
R_TRY(bluetooth::hid::report::InitializeFakeSharedMemory());
|
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
} else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetUserForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::FinalizeBluetooth(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeBluetooth");
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
// Only btm should be able to make this call
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeBluetoothFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
Result BtdrvMitmService::GetEventInfo(sf::Out<BluetoothEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-09 19:58:32 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetEventInfo");
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::core::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 22:18:46 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeHid(sf::OutCopyHandle out_handle, u16 version) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeHid");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::hid::IsInitialized()) {
|
|
|
|
Handle handle = INVALID_HANDLE;
|
|
|
|
R_TRY(btdrvInitializeHidFwd(this->forward_service.get(), &handle, version));
|
|
|
|
R_TRY(bluetooth::hid::Initialize(handle));
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetUserForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::WriteHidData(BluetoothAddress address, const sf::InPointerBuffer &buffer) {
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
//BTDRV_LOG_FMT("btdrv-mitm: WriteHidData (caller: %s)", this->client_info.program_id == ncm::SystemProgramId::Hid ? "HID" : "other");
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: WriteHidData");
|
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Hid) {
|
2020-06-09 20:39:30 +00:00
|
|
|
auto controller = locateController(&address);
|
2020-06-14 22:18:46 +00:00
|
|
|
if (controller && !controller->isSwitchController()) {
|
2020-06-09 20:39:30 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: WriteHidData - Non-Switch controller");
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
// TODO: convert hid data format where possible and call btdrvWriteHidDataFwd
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvWriteHidDataFwd(this->forward_service.get(),
|
2020-05-22 10:29:36 +00:00
|
|
|
&address,
|
|
|
|
reinterpret_cast<const BluetoothHidData *>(buffer.GetPointer())
|
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
Result BtdrvMitmService::SetHidReport(BluetoothAddress address, BluetoothHhReportType type, const sf::InPointerBuffer &buffer) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: SetHidReport");
|
|
|
|
|
|
|
|
R_TRY(btdrvSetHidReportFwd(this->forward_service.get(),
|
|
|
|
&address,
|
|
|
|
type,
|
|
|
|
reinterpret_cast<const BluetoothHidData *>(buffer.GetPointer())
|
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::GetHidReport(BluetoothAddress address, BluetoothHhReportType type, u8 id) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidReport");
|
|
|
|
|
|
|
|
R_TRY(btdrvGetHidReportFwd(this->forward_service.get(), &address, type, id));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-25 21:50:43 +00:00
|
|
|
/*
|
2020-06-28 15:32:48 +00:00
|
|
|
//Result BtdrvMitmService::GetPairedDeviceInfo(sf::Out<BluetoothDevicesSettings> out, BluetoothAddress address) {
|
|
|
|
Result BtdrvMitmService::GetPairedDeviceInfo(BluetoothAddress address, sf::Out<BluetoothDevicesSettings> out) {
|
|
|
|
//Result BtdrvMitmService::GetPairedDeviceInfo(BluetoothAddress address, const sf::OutPointerBuffer &out_buffer) {
|
|
|
|
//Result BtdrvMitmService::GetPairedDeviceInfo(const sf::OutPointerBuffer &out_buffer, BluetoothAddress address) {
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetPairedDeviceInfo");
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
R_TRY(btdrvGetPairedDeviceInfoFwd(this->forward_service.get(), &address, out.GetPointer()));
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
BluetoothDevicesSettings *device = reinterpret_cast<BluetoothDevicesSettings *>(out.GetPointer());
|
|
|
|
|
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
|
|
|
|
|
|
|
if (!IsValidSwitchControllerName(device->name)) {
|
|
|
|
std::strncpy(device->name, "Lic Pro Controller", sizeof(BluetoothLocalName) - 1);
|
|
|
|
device->device_class = {0x00, 0x25, 0x08};
|
2020-06-22 21:14:54 +00:00
|
|
|
}
|
2020-06-28 15:32:48 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
BTDRV_LOG_FMT("name: %s\nvid: %04x\npid: %04x", device->name, device->vid, device->pid);
|
2020-06-25 21:50:43 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
//BTDRV_LOG_DATA_MSG(out_buffer.GetPointer(), sizeof(BluetoothDevicesSettings), "btdrv-mitm: GetPairedDeviceInfo vv");
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-25 21:50:43 +00:00
|
|
|
*/
|
2020-06-28 15:32:48 +00:00
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::FinalizeHid(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeHid");
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
// Only btm should be able to make this call
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeHidFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
Result BtdrvMitmService::GetHidEventInfo(sf::Out<HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidEventInfo");
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::hid::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 22:18:46 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Result BtdrvMitmService::SetTsi(BluetoothAddress address, u8 tsi) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: SetTsi");
|
2020-06-14 22:18:46 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(btdrvSetTsiFwd(this->forward_service.get(), &address, tsi));
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-12 21:25:09 +00:00
|
|
|
/* 1.0.0 - 3.0.2 */
|
|
|
|
Result BtdrvMitmService::RegisterHidReportEventDeprecated(sf::OutCopyHandle out_handle) {
|
|
|
|
return RegisterHidReportEvent(out_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4.0.0+ */
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::RegisterHidReportEvent(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: RegisterHidReportEvent");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::hid::report::IsInitialized()) {
|
2020-06-02 21:24:40 +00:00
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvRegisterHidReportEventFwd(this->forward_service.get(), &handle));
|
|
|
|
R_TRY(bluetooth::hid::report::Initialize(handle));
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetForwardEvent()));
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetUserForwardEvent()));
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 1.0.0 - 6.2.0 */
|
|
|
|
Result _GetHidReportEventInfoDeprecated(Service *srv, sf::Out<HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-12 21:25:09 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidReportEventInfo");
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::hid::report::GetEventInfo(out_type.GetPointer(),
|
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
2020-06-12 21:25:09 +00:00
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-12 21:25:09 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 1.0.0 - 3.0.2 */
|
|
|
|
Result BtdrvMitmService::GetHidReportEventInfoDeprecated1(sf::Out<HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
|
|
|
return _GetHidReportEventInfoDeprecated(this->forward_service.get(), out_type, out_buffer);
|
|
|
|
}
|
2020-06-12 21:25:09 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 4.0.0 - 6.2.0 */
|
|
|
|
Result BtdrvMitmService::GetHidReportEventInfoDeprecated2(sf::Out<HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
|
|
|
return _GetHidReportEventInfoDeprecated(this->forward_service.get(), out_type, out_buffer);
|
2020-06-12 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 7.0.0+ */
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::GetHidReportEventInfo(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidReportEventInfo");
|
|
|
|
|
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvGetHidReportEventInfoFwd(this->forward_service.get(), &handle));
|
2020-06-13 17:15:59 +00:00
|
|
|
R_TRY(bluetooth::hid::report::MapRemoteSharedMemory(handle));
|
|
|
|
out_handle.SetValue(bluetooth::hid::report::GetFakeSharedMemory()->handle);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeBle(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeBle");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::ble::IsInitialized()) {
|
|
|
|
Handle handle = INVALID_HANDLE;
|
|
|
|
R_TRY(btdrvInitializeBleFwd(this->forward_service.get(), &handle));
|
|
|
|
R_TRY(bluetooth::ble::Initialize(handle));
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetForwardEvent()));
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetUserForwardEvent()));
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::FinalizeBle(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeBle");
|
|
|
|
|
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeBleFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
2020-05-11 20:30:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 23:14:09 +00:00
|
|
|
Result BtdrvMitmService::GetBleManagedEventInfoDeprecated(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
|
|
|
return GetBleManagedEventInfo(out_type, out_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::GetBleManagedEventInfo(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetBleManagedEventInfo");
|
|
|
|
|
|
|
|
R_TRY(bluetooth::ble::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 23:14:09 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:28:46 +00:00
|
|
|
void BtdrvMitmService::RedirectSystemEvents(bool redirect) {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-06-25 21:50:43 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: RedirectSystemEvents [%s]", redirect ? "on" : "off");
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
g_redirectEvents = redirect;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:30:08 +00:00
|
|
|
}
|