~aleteoryx/muditaos

58fe677527c3a66c027d3cb84ed7c2f4ab27e89e — Bartosz Cichocki 3 years ago 7a8a868
[MOS-264] Added network registration and roaming status to HFP

Connected HFP with cellular statuses
M module-bluetooth/Bluetooth/CommandHandler.cpp => module-bluetooth/Bluetooth/CommandHandler.cpp +2 -0
@@ 84,6 84,8 @@ namespace bluetooth
            return profileManager->setOperatorNameData(command.getData());
        case Command::BatteryLevelData:
            return profileManager->setBatteryLevelData(command.getData());
        case Command::NetworkStatusData:
            return profileManager->setNetworkStatusData(command.getData());
        case Command::StartStream:
            profileManager->start();
            return Error::Success;

M module-bluetooth/Bluetooth/command/Command.hpp => module-bluetooth/Bluetooth/command/Command.hpp +1 -0
@@ 34,6 34,7 @@ namespace bluetooth
            SignalStrengthData,
            OperatorNameData,
            BatteryLevelData,
            NetworkStatusData,
            None,
        };


M module-bluetooth/Bluetooth/command/CommandData.hpp => module-bluetooth/Bluetooth/command/CommandData.hpp +6 -2
@@ 12,8 12,12 @@

namespace bluetooth
{
    using DataVariant =
        std::variant<BatteryLevel, OperatorName, Store::SignalStrength, Devicei, utils::PhoneNumber::View>;
    using DataVariant = std::variant<BatteryLevel,
                                     OperatorName,
                                     Store::SignalStrength,
                                     Devicei,
                                     utils::PhoneNumber::View,
                                     Store::Network::Status>;

    class CommandData
    {

A module-bluetooth/Bluetooth/command/NetworkStatusData.cpp => module-bluetooth/Bluetooth/command/NetworkStatusData.cpp +14 -0
@@ 0,0 1,14 @@
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NetworkStatusData.hpp"

namespace bluetooth
{
    NetworkStatusData::NetworkStatusData(const Store::Network::Status &status) : status(status)
    {}
    auto NetworkStatusData::getData() -> DataVariant
    {
        return status;
    }
} // namespace bluetooth

A module-bluetooth/Bluetooth/command/NetworkStatusData.hpp => module-bluetooth/Bluetooth/command/NetworkStatusData.hpp +20 -0
@@ 0,0 1,20 @@
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include "CommandData.hpp"

namespace bluetooth
{

    class NetworkStatusData : public CommandData
    {
      public:
        explicit NetworkStatusData(const Store::Network::Status &status);
        auto getData() -> DataVariant override;

      private:
        Store::Network::Status status;
    };

} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/HFP/HFP.cpp => module-bluetooth/Bluetooth/interface/profiles/HFP/HFP.cpp +21 -0
@@ 114,6 114,16 @@ namespace bluetooth
        LOG_DEBUG("Setting battery level: %d", level.getBatteryLevel());
        return pimpl->setBatteryLevel(level);
    }
    auto HFP::setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code
    {
        LOG_DEBUG("Setting network registration status: %s", registered ? "online" : "offline");
        return pimpl->setNetworkRegistrationStatus(registered);
    }
    auto HFP::setRoamingStatus(bool enabled) const noexcept -> Error::Code
    {
        LOG_DEBUG("Setting roaming status: %s", enabled ? "enabled" : "disabled");
        return pimpl->setRoamingStatus(enabled);
    }

    HFP::~HFP() = default;



@@ 516,8 526,19 @@ namespace bluetooth
    auto HFP::HFPImpl::setBatteryLevel(const BatteryLevel &level) const noexcept -> Error::Code
    {
        auto result = hfp_ag_set_battery_level(level.getBatteryLevelBars());

        LOG_DEBUG("Battery level (bars): %d, set result: %d", level.getBatteryLevelBars(), result);
        return Error::Success;
    }
    auto HFP::HFPImpl::setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code
    {
        hfp_ag_set_registration_status(registered);
        return Error::Success;
    }
    auto HFP::HFPImpl::setRoamingStatus(bool enabled) const noexcept -> Error::Code
    {
        hfp_ag_set_roaming_status(enabled);
        return Error::Success;
    }

} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/HFP/HFP.hpp => module-bluetooth/Bluetooth/interface/profiles/HFP/HFP.hpp +2 -0
@@ 50,6 50,8 @@ namespace bluetooth
        [[nodiscard]] auto setSignalStrength(int bars) const noexcept -> Error::Code override;
        [[nodiscard]] auto setOperatorName(const std::string_view &name) const noexcept -> Error::Code override;
        [[nodiscard]] auto setBatteryLevel(const BatteryLevel &level) const noexcept -> Error::Code override;
        [[nodiscard]] auto setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code override;
        auto setRoamingStatus(bool enabled) const noexcept -> Error::Code override;

        void setAudioDevice(std::shared_ptr<bluetooth::BluetoothAudioDevice> audioDevice) override;


M module-bluetooth/Bluetooth/interface/profiles/HFP/HFPImpl.hpp => module-bluetooth/Bluetooth/interface/profiles/HFP/HFPImpl.hpp +2 -0
@@ 32,6 32,8 @@ namespace bluetooth
        [[nodiscard]] auto setSignalStrength(int bars) const noexcept -> Error::Code;
        [[nodiscard]] auto setOperatorName(const std::string_view &name) const noexcept -> Error::Code;
        [[nodiscard]] auto setBatteryLevel(const BatteryLevel &level) const noexcept -> Error::Code;
        [[nodiscard]] auto setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code;
        [[nodiscard]] auto setRoamingStatus(bool enabled) const noexcept -> Error::Code;

      private:
        static void sendAudioEvent(audio::EventType event, audio::Event::DeviceState state);

M module-bluetooth/Bluetooth/interface/profiles/HSP/HSP.cpp => module-bluetooth/Bluetooth/interface/profiles/HSP/HSP.cpp +8 -0
@@ 375,6 375,14 @@ namespace bluetooth
    {
        return pimpl->terminateCall();
    }
    auto HSP::setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code
    {
        return Error::Success;
    }
    auto HSP::setRoamingStatus(bool enabled) const noexcept -> Error::Code
    {
        return Error::Success;
    }

    void HSP::HSPImpl::setAudioDevice(std::shared_ptr<bluetooth::BluetoothAudioDevice> audioDevice)
    {

M module-bluetooth/Bluetooth/interface/profiles/HSP/HSP.hpp => module-bluetooth/Bluetooth/interface/profiles/HSP/HSP.hpp +4 -0
@@ 49,6 49,10 @@ namespace bluetooth
        [[nodiscard]] auto setOperatorName(const std::string_view &name) const noexcept -> Error::Code override;
        /// @return Success - ignoring in HSP
        [[nodiscard]] auto setBatteryLevel(const BatteryLevel &level) const noexcept -> Error::Code override;
        /// @return Success - ignoring in HSP
        [[nodiscard]] auto setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code override;
        /// @return Success - ignoring in HSP
        auto setRoamingStatus(bool enabled) const noexcept -> Error::Code override;

        void setAudioDevice(std::shared_ptr<bluetooth::BluetoothAudioDevice> audioDevice) override;


M module-bluetooth/Bluetooth/interface/profiles/Profile.hpp => module-bluetooth/Bluetooth/interface/profiles/Profile.hpp +6 -0
@@ 68,6 68,12 @@ namespace bluetooth
        /// Sets the operator name in HFP profile
        /// @return Error code that determines, whether operation was successful or not
        [[nodiscard]] virtual auto setBatteryLevel(const BatteryLevel &level) const noexcept -> Error::Code = 0;
        /// Sets the network registration status in HFP profile
        /// @return Error code that determines, whether operation was successful or not
        [[nodiscard]] virtual auto setNetworkRegistrationStatus(bool registered) const noexcept -> Error::Code = 0;
        /// Sets the roaming status in HFP profile
        /// @return Error code that determines, whether operation was successful or not
        virtual auto setRoamingStatus(bool enabled) const noexcept -> Error::Code = 0;
    };

} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/ProfileManager.cpp => module-bluetooth/Bluetooth/interface/profiles/ProfileManager.cpp +17 -0
@@ 141,5 141,22 @@ namespace bluetooth
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::setNetworkStatusData(const DataVariant &data) -> Error::Code
    {
        auto status = std::get<Store::Network::Status>(data);
        if (callProfilePtr) {
            switch (status) {
            case Store::Network::Status::RegisteredRoaming:
                callProfilePtr->setRoamingStatus(true);
                [[fallthrough]];
            case Store::Network::Status::RegisteredHomeNetwork:
                return callProfilePtr->setNetworkRegistrationStatus(true);
            default:
                return callProfilePtr->setNetworkRegistrationStatus(false);
            }
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }

} // namespace bluetooth

M module-bluetooth/Bluetooth/interface/profiles/ProfileManager.hpp => module-bluetooth/Bluetooth/interface/profiles/ProfileManager.hpp +2 -0
@@ 45,6 45,8 @@ namespace bluetooth
        auto setSignalStrengthData(const DataVariant &data) -> Error::Code;
        auto setOperatorNameData(const DataVariant &data) -> Error::Code;
        auto setBatteryLevelData(const DataVariant &data) -> Error::Code;
        auto setNetworkStatusData(const DataVariant &data) -> Error::Code;

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

      private:

M module-bluetooth/CMakeLists.txt => module-bluetooth/CMakeLists.txt +1 -0
@@ 37,6 37,7 @@ set(SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/command/SignalStrengthData.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/command/OperatorNameData.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/command/BatteryLevelData.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/command/NetworkStatusData.cpp

        )


M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +14 -3
@@ 46,6 46,7 @@
#include <command/SignalStrengthData.hpp>
#include <command/OperatorNameData.hpp>
#include <command/BatteryLevelData.hpp>
#include <command/NetworkStatusData.hpp>
#include <service-evtmgr/BatteryMessages.hpp>

namespace


@@ 121,6 122,7 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
    connectHandler<cellular::CallEndedNotification>();
    connectHandler<CellularSignalStrengthUpdateNotification>();
    connectHandler<CellularCurrentOperatorNameNotification>();
    connectHandler<CellularNetworkStatusUpdateNotification>();
    connectHandler<sevm::BatteryStatusChangeMessage>();

    settingsHolder->onStateChange = [this]() {


@@ 522,7 524,7 @@ auto ServiceBluetooth::handle(CellularCallActiveNotification *msg) -> std::share
auto ServiceBluetooth::handle(CellularSignalStrengthUpdateNotification *msg) -> std::shared_ptr<sys::Message>
{
    auto signalStrength = Store::GSM::get()->getSignalStrength();
    LOG_INFO("Bluetooth: RSSI %d/5", static_cast<int>(signalStrength.rssiBar));
    LOG_DEBUG("Bluetooth: RSSI %d/5", static_cast<int>(signalStrength.rssiBar));
    auto commandData = std::make_unique<bluetooth::SignalStrengthData>(signalStrength);
    sendWorkerCommand(bluetooth::Command::Type::SignalStrengthData, std::move(commandData));
    return std::make_shared<sys::ResponseMessage>();


@@ 531,7 533,7 @@ auto ServiceBluetooth::handle(CellularSignalStrengthUpdateNotification *msg) -> 
auto ServiceBluetooth::handle(CellularCurrentOperatorNameNotification *msg) -> std::shared_ptr<sys::Message>
{
    auto opName = msg->getCurrentOperatorName();
    LOG_INFO("Bluetooth: Operator name: %s", opName.c_str());
    LOG_DEBUG("Bluetooth: Operator name: %s", opName.c_str());
    auto commandData = std::make_unique<bluetooth::OperatorNameData>(bluetooth::OperatorName(opName));
    sendWorkerCommand(bluetooth::Command::Type::OperatorNameData, std::move(commandData));
    return std::make_shared<sys::ResponseMessage>();


@@ 573,13 575,14 @@ auto ServiceBluetooth::handle(message::bluetooth::RequestStatusIndicatorData *ms
    // just to execute proper handle method and sending it back to worker
    bus.sendUnicast(std::make_shared<CellularSignalStrengthUpdateNotification>(), service::name::bluetooth);
    bus.sendUnicast(std::make_shared<sevm::BatteryStatusChangeMessage>(), service::name::bluetooth);
    bus.sendUnicast(std::make_shared<CellularNetworkStatusUpdateNotification>(), service::name::bluetooth);

    return sys::MessageNone{};
}
auto ServiceBluetooth::handle(sevm::BatteryStatusChangeMessage *msg) -> std::shared_ptr<sys::Message>
{
    auto batteryLevel = Store::Battery::get().level;
    LOG_INFO("Bluetooth: Battery level %d", batteryLevel);
    LOG_DEBUG("Bluetooth: Battery level %d", batteryLevel);
    auto commandData = std::make_unique<bluetooth::BatteryLevelData>(bluetooth::BatteryLevel(batteryLevel));
    sendWorkerCommand(bluetooth::Command::Type::BatteryLevelData, std::move(commandData));
    return sys::MessageNone{};


@@ 589,3 592,11 @@ auto ServiceBluetooth::handle(cellular::CallEndedNotification *msg) -> std::shar
    sendWorkerCommand(bluetooth::Command::Type::CallTerminated);
    return sys::MessageNone{};
}
auto ServiceBluetooth::handle(CellularNetworkStatusUpdateNotification *msg) -> std::shared_ptr<sys::Message>
{
    auto status = Store::GSM::get()->getNetwork().status;
    LOG_DEBUG("Bluetooth: Network status %s", magic_enum::enum_name(status).data());
    auto commandData = std::make_unique<bluetooth::NetworkStatusData>(status);
    sendWorkerCommand(bluetooth::Command::Type::NetworkStatusData, std::move(commandData));
    return sys::MessageNone{};
}

M module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp => module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +2 -0
@@ 67,6 67,7 @@ class CellularCallerIdMessage;
class CellularCallActiveNotification;
class CellularSignalStrengthUpdateNotification;
class CellularCurrentOperatorNameNotification;
class CellularNetworkStatusUpdateNotification;
namespace cellular
{
    class CallEndedNotification;


@@ 136,6 137,7 @@ class ServiceBluetooth : public sys::Service
    [[nodiscard]] auto handle(cellular::CallEndedNotification *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(CellularSignalStrengthUpdateNotification *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(CellularCurrentOperatorNameNotification *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(CellularNetworkStatusUpdateNotification *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sevm::BatteryStatusChangeMessage *msg) -> std::shared_ptr<sys::Message>;
};