summaryrefslogtreecommitdiff
path: root/include/mts/MTS_IO_CellularRadio.h
blob: e985c21baad9a77c63473f7ce9fcdf0d2de7fe7f (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts-io.
 *
 * libmts-io is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libmts-io is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libmts-io.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*!
 \file MTS_IO_CellularRadio.h
 \brief A brief description
 \date Nov 5, 2014
 \author sgodinez

 A more elaborate description
*/
#ifndef MTS_IO_CELLULARRADIO_H_
#define MTS_IO_CELLULARRADIO_H_

#include <string>
#include <vector>

#include <json/json.h>

#include <mts/MTS_IO_ICellularRadio.h>
#include <mts/MTS_IO_SerialConnection.h>
#include <mts/MTS_NonCopyable.h>
#include <mts/MTS_AutoPtr.h>
#include <mts/MTS_Stdint.h>

namespace MTS {
    namespace IO {
        class CellularRadio : public ICellularRadio {

            public:
                const std::string& getName() const override;

                ~CellularRadio() override;

                bool initialize(uint32_t iTimeoutMillis = 5000) override;
                bool resetConnection(uint32_t iTimeoutMillis = 5000) override;
                void shutdown() override;

                CODE getFirmware(std::string& sFirmware) override;
                CODE getFirmwareBuild(std::string& sFirmwareBuild) override;
                CODE getVendorFirmware(std::string& sVendorFirmware) override;
                CODE getHardware(std::string& sHardware) override;
                CODE getManufacturer(std::string& sManufacturer) override;
                CODE getImei(std::string& sImei) override;
                CODE getMeid(std::string& sMeid) override;
                CODE getImsi(std::string& sImsi) override;
                CODE getSimStatus(std::string& sSimStatus) override;
                CODE getSimStatusSummary(Json::Value& jData) override;
                CODE getLac(std::string& sLac) override;
                CODE getMdn(std::string& sMdn) override;
                CODE getMsid(std::string& sMsid) override;
                CODE getType(std::string& sType) override;
                CODE getCarrier(std::string& sCarrier) override;
                CODE getTower(std::string& sTower) override;
                CODE getTime(std::string& sDate, std::string& sTime, std::string& sTimeZone) override;
                CODE getRoaming(bool& bRoaming) override;
                CODE getCellularMode(CELLULAR_MODES &networks) override;
                CODE getSelectedBandsRaw(std::string& sRawBands) override;

                CODE getSignalStrength(int32_t& iRssi) override;
                CODE getModemLocation(std::string& sLocation) override;

                CODE getRegistration(REGISTRATION& eRegistration) override;
                CODE convertRegistrationToString(REGISTRATION eRegistration, std::string& sRegistration) override;

                CODE convertCellModesToString(CELLULAR_MODES eCellModes, std::string& sCellModes) override;

                CODE unlockSimCard(const Json::Value& jArgs) override;
                CODE getSimCarrierCode(std::string& sCarrierCode) override;

                CODE getMipProfile(Json::Value& jMipProfile) override;
                CODE validateMsl(const Json::Value& jArgs) override;
                CODE setMsid(const Json::Value& jArgs) override;
                CODE setMipActiveProfile(const Json::Value& jArgs) override;
                CODE setMipNai(const Json::Value& jArgs) override;
                CODE setMipHomeIp(const Json::Value& jArgs) override;
                CODE setMipPrimaryHa(const Json::Value& jArgs) override;
                CODE setMipSecondaryHa(const Json::Value& jArgs) override;
                CODE setMipMnAaaSpi(const Json::Value& jArgs) override;
                CODE setMipMnHaSpi(const Json::Value& jArgs) override;
                CODE setMipRevTun(const Json::Value& jArgs) override;
                CODE setMipMnAaaSs(const Json::Value& jArgs) override;
                CODE setMipMnHaSs(const Json::Value& jArgs) override;
                CODE updateDc(const Json::Value& jArgs, UpdateCb& stepCb) override;
                CODE updatePrl(const Json::Value& jArgs, UpdateCb& stepCb) override;
                CODE updateFumo(const Json::Value& jArgs, UpdateCb& stepCb) override;
                CODE updateFumoLocal(int fd, UpdateCb& stepCb) override;
                CODE fumoLocalInject(int fd, UpdateCb& stepCb) override;
                CODE fumoLocalApply(UpdateCb& stepCb) override;
                CODE fumoLocalCleanup() override;
                CODE resetHfa(const Json::Value& jArgs, UpdateCb& stepCb) override;
                CODE activate(const Json::Value& jArgs, UpdateCb& stepCb) override;
                CODE startOmaDm(UpdateCb& stepCb) override;
                CODE setActiveFirmware(const Json::Value& jArgs) override;
                CODE getActiveFirmware(std::string& sFwId) override;
                CODE getEcho(bool& bEnabled) override;
                CODE setEcho(bool bEnabled = true) override;
                CODE getStaticInformation(Json::Value& jData) override;
                CODE sendBasicCommand(const std::string& sCmd, int32_t timeoutMillis = 100, const char& ESC = ICellularRadio::CR) override;

                std::string sendCommand(const std::string& sCmd,
                                                const std::vector<std::string>& vBail = DEFAULT_BAIL_STRINGS,
                                                int32_t timeoutMillis = 100,
                                                const char& ESC = ICellularRadio::CR) override;



                std::string sendCommand(const std::string& sCmd,
                                                IsNeedMoreData& isNeedMoreData,
                                                int32_t timeoutMillis = 100,
                                                const char& ESC = ICellularRadio::CR) override;

                //! Wait for response from the radio without sending any data to it
                virtual std::string waitResponse(const std::vector<std::string>& vBail = DEFAULT_BAIL_STRINGS,
                                                 int32_t timeoutMillis = 100) override;

                //! Wait for response from the radio without sending any data to it
                virtual std::string waitResponse(IsNeedMoreData& isNeedMoreData,
                                                 int32_t timeoutMillis = 100) override;

                CODE setUeModeOfOperation(ICellularRadio::UE_MODES_OF_OPERATION mode) override;
                CODE getUeModeOfOperation(ICellularRadio::UE_MODES_OF_OPERATION& mode) override;

                CODE getSimMccMnc(std::string& sMccMnc) override;
                CODE getSimMccMnc(std::string& sMcc, std::string& sMnc) override;

                CODE disableVoiceSupport() override;
                CODE getVoiceSupport(Json::Value& jData) override;
                CODE getVoiceSupport(bool& bVoiceEnabled, bool& bSmsOnly) override;

                CODE getPdpContexts(Json::Value& jData) override;
                CODE setPdpContext(const std::string& sId, const Json::Value& jConfig) override;

                CODE getDiagnostics(std::string& sReport) override;

            protected:

                CellularRadio(const std::string& sName, const std::string& sRadioPort);

                virtual bool getCarrierFromFirmware(const std::string& sFirmware, std::string& sCarrier);
                virtual bool getHardwareVersionFromFirmware(const std::string& sFirmware, std::string& sHardware);

                virtual void getCommonNetworkStats(Json::Value& jData);

                /**
                 * @brief getIsSimInserted - returns if the SIM card is inserted / installed or not.
                 *
                 * @param bData - an object to be filled with the SIM card insertion status.
                 *        `true` when SIM card is inserted / installed / present,
                 *        `false` otherwise.
                 * @return CODE::SUCCESS when SIM insertion status is fetched successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::ERROR otherwise (when modem is inaccessible or in other cases).
                 */
                virtual CODE getIsSimInserted(bool& bData) = 0;

                /**
                 * @brief getSimLockStatus - return the SIM lock status as defined by AT+CPIN? command.
                 *        Returns "READY", "SIM PIN", "SIM PUK" or other SIM status.
                 *
                 * @param sData - an object to be filled with the SIM lock status
                 * @return CODE::SUCCESS when SIM status is fetched successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::ERROR otherwise (SIM card removed, modem is inaccessible, etc).
                 */
                virtual CODE getSimLockStatus(std::string& sData);

                /**
                 * @brief getSimLockAttempts - get the number of SIM unlock attempts left.
                 *
                 * @param iAttemptsPin - the number of attempts left to enter a PIN code.
                 * @param iAttemptsPuk - the number of attempts left to enter a PUK code.
                 * @return CODE::SUCCESS when both numbers are fetched successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::ERROR otherwise (SIM card removed, modem is inaccessible, etc).
                 */
                virtual CODE getSimLockAttempts(int& iAttemptsPin, int& iAttemptsPuk) = 0;

                //! Get carrier code based on the SIM card ID (ICCID)
                virtual CODE getSimCarrierCode(const std::string& sIccid, std::string& sCarrierCode);
                //! Get carrier code based on the SIM MCC/MNC
                virtual CODE getSimCarrierCode(const std::string& sMcc, const std::string& sMnc, std::string& sCarrierCode);

                /**
                 * @brief simAccessReadBinary - Read a string of bytes from the SIM Elementary File.
                 *        Please see ETSI TS 102 221 v16.4.0 and ETSI TS 127 007 v16.6.0 for the details.
                 *
                 * @param iFiledId - identifier of the Elementary File to read from.
                 * @param iP1 - offset (ETSI TS 102 221 v16.4.0, Section 11.1.3.2).
                 * @param iP2 - offset low  (ETSI TS 102 221 v16.4.0, Section 11.1.3.2).
                 * @param iLe - number of bytes to be read (ETSI TS 102 221 v16.4.0, Section 11.1.3.2).
                 * @param sResult - container for the hex-encoded result string,
                 *        example for EFpnn: "004F474F21FFFFFFFFFFFFFFFFFFFFFFFF".
                 * @return CODE::SUCCESS when the read is completed successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::NO_RESPONSE when the modem doesn't respond,
                 *         CODE::ERROR when the radio returns "ERROR" (SIM card removed, SIM card locked etc),
                 *         CODE::FAILURE otherwise (unexpected response, no data in SIM etc).
                 */
                virtual CODE simAccessReadBinary(uint16_t iFiledId, uint8_t iP1, uint8_t iP2, uint8_t iLe, std::string& sResult);

                /**
                 * @brief getSimMncLength - Read the length of the MNC from SIM EFad.
                 *        Please see ETSI TS 131 102 V16.4.0, Section 4.2.18 for the details.
                 *
                 * @param iLength - container for the number of digits in SIM MNC.
                 * @return CODE::SUCCESS when the read is completed successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::NO_RESPONSE when the modem doesn't respond,
                 *         CODE::ERROR when the radio returns "ERROR" (SIM card removed, SIM card locked etc),
                 *         CODE::FAILURE otherwise (unexpected response, no data in SIM etc).
                 */
                virtual CODE getSimMncLength(uint8_t& iLength);

                /**
                 * @brief getCarrierFromSimMccMnc - get a carrier name based on the MCC/MNC values.
                 *
                 * @param sCarrier - a string to be filled with carrier based on the MCC/MNC values.
                 *
                 * @return CODE::SUCCESS when the read is completed successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::NO_RESPONSE when the modem doesn't respond,
                 *         CODE::ERROR when the radio returns "ERROR" (SIM card removed, SIM card locked etc),
                 *         CODE::FAILURE otherwise (unexpected response, no data in SIM etc).
                 */
                virtual CODE getCarrierFromSimMccMnc(std::string& sCarrier);

                /**
                 * @brief getCarrierFromSimSpn - get a carrier name from SIM EFspn.
                 *
                 * @param sCarrier - a string to be filled with carrier obtained from SIM EFspn.
                 *
                 * @return CODE::SUCCESS when the read is completed successfully,
                 *         CODE::NOT_APPLICABLE when the modem doesn't support this feature,
                 *         CODE::NO_RESPONSE when the modem doesn't respond,
                 *         CODE::ERROR when the radio returns "ERROR" (SIM card removed, SIM card locked etc),
                 *         CODE::FAILURE otherwise (unexpected response, no data in SIM etc).
                 */
                virtual CODE getCarrierFromSimSpn(std::string& sCarrier);

                void initMipProfile(Json::Value& jData);
                bool splitAndAssign(const std::string& sLine, const std::string& sKey, Json::Value& jParent, const std::string& sJsonKey, Json::ValueType eType = Json::ValueType::stringValue);

                virtual std::string queryLteLac();
                virtual std::string queryCGREGstring();
                virtual void setCGREG(std::string value);

                const std::vector<std::string> getRegistrationCommands();
                REGISTRATION parseRegResponse(std::string sResult);
                CODE getRegistration(REGISTRATION& eRegistration, const std::string& sType);

                virtual CODE sendData(const char* pData, size_t nBytes);
                virtual CODE sendBasicQuery(const std::string& sCmd, const std::string& sLabel, std::string& sResult, int32_t timeoutMillis = 100, const char& ESC = ICellularRadio::CR);

                /**
                 * @brief getDiagCommands - returns the list of Cellular Diagnostics commands for this radio.
                 *
                 * @param bIsSimReady - set to "true" if the SIM card is inserted and NOT locked by PIN or PUK.
                 *
                 * @return the list of AT commands (strings).
                 */
                virtual const std::vector<std::string>& getDiagCommands(bool bIsSimReady = true) = 0;

                class RadioBandMap : public MTS::NonCopyable {
                    public:
                        RadioBandMap()
                        {
                            m_sChannel = ICellularRadio::VALUE_UNKNOWN;
                            m_iChannel = 0;
                            m_sRadioType = ICellularRadio::VALUE_UNKNOWN;
                        }

                        RadioBandMap(const std::string &channel, const std::string &radioType) :
                            m_sChannel(channel),
                            m_sRadioType(radioType)
                        {
                            m_iChannel = strtol(m_sChannel.c_str(), NULL, 10);
                        }

                        virtual ~RadioBandMap() {}

                        const char *getRadioBandName();
                        const char *getRadioBandName(const std::string &channel, const std::string &radioType);

                    private:

                        const char *getLTEBand(const int channel);
                        const char *getCDMABand(const int channel);
                        const char *getGSMBand(const int channel);

                        std::string    m_sChannel;
                        int            m_iChannel;
                        std::string    m_sRadioType;
                };

                static CODE getFileSize(int fd, size_t& nBytes);
                static CODE sizeToChunks(const size_t nBytes, const size_t chunkSize, size_t& nChunks);
                static CODE readChunk(int fd, char* pChunk, size_t dChunkSize, size_t& nReadBytes);

            private:
                std::string m_sName;
                std::string m_sRadioPort;
                std::string m_sFirmware;
                std::string m_sCarrier;
                MTS::AutoPtr<MTS::IO::Connection> m_apIo;

                bool m_bEchoEnabled;
                bool m_bEnableEchoOnClose;
        };
    }
}

#endif /* MTS_IO_CELLULARRADIO_H_ */