~aleteoryx/muditaos

ref: 3cc3f50f7b9364ffac38349238cd6d9aa243dc23 muditaos/module-services/service-cellular/src/CSQHandler.cpp -rw-r--r-- 5.1 KiB
3cc3f50f — Maciej Gibowicz [BH-1809][BH-1835] Add date format setting 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
168
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CSQHandler.hpp"

#include <log/log.hpp>
#include <EventStore.hpp>
#include <ticks.hpp>
#include <chrono>

namespace
{
    constexpr auto urcThreshold     = 4;
    constexpr auto pollModeDuration = std::chrono::minutes{60};

    bool isRssiValid(std::uint32_t csq)
    {
        constexpr auto invalidRssiLow  = 99;
        constexpr auto invalidRssiHigh = 199;
        return ((csq != invalidRssiLow) && (csq != invalidRssiHigh));
    }
} // namespace

namespace cellular::service
{
    void CSQHandler::handleTimerTick()
    {
        if (currentMode == CSQMode::HybridPolling) {
            if (isPollModeTimeElapsed()) {
                LOG_INFO("CSQ poll mode timer elapsed");
                switchToHybridReportMode();
                return;
            }
            getCSQ();
        }
    }

    void CSQHandler::handleURCCounterMessage(std::uint32_t counter)
    {
        urcCounter = counter;
        if (isTooManyURCs() && (currentMode == CSQMode::HybridReporting)) {
            switchToHybridPollMode();
        }
    }

    auto CSQHandler::isTooManyURCs() -> bool
    {
        return (urcCounter > urcThreshold);
    }

    auto CSQHandler::isPollModeTimeElapsed() -> bool
    {
        const auto currentTime         = cpp_freertos::Ticks::TicksToMs(cpp_freertos::Ticks::GetTicks());
        const auto timeSpentInPollMode = utils::computeIncrease(currentTime, switchToPollModeTimestamp);
        return (timeSpentInPollMode >= std::chrono::duration_cast<std::chrono::milliseconds>(pollModeDuration).count());
    }

    void CSQHandler::checkConditionToChangeMode()
    {
        if (currentMode != CSQMode::PermanentReporting) {
            if (!isPhoneLocked || isBluetoothCarKitConnected ||
                (Store::Battery::get().state != Store::Battery::State::Discharging)) {
                switchToPermanentReportMode();
            }
        }
        else {
            if (isPhoneLocked && !isBluetoothCarKitConnected &&
                (Store::Battery::get().state == Store::Battery::State::Discharging)) {
                switchToHybridReportMode();
            }
        }
    }

    bool CSQHandler::switchToPermanentReportMode()
    {
        if ((onEnableCsqReporting != nullptr) && onEnableCsqReporting()) {
            currentMode = CSQMode::PermanentReporting;
            LOG_INFO("Switch to permanent report mode");
            return true;
        }

        LOG_ERROR("Failed to switch to CSQ permanent report mode, retrying");
        if (onRetrySwitchMode != nullptr) {
            onRetrySwitchMode(CSQMode::PermanentReporting);
        }
        return false;
    }

    bool CSQHandler::switchToHybridReportMode()
    {
        if ((onEnableCsqReporting != nullptr) && onEnableCsqReporting()) {
            currentMode = CSQMode::HybridReporting;
            LOG_INFO("Switching to hybrid report mode");
            return true;
        }

        LOG_ERROR("Failed to switch to CSQ hybrid report mode, retrying");
        if (onRetrySwitchMode != nullptr) {
            onRetrySwitchMode(CSQMode::HybridReporting);
        }
        return false;
    }

    bool CSQHandler::switchToHybridPollMode()
    {
        if ((onDisableCsqReporting != nullptr) && onDisableCsqReporting()) {
            currentMode               = CSQMode::HybridPolling;
            switchToPollModeTimestamp = cpp_freertos::Ticks::TicksToMs(cpp_freertos::Ticks::GetTicks());
            LOG_INFO("Too many signal strength updates, switching to hybrid poll mode");
            return true;
        }

        LOG_ERROR("Failed to switch to CSQ hybrid poll mode, retrying");
        if (onRetrySwitchMode != nullptr) {
            onRetrySwitchMode(CSQMode::HybridPolling);
        }
        return false;
    }

    bool CSQHandler::getCSQ()
    {
        if (onGetCsq != nullptr) {
            if (const auto &result = onGetCsq(); result.has_value()) {
                const auto &csq = result.value();
                if (isRssiValid(csq.csq)) {
                    LOG_INFO("Propagating valid CSQ");
                    if (onPropagateCSQ != nullptr) {
                        onPropagateCSQ(csq.csq);
                        return true;
                    }
                }
                else {
                    LOG_INFO("Invalid CSQ, notifying service antenna");
                    if (onInvalidCSQ != nullptr) {
                        onInvalidCSQ();
                        return true;
                    }
                }
            }
        }

        LOG_ERROR("Failed to get CSQ, retrying");
        if (onRetryGetCSQ != nullptr) {
            onRetryGetCSQ();
        }
        return false;
    }

    void CSQHandler::handleLockPhone()
    {
        isPhoneLocked = true;
    }

    void CSQHandler::handleUnlockPhone()
    {
        isPhoneLocked = false;
    }

    void CSQHandler::handleBluetoothCarKitConnect()
    {
        isBluetoothCarKitConnected = true;
    }

    void CSQHandler::handleBluetoothCarKitDisconnect()
    {
        isBluetoothCarKitConnected = false;
    }
} // namespace cellular::service