From da797ef946c6b3a95931ac977ae19f51c61ad679 Mon Sep 17 00:00:00 2001 From: mudassar-ublox Date: Thu, 7 Feb 2019 15:44:09 +0500 Subject: [PATCH 1/5] C030_N211 cellular api --- .../TESTS/api/cellular_power/main.cpp | 145 ++++++++++ .../cellular/TESTS/api/cellular_sms/main.cpp | 34 ++- .../cellular/framework/API/CellularPower.h | 158 +++++++++++ .../cellular/framework/AT/AT_CellularBase.h | 2 +- .../framework/AT/AT_CellularPower.cpp | 244 ++++++++++++++++ .../cellular/framework/AT/AT_CellularPower.h | 60 ++++ .../cellular/framework/AT/AT_CellularSMS.cpp | 20 ++ .../framework/common/CellularTargets.h | 4 +- .../targets/UBLOX/N2XX/UBLOX_N2XX.cpp | 69 +++++ .../framework/targets/UBLOX/N2XX/UBLOX_N2XX.h | 47 ++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp | 45 +++ .../UBLOX/N2XX/UBLOX_N2XX_CellularContext.h | 40 +++ .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp | 76 +++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h | 43 +++ .../UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp | 92 ++++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularPower.h | 45 +++ .../UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp | 131 +++++++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h | 54 ++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularSMS.cpp | 49 ++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h | 45 +++ .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp | 264 ++++++++++++++++++ .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.h | 92 ++++++ 22 files changed, 1742 insertions(+), 17 deletions(-) create mode 100644 features/cellular/TESTS/api/cellular_power/main.cpp create mode 100644 features/cellular/framework/API/CellularPower.h create mode 100644 features/cellular/framework/AT/AT_CellularPower.cpp create mode 100644 features/cellular/framework/AT/AT_CellularPower.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h diff --git a/features/cellular/TESTS/api/cellular_power/main.cpp b/features/cellular/TESTS/api/cellular_power/main.cpp new file mode 100644 index 00000000000..0c2dec09208 --- /dev/null +++ b/features/cellular/TESTS/api/cellular_power/main.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2018, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#if !defined(MBED_CONF_NSAPI_PRESENT) +#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. +#endif + +#include "CellularUtil.h" // for CELLULAR_ helper macros +#include "CellularTargets.h" + +#ifndef CELLULAR_DEVICE +#error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined +#endif + +#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN +#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build. +#endif + +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +#include "mbed.h" + +#include "AT_CellularPower.h" +#include "CellularDevice.h" +#include "../../cellular_tests_common.h" +#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) + +#define NETWORK_TIMEOUT (180*1000) + +static CellularDevice *cellular_device; + +static void urc_callback() +{ +} + +static void wait_for_power(CellularPower *pwr) +{ + nsapi_error_t err = pwr->set_device_ready_urc_cb(&urc_callback); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); + + int sanity_count = 0; + err = pwr->set_at_mode(); + while (err != NSAPI_ERROR_OK) { + sanity_count++; + wait(1); + TEST_ASSERT(sanity_count < 40); + err = pwr->set_at_mode(); + } + + TEST_ASSERT(pwr->is_device_ready() == NSAPI_ERROR_OK); + + pwr->remove_device_ready_urc_cb(&urc_callback); +} + +static void test_power_interface() +{ + const char *devi = CELLULAR_STRINGIFY(CELLULAR_DEVICE); + cellular_device = CellularDevice::get_default_instance(); + cellular_device->set_timeout(9000); + CellularPower *pwr = cellular_device->open_power(); + TEST_ASSERT(pwr != NULL); + + nsapi_error_t err = pwr->on(); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); + wait_for_power(pwr); + + TEST_ASSERT(pwr->set_power_level(1, 0, MBED_CONF_APP_CELLULAR_SIM_PIN) == NSAPI_ERROR_OK); + + err = pwr->reset(); + TEST_ASSERT(err == NSAPI_ERROR_OK); + wait_for_power(pwr); + + wait(1); + err = pwr->opt_power_save_mode(0, 0); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); + if (err == NSAPI_ERROR_DEVICE_ERROR) { + if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command + TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem + ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem + } + } + + wait(1); + err = pwr->opt_receive_period(0, CellularPower::EDRXEUTRAN_NB_S1_mode, 3); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); + if (err == NSAPI_ERROR_DEVICE_ERROR) { + if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command + TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem + ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem + } + } + + err = pwr->off(); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); + +} + +using namespace utest::v1; + +static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) +{ + greentea_case_failure_abort_handler(source, reason); + return STATUS_ABORT; +} + +static Case cases[] = { + Case("CellularPower test interface", test_power_interface, greentea_failure_handler) +}; + +static utest::v1::status_t test_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(10 * 60, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +static Specification specification(test_setup, cases); + +int main() +{ +#if MBED_CONF_MBED_TRACE_ENABLE + trace_open(); +#endif + int ret = Harness::run(specification); +#if MBED_CONF_MBED_TRACE_ENABLE + trace_close(); +#endif + return ret; +} diff --git a/features/cellular/TESTS/api/cellular_sms/main.cpp b/features/cellular/TESTS/api/cellular_sms/main.cpp index 1c770c44ca4..4648dfd598a 100644 --- a/features/cellular/TESTS/api/cellular_sms/main.cpp +++ b/features/cellular/TESTS/api/cellular_sms/main.cpp @@ -110,40 +110,40 @@ static void test_sms_initialize_text_mode() static void test_sms_initialize_pdu_mode() { nsapi_error_t err = sms->initialize(CellularSMS::CellularSMSMmodePDU); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_cscs() { nsapi_error_t err = sms->set_cscs("IRA"); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("UCS2"); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("GSM"); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_csca() { nsapi_error_t err = sms->set_csca("55555", 129); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_csca("+35855555", 145); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_csca(service_center_address, service_address_type); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_cpms_me() { nsapi_error_t err = sms->set_cpms("ME", "ME", "ME"); - TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } @@ -170,7 +170,8 @@ static void test_set_cpms_sm() static void test_sms_send() { const int msg_len = strlen(TEST_MESSAGE); - TEST_ASSERT(sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len) == msg_len); + nsapi_error_t err = sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len); + TEST_ASSERT(err == msg_len || err == NSAPI_ERROR_UNSUPPORTED); } static void test_get_sms() @@ -186,13 +187,15 @@ static void test_get_sms() wait(7); - TEST_ASSERT(sms->get_sms(buf, buf_len, phone_num, SMS_MAX_PHONE_NUMBER_SIZE, time_stamp, SMS_MAX_TIME_STAMP_SIZE, &buf_size) == buf_len - 1); - TEST_ASSERT(strcmp(phone_num, MBED_CONF_APP_CELLULAR_PHONE_NUMBER) == 0); - TEST_ASSERT(strcmp(buf, TEST_MESSAGE) == 0); - TEST_ASSERT(buf_size == 0); + nsapi_error_t err = sms->get_sms(buf, buf_len, phone_num, SMS_MAX_PHONE_NUMBER_SIZE, time_stamp, SMS_MAX_TIME_STAMP_SIZE, &buf_size); + if(err != NSAPI_ERROR_UNSUPPORTED) { + TEST_ASSERT(err == buf_len - 1); + TEST_ASSERT(strcmp(phone_num, MBED_CONF_APP_CELLULAR_PHONE_NUMBER) == 0); + TEST_ASSERT(strcmp(buf, TEST_MESSAGE) == 0); + TEST_ASSERT(buf_size == 0); + } TEST_ASSERT(callbacks_received > 0); callbacks_received = 0; - } static void test_delete_all_messages() @@ -200,7 +203,8 @@ static void test_delete_all_messages() //send a message so that there is something to delete test_sms_send(); wait(7); - TEST_ASSERT(sms->delete_all_messages() == NSAPI_ERROR_OK); + nsapi_error_t err = sms->delete_all_messages(); + TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); callbacks_received = 0; } diff --git a/features/cellular/framework/API/CellularPower.h b/features/cellular/framework/API/CellularPower.h new file mode 100644 index 00000000000..a09829fd069 --- /dev/null +++ b/features/cellular/framework/API/CellularPower.h @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CELLULAR_API_CELLULARPOWER_H_ +#define CELLULAR_API_CELLULARPOWER_H_ + +#include "nsapi_types.h" +#include "Callback.h" + +namespace mbed { + +/** + * Class CellularPower + * + * An interface that provides power handling functions for modem/module. + */ +class CellularPower { +protected: + // friend of CellularDevice so that it's the only way to close/delete this class. + friend class CellularDevice; + + /** + * virtual Destructor + */ + virtual ~CellularPower() {} + +public: + /* Access technology used in method opt_receive_period */ + enum EDRXAccessTechnology { + EDRXGSM_EC_GSM_IoT_mode = 1, + EDRXGSM_A_Gb_mode, + EDRXUTRAN_Iu_mode, + EDRXEUTRAN_WB_S1_mode, + EDRXEUTRAN_NB_S1_mode + }; + + /** Set cellular device power on. Default implementation is empty. + * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. + * Power on is done by toggling power pin/button. + * + * @remark set_at_mode must be called to initialise modem + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem + */ + virtual nsapi_error_t on() = 0; + + /** Set cellular device power off. Default implementation is empty. + * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. + * Power off is done by toggling power pin/button. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem + */ + virtual nsapi_error_t off() = 0; + + /** Set AT command mode. Blocking until success or failure. + * + * @remark must be called after power on to prepare correct AT mode + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t set_at_mode() = 0; + + /** Set cellular device power level by enabling/disabling functionality. + * + * @param func_level: + * 0 minimum functionality + * 1 full functionality. Enable (turn on) the transmit and receive RF circuits for all supported radio access technologies. + * For MTs supporting +CSRA, this equals the RATs indicated by the response of +CSRA=?. Current +CSRA setting is ignored. + * It is not required that the MT transmit and receive RF circuits are in a disabled state for this setting to have effect. + * 2 disable (turn off) MT transmit RF circuits only + * 3 disable (turn off) MT receive RF circuits only + * 4 disable (turn off) both MT transmit and receive RF circuits + * @param do_reset 0 for do not reset, 1 for reset the device when changing the functionality + * + * @remark See 3GPP TS 27.007 CFUN for more details + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL) = 0; + + /** Reset and wake-up cellular device. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t reset() = 0; + + /** Opt for power save setting on cellular device. If both parameters are zero, this disables PSM. + * + * @remark See 3GPP TS 27.007 PSM for details + * + * @param periodic_time Timeout in seconds IoT subsystem is not expecting messaging + * @param active_time Timeout in seconds IoT subsystem waits for response + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time) = 0; + + /** Opt for discontinuous reception on cellular device. + * + * @remark See 3GPP TS 27.007 eDRX for details. + * + * @param mode disable or enable the use of eDRX + * @param act_type type of access technology + * @param edrx_value requested edxr value. Extended DRX parameters information element. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) = 0; + + /** Check whether the device is ready to accept commands. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t is_device_ready() = 0; + + /** Set URC callback function for device specific ready urc. URC is defined in device specific + * power API. Used in startup sequence to listen when device is ready + * for using at commands and possible sim. + * + * @param callback Callback function called when urc received + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_NO_MEMORY on memory failure + * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem + */ + virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback) = 0; + + /** Removes the device ready urc from the list of urc's. + * + * @param callback callback to remove from the list of urc's + */ + virtual void remove_device_ready_urc_cb(mbed::Callback callback) = 0; +}; + +} // namespace mbed + +#endif /* CELLULAR_API_CELLULARPOWER_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularBase.h b/features/cellular/framework/AT/AT_CellularBase.h index 4a993f40668..30e7bab3452 100644 --- a/features/cellular/framework/AT/AT_CellularBase.h +++ b/features/cellular/framework/AT/AT_CellularBase.h @@ -78,4 +78,4 @@ class AT_CellularBase { } // namespace mbed -#endif /* AT_CELLULAR_BASE_H_ */ +#endif /* AT_CELLULAR_BASE_H_ */ \ No newline at end of file diff --git a/features/cellular/framework/AT/AT_CellularPower.cpp b/features/cellular/framework/AT/AT_CellularPower.cpp new file mode 100644 index 00000000000..42c04205c8e --- /dev/null +++ b/features/cellular/framework/AT/AT_CellularPower.cpp @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AT_CellularPower.h" +#include "CellularUtil.h" +#include "CellularLog.h" +#include "CellularTargets.h" +#include "nsapi_types.h" + +static const int PSMTimerBits = 5; + +using namespace mbed_cellular_util; +using namespace mbed; + +AT_CellularPower::AT_CellularPower(ATHandler &at) : AT_CellularBase(at) +{ +} + +AT_CellularPower::~AT_CellularPower() +{ +} + +nsapi_error_t AT_CellularPower::on() +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t AT_CellularPower::off() +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t AT_CellularPower::set_at_mode() +{ + _at.lock(); + _at.flush(); + _at.cmd_start("ATE0"); // echo off + _at.cmd_stop_read_resp(); + + _at.cmd_start("AT+CMEE=1"); // verbose responses + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::set_power_level(int func_level, int do_reset, const char *sim_pin) +{ + _at.lock(); + _at.cmd_start("AT+CFUN="); + _at.write_int(func_level); + _at.write_int(do_reset); + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::reset() +{ + _at.lock(); + _at.cmd_start("AT+CFUN=");// reset to full power levels + _at.write_int(1); + _at.write_int(1); + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::opt_power_save_mode(int periodic_time, int active_time) +{ + _at.lock(); + + if (periodic_time == 0 && active_time == 0) { + // disable PSM + _at.cmd_start("AT+CPSMS="); + _at.write_int(0); + _at.cmd_stop_read_resp(); + } else { + /** + Table 10.5.163a/3GPP TS 24.008: GPRS Timer 3 information element + + Bits 5 to 1 represent the binary coded timer value. + + Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: + 8 7 6 + 0 0 0 value is incremented in multiples of 10 minutes + 0 0 1 value is incremented in multiples of 1 hour + 0 1 0 value is incremented in multiples of 10 hours + 0 1 1 value is incremented in multiples of 2 seconds + 1 0 0 value is incremented in multiples of 30 seconds + 1 0 1 value is incremented in multiples of 1 minute + 1 1 0 value is incremented in multiples of 320 hours (NOTE 1) + 1 1 1 value indicates that the timer is deactivated (NOTE 2). + */ + char pt[8 + 1]; // timer value encoded as 3GPP IE + const int ie_value_max = 0x1f; + uint32_t periodic_timer = 0; + if (periodic_time <= 2 * ie_value_max) { // multiples of 2 seconds + periodic_timer = periodic_time / 2; + strcpy(pt, "01100000"); + } else { + if (periodic_time <= 30 * ie_value_max) { // multiples of 30 seconds + periodic_timer = periodic_time / 30; + strcpy(pt, "10000000"); + } else { + if (periodic_time <= 60 * ie_value_max) { // multiples of 1 minute + periodic_timer = periodic_time / 60; + strcpy(pt, "10100000"); + } else { + if (periodic_time <= 10 * 60 * ie_value_max) { // multiples of 10 minutes + periodic_timer = periodic_time / (10 * 60); + strcpy(pt, "00000000"); + } else { + if (periodic_time <= 60 * 60 * ie_value_max) { // multiples of 1 hour + periodic_timer = periodic_time / (60 * 60); + strcpy(pt, "00100000"); + } else { + if (periodic_time <= 10 * 60 * 60 * ie_value_max) { // multiples of 10 hours + periodic_timer = periodic_time / (10 * 60 * 60); + strcpy(pt, "01000000"); + } else { // multiples of 320 hours + int t = periodic_time / (320 * 60 * 60); + if (t > ie_value_max) { + t = ie_value_max; + } + periodic_timer = t; + strcpy(pt, "11000000"); + } + } + } + } + } + } + + uint_to_binary_str(periodic_timer, &pt[3], sizeof(pt) - 3, PSMTimerBits); + pt[8] = '\0'; + + /** + Table 10.5.172/3GPP TS 24.008: GPRS Timer information element + + Bits 5 to 1 represent the binary coded timer value. + + Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: + + 8 7 6 + 0 0 0 value is incremented in multiples of 2 seconds + 0 0 1 value is incremented in multiples of 1 minute + 0 1 0 value is incremented in multiples of decihours + 1 1 1 value indicates that the timer is deactivated. + + Other values shall be interpreted as multiples of 1 minute in this version of the protocol. + */ + char at[8 + 1]; + uint32_t active_timer; // timer value encoded as 3GPP IE + if (active_time <= 2 * ie_value_max) { // multiples of 2 seconds + active_timer = active_time / 2; + strcpy(at, "00000000"); + } else { + if (active_time <= 60 * ie_value_max) { // multiples of 1 minute + active_timer = (1 << 5) | (active_time / 60); + strcpy(at, "00100000"); + } else { // multiples of decihours + int t = active_time / (6 * 60); + if (t > ie_value_max) { + t = ie_value_max; + } + active_timer = t; + strcpy(at, "01000000"); + } + } + + uint_to_binary_str(active_timer, &at[3], sizeof(at) - 3, PSMTimerBits); + at[8] = '\0'; + + // request for both GPRS and LTE + _at.cmd_start("AT+CPSMS="); + _at.write_int(1); + _at.write_string(pt); + _at.write_string(at); + _at.write_string(pt); + _at.write_string(at); + _at.cmd_stop_read_resp(); + + if (_at.get_last_error() != NSAPI_ERROR_OK) { + tr_warn("Power save mode not enabled!"); + } else { + // network may not agree with power save options but + // that should be fine as timeout is not longer than requested + } + } + + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) +{ + char edrx[5]; + uint_to_binary_str(edrx_value, edrx, 5, 4); + edrx[4] = '\0'; + + _at.lock(); + + _at.cmd_start("AT+CEDRXS="); + _at.write_int(mode); + _at.write_int(act_type); + _at.write_string(edrx); + _at.cmd_stop_read_resp(); + + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::is_device_ready() +{ + _at.lock(); + _at.cmd_start("AT"); + _at.cmd_stop_read_resp(); + + // we need to do this twice because for example after data mode the first 'AT' command will give modem a + // stimulus that we are back to command mode. + _at.clear_error(); + _at.cmd_start("AT"); + _at.cmd_stop_read_resp(); + + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularPower::set_device_ready_urc_cb(mbed::Callback callback) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +void AT_CellularPower::remove_device_ready_urc_cb(mbed::Callback callback) +{ +} diff --git a/features/cellular/framework/AT/AT_CellularPower.h b/features/cellular/framework/AT/AT_CellularPower.h new file mode 100644 index 00000000000..dfa4fb8fc55 --- /dev/null +++ b/features/cellular/framework/AT/AT_CellularPower.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AT_CELLULAR_POWER_H_ +#define AT_CELLULAR_POWER_H_ + +#include "CellularPower.h" +#include "AT_CellularBase.h" + +namespace mbed { + +/** + * Class AT_CellularPower + * + * Class that provides power handling functions for modem/module. + */ +class AT_CellularPower : public CellularPower, public AT_CellularBase { +public: + AT_CellularPower(ATHandler &atHandler); + virtual ~AT_CellularPower(); + +public: + virtual nsapi_error_t on(); + + virtual nsapi_error_t off(); + + virtual nsapi_error_t set_at_mode(); + + virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL); + + virtual nsapi_error_t reset(); + + virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time); + + virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value); + + virtual nsapi_error_t is_device_ready(); + + virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback); + + virtual void remove_device_ready_urc_cb(mbed::Callback callback); +}; + +} // namespace mbed + +#endif /* AT_CELLULAR_POWER_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularSMS.cpp b/features/cellular/framework/AT/AT_CellularSMS.cpp index c4e24ced49a..4efb012d529 100644 --- a/features/cellular/framework/AT/AT_CellularSMS.cpp +++ b/features/cellular/framework/AT/AT_CellularSMS.cpp @@ -210,6 +210,10 @@ void AT_CellularSMS::cmti_urc() nsapi_error_t AT_CellularSMS::set_cnmi() { + if (!is_supported(AT_CNMI)) { + return NSAPI_ERROR_UNSUPPORTED; + } + _at.lock(); _at.cmd_start("AT+CNMI=2,1"); _at.cmd_stop_read_resp(); @@ -218,6 +222,10 @@ nsapi_error_t AT_CellularSMS::set_cnmi() nsapi_error_t AT_CellularSMS::set_cmgf(int msg_format) { + if (!is_supported(AT_CMGF)) { + return NSAPI_ERROR_UNSUPPORTED; + } + _at.lock(); _at.cmd_start("AT+CMGF="); _at.write_int(msg_format); @@ -227,6 +235,10 @@ nsapi_error_t AT_CellularSMS::set_cmgf(int msg_format) nsapi_error_t AT_CellularSMS::set_csmp(int fo, int vp, int pid, int dcs) { + if (!is_supported(AT_CSMP)) { + return NSAPI_ERROR_UNSUPPORTED; + } + _at.lock(); _at.cmd_start("AT+CSMP="); _at.write_int(fo); @@ -239,6 +251,10 @@ nsapi_error_t AT_CellularSMS::set_csmp(int fo, int vp, int pid, int dcs) nsapi_error_t AT_CellularSMS::set_csdh(int show_header) { + if (!is_supported(AT_CSDH)) { + return NSAPI_ERROR_UNSUPPORTED; + } + _at.lock(); _at.cmd_start("AT+CSDH="); _at.write_int(show_header); @@ -396,6 +412,10 @@ char *AT_CellularSMS::create_pdu(const char *phone_number, const char *message, nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const char *message, int msg_len) { + if ((_mode == CellularSMSMmodeText) && (!is_supported(AT_CellularSMSMmodeText))) { + return NSAPI_ERROR_UNSUPPORTED; + } + int single_sms_max_length = _use_8bit_encoding ? SMS_MAX_SIZE_8BIT_SINGLE_SMS_SIZE : SMS_MAX_SIZE_GSM7_SINGLE_SMS_SIZE; if ((_mode == CellularSMSMmodeText && msg_len > single_sms_max_length) || !phone_number) { diff --git a/features/cellular/framework/common/CellularTargets.h b/features/cellular/framework/common/CellularTargets.h index 2ffb377c4f6..60d46466410 100644 --- a/features/cellular/framework/common/CellularTargets.h +++ b/features/cellular/framework/common/CellularTargets.h @@ -32,8 +32,10 @@ namespace mbed { #elif TARGET_MTB_MTS_DRAGONFLY #define CELLULAR_DEVICE TELIT_HE910 #elif TARGET_UBLOX_C030 -#if defined(TARGET_UBLOX_C030_N211) || defined(TARGET_UBLOX_C030_R41XM) +#if defined(TARGET_UBLOX_C030_R41XM) #define CELLULAR_DEVICE UBLOX_AT +#elif defined(TARGET_UBLOX_C030_N211) +#define CELLULAR_DEVICE UBLOX_N2XX #else #define CELLULAR_DEVICE UBLOX_PPP #endif diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp new file mode 100644 index 00000000000..35dd062b029 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX.h" +#include "UBLOX_N2XX_CellularNetwork.h" +#include "UBLOX_N2XX_CellularPower.h" +#include "UBLOX_N2XX_CellularContext.h" +#include "UBLOX_N2XX_CellularSIM.h" +#include "UBLOX_N2XX_CellularSMS.h" + +using namespace mbed; +using namespace events; + +static const AT_CellularBase::SupportedFeature unsupported_features[] = { + AT_CellularBase::AT_CellularSMSMmodeText, + AT_CellularBase::AT_CMGF, + AT_CellularBase::AT_CNMI, + AT_CellularBase::AT_CSMP, + AT_CellularBase::AT_CSDH, + AT_CellularBase::SUPPORTED_FEATURE_END_MARK +}; + +UBLOX_N2XX::UBLOX_N2XX(FileHandle *fh): AT_CellularDevice(fh) +{ + AT_CellularBase::set_unsupported_features(unsupported_features); +} + +UBLOX_N2XX::~UBLOX_N2XX() +{ +} + +AT_CellularNetwork *UBLOX_N2XX::open_network_impl(ATHandler &at) +{ + return new UBLOX_N2XX_CellularNetwork(at); +} + +AT_CellularPower *UBLOX_N2XX::open_power_impl(ATHandler &at) +{ + return new UBLOX_N2XX_CellularPower(at); +} + +AT_CellularContext *UBLOX_N2XX::create_context_impl(ATHandler &at, const char *apn) +{ + return new UBLOX_N2XX_CellularContext(at, this, apn); +} + +AT_CellularSIM *UBLOX_N2XX::open_sim_impl(ATHandler &at) +{ + return new UBLOX_N2XX_CellularSIM(at); +} + +AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at) +{ + return new UBLOX_N2XX_CellularSMS(at); +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h new file mode 100644 index 00000000000..42c88aa0db7 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UBLOX_N2XX_H_ +#define UBLOX_N2XX_H_ + +#include "AT_CellularDevice.h" + +namespace mbed { + +class UBLOX_N2XX : public AT_CellularDevice { + +public: + + UBLOX_N2XX(FileHandle *fh); + virtual ~UBLOX_N2XX(); + +protected: // AT_CellularDevice + + virtual AT_CellularNetwork *open_network_impl(ATHandler &at); + virtual AT_CellularPower *open_power_impl(ATHandler &at); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularSIM *open_sim_impl(ATHandler &at); + virtual AT_CellularSMS *open_sms_impl(ATHandler &at); + +public: // NetworkInterface + + void handle_urc(FileHandle *fh); +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp new file mode 100644 index 00000000000..9e68a0ac757 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "UBLOX_N2XX_CellularContext.h" +#include "UBLOX_N2XX_CellularStack.h" +#include "APN_db.h" + +namespace mbed { + +UBLOX_N2XX_CellularContext::UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : + AT_CellularContext(at, device, apn) +{ +} + +UBLOX_N2XX_CellularContext::~UBLOX_N2XX_CellularContext() +{ +} + +NetworkStack *UBLOX_N2XX_CellularContext::get_stack() +{ + if (!_stack) { + _stack = new UBLOX_N2XX_CellularStack(_at, _cid, _ip_stack_type); + } + return _stack; +} + +bool UBLOX_N2XX_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) +{ + return stack_type == IPV4_STACK ? true : false; +} + +} /* namespace mbed */ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h new file mode 100644 index 00000000000..9db5e1855e2 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef UBLOX_N2XX_CELLULARCONTEXT_H_ +#define UBLOX_N2XX_CELLULARCONTEXT_H_ + +#include "AT_CellularContext.h" + +namespace mbed { + +class UBLOX_N2XX_CellularContext: public AT_CellularContext { + +public: + + UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + virtual ~UBLOX_N2XX_CellularContext(); + +protected: + + virtual NetworkStack *get_stack(); + + virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); +}; + +} /* namespace mbed */ + +#endif // UBLOX_N2XX_CELLULARCONTEXT_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp new file mode 100644 index 00000000000..dfa6f41de73 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX_CellularNetwork.h" + +using namespace mbed; + +UBLOX_N2XX_CellularNetwork::UBLOX_N2XX_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) +{ + _op_act = RAT_UNKNOWN; +} + +UBLOX_N2XX_CellularNetwork::~UBLOX_N2XX_CellularNetwork() +{ + if (_connection_status_cb) { + _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_ERROR_CONNECTION_LOST); + } +} + +AT_CellularNetwork::RegistrationMode UBLOX_N2XX_CellularNetwork::has_registration(RegistrationType reg_type) +{ + return (reg_type == C_EREG) ? RegistrationModeLAC : RegistrationModeDisable; +} + +nsapi_error_t UBLOX_N2XX_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) // Not understandable, RAT (+CEDRXS) +{ + switch (opRat) { +#if defined(TARGET_UBLOX_C030_N211) + case RAT_NB1: + break; +#endif + default: { + _op_act = RAT_UNKNOWN; + return NSAPI_ERROR_UNSUPPORTED; + } + } + + return NSAPI_ERROR_OK; +} + +nsapi_error_t UBLOX_N2XX_CellularNetwork::get_signal_quality(int &rssi, int &ber) +{ + _at.lock(); + + _at.cmd_start("AT+CSQ"); + _at.cmd_stop(); + + _at.resp_start("+CSQ:"); + rssi = _at.read_int(); + ber = _at.read_int(); + _at.resp_stop(); + if (rssi < 0 || ber < 0) { + _at.unlock(); + return NSAPI_ERROR_DEVICE_ERROR; + } + + if (rssi == 99) { + rssi = 0; + } + + return _at.unlock_return_error(); +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h new file mode 100644 index 00000000000..05ed52b315e --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UBLOX_N2XX_CELLULAR_NETWORK_H_ +#define UBLOX_N2XX_CELLULAR_NETWORK_H_ + +#include "AT_CellularNetwork.h" + +namespace mbed { + +class UBLOX_N2XX_CellularNetwork : public AT_CellularNetwork { + +public: + + UBLOX_N2XX_CellularNetwork(ATHandler &atHandler); + virtual ~UBLOX_N2XX_CellularNetwork(); + +protected: + + virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); + + virtual RegistrationMode has_registration(RegistrationType rat); + + virtual nsapi_error_t get_signal_quality(int &rssi, int &ber); +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp new file mode 100644 index 00000000000..97671819789 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX_CellularPower.h" +#include "onboard_modem_api.h" +#include "platform/mbed_wait_api.h" + +using namespace mbed; + +const int MAX_SIM_RESPONSE_LENGTH = 25; + +UBLOX_N2XX_CellularPower::UBLOX_N2XX_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) +{ +} + +UBLOX_N2XX_CellularPower::~UBLOX_N2XX_CellularPower() +{ +} + +nsapi_error_t UBLOX_N2XX_CellularPower::on() +{ +#if MODEM_ON_BOARD + ::onboard_modem_init(); + ::onboard_modem_power_up(); +#endif + return NSAPI_ERROR_OK; +} + +nsapi_error_t UBLOX_N2XX_CellularPower::off() +{ +#if MODEM_ON_BOARD + ::onboard_modem_power_down(); +#endif + return NSAPI_ERROR_OK; +} + +nsapi_error_t UBLOX_N2XX_CellularPower::set_at_mode() +{ + _at.lock(); + _at.flush(); + _at.cmd_start("AT"); + _at.cmd_stop_read_resp(); + + _at.cmd_start("AT+CMEE=1"); // verbose responses + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + + +nsapi_error_t UBLOX_N2XX_CellularPower::set_power_level(int func_level, int do_reset, const char *sim_pin) +{ + char simstr[MAX_SIM_RESPONSE_LENGTH]; + + _at.lock(); + if(func_level == 1) { + _at.cmd_start("AT+CFUN=1"); + _at.cmd_stop(); + _at.resp_start(); + _at.read_string(simstr, sizeof(simstr)); + _at.resp_stop(); + _at.get_last_error(); + _at.clear_error(); + + if (memcmp(simstr, "ENTER PIN", 9) == 0) { + _at.cmd_start("AT+NPIN=0,"); + _at.write_string(sim_pin); + _at.cmd_stop_read_resp(); + _at.get_last_error(); + _at.clear_error(); + wait_ms(1000); + } + } + _at.cmd_start("AT+CFUN="); + _at.write_int(func_level); + _at.write_int(do_reset); + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h new file mode 100644 index 00000000000..d0037a219b6 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UBLOX_N2XX_CELLULARPOWER_H_ +#define UBLOX_N2XX_CELLULARPOWER_H_ + +#include "AT_CellularPower.h" + +namespace mbed { + +class UBLOX_N2XX_CellularPower : public AT_CellularPower { + +public: + + UBLOX_N2XX_CellularPower(ATHandler &atHandler); + virtual ~UBLOX_N2XX_CellularPower(); + +public: + + virtual nsapi_error_t on(); + + virtual nsapi_error_t off(); + + virtual nsapi_error_t set_at_mode(); + + virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL); +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_CELLULARPOWER_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp new file mode 100644 index 00000000000..0f5b2ff431d --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX_CellularSIM.h" +#include "CellularLog.h" +#include "platform/mbed_wait_api.h" + +using namespace mbed; + +UBLOX_N2XX_CellularSIM::UBLOX_N2XX_CellularSIM(ATHandler &atHandler) : AT_CellularSIM(atHandler) +{ + _at.set_urc_handler("+NPIN:", callback(this, &UBLOX_N2XX_CellularSIM::NPIN_URC)); + memset(simstr,0,sizeof(simstr)); +} + +UBLOX_N2XX_CellularSIM::~UBLOX_N2XX_CellularSIM() +{ +} + +// Callback for Sim Pin. +void UBLOX_N2XX_CellularSIM::NPIN_URC() +{ + _at.read_string(simstr, sizeof(simstr)); +} + +nsapi_error_t UBLOX_N2XX_CellularSIM::get_sim_state(SimState &state) +{ + nsapi_error_t error = NSAPI_ERROR_DEVICE_ERROR; + + _at.lock(); + _at.flush(); + _at.cmd_start("AT+CFUN=1"); + _at.cmd_stop(); + + _at.resp_start(); + _at.read_string(simstr, sizeof(simstr)); + _at.resp_stop(); + error = _at.unlock_return_error(); + + int len = strlen(simstr); + if (len > 0 || error == NSAPI_ERROR_OK) { + if (error == NSAPI_ERROR_OK) { + state = SimStateReady; + } else if (len >= 6 && memcmp(simstr, "ENTER PIN", 9) == 0) { + state = SimStatePinNeeded; + } else { + simstr[len] = '\0'; + tr_error("Unknown SIM state %s", simstr); + state = SimStateUnknown; + } + error = NSAPI_ERROR_OK; + } else { + tr_warn("SIM not readable."); + state = SimStateUnknown; // SIM may not be ready yet or pin command may not be supported + } + +#if MBED_CONF_MBED_TRACE_ENABLE + switch (state) { + case SimStatePinNeeded: + tr_info("SIM PIN required"); + break; + case SimStatePukNeeded: + tr_error("SIM PUK required"); + break; + case SimStateUnknown: + tr_warn("SIM state unknown"); + break; + default: + tr_info("SIM is ready"); + break; + } +#endif + return error; +} + +nsapi_error_t UBLOX_N2XX_CellularSIM::set_pin(const char *sim_pin) +{ + // if SIM is already in ready state then settings the PIN + // will return error so let's check the state before settings the pin. + SimState state; + if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { + return NSAPI_ERROR_OK; + } + + _at.lock(); + _at.cmd_start("AT+NPIN=0,"); + _at.write_string(sim_pin); + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + +nsapi_error_t UBLOX_N2XX_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) +{ + _at.lock(); + _at.cmd_start("AT+NPIN=1,"); + _at.write_string(sim_pin); + _at.write_string(new_pin); + _at.cmd_stop_read_resp(); + return _at.unlock_return_error(); +} + +nsapi_error_t UBLOX_N2XX_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) +{ + _at.lock(); + if (query_pin) { + /* use the SIM locked */ + _at.cmd_start("AT+NPIN=2,"); + _at.write_string(sim_pin); + _at.cmd_stop_read_resp(); + } else { + /* use the SIM unlocked */ + _at.cmd_start("AT+NPIN=3,"); + _at.write_string(sim_pin); + _at.cmd_stop_read_resp(); + } + return _at.unlock_return_error(); +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h new file mode 100644 index 00000000000..0e2821ce7dd --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UBLOX_N2XX_CELLULAR_SIM_H_ +#define UBLOX_N2XX_CELLULAR_SIM_H_ + +#include "AT_CellularSIM.h" + +namespace mbed { + +class UBLOX_N2XX_CellularSIM : public AT_CellularSIM { + +public: + + UBLOX_N2XX_CellularSIM(ATHandler &atHandler); + virtual ~UBLOX_N2XX_CellularSIM(); + +public: + + virtual nsapi_error_t set_pin(const char *sim_pin); + + virtual nsapi_error_t change_pin(const char *sim_pin, const char *new_pin); + + virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin); + + virtual nsapi_error_t get_sim_state(SimState &state); + +private: + + static const int MAX_SIM_RESPONSE_LENGTH = 25; + + // URC handlers + void NPIN_URC(); + + char simstr[MAX_SIM_RESPONSE_LENGTH]; +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_CELLULAR_SIM_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.cpp new file mode 100644 index 00000000000..08498b34523 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX_CellularSMS.h" + +using namespace mbed; + +UBLOX_N2XX_CellularSMS::UBLOX_N2XX_CellularSMS(ATHandler &atHandler) : AT_CellularSMS(atHandler) +{ +} + +UBLOX_N2XX_CellularSMS::~UBLOX_N2XX_CellularSMS() +{ +} + +nsapi_error_t UBLOX_N2XX_CellularSMS::set_cpms(const char *memr, const char *memw, const char *mems) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_size_or_error_t UBLOX_N2XX_CellularSMS::set_cscs(const char *chr_set) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_size_or_error_t UBLOX_N2XX_CellularSMS::get_sms(char *buf, uint16_t len, char *phone_num, uint16_t phone_len, + char *time_stamp, uint16_t time_len, int *buf_size) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t UBLOX_N2XX_CellularSMS::delete_all_messages() +{ + return NSAPI_ERROR_UNSUPPORTED; +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h new file mode 100644 index 00000000000..4e75eb571e4 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UBLOX_N2XX_CELLULAR_SMS_H_ +#define UBLOX_N2XX_CELLULAR_SMS_H_ + +#include "AT_CellularSMS.h" + +namespace mbed { + +class UBLOX_N2XX_CellularSMS : public AT_CellularSMS { + +public: + UBLOX_N2XX_CellularSMS(ATHandler &atHandler); + virtual ~UBLOX_N2XX_CellularSMS(); + +public: + + virtual nsapi_error_t set_cpms(const char *memr, const char *memw, const char *mems); + + virtual nsapi_size_or_error_t set_cscs(const char *chr_set); + + virtual nsapi_size_or_error_t get_sms(char *buf, uint16_t buf_len, char *phone_num, uint16_t phone_len, + char *time_stamp, uint16_t time_len, int *buf_size); + + virtual nsapi_error_t delete_all_messages(); +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_CELLULAR_SMS_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp new file mode 100644 index 00000000000..b8dd2073d6d --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UBLOX_N2XX_CellularStack.h" +#include "CellularUtil.h" +#include + +using namespace mbed; +using namespace mbed_cellular_util; + +UBLOX_N2XX_CellularStack::UBLOX_N2XX_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : AT_CellularStack(atHandler, cid, stack_type) +{ + // URC handlers for sockets + _at.set_urc_handler("+NSONMI:", callback(this, &UBLOX_N2XX_CellularStack::NSONMI_URC)); +} + +UBLOX_N2XX_CellularStack::~UBLOX_N2XX_CellularStack() +{ +} + +nsapi_error_t UBLOX_N2XX_CellularStack::socket_listen(nsapi_socket_t handle, int backlog) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t UBLOX_N2XX_CellularStack::socket_accept(void *server, void **socket, SocketAddress *addr) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +// Callback for Socket Read URC. +void UBLOX_N2XX_CellularStack::NSONMI_URC() +{ + int a, b; + CellularSocket *socket; + + a = _at.read_int(); + b = _at.read_int(); + + socket = find_socket(a); + if (socket != NULL) { + socket->pending_bytes = b; + // No debug prints here as they can affect timing + // and cause data loss in UARTSerial + if (socket->_cb != NULL) { + socket->_cb(socket->_data); + } + } +} + +int UBLOX_N2XX_CellularStack::get_max_socket_count() +{ + return N2XX_MAX_SOCKET; +} + +bool UBLOX_N2XX_CellularStack::is_protocol_supported(nsapi_protocol_t protocol) +{ + return (protocol == NSAPI_UDP); +} + +nsapi_error_t UBLOX_N2XX_CellularStack::create_socket_impl(CellularSocket *socket) +{ + int sock_id = 0; + int localport = socket->localAddress.get_port(); + + if(localport == 5683 || localport < 0 || localport > 65535) { + return NSAPI_ERROR_NO_SOCKET; + } + + _at.lock(); + _at.cmd_start("AT+NSOCR=\"DGRAM\",17,"); + _at.write_int(localport); + _at.write_int(1); + _at.cmd_stop(); + + _at.resp_start(); + sock_id = _at.read_int(); + _at.resp_stop(); + + if ((_at.get_last_error() != NSAPI_ERROR_OK) || (sock_id == -1)) { + _at.unlock(); + return NSAPI_ERROR_NO_SOCKET; + } + _at.unlock(); + + // Check for duplicate socket id delivered by modem + for (int i = 0; i < N2XX_MAX_SOCKET; i++) { + CellularSocket *sock = _socket[i]; + if (sock && sock->created && sock->id == sock_id) { + return NSAPI_ERROR_NO_SOCKET; + } + } + + socket->id = sock_id; + socket->created = true; + + return NSAPI_ERROR_OK; +} + +nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_sendto_impl(CellularSocket *socket, const SocketAddress &address, + const void *data, nsapi_size_t size) +{ + if (size > N2XX_MAX_PACKET_SIZE) { + return NSAPI_ERROR_PARAMETER; + } + + int sent_len = 0, len = 0; + char *dataStr = (char *) malloc((size * 2) + 1); + memset(dataStr, 0, size*2+1); + char_str_to_hex_str((const char*)data, size, dataStr); + len = strlen(dataStr); + + _at.cmd_start("AT+NSOST="); + _at.write_int(socket->id); + _at.write_string(address.get_ip_address()); + _at.write_int(address.get_port()); + _at.write_int(len/2); + _at.write_string(dataStr); + _at.cmd_stop(); + + _at.resp_start(); + _at.skip_param(); // skip socket id + sent_len = _at.read_int(); + _at.resp_stop(); + + free(dataStr); + if ((_at.get_last_error() == NSAPI_ERROR_OK)) { + return sent_len; + } + + return _at.get_last_error(); +} + +nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address, + void *buffer, nsapi_size_t size) +{ + nsapi_size_or_error_t nsapi_error_size = NSAPI_ERROR_DEVICE_ERROR; + nsapi_size_t read_blk, usorf_sz, count = 0, length = size*2; + bool success = true; + char ipAddress[NSAPI_IP_SIZE]; + char *tmpBuf = (char *) malloc(size*2+1); + memset(tmpBuf, 0, size*2+1); + int port = 0; + Timer timer; + + if (socket->pending_bytes == 0) { + _at.process_oob(); + if (socket->pending_bytes == 0) { + free(tmpBuf); + return NSAPI_ERROR_WOULD_BLOCK; + } + } + + timer.start(); + while (success && (length > 0)) { + read_blk = N2XX_MAX_PACKET_SIZE; + if (read_blk > length) { + read_blk = length; + } + if (socket->pending_bytes > 0) { + _at.cmd_start("AT+NSORF="); + _at.write_int(socket->id); + _at.write_int(read_blk); + _at.cmd_stop(); + + _at.resp_start(); + _at.skip_param(); // receiving socket id + _at.read_string(ipAddress, sizeof(ipAddress)); + port = _at.read_int(); + usorf_sz = _at.read_int(); + + // Must use what +NSORF returns here as it may be less or more than we asked for + if (usorf_sz >= socket->pending_bytes) { + socket->pending_bytes = 0; + } else { + socket->pending_bytes -= usorf_sz; + } + + if (usorf_sz > length) { + usorf_sz = length; + } + _at.read_string(tmpBuf + count, usorf_sz*2+1); + _at.resp_stop(); + + if (usorf_sz > 0) { + count += (usorf_sz*2); + length -= (usorf_sz*2); + } else { + // read() should not fail + success = false; + } + } else if (timer.read_ms() < SOCKET_TIMEOUT) { + // Wait for URCs + _at.process_oob(); + } else { + if (count == 0) { + // Timeout with nothing received + nsapi_error_size = NSAPI_ERROR_WOULD_BLOCK; + success = false; + } + length = 0; // This simply to cause an exit + } + } + timer.stop(); + + socket->pending_bytes = 0; + if (!count || (_at.get_last_error() != NSAPI_ERROR_OK)) { + free(tmpBuf); + return NSAPI_ERROR_WOULD_BLOCK; + } else { + nsapi_error_size = count; + } + + if (success && socket->proto == NSAPI_UDP && address) { + address->set_ip_address(ipAddress); + address->get_ip_address(); + address->set_port(port); + } + + hex_str_to_char_str(tmpBuf, count, (char*) buffer); + + free(tmpBuf); + return nsapi_error_size = count/2; +} + +nsapi_error_t UBLOX_N2XX_CellularStack::socket_close_impl(int sock_id) +{ + _at.lock(); + _at.cmd_start("AT+NSOCL="); + _at.write_int(sock_id); + _at.cmd_stop_read_resp(); + + return _at.unlock_return_error(); +} + +// Find or create a socket from the list. +UBLOX_N2XX_CellularStack::CellularSocket *UBLOX_N2XX_CellularStack::find_socket(int id) +{ + CellularSocket *socket = NULL; + + for (unsigned int x = 0; (socket == NULL) && (x < N2XX_MAX_SOCKET); x++) { + if (_socket) { + if (_socket[x]->id == id) { + socket = (_socket[x]); + } + } + } + + return socket; +} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h new file mode 100644 index 00000000000..2f9f7bde3a1 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef UBLOX_N2XX_CELLULARSTACK_H_ +#define UBLOX_N2XX_CELLULARSTACK_H_ + +#include "AT_CellularStack.h" +#include "CellularUtil.h" +#include "mbed_wait_api.h" +#include "drivers/Timer.h" + +namespace mbed { + +class UBLOX_N2XX_CellularStack : public AT_CellularStack { + +public: + + UBLOX_N2XX_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type); + virtual ~UBLOX_N2XX_CellularStack(); + +protected: + + virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog); + + virtual nsapi_error_t socket_accept(nsapi_socket_t server, + nsapi_socket_t *handle, SocketAddress *address = 0); + +protected: + + /** Socket "unused" value. + */ + static const int SOCKET_UNUSED = -1; + + /** Socket timeout value in milliseconds. + * Note: the sockets layer above will retry the + * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK + * and the user has set a larger timeout or full blocking. + */ + static const int SOCKET_TIMEOUT = 1000; + + /** Maximum allowed sockets. + */ + static const int N2XX_MAX_SOCKET = 7; + + /** The maximum number of bytes in a packet that can be write/read from + * the AT interface in one go. + */ + static const int N2XX_MAX_PACKET_SIZE = 512; + + virtual int get_max_socket_count(); + + virtual bool is_protocol_supported(nsapi_protocol_t protocol); + + virtual nsapi_error_t create_socket_impl(CellularSocket *socket); + + virtual nsapi_size_or_error_t socket_sendto_impl(CellularSocket *socket, const SocketAddress &address, + const void *data, nsapi_size_t size); + + virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address, + void *buffer, nsapi_size_t size); + + virtual nsapi_error_t socket_close_impl(int sock_id); + +private: + + // URC handlers + void NSONMI_URC(); + + /** Find a socket from the list. + * + * @param id Socket ID. + * @return Socket if True, otherwise NULL. + */ + CellularSocket *find_socket(int id = SOCKET_UNUSED); +}; + +} // namespace mbed + +#endif // UBLOX_N2XX_CELLULARSTACK_H_ From 6c347eafe5b600a0a3336a3ced66ca756a2696aa Mon Sep 17 00:00:00 2001 From: mudassar-ublox Date: Tue, 26 Feb 2019 17:28:01 +0500 Subject: [PATCH 2/5] C030_N211 cellular api refactoring --- .../TESTS/api/cellular_power/main.cpp | 145 ----------- .../cellular/TESTS/api/cellular_sms/main.cpp | 24 +- .../cellular/framework/API/CellularPower.h | 158 ------------ .../cellular/framework/AT/AT_CellularBase.h | 6 +- .../framework/AT/AT_CellularPower.cpp | 244 ------------------ .../cellular/framework/AT/AT_CellularPower.h | 60 ----- .../cellular/framework/AT/AT_CellularSMS.cpp | 16 +- .../GEMALTO/CINTERION/GEMALTO_CINTERION.cpp | 4 + .../GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp | 4 + .../MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp | 4 + .../targets/QUECTEL/BC95/QUECTEL_BC95.cpp | 4 + .../targets/QUECTEL/BG96/QUECTEL_BG96.cpp | 4 + .../targets/QUECTEL/M26/QUECTEL_M26.cpp | 4 + .../targets/QUECTEL/UG96/QUECTEL_UG96.cpp | 4 + .../targets/TELIT/HE910/TELIT_HE910.cpp | 4 + .../framework/targets/UBLOX/AT/UBLOX_AT.cpp | 10 +- .../targets/UBLOX/N2XX/UBLOX_N2XX.cpp | 135 ++++++++-- .../framework/targets/UBLOX/N2XX/UBLOX_N2XX.h | 23 +- .../UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp | 18 +- .../UBLOX/N2XX/UBLOX_N2XX_CellularContext.h | 5 +- .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp | 30 +-- .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h | 4 - .../UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp | 92 ------- .../UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp | 131 ---------- .../UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h | 54 ---- .../UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h | 4 +- .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp | 19 +- .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.h | 11 - .../targets/UBLOX/N2XX/mbed_lib.json | 29 +++ .../framework/targets/UBLOX/PPP/UBLOX_PPP.cpp | 8 + .../TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp | 10 +- .../TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.cpp | 52 ++++ .../TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.h | 33 +-- 33 files changed, 320 insertions(+), 1033 deletions(-) delete mode 100644 features/cellular/TESTS/api/cellular_power/main.cpp delete mode 100644 features/cellular/framework/API/CellularPower.h delete mode 100644 features/cellular/framework/AT/AT_CellularPower.cpp delete mode 100644 features/cellular/framework/AT/AT_CellularPower.h delete mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp delete mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp delete mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h create mode 100644 features/cellular/framework/targets/UBLOX/N2XX/mbed_lib.json create mode 100644 targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.cpp rename features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h => targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.h (50%) diff --git a/features/cellular/TESTS/api/cellular_power/main.cpp b/features/cellular/TESTS/api/cellular_power/main.cpp deleted file mode 100644 index 0c2dec09208..00000000000 --- a/features/cellular/TESTS/api/cellular_power/main.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2018, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#if !defined(MBED_CONF_NSAPI_PRESENT) -#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. -#endif - -#include "CellularUtil.h" // for CELLULAR_ helper macros -#include "CellularTargets.h" - -#ifndef CELLULAR_DEVICE -#error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined -#endif - -#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN -#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build. -#endif - -#include "greentea-client/test_env.h" -#include "unity.h" -#include "utest.h" - -#include "mbed.h" - -#include "AT_CellularPower.h" -#include "CellularDevice.h" -#include "../../cellular_tests_common.h" -#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) - -#define NETWORK_TIMEOUT (180*1000) - -static CellularDevice *cellular_device; - -static void urc_callback() -{ -} - -static void wait_for_power(CellularPower *pwr) -{ - nsapi_error_t err = pwr->set_device_ready_urc_cb(&urc_callback); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - - int sanity_count = 0; - err = pwr->set_at_mode(); - while (err != NSAPI_ERROR_OK) { - sanity_count++; - wait(1); - TEST_ASSERT(sanity_count < 40); - err = pwr->set_at_mode(); - } - - TEST_ASSERT(pwr->is_device_ready() == NSAPI_ERROR_OK); - - pwr->remove_device_ready_urc_cb(&urc_callback); -} - -static void test_power_interface() -{ - const char *devi = CELLULAR_STRINGIFY(CELLULAR_DEVICE); - cellular_device = CellularDevice::get_default_instance(); - cellular_device->set_timeout(9000); - CellularPower *pwr = cellular_device->open_power(); - TEST_ASSERT(pwr != NULL); - - nsapi_error_t err = pwr->on(); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - wait_for_power(pwr); - - TEST_ASSERT(pwr->set_power_level(1, 0, MBED_CONF_APP_CELLULAR_SIM_PIN) == NSAPI_ERROR_OK); - - err = pwr->reset(); - TEST_ASSERT(err == NSAPI_ERROR_OK); - wait_for_power(pwr); - - wait(1); - err = pwr->opt_power_save_mode(0, 0); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); - if (err == NSAPI_ERROR_DEVICE_ERROR) { - if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command - TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem - ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem - } - } - - wait(1); - err = pwr->opt_receive_period(0, CellularPower::EDRXEUTRAN_NB_S1_mode, 3); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); - if (err == NSAPI_ERROR_DEVICE_ERROR) { - if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command - TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem - ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem - } - } - - err = pwr->off(); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - -} - -using namespace utest::v1; - -static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) -{ - greentea_case_failure_abort_handler(source, reason); - return STATUS_ABORT; -} - -static Case cases[] = { - Case("CellularPower test interface", test_power_interface, greentea_failure_handler) -}; - -static utest::v1::status_t test_setup(const size_t number_of_cases) -{ - GREENTEA_SETUP(10 * 60, "default_auto"); - return verbose_test_setup_handler(number_of_cases); -} - -static Specification specification(test_setup, cases); - -int main() -{ -#if MBED_CONF_MBED_TRACE_ENABLE - trace_open(); -#endif - int ret = Harness::run(specification); -#if MBED_CONF_MBED_TRACE_ENABLE - trace_close(); -#endif - return ret; -} diff --git a/features/cellular/TESTS/api/cellular_sms/main.cpp b/features/cellular/TESTS/api/cellular_sms/main.cpp index 4648dfd598a..89e4760444c 100644 --- a/features/cellular/TESTS/api/cellular_sms/main.cpp +++ b/features/cellular/TESTS/api/cellular_sms/main.cpp @@ -130,13 +130,13 @@ static void test_set_cscs() static void test_set_csca() { nsapi_error_t err = sms->set_csca("55555", 129); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_csca("+35855555", 145); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_csca(service_center_address, service_address_type); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && + TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR && ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } @@ -170,8 +170,7 @@ static void test_set_cpms_sm() static void test_sms_send() { const int msg_len = strlen(TEST_MESSAGE); - nsapi_error_t err = sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len); - TEST_ASSERT(err == msg_len || err == NSAPI_ERROR_UNSUPPORTED); + TEST_ASSERT(sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len) == msg_len); } static void test_get_sms() @@ -187,15 +186,13 @@ static void test_get_sms() wait(7); - nsapi_error_t err = sms->get_sms(buf, buf_len, phone_num, SMS_MAX_PHONE_NUMBER_SIZE, time_stamp, SMS_MAX_TIME_STAMP_SIZE, &buf_size); - if(err != NSAPI_ERROR_UNSUPPORTED) { - TEST_ASSERT(err == buf_len - 1); - TEST_ASSERT(strcmp(phone_num, MBED_CONF_APP_CELLULAR_PHONE_NUMBER) == 0); - TEST_ASSERT(strcmp(buf, TEST_MESSAGE) == 0); - TEST_ASSERT(buf_size == 0); - } + TEST_ASSERT(sms->get_sms(buf, buf_len, phone_num, SMS_MAX_PHONE_NUMBER_SIZE, time_stamp, SMS_MAX_TIME_STAMP_SIZE, &buf_size) == buf_len - 1); + TEST_ASSERT(strcmp(phone_num, MBED_CONF_APP_CELLULAR_PHONE_NUMBER) == 0); + TEST_ASSERT(strcmp(buf, TEST_MESSAGE) == 0); + TEST_ASSERT(buf_size == 0); TEST_ASSERT(callbacks_received > 0); callbacks_received = 0; + } static void test_delete_all_messages() @@ -203,8 +200,7 @@ static void test_delete_all_messages() //send a message so that there is something to delete test_sms_send(); wait(7); - nsapi_error_t err = sms->delete_all_messages(); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); + TEST_ASSERT(sms->delete_all_messages() == NSAPI_ERROR_OK); callbacks_received = 0; } diff --git a/features/cellular/framework/API/CellularPower.h b/features/cellular/framework/API/CellularPower.h deleted file mode 100644 index a09829fd069..00000000000 --- a/features/cellular/framework/API/CellularPower.h +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef CELLULAR_API_CELLULARPOWER_H_ -#define CELLULAR_API_CELLULARPOWER_H_ - -#include "nsapi_types.h" -#include "Callback.h" - -namespace mbed { - -/** - * Class CellularPower - * - * An interface that provides power handling functions for modem/module. - */ -class CellularPower { -protected: - // friend of CellularDevice so that it's the only way to close/delete this class. - friend class CellularDevice; - - /** - * virtual Destructor - */ - virtual ~CellularPower() {} - -public: - /* Access technology used in method opt_receive_period */ - enum EDRXAccessTechnology { - EDRXGSM_EC_GSM_IoT_mode = 1, - EDRXGSM_A_Gb_mode, - EDRXUTRAN_Iu_mode, - EDRXEUTRAN_WB_S1_mode, - EDRXEUTRAN_NB_S1_mode - }; - - /** Set cellular device power on. Default implementation is empty. - * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. - * Power on is done by toggling power pin/button. - * - * @remark set_at_mode must be called to initialise modem - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t on() = 0; - - /** Set cellular device power off. Default implementation is empty. - * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. - * Power off is done by toggling power pin/button. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t off() = 0; - - /** Set AT command mode. Blocking until success or failure. - * - * @remark must be called after power on to prepare correct AT mode - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_at_mode() = 0; - - /** Set cellular device power level by enabling/disabling functionality. - * - * @param func_level: - * 0 minimum functionality - * 1 full functionality. Enable (turn on) the transmit and receive RF circuits for all supported radio access technologies. - * For MTs supporting +CSRA, this equals the RATs indicated by the response of +CSRA=?. Current +CSRA setting is ignored. - * It is not required that the MT transmit and receive RF circuits are in a disabled state for this setting to have effect. - * 2 disable (turn off) MT transmit RF circuits only - * 3 disable (turn off) MT receive RF circuits only - * 4 disable (turn off) both MT transmit and receive RF circuits - * @param do_reset 0 for do not reset, 1 for reset the device when changing the functionality - * - * @remark See 3GPP TS 27.007 CFUN for more details - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL) = 0; - - /** Reset and wake-up cellular device. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t reset() = 0; - - /** Opt for power save setting on cellular device. If both parameters are zero, this disables PSM. - * - * @remark See 3GPP TS 27.007 PSM for details - * - * @param periodic_time Timeout in seconds IoT subsystem is not expecting messaging - * @param active_time Timeout in seconds IoT subsystem waits for response - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time) = 0; - - /** Opt for discontinuous reception on cellular device. - * - * @remark See 3GPP TS 27.007 eDRX for details. - * - * @param mode disable or enable the use of eDRX - * @param act_type type of access technology - * @param edrx_value requested edxr value. Extended DRX parameters information element. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) = 0; - - /** Check whether the device is ready to accept commands. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t is_device_ready() = 0; - - /** Set URC callback function for device specific ready urc. URC is defined in device specific - * power API. Used in startup sequence to listen when device is ready - * for using at commands and possible sim. - * - * @param callback Callback function called when urc received - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_NO_MEMORY on memory failure - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback) = 0; - - /** Removes the device ready urc from the list of urc's. - * - * @param callback callback to remove from the list of urc's - */ - virtual void remove_device_ready_urc_cb(mbed::Callback callback) = 0; -}; - -} // namespace mbed - -#endif /* CELLULAR_API_CELLULARPOWER_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularBase.h b/features/cellular/framework/AT/AT_CellularBase.h index 30e7bab3452..0e6ba258e1f 100644 --- a/features/cellular/framework/AT/AT_CellularBase.h +++ b/features/cellular/framework/AT/AT_CellularBase.h @@ -49,6 +49,10 @@ class AT_CellularBase { PROPERTY_AT_CGSN_WITH_TYPE, // 0 = not supported, 1 = supported. AT+CGSN without type is likely always supported similar to AT+GSN. PROPERTY_AT_CGDATA, // 0 = not supported, 1 = supported. Alternative is to support only ATD*99***# PROPERTY_AT_CGAUTH, // 0 = not supported, 1 = supported. APN authentication AT commands supported + PROPERTY_AT_CNMI, // 0 = not supported, 1 = supported. New message (SMS) indication AT command + PROPERTY_AT_CSMP, // 0 = not supported, 1 = supported. Set text mode AT command + PROPERTY_AT_CMGF, // 0 = not supported, 1 = supported. Set preferred message format AT command + PROPERTY_AT_CSDH, // 0 = not supported, 1 = supported. Show text mode AT command PROPERTY_IPV4_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support IPV4? PROPERTY_IPV6_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support IPV6? PROPERTY_IPV4V6_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support dual stack IPV4V6? @@ -78,4 +82,4 @@ class AT_CellularBase { } // namespace mbed -#endif /* AT_CELLULAR_BASE_H_ */ \ No newline at end of file +#endif /* AT_CELLULAR_BASE_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularPower.cpp b/features/cellular/framework/AT/AT_CellularPower.cpp deleted file mode 100644 index 42c04205c8e..00000000000 --- a/features/cellular/framework/AT/AT_CellularPower.cpp +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "AT_CellularPower.h" -#include "CellularUtil.h" -#include "CellularLog.h" -#include "CellularTargets.h" -#include "nsapi_types.h" - -static const int PSMTimerBits = 5; - -using namespace mbed_cellular_util; -using namespace mbed; - -AT_CellularPower::AT_CellularPower(ATHandler &at) : AT_CellularBase(at) -{ -} - -AT_CellularPower::~AT_CellularPower() -{ -} - -nsapi_error_t AT_CellularPower::on() -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -nsapi_error_t AT_CellularPower::off() -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -nsapi_error_t AT_CellularPower::set_at_mode() -{ - _at.lock(); - _at.flush(); - _at.cmd_start("ATE0"); // echo off - _at.cmd_stop_read_resp(); - - _at.cmd_start("AT+CMEE=1"); // verbose responses - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::set_power_level(int func_level, int do_reset, const char *sim_pin) -{ - _at.lock(); - _at.cmd_start("AT+CFUN="); - _at.write_int(func_level); - _at.write_int(do_reset); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::reset() -{ - _at.lock(); - _at.cmd_start("AT+CFUN=");// reset to full power levels - _at.write_int(1); - _at.write_int(1); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::opt_power_save_mode(int periodic_time, int active_time) -{ - _at.lock(); - - if (periodic_time == 0 && active_time == 0) { - // disable PSM - _at.cmd_start("AT+CPSMS="); - _at.write_int(0); - _at.cmd_stop_read_resp(); - } else { - /** - Table 10.5.163a/3GPP TS 24.008: GPRS Timer 3 information element - - Bits 5 to 1 represent the binary coded timer value. - - Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: - 8 7 6 - 0 0 0 value is incremented in multiples of 10 minutes - 0 0 1 value is incremented in multiples of 1 hour - 0 1 0 value is incremented in multiples of 10 hours - 0 1 1 value is incremented in multiples of 2 seconds - 1 0 0 value is incremented in multiples of 30 seconds - 1 0 1 value is incremented in multiples of 1 minute - 1 1 0 value is incremented in multiples of 320 hours (NOTE 1) - 1 1 1 value indicates that the timer is deactivated (NOTE 2). - */ - char pt[8 + 1]; // timer value encoded as 3GPP IE - const int ie_value_max = 0x1f; - uint32_t periodic_timer = 0; - if (periodic_time <= 2 * ie_value_max) { // multiples of 2 seconds - periodic_timer = periodic_time / 2; - strcpy(pt, "01100000"); - } else { - if (periodic_time <= 30 * ie_value_max) { // multiples of 30 seconds - periodic_timer = periodic_time / 30; - strcpy(pt, "10000000"); - } else { - if (periodic_time <= 60 * ie_value_max) { // multiples of 1 minute - periodic_timer = periodic_time / 60; - strcpy(pt, "10100000"); - } else { - if (periodic_time <= 10 * 60 * ie_value_max) { // multiples of 10 minutes - periodic_timer = periodic_time / (10 * 60); - strcpy(pt, "00000000"); - } else { - if (periodic_time <= 60 * 60 * ie_value_max) { // multiples of 1 hour - periodic_timer = periodic_time / (60 * 60); - strcpy(pt, "00100000"); - } else { - if (periodic_time <= 10 * 60 * 60 * ie_value_max) { // multiples of 10 hours - periodic_timer = periodic_time / (10 * 60 * 60); - strcpy(pt, "01000000"); - } else { // multiples of 320 hours - int t = periodic_time / (320 * 60 * 60); - if (t > ie_value_max) { - t = ie_value_max; - } - periodic_timer = t; - strcpy(pt, "11000000"); - } - } - } - } - } - } - - uint_to_binary_str(periodic_timer, &pt[3], sizeof(pt) - 3, PSMTimerBits); - pt[8] = '\0'; - - /** - Table 10.5.172/3GPP TS 24.008: GPRS Timer information element - - Bits 5 to 1 represent the binary coded timer value. - - Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: - - 8 7 6 - 0 0 0 value is incremented in multiples of 2 seconds - 0 0 1 value is incremented in multiples of 1 minute - 0 1 0 value is incremented in multiples of decihours - 1 1 1 value indicates that the timer is deactivated. - - Other values shall be interpreted as multiples of 1 minute in this version of the protocol. - */ - char at[8 + 1]; - uint32_t active_timer; // timer value encoded as 3GPP IE - if (active_time <= 2 * ie_value_max) { // multiples of 2 seconds - active_timer = active_time / 2; - strcpy(at, "00000000"); - } else { - if (active_time <= 60 * ie_value_max) { // multiples of 1 minute - active_timer = (1 << 5) | (active_time / 60); - strcpy(at, "00100000"); - } else { // multiples of decihours - int t = active_time / (6 * 60); - if (t > ie_value_max) { - t = ie_value_max; - } - active_timer = t; - strcpy(at, "01000000"); - } - } - - uint_to_binary_str(active_timer, &at[3], sizeof(at) - 3, PSMTimerBits); - at[8] = '\0'; - - // request for both GPRS and LTE - _at.cmd_start("AT+CPSMS="); - _at.write_int(1); - _at.write_string(pt); - _at.write_string(at); - _at.write_string(pt); - _at.write_string(at); - _at.cmd_stop_read_resp(); - - if (_at.get_last_error() != NSAPI_ERROR_OK) { - tr_warn("Power save mode not enabled!"); - } else { - // network may not agree with power save options but - // that should be fine as timeout is not longer than requested - } - } - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) -{ - char edrx[5]; - uint_to_binary_str(edrx_value, edrx, 5, 4); - edrx[4] = '\0'; - - _at.lock(); - - _at.cmd_start("AT+CEDRXS="); - _at.write_int(mode); - _at.write_int(act_type); - _at.write_string(edrx); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::is_device_ready() -{ - _at.lock(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - // we need to do this twice because for example after data mode the first 'AT' command will give modem a - // stimulus that we are back to command mode. - _at.clear_error(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::set_device_ready_urc_cb(mbed::Callback callback) -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -void AT_CellularPower::remove_device_ready_urc_cb(mbed::Callback callback) -{ -} diff --git a/features/cellular/framework/AT/AT_CellularPower.h b/features/cellular/framework/AT/AT_CellularPower.h deleted file mode 100644 index dfa4fb8fc55..00000000000 --- a/features/cellular/framework/AT/AT_CellularPower.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef AT_CELLULAR_POWER_H_ -#define AT_CELLULAR_POWER_H_ - -#include "CellularPower.h" -#include "AT_CellularBase.h" - -namespace mbed { - -/** - * Class AT_CellularPower - * - * Class that provides power handling functions for modem/module. - */ -class AT_CellularPower : public CellularPower, public AT_CellularBase { -public: - AT_CellularPower(ATHandler &atHandler); - virtual ~AT_CellularPower(); - -public: - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); - - virtual nsapi_error_t set_at_mode(); - - virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL); - - virtual nsapi_error_t reset(); - - virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time); - - virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value); - - virtual nsapi_error_t is_device_ready(); - - virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback); - - virtual void remove_device_ready_urc_cb(mbed::Callback callback); -}; - -} // namespace mbed - -#endif /* AT_CELLULAR_POWER_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularSMS.cpp b/features/cellular/framework/AT/AT_CellularSMS.cpp index 4efb012d529..435f513812e 100644 --- a/features/cellular/framework/AT/AT_CellularSMS.cpp +++ b/features/cellular/framework/AT/AT_CellularSMS.cpp @@ -210,10 +210,9 @@ void AT_CellularSMS::cmti_urc() nsapi_error_t AT_CellularSMS::set_cnmi() { - if (!is_supported(AT_CNMI)) { + if (!get_property(PROPERTY_AT_CNMI)) { return NSAPI_ERROR_UNSUPPORTED; } - _at.lock(); _at.cmd_start("AT+CNMI=2,1"); _at.cmd_stop_read_resp(); @@ -222,10 +221,9 @@ nsapi_error_t AT_CellularSMS::set_cnmi() nsapi_error_t AT_CellularSMS::set_cmgf(int msg_format) { - if (!is_supported(AT_CMGF)) { + if (!get_property(PROPERTY_AT_CMGF)) { return NSAPI_ERROR_UNSUPPORTED; } - _at.lock(); _at.cmd_start("AT+CMGF="); _at.write_int(msg_format); @@ -235,10 +233,9 @@ nsapi_error_t AT_CellularSMS::set_cmgf(int msg_format) nsapi_error_t AT_CellularSMS::set_csmp(int fo, int vp, int pid, int dcs) { - if (!is_supported(AT_CSMP)) { + if (!get_property(PROPERTY_AT_CSMP)) { return NSAPI_ERROR_UNSUPPORTED; } - _at.lock(); _at.cmd_start("AT+CSMP="); _at.write_int(fo); @@ -251,10 +248,9 @@ nsapi_error_t AT_CellularSMS::set_csmp(int fo, int vp, int pid, int dcs) nsapi_error_t AT_CellularSMS::set_csdh(int show_header) { - if (!is_supported(AT_CSDH)) { + if (!get_property(PROPERTY_AT_CSDH)) { return NSAPI_ERROR_UNSUPPORTED; } - _at.lock(); _at.cmd_start("AT+CSDH="); _at.write_int(show_header); @@ -412,10 +408,6 @@ char *AT_CellularSMS::create_pdu(const char *phone_number, const char *message, nsapi_size_or_error_t AT_CellularSMS::send_sms(const char *phone_number, const char *message, int msg_len) { - if ((_mode == CellularSMSMmodeText) && (!is_supported(AT_CellularSMSMmodeText))) { - return NSAPI_ERROR_UNSUPPORTED; - } - int single_sms_max_length = _use_8bit_encoding ? SMS_MAX_SIZE_8BIT_SINGLE_SMS_SIZE : SMS_MAX_SIZE_GSM7_SINGLE_SMS_SIZE; if ((_mode == CellularSMSMmodeText && msg_len > single_sms_max_length) || !phone_number) { diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp index 26d7feb9f1c..3b21c90c755 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp @@ -99,6 +99,10 @@ void GEMALTO_CINTERION::init_module_bgs2() 0, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp index a7203fb0c43..5358346b664 100644 --- a/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp +++ b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp @@ -28,6 +28,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 1, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 1, // PROPERTY_IPV6_STACK 1, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp index 55fe2df1de2..79f617c82e7 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp @@ -28,6 +28,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 0, // AT_CGSN_WITH_TYPE 0, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp index a76aedccb94..ef35e4f3a52 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp @@ -35,6 +35,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 1, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 0, // AT_CGAUTH, BC95_AT_Commands_Manual_V1.9 + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp index 092652f354a..f45568cba87 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp @@ -37,6 +37,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 0, // AT_CGSN_WITH_TYPE 0, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/QUECTEL/M26/QUECTEL_M26.cpp b/features/cellular/framework/targets/QUECTEL/M26/QUECTEL_M26.cpp index 612628a7a03..ee6b27dd7c0 100644 --- a/features/cellular/framework/targets/QUECTEL/M26/QUECTEL_M26.cpp +++ b/features/cellular/framework/targets/QUECTEL/M26/QUECTEL_M26.cpp @@ -31,6 +31,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 0, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 0, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp index 695ce9f5025..ebb20b1c731 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp +++ b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp @@ -34,6 +34,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 1, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp index cd25409d82b..6c40f91f96b 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp +++ b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp @@ -28,6 +28,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 0, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 0, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp index b4bb6cbfb3d..33d3b808a40 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp @@ -29,7 +29,11 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { AT_CellularNetwork::RegistrationModeLAC, // C_REG 0, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA - 1, // AT_CGAUTH, + 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK @@ -42,6 +46,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 1, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp index 35dd062b029..cdce035e50d 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp @@ -16,31 +16,42 @@ */ #include "UBLOX_N2XX.h" -#include "UBLOX_N2XX_CellularNetwork.h" -#include "UBLOX_N2XX_CellularPower.h" -#include "UBLOX_N2XX_CellularContext.h" -#include "UBLOX_N2XX_CellularSIM.h" -#include "UBLOX_N2XX_CellularSMS.h" using namespace mbed; using namespace events; -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CellularSMSMmodeText, - AT_CellularBase::AT_CMGF, - AT_CellularBase::AT_CNMI, - AT_CellularBase::AT_CSMP, - AT_CellularBase::AT_CSDH, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeDisable, // C_GREG + AT_CellularNetwork::RegistrationModeDisable, // C_REG + 1, // AT_CGSN_WITH_TYPE + 0, // AT_CGDATA + 0, // AT_CGAUTH + 0, // AT_CNMI + 0, // AT_CSMP + 0, // AT_CMGF + 0, // AT_CSDH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; UBLOX_N2XX::UBLOX_N2XX(FileHandle *fh): AT_CellularDevice(fh) { - AT_CellularBase::set_unsupported_features(unsupported_features); + AT_CellularBase::set_cellular_properties(cellular_properties); + _at->set_urc_handler("+NPIN:", mbed::Callback(this, &UBLOX_N2XX::NPIN_URC)); + memset(simstr,0,sizeof(simstr)); } UBLOX_N2XX::~UBLOX_N2XX() { + _at->set_urc_handler("+NPIN:", NULL); +} + +// Callback for Sim Pin. +void UBLOX_N2XX::NPIN_URC() +{ + _at->read_string(simstr, sizeof(simstr)); } AT_CellularNetwork *UBLOX_N2XX::open_network_impl(ATHandler &at) @@ -48,22 +59,104 @@ AT_CellularNetwork *UBLOX_N2XX::open_network_impl(ATHandler &at) return new UBLOX_N2XX_CellularNetwork(at); } -AT_CellularPower *UBLOX_N2XX::open_power_impl(ATHandler &at) +AT_CellularContext *UBLOX_N2XX::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { - return new UBLOX_N2XX_CellularPower(at); + return new UBLOX_N2XX_CellularContext(at, this, apn, cp_req, nonip_req); } -AT_CellularContext *UBLOX_N2XX::create_context_impl(ATHandler &at, const char *apn) +AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at) { - return new UBLOX_N2XX_CellularContext(at, this, apn); + return new UBLOX_N2XX_CellularSMS(at); } -AT_CellularSIM *UBLOX_N2XX::open_sim_impl(ATHandler &at) + +nsapi_error_t UBLOX_N2XX::init() { - return new UBLOX_N2XX_CellularSIM(at); + _at->set_at_timeout(5000); + _at->lock(); + _at->flush(); + _at->cmd_start("AT"); // echo off + _at->cmd_stop_read_resp(); + + _at->cmd_start("AT+CMEE=1"); // verbose responses + _at->cmd_stop_read_resp(); + +#ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN + set_pin(MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN); + wait(1); + _at->cmd_start("AT+CFUN=1"); // set full functionality + _at->cmd_stop_read_resp(); +#endif + _at->restore_at_timeout(); + return _at->unlock_return_error(); } -AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at) +nsapi_error_t UBLOX_N2XX::get_sim_state(SimState &state) { - return new UBLOX_N2XX_CellularSMS(at); + nsapi_error_t error = NSAPI_ERROR_DEVICE_ERROR; + + _at->lock(); + _at->flush(); + _at->cmd_start("AT+CFUN=1"); + _at->cmd_stop(); + + _at->resp_start(); + _at->read_string(simstr, sizeof(simstr)); + _at->resp_stop(); + error = _at->unlock_return_error(); + + int len = strlen(simstr); + if (len > 0 || error == NSAPI_ERROR_OK) { + if (error == NSAPI_ERROR_OK) { + state = SimStateReady; + } else if (len >= 6 && memcmp(simstr, "ENTER PIN", 9) == 0) { + state = SimStatePinNeeded; + } else { + simstr[len] = '\0'; + tr_error("Unknown SIM state %s", simstr); + state = SimStateUnknown; + } + error = NSAPI_ERROR_OK; + } else { + tr_warn("SIM not readable."); + state = SimStateUnknown; // SIM may not be ready yet or pin command may not be supported + } + +#if MBED_CONF_MBED_TRACE_ENABLE + switch (state) { + case SimStatePinNeeded: + tr_info("SIM PIN required"); + break; + case SimStatePukNeeded: + tr_error("SIM PUK required"); + break; + case SimStateUnknown: + tr_warn("SIM state unknown"); + break; + default: + tr_info("SIM is ready"); + break; + } +#endif + return error; +} + +nsapi_error_t UBLOX_N2XX::set_pin(const char *sim_pin) +{ + // if SIM is already in ready state then settings the PIN + // will return error so let's check the state before settings the pin. + SimState state; + if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { + return NSAPI_ERROR_OK; + } + + if (sim_pin == NULL) { + return NSAPI_ERROR_PARAMETER; + } + + _at->lock(); + _at->cmd_start("AT+NPIN=0,"); + _at->write_string(sim_pin); + _at->cmd_stop_read_resp(); + return _at->unlock_return_error(); } diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h index 42c88aa0db7..8836a017a5e 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h @@ -18,7 +18,12 @@ #ifndef UBLOX_N2XX_H_ #define UBLOX_N2XX_H_ +#include "mbed.h" +#include "CellularLog.h" #include "AT_CellularDevice.h" +#include "UBLOX_N2XX_CellularSMS.h" +#include "UBLOX_N2XX_CellularNetwork.h" +#include "UBLOX_N2XX_CellularContext.h" namespace mbed { @@ -29,17 +34,29 @@ class UBLOX_N2XX : public AT_CellularDevice { UBLOX_N2XX(FileHandle *fh); virtual ~UBLOX_N2XX(); + virtual nsapi_error_t init(); + virtual nsapi_error_t get_sim_state(SimState &state); + virtual nsapi_error_t set_pin(const char *sim_pin); + + protected: // AT_CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); - virtual AT_CellularSIM *open_sim_impl(ATHandler &at); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); virtual AT_CellularSMS *open_sms_impl(ATHandler &at); public: // NetworkInterface void handle_urc(FileHandle *fh); + +private: + + static const int MAX_SIM_RESPONSE_LENGTH = 25; + + // URC handlers + void NPIN_URC(); + + char simstr[MAX_SIM_RESPONSE_LENGTH]; }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp index 9e68a0ac757..2d39c10429d 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.cpp @@ -16,12 +16,13 @@ */ #include "UBLOX_N2XX_CellularContext.h" #include "UBLOX_N2XX_CellularStack.h" +#include "CellularLog.h" #include "APN_db.h" namespace mbed { -UBLOX_N2XX_CellularContext::UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) +UBLOX_N2XX_CellularContext::UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req): + AT_CellularContext(at, device, apn, cp_req, nonip_req) { } @@ -29,17 +30,18 @@ UBLOX_N2XX_CellularContext::~UBLOX_N2XX_CellularContext() { } +#if !NSAPI_PPP_AVAILABLE NetworkStack *UBLOX_N2XX_CellularContext::get_stack() { + if (_pdp_type == NON_IP_PDP_TYPE || _cp_in_use) { + tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()"); + return NULL; + } if (!_stack) { - _stack = new UBLOX_N2XX_CellularStack(_at, _cid, _ip_stack_type); + _stack = new UBLOX_N2XX_CellularStack(_at, _cid, (nsapi_ip_stack_t)_pdp_type); } return _stack; } - -bool UBLOX_N2XX_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - return stack_type == IPV4_STACK ? true : false; -} +#endif } /* namespace mbed */ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h index 9db5e1855e2..d2bd9d33277 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularContext.h @@ -25,14 +25,15 @@ class UBLOX_N2XX_CellularContext: public AT_CellularContext { public: - UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + UBLOX_N2XX_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~UBLOX_N2XX_CellularContext(); protected: +#if !NSAPI_PPP_AVAILABLE virtual NetworkStack *get_stack(); +#endif - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); }; } /* namespace mbed */ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp index dfa6f41de73..b800b4018ac 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp @@ -19,7 +19,7 @@ using namespace mbed; -UBLOX_N2XX_CellularNetwork::UBLOX_N2XX_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) +UBLOX_N2XX_CellularNetwork::UBLOX_N2XX_CellularNetwork(ATHandler &atHandler): AT_CellularNetwork(atHandler) { _op_act = RAT_UNKNOWN; } @@ -31,11 +31,6 @@ UBLOX_N2XX_CellularNetwork::~UBLOX_N2XX_CellularNetwork() } } -AT_CellularNetwork::RegistrationMode UBLOX_N2XX_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_EREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - nsapi_error_t UBLOX_N2XX_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) // Not understandable, RAT (+CEDRXS) { switch (opRat) { @@ -51,26 +46,3 @@ nsapi_error_t UBLOX_N2XX_CellularNetwork::set_access_technology_impl(RadioAccess return NSAPI_ERROR_OK; } - -nsapi_error_t UBLOX_N2XX_CellularNetwork::get_signal_quality(int &rssi, int &ber) -{ - _at.lock(); - - _at.cmd_start("AT+CSQ"); - _at.cmd_stop(); - - _at.resp_start("+CSQ:"); - rssi = _at.read_int(); - ber = _at.read_int(); - _at.resp_stop(); - if (rssi < 0 || ber < 0) { - _at.unlock(); - return NSAPI_ERROR_DEVICE_ERROR; - } - - if (rssi == 99) { - rssi = 0; - } - - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h index 05ed52b315e..be9308ec771 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h @@ -32,10 +32,6 @@ class UBLOX_N2XX_CellularNetwork : public AT_CellularNetwork { protected: virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); - - virtual RegistrationMode has_registration(RegistrationType rat); - - virtual nsapi_error_t get_signal_quality(int &rssi, int &ber); }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp deleted file mode 100644 index 97671819789..00000000000 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "UBLOX_N2XX_CellularPower.h" -#include "onboard_modem_api.h" -#include "platform/mbed_wait_api.h" - -using namespace mbed; - -const int MAX_SIM_RESPONSE_LENGTH = 25; - -UBLOX_N2XX_CellularPower::UBLOX_N2XX_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) -{ -} - -UBLOX_N2XX_CellularPower::~UBLOX_N2XX_CellularPower() -{ -} - -nsapi_error_t UBLOX_N2XX_CellularPower::on() -{ -#if MODEM_ON_BOARD - ::onboard_modem_init(); - ::onboard_modem_power_up(); -#endif - return NSAPI_ERROR_OK; -} - -nsapi_error_t UBLOX_N2XX_CellularPower::off() -{ -#if MODEM_ON_BOARD - ::onboard_modem_power_down(); -#endif - return NSAPI_ERROR_OK; -} - -nsapi_error_t UBLOX_N2XX_CellularPower::set_at_mode() -{ - _at.lock(); - _at.flush(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - _at.cmd_start("AT+CMEE=1"); // verbose responses - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - - -nsapi_error_t UBLOX_N2XX_CellularPower::set_power_level(int func_level, int do_reset, const char *sim_pin) -{ - char simstr[MAX_SIM_RESPONSE_LENGTH]; - - _at.lock(); - if(func_level == 1) { - _at.cmd_start("AT+CFUN=1"); - _at.cmd_stop(); - _at.resp_start(); - _at.read_string(simstr, sizeof(simstr)); - _at.resp_stop(); - _at.get_last_error(); - _at.clear_error(); - - if (memcmp(simstr, "ENTER PIN", 9) == 0) { - _at.cmd_start("AT+NPIN=0,"); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - _at.get_last_error(); - _at.clear_error(); - wait_ms(1000); - } - } - _at.cmd_start("AT+CFUN="); - _at.write_int(func_level); - _at.write_int(do_reset); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp deleted file mode 100644 index 0f5b2ff431d..00000000000 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "UBLOX_N2XX_CellularSIM.h" -#include "CellularLog.h" -#include "platform/mbed_wait_api.h" - -using namespace mbed; - -UBLOX_N2XX_CellularSIM::UBLOX_N2XX_CellularSIM(ATHandler &atHandler) : AT_CellularSIM(atHandler) -{ - _at.set_urc_handler("+NPIN:", callback(this, &UBLOX_N2XX_CellularSIM::NPIN_URC)); - memset(simstr,0,sizeof(simstr)); -} - -UBLOX_N2XX_CellularSIM::~UBLOX_N2XX_CellularSIM() -{ -} - -// Callback for Sim Pin. -void UBLOX_N2XX_CellularSIM::NPIN_URC() -{ - _at.read_string(simstr, sizeof(simstr)); -} - -nsapi_error_t UBLOX_N2XX_CellularSIM::get_sim_state(SimState &state) -{ - nsapi_error_t error = NSAPI_ERROR_DEVICE_ERROR; - - _at.lock(); - _at.flush(); - _at.cmd_start("AT+CFUN=1"); - _at.cmd_stop(); - - _at.resp_start(); - _at.read_string(simstr, sizeof(simstr)); - _at.resp_stop(); - error = _at.unlock_return_error(); - - int len = strlen(simstr); - if (len > 0 || error == NSAPI_ERROR_OK) { - if (error == NSAPI_ERROR_OK) { - state = SimStateReady; - } else if (len >= 6 && memcmp(simstr, "ENTER PIN", 9) == 0) { - state = SimStatePinNeeded; - } else { - simstr[len] = '\0'; - tr_error("Unknown SIM state %s", simstr); - state = SimStateUnknown; - } - error = NSAPI_ERROR_OK; - } else { - tr_warn("SIM not readable."); - state = SimStateUnknown; // SIM may not be ready yet or pin command may not be supported - } - -#if MBED_CONF_MBED_TRACE_ENABLE - switch (state) { - case SimStatePinNeeded: - tr_info("SIM PIN required"); - break; - case SimStatePukNeeded: - tr_error("SIM PUK required"); - break; - case SimStateUnknown: - tr_warn("SIM state unknown"); - break; - default: - tr_info("SIM is ready"); - break; - } -#endif - return error; -} - -nsapi_error_t UBLOX_N2XX_CellularSIM::set_pin(const char *sim_pin) -{ - // if SIM is already in ready state then settings the PIN - // will return error so let's check the state before settings the pin. - SimState state; - if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { - return NSAPI_ERROR_OK; - } - - _at.lock(); - _at.cmd_start("AT+NPIN=0,"); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t UBLOX_N2XX_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) -{ - _at.lock(); - _at.cmd_start("AT+NPIN=1,"); - _at.write_string(sim_pin); - _at.write_string(new_pin); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t UBLOX_N2XX_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) -{ - _at.lock(); - if (query_pin) { - /* use the SIM locked */ - _at.cmd_start("AT+NPIN=2,"); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - } else { - /* use the SIM unlocked */ - _at.cmd_start("AT+NPIN=3,"); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - } - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h deleted file mode 100644 index 0e2821ce7dd..00000000000 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSIM.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UBLOX_N2XX_CELLULAR_SIM_H_ -#define UBLOX_N2XX_CELLULAR_SIM_H_ - -#include "AT_CellularSIM.h" - -namespace mbed { - -class UBLOX_N2XX_CellularSIM : public AT_CellularSIM { - -public: - - UBLOX_N2XX_CellularSIM(ATHandler &atHandler); - virtual ~UBLOX_N2XX_CellularSIM(); - -public: - - virtual nsapi_error_t set_pin(const char *sim_pin); - - virtual nsapi_error_t change_pin(const char *sim_pin, const char *new_pin); - - virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin); - - virtual nsapi_error_t get_sim_state(SimState &state); - -private: - - static const int MAX_SIM_RESPONSE_LENGTH = 25; - - // URC handlers - void NPIN_URC(); - - char simstr[MAX_SIM_RESPONSE_LENGTH]; -}; - -} // namespace mbed - -#endif // UBLOX_N2XX_CELLULAR_SIM_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h index 4e75eb571e4..eee36417080 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularSMS.h @@ -25,11 +25,10 @@ namespace mbed { class UBLOX_N2XX_CellularSMS : public AT_CellularSMS { public: + UBLOX_N2XX_CellularSMS(ATHandler &atHandler); virtual ~UBLOX_N2XX_CellularSMS(); -public: - virtual nsapi_error_t set_cpms(const char *memr, const char *memw, const char *mems); virtual nsapi_size_or_error_t set_cscs(const char *chr_set); @@ -38,6 +37,7 @@ class UBLOX_N2XX_CellularSMS : public AT_CellularSMS { char *time_stamp, uint16_t time_len, int *buf_size); virtual nsapi_error_t delete_all_messages(); + }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp index b8dd2073d6d..d9d8b2ebdea 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp @@ -22,7 +22,7 @@ using namespace mbed; using namespace mbed_cellular_util; -UBLOX_N2XX_CellularStack::UBLOX_N2XX_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : AT_CellularStack(atHandler, cid, stack_type) +UBLOX_N2XX_CellularStack::UBLOX_N2XX_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type): AT_CellularStack(atHandler, cid, stack_type) { // URC handlers for sockets _at.set_urc_handler("+NSONMI:", callback(this, &UBLOX_N2XX_CellularStack::NSONMI_URC)); @@ -30,6 +30,7 @@ UBLOX_N2XX_CellularStack::UBLOX_N2XX_CellularStack(ATHandler &atHandler, int cid UBLOX_N2XX_CellularStack::~UBLOX_N2XX_CellularStack() { + _at.set_urc_handler("+NSONMI:", NULL); } nsapi_error_t UBLOX_N2XX_CellularStack::socket_listen(nsapi_socket_t handle, int backlog) @@ -246,19 +247,3 @@ nsapi_error_t UBLOX_N2XX_CellularStack::socket_close_impl(int sock_id) return _at.unlock_return_error(); } - -// Find or create a socket from the list. -UBLOX_N2XX_CellularStack::CellularSocket *UBLOX_N2XX_CellularStack::find_socket(int id) -{ - CellularSocket *socket = NULL; - - for (unsigned int x = 0; (socket == NULL) && (x < N2XX_MAX_SOCKET); x++) { - if (_socket) { - if (_socket[x]->id == id) { - socket = (_socket[x]); - } - } - } - - return socket; -} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h index 2f9f7bde3a1..638e2217b5d 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h @@ -40,10 +40,6 @@ class UBLOX_N2XX_CellularStack : public AT_CellularStack { protected: - /** Socket "unused" value. - */ - static const int SOCKET_UNUSED = -1; - /** Socket timeout value in milliseconds. * Note: the sockets layer above will retry the * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK @@ -78,13 +74,6 @@ class UBLOX_N2XX_CellularStack : public AT_CellularStack { // URC handlers void NSONMI_URC(); - - /** Find a socket from the list. - * - * @param id Socket ID. - * @return Socket if True, otherwise NULL. - */ - CellularSocket *find_socket(int id = SOCKET_UNUSED); }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/N2XX/mbed_lib.json b/features/cellular/framework/targets/UBLOX/N2XX/mbed_lib.json new file mode 100644 index 00000000000..e4af2bade84 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/N2XX/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "UBLOX_N2XX", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 9600 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp index 0acc86b7c5b..2f09de4d759 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp +++ b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp @@ -29,6 +29,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 0, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK @@ -41,6 +45,10 @@ static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { 1, // AT_CGSN_WITH_TYPE 1, // AT_CGDATA 1, // AT_CGAUTH + 1, // AT_CNMI + 1, // AT_CSMP + 1, // AT_CMGF + 1, // AT_CSDH 1, // PROPERTY_IPV4_STACK 0, // PROPERTY_IPV6_STACK 0, // PROPERTY_IPV4V6_STACK diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp index fd630a8422d..b6ec7052ed2 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp @@ -18,17 +18,21 @@ #include "ONBOARD_UBLOX_AT.h" #include "ONBOARD_UBLOX_PPP.h" +#include "ONBOARD_UBLOX_N2XX.h" #include "UARTSerial.h" using namespace mbed; CellularDevice *CellularDevice::get_target_default_instance() { -#if defined(TARGET_UBLOX_C030_N211) || defined(TARGET_UBLOX_C030_R41XM) - static UARTSerial serial(MDMTXD, MDMRXD, 115200); +#if defined(TARGET_UBLOX_C030_R410M) + static UARTSerial serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); static ONBOARD_UBLOX_AT device(&serial); +#elif defined(TARGET_UBLOX_C030_N211) + static UARTSerial serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + static ONBOARD_UBLOX_N2XX device(&serial); #else - static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static UARTSerial serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); static ONBOARD_UBLOX_PPP device(&serial); #endif return &device; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.cpp new file mode 100644 index 00000000000..4d867beab0d --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.cpp @@ -0,0 +1,52 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if MBED_CONF_NSAPI_PRESENT + +#include "ONBOARD_UBLOX_N2XX.h" +#include "cellular/onboard_modem_api.h" + +using namespace mbed; + +ONBOARD_UBLOX_N2XX::ONBOARD_UBLOX_N2XX(FileHandle *fh) : UBLOX_N2XX(fh) +{ +} + +nsapi_error_t ONBOARD_UBLOX_N2XX::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_N2XX::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_N2XX::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_N2XX::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +#endif // MBED_CONF_NSAPI_PRESENT diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.h similarity index 50% rename from features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.h index d0037a219b6..a12a5a55ffe 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularPower.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_N2XX.h @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,31 +14,23 @@ * limitations under the License. */ -#ifndef UBLOX_N2XX_CELLULARPOWER_H_ -#define UBLOX_N2XX_CELLULARPOWER_H_ +#ifndef ONBOARD_UBLOX_N2XX_ +#define ONBOARD_UBLOX_N2XX_ -#include "AT_CellularPower.h" +#include "UBLOX_N2XX.h" namespace mbed { -class UBLOX_N2XX_CellularPower : public AT_CellularPower { - -public: - - UBLOX_N2XX_CellularPower(ATHandler &atHandler); - virtual ~UBLOX_N2XX_CellularPower(); - +class ONBOARD_UBLOX_N2XX : public UBLOX_N2XX { public: + ONBOARD_UBLOX_N2XX(FileHandle *fh); - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); - - virtual nsapi_error_t set_at_mode(); - - virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0, const char *sim_pin = NULL); + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); }; } // namespace mbed -#endif // UBLOX_N2XX_CELLULARPOWER_H_ +#endif // ONBOARD_UBLOX_N2XX_ From d34b0475a9fe49217509f163464482a8b9997e5b Mon Sep 17 00:00:00 2001 From: mudassar-ublox Date: Thu, 28 Feb 2019 14:20:55 +0500 Subject: [PATCH 3/5] C030_N211 api's update and removed N2XX_CellularNetwork class --- .../UBLOX/AT/UBLOX_AT_CellularStack.cpp | 7 +-- .../targets/UBLOX/N2XX/UBLOX_N2XX.cpp | 6 --- .../framework/targets/UBLOX/N2XX/UBLOX_N2XX.h | 12 ++++- .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp | 48 ------------------- .../UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h | 39 --------------- .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp | 30 ++++-------- 6 files changed, 24 insertions(+), 118 deletions(-) delete mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp delete mode 100644 features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp index 33e92ca4110..5ad4ac140b4 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp @@ -297,6 +297,10 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke port = _at.read_int(); usorf_sz = _at.read_int(); + if (usorf_sz > size) { + usorf_sz = size; + } + // Must use what +USORF returns here as it may be less or more than we asked for if (usorf_sz > socket->pending_bytes) { socket->pending_bytes = 0; @@ -304,9 +308,6 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke socket->pending_bytes -= usorf_sz; } - if (usorf_sz > size) { - usorf_sz = size; - } _at.read_bytes(&ch, 1); _at.read_bytes((uint8_t *)buffer + count, usorf_sz); _at.resp_stop(); diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp index cdce035e50d..5bfd6ba0dcc 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp @@ -54,11 +54,6 @@ void UBLOX_N2XX::NPIN_URC() _at->read_string(simstr, sizeof(simstr)); } -AT_CellularNetwork *UBLOX_N2XX::open_network_impl(ATHandler &at) -{ - return new UBLOX_N2XX_CellularNetwork(at); -} - AT_CellularContext *UBLOX_N2XX::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { return new UBLOX_N2XX_CellularContext(at, this, apn, cp_req, nonip_req); @@ -69,7 +64,6 @@ AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at) return new UBLOX_N2XX_CellularSMS(at); } - nsapi_error_t UBLOX_N2XX::init() { _at->set_at_timeout(5000); diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h index 8836a017a5e..4f96b1e772b 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.h @@ -18,11 +18,20 @@ #ifndef UBLOX_N2XX_H_ #define UBLOX_N2XX_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_UBLOX_N2XX_TX +#define MBED_CONF_UBLOX_N2XX_TX D1 +#endif +#ifndef MBED_CONF_UBLOX_N2XX_RX +#define MBED_CONF_UBLOX_N2XX_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "mbed.h" #include "CellularLog.h" #include "AT_CellularDevice.h" +#include "AT_CellularNetwork.h" #include "UBLOX_N2XX_CellularSMS.h" -#include "UBLOX_N2XX_CellularNetwork.h" #include "UBLOX_N2XX_CellularContext.h" namespace mbed { @@ -41,7 +50,6 @@ class UBLOX_N2XX : public AT_CellularDevice { protected: // AT_CellularDevice - virtual AT_CellularNetwork *open_network_impl(ATHandler &at); virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); virtual AT_CellularSMS *open_sms_impl(ATHandler &at); diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp deleted file mode 100644 index b800b4018ac..00000000000 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "UBLOX_N2XX_CellularNetwork.h" - -using namespace mbed; - -UBLOX_N2XX_CellularNetwork::UBLOX_N2XX_CellularNetwork(ATHandler &atHandler): AT_CellularNetwork(atHandler) -{ - _op_act = RAT_UNKNOWN; -} - -UBLOX_N2XX_CellularNetwork::~UBLOX_N2XX_CellularNetwork() -{ - if (_connection_status_cb) { - _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_ERROR_CONNECTION_LOST); - } -} - -nsapi_error_t UBLOX_N2XX_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) // Not understandable, RAT (+CEDRXS) -{ - switch (opRat) { -#if defined(TARGET_UBLOX_C030_N211) - case RAT_NB1: - break; -#endif - default: { - _op_act = RAT_UNKNOWN; - return NSAPI_ERROR_UNSUPPORTED; - } - } - - return NSAPI_ERROR_OK; -} diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h deleted file mode 100644 index be9308ec771..00000000000 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularNetwork.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UBLOX_N2XX_CELLULAR_NETWORK_H_ -#define UBLOX_N2XX_CELLULAR_NETWORK_H_ - -#include "AT_CellularNetwork.h" - -namespace mbed { - -class UBLOX_N2XX_CellularNetwork : public AT_CellularNetwork { - -public: - - UBLOX_N2XX_CellularNetwork(ATHandler &atHandler); - virtual ~UBLOX_N2XX_CellularNetwork(); - -protected: - - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); -}; - -} // namespace mbed - -#endif // UBLOX_N2XX_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp index d9d8b2ebdea..9730b6dd40e 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp @@ -119,17 +119,17 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_sendto_impl(CellularSocke return NSAPI_ERROR_PARAMETER; } - int sent_len = 0, len = 0; - char *dataStr = (char *) malloc((size * 2) + 1); - memset(dataStr, 0, size*2+1); + int sent_len = 0; + char *dataStr = new char [(size * 2) + 1](); + if (!dataStr) + return NSAPI_ERROR_NO_MEMORY; char_str_to_hex_str((const char*)data, size, dataStr); - len = strlen(dataStr); _at.cmd_start("AT+NSOST="); _at.write_int(socket->id); _at.write_string(address.get_ip_address()); _at.write_int(address.get_port()); - _at.write_int(len/2); + _at.write_int(size); _at.write_string(dataStr); _at.cmd_stop(); @@ -153,15 +153,12 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc nsapi_size_t read_blk, usorf_sz, count = 0, length = size*2; bool success = true; char ipAddress[NSAPI_IP_SIZE]; - char *tmpBuf = (char *) malloc(size*2+1); - memset(tmpBuf, 0, size*2+1); int port = 0; Timer timer; if (socket->pending_bytes == 0) { _at.process_oob(); if (socket->pending_bytes == 0) { - free(tmpBuf); return NSAPI_ERROR_WOULD_BLOCK; } } @@ -183,6 +180,11 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc _at.read_string(ipAddress, sizeof(ipAddress)); port = _at.read_int(); usorf_sz = _at.read_int(); + if (usorf_sz > length) { + usorf_sz = length; + } + _at.read_hex_string((char *)buffer + count, usorf_sz*2+1); + _at.resp_stop(); // Must use what +NSORF returns here as it may be less or more than we asked for if (usorf_sz >= socket->pending_bytes) { @@ -191,12 +193,6 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc socket->pending_bytes -= usorf_sz; } - if (usorf_sz > length) { - usorf_sz = length; - } - _at.read_string(tmpBuf + count, usorf_sz*2+1); - _at.resp_stop(); - if (usorf_sz > 0) { count += (usorf_sz*2); length -= (usorf_sz*2); @@ -220,10 +216,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc socket->pending_bytes = 0; if (!count || (_at.get_last_error() != NSAPI_ERROR_OK)) { - free(tmpBuf); return NSAPI_ERROR_WOULD_BLOCK; - } else { - nsapi_error_size = count; } if (success && socket->proto == NSAPI_UDP && address) { @@ -232,9 +225,6 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc address->set_port(port); } - hex_str_to_char_str(tmpBuf, count, (char*) buffer); - - free(tmpBuf); return nsapi_error_size = count/2; } From 578fb7ae2b9d033525d5e7451913e2f8d6af8c18 Mon Sep 17 00:00:00 2001 From: mudassar-ublox Date: Mon, 4 Mar 2019 16:47:15 +0500 Subject: [PATCH 4/5] updated api and removed Astyle issue --- .../cellular/TESTS/api/cellular_sms/main.cpp | 10 ++++----- .../framework/AT/AT_CellularStack.cpp | 4 +--- .../UBLOX/AT/UBLOX_AT_CellularStack.cpp | 21 ++++++++----------- .../targets/UBLOX/N2XX/UBLOX_N2XX.cpp | 5 ----- .../UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp | 12 +++++------ 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/features/cellular/TESTS/api/cellular_sms/main.cpp b/features/cellular/TESTS/api/cellular_sms/main.cpp index 89e4760444c..7947696dd13 100644 --- a/features/cellular/TESTS/api/cellular_sms/main.cpp +++ b/features/cellular/TESTS/api/cellular_sms/main.cpp @@ -111,20 +111,20 @@ static void test_sms_initialize_pdu_mode() { nsapi_error_t err = sms->initialize(CellularSMS::CellularSMSMmodePDU); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_cscs() { nsapi_error_t err = sms->set_cscs("IRA"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("UCS2"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("GSM"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_csca() @@ -144,7 +144,7 @@ static void test_set_cpms_me() { nsapi_error_t err = sms->set_cpms("ME", "ME", "ME"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } #ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER diff --git a/features/cellular/framework/AT/AT_CellularStack.cpp b/features/cellular/framework/AT/AT_CellularStack.cpp index 8c09f738494..432ca3da66c 100644 --- a/features/cellular/framework/AT/AT_CellularStack.cpp +++ b/features/cellular/framework/AT/AT_CellularStack.cpp @@ -132,9 +132,7 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc tr_info("Socket %d open", index); // create local socket structure, socket on modem is created when app calls sendto/recvfrom _socket[index] = new CellularSocket; - CellularSocket *psock; - psock = _socket[index]; - memset(psock, 0, sizeof(CellularSocket)); + CellularSocket *psock = _socket[index]; SocketAddress addr(0, get_dynamic_ip_port()); psock->id = index; psock->localAddress = addr; diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp index 5ad4ac140b4..0c49d85c0e6 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp @@ -296,10 +296,12 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke _at.read_string(ipAddress, sizeof(ipAddress)); port = _at.read_int(); usorf_sz = _at.read_int(); - if (usorf_sz > size) { usorf_sz = size; } + _at.read_bytes(&ch, 1); + _at.read_bytes((uint8_t *)buffer + count, usorf_sz); + _at.resp_stop(); // Must use what +USORF returns here as it may be less or more than we asked for if (usorf_sz > socket->pending_bytes) { @@ -308,10 +310,6 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke socket->pending_bytes -= usorf_sz; } - _at.read_bytes(&ch, 1); - _at.read_bytes((uint8_t *)buffer + count, usorf_sz); - _at.resp_stop(); - if (usorf_sz > 0) { count += usorf_sz; size -= usorf_sz; @@ -346,6 +344,12 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke _at.resp_start("+USORD:"); _at.skip_param(); // receiving socket id usorf_sz = _at.read_int(); + if (usorf_sz > size) { + usorf_sz = size; + } + _at.read_bytes(&ch, 1); + _at.read_bytes((uint8_t *)buffer + count, usorf_sz); + _at.resp_stop(); // Must use what +USORD returns here as it may be less or more than we asked for if (usorf_sz > socket->pending_bytes) { @@ -354,13 +358,6 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke socket->pending_bytes -= usorf_sz; } - if (usorf_sz > size) { - usorf_sz = size; - } - _at.read_bytes(&ch, 1); - _at.read_bytes((uint8_t *)buffer + count, usorf_sz); - _at.resp_stop(); - if (usorf_sz > 0) { count += usorf_sz; size -= usorf_sz; diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp index 5bfd6ba0dcc..0aa1ede2d74 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX.cpp @@ -66,7 +66,6 @@ AT_CellularSMS *UBLOX_N2XX::open_sms_impl(ATHandler &at) nsapi_error_t UBLOX_N2XX::init() { - _at->set_at_timeout(5000); _at->lock(); _at->flush(); _at->cmd_start("AT"); // echo off @@ -77,11 +76,7 @@ nsapi_error_t UBLOX_N2XX::init() #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN set_pin(MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN); - wait(1); - _at->cmd_start("AT+CFUN=1"); // set full functionality - _at->cmd_stop_read_resp(); #endif - _at->restore_at_timeout(); return _at->unlock_return_error(); } diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp index 9730b6dd40e..e9d0287aa04 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp @@ -138,7 +138,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_sendto_impl(CellularSocke sent_len = _at.read_int(); _at.resp_stop(); - free(dataStr); + delete[] dataStr; if ((_at.get_last_error() == NSAPI_ERROR_OK)) { return sent_len; } @@ -150,7 +150,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc void *buffer, nsapi_size_t size) { nsapi_size_or_error_t nsapi_error_size = NSAPI_ERROR_DEVICE_ERROR; - nsapi_size_t read_blk, usorf_sz, count = 0, length = size*2; + nsapi_size_t read_blk, usorf_sz, count = 0, length = size; bool success = true; char ipAddress[NSAPI_IP_SIZE]; int port = 0; @@ -183,7 +183,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc if (usorf_sz > length) { usorf_sz = length; } - _at.read_hex_string((char *)buffer + count, usorf_sz*2+1); + _at.read_hex_string((char *)buffer + count, usorf_sz); _at.resp_stop(); // Must use what +NSORF returns here as it may be less or more than we asked for @@ -194,8 +194,8 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc } if (usorf_sz > 0) { - count += (usorf_sz*2); - length -= (usorf_sz*2); + count += (usorf_sz); + length -= (usorf_sz); } else { // read() should not fail success = false; @@ -225,7 +225,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc address->set_port(port); } - return nsapi_error_size = count/2; + return nsapi_error_size = count; } nsapi_error_t UBLOX_N2XX_CellularStack::socket_close_impl(int sock_id) From f19a412a555c1f745eb53c0bb0e8a5f6f5ee3b15 Mon Sep 17 00:00:00 2001 From: mudassar-ublox Date: Wed, 6 Mar 2019 11:00:50 +0500 Subject: [PATCH 5/5] Applied suggested astyle fixes --- features/cellular/TESTS/api/cellular_sms/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/features/cellular/TESTS/api/cellular_sms/main.cpp b/features/cellular/TESTS/api/cellular_sms/main.cpp index 7947696dd13..79390bc0d6b 100644 --- a/features/cellular/TESTS/api/cellular_sms/main.cpp +++ b/features/cellular/TESTS/api/cellular_sms/main.cpp @@ -111,20 +111,20 @@ static void test_sms_initialize_pdu_mode() { nsapi_error_t err = sms->initialize(CellularSMS::CellularSMSMmodePDU); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_cscs() { nsapi_error_t err = sms->set_cscs("IRA"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("UCS2"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); err = sms->set_cscs("GSM"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } static void test_set_csca() @@ -144,7 +144,7 @@ static void test_set_cpms_me() { nsapi_error_t err = sms->set_cpms("ME", "ME", "ME"); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED || (err == NSAPI_ERROR_DEVICE_ERROR && - ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); + ((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY)); } #ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER