~aleteoryx/muditaos

ref: 3cc3f50f7b9364ffac38349238cd6d9aa243dc23 muditaos/module-services/service-cellular/src/SMSSendHandler.cpp -rw-r--r-- 3.6 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SMSSendHandler.hpp"
#include <queries/messages/sms/QuerySMSSearchByType.hpp>
#include <queries/messages/sms/QuerySMSUpdate.hpp>

#if DEBUG_SERVICE_CELLULAR > 0
#define log_debug(format, ...) LOG_DEBUG(format, ##__VA_ARGS__)
#else
#define log_debug(...) (void)(0)
#endif

namespace cellular::internal::sms
{
    SMSSendHandler::SMSSendHandler() : currentState(std::make_unique<IdleState>(*this)), delayedMessage{false}
    {}

    void SMSSendHandler::handleStateChange(OptionalState state)
    {
        if (state) {
            log_debug("Switched from %s to %s", currentState->toStr().c_str(), state.value()->toStr().c_str());
            currentState = std::move(state.value());
        }
    }

    void SMSSendHandler::handleDBNotification()
    {
        handleStateChange(currentState->handle({}));
    }
    void SMSSendHandler::handleIncomingDbRecord(SMSRecord &record, bool onDelay)
    {
        if (!onDelay) {
            handleStateChange(currentState->handle(record));
        }
        else {
            delayedMessage = true;
            actionOnDelay  = [=]() -> void { handleStateChange(currentState->handle(record)); };
        }
    }

    void SMSSendHandler::sendMessageIfDelayed()
    {
        if (delayedMessage) {
            delayedMessage = false;
            actionOnDelay();
        }
    }

    void SMSSendHandler::handleNoMoreDbRecords()
    {
        handleStateChange(currentState->handle(SMSRecord{}));
    }
    void SMSSendHandler::requestNotSendMessage()
    {
        onSendQuery(db::Interface::Name::SMS, std::make_unique<db::query::SMSSearchByType>(SMSType::QUEUED, 0, 1));
    }
    bool State::isNotification(const std::optional<const SMSRecord> record)
    {
        return !record;
    }
    bool State::isRecordEmpty(const std::optional<const SMSRecord> record)
    {
        return record && !record->isValid();
    }

    OptionalState IdleState::handle(std::optional<SMSRecord> record)
    {
        if (!isNotification(record) || isRecordEmpty(record)) {
            return {};
        }

        context.onSendQuery(db::Interface::Name::SMS,
                            std::make_unique<db::query::SMSSearchByType>(SMSType::QUEUED, 0, 1));

        return std::make_unique<ProcessQueryState>(context);
    }

    OptionalState ProcessQueryState::handle(std::optional<SMSRecord> record)
    {
        if (isNotification(record)) {
            return {};
        }

        if (isRecordEmpty(record)) {
            return std::make_unique<IdleState>(context);
        }

        if (context.onGetModemResetInProgress()) {
            LOG_ERROR("Modem is rebooting, skipping SMS sending attempt");
            return {};
        }

        if (context.onGetOfflineMode() || context.onSIMNotInitialized()) {
            LOG_ERROR("Failed to send SMS");
            record->type = SMSType::FAILED;
            context.onSendQuery(db::Interface::Name::SMS, std::make_unique<db::query::SMSUpdate>(std::move(*record)));
            return std::make_unique<IdleState>(context);
        }

        context.onSend(*record);

        // Check if there are more queued records
        context.onSendQuery(db::Interface::Name::SMS,
                            std::make_unique<db::query::SMSSearchByType>(SMSType::QUEUED, 0, 1));
        return {};
    }

    std::string ProcessQueryState::toStr() const
    {
        return "ProcessQuery";
    }

    std::string IdleState::toStr() const
    {
        return "Idle";
    }
} // namespace cellular::internal::sms