~aleteoryx/muditaos

ref: master muditaos/module-services/service-evtmgr/battery/BatteryState.cpp -rw-r--r-- 5.0 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "BatteryState.hpp"
#include <boost/sml.hpp>
#include <utility>

namespace
{
    namespace sml = boost::sml;
    using State   = BatteryState::ChargingState;

    namespace events
    {
        struct Check
        {
            const float soc;
            const BatteryState::ChargingState state;
            const float criticalThreshold;
            const float shutdownThreshold;
        };
    } // namespace events

    // guards
    struct isNormal
    {
        bool operator()(const events::Check &e) const
        {
            return e.soc >= e.criticalThreshold;
        }
    } isNormal;

    struct isCriticalNotCharging
    {
        bool operator()(const events::Check &e) const
        {
            return e.soc < e.criticalThreshold && e.state != State::Charging && e.state != State::ChargingDone;
        }
    } isCriticalNotCharging;

    struct isCriticalCharging
    {
        bool operator()(const events::Check &e) const
        {
            return e.soc < e.criticalThreshold && (e.state == State::Charging || e.state == State::ChargingDone);
        }
    } isCriticalCharging;

    struct isShutdown
    {
        bool operator()(const events::Check &e) const
        {
            return e.soc < e.shutdownThreshold;
        }
    } isShutdown;

    // actions
    struct setCriticalNotCharging
    {
        void operator()(BatteryState::NotifyStateChangedCallback &notifier)
        {
            notifier(BatteryState::State::CriticalNotCharging);
        }
    } setCriticalNotCharging;

    struct setCriticalCharging
    {
        void operator()(BatteryState::NotifyStateChangedCallback &notifier)
        {
            notifier(BatteryState::State::CriticalCharging);
        }
    } setCriticalCharging;

    struct setNormal
    {
        void operator()(BatteryState::NotifyStateChangedCallback &notifier)
        {
            notifier(BatteryState::State::Normal);
        }
    } setNormal;

    struct setShutdown
    {
        void operator()(BatteryState::NotifyStateChangedCallback &notifier)
        {
            notifier(BatteryState::State::Shutdown);
        }
    } setShutdown;

    struct StateMachine
    {
        auto operator()() const
        {
            using namespace sml;
            // clang-format off
                return make_transition_table(
                *"InitialCheck"_s + event<events::Check> [ isCriticalNotCharging && !isShutdown ] = "LevelCriticalNotCharging"_s,
                "InitialCheck"_s + event<events::Check> [ isCriticalNotCharging && isShutdown ] = "Shutdown"_s,
                "InitialCheck"_s + event<events::Check> [ isCriticalCharging ] = "LevelCriticalCharging"_s,
                "InitialCheck"_s + event<events::Check> [ isNormal ] = "LevelNormal"_s,

                "LevelNormal"_s + sml::on_entry<_> / setNormal,
                "LevelNormal"_s + event<events::Check> [ isCriticalNotCharging && !isShutdown ] = "LevelCriticalNotCharging"_s,
                "LevelNormal"_s + event<events::Check> [ isCriticalNotCharging && isShutdown ] = "Shutdown"_s,
                "LevelNormal"_s + event<events::Check> [ isCriticalCharging ]  = "LevelCriticalCharging"_s,

                "LevelCriticalNotCharging"_s + sml::on_entry<_> / setCriticalNotCharging,
                "LevelCriticalNotCharging"_s + event<events::Check> [ isNormal ]  = "LevelNormal"_s,
                "LevelCriticalNotCharging"_s + event<events::Check> [ isCriticalCharging ]  = "LevelCriticalCharging"_s,
                "LevelCriticalNotCharging"_s + event<events::Check> [ isShutdown ]  = "Shutdown"_s,

                "LevelCriticalCharging"_s + sml::on_entry<_> / setCriticalCharging,
                "LevelCriticalCharging"_s + event<events::Check> [ isCriticalNotCharging && !isShutdown ]  = "LevelCriticalNotCharging"_s,
                "LevelCriticalCharging"_s + event<events::Check> [ isNormal ]  = "LevelNormal"_s,
                "LevelCriticalCharging"_s + event<events::Check> [ isCriticalNotCharging && isShutdown ]  = "Shutdown"_s,

                "Shutdown"_s + sml::on_entry<_> / setShutdown
                );
            // clang-format on
        }
    };

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

class BatteryState::Pimpl
{
    friend BatteryState;

  public:
    Pimpl(NotifyStateChangedCallback notifyCallback, Thresholds thresholds)
        : thresholds(thresholds), notifyCallback{std::move(notifyCallback)}
    {}

  private:
    const Thresholds thresholds;
    NotifyStateChangedCallback notifyCallback;
    sml::sm<StateMachine, NotifyStateChangedCallback> sm{notifyCallback};
};

void BatteryState::check(const ChargingState state, const float soc)
{
    pimpl->sm.process_event(events::Check{soc, state, pimpl->thresholds.critical, pimpl->thresholds.shutdown});
}

BatteryState::BatteryState(sys::Service *service, NotifyStateChangedCallback notifyCallback, Thresholds thresholds)
    : pimpl{std::make_shared<Pimpl>(std::move(notifyCallback), thresholds)}
{}