summaryrefslogtreecommitdiff
path: root/src/MTS_IO_CellularRadio.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/MTS_IO_CellularRadio.cpp')
-rw-r--r--src/MTS_IO_CellularRadio.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp
index afdfc1e..3de96ce 100644
--- a/src/MTS_IO_CellularRadio.cpp
+++ b/src/MTS_IO_CellularRadio.cpp
@@ -1755,3 +1755,117 @@ ICellularRadio::CODE CellularRadio::getSelectedBandsRaw(std::string& sRawBands)
printTrace("%s| Acquiring selected bands: not applicable", m_sName.c_str());
return NOT_APPLICABLE;
}
+
+ICellularRadio::CODE CellularRadio::getPdpContexts(Json::Value& jData) {
+ printTrace("%s| Fetching the list of PDP contexts from the radio", getName().c_str());
+ CODE rc;
+
+ const std::string sCommand = "AT+CGDCONT?";
+ const int dTimeout = 1000;
+ std::string sResult;
+
+ rc = sendBasicQuery(sCommand, "", sResult, dTimeout);
+ if (rc != SUCCESS) {
+ return rc;
+ }
+
+ std::vector<std::string> vContexts = MTS::Text::split(sResult, "+CGDCONT: ");
+ std::vector<std::string> vContextParams;
+
+ for (size_t i = 0; i < vContexts.size(); i++) {
+ vContexts[i] = MTS::Text::trim(vContexts[i]);
+
+ if (vContexts[i].empty()) {
+ continue;
+ }
+
+ vContextParams = MTS::Text::split(vContexts[i], ",", 4);
+
+ if (vContextParams.size() < 3) {
+ return FAILURE;
+ }
+
+ std::string sContextId = vContextParams[0];
+ std::string sIpMode = MTS::Text::trim(vContextParams[1], '"'); // Remove double quotes from the start and end of the value
+ std::string sApn = MTS::Text::trim(vContextParams[2], '"'); // Remove double quotes from the start and end of the value
+
+ jData[sContextId][ICellularRadio::KEY_PDP_CONTEXT_IPMODE] = sIpMode;
+ jData[sContextId][ICellularRadio::KEY_PDP_CONTEXT_APN] = sApn;
+ }
+
+ return SUCCESS;
+}
+
+ICellularRadio::CODE CellularRadio::setPdpContext(const std::string& sId, const Json::Value& jConfig) {
+ printTrace("%s| Setting context to the radio", getName().c_str());
+ CODE rc;
+
+ if (sId.empty()) {
+ printError("%s| PDP Context ID is not specified", getName().c_str());
+ return FAILURE;
+ }
+
+ std::string sCommand = "AT+CGDCONT=" + sId;
+ const int dTimeout = 1000;
+
+ Json::Value jAllContexts;
+
+ rc = getPdpContexts(jAllContexts);
+ if (rc != SUCCESS) {
+ printError("%s| Failed to retrieve the current PDP context configuration: [%d]", getName().c_str(), rc);
+ return rc;
+ }
+
+ // Remove the context if no parameters defined
+ if (!jConfig.isMember(ICellularRadio::KEY_PDP_CONTEXT_IPMODE) && !jConfig.isMember(ICellularRadio::KEY_PDP_CONTEXT_APN)) {
+ if (jAllContexts.isMember(sId)) {
+ rc = sendBasicCommand(sCommand, dTimeout);
+ return rc;
+ } else {
+ printError("%s| PDP Context [%s] does not exist", getName().c_str(), sId.c_str());
+ return FAILURE;
+ }
+ }
+
+ std::string sIpMode;
+
+ if (jConfig.isMember(ICellularRadio::KEY_PDP_CONTEXT_IPMODE)) {
+ if (jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE] == "IP" || jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE] == "PPP" ||
+ jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE] == "IPV6" || jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE] == "IPV4V6") {
+ sIpMode = jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE].asString();
+ } else {
+ printError("%s| Invalid IP Mode defined: [%s]", getName().c_str(), jConfig[ICellularRadio::KEY_PDP_CONTEXT_IPMODE].asString().c_str());
+ return FAILURE;
+ }
+ } else if (jAllContexts.isMember(sId)) {
+ printInfo("%s| Re-using IP Mode [%s] for PDP context [%s]", getName().c_str(), jAllContexts[sId][ICellularRadio::KEY_PDP_CONTEXT_IPMODE].asString().c_str(), sId.c_str());
+ sIpMode = jAllContexts[sId][ICellularRadio::KEY_PDP_CONTEXT_IPMODE].asString();
+ } else {
+ printError("%s| Failed to edit PDP context [%s] - no such context defined", getName().c_str(), sId.c_str());
+ return FAILURE;
+ }
+
+ sCommand += ",\"";
+ sCommand += sIpMode;
+ sCommand += "\"";
+
+ std::string sApn;
+
+ if (jConfig.isMember(ICellularRadio::KEY_PDP_CONTEXT_APN)) {
+ sApn = jConfig[ICellularRadio::KEY_PDP_CONTEXT_APN].asString();
+ } else if (jAllContexts.isMember(sId)) {
+ printInfo("%s| Re-using APN [%s] for PDP context [%s]", getName().c_str(), jAllContexts[sId][ICellularRadio::KEY_PDP_CONTEXT_APN].asString().c_str(), sId.c_str());
+ sApn = jAllContexts[sId][ICellularRadio::KEY_PDP_CONTEXT_APN].asString();
+ } else {
+ printError("%s| Failed to edit PDP context [%s] - no such context defined", getName().c_str(), sId.c_str());
+ return FAILURE;
+ }
+
+ sCommand += ",\"";
+ sCommand += sApn;
+ sCommand += "\"";
+
+ rc = sendBasicCommand(sCommand, dTimeout);
+
+ return rc;
+} \ No newline at end of file