~aleteoryx/muditaos

c51411c8768f02327ef61abfc3ab85ca8b8f428d — Pawel Olejniczak 4 years ago 8907669
[EGD-5861] Complete bluetooth connecting process on settings side

Enable connecting BT devices in settings.
Connect popup and add adjust messages for connection error handling.
M image/assets/lang/English.json => image/assets/lang/English.json +5 -0
@@ 25,6 25,8 @@
  "common_pause": "PAUSE",
  "common_retry": "TRY AGAIN",
  "common_abort": "ABORT",
  "common_connect": "CONNECT",
  "common_disconnect": "DISCONNECT",
  "common_mo": "MO",
  "common_tu": "TU",
  "common_we": "WE",


@@ 321,7 323,9 @@
  "app_settings_bluetooth_phone_name": "Phone name",
  "app_settings_bluetooth_phone_visibility": "Phone visibility",
  "app_settings_bluetooth_enter_passkey": "Enter passkey:",
  "app_settings_bluetooth_init_error_message": "<text font='gt_pressura' weight='regular' size='27'>Bluetooth initialization process \nhas failed. </text> <text font='gt_pressura' weight='bold' size='27'>TRY AGAIN.</text>",
  "app_settings_bluetooth_pairing_error_message": "<text font='gt_pressura' weight='regular' size='27'>Pairing process with</text> <text font='gt_pressura' weight='bold' size='27'>%NAME</text>\n<text font='gt_pressura' weight='regular' size='27'>has failed. Check the device\nand</text> <text font='gt_pressura' weight='bold' size='27'>TRY AGAIN.</text>",
  "app_settings_bluetooth_connecting_error_message": "<text font='gt_pressura' weight='regular' size='27'>Connection process has failed.\nCheck the device and</text> <text font='gt_pressura' weight='bold' size='27'>TRY AGAIN.</text>",
  "app_settings_net": "Network",
  "app_settings_disp_key": "Display and keypad",
  "app_settings_display_display_light": "Display light",


@@ 422,6 426,7 @@
  "app_settings_apn_apnprotocol": "APN Protocol",
  "app_settings_title_color_test": "Display available colors",
  "app_settings_toolbar_reset": "RESET",
  "app_settings_option_connected": "<text font='gt_pressura' weight='regular' size='27'>CONNECTED</text>",
  "app_phonebook_title_main": "Contacts",
  "app_phonebook_search_win_contacts": "Contacts",
  "common_search_uc": "Search",

M module-apps/application-settings-new/ApplicationSettings.cpp => module-apps/application-settings-new/ApplicationSettings.cpp +28 -2
@@ 44,6 44,7 @@
#include <service-bluetooth/Constants.hpp>
#include <service-bluetooth/messages/Status.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/messages/Connect.hpp>
#include <service-bluetooth/messages/DeviceName.hpp>
#include <service-bluetooth/messages/Passkey.hpp>
#include <service-bluetooth/messages/ResponseVisibleDevices.hpp>


@@ 149,8 150,8 @@ namespace app
        connect(typeid(::message::bluetooth::ResponseBondedDevices), [&](sys::Message *msg) {
            auto responseBondedDevicesMsg = static_cast<::message::bluetooth::ResponseBondedDevices *>(msg);
            if (gui::window::name::all_devices == getCurrentWindow()->getName()) {
                auto bondedDevicesData =
                    std::make_unique<gui::BondedDevicesData>(responseBondedDevicesMsg->getDevices());
                auto bondedDevicesData = std::make_unique<gui::BondedDevicesData>(
                    responseBondedDevicesMsg->getDevices(), responseBondedDevicesMsg->getAddressOfConnectedDevice());
                switchWindow(gui::window::name::all_devices, std::move(bondedDevicesData));
            }
            return sys::MessageNone{};


@@ 197,6 198,31 @@ namespace app
            return sys::MessageNone{};
        });

        connect(typeid(::message::bluetooth::ConnectResult), [&](sys::Message *msg) {
            auto connectResultMsg = static_cast<::message::bluetooth::ConnectResult *>(msg);
            if (connectResultMsg->isSucceed()) {
                bus.sendUnicast(std::make_shared<::message::bluetooth::RequestBondedDevices>(),
                                service::name::bluetooth);
                return sys::MessageNone{};
            }

            switchWindow(gui::window::name::dialog_retry,
                         gui::ShowMode::GUI_SHOW_INIT,
                         std::make_unique<gui::DialogMetadataMessage>(
                             gui::DialogMetadata{utils::localize.get("app_settings_bt"),
                                                 "search_big",
                                                 utils::localize.get("app_settings_bluetooth_connecting_error_message"),
                                                 "",
                                                 [=]() -> bool {
                                                     bus.sendUnicast(std::make_shared<message::bluetooth::Connect>(
                                                                         connectResultMsg->getAddr()),
                                                                     service::name::bluetooth);
                                                     return true;
                                                 }}));

            return sys::MessageNone{};
        });

        connect(typeid(CellularGetAPNResponse), [&](sys::Message *msg) {
            if (gui::window::name::apn_settings == getCurrentWindow()->getName()) {
                auto apns = dynamic_cast<CellularGetAPNResponse *>(msg);

M module-apps/application-settings-new/data/BondedDevicesData.hpp => module-apps/application-settings-new/data/BondedDevicesData.hpp +8 -2
@@ 11,14 11,20 @@ namespace gui
    class BondedDevicesData : public SwitchData
    {
      public:
        explicit BondedDevicesData(std::vector<Devicei> devices) : devices(std::move(devices))
        explicit BondedDevicesData(std::vector<Devicei> devices, std::string addressOfConnectedDevice)
            : devices(std::move(devices)), addressOfConnectedDevice(std::move(addressOfConnectedDevice))
        {}
        [[nodiscard]] auto getDevices() const noexcept -> const std::vector<Devicei> &
        {
            return devices;
        }
        [[nodiscard]] auto getAddressOfConnectedDevice() const noexcept -> const std::string &
        {
            return addressOfConnectedDevice;
        }

      private:
        const std::vector<Devicei> devices;
        const std::vector<Devicei> devices{};
        const std::string addressOfConnectedDevice;
    };
} // namespace gui

M module-apps/application-settings-new/models/BluetoothSettingsModel.cpp => module-apps/application-settings-new/models/BluetoothSettingsModel.cpp +11 -2
@@ 5,7 5,9 @@

#include <Constants.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/messages/Connect.hpp>
#include <service-bluetooth/messages/DeviceName.hpp>
#include <service-bluetooth/messages/Disconnect.hpp>
#include <service-bluetooth/messages/Status.hpp>
#include <service-bluetooth/messages/SetStatus.hpp>
#include <service-bluetooth/messages/SetDeviceName.hpp>


@@ 67,7 69,14 @@ void BluetoothSettingsModel::responsePasskey(std::string passkey)
    application->bus.sendUnicast(std::make_shared<message::bluetooth::ResponsePasskey>(std::move(passkey)),
                                 service::name::bluetooth);
}
void BluetoothSettingsModel::requestAudioConnection(std::string addr)

void BluetoothSettingsModel::requestConnection(std::string addr)
{
    application->bus.sendUnicast(std::make_shared<message::bluetooth::Connect>(std::move(addr)),
                                 service::name::bluetooth);
}

void BluetoothSettingsModel::requestDisconnection()
{
    application->bus.sendUnicast(std::make_shared<BluetoothAddrMessage>(std::move(addr)), service::name::bluetooth);
    application->bus.sendUnicast(std::make_shared<message::bluetooth::Disconnect>(), service::name::bluetooth);
}

M module-apps/application-settings-new/models/BluetoothSettingsModel.hpp => module-apps/application-settings-new/models/BluetoothSettingsModel.hpp +2 -1
@@ 21,10 21,11 @@ class BluetoothSettingsModel
    void setDeviceName(const UTF8 &deviceName);
    void requestBondedDevices();
    void requestScan();
    void requestAudioConnection(std::string addr);
    void stopScan();
    void requestDevicePairing(std::string addr);
    void responsePasskey(std::string passkey);
    void requestConnection(std::string addr);
    void requestDisconnection();

  private:
    app::Application *application = nullptr;

M module-apps/application-settings-new/windows/AddDeviceWindow.cpp => module-apps/application-settings-new/windows/AddDeviceWindow.cpp +1 -0
@@ 44,6 44,7 @@ namespace gui
                [=](gui::Item & /*unused*/) {
                    LOG_DEBUG("Device: %s", device.name.c_str());
                    bluetoothSettingsModel->requestDevicePairing(bd_addr_to_str(device.address));
                    application->switchWindow(gui::window::name::all_devices);
                    return true;
                },
                nullptr,

M module-apps/application-settings-new/windows/AllDevicesWindow.cpp => module-apps/application-settings-new/windows/AllDevicesWindow.cpp +16 -5
@@ 42,7 42,8 @@ namespace gui
    {
        const auto newData = dynamic_cast<BondedDevicesData *>(data);
        if (newData != nullptr) {
            devices = newData->getDevices();
            devices                  = newData->getDevices();
            addressOfConnectedDevice = newData->getAddressOfConnectedDevice();
        }
        refreshOptionsList();
    }


@@ 66,17 67,27 @@ namespace gui
    {
        std::list<gui::Option> optionsList;
        for (const auto &device : devices) {
            std::string addr{bd_addr_to_str(device.address)};
            auto isConnected = addr == addressOfConnectedDevice;
            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name,
                [=](gui::Item & /*item*/) {
                    LOG_DEBUG("Device: %s", device.name.c_str());
                    std::string addr{bd_addr_to_str(device.address)};
                    bluetoothSettingsModel->requestAudioConnection(std::move(addr));
                    bluetoothSettingsModel->requestConnection(addr);
                    return true;
                },
                [=](gui::Item &item) {
                    if (item.focus) {
                        this->setBottomBarText(isConnected ? utils::translateI18("common_disconnect")
                                                           : utils::translateI18("common_connect"),
                                               BottomBar::Side::CENTER);
                    }
                    return true;
                },
                nullptr,
                nullptr,
                gui::option::SettingRightItem::Bt));
                isConnected ? gui::option::SettingRightItem::Text : gui::option::SettingRightItem::Bt,
                false,
                utils::localize.get("app_settings_option_connected")));
        }
        return optionsList;
    }

M module-apps/application-settings-new/windows/AllDevicesWindow.hpp => module-apps/application-settings-new/windows/AllDevicesWindow.hpp +1 -0
@@ 23,6 23,7 @@ namespace gui
        Image *leftArrowImage = nullptr;
        Image *crossImage     = nullptr;
        std::vector<Devicei> devices;
        std::string addressOfConnectedDevice;
        std::unique_ptr<BluetoothSettingsModel> bluetoothSettingsModel;
    };


M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +23 -5
@@ 4,13 4,14 @@
#include "service-bluetooth/ServiceBluetooth.hpp"
#include "service-bluetooth/BluetoothMessage.hpp"


#include <Bluetooth/BluetoothWorker.hpp>
#include <interface/profiles/Profile.hpp>
#include <MessageType.hpp>
#include <Service/Service.hpp>
#include <Service/Message.hpp>
#include <service-db/Settings.hpp>
#include "service-bluetooth/messages/Connect.hpp"
#include "service-bluetooth/messages/Disconnect.hpp"
#include "service-bluetooth/messages/Status.hpp"
#include "service-bluetooth/messages/SetStatus.hpp"
#include <service-bluetooth/messages/BondedDevices.hpp>


@@ 28,8 29,8 @@

ServiceBluetooth::ServiceBluetooth() : sys::Service(service::name::bluetooth, "", 4096)
{
    auto settings  = std::make_unique<settings::Settings>(this);
    settingsHolder = std::make_shared<bluetooth::SettingsHolder>(std::move(settings));
    auto settings                   = std::make_unique<settings::Settings>(this);
    settingsHolder                  = std::make_shared<bluetooth::SettingsHolder>(std::move(settings));
    bluetooth::KeyStorage::settings = settingsHolder;
    LOG_INFO("[ServiceBluetooth] Initializing");
}


@@ 60,7 61,7 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
            std::visit(bluetooth::StringVisitor(), this->settingsHolder->getValue(bluetooth::Settings::BondedDevices));

        return std::make_shared<message::bluetooth::ResponseBondedDevices>(
            SettingsSerializer::fromString(bondedDevicesStr));
            SettingsSerializer::fromString(bondedDevicesStr), std::string());
    });

    connect(message::bluetooth::RequestStatus(), [&](sys::Message *msg) {


@@ 101,7 102,7 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
    });

    connect(typeid(BluetoothPairMessage), [&](sys::Message *msg) {
        auto pairMsg = static_cast<BluetoothPairMessage *>(msg);
        auto pairMsg          = static_cast<BluetoothPairMessage *>(msg);
        const auto addrString = pairMsg->addr;
        bd_addr_t addr;
        sscanf_bd_addr(addrString.c_str(), addr);


@@ 130,6 131,23 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
        return sys::MessageNone{};
    });

    connect(typeid(message::bluetooth::Connect), [&](sys::Message *msg) {
        auto connectMsg       = static_cast<message::bluetooth::Connect *>(msg);
        const auto addrString = connectMsg->getAddr();
        LOG_DEBUG("Connecting with %s", addrString.c_str());
        bd_addr_t addr;
        sscanf_bd_addr(addrString.c_str(), addr);
        sendWorkerCommand(bluetooth::Command(bluetooth::Command::Type::ConnectAudio, addr));
        bus.sendUnicast(std::make_shared<message::bluetooth::ConnectResult>(addrString, true),
                        service::name::bluetooth);
        return sys::MessageNone{};
    });

    connect(typeid(message::bluetooth::Disconnect), [&](sys::Message *msg) {
        sendWorkerCommand(bluetooth::Command(bluetooth::Command::Type::DisconnectAudio));
        return sys::MessageNone{};
    });

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

M module-services/service-bluetooth/service-bluetooth/messages/BondedDevices.hpp => module-services/service-bluetooth/service-bluetooth/messages/BondedDevices.hpp +7 -1
@@ 13,14 13,20 @@ namespace message::bluetooth
    class ResponseBondedDevices : public BluetoothMessage
    {
      public:
        explicit ResponseBondedDevices(std::vector<Devicei> devices) : devices(std::move(devices))
        explicit ResponseBondedDevices(std::vector<Devicei> devices, std::string addressOfConnectedDevice)
            : devices(std::move(devices)), addressOfConnectedDevice(std::move(addressOfConnectedDevice))
        {}
        [[nodiscard]] auto getDevices() const -> std::vector<Devicei>
        {
            return devices;
        }
        [[nodiscard]] auto getAddressOfConnectedDevice() const noexcept -> const std::string &
        {
            return addressOfConnectedDevice;
        }

      private:
        std::vector<Devicei> devices;
        std::string addressOfConnectedDevice;
    };
} // namespace message::bluetooth

A module-services/service-bluetooth/service-bluetooth/messages/Connect.hpp => module-services/service-bluetooth/service-bluetooth/messages/Connect.hpp +42 -0
@@ 0,0 1,42 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "service-bluetooth/BluetoothMessage.hpp"

namespace message::bluetooth
{
    class Connect : public BluetoothMessage
    {
      public:
        explicit Connect(std::string addr) : addr(std::move(addr))
        {}
        [[nodiscard]] auto getAddr() const -> std::string
        {
            return addr;
        }

      private:
        std::string addr;
    };

    class ConnectResult : public BluetoothMessage
    {
      public:
        explicit ConnectResult(std::string addr, bool succeed) : addr(std::move(addr)), succeed(succeed)
        {}
        [[nodiscard]] auto getAddr() const -> std::string
        {
            return addr;
        }
        [[nodiscard]] auto isSucceed() const -> bool
        {
            return succeed;
        }

      private:
        std::string addr;
        bool succeed;
    };
} // namespace message::bluetooth

A module-services/service-bluetooth/service-bluetooth/messages/Disconnect.hpp => module-services/service-bluetooth/service-bluetooth/messages/Disconnect.hpp +12 -0
@@ 0,0 1,12 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "service-bluetooth/BluetoothMessage.hpp"

namespace message::bluetooth
{
    class Disconnect : public BluetoothMessage
    {};
} // namespace message::bluetooth

M module-services/service-bluetooth/service-bluetooth/messages/Passkey.hpp => module-services/service-bluetooth/service-bluetooth/messages/Passkey.hpp +1 -1
@@ 15,7 15,7 @@ namespace message::bluetooth
      public:
        explicit ResponsePasskey(std::string passkey) : passkey(std::move(passkey))
        {}
        [[nodiscard]] auto getName() const -> std::string
        [[nodiscard]] auto getPasskey() const -> std::string
        {
            return passkey;
        }