~aleteoryx/muditaos

ref: 9daf2a9d6a3f7d05155f7c1bf2d9c3c1681171d2 muditaos/module-services/service-evtmgr/battery-level-check/BatteryLevelCheck.cpp -rw-r--r-- 6.5 KiB
9daf2a9d — Przemyslaw Brudny Merge remote-tracking branch 'origin/stable' 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BatteryLevelCheck.hpp"
#include "service-evtmgr/BatteryMessages.hpp"
#include "system/Constants.hpp"
#include <service-db/agents/settings/SystemSettings.hpp>
#include <EventStore.hpp>
#include <Utils.hpp>

#define BOOST_SML_CFG_DISABLE_MIN_SIZE // GCC10 fix
#include <boost/sml.hpp>

namespace battery_level_check
{
    namespace
    {
        unsigned int batteryLevelCritical = 10;

        constexpr auto batteryShutdownLevel = 5;

        sys::Service *parentService = nullptr;

        std::shared_ptr<settings::Settings> settings = nullptr;

        namespace sml = boost::sml;

        // events
        struct initialLevelCheck
        {};
        struct criticalLevelCheck
        {};

        // guards
        struct isNormal
        {
            bool operator()() const
            {
                return Store::Battery::get().level >= batteryLevelCritical;
            }
        } isNormal;
        struct isCriticalNotCharging
        {
            bool operator()() const
            {
                return Store::Battery::get().level < batteryLevelCritical &&
                       Store::Battery::get().state != Store::Battery::State::Charging &&
                       Store::Battery::get().state != Store::Battery::State::ChargingDone;
            }
        } isCriticalNotCharging;
        struct isCriticalCharging
        {
            bool operator()() const
            {
                return Store::Battery::get().level < batteryLevelCritical &&
                       (Store::Battery::get().state == Store::Battery::State::Charging ||
                        Store::Battery::get().state == Store::Battery::State::ChargingDone);
            }
        } isCriticalCharging;
        struct isShutdown
        {
            bool operator()() const
            {
                return Store::Battery::get().level < batteryShutdownLevel;
            }
        } isShutdown;

        // actions
        struct setCriticalNotCharging
        {
            void operator()()
            {
                Store::Battery::modify().levelState = Store::Battery::LevelState::CriticalNotCharging;
            }
        } setCriticalNotCharging;
        struct setCriticalCharging
        {
            void operator()()
            {
                Store::Battery::modify().levelState = Store::Battery::LevelState::CriticalCharging;
            }
        } setCriticalCharging;
        struct setNormal
        {
            void operator()()
            {
                Store::Battery::modify().levelState = Store::Battery::LevelState::Normal;
            }
        } setNormal;
        struct setShutdown
        {
            void operator()()
            {
                Store::Battery::modify().levelState = Store::Battery::LevelState::Shutdown;
            }
        } setShutdown;

        struct sendStateChange
        {
            void operator()()
            {
                auto stateChangeMessage = std::make_shared<sevm::BatteryStateChangeMessage>();
                parentService->bus.sendUnicast(std::move(stateChangeMessage), service::name::system_manager);
            }
        } sendStateChange;

        struct StateMachine
        {
            auto operator()() const
            {
                using namespace sml;
                // clang-format off
                return make_transition_table(
                *"InitialCheck"_s + event<initialLevelCheck> [ isCriticalNotCharging && !isShutdown ] / setCriticalNotCharging = "LevelCriticalNotCharging"_s,
                "InitialCheck"_s + event<initialLevelCheck> [ isCriticalNotCharging && isShutdown ] / setShutdown = "Shutdown"_s,
                "InitialCheck"_s + event<initialLevelCheck> [ isCriticalCharging ] / setCriticalCharging = "LevelCriticalCharging"_s,
                "InitialCheck"_s + event<initialLevelCheck> [ isNormal ]  / setNormal = "LevelNormal"_s,
                "LevelNormal"_s + event<criticalLevelCheck> [ isCriticalNotCharging && !isShutdown ] / (setCriticalNotCharging, sendStateChange) = "LevelCriticalNotCharging"_s,
                "LevelNormal"_s + event<criticalLevelCheck> [ isCriticalNotCharging && isShutdown ] / (setShutdown, sendStateChange) = "Shutdown"_s,
                "LevelNormal"_s + event<criticalLevelCheck> [ isCriticalCharging ] / (setCriticalCharging, sendStateChange) = "LevelCriticalCharging"_s,
                "LevelCriticalNotCharging"_s + event<criticalLevelCheck> [ isNormal ] / (setNormal, sendStateChange) = "LevelNormal"_s,
                "LevelCriticalNotCharging"_s + event<criticalLevelCheck> [ isCriticalCharging ] / (setCriticalCharging, sendStateChange) = "LevelCriticalCharging"_s,
                "LevelCriticalNotCharging"_s + event<criticalLevelCheck> [ isShutdown ] / (setShutdown, sendStateChange) = "Shutdown"_s,
                "LevelCriticalCharging"_s + event<criticalLevelCheck> [ isCriticalNotCharging && !isShutdown ] / (setCriticalNotCharging, sendStateChange) = "LevelCriticalNotCharging"_s,
                "LevelCriticalCharging"_s + event<criticalLevelCheck> [ isNormal ] / (setNormal, sendStateChange) = "LevelNormal"_s,
                "LevelCriticalCharging"_s + event<criticalLevelCheck> [ isCriticalNotCharging && isShutdown ] / (setShutdown, sendStateChange) = "Shutdown"_s
                );
                // clang-format on
            }
        };

        std::unique_ptr<sml::sm<StateMachine>> sm;
    } // namespace

    void init(sys::Service *service, std::shared_ptr<settings::Settings> &setts)
    {
        sm            = std::make_unique<sml::sm<StateMachine>>();
        parentService = service;
        settings      = setts;
        settings->registerValueChange(
            settings::Battery::batteryCriticalLevel,
            [&](const std::string &value) { batteryLevelCritical = utils::getNumericValue<unsigned int>(value); },
            settings::SettingsScope::Global);
        sm->process_event(initialLevelCheck{});
    }

    void deinit()
    {
        settings->unregisterValueChange(settings::Battery::batteryCriticalLevel, settings::SettingsScope::Global);
        settings.reset();
        sm.reset();
    }

    void checkBatteryLevel()
    {
        sm->process_event(criticalLevelCheck{});
    }

    void setBatteryCriticalLevel(unsigned int level)
    {
        batteryLevelCritical = level;
        settings->setValue(
            settings::Battery::batteryCriticalLevel, utils::to_string(level), settings::SettingsScope::Global);
        checkBatteryLevel();
    }

} // namespace battery_level_check