Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UBLOX_AT.cpp Source File

UBLOX_AT.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 
00018 #include "UBLOX_AT.h"
00019 
00020 using namespace mbed;
00021 using namespace events;
00022 
00023 #ifdef UBX_MDM_SARA_R41XM
00024 static const intptr_t cellular_properties[AT_CellularDevice::PROPERTY_MAX] = {
00025     AT_CellularNetwork::RegistrationModeDisable,// C_EREG
00026     AT_CellularNetwork::RegistrationModeLAC,    // C_GREG
00027     AT_CellularNetwork::RegistrationModeLAC,    // C_REG
00028     0,  // AT_CGSN_WITH_TYPE
00029     0,  // AT_CGDATA
00030     0,  // AT_CGAUTH
00031     1,  // AT_CNMI
00032     1,  // AT_CSMP
00033     1,  // AT_CMGF
00034     0,  // AT_CSDH
00035     1,  // PROPERTY_IPV4_STACK
00036     0,  // PROPERTY_IPV6_STACK
00037     0,  // PROPERTY_IPV4V6_STACK
00038     0,  // PROPERTY_NON_IP_PDP_TYPE
00039     1,  // PROPERTY_AT_CGEREP
00040 };
00041 #elif defined(UBX_MDM_SARA_U2XX) || defined(UBX_MDM_SARA_G3XX)
00042 static const intptr_t cellular_properties[AT_CellularDevice::PROPERTY_MAX] = {
00043     AT_CellularNetwork::RegistrationModeDisable,// C_EREG
00044     AT_CellularNetwork::RegistrationModeLAC,    // C_GREG
00045     AT_CellularNetwork::RegistrationModeLAC,    // C_REG
00046 #ifdef UBX_MDM_SARA_G3XX
00047     0,  // AT_CGSN_WITH_TYPE
00048 #else
00049     1,  // AT_CGSN_WITH_TYPE
00050 #endif
00051     1,  // AT_CGDATA
00052     0,  // AT_CGAUTH
00053     1,  // AT_CNMI
00054     1,  // AT_CSMP
00055     1,  // AT_CMGF
00056     1,  // AT_CSDH
00057     1,  // PROPERTY_IPV4_STACK
00058     0,  // PROPERTY_IPV6_STACK
00059     0,  // PROPERTY_IPV4V6_STACK
00060     0,  // PROPERTY_NON_IP_PDP_TYPE
00061     1,  // PROPERTY_AT_CGEREP
00062 };
00063 #else
00064 static const intptr_t cellular_properties[AT_CellularDevice::PROPERTY_MAX] = {
00065     0,  // C_EREG
00066     0,  // C_GREG
00067     0,  // C_REG
00068     0,  // AT_CGSN_WITH_TYPE
00069     0,  // AT_CGDATA
00070     0,  // AT_CGAUTH
00071     0,  // AT_CNMI
00072     0,  // AT_CSMP
00073     0,  // AT_CMGF
00074     0,  // AT_CSDH
00075     0,  // PROPERTY_IPV4_STACK
00076     0,  // PROPERTY_IPV6_STACK
00077     0,  // PROPERTY_IPV4V6_STACK
00078     0,  // PROPERTY_NON_IP_PDP_TYPE
00079     0,  // PROPERTY_AT_CGEREP
00080 };
00081 #endif
00082 
00083 UBLOX_AT::UBLOX_AT(FileHandle *fh) : AT_CellularDevice(fh), ubx_context(0)
00084 {
00085     set_cellular_properties(cellular_properties);
00086 }
00087 
00088 AT_CellularNetwork *UBLOX_AT::open_network_impl(ATHandler &at)
00089 {
00090     return new UBLOX_AT_CellularNetwork(at, *this);
00091 }
00092 
00093 AT_CellularContext *UBLOX_AT::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
00094 {
00095     ubx_context = new UBLOX_AT_CellularContext(at, this, apn, cp_req, nonip_req);
00096     return  ubx_context;
00097 }
00098 
00099 #if MBED_CONF_UBLOX_AT_PROVIDE_DEFAULT
00100 #include "UARTSerial.h"
00101 CellularDevice *CellularDevice::get_default_instance()
00102 {
00103     static UARTSerial serial(MBED_CONF_UBLOX_AT_TX, MBED_CONF_UBLOX_AT_RX, MBED_CONF_UBLOX_AT_BAUDRATE);
00104 #if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS)
00105     tr_debug("UBLOX_AT flow control: RTS %d CTS %d", MBED_CONF_UBLOX_AT_RTS, MBED_CONF_UBLOX_AT_CTS);
00106     serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_UBLOX_AT_RTS, MBED_CONF_UBLOX_AT_CTS);
00107 #endif
00108     static UBLOX_AT device(&serial);
00109     return &device;
00110 }
00111 #endif
00112 
00113 nsapi_error_t UBLOX_AT::init()
00114 {
00115     setup_at_handler();
00116 
00117     _at->lock();
00118     _at->flush();
00119     _at->at_cmd_discard("", "");
00120     int value = -1;
00121 
00122 #ifdef UBX_MDM_SARA_G3XX
00123     value = 0;
00124 #elif defined(UBX_MDM_SARA_U2XX) || defined(UBX_MDM_SARA_R41XM)
00125     value = 4;
00126 #else
00127     _at->unlock();
00128     return NSAPI_ERROR_UNSUPPORTED ;
00129 #endif
00130 
00131     nsapi_error_t err = _at->at_cmd_discard("+CFUN", "=", "%d", value);
00132 
00133     if (err == NSAPI_ERROR_OK ) {
00134         _at->at_cmd_discard("E0", ""); // echo off
00135         _at->at_cmd_discard("+CMEE", "=1"); // verbose responses
00136         config_authentication_parameters();
00137         err = _at->at_cmd_discard("+CFUN", "=1"); // set full functionality
00138     }
00139     return _at->unlock_return_error();
00140 }
00141 
00142 nsapi_error_t UBLOX_AT::config_authentication_parameters()
00143 {
00144     char *config = NULL;
00145     nsapi_error_t err;
00146     const char *apn;
00147     const char *uname;
00148     const char *pwd;
00149     CellularContext::AuthenticationType auth = CellularContext::NOAUTH;
00150     char imsi[MAX_IMSI_LENGTH + 1];
00151 
00152     if (ubx_context->get_apn() == NULL) {
00153         err = get_imsi(imsi);
00154         if (err == NSAPI_ERROR_OK ) {
00155             config = (char *)apnconfig(imsi);
00156         }
00157         ubx_context->get_next_credentials(&config);
00158     }
00159 
00160     apn = ubx_context->get_apn();
00161     pwd = ubx_context->get_pwd();
00162     uname = ubx_context->get_uname();
00163 
00164     if (*uname && *pwd) {
00165         auth = ubx_context->get_auth();
00166     }
00167     err = set_authentication_parameters(apn, uname, pwd, auth);
00168 
00169     return err;
00170 }
00171 
00172 nsapi_error_t UBLOX_AT::set_authentication_parameters(const char *apn,
00173                                                       const char *username,
00174                                                       const char *password,
00175                                                       CellularContext::AuthenticationType auth)
00176 {
00177     int modem_security = ubx_context->nsapi_security_to_modem_security(auth);
00178 
00179     nsapi_error_t err = _at->at_cmd_discard("+CGDCONT", "=", "%d%s%s", 1, "IP", apn);
00180 
00181     if (err == NSAPI_ERROR_OK ) {
00182 #ifdef UBX_MDM_SARA_R41XM
00183         if (modem_security == CellularContext::CHAP) {
00184             err = _at->at_cmd_discard("+UAUTHREQ", "=", "%d%d%s%s", 1, modem_security, password, username);
00185         } else if (modem_security == CellularContext::NOAUTH) {
00186             err = _at->at_cmd_discard("+UAUTHREQ", "=", "%d%d", 1, modem_security);
00187         } else {
00188             err = _at->at_cmd_discard("+UAUTHREQ", "=", "%d%d%s%s", 1, modem_security, username, password);
00189         }
00190 #else
00191         err = _at->at_cmd_discard("+UAUTHREQ", "=", "%d%d%s%s", 1, modem_security, username, password);
00192 #endif
00193     }
00194 
00195     return err;
00196 }
00197 
00198 nsapi_error_t UBLOX_AT::get_imsi(char *imsi)
00199 {
00200     //Special case: Command put in cmd_chr to make a 1 liner
00201     return _at->at_cmd_str("", "+CIMI", imsi, MAX_IMSI_LENGTH + 1);
00202 }