summaryrefslogtreecommitdiff
path: root/main.cpp
blob: bb8bc49365f10d2d71d2cb7d3a8aa624424987a3 (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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of radio-cmd.
 *
 * radio-cmd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * radio-cmd 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with radio-cmd.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mts/MTS_Text.h>
#include <mts/MTS_Logger.h>
#include <mts/MTS_AutoPtr.h>
#include <mts/MTS_IO_ICellularRadio.h>
#include <mts/MTS_IO_CellularRadioFactory.h>
#include <json/json.h>
#include <sstream>
#include <getopt.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include "Version.h"

const std::string DEFAULT_PORT("/dev/modem_at1");
const int32_t DEFAULT_TIMEOUT = 100;
const std::string DEFAULT_COMMAND("AT");
const std::string DEFAULT_CACHE("/var/run/radio");
const std::string FMODEL("model");
const std::string FMSL("msl");

std::string g_sTimeout;
std::string g_sDevice(DEFAULT_PORT);
std::string g_sAtCommand(DEFAULT_COMMAND);
std::string g_sCache(DEFAULT_CACHE);
std::string g_sModel;

std::string g_sMDN;
std::string g_sMSID;
std::string g_sMSL;
std::string g_sMIPActiveProfile;
std::string g_sMIPNai;
std::string g_sMIPHomeAddr;
std::string g_sMIPPrimaryHa;
std::string g_sMIPSecondaryHa;
std::string g_sMIPMnAaaSpi;
std::string g_sMIPMnHaSpi;
std::string g_sMIPRevTun;
std::string g_sMIPMnAaaSs;
std::string g_sMIPMnHaSs;
std::string g_sRxDiversity;
std::string g_sActiveFirmware;
Json::Value g_jData;
std::string g_sSimPin;
std::string g_sCellularMode;
std::string g_sDeltaFwPath;
std::string g_sUeModeOfOperation;
std::string g_sPdpId;
Json::Value g_jPdpConfig;

MTS::AutoPtr<MTS::IO::ICellularRadio> g_apRadio;

int32_t g_iOptions = 0;

const uint32_t OPT_INDIVIDUAL          = 0x01FFFFFF;
const uint32_t OPT_SUMMARY             = 0x7E000000;
const uint32_t OPT_INITIALIZE          = 0x80000000;

const uint32_t OPT_TIMEOUT             = 0x00000001;
const uint32_t OPT_DEVICE              = 0x00000002;
const uint32_t OPT_COMMAND             = 0x00000004;
const uint32_t OPT_MSL                 = 0x00000008;
const uint32_t OPT_SETMDN              = 0x00000010;
const uint32_t OPT_SETMSID             = 0x00000020;
const uint32_t OPT_SETMSL              = 0x00000040;
const uint32_t OPT_SETMIPACTIVEPROFILE = 0x00000080;
const uint32_t OPT_SETMIPNAI           = 0x00000100;
const uint32_t OPT_SETMIPHOMEADDR      = 0x00000200;
const uint32_t OPT_SETMIPPRIMARYHA     = 0x00000400;
const uint32_t OPT_SETMIPSECONDARYHA   = 0x00000800;
const uint32_t OPT_SETMIPMNAAASPI      = 0x00001000;
const uint32_t OPT_SETMIPMNHASPI       = 0x00002000;
const uint32_t OPT_SETMIPREVTUN        = 0x00004000;
const uint32_t OPT_SETMIPMNAAASS       = 0x00008000;
const uint32_t OPT_SETMIPMNHASS        = 0x00010000;
const uint32_t OPT_INIT_DC             = 0x00020000;
const uint32_t OPT_INIT_FUMO           = 0x00040000;
const uint32_t OPT_INIT_PRL            = 0x00080000;
const uint32_t OPT_INIT_RTN            = 0x00100000;
const uint32_t OPT_INIT_ACTIVATION     = 0x00200000;
const uint32_t OPT_MDN                 = 0x00400000;
const uint32_t OPT_MSID                = 0x00800000;
const uint32_t OPT_SETRXDIVERSITY      = 0x01000000;
const uint32_t OPT_SETACTIVEFIRMWARE   = 0x02000000;
const uint32_t OPT_SET_CELLULAR_MODE   = 0x08000000;
const uint32_t OPT_UNLOCK_SIM_CARD     = 0x20000000;
const uint32_t OPT_RESET_RADIO         = 0x40000000;

int32_t g_iAuxOptions = 0;
const uint32_t AOPT_OMA_DM_START       = 0x00000001;
const uint32_t AOPT_DFU_UPLOAD         = 0x00000002;
const uint32_t AOPT_DFU_APPLY          = 0x00000004;
const uint32_t AOPT_FUMO_LOCAL         = 0x00000008;
const uint32_t AOPT_SET_UE_MODE_OF_OPERATION = 0x00000010;
const uint32_t AOPT_DISABLE_VOICE_SUPPORT = 0x00000020;
const uint32_t AOPT_SET_PDP_CONF_STATIC   = 0x00000040;

void handle_sigterm(int signum);
void printHelp(const std::string& sApp);
void parseOptions(int argc, char** argv);
void initializeCache();
void shutdown();
const char *code2str(MTS::IO::ICellularRadio::CODE code);
std::string loadImeiFromDeviceInfo();
MTS::IO::ICellularRadio::CELLULAR_MODES cellularModeFlags(const std::string networks);


static int iOption = 0;
static struct option long_options[] = {
    { "help",                   no_argument,        0, '?' },
    { "version",                no_argument,        0, 'v' },
    { "printlvl",               required_argument,  0, 'p' },
    { "device",                 required_argument,  0, 'd' },
    { "set-mdn",                required_argument,  0, 'n' },
    { "set-msl",                required_argument,  0, 'm' },
    { "set-msid",               required_argument,  0, 's' },
    { "msl",                    required_argument,  0, 'l' },
    { "mdn",                    required_argument,  0, 'k' },
    { "msid",                   required_argument,  0, 'j' },
    { "set-active-firmware" ,   required_argument,  0, 'f' },
    { "set-rx-diversity",       required_argument,  0, 'r' },
    { "set-mip-active-profile", required_argument,  0, '0' },
    { "set-mip-nai",            required_argument,  0, '1' },
    { "set-mip-home-ip",        required_argument,  0, '2' },
    { "set-mip-primary-ha",     required_argument,  0, '3' },
    { "set-mip-secondary-ha",   required_argument,  0, '4' },
    { "set-mip-mn-aaa-spi",     required_argument,  0, '5' },
    { "set-mip-mn-ha-spi",      required_argument,  0, '6' },
    { "set-mip-rev-tun",        required_argument,  0, '7' },
    { "set-mip-mn-aaa-ss",      required_argument,  0, '8' },
    { "set-mip-mn-ha-ss",       required_argument,  0, '9' },
    { "unlock-sim-card",        required_argument,  0, OPT_UNLOCK_SIM_CARD },
    { "reset-radio",            no_argument,        &iOption, OPT_RESET_RADIO },
    { "init-dc",                no_argument,        &iOption, OPT_INIT_DC },
    { "init-fumo",              no_argument,        &iOption, OPT_INIT_FUMO },
    { "init-prl",               no_argument,        &iOption, OPT_INIT_PRL },
    { "init-activation",        no_argument,        &iOption, OPT_INIT_ACTIVATION },
    { "factory-default",        no_argument,        &iOption, OPT_INIT_RTN },
    { "set-cellular-mode",      required_argument,  0, 'w' },
    { "start-oma-dm",           no_argument,        0, 'o' },
    { "delta-fwu-upload",       required_argument,  0, 'x' },
    { "delta-fwu-apply",        no_argument,        0, 'A' },
    { "init-fumo-local",        required_argument,  0, 'F' },
    { "set-ue-mode-of-operation",required_argument, 0, 'U' },
    { "disable-voice-support",  no_argument,        0, 'V' },
    { "set-pdp-conf-static",    no_argument,        0, 'P' },
    { "ctx-id",                 required_argument,  0, 'C' },
    { "apn",                    required_argument,  0, 'a' },
    { "ipMode",                 required_argument,  0, 'i' },
    { "authType",               required_argument,  0, 'T' },
    { "username",               required_argument,  0, 'u' },
    { "password",               required_argument,  0, 'W' },
    { 0, 0, 0, 0 }
};

Json::Value getStaticData();
Json::Value getNetworkData();
MTS::IO::ICellularRadio::CODE handleDeltaFwUpload(const std::string& sPath);
MTS::IO::ICellularRadio::CODE handleFumoLocal(const std::string& sPath);

MTS::IO::ICellularRadio::UpdateCb cb = [](const Json::Value& s)->void {
    if(s.isString()) {
        printf("%s\n", s.asCString());
    }
};

int main(int argc, char** argv) {
    using namespace MTS::IO;

    //Disable stdout buffering so this process can pipe out data as needed
    // * otherwise entire stdout is buffered until process is closed using popen
    setbuf(stdout, NULL);

    signal(SIGTERM, handle_sigterm);
    signal(SIGINT, handle_sigterm);

    MTS::Logger::setPrintLevel(MTS::Logger::PrintLevel::OFF_LEVEL, true);

    parseOptions(argc, argv);

    if(!(g_iOptions & OPT_INITIALIZE)) {
        // get model from cache
        std::ifstream fModel((g_sCache + "/" + FMODEL).c_str());
        if(fModel.is_open()) {
            std::string sModel;
            fModel >> sModel;
            if(sModel.size() > 0) {
                printTrace("Pulling Model from cache: [%s]", sModel.c_str());
                g_sModel = sModel;
            }
        }
        // get msl from cache
        std::ifstream fMSL((g_sCache + "/" + FMSL).c_str());
        if(fMSL.is_open()) {
            std::string sMSL;
            fMSL >> sMSL;
            if(sMSL.size() > 0) {
                printTrace("Pulling Msl from cache: [%s]", sMSL.c_str());
                g_sMSL = sMSL;
                g_iOptions |= OPT_MSL;
            }
        }
    }

    MTS::IO::CellularRadioFactory factory;
    g_apRadio.reset(factory.create(g_sModel, g_sDevice));

    if(g_apRadio.isNull() || !g_apRadio->initialize()) {
        printf("Error creating radio interface [%s][%s]\n", g_sDevice.c_str(), g_sModel.c_str());
        return EXIT_FAILURE;
    }

    ICellularRadio::CODE result = ICellularRadio::CODE::SUCCESS;

    if(g_iOptions & OPT_COMMAND) {
        int32_t timeoutMillis = DEFAULT_TIMEOUT;
        if(g_iOptions & OPT_TIMEOUT) {
            timeoutMillis = atoi(g_sTimeout.c_str());
            if(!timeoutMillis) {
                printError("Timeout: invalid, use default timeout\n");
                timeoutMillis = DEFAULT_TIMEOUT;
            }
        }
        printDebug("AT Command: %s\n", g_sAtCommand.c_str());
        std::string sResult = g_apRadio->sendCommand(g_sAtCommand, MTS::IO::ICellularRadio::DEFAULT_BAIL_STRINGS, timeoutMillis);
        printDebug("RESULT: %s\n", sResult.c_str());
        // skip first line
        std::size_t pos = sResult.find('\n');
        if(pos == std::string::npos) {
            pos = 0;
        } else {
            ++pos;
        }
        printf("%s", sResult.substr(pos).c_str());
    } else if(g_iOptions & OPT_SETMDN) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MDN: %s\n", g_sMDN.c_str());
        jArgs["mdn"] = g_sMDN;
        if(g_iOptions & OPT_MSL) {
            printDebug("MSL: %s\n", g_sMSL.c_str());
            jArgs["msl"] = g_sMSL;
        }
        result = g_apRadio->setMdn(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMSID) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MSID: %s\n", g_sMSID.c_str());
        jArgs["msid"] = g_sMSID;
        if(g_iOptions & OPT_MSL) {
            printDebug("MSL: %s\n", g_sMSL.c_str());
            jArgs["msl"] = g_sMSL;
        }
        result = g_apRadio->setMsid(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPACTIVEPROFILE) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP ACTIVE PROFILE: %s\n", g_sMIPActiveProfile.c_str());
        jArgs["activeProfile"] = g_sMIPActiveProfile;
        result = g_apRadio->setMipActiveProfile(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPNAI) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP NAI: %s\n", g_sMIPNai.c_str());
        jArgs["nai"] = g_sMIPNai;
        result = g_apRadio->setMipNai(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPHOMEADDR) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP HOME ADDR: %s\n", g_sMIPHomeAddr.c_str());
        jArgs["homeIp"] = g_sMIPHomeAddr;
        result = g_apRadio->setMipHomeIp(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPPRIMARYHA) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP PRIMARY HA: %s\n", g_sMIPPrimaryHa.c_str());
        jArgs["primaryHa"] = g_sMIPPrimaryHa;
        result = g_apRadio->setMipPrimaryHa(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPSECONDARYHA) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP SECONDARY HA: %s\n", g_sMIPSecondaryHa.c_str());
        jArgs["secondaryHa"] = g_sMIPSecondaryHa;
        result = g_apRadio->setMipSecondaryHa(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPMNAAASPI) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP MN-AAA SPI: %s\n", g_sMIPMnAaaSpi.c_str());
        jArgs["mnAaaSpi"] = g_sMIPMnAaaSpi;
        result = g_apRadio->setMipMnAaaSpi(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPMNHASPI) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP MN-HA SPI: %s\n", g_sMIPMnHaSpi.c_str());
        jArgs["mnHaSpi"] = g_sMIPMnHaSpi;
        result = g_apRadio->setMipMnHaSpi(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPREVTUN) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP REV TUN: %s\n", g_sMIPRevTun.c_str());
        jArgs["revTun"] = g_sMIPRevTun;
        result = g_apRadio->setMipRevTun(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPMNAAASS) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP MN-AAA SS: %s\n", g_sMIPMnAaaSs.c_str());
        jArgs["mnAaaSs"] = g_sMIPMnAaaSs;
        result = g_apRadio->setMipMnAaaSs(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMIPMNHASS) {
        Json::Value jArgs(Json::objectValue);
        printDebug("SET MIP MN-HA SS: %s\n", g_sMIPMnHaSs.c_str());
        jArgs["mnHaSs"] = g_sMIPMnHaSs;
        result = g_apRadio->setMipMnHaSs(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETMSL) {
        Json::Value jArgs(Json::objectValue);
        jArgs["msl"] = g_sMSL;
        result = g_apRadio->validateMsl(jArgs);
        if(result == MTS::IO::ICellularRadio::CODE::SUCCESS) {
            std::ofstream fMSL((g_sCache + "/" + FMSL).c_str(), std::ios_base::trunc | std::ios_base::out);
            if(fMSL.is_open()) {
                std::string sMSL;
                fMSL << g_sMSL;
            }
        }
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_INIT_DC) {
        Json::Value jArgs(Json::nullValue);
        result = g_apRadio->updateDc(jArgs, cb);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_INIT_PRL) {
        Json::Value jArgs(Json::nullValue);
        result = g_apRadio->updatePrl(jArgs, cb);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_INIT_FUMO) {
        Json::Value jArgs(Json::nullValue);
        result = g_apRadio->updateFumo(jArgs, cb);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_INIT_RTN) {
        Json::Value jArgs(Json::objectValue);
        if(g_iOptions & OPT_MSL) {
            printDebug("MSL: %s\n", g_sMSL.c_str());
            jArgs["msl"] = g_sMSL;
        }
        result = g_apRadio->resetHfa(jArgs, cb);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_INIT_ACTIVATION) {
        Json::Value jArgs(Json::objectValue);
        if(g_iOptions & OPT_MSID) {
            printDebug("MSID: %s", g_sMSID.c_str());
            jArgs["msid"] = g_sMSID;
        }
        if(g_iOptions & OPT_MDN) {
            printDebug("MDN: %s", g_sMDN.c_str());
            jArgs["mdn"] = g_sMDN;
        }
        result = g_apRadio->activate(jArgs, cb);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETRXDIVERSITY) {
        Json::Value jArgs(Json::objectValue);
        jArgs["enabled"] = g_sRxDiversity;
        result = g_apRadio->setRxDiversity(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SETACTIVEFIRMWARE) {
        Json::Value jArgs(Json::objectValue);
        jArgs["fwid"] = g_sActiveFirmware;
        result = g_apRadio->setActiveFirmware(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_UNLOCK_SIM_CARD) {
        Json::Value jArgs(Json::objectValue);
        jArgs["pin"] = g_sSimPin;
        result = g_apRadio->unlockSimCard(jArgs);
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_RESET_RADIO) {
        bool ret = g_apRadio->resetRadio();
        result = (ret) ? ICellularRadio::CODE::SUCCESS : ICellularRadio::CODE::FAILURE;
        printf("%s\n", code2str(result));
    } else if(g_iOptions & OPT_SET_CELLULAR_MODE) {
        ICellularRadio::CELLULAR_MODES networks = cellularModeFlags(g_sCellularMode);
        if (networks != ICellularRadio::CELLULAR_MODE_NA) {
            result = g_apRadio->setCellularMode(networks);
            printf("%s\n", code2str(result));
        } else {
            printf("Invalid argument: %s\n", g_sCellularMode.c_str());
        }
    } else if (g_iAuxOptions & AOPT_OMA_DM_START) {
        result = g_apRadio->startOmaDm(cb);
    } else if (g_iAuxOptions & AOPT_DFU_UPLOAD) {
        result = handleDeltaFwUpload(g_sDeltaFwPath);
    } else if (g_iAuxOptions & AOPT_DFU_APPLY) {
        result = g_apRadio->fumoLocalApply(cb);
    } else if (g_iAuxOptions & AOPT_FUMO_LOCAL) {
        result = handleFumoLocal(g_sDeltaFwPath);
    } else if (g_iAuxOptions & AOPT_SET_UE_MODE_OF_OPERATION) {
        ICellularRadio::UE_MODES_OF_OPERATION mode;
        result = ICellularRadio::convertStringToUeMode(g_sUeModeOfOperation, mode);
        if (result == ICellularRadio::CODE::SUCCESS) {
            result = g_apRadio->setUeModeOfOperation(mode);
            printf("%s\n", code2str(result));
        } else {
            printf("Invalid argument: %s\n", g_sUeModeOfOperation.c_str());
        }
    } else if (g_iAuxOptions & AOPT_DISABLE_VOICE_SUPPORT) {
        result = g_apRadio->disableVoiceSupport();
        printf("%s\n", code2str(result));
    } else if (g_iAuxOptions & AOPT_SET_PDP_CONF_STATIC) {
        result = g_apRadio->setPdpContext(g_sPdpId, g_jPdpConfig);
        printf("%s\n", code2str(result));
    }

    shutdown();

    return (result == ICellularRadio::CODE::SUCCESS) ? 0 : 1;
}

void handle_sigterm(int signum) {
    if(signum == SIGTERM) {
        shutdown();
    }
}

void shutdown() {
    printDebug("Shutting Down");
    if(!g_apRadio.isNull()) {
        g_apRadio->shutdown();
    }
}

void parseOptions(int argc, char** argv)
{
    if(argc == 1) {
        printHelp(argv[0]);
        exit(0);
    }

    while (true) {
        int c = getopt_long(argc, argv, "t:d:p:r:?vC:", long_options, nullptr);

        /* Detect the end of the options. */
        if(c == -1) {
            break;
        }

        if(c == 0) {
            g_iOptions |= iOption;
            continue;
        }

        switch (c) {

            case '?':
                printHelp(argv[0]);
                exit(0);
                break;

            case 'v':
                printf("Version: %s\n", Version::version.c_str());
                exit(0);
                break;

            case 'p':
                int iPrintLvl;
                if(MTS::Text::parse(iPrintLvl, optarg) && iPrintLvl >= 0 && iPrintLvl <= 100) {
                    MTS::Logger::setPrintLevel(iPrintLvl);
                } else {
                    printf("print level out of range [0-100]\n");
                    exit(1);
                }
                break;

            case 'r':
                if(optarg != 0) {
                    g_sRxDiversity = optarg;
                }
                g_iOptions |= OPT_SETRXDIVERSITY;
                break;

            case 'f':
                if(optarg != 0) {
                    g_sActiveFirmware = optarg;
                }
                g_iOptions |= OPT_SETACTIVEFIRMWARE;
                break;

            case 't':
                if(optarg != 0) {
                    g_sTimeout = optarg;
                }
                g_iOptions |= OPT_TIMEOUT;
                break;

            case 'd':
                if(optarg != 0) {
                    g_sDevice = optarg;
                }
                g_iOptions |= OPT_DEVICE;
                break;

            case 'n':
                if(optarg != 0) {
                    g_sMDN = optarg;
                }
                g_iOptions |= OPT_SETMDN;
                break;

            case 's':
                if(optarg != 0) {
                    g_sMSID = optarg;
                }
                g_iOptions |= OPT_SETMSID;
                break;

            case 'm':
                if(optarg != 0) {
                    g_sMSL = optarg;
                }
                g_iOptions |= OPT_SETMSL;
                break;

            case 'l':
                if(optarg != 0) {
                    g_sMSL = optarg;
                }
                g_iOptions |= OPT_MSL;
                break;

            case 'k':
                if(optarg != 0) {
                    g_sMDN = optarg;
                }
                g_iOptions |= OPT_MDN;
                break;
            case 'j':
                if(optarg != 0) {
                    g_sMSID = optarg;
                }
                g_iOptions |= OPT_MSID;
                break;
            case '0':
                if(optarg != 0) {
                    g_sMIPActiveProfile = optarg;
                }
                g_iOptions |= OPT_SETMIPACTIVEPROFILE;
                break;

            case '1':
                if(optarg != 0) {
                    g_sMIPNai = optarg;
                }
                g_iOptions |= OPT_SETMIPNAI;
                break;

            case '2':
                if(optarg != 0) {
                    g_sMIPHomeAddr = optarg;
                }
                g_iOptions |= OPT_SETMIPHOMEADDR;
                break;

            case '3':
                if(optarg != 0) {
                    g_sMIPPrimaryHa = optarg;
                }
                g_iOptions |= OPT_SETMIPPRIMARYHA;
                break;

            case '4':
                if(optarg != 0) {
                    g_sMIPSecondaryHa = optarg;
                }
                g_iOptions |= OPT_SETMIPSECONDARYHA;
                break;

            case '5':
                if(optarg != 0) {
                    g_sMIPMnAaaSpi = optarg;
                }
                g_iOptions |= OPT_SETMIPMNAAASPI;
                break;

            case '6':
                if(optarg != 0) {
                    g_sMIPMnHaSpi = optarg;
                }
                g_iOptions |= OPT_SETMIPMNHASPI;
                break;

            case '7':
                if(optarg != 0) {
                    g_sMIPRevTun = optarg;
                }
                g_iOptions |= OPT_SETMIPREVTUN;
                break;

            case '8':
                if(optarg != 0) {
                    g_sMIPMnAaaSs = optarg;
                }
                g_iOptions |= OPT_SETMIPMNAAASS;
                break;

            case '9':
                if(optarg != 0) {
                    g_sMIPMnHaSs = optarg;
                }
                g_iOptions |= OPT_SETMIPMNHASS;
                break;

            case OPT_UNLOCK_SIM_CARD:
                if(optarg != 0) {
                    g_sSimPin = optarg;
                }
                g_iOptions |= OPT_UNLOCK_SIM_CARD;
                break;

            case 'w':
                if (optarg)
                    g_sCellularMode = optarg;
                g_iOptions |= OPT_SET_CELLULAR_MODE;
                break;

            case 'o':
                g_iAuxOptions |= AOPT_OMA_DM_START;
                break;

            case 'x':
                if (optarg)
                    g_sDeltaFwPath = optarg;
                g_iAuxOptions |= AOPT_DFU_UPLOAD;
                break;

            case 'A':
                g_iAuxOptions |= AOPT_DFU_APPLY;
                break;

            case 'F':
                if (optarg)
                    g_sDeltaFwPath = optarg;
                g_iAuxOptions |= AOPT_FUMO_LOCAL;
                break;

            case 'U':
                if (optarg)
                    g_sUeModeOfOperation = optarg;
                g_iAuxOptions |= AOPT_SET_UE_MODE_OF_OPERATION;
                break;

            case 'V':
                g_iAuxOptions |= AOPT_DISABLE_VOICE_SUPPORT;
                break;

            case 'P':
                g_iAuxOptions |= AOPT_SET_PDP_CONF_STATIC;
                break;

            case 'C':
                if(optarg != 0) {
                    g_sPdpId = optarg;
                }
                break;

            case 'a':
                if(optarg != 0) {
                    g_jPdpConfig[MTS::IO::ICellularRadio::KEY_PDP_CONTEXT_APN] = optarg;
                }
                break;

            case 'i':
                if(optarg != 0) {
                    g_jPdpConfig[MTS::IO::ICellularRadio::KEY_PDP_CONTEXT_IPMODE] = optarg;
                }
                break;
            
            case 'T':
                if(optarg != 0) {
                    g_jPdpConfig[MTS::IO::ICellularRadio::KEY_PDP_CONTEXT_AUTH_TYPE] = optarg;
                }
                break;
            
            case 'u':
                if(optarg != 0) {
                    g_jPdpConfig[MTS::IO::ICellularRadio::KEY_PDP_CONTEXT_AUTH_USERNAME] = optarg;
                }
                break;

            case 'W':
                if(optarg != 0) {
                    g_jPdpConfig[MTS::IO::ICellularRadio::KEY_PDP_CONTEXT_AUTH_PASSWORD] = optarg;
                }
                break;

            default:
                printf("OPTION: [%d] ABORTING!!\n", c);
                abort();
                break;
        }
    }

    if((g_iOptions & (OPT_TIMEOUT | OPT_DEVICE)) || !g_iOptions) {
        /* Assume that any remaining command line arguments is AT Command */
        if(optind < argc) {
            g_sAtCommand = argv[optind];
            g_iOptions |= OPT_COMMAND;
        }
    }

    printDebug("OPTS: %08X\n", g_iOptions);

}

void printHelp(const std::string& sApp) {
    printf("Usage: %s [OPTION] [<AT_COMMAND>] ...\n", sApp.c_str());
    printf("\tSimple utility to send either general AT command or predefined specific command to radio module\n");
    printf("\t<AT_COMMAND> [ -t <TIMEOUT> -d <DEVICE> ] : send AT command. Timeout and device are optional\n");
    printf("\t--device (d) <device>                     : modem device to use, default: /dev/modem_at1\n");
    printf("\t--init-dc                                 : initiate device configuration update\n");
    printf("\t--init-fumo                               : initiate module/modem firmware update\n");
    printf("\t--init-prl                                : initiate PRL update\n");
    printf("\t--init-activation [ --msid <MSID> --mdn <MDN>]\n");
    printf("\t                                          : initiate account activation.  Some carriers require MDN and MSID\n");
    printf("\n");
    printf("\t--set-mdn <MDN> [ --msl <MSL> ]           : set MDN value. Some carriers require MSL\n");
    printf("\t--set-msid <MSID> [ --msl <MSL> ]         : set MSID value. Some carriers require MSL\n");
    printf("\t--set-msl <MSL>                           : validate and save Master Subsidy Lock (MSL) in cache\n");
    printf("\t--set-mip-active-profile <VAL>            : set active MIP profile\n");
    printf("\t--set-mip-nai <VAL>                       : set MIP Network Access Identifier\n");
    printf("\t--set-mip-home-ip <VAL>                   : set MIP Home Address\n");
    printf("\t--set-mip-primary-ha <VAL>                : set MIP Primary Home Agent\n");
    printf("\t--set-mip-secondary-ha <VAL>              : set MIP Secondary Home Agent\n");
    printf("\t--set-mip-mn-aaa-spi <VAL>                : set MIP MN AAA SPI\n");
    printf("\t--set-mip-mn-ha-spi <VAL>                 : set MIP MN HA SPI\n");
    printf("\t--set-mip-rev-tun <VAL>                   : set MIP Reverse Tunneling\n");
    printf("\t--set-mip-mn-aaa-ss <VAL>                 : set MIP MN AAA SS\n");
    printf("\t--set-mip-mn-ha-ss <VAL>                  : set MIP MN HA SS\n");
    printf("\t--set-rx-diversity <VAL>                  : set RX Diversity\n");
    printf("\t--set-cellular-mode                       : set preferred networks eg. 2g,3g,4g\n");
    printf("\t--set-ue-mode-of-operation <MODE>         : set the UE mode of operation: ps_1, ps_2, csps_1, csps_2\n");
    printf("\t--disable-voice-support                   : disable support of voice calls (selected radios only)\n");
    // Applicable for LTE910-NA1 dual FW images only
    // printf("\t--set-active-firmware <VAL>               : switch to a specific firmware image\n");
    printf("\t--factory-default [ --msl <MSL> ]         : reset to factory defaults\n");
    printf("\t--unlock-sim-card <SIM PIN>               : unlock the SIM card using the PIN code provided\n");
    printf("\t--reset-radio                             : reset the radio module using AT commands\n");
    printf("\t--start-oma-dm                            : start the OMA DM procedure (selected radios and networks only)\n");
    printf("\t--init-fumo-local <FILE_PATH>             : perform the radio firmware upgrade using the provided image file\n");
    printf("\t--set-pdp-conf-static                     : set the specified PDP context\n");
    printf("\t    --ctx-id <ID>                         : specify a Context ID for the set-pdp-conf-static command\n");
    printf("\t    --apn <APN>                           : specify an APN for the set-pdp-conf-static command\n");
    printf("\t    --ipMode <MODE>                       : specify an IP Mode for the set-pdp-conf-static command\n");
    printf("\t    --authType <TYPE>                     : specify an authentication type for the set-pdp-conf-static: NONE, PAP, CHAP, PAP-CHAP\n");
    printf("\t    --username <USERNAME>                 : specify an authentication username for the set-pdp-conf-static\n");
    printf("\t    --password <PASSWORD>                 : specify an authentication password for the set-pdp-conf-static\n");
    // Undocumented: use for debugging and testing purposes
    // printf("\t--delta-fwu-upload <FILE_PATH>            : upload a delta radio FWU file to the radio\n");
    // printf("\t--delta-fwu-apply                         : apply the delta radio FWU image that was already uploaded to the radio\n");
    printf("\n");
    printf("\t--printlvl (p) <level>                    : sets the printlvl [0-100]\n");
    printf("\t--version (v)                             : returns version\n");
    printf("\t--help (?)                                : returns this message\n");
    printf("\n");
    printf("\tSupported Radios:\n");
    printf("\t\tLE910, HE910, GE910, DE910, CE910, ME910, EG95, EG25\n");
}

const char *code2str(MTS::IO::ICellularRadio::CODE code) {
    switch (code) {
        case MTS::IO::ICellularRadio::CODE::SUCCESS:
            return "Success";
        case MTS::IO::ICellularRadio::CODE::ERROR:
            return "Error";
        case MTS::IO::ICellularRadio::CODE::FAILURE:
            return "Failure";
        case MTS::IO::ICellularRadio::CODE::NO_RESPONSE:
            return "No Response";
        case MTS::IO::ICellularRadio::CODE::NOT_APPLICABLE:
            return "Not Applicable";
        case MTS::IO::ICellularRadio::CODE::INVALID_ARGS:
            return "Invalid Arguments";
    }
    return "Unknown";
}

MTS::IO::ICellularRadio::CELLULAR_MODES cellularModeFlags(const std::string networks)
{
    using namespace MTS::IO;
    int result = ICellularRadio::CELLULAR_MODE_NA;
    const std::vector<std::string> &items = MTS::Text::split(networks, ",");
    for (const auto &it: items) {
        if (it == "2g") {
            result |= ICellularRadio::CELLULAR_MODE_2G;
        } else if (it == "3g") {
            result |= ICellularRadio::CELLULAR_MODE_3G;
        } else if (it == "4g") {
            result |= ICellularRadio::CELLULAR_MODE_4G;
        } else if (it == "5g") {
            result |= ICellularRadio::CELLULAR_MODE_5G;
        } else {
            result = ICellularRadio::CELLULAR_MODE_NA;
            break;
        }
    }
    return static_cast<ICellularRadio::CELLULAR_MODES>(result);
}

MTS::IO::ICellularRadio::CODE handleDeltaFwUpload(const std::string& sPath) {
    int fd = open(sPath.c_str(), O_RDONLY);
    if (fd < 0) {
        printf("Failed to open file [%s]: %d\n", sPath.c_str(), errno);
        return MTS::IO::ICellularRadio::CODE::ERROR;
    }

    auto rc = g_apRadio->fumoLocalInject(fd, cb);
    close(fd);

    return rc;
}


MTS::IO::ICellularRadio::CODE handleFumoLocal(const std::string& sPath) {
    int fd = open(sPath.c_str(), O_RDONLY);
    if (fd < 0) {
        printf("FUMO Error: failed to open file [%s]: %d\n", sPath.c_str(), errno);
        return MTS::IO::ICellularRadio::CODE::ERROR;
    }

    auto rc = g_apRadio->updateFumoLocal(fd, cb);
    close(fd);

    return rc;
}