~aleteoryx/muditaos

12aff015de2fed6d569b75aaee3d3e4a2bf2c24a — Bartosz Cichocki 4 years ago d731a85
[EGD-7874] Fix no disconnect after unpair

When user tried to unpair connected device, the device was not
disconnecting. Now it's fixed
M module-bluetooth/Bluetooth/BluetoothWorker.cpp => module-bluetooth/Bluetooth/BluetoothWorker.cpp +14 -0
@@ 157,6 157,7 @@ auto BluetoothWorker::handleCommand(QueueHandle_t queue) -> bool
    case bluetooth::Command::Unpair:
        controller->processCommand(command);
        removeFromBoundDevices(command.getDevice().address);
        handleUnpairDisconnect(command.getDevice());
        break;
    case bluetooth::Command::None:
        break;


@@ 284,3 285,16 @@ void BluetoothWorker::setAudioDevice(std::shared_ptr<bluetooth::BluetoothAudioDe
    cpp_freertos::LockGuard lock(loopMutex);
    profileManager->setAudioDevice(std::move(device));
}
auto BluetoothWorker::isAddressConnected(const uint8_t *addr) -> bool
{
    auto deviceAddr =
        std::visit(bluetooth::StringVisitor(), this->settings->getValue(bluetooth::Settings::ConnectedDevice));
    return static_cast<bool>(deviceAddr == bd_addr_to_str(addr));
}
void BluetoothWorker::handleUnpairDisconnect(const Devicei &device)
{
    if (isAddressConnected(device.address)) {
        auto disconnectCmd = bluetooth::Command(bluetooth::Command::DisconnectAudio, device);
        controller->processCommand(disconnectCmd);
    }
}

M module-bluetooth/Bluetooth/BluetoothWorker.hpp => module-bluetooth/Bluetooth/BluetoothWorker.hpp +2 -0
@@ 80,6 80,8 @@ class BluetoothWorker : private sys::Worker
    void onLinkKeyAdded(const std::string &deviceAddress);
    void initDevicesList();
    void removeFromBoundDevices(uint8_t *addr);
    auto isAddressConnected(const bd_addr_t addr) -> bool;
    void handleUnpairDisconnect(const Devicei &device);

  public:
    enum Error

M module-bluetooth/Bluetooth/CommandHandler.cpp => module-bluetooth/Bluetooth/CommandHandler.cpp +0 -3
@@ 152,9 152,6 @@ namespace bluetooth
    Error::Code CommandHandler::unpair(const Devicei &device)
    {
        LOG_INFO("Unpairing...");
        if (profileManager->isAddressActuallyUsed(device.address)) {
            profileManager->disconnect();
        }
        const auto error_code = driver->unpair(device) ? Error::Success : Error::LibraryError;
        LOG_INFO("Unpairing result: %s", magic_enum::enum_name(error_code).data());
        return error_code;

M module-bluetooth/Bluetooth/interface/profiles/ProfileManager.cpp => module-bluetooth/Bluetooth/interface/profiles/ProfileManager.cpp +0 -5
@@ 47,7 47,6 @@ namespace bluetooth
                ptr->connect();
            }
        }

        return Error::Success;
    }



@@ 116,10 115,6 @@ namespace bluetooth
        profilesList[profileType]->setAudioDevice(device);
        return switchProfile(profileType);
    }
    auto ProfileManager::isAddressActuallyUsed(const bd_addr_t address) -> bool
    {
        return !static_cast<bool>(bd_addr_cmp(address, remoteAddr));
    }
    auto ProfileManager::callAnswered() -> Error::Code
    {
        return currentProfilePtr->callAnswered();

M module-bluetooth/Bluetooth/interface/profiles/ProfileManager.hpp => module-bluetooth/Bluetooth/interface/profiles/ProfileManager.hpp +0 -3
@@ 45,15 45,12 @@ namespace bluetooth
        auto initializeCall() -> Error::Code;
        auto callAnswered() -> Error::Code;
        auto setIncomingCallNumber(const std::string &num) -> Error::Code;
        auto isAddressActuallyUsed(const bd_addr_t address) -> bool;

        auto setAudioDevice(std::shared_ptr<BluetoothAudioDevice> device) -> Error::Code;

      private:
        sys::Service *ownerService;
        ProfileList profilesList;
        bluetooth::Profile *currentProfilePtr = nullptr;
        bd_addr_t remoteAddr{};
        bool initialized = false;
    };
} // namespace bluetooth

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +0 -2
@@ 294,8 294,6 @@ auto ServiceBluetooth::handle(message::bluetooth::ConnectResult *msg) -> std::sh
{
    if (msg->isSucceed()) {
        auto device        = msg->getDevice();
        auto deviceInModel = bluetoothDevicesModel->getDeviceByAddress(device.address)->get();

        bluetoothDevicesModel->mergeInternalDeviceState(device);

        settingsHolder->setValue(bluetooth::Settings::ConnectedDevice, bd_addr_to_str(device.address));