summaryrefslogtreecommitdiff
path: root/src/MTS_IO_TelitRadio.cpp
blob: be31b031de6b531ab07693632c14c3ade67cd334 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
 * Copyright (C) 2019 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/>.
 *
 */

#include <mts/MTS_IO_TelitRadio.h>

#include <mts/MTS_Logger.h>
#include <mts/MTS_Thread.h>
#include <mts/MTS_Text.h>

using namespace MTS::IO;

TelitRadio::TelitRadio(const std::string& sName, const std::string& sRadioPort)
: CellularRadio(sName, sRadioPort)
{
}

bool TelitRadio::resetRadio(uint32_t iTimeoutMillis) {

    printInfo("%s| Rebooting radio", getName().c_str());
    if(sendBasicCommand("AT#REBOOT") == SUCCESS) {
        if(iTimeoutMillis > 5000) {
            MTS::Thread::sleep(5000);
            iTimeoutMillis -= 5000;
        }
        return resetConnection(iTimeoutMillis);
    }

    return false;
}


ICellularRadio::CODE TelitRadio::getModel(std::string& sModel) {
    printTrace("%s| Get Model", getName().c_str());
    //Always returns SUCCESS because the model should be m_sName
    sModel = getName();
    std::string sCmd("ATI4");
    std::string sResult = sendCommand(sCmd);
    if (sResult.find("OK") == std::string::npos) {
        printWarning("%s| Unable to get model from radio.  Returning [%s]", getName().c_str(), getName().c_str());
        return SUCCESS;
    } else {
        sModel = extractModelFromResult(sResult);
        if(sModel.size() == 0) {
            printWarning("%s| Unable to get model from radio.  Returning [%s]", getName().c_str(), getName().c_str());
            return SUCCESS;
        }
    }

    printDebug("%s| Extracted [%s] from [%s] query", getName().c_str(), sModel.c_str(), sCmd.c_str());
    if(sModel != getName()) {
        printWarning("%s| Model identified [%s] does not match expected [%s]. Returning [%s]",
                     getName().c_str(),  sModel.c_str(), getName().c_str(), sModel.c_str());
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::getIccid(std::string& sIccid) {
    printTrace("%s| Get ICCID", getName().c_str());
    sIccid = ICellularRadio::VALUE_NOT_SUPPORTED;
    std::string sCmd("AT#CCID");
    std::string sResult = CellularRadio::sendCommand(sCmd);
    size_t end = sResult.find(ICellularRadio::RSP_OK);
    if (end == std::string::npos) {
        printWarning("%s| Unable to get ICCID from radio using command [%s]", getName().c_str(), sCmd.c_str());
        return FAILURE;
    }

    size_t start = sResult.find("#CCID:");
    if(start != std::string::npos) {
        start += sizeof("#CCID:");
        sIccid = MTS::Text::trim(sResult.substr(start, end-start));
        if(sIccid.size() == 0) {
            printWarning("%s| Unable to get ICCID from radio using command [%s]", getName().c_str(), sCmd.c_str());
            return FAILURE;
        }
    }
    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::getService(std::string& sService) {
    printTrace("%s| Get Service", getName().c_str());
    sService = ICellularRadio::VALUE_NOT_SUPPORTED;
    std::string sCmd("AT#PSNT?");
    std::string sResult = CellularRadio::sendCommand(sCmd);
    size_t end = sResult.find(ICellularRadio::RSP_OK);
    if (end == std::string::npos) {
        printWarning("%s| Unable to get Service from radio using command [%s]", getName().c_str(), sCmd.c_str());
        return FAILURE;
    }

    size_t start = sResult.find(",");
    if(start != std::string::npos) {
        start += 1; //comma
        std::string sPsnt = MTS::Text::trim(sResult.substr(start, end-start));
        int32_t iService;
        sscanf(sPsnt.c_str(), "%d", &iService);

        switch(iService) {
            case 0: sService = "GPRS"; break;
            case 1: sService = "EGPRS"; break;
            case 2: sService = "WCDMA"; break;
            case 3: sService = "HSDPA"; break;
            case 4: sService = "LTE"; break;
            default: sService = ICellularRadio::VALUE_UNKNOWN; break;
        }

        printDebug("%s| Service ID: [%d][%s]", getName().c_str(), iService, sService.c_str());
    }
    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::getNetwork(std::string& sNetwork) {
    Json::Value jData;

    printTrace("%s| Get Network", getName().c_str());
    sNetwork = ICellularRadio::VALUE_NOT_SUPPORTED;

    if(getNetworkStatus(jData) == SUCCESS) {
        if(jData.isMember(ICellularRadio::KEY_NETWORK)) {
            sNetwork = jData[ICellularRadio::KEY_NETWORK].asString();
            return SUCCESS;
        }
    }
    return FAILURE;
}

/*  AT#RFSTS - NETWORK STATUS

    (GSM network)
    #RFSTS:<PLMN>,<ARFCN>,<RSSI>,<LAC>,<RAC>,<TXPWR>,<MM>,<RR>,<NOM>,<CID>,<IMSI>,<NetNameAsc>,<SD>,<ABND>
    Where:
    <PLMN> - Country code and operator code(MCC, MNC)
    <ARFCN> - GSM Assigned Radio Channel
    <RSSI> - Received Signal Strength Indication
    <LAC> - Localization Area Code
    <RAC> - Routing Area Code
    <TXPWR> - Tx Power
    <MM> - Mobility Management state
    <RR> - Radio Resource state
    <NOM> - Network Operator Mode
    <CID> - Cell ID
    <IMSI> - International Mobile Subscriber Identity
    <NetNameAsc> - Operator name
    <SD> - Service Domain
    0 - No Service
    1 - CS only
    2 - PS only
    3 - CS+PS
    <ABND> - Active Band
    1 - GSM 850
    2 - GSM 900
    3 - DCS 1800
    4 - PCS 1900


    (WCDMA network)
    #RFSTS:
    <PLMN>,<UARFCN>,<PSC>,<Ec/Io>,<RSCP>, RSSI>,<LAC>,<RAC>,<TXPWR>,<DRX>,<MM>,<RRC>,<NOM>,<BLER>,<CID>,<IMSI>,
    <NetNameAsc>,<SD>,<nAST>[,<nUARFCN><nPSC>,<nEc/Io>]
    Where:
    <PLMN> - Country code and operator code(MCC, MNC)
    <UARFCN> - UMTS Assigned Radio Channel
    <PSC> - Active PSC(Primary Synchronization Code)
    <Ec/Io> - Active Ec/Io(chip energy per total wideband power in dBm)
    <RSCP> - Active RSCP (Received Signal Code Power in dBm)
    <RSSI> - Received Signal Strength Indication
    <LAC> - Localization Area Code
    <RAC> - Routing Area Code
    <TXPWR> - Tx Power
    <DRX> - Discontinuous reception cycle Length (cycle length in ms)
    <MM> - Mobility Management state
    <RR> - Radio Resource state
    <NOM> - Network Operator Mode
    <BLER> - Block Error Rate (e.g., 005 means 0.5 %)
    <CID> - Cell ID
    <IMSI> - International Mobile Station ID
    <NetNameAsc> - Operator name
    <SD> - Service Domain (see above)
    <nAST> - Number of Active Set (Maximum 6)
    <nUARFCN> UARFCN of n th active set
    <nPSC> PSC of n th active set
    <nEc/Io > Ec/Io of n th active Set

    (LTE Network)
    #RFSTS:
    <PLMN> -
    <EARFCN> -
    <RSRP> -
    <RSSI> -
    <RSRQ> -
    <TAC> -
    [<TXPWR>] -
    <DRX> -
    <MM> -
    <RRC> -
    <CID> -
    <IMSI> -
    [<NetNameAsc>] -
    <SD> -
    <ABND> -
*/
ICellularRadio::CODE TelitRadio::getNetworkStatus(Json::Value& jData) {
    int32_t iValue;
    std::string sValue;
    const uint32_t GSM_NETWORK_FORMAT = 14;
    const uint32_t WCDMA_NETWORK_FORMAT = 19;
    const uint32_t LTE_NETWORK_FORMAT = 16;

    printTrace("%s| Get Network Status", getName().c_str());

    //Always get common network stats because this should never fail
    //This way the basic stats are always returned even if AT#RFSTS fails below
    getCommonNetworkStats(jData);

    std::string sCmd;
    std::string sResult;
    // LE910 radios have a bug where issuing AT#RFSTS with a locked SIM
    // will cause the radio to stop responding until a radio power cycle
    // Telit Support Portal Case #5069697
    // LE910C1-NS is an LE910, so we stop the scan after the 0.
    if (getName().find("LE910") != std::string::npos) {
        sCmd = "AT+CPIN?";
        sResult = sendCommand(sCmd);
        if (sResult.find("+CPIN:") == std::string::npos) {
            printDebug("%s| AT+CPIN? returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
            printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
            return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
        }
        if (sResult.find("SIM PIN") != std::string::npos) {
            printError("%s| The SIM is locked and must first be unlocked", getName().c_str());
            printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
            return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
        }
    }

    sCmd = "AT#RFSTS";
    sResult = sendCommand(sCmd, DEFAULT_BAIL_STRINGS, 200);
    if (sResult.find("#RFSTS:") == std::string::npos) {
        //On LTE radios without signal, this case will run because AT#RFSTS just returns "OK"
        printDebug("%s| Network Status command returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
        printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
        return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
    }

    size_t start = sResult.find(":") + 1; //Position right after "#RFSTS:"
    std::vector<std::string> vParts = MTS::Text::split(MTS::Text::trim(sResult.substr(start)), ",");

    if (vParts.size() < 3) {
        printDebug("%s| Network Status command reponse is an unknown format: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
        printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
        return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
    } else {
        //Country Code and Operator Code
        std::vector<std::string> vPLMN = MTS::Text::split(vParts[0], ' ');
        if(vPLMN.size() == 2) {
            jData[ICellularRadio::KEY_MCC] = MTS::Text::strip(vPLMN[0], '"');
            jData[ICellularRadio::KEY_MNC] = MTS::Text::strip(vPLMN[1], '"');
        }

        jData[ICellularRadio::KEY_CHANNEL] = vParts[1];
    }

    if (vParts.size() == GSM_NETWORK_FORMAT ) {
        //Parse as GSM Network Format
        jData[ICellularRadio::KEY_RSSIDBM] = vParts[2];
        jData[ICellularRadio::KEY_LAC] =  vParts[3];
        jData[ICellularRadio::KEY_RAC] = vParts[4];
        jData[ICellularRadio::KEY_TXPWR] = vParts[5];
        jData[ICellularRadio::KEY_MM] = vParts[6];
        jData[ICellularRadio::KEY_RR] = vParts[7];
        jData[ICellularRadio::KEY_NOM] = vParts[8];
        jData[ICellularRadio::KEY_CID] = vParts[9];
        jData[ICellularRadio::KEY_IMSI] = MTS::Text::strip(vParts[10], '"');
        jData[ICellularRadio::KEY_NETWORK] = MTS::Text::strip(vParts[11], '"');
        if(MTS::Text::parse(iValue, vParts[12]) && convertServiceDomainToString((SERVICEDOMAIN)iValue, sValue) == SUCCESS) {
            jData[ICellularRadio::KEY_SD] = sValue;
        }
        if(MTS::Text::parse(iValue, vParts[13]) && convertActiveBandToString((ACTIVEBAND)iValue, sValue) == SUCCESS) {
            jData[ICellularRadio::KEY_ABND] = sValue;
        }
      // IN003567 ME910C1 radios have some odd behavior with regards to WCDMA.  The ordering of the fields from #RFSTS are
      // the same as LTE up to the 16th field (for ME901C1-WW anyway).  Drop into LTE parsing for ME910C1-WW.
    } else if((vParts.size() >= WCDMA_NETWORK_FORMAT) && (getName().find("ME910C1-WW") == std::string::npos)) {
        Json::Value jDebug;

        //Parse as WCDMA Network Format

        jDebug[ICellularRadio::KEY_PSC] = vParts[2];
        jDebug[ICellularRadio::KEY_ECIO] = vParts[3];
        jDebug[ICellularRadio::KEY_RSCP] = vParts[4];

        jData[ICellularRadio::KEY_RSSIDBM] = vParts[5];
        jData[ICellularRadio::KEY_LAC] = vParts[6];
        jData[ICellularRadio::KEY_RAC] = vParts[7];

        jDebug[ICellularRadio::KEY_TXPWR] = vParts[8];
        jDebug[ICellularRadio::KEY_DRX] = vParts[9];
        jDebug[ICellularRadio::KEY_MM] = vParts[10];
        jDebug[ICellularRadio::KEY_RR] = vParts[11];
        jDebug[ICellularRadio::KEY_NOM] = vParts[12];

        if(vParts[13].size() != 0) {
            jDebug[ICellularRadio::KEY_BLER] = vParts[13];
        } else {
            jDebug[ICellularRadio::KEY_BLER] = "000";
        }

        jData[ICellularRadio::KEY_CID] = vParts[14];
        jData[ICellularRadio::KEY_IMSI] = MTS::Text::strip(vParts[15], '"');
        jData[ICellularRadio::KEY_NETWORK] = MTS::Text::strip(vParts[16], '"');

        // Get the radio band given the channel (UARFCN)
        RadioBandMap radioBandMap(vParts[1], CellularRadio::ICellularRadio::VALUE_TYPE_CDMA);
        jData[ICellularRadio::KEY_ABND] = radioBandMap.getRadioBandName();

        if(MTS::Text::parse(iValue, vParts[17]) && convertServiceDomainToString((SERVICEDOMAIN)iValue, sValue) == SUCCESS) {
            jDebug[ICellularRadio::KEY_SD] = sValue;
        }
        //Ignoring Active Set Values
        //  <nAST> - Number of Active Set (Maximum 6)
        //  <nUARFCN> - UARFCN of n th active set
        //  <nPSC> - PSC of n th active set
        //  <nEc/Io > - Ec/Io of n th active Set

        jData[ICellularRadio::KEY_DEBUG] = jDebug;
    } else if(vParts.size() >= LTE_NETWORK_FORMAT) {
        Json::Value jDebug;

        //Parse as LTE Network Format

        //
        // MD: It is noticed that LTE Network format may vary depending on the firmware version:
        //
        // <PLMN>,<EARFCN>,<RSRP>,<RSSI>,<RSRQ>,<TAC>,[<TXPWR>],<DRX>,<MM>,<RRC>,<CID>,<IMSI>,[<NetNameAsc>],<SD>,<ABND>,<SINR>
        //    Ex 1: #RFSTS: "310 260",2300,-98,-63,-14,AA06,,128,19,0,0501D02,"310260754792598","T-Mobile",3,4,197
        //
        // <PLMN>,<EARFCN>,<RSRP>,<RSSI>,<RSRQ>,<TAC>,<RAC>,[<TXPWR>],<DRX>,<MM>,<RRC>,<CID>,<IMSI>,[<NetNameAsc>],<SD>,<ABND>
        //    Ex 2: #RFSTS:"310 410",5780,-105,-73,-14,4603,255,,128,19,0,0000098,"310410536498694","AT&T",3,17
        //          #RFSTS:"311 480",1150,-96,-66,-9.0,bf35,FF,0,0,19,1,"2ED1B0E","311480148817753","Verizon",2,2,720000,10800
        //          #RFSTS:"310 410",2175,-120,-89,-17.5,4612,FF,0,0,19,1,"4E5E916","310410807276607","AT&T",3,4
        //
        // Additional <RAC> parameter in the second example shifts the rest of the parameters. Here we are trying to figure out
        // which format is currently produced based on <NetNameAsc> field position which always has double quotation marks.
        //
        if (vParts[13].find("\"") != std::string::npos) {
            // parse the RAC and then remove it from the vector
            jData[ICellularRadio::KEY_RAC] = vParts[6];
            vParts.erase(vParts.begin() + 6);
        }

        jDebug["rsrp"] = vParts[2];
        jDebug[ICellularRadio::KEY_RSSIDBM] = vParts[3];
        jDebug["rsrq"] = vParts[4];

        jData["tac"] = vParts[5];
        jDebug[ICellularRadio::KEY_TXPWR] = vParts[6];
        jData[ICellularRadio::KEY_DRX] = vParts[7];
        jDebug[ICellularRadio::KEY_MM] = vParts[8];
        jDebug["rrc"] = vParts[9];
        jData[ICellularRadio::KEY_CID] = MTS::Text::strip(vParts[10], '"');
        jData[ICellularRadio::KEY_IMSI] = MTS::Text::strip(vParts[11], '"');
        jData[ICellularRadio::KEY_NETWORK] = MTS::Text::strip(vParts[12], '"');

        // Get the radio band given the channel (EARFCN)
        RadioBandMap radioBandMap(vParts[1], ICellularRadio::VALUE_TYPE_LTE);
        jData[ICellularRadio::KEY_ABND] = radioBandMap.getRadioBandName();

        jData[ICellularRadio::KEY_LAC] = queryLteLac();

        if(MTS::Text::parse(iValue, vParts[13]) && convertServiceDomainToString((SERVICEDOMAIN)iValue, sValue) == SUCCESS) {
            jDebug[ICellularRadio::KEY_SD] = sValue;
        }

        jData[ICellularRadio::KEY_DEBUG] = jDebug;
    }

    printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::convertSignalStrengthTodBm(const int32_t& iRssi, int32_t& iDbm) {

    //Telit Conversion
    if(iRssi < 0 || iRssi == 99) {
        return FAILURE;
    }

    if(iRssi == 0) {
        iDbm = -113;
    } else if(iRssi == 1) {
        iDbm = -111;
    } else if(iRssi <= 30) {
        //28 steps between 2 and 30
        //54 dbm between 53 and 109
        float stepSize = 54.0 / 28.0;
        iDbm = -109 + (int)(stepSize * (iRssi-2));
    } else {
        iDbm = -51;
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::convertdBmToSignalStrength(const int32_t& iDBm, int32_t& iRssi) {
    //Telit Conversion
    if(iDBm <= -113) {
        iRssi = 0;
    } else if(iDBm <= -111) {
        iRssi = 1;
    } else if(iDBm <= -53) {
        //54 dbm between -109 and -53
        //28 steps between 2 and 30
        float stepSize = 28.0/54.0;
        iRssi = ((iDBm + 109)*stepSize) + 2;
    } else {
        iRssi = 31;
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::setMdn(const Json::Value& jArgs) {
    printTrace("%s| Set MDN", getName().c_str());

    if(!jArgs["mdn"].isString()) {
        return INVALID_ARGS;
    }

    std::string sCmd("AT#SNUM=1,\"");
    sCmd += jArgs["mdn"].asString() + "\"";

    std::string sResult = sendCommand(sCmd, DEFAULT_BAIL_STRINGS, 1000);
    size_t end = sResult.find(ICellularRadio::RSP_OK);
    if (end == std::string::npos) {
        printWarning("%s| Unable to set MDN for radio using command [%s]", getName().c_str(), sCmd.c_str());
        return FAILURE;
    }

    return SUCCESS;
}

bool TelitRadio::getCarrierFromFirmware(const std::string& sFirmware, std::string& sCarrier) {
    // Telit Radios
    //    H.ab.zyx => 3 Main Components
    //    "H" = Hardware -> 15 = DE910 family, 18 = CE910 family, 12 = HE910 family
    //    "a" = Hardware version
    //    "b" = Software Major Version
    //    "z" = is the product type, i.e. DUAL or SC
    //    "y" = is the carrier variant
    //    "x" = is the firmware version
    //    Telit will do their best to keep the carrier variant as "0" for Sprint, "1" for Aeris, "2" for Verizon, and "3" for U.S. Cellular.

    const uint32_t CARRIER_INDEX = 1;   //y in [zyx]

    bool bResult = false;
    std::vector<std::string> vParts = MTS::Text::split(sFirmware, '.');

    if(vParts.size() == 3) {
        //CDMA firmware version notation
        if(vParts[0] == "15" || vParts[0] == "18") {
            //DE910 or CE910 -> Good good
            std::string sID = vParts[2];
            if(sID.size() == 3) {
                char cId = sID[CARRIER_INDEX];

                //Good good
                if(cId == '0') {
                    sCarrier = ICellularRadio::VALUE_CARRIER_SPRINT;
                    bResult = true;
                } else
                if(cId == '1') {
                    sCarrier = ICellularRadio::VALUE_CARRIER_AERIS;
                    bResult = true;
                } else
                if(cId == '2') {
                    sCarrier = ICellularRadio::VALUE_CARRIER_VERIZON;
                    bResult = true;
                } else
                if(cId == '3') {
                    sCarrier = ICellularRadio::VALUE_CARRIER_USCELLULAR;
                    bResult = true;
                }
            }
        }
    }

    return bResult;
}

bool TelitRadio::getHardwareVersionFromFirmware(const std::string& sFirmware, std::string& sHardware) {
    // Telit Radios
    //    H.ab.zyx => 3 Main Components
    //    "H" = Hardware -> 15 = DE910 family, 18 = CE910 family, 12 = HE910 family
    //    "a" = Hardware version
    //    "b" = Software Major Version
    //    "z" = is the product type, i.e. DUAL or SC
    //    "y" = is the carrier variant
    //    "x" = is the firmware version
    //    Telit will do their best to keep the carrier variant as "0" for Sprint, "1" for Aeris, and "2" for Verizon.

    const uint32_t HARDWARE_INDEX = 0;   //a in [ab]

    bool bResult = false;
    std::vector<std::string> vParts = MTS::Text::split(sFirmware, '.');

    if(vParts.size() == 3) {
        //GSM Hardware Version
        if(!(vParts[0] == "15" || vParts[0] == "18")) {
            //Not DE910 or CE910 -> Good good
            std::string sVersion = vParts[1];
            if(sVersion.size() == 2) {
                sHardware = "1.";
                sHardware += sVersion[HARDWARE_INDEX];
                bResult = true;
            }
        }
    }

    return bResult;

}

ICellularRadio::CODE TelitRadio::getIsSimInserted(bool& bData) {
    printTrace("%s| Get SIM insertion status", getName().c_str());
    std::string sCmd("AT#SIMDET?");
    std::string sResult = sendCommand(sCmd);

    const std::string sPrefix = "#SIMDET: ";
    size_t start = sResult.find(sPrefix);
    size_t end = sResult.rfind(ICellularRadio::RSP_OK);

    if (end == std::string::npos) {
        printWarning("%s| Unable to get SIM insertion status from radio using command [%s]", getName().c_str(), sCmd.c_str());
        return FAILURE;
    }

    if (start == std::string::npos) {
        printDebug("%s| AT#SIMDET? returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
        return FAILURE;
    }

    // #SIMDET: <mode>,<simin>
    start += sPrefix.size();
    std::vector<std::string> vParts = MTS::Text::split(MTS::Text::trim(sResult.substr(start, end-start)), ',');

    if(vParts.size() != 2) {
        printWarning("%s| Unable to parse SIM insertion status from response [%s]", getName().c_str(), sResult.c_str());
        return FAILURE;
    }

    if (vParts[1] == "1") { // <simin>
        bData = true;
    } else {
        bData = false;
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::getSimLockAttempts(int& iAttemptsPin, int& iAttemptsPuk) {
    std::string sLockStatus;
    ICellularRadio::CODE retCode;

    retCode = getSimLockStatus(sLockStatus);
    if (retCode != SUCCESS) {
        printWarning("%s| Unable determine the number of SIM unlock attempts: SIM lock status is unavailable [%s]", getName().c_str(), sLockStatus.c_str());
        return retCode;
    }

    return getSimLockAttempts(iAttemptsPin, iAttemptsPuk, sLockStatus);
}

ICellularRadio::CODE TelitRadio::getSimLockAttempts(int& iAttemptsPin, int& iAttemptsPuk, const std::string& sLockStatus) {
    printTrace("%s| Get SIM unlock attempts left", getName().c_str());
    std::string sCmd("AT#PCT");
    std::string sResult = sendCommand(sCmd, DEFAULT_BAIL_STRINGS, 500);
    std::string sValue;
    int iValue;

    const std::string sPrefix = "#PCT: ";
    size_t start = sResult.find(sPrefix);
    size_t end = sResult.rfind(ICellularRadio::RSP_OK);

    if (end == std::string::npos) {
        printWarning("%s| Unable to get SIM unlock attempts from radio using command [%s]", getName().c_str(), sCmd.c_str());
        return FAILURE;
    }

    if (start == std::string::npos) {
        printDebug("%s| AT#PCT? returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
        return FAILURE;
    }

    // #PCT: <n>
    start += sPrefix.size();
    sValue = MTS::Text::trim(sResult.substr(start, end-start));

    if (!MTS::Text::parse(iValue, sValue)) {
        printWarning("%s| Unable to parse SIM unlock attempts from response [%s]", getName().c_str(), sResult.c_str());
        return FAILURE;
    }

    if (sLockStatus == "READY" || sLockStatus == "SIM PIN") {
        iAttemptsPin = iValue;  // Some PIN attempts left, maximum PUK attempts left
        iAttemptsPuk = 10;
    } else {
        iAttemptsPin = 0;  // No PIN attempts left
        iAttemptsPuk = iValue;
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::getSupportedCellularModes(CELLULAR_MODES &networks) {
    networks = CELLULAR_MODE_NA;

    std::set<int> wds;
    if ( TelitRadio::wdsList(wds) != SUCCESS ) {
        return FAILURE;
    }
    for(const auto &it : wds) {
        switch (it) {
        case 12: networks = static_cast<CELLULAR_MODES>(networks | CELLULAR_MODE_2G); break;
        case 22: networks = static_cast<CELLULAR_MODES>(networks | CELLULAR_MODE_3G); break;
        case 28: networks = static_cast<CELLULAR_MODES>(networks | CELLULAR_MODE_4G); break;
        case 36: networks = static_cast<CELLULAR_MODES>(networks | CELLULAR_MODE_5G); break;
        }
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::wdsList(std::set<int> &wds)
{
    std::string sCmd("AT+WS46=?");
    std::string cmdResult = sendCommand(sCmd);
    if (cmdResult.find("+WS46:") == std::string::npos) {
        printError("%s| AT+WS46=? returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), cmdResult.c_str());
        return FAILURE;
    }

    if (cmdResult.find('(') == std::string::npos) {
        printError("AT+WS46: error responce %s", cmdResult.c_str());
        return FAILURE;
    }
    std::string s = MTS::Text::split(cmdResult, '(')[1];
    s = MTS::Text::split(s, ')')[0];
    std::vector<std::string>v = MTS::Text::split(s, ',');
    for(const auto &it : v)
    {
        if (it.find("-") != std::string::npos) {
            const std::vector<std::string> &r = MTS::Text::split(it, "-");
            int begin, end;
            if ( ! MTS::Text::parse(begin, r[0]) || ! MTS::Text::parse(end, r[1])) {
                printError("AT+WS46: error parsing network mode range: %s-%s", r[0].c_str(), r[1].c_str());
                return FAILURE;
            }
            for (int i = begin; i<=end; ++i) {
                wds.insert(i);
                if (wds.size()>1024)
                    break;
            }
        } else {
            int v;
            if ( ! MTS::Text::parse(v, it)) {
                printError("AT+WS46: error parsing network mode: %s", it.c_str());
                return FAILURE;
            }
            wds.insert(v);
        }
    }
    if (wds.size()>1024) {
        printError("AT+WS46: network modes count overflow, parsing error");
        return FAILURE;
    }

    return SUCCESS;
}

ICellularRadio::CODE TelitRadio::setCellularMode(CELLULAR_MODES networks) {
    std::set<int> supportedWds;
    if ( TelitRadio::wdsList(supportedWds) != SUCCESS ) {
        return FAILURE;
    }

    int wds = 0;
    // 3GPP TS 27.007
    // https://portal.3gpp.org/desktopmodules/Specifications/SpecificationDetails.aspx?specificationId=1515
    switch (static_cast<int>(networks)) {
    case CELLULAR_MODE_2G                                                         : wds = 12; break;
    case                    CELLULAR_MODE_3G                                      : wds = 22; break;
    case CELLULAR_MODE_2G | CELLULAR_MODE_3G | CELLULAR_MODE_4G                   : wds = 25; break;
    case                                       CELLULAR_MODE_4G                   : wds = 28; break;
    case CELLULAR_MODE_2G | CELLULAR_MODE_3G                                      : supportedWds.count(28) ? wds = 29 : wds = 25; break;
    case CELLULAR_MODE_2G |                    CELLULAR_MODE_4G                   : wds = 30; break;
    case                    CELLULAR_MODE_3G | CELLULAR_MODE_4G                   : wds = 31; break;
    case CELLULAR_MODE_2G | CELLULAR_MODE_3G | CELLULAR_MODE_4G | CELLULAR_MODE_5G: wds = 35; break;
    case                                                          CELLULAR_MODE_5G: wds = 36; break;
    case                                       CELLULAR_MODE_4G | CELLULAR_MODE_5G: wds = 37; break;
    case                    CELLULAR_MODE_3G | CELLULAR_MODE_4G | CELLULAR_MODE_5G: wds = 38; break;
    case CELLULAR_MODE_2G                    | CELLULAR_MODE_4G | CELLULAR_MODE_5G: wds = 39; break;
    case                    CELLULAR_MODE_3G                    | CELLULAR_MODE_5G: wds = 40; break;
    case CELLULAR_MODE_2G | CELLULAR_MODE_3G                    | CELLULAR_MODE_5G: wds = 41; break;
    case CELLULAR_MODE_2G                                       | CELLULAR_MODE_5G: wds = 42; break;
    }
    std::string sCmd("AT+WS46=");
    sCmd += std::to_string(wds);
    std::string cmdResult = sendCommand(sCmd);
    if (cmdResult.find(ICellularRadio::RSP_OK) == std::string::npos) {
        printError("%s| AT+WS46= returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), cmdResult.c_str());
        return FAILURE;
    }
    return SUCCESS;
}