~aleteoryx/muditaos

b911ad9044a57a649c91d915cedf16e77c4b5034 — SP2FET 4 years ago 686d539
[EGD-5940] Add BT device unpairing

Implemented possibility to forget/unpair a device via GUI
M module-bluetooth/Bluetooth/BluetoothWorker.cpp => module-bluetooth/Bluetooth/BluetoothWorker.cpp +14 -0
@@ 152,6 152,10 @@ auto BluetoothWorker::handleCommand(QueueHandle_t queue) -> bool
    case bluetooth::Command::PowerOff:
        controller->turnOff();
        break;
    case bluetooth::Command::Unpair:
        controller->processCommand(command);
        removeFromBoundDevices(command.getAddress());
        break;
    default:
        controller->processCommand(command);
        break;


@@ 259,3 263,13 @@ void BluetoothWorker::initDevicesList()
        std::visit(bluetooth::StringVisitor(), settings->getValue(bluetooth::Settings::BondedDevices));
    pairedDevices = SettingsSerializer::fromString(bondedDevicesStr);
}
void BluetoothWorker::removeFromBoundDevices(uint8_t *addr)
{
    auto position = std::find_if(
        pairedDevices.begin(), pairedDevices.end(), [&](Devicei device) { return !bd_addr_cmp(addr, device.address); });
    if (position != pairedDevices.end()) {
        pairedDevices.erase(position);
        settings->setValue(bluetooth::Settings::BondedDevices, SettingsSerializer::toString(pairedDevices));
        LOG_INFO("Device %s removed from paired devices list", bd_addr_to_str(addr));
    }
}

M module-bluetooth/Bluetooth/BluetoothWorker.hpp => module-bluetooth/Bluetooth/BluetoothWorker.hpp +1 -0
@@ 82,6 82,7 @@ class BluetoothWorker : private sys::Worker
    void registerQueues();
    void onLinkKeyAdded(const std::string &deviceAddress);
    void initDevicesList();
    void removeFromBoundDevices(uint8_t *addr);

  public:
    enum Error

M module-bluetooth/Bluetooth/CommandHandler.cpp => module-bluetooth/Bluetooth/CommandHandler.cpp +8 -0
@@ 48,6 48,8 @@ namespace bluetooth
            return startPan();
        case bluetooth::Command::Pair:
            return pair(command.getAddress());
        case bluetooth::Command::Unpair:
            return unpair(command.getAddress());
        case bluetooth::Command::VisibilityOn:
            return setVisibility(true);
        case bluetooth::Command::VisibilityOff:


@@ 144,5 146,11 @@ namespace bluetooth
        profileManager->switchProfile(profile);
        return Error::Success;
    }
    Error::Code CommandHandler::unpair(uint8_t *addr)
    {
        LOG_INFO("Unpairing %s", bd_addr_to_str(addr));

        return driver->unpair(addr) ? Error::Success : Error::LibraryError;
    }

} // namespace bluetooth

M module-bluetooth/Bluetooth/CommandHandler.hpp => module-bluetooth/Bluetooth/CommandHandler.hpp +2 -0
@@ 32,6 32,7 @@ namespace bluetooth
            PowerOn,
            PowerOff,
            Pair,
            Unpair,
            StartStream,
            StopStream,
            SwitchProfile,


@@ 89,6 90,7 @@ namespace bluetooth
        Error::Code establishAudioConnection(bd_addr_t addr);
        Error::Code disconnectAudioConnection();
        Error::Code pair(bd_addr_t addr);
        Error::Code unpair(bd_addr_t addr);

        Error::Code switchAudioProfile();
        sys::Service *service;

M module-bluetooth/Bluetooth/interface/BluetoothDriver.hpp => module-bluetooth/Bluetooth/interface/BluetoothDriver.hpp +2 -0
@@ 21,6 21,8 @@ namespace bluetooth
        virtual void stopScan()                                                                  = 0;
        virtual void setVisibility(bool visibility)                                              = 0;
        [[nodiscard]] virtual auto pair(uint8_t *addr, std::uint8_t protectionLevel = 0) -> bool = 0;
        [[nodiscard]] virtual auto unpair(uint8_t *addr) -> bool                                 = 0;

        virtual void registerErrorCallback(const ErrorCallback &newCallback) = 0;
    };
} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.cpp => module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.cpp +4 -0
@@ 206,4 206,8 @@ namespace bluetooth
    {
        return gap->pair(addr, protectionLevel);
    }
    auto Driver::unpair(uint8_t *addr) -> bool
    {
        return gap->unpair(addr);
    }
} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.hpp => module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.hpp +1 -0
@@ 39,5 39,6 @@ namespace bluetooth
        void stopScan() override;
        void setVisibility(bool visibility) override;
        auto pair(uint8_t *addr, std::uint8_t protectionLevel = 0) -> bool override;
        auto unpair(uint8_t *addr) -> bool override;
    };
} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp +13 -0
@@ 6,6 6,7 @@
#include <log/log.hpp>
#include <service-bluetooth/BluetoothMessage.hpp>
#include <service-bluetooth/messages/ResponseVisibleDevices.hpp>
#include <service-bluetooth/messages/Unpair.hpp>

extern "C"
{


@@ 269,5 270,17 @@ namespace bluetooth
    {
        return devices;
    }
    auto GAP::unpair(uint8_t *addr) -> bool
    {
        LOG_INFO("Unpairing device %s", bd_addr_to_str(addr));
        gap_drop_link_key_for_bd_addr(addr);

        LOG_INFO("Device %s unpaired", bd_addr_to_str(addr));
        std::string unpairedDevAddr{bd_addr_to_str(addr)};
        ownerService->bus.sendUnicast(
            std::make_shared<message::bluetooth::UnpairResult>(std::move(unpairedDevAddr), true),
            "ApplicationSettingsNew");
        return true;
    }

} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp +1 -0
@@ 50,6 50,7 @@ namespace bluetooth
        void stopScan();
        void setVisibility(bool visibility);
        auto pair(uint8_t *addr, std::uint8_t protectionLevel = 0) -> bool;
        auto unpair(uint8_t *addr) -> bool;
        static auto getDevicesList() -> const std::vector<Devicei> &;
        explicit GAP(sys::Service *owner);
    };

M module-bluetooth/tests/tests-StatefulController.cpp => module-bluetooth/tests/tests-StatefulController.cpp +4 -0
@@ 34,6 34,10 @@ class DriverMock : public AbstractDriver
    {
        return true;
    }
    bool unpair(uint8_t *addr) override
    {
        return true;
    }
    void registerErrorCallback(const ErrorCallback &) override
    {}


M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +9 -0
@@ 26,6 26,7 @@
#include <service-bluetooth/messages/SetDeviceName.hpp>
#include <BtCommand.hpp>
#include <BtKeysStorage.hpp>
#include <service-bluetooth/messages/Unpair.hpp>

ServiceBluetooth::ServiceBluetooth() : sys::Service(service::name::bluetooth, "", 4096)
{


@@ 110,6 111,14 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
        sendWorkerCommand(bluetooth::Command(bluetooth::Command::Type::Pair, addr));
        return sys::MessageNone{};
    });
    connect(typeid(message::bluetooth::Unpair), [&](sys::Message *msg) {
        auto unpairMsg        = static_cast<message::bluetooth::Unpair *>(msg);
        const auto addrString = unpairMsg->getAddr();
        bd_addr_t addr;
        sscanf_bd_addr(addrString.c_str(), addr);
        sendWorkerCommand(bluetooth::Command(bluetooth::Command::Type::Unpair, addr));
        return sys::MessageNone{};
    });

    connect(typeid(message::bluetooth::SetDeviceName), [&](sys::Message *msg) {
        auto setNameMsg = static_cast<message::bluetooth::SetDeviceName *>(msg);