~aleteoryx/muditaos

edc7fd63cb0c677709ffb2f5cf8aac089aa0dada — SP2FET 5 years ago d1b2d4c
[EGD-4579] Add bluetooth settings storage

Add settings storage to bluetooth related items
M changelog.md => changelog.md +1 -0
@@ 5,6 5,7 @@
### Added

* `[PowerManagement]` Critial battery level notification to SystemManager.
* `[Bluetooth]`  Add settings storage to bluetooth related items

## [0.51.1 2020-12-18]


M module-services/service-bluetooth/CMakeLists.txt => module-services/service-bluetooth/CMakeLists.txt +1 -0
@@ 4,6 4,7 @@ message( "${PROJECT_NAME}  ${CMAKE_CURRENT_LIST_DIR}" )

set(SOURCES
    ServiceBluetooth.cpp
    service-bluetooth/SettingsHolder.cpp
)

add_library(${PROJECT_NAME} STATIC ${SOURCES})

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +3 -34
@@ 12,7 12,6 @@
#include <MessageType.hpp>
#include <Service/Service.hpp>
#include <Service/Message.hpp>
#include <agents/settings/SystemSettings.hpp>
#include <service-db/Settings.hpp>

#include <log/log.hpp>


@@ 20,9 19,10 @@
#include <bits/exception.h>
#include <utility>

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



@@ 37,14 37,6 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
{
    LOG_ERROR("Bluetooth experimental!");
    worker = std::make_unique<BluetoothWorker>(this);
    settingsProvider->registerValueChange(settings::Bluetooth::state,
                                          [this](std::string value) { stateSettingChanged(value); });
    settingsProvider->registerValueChange(settings::Bluetooth::deviceVisibility,
                                          [this](std::string value) { deviceVisibilitySettingChanged(value); });
    settingsProvider->registerValueChange(settings::Bluetooth::deviceName,
                                          [this](std::string value) { deviceNameSettingChanged(value); });
    settingsProvider->registerValueChange(settings::Bluetooth::bondedDevices,
                                          [this](std::string value) { bondedDevicesSettingChanged(value); });

    return sys::ReturnCodes::Success;
}


@@ 135,26 127,3 @@ sys::ReturnCodes ServiceBluetooth::SwitchPowerModeHandler(const sys::ServicePowe
    return sys::ReturnCodes::Success;
}

void ServiceBluetooth::stateSettingChanged(std::string value)
{
    LOG_DEBUG("Received new bt_state: %s", value.c_str());
    settingsProvider->unregisterValueChange(settings::Bluetooth::state);
}

void ServiceBluetooth::deviceVisibilitySettingChanged(std::string value)
{
    LOG_DEBUG("Received new bt_device_visibility: %s", value.c_str());
    settingsProvider->unregisterValueChange(settings::Bluetooth::deviceVisibility);
}

void ServiceBluetooth::deviceNameSettingChanged(std::string value)
{
    LOG_DEBUG("Received new bt_device_name: %s", value.c_str());
    settingsProvider->unregisterValueChange(settings::Bluetooth::deviceName);
}

void ServiceBluetooth::bondedDevicesSettingChanged(std::string value)
{
    LOG_DEBUG("Received new bt_bonded_devices: %s", value.c_str());
    settingsProvider->unregisterValueChange(settings::Bluetooth::bondedDevices);
}

M module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp => module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +2 -6
@@ 7,6 7,7 @@
#include <Service/Common.hpp>
#include <Service/Message.hpp>
#include <Service/Service.hpp>
#include "service-bluetooth/SettingsHolder.hpp"

#include <memory> // for unique_ptr



@@ 30,10 31,5 @@ class ServiceBluetooth : public sys::Service

  private:
    std::unique_ptr<BluetoothWorker> worker;
    std::unique_ptr<settings::Settings> settingsProvider;

    void stateSettingChanged(std::string value);
    void deviceVisibilitySettingChanged(std::string value);
    void deviceNameSettingChanged(std::string value);
    void bondedDevicesSettingChanged(std::string value);
    std::unique_ptr<Bluetooth::SettingsHolder> settingsHolder;
};

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

#include "SettingsHolder.hpp"
#include <log/log.hpp>
namespace Bluetooth
{
    std::map<Settings, std::string> SettingsHolder::settingString{
        {Settings::DeviceName, settings::Bluetooth::deviceName},
        {Settings::Visibility, settings::Bluetooth::deviceVisibility},
        {Settings::Power, settings::Bluetooth::state},
        {Settings::BondedDevices, settings::Bluetooth::bondedDevices},
    };

    auto SettingsHolder::getValue(const Settings setting) -> SettingEntry
    {
        return settingsMap[setting];
    }
    void SettingsHolder::setValue(const Settings &newSetting, const SettingEntry &value)
    {
        settingsMap[newSetting] = value;

        settingsProvider->setValue(settingString[newSetting], std::visit(StringVisitor(), value));
    }
    SettingsHolder::SettingsHolder(std::unique_ptr<settings::Settings> settingsPtr)
        : settingsProvider(std::move(settingsPtr))
    {
        settingsProvider->registerValueChange(settings::Bluetooth::state, [this](std::string value) {
            setValue(Settings::Power, value);
            this->settingsProvider->unregisterValueChange(settings::Bluetooth::state);
        });
        settingsProvider->registerValueChange(settings::Bluetooth::deviceVisibility, [this](std::string value) {
            setValue(Settings::Visibility, value);
            this->settingsProvider->unregisterValueChange(settings::Bluetooth::deviceVisibility);
        });
        settingsProvider->registerValueChange(settings::Bluetooth::deviceName, [this](std::string value) {
            setValue(Settings::DeviceName, value);
            this->settingsProvider->unregisterValueChange(settings::Bluetooth::deviceName);
        });
        settingsProvider->registerValueChange(settings::Bluetooth::bondedDevices, [this](std::string value) {
            setValue(Settings::BondedDevices, value);
            this->settingsProvider->unregisterValueChange(settings::Bluetooth::bondedDevices);
        });
    }
} // namespace Bluetooth

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

#include <json/json11.hpp>
#include <string>
#include <variant>
#include <service-db/Settings.hpp>
#include <agents/settings/SystemSettings.hpp>
#include <module-utils/Utils.hpp>

namespace Bluetooth
{

    enum class Settings
    {
        DeviceName,
        Visibility,
        Power,
        BondedDevices
    };

    constexpr inline auto trueStr  = "true";
    constexpr inline auto falseStr = "false";

    using SettingEntry = std::variant<int, bool, std::string>;
    struct StringVisitor
    {
        auto operator()(int input) const -> std::string
        {
            return utils::to_string(input);
        }
        auto operator()(bool input) const -> std::string
        {
            return input ? trueStr : falseStr;
        }
        auto operator()(const std::string &input) const -> std::string
        {
            return input;
        }
    };

    class SettingsHolder
    {
      public:
        explicit SettingsHolder(std::unique_ptr<settings::Settings> settingsPtr);
        auto getValue(const Settings setting) -> SettingEntry;
        void setValue(const Settings &newSetting, const SettingEntry &value);

      private:
        static std::map<Settings, std::string> settingString;
        std::unique_ptr<settings::Settings> settingsProvider;
        std::map<Settings, SettingEntry> settingsMap;
    };
} // namespace Bluetooth