summaryrefslogtreecommitdiff
path: root/src/MTS_IO_LE910NA1Radio.cpp
blob: c7341b0d96a806c7778a8dfe356aeb7b84bda9ff (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
/*
 * Copyright (C) 2017 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_LE910NA1Radio.cpp
 \brief A brief description

 A more elaborate description
*/
#include <mts/MTS_Text.h>
#include <mts/MTS_Logger.h>
#include <mts/MTS_IO_LE910NA1Radio.h>

using namespace MTS::IO;

const std::string LE910NA1Radio::MODEL_NAME("LE910-NA1");

LE910NA1Radio::LE910NA1Radio(const std::string& sPort)
: LE910Radio(MODEL_NAME, sPort)
{

}

ICellularRadio::CODE LE910NA1Radio::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;
    }

    if (jArgs["fwid"].asString() != "1" && jArgs["fwid"].asString() != "0") {
            return INVALID_ARGS;
    }

    //  LE910-NA1 and LE910-NA V2 is orderable in single image or dual image (single SKU)
    //  configuration. So issue the test command first
    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 LE910NA1Radio::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());

    //  LE910-NA1 and LE910-NA V2 is orderable in single image or dual image (single SKU)
    //  configuration. So issue the test command first
    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;
}