~aleteoryx/muditaos

1b2dbc813c0a18e441bc5ca85241bd555939b828 — Jakub Pyszczak 4 years ago 9daf2a9
[EGD-7931] Fixed bt pairing

Fixed bluetooth pairing for devices with long names.
M module-apps/application-settings/windows/bluetooth/AddDeviceWindow.cpp => module-apps/application-settings/windows/bluetooth/AddDeviceWindow.cpp +1 -1
@@ 39,7 39,7 @@ namespace gui

        for (auto &device : devices) {
            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name,
                device.name.data(),
                [&](gui::Item & /*unused*/) {
                    bluetoothSettingsModel->requestDevicePair(device);
                    application->switchWindow(gui::window::name::all_devices);

M module-apps/application-settings/windows/bluetooth/AllDevicesWindow.cpp => module-apps/application-settings/windows/bluetooth/AllDevicesWindow.cpp +1 -1
@@ 91,7 91,7 @@ namespace gui
            UTF8 textOnRight                   = getTextOnRight(device.deviceState);

            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name,
                device.name.data(),
                [=](gui::Item & /*item*/) { return handleDeviceAction(device); },
                [=](gui::Item &item) {
                    if (item.focus) {

M module-bluetooth/Bluetooth/Device.hpp => module-bluetooth/Bluetooth/Device.hpp +13 -5
@@ 9,13 9,21 @@
#include <string>
#include <utility>

#include <stdexcept>

struct Device
{
  public:
    explicit Device(std::string name = "") : name(std::move(name))
    {}
    static constexpr auto NameBufferSize = 240;
    explicit Device(std::string name = "")
    {
        if (name.size() > NameBufferSize) {
            throw std::runtime_error("Requested name is bigger than buffer size");
        }
        strcpy(this->name.data(), name.c_str());
    }
    virtual ~Device() = default;
    std::string name;
    std::array<char, NameBufferSize> name;
};

enum DEVICE_STATE


@@ 107,12 115,12 @@ struct Devicei : public Device

    inline bool operator==(const Devicei &cmpDevice) const
    {
        return (cmpDevice.name == name) && (bd_addr_cmp(cmpDevice.address, address) == 0);
        return (strcmp(cmpDevice.name.data(), name.data()) == 0) && (bd_addr_cmp(cmpDevice.address, address) == 0);
    }

    inline bool operator!=(const Devicei &cmpDevice) const
    {
        return (cmpDevice.name != name) || (bd_addr_cmp(cmpDevice.address, address) != 0);
        return (strcmp(cmpDevice.name.data(), name.data()) != 0) || (bd_addr_cmp(cmpDevice.address, address) != 0);
    }

    auto address_str() const -> const char *

M module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.cpp => module-bluetooth/Bluetooth/interface/BluetoothDriverImpl.cpp +1 -1
@@ 215,7 215,7 @@ namespace bluetooth
    }
    auto Driver::pair(Devicei device, std::uint8_t protectionLevel) -> bool
    {
        LOG_INFO("Device: %s, addr: %s", device.name.c_str(), device.address_str());
        LOG_INFO("Device: %s, addr: %s", device.name.data(), device.address_str());
        return gap->pair(device, protectionLevel);
    }
    auto Driver::unpair(Devicei device) -> bool

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.cpp +14 -8
@@ 58,13 58,13 @@ namespace bluetooth
        LOG_INFO("Visibility: %s", visibility ? "true" : "false");
    }

    auto GAP::pair(const Devicei &device, std::uint8_t protectionLevel) -> bool
    auto GAP::pair(Devicei device, std::uint8_t protectionLevel) -> bool
    {
        if (hci_get_state() == HCI_STATE_WORKING) {
            auto devIndex            = getDeviceIndexForAddress(devices, device.address);
            currentlyProccesedDevice = devices.at(devIndex);

            return gap_dedicated_bonding(const_cast<uint8_t *>(device.address), protectionLevel) == 0;
            return gap_dedicated_bonding(device.address, protectionLevel) == 0;
        }
        return false;
    }


@@ 130,7 130,7 @@ namespace bluetooth
        if (index >= 0) {
            if (packet[2] == 0) {
                devices[index].state = REMOTE_NAME_FETCHED;
                devices[index].name  = std::string{reinterpret_cast<const char *>(&packet[9])};
                strcpy(devices[index].name.data(), reinterpret_cast<const char *>(&packet[9]));
                return true;
            }
            else {


@@ 156,13 156,19 @@ namespace bluetooth
            LOG_INFO(", rssi %d dBm", static_cast<int8_t>(gap_event_inquiry_result_get_rssi(packet)));
        }
        if (gap_event_inquiry_result_get_name_available(packet) != 0u) {
            auto name    = gap_event_inquiry_result_get_name(packet);
            device.name  = std::string{reinterpret_cast<const char *>(name)};
            if (const auto nameLen = gap_event_inquiry_result_get_name_len(packet); nameLen > Device::NameBufferSize) {
                LOG_ERROR("Can't add new device - name length is too large.");
                return;
            }
            auto name = gap_event_inquiry_result_get_name(packet);
            strcpy(device.name.data(), reinterpret_cast<const char *>(name));
            device.state = REMOTE_NAME_FETCHED;
        }
        else {
            bd_addr_t devAddr;
            gap_event_inquiry_result_get_bd_addr(packet, devAddr);
            device.state = REMOTE_NAME_REQUEST;
            device.name  = std::string{};
            strcpy(device.name.data(), bd_addr_to_str(devAddr));
        }

        devices.emplace_back(std::move(device));


@@ 284,10 290,10 @@ namespace bluetooth
    {
        return devices;
    }
    auto GAP::unpair(const Devicei &device) -> bool
    auto GAP::unpair(Devicei device) -> bool
    {
        LOG_INFO("Unpairing device");
        gap_drop_link_key_for_bd_addr(const_cast<uint8_t *>(device.address));
        gap_drop_link_key_for_bd_addr(device.address);

        LOG_INFO("Device unpaired");
        ownerService->bus.sendMulticast(std::make_shared<message::bluetooth::UnpairResult>(device, true),

M module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp => module-bluetooth/Bluetooth/interface/profiles/GAP/GAP.hpp +2 -2
@@ 50,8 50,8 @@ namespace bluetooth
        auto scan() -> Error;
        void stopScan();
        void setVisibility(bool visibility);
        auto pair(const Devicei &device, std::uint8_t protectionLevel = 0) -> bool;
        auto unpair(const Devicei &device) -> bool;
        auto pair(Devicei device, std::uint8_t protectionLevel = 0) -> bool;
        auto unpair(Devicei device) -> bool;
        static auto getDevicesList() -> const std::vector<Devicei> &;
        static void respondPinCode(const std::string &pin);


M module-bluetooth/tests/tests-BluetoothDevicesModel.cpp => module-bluetooth/tests/tests-BluetoothDevicesModel.cpp +1 -1
@@ 119,7 119,7 @@ TEST_CASE("Device handling")

    SECTION("Get device with correct address")
    {
        REQUIRE(devicesModel.getDeviceByAddress(addr2Str).value().get().name == "Dev2");
        REQUIRE(strcmp(devicesModel.getDeviceByAddress(addr2Str).value().get().name.data(), "Dev2") == 0);
    }

    SECTION("Remove device from the list")

M module-services/service-bluetooth/service-bluetooth/BluetoothDevicesModel.cpp => module-services/service-bluetooth/service-bluetooth/BluetoothDevicesModel.cpp +13 -7
@@ 77,15 77,21 @@ void BluetoothDevicesModel::setInternalDeviceState(const Devicei &device, const 
}
void BluetoothDevicesModel::mergeInternalDeviceState(const Devicei &device)
{
    auto deviceInModel = getDeviceByAddress(device.address).value().get();
    try {
        auto deviceInModel = getDeviceByAddress(device.address).value().get();

    if ((deviceInModel.deviceState == DeviceState::ConnectedVoice &&
         device.deviceState == DeviceState::ConnectedAudio) ||
        (device.deviceState == DeviceState::ConnectedVoice &&
         deviceInModel.deviceState == DeviceState::ConnectedAudio)) {
        setInternalDeviceState(device, DeviceState::ConnectedBoth);
        if ((deviceInModel.deviceState == DeviceState::ConnectedVoice &&
             device.deviceState == DeviceState::ConnectedAudio) ||
            (device.deviceState == DeviceState::ConnectedVoice &&
             deviceInModel.deviceState == DeviceState::ConnectedAudio)) {
            setInternalDeviceState(device, DeviceState::ConnectedBoth);
        }
        else {
            setInternalDeviceState(device, device.deviceState);
        }
    }
    else {
    catch (std::bad_optional_access &e) {
        LOG_ERROR("Can't get device by address! Unable to update device state. %s", e.what());
        setInternalDeviceState(device, device.deviceState);
    }
}

M module-services/service-bluetooth/service-bluetooth/SettingsSerializer.cpp => module-services/service-bluetooth/service-bluetooth/SettingsSerializer.cpp +3 -3
@@ 16,8 16,8 @@ auto SettingsSerializer::toString(const std::vector<Devicei> &devices) -> std::s
{
    json11::Json::array devicesJson;
    for (auto &device : devices) {
        auto deviceEntry =
            json11::Json::object{{strings::addr, bd_addr_to_str(device.address)}, {strings::name, device.name}};
        auto deviceEntry = json11::Json::object{{strings::addr, bd_addr_to_str(device.address)},
                                                {strings::name, std::string{device.name.data()}}};
        devicesJson.emplace_back(deviceEntry);
    }
    json11::Json finalJson = json11::Json::object{{strings::devices, devicesJson}};


@@ 40,7 40,7 @@ auto SettingsSerializer::fromString(const std::string &jsonStr) -> std::vector<D
    for (auto &device : devicesArray) {
        Devicei temp;
        sscanf_bd_addr(device[strings::addr].string_value().c_str(), temp.address);
        temp.name        = device[strings::name].string_value();
        strcpy(temp.name.data(), device[strings::name].string_value().c_str());
        temp.deviceState = DeviceState::Paired;
        devicesVector.emplace_back(temp);
    }

M module-services/service-desktop/endpoints/bluetooth/BluetoothMessagesHandler.cpp => module-services/service-desktop/endpoints/bluetooth/BluetoothMessagesHandler.cpp +1 -1
@@ 27,7 27,7 @@ namespace
{
    json11::Json::object device2json(const Devicei &device)
    {
        LOG_DEBUG("Device: name=%s, address=%s", device.name.c_str(), bd_addr_to_str(device.address));
        LOG_DEBUG("Device: name=%s, address=%s", device.name.data(), bd_addr_to_str(device.address));
        return json11::Json::object{{btConstants::devicesValues::address, std::string(bd_addr_to_str(device.address))},
                                    {btConstants::devicesValues::name, device.name}};
    }