Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QUECTEL_BG96_CellularContext.cpp Source File

QUECTEL_BG96_CellularContext.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "QUECTEL_BG96_CellularContext.h"
00018 #include "QUECTEL_BG96_CellularStack.h"
00019 #include "QUECTEL_BG96_ControlPlane_netif.h"
00020 #include "CellularLog.h"
00021 
00022 #include "Semaphore.h"
00023 
00024 namespace mbed {
00025 
00026 // Non-IP context supported only for context id 1
00027 #define NIDD_PDP_CONTEXT_ID 1
00028 // Timeout to wait for URC indicating NIDD connection opening
00029 #define NIDD_OPEN_URC_TIMEOUT 3000
00030 
00031 QUECTEL_BG96_CellularContext::QUECTEL_BG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
00032     AT_CellularContext(at, device, apn, cp_req, nonip_req)
00033 {
00034     if (_nonip_req) {
00035         _at.set_urc_handler("+QIND:", mbed::Callback<void()>(this, &QUECTEL_BG96_CellularContext::urc_nidd));
00036     }
00037 }
00038 
00039 QUECTEL_BG96_CellularContext::~QUECTEL_BG96_CellularContext()
00040 {
00041     if (_nonip_req) {
00042         _at.set_urc_handler("+QIND:", 0);
00043     }
00044 }
00045 
00046 #if !NSAPI_PPP_AVAILABLE
00047 NetworkStack *QUECTEL_BG96_CellularContext::get_stack()
00048 {
00049     if (_pdp_type == NON_IP_PDP_TYPE || (_nonip_req && _pdp_type != DEFAULT_PDP_TYPE)) {
00050         tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()");
00051         return NULL;
00052     }
00053 
00054     if (!_stack) {
00055         _stack = new QUECTEL_BG96_CellularStack(_at, _cid, (nsapi_ip_stack_t)_pdp_type, *get_device());
00056     }
00057 
00058     return _stack;
00059 }
00060 #endif // #if !NSAPI_PPP_AVAILABLE
00061 
00062 ControlPlane_netif *QUECTEL_BG96_CellularContext::get_cp_netif()
00063 {
00064     if (!_cp_netif) {
00065         _cp_netif = new QUECTEL_BG96_ControlPlane_netif(_at, _cid);
00066     }
00067     return _cp_netif;
00068 }
00069 
00070 nsapi_error_t QUECTEL_BG96_CellularContext::do_user_authentication()
00071 {
00072     if (_pwd && _uname) {
00073         if (_at.at_cmd_discard("+QICSGP", "=", "%d%d%s%s%s%d", _cid, 1,
00074                                _apn, _uname, _pwd, _authentication_type) != NSAPI_ERROR_OK ) {
00075             return NSAPI_ERROR_AUTH_FAILURE ;
00076         }
00077     }
00078 
00079     return NSAPI_ERROR_OK ;
00080 }
00081 
00082 nsapi_error_t QUECTEL_BG96_CellularContext::activate_non_ip_context()
00083 {
00084 
00085     // Open the NIDD connection
00086     nsapi_size_or_error_t ret = _at.at_cmd_discard("+QCFGEXT", "=\"nipd\",1");
00087 
00088     if (ret == NSAPI_ERROR_OK ) {
00089         _semaphore.try_acquire_for(NIDD_OPEN_URC_TIMEOUT);
00090         if (_cid == -1) {
00091             return NSAPI_ERROR_NO_CONNECTION ;
00092         }
00093     }
00094 
00095     return (ret == NSAPI_ERROR_OK ) ? NSAPI_ERROR_OK  : NSAPI_ERROR_NO_CONNECTION ;
00096 }
00097 
00098 void QUECTEL_BG96_CellularContext::activate_context()
00099 {
00100     tr_info("Activate PDP context %d", _cid);
00101 
00102     if (_at.at_cmd_discard("+QIACT", "=", "%d", _cid) == NSAPI_ERROR_OK ) {
00103         _is_context_activated = true;
00104     }
00105 }
00106 
00107 void QUECTEL_BG96_CellularContext::deactivate_context()
00108 {
00109     _at.at_cmd_discard("+QIDEACT", "=", "%d", _cid);
00110 }
00111 
00112 void QUECTEL_BG96_CellularContext::deactivate_non_ip_context()
00113 {
00114     // Close the NIDD connection
00115     _at.at_cmd_discard("+QCFGEXT", "=\"nipd\",0");
00116 }
00117 
00118 void QUECTEL_BG96_CellularContext::urc_nidd()
00119 {
00120     char nipd_string[6];
00121 
00122     // skip "nipd"
00123     _at.skip_param();
00124     _at.read_string(nipd_string, sizeof(nipd_string));
00125 
00126     if (!strcmp(nipd_string, "recv")) {
00127         cp_data_received();
00128     } else if (!strcmp(nipd_string, "open")) {
00129         urc_nidd_open();
00130     } else if (!strcmp(nipd_string, "close")) {
00131         urc_nidd_close();
00132     }
00133 }
00134 
00135 void QUECTEL_BG96_CellularContext::urc_nidd_open()
00136 {
00137     int err = _at.read_int();
00138     if (!err) {
00139         _is_context_active = true;
00140         _is_context_activated = true;
00141         _cid = NIDD_PDP_CONTEXT_ID;
00142     } else {
00143         tr_error("NIDD connection open failed with error: %d", err);
00144     }
00145     _semaphore.release();
00146 }
00147 
00148 void QUECTEL_BG96_CellularContext::urc_nidd_close()
00149 {
00150     set_disconnect();
00151 }
00152 
00153 nsapi_error_t QUECTEL_BG96_CellularContext::setup_control_plane_opt()
00154 {
00155     _at.lock();
00156 
00157     _at.at_cmd_discard("+QCFGEXT", "=\"pdp_type\",", "%d%s%s", NIDD_PDP_CONTEXT_ID, "Non-IP", _apn);
00158 
00159     _at.at_cmd_discard("+CFUN", "=0");
00160 
00161     _at.at_cmd_discard("+CFUN", "=1");
00162 
00163     // Configure Non-IP outgoing data type - 0 for no exception data
00164     nsapi_error_t err = _at.at_cmd_discard("+QCFGEXT", "=\"nipdcfg\",0,", "%s", _apn);
00165 
00166     if (err == NSAPI_ERROR_OK ) {
00167         _cp_in_use = true;
00168         if (_nonip_req) {
00169             _pdp_type = NON_IP_PDP_TYPE;
00170         }
00171     }
00172 
00173     return _at.unlock_return_error();
00174 }
00175 
00176 } /* namespace mbed */