~aleteoryx/muditaos

ref: 32f7786e4bc48e6bdc5539d0c3dece8f58b62649 muditaos/module-services/service-cellular/src/CSQHandler.hpp -rw-r--r-- 1.8 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <at/cmd/CSQ.hpp>

#include <cstdint>
#include <chrono>
#include <functional>
#include <FreeRTOS.h>

namespace cellular::service
{

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

    static const auto invalid_rssi_low  = 99;
    static const auto invalid_rssi_high = 199;

    enum class CSQMode
    {
        PermanentReporting,
        HybridReporting,
        HybridPolling
    };

    class CSQHandler
    {
      public:
        void handleTimerTick();
        void handleURCCounterMessage(const uint32_t counter);

        std::function<bool()> onEnableCsqReporting;
        std::function<bool()> onDisableCsqReporting;
        std::function<std::optional<at::result::CSQ>()> onGetCsq;
        std::function<void(uint32_t)> onPropagateCSQ;
        std::function<void()> onInvalidCSQ;

        std::function<void(CSQMode)> onRetrySwitchMode;
        std::function<void()> onRetryGetCSQ;

        void handleLockPhone();
        void handleUnlockPhone();
        void handleBluetoothCarKitConnect();
        void handleBluetoothCarKitDisconnect();
        void checkConditionToChangeMode();
        bool switchToPermanentReportMode();
        bool switchToHybridReportMode();
        bool switchToHybridPollMode();
        bool getCSQ();

      private:
        uint32_t urcCounter = 0;
        CSQMode currentMode = CSQMode::HybridReporting;
        TickType_t switchToPollModeTimestamp{0};

        // parameters needed to switch poll/report mode
        bool isPhoneLocked{true};
        bool isBluetoothCarKitConnected{false};

        auto isPollModeTimeElapsed() -> bool;
        auto isTooManyURC() -> bool;
    };
} // namespace cellular::service