~aleteoryx/muditaos

52d5a35c1b9584612587fa62bf8e83c5c5a641fe — Bartosz Cichocki 4 years ago ce3676b
[EGD-6447] Add Bluetooth PIN pairing

Implemented legacy pairing using PIN code
M module-apps/application-settings-new/ApplicationSettings.cpp => module-apps/application-settings-new/ApplicationSettings.cpp +5 -0
@@ 55,6 55,7 @@
#include <application-settings-new/data/LanguagesData.hpp>
#include <application-settings-new/data/PhoneNameData.hpp>
#include <application-settings-new/data/PINSettingsLockStateData.hpp>
#include <application-settings-new/windows/BluetoothCheckPasskeyWindow.hpp>

#include <service-evtmgr/EventManagerServiceAPI.hpp>
#include <service-cellular/CellularServiceAPI.hpp>


@@ 502,6 503,10 @@ namespace app
        windowsFactory.attach(gui::window::name::connection_frequency, [](Application *app, const std::string &name) {
            return std::make_unique<gui::ConnectionFrequencyWindow>(app, static_cast<ApplicationSettingsNew *>(app));
        });
        windowsFactory.attach(gui::window::name::bluetooth_check_passkey,
                              [](Application *app, const std::string &name) {
                                  return std::make_unique<gui::BluetoothCheckPasskeyWindow>(app);
                              });
        attachPopups(
            {gui::popup::ID::Volume, gui::popup::ID::Tethering, gui::popup::ID::PhoneModes, gui::popup::ID::PhoneLock});
    }

M module-apps/application-settings-new/windows/BluetoothCheckPasskeyWindow.cpp => module-apps/application-settings-new/windows/BluetoothCheckPasskeyWindow.cpp +2 -0
@@ 19,6 19,7 @@ namespace gui
    BluetoothCheckPasskeyWindow::BluetoothCheckPasskeyWindow(app::Application *app)
        : AppWindow(app, window::name::bluetooth_check_passkey)
    {
        bluetoothSettingsModel = std::make_unique<BluetoothSettingsModel>(application);
        buildInterface();
    }



@@ 61,6 62,7 @@ namespace gui
        auto passkey = text->getText();
        if (passkey.length() >= minPasskeyCharactersCount && inputEvent.isShortRelease(KeyCode::KEY_ENTER)) {
            bluetoothSettingsModel->responsePasskey(passkey);
            application->returnToPreviousWindow();
            return true;
        }
        return AppWindow::onInput(inputEvent);

M module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DP.cpp => module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DP.cpp +0 -7
@@ 299,13 299,6 @@ namespace bluetooth
        if (packetType != HCI_EVENT_PACKET) {
            return;
        }

        if (hci_event_packet_get_type(packet) == HCI_EVENT_PIN_CODE_REQUEST) {
            bd_addr_t address;
            LOG_INFO("Pin code request - using '0000'\n");
            hci_event_pin_code_request_get_bd_addr(packet, address);
            gap_pin_code_response(address, "0000");
        }
    }

    void A2DP::A2DPImpl::sourcePacketHandler(uint8_t packetType, uint16_t channel, uint8_t *packet, uint16_t size)

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp +18 -5
@@ 7,7 7,8 @@
#include <service-bluetooth/BluetoothMessage.hpp>
#include <service-bluetooth/messages/ResponseVisibleDevices.hpp>
#include <service-bluetooth/messages/Unpair.hpp>

#include <service-bluetooth/messages/Passkey.hpp>
#include <application-settings-new/ApplicationSettings.hpp>
extern "C"
{
#include "btstack.h"


@@ 84,7 85,7 @@ namespace bluetooth
    {
        auto msg = std::make_shared<message::bluetooth::ResponseVisibleDevices>(devices);
        ownerService->bus.sendUnicast(msg, "ApplicationSettings");
        ownerService->bus.sendUnicast(std::move(msg), "ApplicationSettingsNew");
        ownerService->bus.sendUnicast(std::move(msg), app::name_settings_new);
    }

    auto GAP::startScan() -> int


@@ 209,7 210,7 @@ namespace bluetooth

        auto msg = std::make_shared<BluetoothPairResultMessage>(currentlyProcessedDeviceAddr, result == 0u);
        currentlyProcessedDeviceAddr.clear();
        ownerService->bus.sendUnicast(std::move(msg), "ApplicationSettingsNew");
        ownerService->bus.sendUnicast(std::move(msg), app::name_settings_new);
    }
    /* @text In ACTIVE, the following events are processed:
     *  - GAP Inquiry result event: BTstack provides a unified inquiry result that contain


@@ 257,7 258,13 @@ namespace bluetooth
        if (packet_type != HCI_EVENT_PACKET) {
            return;
        }

        if (hci_event_packet_get_type(packet) == HCI_EVENT_PIN_CODE_REQUEST) {
            bd_addr_t address;
            LOG_DEBUG("PIN code request!");
            hci_event_pin_code_request_get_bd_addr(packet, address);
            auto msg = std::make_shared<::message::bluetooth::RequestPasskey>();
            ownerService->bus.sendUnicast(std::move(msg), app::name_settings_new);
        }
        const auto eventType = hci_event_packet_get_type(packet);
        switch (state) {
        case ScanState::init:


@@ 288,7 295,7 @@ namespace bluetooth
        std::string unpairedDevAddr{bd_addr_to_str(addr)};
        ownerService->bus.sendUnicast(
            std::make_shared<message::bluetooth::UnpairResult>(std::move(unpairedDevAddr), true),
            "ApplicationSettingsNew");
            app::name_settings_new);
        return true;
    }
    auto GAP::isServiceSupportedByRemote(bd_addr_t addr, uint32_t typeOfService) -> bool


@@ 300,4 307,10 @@ namespace bluetooth
        }
        return false;
    }
    void GAP::respondPinCode(const std::string &pin)
    {
        bd_addr_t address{};
        sscanf_bd_addr(currentlyProcessedDeviceAddr.c_str(), address);
        gap_pin_code_response(address, pin.c_str());
    }
} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp +1 -0
@@ 53,6 53,7 @@ namespace bluetooth
        auto unpair(uint8_t *addr) -> bool;
        static auto getDevicesList() -> const std::vector<Devicei> &;
        static auto isServiceSupportedByRemote(bd_addr_t addr, uint32_t typeOfService) -> bool;
        static void respondPinCode(const std::string &pin);

        static std::string currentlyProcessedDeviceAddr;
        explicit GAP(sys::Service *owner);

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +10 -0
@@ 35,6 35,8 @@
#include <BtKeysStorage.hpp>
#include <Timers/TimerFactory.hpp>
#include <typeinfo>
#include <service-bluetooth/messages/Passkey.hpp>
#include <GAP/GAP.hpp>

namespace
{


@@ 94,6 96,7 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
    connectHandler<message::bluetooth::SetStatus>();
    connectHandler<message::bluetooth::Unpair>();
    connectHandler<sdesktop::developerMode::DeveloperModeRequest>();
    connectHandler<message::bluetooth::ResponsePasskey>();

    settingsHolder->onStateChange = [this]() {
        auto initialState = std::visit(bluetooth::IntVisitor(), settingsHolder->getValue(bluetooth::Settings::State));


@@ 259,6 262,13 @@ auto ServiceBluetooth::handle(message::bluetooth::DisconnectResult *msg) -> std:
    return sys::MessageNone{};
}

auto ServiceBluetooth::handle(message::bluetooth::ResponsePasskey *msg) -> std::shared_ptr<sys::Message>
{
    auto passKey = msg->getPasskey();
    bluetooth::GAP::respondPinCode(passKey);
    return sys::MessageNone{};
}

auto ServiceBluetooth::handle(BluetoothMessage *msg) -> std::shared_ptr<sys::Message>
{
    LOG_INFO("Bluetooth request!");

M module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp => module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +5 -1
@@ 16,7 16,10 @@
#include <Timers/TimerHandle.hpp>

#include <memory> // for unique_ptr

namespace message::bluetooth
{
    class ResponsePasskey;
}
class BluetoothWorker;
namespace settings
{


@@ 98,6 101,7 @@ class ServiceBluetooth : public sys::Service
    [[nodiscard]] auto handle(message::bluetooth::AudioVolume *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::Ring *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::StartAudioRouting *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::ResponsePasskey *msg) -> std::shared_ptr<sys::Message>;
};

namespace sys