~aleteoryx/muditaos

ref: 1da915ace94b631914c768efcb878e0a6393195e muditaos/module-services/service-cellular/CellularUrcHandler.cpp -rw-r--r-- 6.0 KiB
1da915ac — Wojtek Rzepecki [BH-777] Add Bell specific ld script configuration 4 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CellularUrcHandler.hpp"

#include "service-cellular/CellularMessage.hpp"
#include "messages.hpp"
#include "service-cellular/CellularServiceAPI.hpp"

#include <service-antenna/AntennaServiceAPI.hpp>
#include <service-evtmgr/Constants.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-time/Constants.hpp>

using namespace at::urc;


void CellularUrcHandler::Handle(Clip &urc)
{
    LOG_TRACE("incoming call...");
    std::string phoneNumber;
    if (urc.isValid()) {
        phoneNumber = urc.getNumber();
    }
    response = std::make_unique<CellularCallerIdNotification>(phoneNumber);
    urc.setHandled(true);
}

void CellularUrcHandler::Handle(Ring &urc)
{
    response = std::make_unique<CellularRingNotification>("");
    urc.setHandled(true);
}

void CellularUrcHandler::Handle(Creg &urc)
{
    if (urc.isValid()) {
        auto accessTechnology = urc.getAccessTechnology();
        auto status           = urc.getStatus();

        LOG_INFO("Network status - %s, access technology %s",
                 utils::enumToString(status).c_str(),
                 utils::enumToString(accessTechnology).c_str());

        CellularServiceAPI::GetCurrentOperator(&cellularService);

        Store::Network network{status, accessTechnology};
        Store::GSM::get()->setNetwork(network);
        response = std::make_unique<CellularNetworkStatusUpdateNotification>();
        urc.setHandled(true);
    }
    else {
        LOG_WARN("Network status - not valid");
    }
}

void CellularUrcHandler::Handle(Cmti &urc)
{
    LOG_TRACE("received new SMS notification");
    if (urc.isValid()) {
        response = std::make_unique<CellularNewIncomingSMSNotification>(urc.getIndex());
        urc.setHandled(true);
    }
    else {
        LOG_ERROR("Could not parse Cmti message");
    }
}

void CellularUrcHandler::Handle(Cusd &urc)
{
    response     = std::nullopt;
    auto message = urc.getMessage();
    if (!message) {
        return;
    }

    if (urc.isActionNeeded()) {
        if (cellularService.ussdState == ussd::State::pullRequestSent) {
            cellularService.ussdState = ussd::State::pullResponseReceived;
            cellularService.setUSSDTimer();
            auto msg = std::make_shared<CellularMMIResponseMessage>(*message);
            cellularService.bus.sendUnicast(msg, app::manager::ApplicationManager::ServiceName);
        }
    }
    else {
        CellularServiceAPI::USSDRequest(&cellularService, CellularUSSDMessage::RequestType::abortSesion);
        cellularService.ussdState = ussd::State::sesionAborted;
        cellularService.setUSSDTimer();
        auto msg = std::make_shared<CellularMMIPushMessage>(*message);
        cellularService.bus.sendUnicast(msg, app::manager::ApplicationManager::ServiceName);
    }

    urc.setHandled(true);
}

void CellularUrcHandler::Handle(Ctze &urc)
{
    if (!urc.isValid()) {
        return;
    }

        auto msg = std::make_shared<CellularTimeNotificationMessage>(
            urc.getGMTTime(), urc.getTimeZoneOffset(), urc.getTimeZoneString());
        cellularService.bus.sendUnicast(msg, service::name::service_time);

    urc.setHandled(true);
}

void CellularUrcHandler::Handle(Qind &urc)
{
    if (urc.isCsq()) {
        // Received signal strength change
        AntennaServiceAPI::CSQChange(&cellularService);
        auto rssi = urc.getRSSI();
        if (!rssi) {
            LOG_INFO("Invalid csq - ignore");
        }
        else {
            SignalStrength signalStrength(*rssi);

            Store::GSM::get()->setSignalStrength(signalStrength.data);
            response = std::make_unique<CellularSignalStrengthUpdateNotification>(urc.getUrcBody());
        }
        auto ber = urc.getBER();
        if (ber.has_value()) {
            LOG_INFO("BER value: %d", ber.value());
        }
        urc.setHandled(true);
    }
    else if (urc.isFota()) {
        std::string httpSuccess = "0";
        if (urc.getFotaStage() == Qind::FotaStage::HTTPEND && urc.getFotaParameter() == httpSuccess) {
            LOG_DEBUG("Fota UPDATE, switching to AT mode");
            cellularService.cmux->setMode(CellularMux::Mode::AT);
            urc.setHandled(true);
        }
    }
    else if (urc.isSmsDone()) {
        response = std::make_unique<CellularSmsDoneNotification>();

        urc.setHandled(true);
    }
}

void CellularUrcHandler::Handle(Cpin &urc)
{
    if (urc.isValid()) {
        auto state = urc.getState();
        if (!state) {
            LOG_INFO("Invalid cpin - ignore");
        }
        else {
            response = std::make_unique<cellular::internal::msg::HandleATSimStateChange>(*state);
            urc.setHandled(true);
        }
    }
}

void CellularUrcHandler::Handle(Qiurc &urc)
{
    auto urcType = urc.getType();
    if (urc.isValid() && urcType) {
        switch (*urcType) {
        case Qiurc::QIUrcMessages::DeactivateContext:
            if (auto urcFirstParam = urc.getFirstParam(); urcFirstParam) {
                int ctxid = 0;
                if (utils::toNumeric(*urcFirstParam, ctxid)) {
                    response = std::make_unique<CellularDeactivateContextResponse>(at::Result::Code::OK, ctxid);
                    urc.setHandled(true);
                }
            }
            break;
        }
    }
}

void CellularUrcHandler::Handle(PoweredDown &urc)
{
    if (urc.isValid()) {
        response = std::make_unique<CellularPowerDownDeregisteringNotification>();
        urc.setHandled(true);
    }
}

void CellularUrcHandler::Handle(UrcResponse &urc)
{
    std::vector<UrcResponse::URCResponseType> typesToHandle = {
        UrcResponse::URCResponseType::NoCarrier,
        UrcResponse::URCResponseType::Busy,
        UrcResponse::URCResponseType::NoAnswer,
    };

    for (auto &t : typesToHandle) {
        if (t == urc.getURCResponseType()) {
            LOG_TRACE("call aborted");
            response = std::make_unique<CellularCallAbortedNotification>();
            urc.setHandled(true);
        }
    }
}