From d65b82da9deb883b45537487e8cdb2b78654c43c Mon Sep 17 00:00:00 2001 From: Kimmo Vaisanen Date: Thu, 19 Mar 2020 09:06:45 +0200 Subject: [PATCH] Cellular: ALT1250 PPP cellular driver for mbed-os 5.15 This commit introduces Altair ALT1250 PPP mode cellular driver for mbed-os 5.15 -branch. --- .../Altair/ALT1250/PPP/ALT1250_PPP.cpp | 121 ++++++++++++++++++ .../targets/Altair/ALT1250/PPP/ALT1250_PPP.h | 48 +++++++ .../PPP/ALT1250_PPP_CellularContext.cpp | 44 +++++++ .../ALT1250/PPP/ALT1250_PPP_CellularContext.h | 35 +++++ .../PPP/ALT1250_PPP_CellularNetwork.cpp | 68 ++++++++++ .../ALT1250/PPP/ALT1250_PPP_CellularNetwork.h | 36 ++++++ .../targets/Altair/ALT1250/PPP/mbed_lib.json | 33 +++++ 7 files changed, 385 insertions(+) create mode 100755 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp create mode 100644 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.h create mode 100644 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.cpp create mode 100644 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.h create mode 100755 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.cpp create mode 100755 features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.h create mode 100644 features/cellular/framework/targets/Altair/ALT1250/PPP/mbed_lib.json diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp new file mode 100755 index 0000000000..b38dee16d7 --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2020, 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 "ALT1250_PPP.h" +#include "ALT1250_PPP_CellularContext.h" +#include "AT_CellularNetwork.h" +#include "ALT1250_PPP_CellularNetwork.h" +#include "CellularLog.h" +#include "rtos/ThisThread.h" + +using namespace rtos; +using namespace mbed; +using namespace events; + +#define CONNECT_DELIM "\r\n" +#define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format +#define CONNECT_TIMEOUT 8000 + +#if !defined(MBED_CONF_ALT1250_PPP_RST) +#define MBED_CONF_ALT1250_PPP_RST NC +#endif + +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeEnable,// C_EREG + AT_CellularNetwork::RegistrationModeDisable, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 1, // AT_CGSN_WITH_TYPE + 0, // 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 + 0, // PROPERTY_NON_IP_PDP_TYPE + 1, // PROPERTY_AT_CGEREP +}; + +ALT1250_PPP::ALT1250_PPP(FileHandle *fh, PinName rst, PinDirection pin_dir, PinMode pin_mode, bool value) + : AT_CellularDevice(fh), + _rst(rst, pin_dir, pin_mode, value) +{ + AT_CellularBase::set_cellular_properties(cellular_properties); +} + +AT_CellularContext *ALT1250_PPP::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) +{ + return new ALT1250_PPP_CellularContext(at, this, apn, cp_req, nonip_req); +} + +AT_CellularNetwork *ALT1250_PPP::open_network_impl(ATHandler &at) +{ + return new ALT1250_PPP_CellularNetwork(at); +} + +nsapi_error_t ALT1250_PPP::soft_power_on() +{ + // check if modem is already ready + _at->lock(); + _at->flush(); + _at->set_at_timeout(30); + _at->cmd_start("AT"); + _at->cmd_stop_read_resp(); + nsapi_error_t err = _at->get_last_error(); + _at->restore_at_timeout(); + _at->unlock(); + + // modem is not responding to AT commands, power it on + if (err != NSAPI_ERROR_OK) { + tr_warn("Modem is not responding to AT commands, reset it"); + if (_rst.is_connected()) { + _rst = 0; + ThisThread::sleep_for(100); + _rst = 1; + } + if (_at->sync(2000)) { + tr_warn("Modem is AT ready"); + return NSAPI_ERROR_OK; + } + tr_warn("Modem is not AT ready following reset"); + return _at->get_last_error(); + } + + return NSAPI_ERROR_OK; +} + +#if MBED_CONF_ALT1250_PPP_PROVIDE_DEFAULT + +#if !NSAPI_PPP_AVAILABLE +#error Must define lwip.ppp-enabled +#endif + +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() +{ + static UARTSerial serial(MBED_CONF_ALT1250_PPP_TX, MBED_CONF_ALT1250_PPP_RX, MBED_CONF_ALT1250_PPP_BAUDRATE); +#if defined (MBED_CONF_ALT1250_PPP_RTS) && defined (MBED_CONF_ALT1250_PPP_CTS) + tr_debug("ALT1250_PPP flow control: RTS %d CTS %d", MBED_CONF_ALT1250_PPP_RTS, MBED_CONF_ALT1250_PPP_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_ALT1250_PPP_RTS, MBED_CONF_ALT1250_PPP_CTS); +#endif + static ALT1250_PPP device(&serial, MBED_CONF_ALT1250_PPP_RST, PIN_OUTPUT, OpenDrainNoPull, 1); + return &device; +} +#endif + diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.h b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.h new file mode 100644 index 0000000000..d3049b611b --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020, 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 ALT1250_PPP_H_ +#define ALT1250_PPP_H_ + +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_ALT1250_PPP_TX +#define MBED_CONF_ALT1250_PPP_TX D1 +#endif +#ifndef MBED_CONF_ALT1250_PPP_RX +#define MBED_CONF_ALT1250_PPP_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + +#include "AT_CellularDevice.h" +#include "DigitalInOut.h" + +namespace mbed { + +class ALT1250_PPP : public AT_CellularDevice { +public: + ALT1250_PPP(FileHandle *fh, PinName rst = NC, PinDirection pin_dir = PIN_OUTPUT, PinMode pin_mode = PullUp, bool value = 1); + +protected: // AT_CellularDevice + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); + + AT_CellularNetwork *open_network_impl(ATHandler &at); + virtual nsapi_error_t soft_power_on(); + DigitalInOut _rst; +}; + +} // namespace mbed +#endif // ALT1250_PPP_H_ diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.cpp b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.cpp new file mode 100644 index 0000000000..49d7fccf5a --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020, 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 "ALT1250_PPP_CellularContext.h" + +namespace mbed { + +ALT1250_PPP_CellularContext::ALT1250_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularContext(at, device, apn, cp_req, nonip_req) +{ +} + +ALT1250_PPP_CellularContext::~ALT1250_PPP_CellularContext() +{ +} + +nsapi_error_t ALT1250_PPP_CellularContext::do_user_authentication() +{ + nsapi_error_t err = NSAPI_ERROR_OK; + if (_pwd && _uname) { + err = _at.at_cmd_discard("%PPPAUTH", "=", "%d%d%s%s", _cid, _authentication_type, + _uname, _pwd); + if (err != NSAPI_ERROR_OK) { + return NSAPI_ERROR_AUTH_FAILURE; + } + } + + return err; +} + +} /* namespace mbed */ diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.h b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.h new file mode 100644 index 0000000000..111ca56780 --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularContext.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020, 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 ALT1250_PPP_CELLULARCONTEXT_H_ +#define ALT1250_PPP_CELLULARCONTEXT_H_ + +#include "AT_CellularContext.h" + +namespace mbed { + +class ALT1250_PPP_CellularContext: public AT_CellularContext { +public: + ALT1250_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); + virtual ~ALT1250_PPP_CellularContext(); + +protected: + virtual nsapi_error_t do_user_authentication(); +}; + +} /* namespace mbed */ + +#endif // ALT1250_PPP_CELLULARCONTEXT_H_ diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.cpp b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.cpp new file mode 100755 index 0000000000..4f39d52fd6 --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2020, 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 "ALT1250_PPP_CellularNetwork.h" +#include "CellularLog.h" + +using namespace mbed; + +ALT1250_PPP_CellularNetwork::ALT1250_PPP_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) +{ +} + +ALT1250_PPP_CellularNetwork::~ALT1250_PPP_CellularNetwork() +{ +} + +nsapi_error_t ALT1250_PPP_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opsAct) +{ + _at.lock(); + + _at.set_at_timeout(10000); + char resp[20]; + _at.at_cmd_str("%RATACT", "?", resp, 20); + tr_debug("ALT1250_PPP RAT: %s", resp); + + switch (opsAct) { + case RAT_CATM1: + if (memcmp(resp, "CATM", 4)) { + _at.at_cmd_discard("%RATACT", "=\"CATM\""); + } + break; + case RAT_NB1: + if (memcmp(resp, "NBIOT", 5)) { + _at.at_cmd_discard("%RATACT", "=\"NBIOT\""); + } + break; + case RAT_GSM: + case RAT_GSM_COMPACT: + case RAT_UTRAN: + case RAT_EGPRS: + break; + default: + if (memcmp(resp, "DEFAULT", 7)) { + _at.at_cmd_discard("%RATACT", "=\"DEFAULT\""); + } + _at.unlock(); + _op_act = RAT_UNKNOWN; + return NSAPI_ERROR_UNSUPPORTED; + } + + _at.restore_at_timeout(); + + return _at.unlock_return_error(); +} diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.h b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.h new file mode 100755 index 0000000000..d8c485ca00 --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP_CellularNetwork.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020, 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 ALT1250_PPP_CELLULAR_NETWORK_H_ +#define ALT1250_PPP_CELLULAR_NETWORK_H_ + +#include "AT_CellularNetwork.h" + +namespace mbed { + +class ALT1250_PPP_CellularNetwork : public AT_CellularNetwork { +public: + ALT1250_PPP_CellularNetwork(ATHandler &atHandler); + virtual ~ALT1250_PPP_CellularNetwork(); + +protected: + virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); +}; + +} // namespace mbed + +#endif // ALT1250_PPP_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/mbed_lib.json b/features/cellular/framework/targets/Altair/ALT1250/PPP/mbed_lib.json new file mode 100644 index 0000000000..b5c2100674 --- /dev/null +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/mbed_lib.json @@ -0,0 +1,33 @@ +{ + "name": "ALT1250_PPP", + "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 + }, + "rst": { + "help": "Reset control pin", + "value": null + }, + "baudrate": { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +}