~aleteoryx/muditaos

ref: 674a7683645c7263ee7ca632f86f6a8c8b4a5846 muditaos/module-bsp/board/linux/hal/battery_charger/BatteryCharger.cpp -rw-r--r-- 5.3 KiB
674a7683 — Lefucjusz [MOS-669] Add too hot battery notification when charging 2 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-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <hal/battery_charger/AbstractBatteryCharger.hpp>

#include <magic_enum.hpp>

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

namespace hal::battery
{
    namespace
    {
        constexpr auto batteryFIFO          = "/tmp/fifoBattKeys";
        constexpr auto fifoFileAccessRights = 0666;
        constexpr auto fifoBuffSize         = 10;
        constexpr auto queueTimeoutTicks    = 100;
        constexpr auto taskDelay            = 50;

        constexpr auto chargerPlugStateChange = 'p';
        constexpr auto batteryLevelUp         = ']';
        constexpr auto batteryLevelDown       = '[';
        constexpr auto batteryVoltageUp       = '=';
        constexpr auto batteryVoltageDown     = '-';
        constexpr auto chargerTypeDcdSDP      = 'l';
        constexpr auto chargerTypeDcdCDP      = ';';
        constexpr auto chargerTypeDcdDCP      = '\'';

    } // namespace

    class BatteryCharger : public AbstractBatteryCharger
    {
      public:
        explicit BatteryCharger(xQueueHandle irqQueueHandle);
        ~BatteryCharger();

        std::optional<Voltage> getBatteryVoltage() const final;
        std::optional<SOC> getSOC() const final;
        ChargingStatus getChargingStatus() const final;
        ChargerPresence getChargerPresence() const final;
        TemperatureState getTemperatureState() const final;

      private:
        void worker();

        xQueueHandle notificationChannel = nullptr;
        TaskHandle_t batteryWorkerHandle = nullptr;
        unsigned batteryLevel            = 100;
        unsigned batteryVoltageLevel     = 3700;
        bool isPlugged                   = false;
        bool shouldRun                   = true;
    };

    BatteryCharger::BatteryCharger(xQueueHandle irqQueueHandle)
    {
        notificationChannel = irqQueueHandle;

        xTaskCreate(
            [](void *pvp) {
                BatteryCharger *inst = static_cast<BatteryCharger *>(pvp);
                inst->worker();
            },
            "battery",
            512,
            this,
            0,
            &batteryWorkerHandle);
    }

    BatteryCharger::~BatteryCharger()
    {
        shouldRun = false;
        /// The worker operates in taskDelay intervals. Give it at least taskDelay to handle the close procedure.
        vTaskDelay(taskDelay * 2);
    }

    std::optional<AbstractBatteryCharger::Voltage> BatteryCharger::getBatteryVoltage() const
    {
        return batteryVoltageLevel;
    }

    std::optional<AbstractBatteryCharger::SOC> BatteryCharger::getSOC() const
    {
        return batteryLevel;
    }

    AbstractBatteryCharger::ChargingStatus BatteryCharger::getChargingStatus() const
    {
        if (isPlugged && batteryLevel >= 100) {
            return ChargingStatus::ChargingDone;
        }
        else if (isPlugged && batteryLevel < 100) {
            return ChargingStatus::Charging;
        }
        else {
            return ChargingStatus::Discharging;
        }
    }

    AbstractBatteryCharger::ChargerPresence BatteryCharger::getChargerPresence() const
    {
        return isPlugged ? AbstractBatteryCharger::ChargerPresence::PluggedIn
                         : AbstractBatteryCharger::ChargerPresence::Unplugged;
    }

    AbstractBatteryCharger::TemperatureState BatteryCharger::getTemperatureState() const
    {
        return AbstractBatteryCharger::TemperatureState::Normal;
    }

    void BatteryCharger::worker()
    {
        mkfifo(batteryFIFO, fifoFileAccessRights);

        // Open FIFO for write only
        const auto fd = open(batteryFIFO, O_RDONLY | O_NONBLOCK);

        while (shouldRun) {
            std::uint8_t buff[fifoBuffSize];
            const auto readBytes = read(fd, buff, fifoBuffSize);

            if (readBytes > 0) {
                Events evt{};
                switch (static_cast<char>(buff[0])) {
                case chargerPlugStateChange:
                    isPlugged = !isPlugged;
                    evt       = Events::Charger;
                    break;
                case batteryLevelUp:
                    batteryLevel++;
                    evt = Events::SOC;
                    break;
                case batteryLevelDown:
                    batteryLevel--;
                    evt = Events::SOC;
                    break;
                case batteryVoltageUp:
                    batteryVoltageLevel += 10;
                    evt = Events::SOC;
                    break;
                case batteryVoltageDown:
                    batteryVoltageLevel -= 10;
                    evt = Events::SOC;
                    break;
                case chargerTypeDcdSDP:
                case chargerTypeDcdCDP:
                case chargerTypeDcdDCP:
                    evt = Events::Charger;
                    break;
                default:
                    continue;
                }
                xQueueSend(notificationChannel, &evt, queueTimeoutTicks);
            }
            vTaskDelay(taskDelay);
        }
        close(fd);
        vTaskDelete(nullptr);
    }

    std::unique_ptr<AbstractBatteryCharger> AbstractBatteryCharger::Factory::create(xQueueHandle irqQueueHandle)
    {
        return std::make_unique<BatteryCharger>(irqQueueHandle);
    }
} // namespace hal::battery