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_N2XX.cpp Source File

UBLOX_N2XX.cpp

00001 /*
00002  * Copyright (c) 2019, 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_N2XX.h"
00019 
00020 using namespace mbed;
00021 using namespace events;
00022 
00023 static const intptr_t cellular_properties[AT_CellularDevice::PROPERTY_MAX] = {
00024     AT_CellularNetwork::RegistrationModeLAC,        // C_EREG
00025     AT_CellularNetwork::RegistrationModeDisable,    // C_GREG
00026     AT_CellularNetwork::RegistrationModeDisable,    // C_REG
00027     1,  // AT_CGSN_WITH_TYPE
00028     0,  // AT_CGDATA
00029     0,  // AT_CGAUTH
00030     0,  // AT_CNMI
00031     0,  // AT_CSMP
00032     0,  // AT_CMGF
00033     0,  // AT_CSDH
00034     1,  // PROPERTY_IPV4_STACK
00035     0,  // PROPERTY_IPV6_STACK
00036     0,  // PROPERTY_IPV4V6_STACK
00037 };
00038 
00039 UBLOX_N2XX::UBLOX_N2XX(FileHandle *fh): AT_CellularDevice(fh)
00040 {
00041     set_cellular_properties(cellular_properties);
00042     memset(simstr, 0, sizeof(simstr));
00043 }
00044 
00045 void UBLOX_N2XX::set_at_urcs_impl()
00046 {
00047     _at->set_urc_handler("+NPIN:", mbed::Callback<void()>(this, &UBLOX_N2XX::NPIN_URC));
00048 }
00049 
00050 UBLOX_N2XX::~UBLOX_N2XX()
00051 {
00052     _at->set_urc_handler("+NPIN:", NULL);
00053 }
00054 
00055 // Callback for Sim Pin.
00056 void UBLOX_N2XX::NPIN_URC()
00057 {
00058     _at->read_string(simstr, sizeof(simstr));
00059 }
00060 
00061 AT_CellularContext *UBLOX_N2XX::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
00062 {
00063     return new UBLOX_N2XX_CellularContext(at, this, apn, cp_req, nonip_req);
00064 }
00065 
00066 #if MBED_CONF_CELLULAR_USE_SMS
00067 AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at)
00068 {
00069     return new UBLOX_N2XX_CellularSMS(at, *this);
00070 }
00071 #endif // MBED_CONF_CELLULAR_USE_SMS
00072 
00073 nsapi_error_t UBLOX_N2XX::init()
00074 {
00075     setup_at_handler();
00076 
00077     _at->lock();
00078     _at->flush();
00079     _at->at_cmd_discard("", "");
00080 
00081     _at->at_cmd_discard("+CMEE", "=1"); // verbose responses
00082 
00083 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN
00084     set_pin(MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN);
00085 #endif
00086     return _at->unlock_return_error();
00087 }
00088 
00089 nsapi_error_t UBLOX_N2XX::get_sim_state(SimState &state)
00090 {
00091     nsapi_error_t error = NSAPI_ERROR_DEVICE_ERROR ;
00092 
00093     _at->lock();
00094     _at->flush();
00095     //Special case: Command put in cmd_chr to make a 1 liner
00096     _at->at_cmd_str("", "+CFUN=1", simstr, sizeof(simstr));
00097     error = _at->unlock_return_error();
00098 
00099     int len = strlen(simstr);
00100     if (len > 0 || error == NSAPI_ERROR_OK ) {
00101         if (error == NSAPI_ERROR_OK ) {
00102             state = SimStateReady;
00103         } else if (len >= 6 && memcmp(simstr, "ENTER PIN", 9) == 0) {
00104             state = SimStatePinNeeded;
00105         } else {
00106             simstr[len] = '\0';
00107             tr_error("Unknown SIM state %s", simstr);
00108             state = SimStateUnknown;
00109         }
00110         error = NSAPI_ERROR_OK ;
00111     } else {
00112         tr_warn("SIM not readable.");
00113         state = SimStateUnknown; // SIM may not be ready yet or pin command may not be supported
00114     }
00115 
00116 #if MBED_CONF_MBED_TRACE_ENABLE
00117     switch (state) {
00118         case SimStatePinNeeded:
00119             tr_info("SIM PIN required");
00120             break;
00121         case SimStatePukNeeded:
00122             tr_error("SIM PUK required");
00123             break;
00124         case SimStateUnknown:
00125             tr_warn("SIM state unknown");
00126             break;
00127         default:
00128             tr_info("SIM is ready");
00129             break;
00130     }
00131 #endif
00132     return error;
00133 }
00134 
00135 nsapi_error_t UBLOX_N2XX::set_pin(const char *sim_pin)
00136 {
00137     // if SIM is already in ready state then settings the PIN
00138     // will return error so let's check the state before settings the pin.
00139     SimState state;
00140     if (get_sim_state(state) == NSAPI_ERROR_OK  && state == SimStateReady) {
00141         return NSAPI_ERROR_OK ;
00142     }
00143 
00144     if (sim_pin == NULL) {
00145         return NSAPI_ERROR_PARAMETER ;
00146     }
00147 
00148     return _at->at_cmd_discard("+NPIN", "=", "%d%s", 0, sim_pin);
00149 }
00150 
00151 #if MBED_CONF_UBLOX_N2XX_PROVIDE_DEFAULT
00152 #include "UARTSerial.h"
00153 CellularDevice *CellularDevice::get_default_instance()
00154 {
00155     static UARTSerial serial(MBED_CONF_UBLOX_N2XX_TX, MBED_CONF_UBLOX_N2XX_RX, MBED_CONF_UBLOX_N2XX_BAUDRATE);
00156 #if defined (MBED_CONF_UBLOX_N2XX_RTS) && defined(MBED_CONF_UBLOX_N2XX_CTS)
00157     tr_debug("UBLOX_N2XX flow control: RTS %d CTS %d", MBED_CONF_UBLOX_N2XX_RTS, MBED_CONF_UBLOX_N2XX_CTS);
00158     serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_UBLOX_N2XX_RTS, MBED_CONF_UBLOX_N2XX_CTS);
00159 #endif
00160     static UBLOX_N2XX device(&serial);
00161     return &device;
00162 }
00163 #endif