~aleteoryx/muditaos

ref: a5f957823daa836b22898fdc1527213f8f72f5d1 muditaos/module-services/service-cellular/PacketData.cpp -rw-r--r-- 12.0 KiB
a5f95782 — breichel [EGD-4628] Add APN settings and URC handler for PDP context 5 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PacketData.hpp"

#include <optional>
#include <algorithm>
#include <iterator>

#include <response.hpp>
#include <Utils.hpp>
namespace at
{
    namespace response
    {
        bool parseQICSGP(const at::Result &resp, std::shared_ptr<packet_data::APN::Config> retAPN)
        {

            /// AT+QICSGP=<contextID>[,<context_type>,<APN>[,<username>,<password>)[,<authentication>[,<cdma_pwd>]]]]
            /// +QICSGP: <context_type>,<APN>,<username>,<password>,<authentication>
            /// +QICSGP: 1,"","","",0
            constexpr auto AT_QICSGP = "+QICSGP:";
            if (auto tokens = at::response::getTokensForATCommand(resp, AT_QICSGP); tokens) {

                constexpr int QICSGP_TokensCount = 5; /// Could be depend ? on firmware version, assume that always have
                                                      /// <context_type>,<APN>,<username>,<password>,<authentication>
                if ((*tokens).size() == QICSGP_TokensCount) {

                    retAPN->contextType =
                        static_cast<packet_data::APN::ContextType>(utils::getNumericValue<unsigned int>((*tokens)[0]));
                    retAPN->apn = (*tokens)[1];
                    utils::findAndReplaceAll(retAPN->apn, at::response::StringDelimiter, "");
                    retAPN->username = (*tokens)[2];
                    utils::findAndReplaceAll(retAPN->username, at::response::StringDelimiter, "");
                    retAPN->password = (*tokens)[3];
                    utils::findAndReplaceAll(retAPN->password, at::response::StringDelimiter, "");
                    retAPN->authMethod =
                        static_cast<packet_data::APN::AuthMethod>(utils::getNumericValue<unsigned int>((*tokens)[4]));
                    return true;
                }
            }
            return false;
        }

        bool parseQIACT(const at::Result &resp, std::vector<std::shared_ptr<packet_data::APN::Config>> &ret)
        {

            /**
             * In case of AT+QIACT?
             * +QIACT:1,<context_state>,<context_type>[,<IP_address>]
             * [.....
             * +QIACT:16,<context_state>,<context_type>[,<IP_address>]]
             *
             * also could be empty list
             */
            constexpr int QIACT_TokensCount        = 3;
            constexpr int QIACT_WithIP_TokensCount = 4;

            constexpr auto AT_QIACT = "+QIACT:";
            if (auto mtokens = at::response::getTokensForATResults(resp, AT_QIACT); mtokens) {
                for (const auto &tokens : *mtokens) {

                    if (tokens.size() < QIACT_TokensCount) {
                        continue;
                    }
                    std::shared_ptr<packet_data::APN::Config> retAPN = std::make_shared<packet_data::APN::Config>();
                    retAPN->contextId                                = utils::getNumericValue<std::uint8_t>(tokens[0]);
                    retAPN->state =
                        static_cast<packet_data::APN::ContextState>(utils::getNumericValue<std::uint8_t>(tokens[1]));
                    retAPN->contextType =
                        static_cast<packet_data::APN::ContextType>(utils::getNumericValue<std::uint8_t>(tokens[2]));

                    if (tokens.size() >= QIACT_WithIP_TokensCount) {
                        retAPN->ip = tokens[3];
                        utils::findAndReplaceAll(retAPN->ip, at::response::StringDelimiter, "");
                    }
                    ret.push_back(retAPN);
                }
                return true; /// empty list is also good result
            }
            return false;
        }
    } // namespace response
    namespace query
    {
        std::string prepareQICSGP(std::shared_ptr<packet_data::APN::Config> apn, bool setEmpty)
        {
            /// AT+QICSGP=<contextID>[,<context_type>,<APN>[,<username>,<password>)[,<authentication>]]]

            if (setEmpty) {
                LOG_DEBUG("Set empty APN");
                return at::factory(at::AT::QICSGP) + "=" + utils::to_string(static_cast<int>(apn->contextId)) + "," +
                       "1,\"\",\"\",\"\",1";
            }

            return at::factory(at::AT::QICSGP) + "=" + utils::to_string(static_cast<int>(apn->contextId)) + "," +
                   utils::to_string(static_cast<int>(apn->contextType)) + ",\"" + apn->apn + "\",\"" + apn->username +
                   "\",\"" + apn->password + "\"," + utils::to_string(static_cast<int>(apn->authMethod));
        }

    } // namespace query
} // namespace at

namespace packet_data
{

    PDPContext::PDPContext(ServiceCellular &cellularService) : cellularService(cellularService)
    {
        channel = cellularService.cmux->get(TS0710::Channel::Commands);
    }

    std::shared_ptr<APN::Config> PDPContext::getConfiguration(std::uint8_t contextId)
    {
        if (channel) {
            auto resp = channel->cmd(at::factory(at::AT::QICSGP) + "=" + utils::to_string(static_cast<int>(contextId)));
            if (resp.code == at::Result::Code::OK) {
                std::shared_ptr<APN::Config> ret = std::make_shared<APN::Config>();
                ret->contextId                   = contextId;
                if (at::response::parseQICSGP(resp, ret)) {
                    return ret;
                }
            }
        }
        return nullptr;
    }

    at::Result::Code PDPContext::setConfiguration(std::shared_ptr<APN::Config> apn, bool setEmpty)
    {
        if (channel) {
            auto resp = channel->cmd(at::query::prepareQICSGP(apn, setEmpty));
            return resp.code;
        }
        return at::Result::Code::ERROR;
    }

    at::Result::Code PDPContext::activate(std::uint8_t contextId)
    {

        if (channel) {
            auto resp = channel->cmd(at::factory(at::AT::QIACT) + "=" + utils::to_string(static_cast<int>(contextId)));

            /**
             * From quectel documentation:
             * 1. Reboot the module if there is no response in 150s.
             * 2. If failed to deactivate the PDP context for 3 times continuously, then reboot the module.
             */
            return resp.code;
        }
        return at::Result::Code::ERROR;
    }
    at::Result::Code PDPContext::deactivate(std::uint8_t contextId)
    {
        if (channel) {
            /// this command could generate URC, deactivate
            auto resp =
                channel->cmd(at::factory(at::AT::QIDEACT) + "=" + utils::to_string(static_cast<int>(contextId)));

            return resp.code;
        }
        return at::Result::Code::ERROR;
    }

    std::optional<const std::vector<std::shared_ptr<APN::Config>>> PDPContext::getActive()
    {
        if (channel) {
            auto resp = channel->cmd(at::factory(at::AT::QIACT) + "?");

            if (resp.code == at::Result::Code::OK) {
                std::vector<std::shared_ptr<APN::Config>> ret;
                if (at::response::parseQIACT(resp, ret)) {
                    return ret;
                }
            }
        }
        return std::nullopt;
    }

    PacketData::PacketData(ServiceCellular &cellularService) : cellularService(cellularService){};

    void PacketData::loadAPNSettings()
    {

        std::shared_ptr<APN::Config> apnConfig = std::make_shared<APN::Config>();
        apnConfig->contextId                   = 1;
        apnConfig->apnType                     = APN::APNType::Default;
        apnConfig->apn                         = "internet";
        apnConfig->authMethod                  = APN::AuthMethod::NONE;

        contextMap[apnConfig->contextId] = apnConfig;

        LOG_ERROR("loadAPNSettings");
    }

    void PacketData::saveAPNSettings()
    {
        /// Save in phone memory
    }

    at::Result::Code PacketData::updateAPNSettings(std::uint8_t ctxId)
    {
        LOG_DEBUG("updateAPNSettings %d", ctxId);
        PDPContext pdpCtx(cellularService);
        std::shared_ptr<APN::Config> apnConfig;
        std::shared_ptr<APN::Config> modemApn;
        if ((modemApn = pdpCtx.getConfiguration(ctxId)) && (modemApn != nullptr)) {

            auto phoneApn = contextMap.find(ctxId);

            if (phoneApn != contextMap.end()) {
                LOG_DEBUG("Phone context exists");
                if (dataTransfer == DataTransfer::Off) {
                    /// set null configuration, solution based on lack of quectel documentation
                    return pdpCtx.setConfiguration(phoneApn->second, true);
                }
                else {
                    if (!phoneApn->second->compare(modemApn)) {
                        /// rebuild configuratio
                        LOG_DEBUG("Update modem context %d", ctxId);
                        return pdpCtx.setConfiguration(phoneApn->second);
                    }
                }
            }
            else {
                LOG_ERROR("Phone context not exists");
                if (!modemApn->isEmpty()) {
                    /** update phone configuration base on modem conf (eg. ims)
                     *
                     * Comment from quectel (2020.12):
                     * As I know, only VZW MBN would use IMS on CID 1 to register IMS service directly.
                     * So we usually ask customers to set up data connection on CID3 for VZW sim card.
                     * Regarding other MBN files, the CID 1 shouldn’t be IMS because the CID1 is used
                     * to activate default bearer for LTE network. So we usually ask customers to
                     * configure their own APN on CID1.  With this rule, it’s easy for customer
                     * to configure their APN no matter which MBN file is activated
                     */
                    LOG_ERROR("Update modem context %d", ctxId);
                    return pdpCtx.setConfiguration(modemApn, true);
                }
            }
        }
        return at::Result::Code::OK;
    }
    void PacketData::setupAPNSettings()
    {
        for (std::uint8_t ctxId = MINContextId; ctxId <= MAXContextId; ctxId++) {
            updateAPNSettings(ctxId);
        }
    }

    std::optional<std::shared_ptr<APN::Config>> PacketData::getAPN(std::uint8_t ctxid)
    {
        auto apn = std::find_if(contextMap.begin(), contextMap.end(), [&ctxid](const ContextPair &pair) {
            return pair.second->contextId == ctxid;
        });

        if (apn != contextMap.end()) {
            return apn->second;
        }

        return std::nullopt;
    }

    const std::vector<std::shared_ptr<APN::Config>> PacketData::getAPNs() const
    {
        std::vector<std::shared_ptr<APN::Config>> vconf;
        std::transform(
            contextMap.begin(), contextMap.end(), std::back_inserter(vconf), [](auto &cm) { return cm.second; });
        return vconf;
    }

    std::optional<std::shared_ptr<APN::Config>> PacketData::getAPNFirst(APN::APNType type)
    {

        auto apn = std::find_if(contextMap.begin(), contextMap.end(), [&type](const ContextPair &pair) -> bool {
            return pair.second->apnType == type;
        });

        if (apn != contextMap.end()) {
            return apn->second;
        }

        return std::nullopt;
    }

    at::Result::Code PacketData::setAPN(std::shared_ptr<APN::Config> apn)
    {
        contextMap[apn->contextId] = apn;
        return updateAPNSettings(apn->contextId);
    }

    bool PacketData::setDataTransfer(DataTransfer dt)
    {
        dataTransfer = dt;
        setupAPNSettings();
        return true;
    }
    DataTransfer PacketData::getDataTransfer() const
    {
        return dataTransfer;
    }

    std::optional<const std::vector<std::shared_ptr<APN::Config>>> PacketData::getActiveContexts()
    {
        PDPContext pdpCtx(cellularService);
        return pdpCtx.getActive();
    }
    at::Result::Code PacketData::activateContext(std::uint8_t contextId)
    {
        PDPContext pdpCtx(cellularService);
        return pdpCtx.activate(contextId);
    }
    at::Result::Code PacketData::deactivateContext(std::uint8_t contextId)
    {
        PDPContext pdpCtx(cellularService);
        return pdpCtx.deactivate(contextId);
    }
} // namespace packet_data