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
|
/*
* 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 <fstream>
#include <mts/MTS_Text.h>
#include <mts/MTS_Logger.h>
#include <mts/MTS_Thread.h>
#include <mts/MTS_Timer.h>
#include <mts/MTS_IO_ME910C1WWRadio.h>
using namespace MTS::IO;
const std::string ME910C1WWRadio::MODEL_NAME("ME910C1-WW");
const std::string ME910C1WWRadio::KEY_FUMO_PDPID("pdpid"); // optional (default : "3")
const std::string ME910C1WWRadio::KEY_FUMO_PDPTYPE("pdptype"); // optional (default : "IPV4V6")
const std::string ME910C1WWRadio::KEY_FUMO_APN("apn"); // optional (default : "")
const std::string ME910C1WWRadio::KEY_FUMO_ADDRESS("address");
const std::string ME910C1WWRadio::KEY_FUMO_DIR("dir");
const std::string ME910C1WWRadio::KEY_FUMO_FILE("file");
const std::string ME910C1WWRadio::KEY_FUMO_USER("user");
const std::string ME910C1WWRadio::KEY_FUMO_PASSWORD("password");
const std::string ME910C1WWRadio::KEY_FUMO_DRYRUN("dryrun");
ME910C1WWRadio::ME910C1WWRadio(const std::string& sPort)
: ME910Radio(MODEL_NAME, sPort)
{
}
ICellularRadio::CODE ME910C1WWRadio::setActiveFirmware(const Json::Value& jArgs) {
ICellularRadio::CODE rc;
// Set command allows enabling a specific firmware image on products
// embedding 2 different firmware images:
//
// "AT#FWSWITCH=<image_number>[,<storage_conf>]"
// <image_number> - Firmware Image To Be Enabled
// 0 – Image 1 (Default)
// 1 – Image 2
// <storage_conf> - Setting Storage Configuration
// 0 – Save the <image_number> value in RAM
// 1 – Save the <image_number> value in NVM
printTrace("%s| Set Active Firmware Image Number", getName().c_str());
if(!jArgs["fwid"].isString()) {
return INVALID_ARGS;
}
// ME910C1-WW uses code "2" for World-Wide mode
if (jArgs["fwid"].asString() != "2" && jArgs["fwid"].asString() != "1" && jArgs["fwid"].asString() != "0") {
return INVALID_ARGS;
}
// Test command to check if firmware switch is supported
rc = sendBasicCommand("AT#FWSWITCH=?");
if (rc == ERROR) {
printTrace("%s| FWSWITCH is not supported", getName().c_str());
return NOT_APPLICABLE;
}
else if (rc != SUCCESS) {
return rc;
}
std::string sCmd = "AT#FWSWITCH=";
sCmd += jArgs["fwid"].asString();
sCmd += ",1";
printTrace("%s| Issuing %s command", getName().c_str(), sCmd.c_str());
return sendBasicCommand(sCmd, 5000);
}
ICellularRadio::CODE ME910C1WWRadio::getActiveFirmware(std::string& sFwId) {
std::string sCmd;
ICellularRadio::CODE rc;
//
// Read command reports the current active firmware image:
// AT#FWSWITCH?
// #FWSWITCH: 1
//
// OK
//
printTrace("%s| Get Active Firmware Image Number", getName().c_str());
// Test command to check if firmware switch is supported
sCmd = "AT#FWSWITCH=?";
rc = sendBasicCommand(sCmd);
if (rc == ERROR) {
printTrace("%s| FWSWITCH is not supported", getName().c_str());
return NOT_APPLICABLE;
}
else if (rc != SUCCESS) {
return rc;
}
sCmd = "AT#FWSWITCH?";
std::string sResult = sendCommand(sCmd);
size_t end = sResult.find(ICellularRadio::RSP_OK);
if (end == std::string::npos) {
printWarning("%s| Unable to get active image number from radio using command [%s]",
getName().c_str(),
sCmd.c_str());
return FAILURE;
}
size_t start = sResult.find("#FWSWITCH:") + sizeof("#FWSWITCH:");
sFwId = MTS::Text::trim(sResult.substr(start, end-start));
if(sFwId.size() == 0) {
printWarning("%s| Firmware Image Number is empty", getName().c_str());
return FAILURE;
}
return SUCCESS;
}
ICellularRadio::CODE ME910C1WWRadio::doGetFirmwareNumbers(std::string &sFirmware, std::string &sFirmwareBuild) {
ICellularRadio::CODE rc = FAILURE;
rc = getFirmware(sFirmware);
if (rc != SUCCESS){
return rc;
}
rc = getFirmwareBuild(sFirmwareBuild);
if (rc != SUCCESS){
return rc;
}
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoReadConfig(const Json::Value& jArgs, Json::Value &jConfig) {
ICellularRadio::CODE rc = INVALID_ARGS;
std::string sPath;
do
{
if (!jArgs["config-file"].isString()) {
rc = INVALID_ARGS;
break;
}
sPath = jArgs["config-file"].asString();
std::ifstream file(sPath.c_str());
if (!file.is_open()) {
printError("Failed to open file [%s]", sPath.c_str());
break;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
std::string buffer(size, ' ');
file.seekg(0);
file.read(&buffer[0], size);
file.close();
Json::Features features = Json::Features::strictMode();
Json::Reader reader(features);
if (!reader.parse(buffer, jConfig)) {
printError("Error parsing FOTA configuration file");
break;
}
//
// set default values if missing
//
if (!jConfig.isMember(KEY_FUMO_PDPID)) {
jConfig[KEY_FUMO_PDPID] = std::string("3");
}
if (!jConfig.isMember(KEY_FUMO_PDPTYPE)) {
jConfig[KEY_FUMO_PDPTYPE] = std::string("IPV4V6");
}
if (!jConfig.isMember(KEY_FUMO_APN)) {
jConfig[KEY_FUMO_APN] = std::string("");
}
//
// validate
//
if (!jConfig[KEY_FUMO_PDPID].isString()) {
printError("Error loading FOTA configuration: PDP context id is not set");
break;
}
if (jConfig[KEY_FUMO_PDPID].asString().empty()) {
printError("Error loading FOTA configuration: context id is empty");
break;
}
if (!jConfig[KEY_FUMO_PDPTYPE].isString()) {
printError("Error loading FOTA configuration: PDP type is not set");
break;
}
if (jConfig[KEY_FUMO_PDPTYPE].asString().empty()) {
printError("Error loading FOTA configuration: PDP type is empty");
break;
}
// Note : allow empty APN
if (!jConfig[KEY_FUMO_APN].isString()) {
printError("Error loading FOTA configuration: APN is not set");
break;
}
if (!jConfig[KEY_FUMO_ADDRESS].isString()) {
printError("Error loading FOTA configuration: address is not set");
break;
}
if (jConfig[KEY_FUMO_ADDRESS].asString().empty()) {
printError("Error loading FOTA configuration: address is empty");
break;
}
// Note: allow empty dir
if (!jConfig[KEY_FUMO_DIR].isString()) {
printError("Error loading FOTA configuration: directory is not set");
break;
}
if (!jConfig[KEY_FUMO_FILE].isString()) {
printError("Error loading FOTA configuration: filename is not set");
break;
}
if (jConfig[KEY_FUMO_FILE].asString().empty()) {
printError("Error loading FOTA configuration: filename is empty");
break;
}
// Note: allow empty username/password
if (!jConfig[KEY_FUMO_USER].isString()) {
printError("Error loading FOTA configuration: username is not set");
break;
}
if (!jConfig[KEY_FUMO_PASSWORD].isString()) {
printError("Error loading FOTA configuration: password is not set");
break;
}
rc = SUCCESS;
}
while(0);
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoSetup(const Json::Value &jConfig, UpdateCb& stepCb) {
ICellularRadio::CODE rc = FAILURE;
std::string sCmd;
std::string sContextId = jConfig[KEY_FUMO_PDPID].asString();
std::string sApn = jConfig[KEY_FUMO_APN].asString();
std::string sPdpType = jConfig[KEY_FUMO_PDPTYPE].asString();
do
{
//
// Execution command is used to activate or deactivate either the GSM
// context or the specified PDP context.
//
// AT#SGACT=<cid>,<stat>[,<userId>,<pwd>]
//
sCmd = "AT#SGACT=" + sContextId + ",0";
rc = sendBasicCommand(sCmd);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to deactivate PDP context"));
}
break;
}
//
// Read current Firmware numbers (let it be after AT#SGACT not to confuse users)
//
rc = doGetFirmwareNumbers(m_sFw, m_sFwBuild);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to obtain current firmware version"));
}
break;
}
//
// Set command specifies PDP context parameter values for a PDP context identified by
// the (local) context identification parameter <cid>.
//
// AT+CGDCONT= [<cid>[,<PDP_type>[,<APN>[,<PDP_addr>[,<d_comp>[,<h_comp>[,<pd1>[,...[,pdN]]]]]]]]]
//
sCmd = "AT+CGDCONT=" + sContextId + ",\"" + sPdpType + "\"";
if (!sApn.empty()) {
sCmd += ",\"" + sApn + "\"";
}
rc = sendBasicCommand(sCmd, 1000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to setup PDP context"));
}
break;
}
//
// Set command sets the socket configuration parameters.
//
// AT#SCFG==<connId>,<cid>,<pktSz>,<maxTo>,<connTo>,<txTo>
// <connId> - socket connection identifier
// <cid> - PDP context identifier
// <pktSz> - packet size to be used by the TCP/UDP/IP stack for data sending.
// <maxTo> - exchange timeout (or socket inactivity timeout); if there’s no
// data exchange within this timeout period the connection is closed (timeout value in seconds).
// <connTo> - connection timeout; if we can’t establish a connection to the
// remote within this timeout period, an error is raised timeout value in hundreds of milliseconds.
// <txTo> - data sending timeout; after this period data are sent also if they’re
// less than max packet size (timeout value in hundreds of milliseconds).
//
sCmd = "AT#SCFG=1," + sContextId + ",300,90,600,50";
rc = sendBasicCommand(sCmd);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to set connection configuration parameters"));
}
break;
}
//
// Activate PDP context
//
sCmd = "AT#SGACT=" + sContextId + ",1";
rc = sendBasicCommand(sCmd, 60 * 1000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to activate PDP context"));
}
break;
}
}
while (0);
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoFtp(const Json::Value &jConfig, UpdateCb& stepCb) {
ICellularRadio::CODE rc = FAILURE;
std::string sCmd;
std::string sResult;
//
// Set command sets the time-out used when opening either the FTP control
// channel or the FTP traffic channel.
//
// AT#FTPTO= [<tout>]
// <tout> - time-out in 100 ms units
//
rc = sendBasicCommand("AT#FTPTO=2400");
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to setup connection timeout"));
}
return rc;
}
//
// Execution command opens an FTP connection toward the FTP server.
//
// AT#FTPOPEN=[<server:port>,<username>,<password>[,<mode>]]
//
sCmd = "AT#FTPOPEN=";
sCmd += "\"" + jConfig[KEY_FUMO_ADDRESS].asString() + "\",";
sCmd += "\"" + jConfig[KEY_FUMO_USER].asString() + "\",";
sCmd += "\"" + jConfig[KEY_FUMO_PASSWORD].asString() + "\",1";
rc = sendBasicCommand(sCmd, 60 * 1000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to open connection"));
}
return rc;
}
if (stepCb) {
stepCb(Json::Value("FUMO Info: connection opened"));
}
do
{
//
// Set command, issued during an FTP connection, sets the file transfer type.
//
// AT#FTPTYPE=[<type>]
// <type> - file transfer type:
// 0 - binary
// 1 - ascii
//
rc = sendBasicCommand("AT#FTPTYPE=0", 1000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to set file transfer type"));
}
break;
}
//
// Execution command, issued during an FTP connection, changes the
// working directory on FTP server.
//
// AT#FTPCWD=[<dirname>]
//
sCmd = "AT#FTPCWD=\"/";
if (!jConfig[KEY_FUMO_DIR].asString().empty()) {
sCmd += jConfig[KEY_FUMO_DIR].asString() + "/";
}
sCmd += "\"";
rc = sendBasicCommand(sCmd, 60 * 1000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to change working directory on the server"));
}
break;
}
if (stepCb) {
stepCb(Json::Value("FUMO Info: downloading the firmware"));
}
//
// Start FTP transfer
//
sCmd = "AT#FTPGETOTA=";
sCmd += "\"" + jConfig[KEY_FUMO_FILE].asString() + "\",1,1";
sendBasicCommand(sCmd);
//
// Noticed that after successful AT#FTPGETOTA the radio resets the connection.
// and the response code (OK, ERROR.. ) not always reach the host. Therefore
// we send the AT#FTPGETOTA with relatively small timeout and then poll with AT
// until we get valid response. After that, using AT#FTPMSG we can check the
// result of the last FTP command (which is AT#FTPGETOTA in our case).
MTS::Timer oTimer;
oTimer.start();
while (oTimer.getSeconds() < (30 * 60)) // 30 min
{
MTS::Thread::sleep(5000);
rc = sendBasicCommand("AT");
if (rc == SUCCESS) {
break;
}
resetConnection(1);
}
oTimer.stop();
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: unable to obtain radio after reset"));
}
break;
}
//
// Now check the FTP status
//
std::string sResult = sendCommand("AT#FTPMSG");
printTrace("RADIO| AT#FTPMSG result [%s]", sResult.c_str());
if (sResult.find(ICellularRadio::RSP_OK) == std::string::npos) {
rc = FAILURE;
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to download the firmware file"));
}
break;
}
if (sResult.find("#FTPMSG: 550") != std::string::npos) {
// FTP(550) : File not found
rc = FAILURE;
if(stepCb) {
stepCb(Json::Value("FUMO Error: file not found"));
}
break;
}
if (sResult.find("#FTPMSG: 226") == std::string::npos) {
// FTP(226) : Successfully transferred
rc = FAILURE;
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to download the firmware file"));
}
break;
}
}
while (0);
//
// Execution command closes an FTP connection.
//
// AT#FTPCLOSE
//
ICellularRadio::CODE rcclose = sendBasicCommand("AT#FTPCLOSE", 60 * 1000);
if (rcclose != SUCCESS && rc == SUCCESS) {
if(stepCb) {
// Only one "FUMO Error" message should be sent
stepCb(Json::Value("FUMO Error: Failed to close FTP connection"));
}
rc = rcclose;
}
if (rc == SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Info: firmware downloaded successfully"));
}
}
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoCleanup(const Json::Value &jConfig, UpdateCb& stepCb) {
ICellularRadio::CODE rc = FAILURE;
std::string sCmd;
std::string sContextId = jConfig[KEY_FUMO_PDPID].asString();
//
// Deactivate PDP context
//
sCmd = "AT#SGACT=" + sContextId + ",0";
rc = sendBasicCommand(sCmd, 10000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: Failed to deactivate PDP context"));
}
}
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoApplyFirmware(const Json::Value &jConfig, UpdateCb& stepCb) {
ICellularRadio::CODE rc = FAILURE;
if (jConfig.isMember(KEY_FUMO_DRYRUN)) {
if(stepCb) {
stepCb(Json::Value("FUMO Info: applying the radio firmware"));
}
return SUCCESS;
}
rc = sendBasicCommand("AT#OTAUP=0", 10000);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to apply the firmware"));
}
return rc;
}
if(stepCb) {
stepCb(Json::Value("FUMO Info: applying the radio firmware"));
}
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoWaitNewFirmware(const Json::Value &jConfig, UpdateCb& stepCb) {
std::string sFirmware;
std::string sFirmwareBuild;
ICellularRadio::CODE rc = FAILURE;
if (jConfig.isMember(KEY_FUMO_DRYRUN)) {
if(stepCb) {
stepCb(Json::Value("FUMO done: radio firmware applied successfully"));
}
return SUCCESS;
}
// The radio is expected to send "#OTAEV: Module Upgraded To New Fw" unsolicited message
// on success. However, for some reason, we do not see this message.
MTS::Timer oTimer;
oTimer.start();
while (oTimer.getSeconds() < (5 * 60)) { // 5 minutes
MTS::Thread::sleep(10000);
if (doGetFirmwareNumbers(sFirmware, sFirmwareBuild) != SUCCESS) {
// The radio is probably unavailable
resetConnection(100);
continue;
}
if (sFirmware == m_sFw && sFirmwareBuild == m_sFwBuild) {
// Have the same firmware. The radio resets several time
// before the firmware is actually get upgraded. So keep polling.
continue;
}
// The firmware numbers have changed
rc = SUCCESS;
break;
}
oTimer.stop();
if (rc == SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO done: radio firmware applied successfully"));
}
}
else {
if(stepCb) {
stepCb(Json::Value("FUMO error: radio firmware has not been updated"));
}
}
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::doFumoPerform(const Json::Value &jConfig, UpdateCb& stepCb) {
ICellularRadio::CODE rc = FAILURE;
UpdateCb dummyCb;
// Set the PDP context for the FOTA
rc = doFumoSetup(jConfig, stepCb);
if (rc != SUCCESS) {
return rc;
}
// Download FW over FTP
rc = doFumoFtp(jConfig, stepCb);
if (rc != SUCCESS) {
doFumoCleanup(jConfig, dummyCb);
return rc;
}
// Clean up before applying the FW file
rc = doFumoCleanup(jConfig, stepCb);
if (rc != SUCCESS) {
return rc;
}
// Apply the FW file
rc = doFumoApplyFirmware(jConfig, stepCb);
if (rc != SUCCESS) {
return rc;
}
rc = doFumoWaitNewFirmware(jConfig, stepCb);
return rc;
}
ICellularRadio::CODE ME910C1WWRadio::updateFumo(const Json::Value& jArgs, UpdateCb& stepCb) {
Json::Value jConfig(Json::objectValue);
ICellularRadio::CODE rc = FAILURE;
std::string sFwId;
do
{
rc = getActiveFirmware(sFwId);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: failed to obtain current active firmware id"));
}
break;
}
// For Verizon Only
if (sFwId != "1") {
if(stepCb) {
stepCb(Json::Value("FUMO Error: fumo is not supported"));
}
break;
}
rc = doFumoReadConfig(jArgs, jConfig);
if (rc != SUCCESS) {
if(stepCb) {
stepCb(Json::Value("FUMO Error: bad configuration parameters"));
}
break;
}
rc = doFumoPerform(jConfig, stepCb);
}
while(0);
return rc;
}
|