~aleteoryx/muditaos

ref: 32f7786e4bc48e6bdc5539d0c3dece8f58b62649 muditaos/module-services/service-cellular/src/CSQHandler.cpp -rw-r--r-- 4.9 KiB
32f7786e — Lefucjusz Revert "[BH-1694] Increase CPU core voltage from 900mV to 975mV" 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
// 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 cellular::service
{

    void CSQHandler::handleTimerTick()
    {
        if (currentMode == CSQMode::HybridPolling) {
            if (isPollModeTimeElapsed()) {
                LOG_INFO("CSQ poll mode time elapsed.");
                switchToHybridReportMode();
                return;
            }

            getCSQ();
        }
    }

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

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

    auto CSQHandler::isPollModeTimeElapsed() -> bool
    {
        auto currentTime = cpp_freertos::Ticks::TicksToMs(cpp_freertos::Ticks::GetTicks());
        auto timeSpentInPollMode =
            currentTime >= switchToPollModeTimestamp
                ? currentTime - switchToPollModeTimestamp
                : std::numeric_limits<TickType_t>::max() - switchToPollModeTimestamp + currentTime;
        return timeSpentInPollMode > std::chrono::duration_cast<std::chrono::milliseconds>(pollTime).count();
    }

    void CSQHandler::checkConditionToChangeMode()
    {
        if (currentMode != CSQMode::PermanentReporting) {
            if (not isPhoneLocked || isBluetoothCarKitConnected ||
                Store::Battery::get().state != Store::Battery::State::Discharging) {
                switchToPermanentReportMode();
            }
        }
        else {
            if (isPhoneLocked && not 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! Retry!");
        if (onRetrySwitchMode != nullptr) {
            onRetrySwitchMode(CSQMode::PermanentReporting);
        }
        return false;
    }

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

        LOG_ERROR("Failed to switch to CSQ hybrid report mode! Retry!");
        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, switch to hybrid poll mode.");
            return true;
        }

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

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

        LOG_ERROR("Failed to get CSQ! Retry!");
        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