diff --git a/libraries/GSM/examples/GSMSSLClient/GSMSSLClient.ino b/libraries/GSM/examples/GSMSSLClient/GSMSSLClient.ino new file mode 100644 index 000000000..5d35c7d62 --- /dev/null +++ b/libraries/GSM/examples/GSMSSLClient/GSMSSLClient.ino @@ -0,0 +1,66 @@ +/* + GSMSSLlient + + This sketch connects to a website (https://ifconfig.me) + using the Portenta CAT.M1/NB IoT GNSS Shield and TLS. + + */ + +#include + +#include "arduino_secrets.h" +char pin[] = SECRET_PIN; +char apn[] = SECRET_APN; +char username[] = SECRET_USERNAME; +char pass[] = SECRET_PASSWORD; + +const char server[] = "ifconfig.me"; +const char* ip_address; +int port = 443; +GSMSSLClient client; + +void setup() { + Serial.begin(115200); + while(!Serial) {} + Serial.println("Starting Carrier Network registration"); + if(!GSM.begin(pin, apn, username, pass, CATM1, BAND_3 | BAND_20 | BAND_19)){ + Serial.println("The board was not able to register to the network..."); + // do nothing forevermore: + while(1); + } + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, port)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /ip HTTP/1.1"); + client.print("Host: "); + client.println(server); + client.println("Connection: close"); + client.println(); + } else { + Serial.println("unable to connect to server"); + } + +} + +void loop() { + + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while (true); + } + +} diff --git a/libraries/GSM/examples/GSMSSLClient/arduino_secrets.h b/libraries/GSM/examples/GSMSSLClient/arduino_secrets.h new file mode 100644 index 000000000..8c5842fa6 --- /dev/null +++ b/libraries/GSM/examples/GSMSSLClient/arduino_secrets.h @@ -0,0 +1,4 @@ +#define SECRET_PIN "" +#define SECRET_APN "" +#define SECRET_USERNAME "" +#define SECRET_PASSWORD "" diff --git a/libraries/GSM/src/GSM.cpp b/libraries/GSM/src/GSM.cpp index 1da0d49b3..19305540b 100644 --- a/libraries/GSM/src/GSM.cpp +++ b/libraries/GSM/src/GSM.cpp @@ -1,15 +1,34 @@ +/* + GSM.cpp - Library for GSM on mbed platforms. + Copyright (c) 2011-2023 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + #include "GSM.h" #include "mbed.h" #include "CellularLog.h" +#include "CellularDevice.h" #include "CellularContext.h" #include "CellularInterface.h" #include "GEMALTO_CINTERION_CellularStack.h" #define MAXRETRY 3 -bool _cmuxEnable = false; -arduino::CMUXClass * arduino::CMUXClass::get_default_instance() +arduino::CMUXClass *arduino::CMUXClass::get_default_instance() { static mbed::UnbufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200); serial.set_flow_control(mbed::SerialBase::RTSCTS_SW, MBED_CONF_GEMALTO_CINTERION_CTS, NC); @@ -19,24 +38,24 @@ arduino::CMUXClass * arduino::CMUXClass::get_default_instance() mbed::CellularDevice *mbed::CellularDevice::get_default_instance() { - static auto cmux = arduino::CMUXClass::get_default_instance(); - static mbed::GEMALTO_CINTERION device(cmux->get_serial(0)); - nextSerialPort++; - device.enableCMUXChannel = mbed::callback(cmux, &arduino::CMUXClass::enableCMUXChannel); - return &device; + static auto cmux = arduino::CMUXClass::get_default_instance(); + static mbed::GEMALTO_CINTERION device(cmux->get_serial(0)); + nextSerialPort++; + device.enableCMUXChannel = mbed::callback(cmux, &arduino::CMUXClass::enableCMUXChannel); + return &device; } int arduino::GSMClass::begin(const char* pin, const char* apn, const char* username, const char* password, RadioAccessTechnologyType rat, uint32_t band, bool restart) { if(restart || isCmuxEnable()) { - pinMode(PJ_10, OUTPUT); - digitalWrite(PJ_10, HIGH); + pinMode(MBED_CONF_GEMALTO_CINTERION_RST, OUTPUT); + digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, HIGH); delay(800); - digitalWrite(PJ_10, LOW); - pinMode(PJ_7, OUTPUT); - digitalWrite(PJ_7, LOW); + digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, LOW); + pinMode(MBED_CONF_GEMALTO_CINTERION_ON, OUTPUT); + digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, LOW); delay(1); - digitalWrite(PJ_7, HIGH); + digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, HIGH); delay(1); // this timer is to make sure that at boottime and when the CMUX is used, // ^SYSTART is received in time to avoid stranger behaviour @@ -50,7 +69,7 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern printf("Invalid context\n"); return 0; } - pinMode(PJ_7, INPUT_PULLDOWN); + pinMode(MBED_CONF_GEMALTO_CINTERION_ON, INPUT_PULLDOWN); static mbed::DigitalOut rts(MBED_CONF_GEMALTO_CINTERION_RTS, 0); @@ -99,11 +118,11 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern return connect_status == NSAPI_ERROR_OK ? 1 : 0; } -void arduino::GSMClass::enableCmux(){ +void arduino::GSMClass::enableCmux() { _cmuxGSMenable = true; } -bool arduino::GSMClass::isCmuxEnable(){ +bool arduino::GSMClass::isCmuxEnable() { return _cmuxGSMenable; } @@ -130,53 +149,16 @@ bool arduino::GSMClass::setTime(unsigned long const epoch, int const timezone) return _device->set_time(epoch, timezone); } -static PlatformMutex trace_mutex; - -static void trace_wait() +bool arduino::GSMClass::isConnected() { - trace_mutex.lock(); -} - -static void trace_release() -{ - trace_mutex.unlock(); -} - -static char* trace_time(size_t ss) -{ - static char time_st[50]; - auto ms = std::chrono::time_point_cast(rtos::Kernel::Clock::now()).time_since_epoch().count(); - //snprintf(time_st, 49, "[%08llums]", ms); - snprintf(time_st, 1, "\n"); - return time_st; -} - -static Stream* trace_stream = nullptr; -static void arduino_print(const char* c) { - if (trace_stream) { - trace_stream->println(c); + if (_context) { + return _context->is_connected(); + } else { + return false; } } -void arduino::GSMClass::debug(Stream& stream) { - -#if MBED_CONF_MBED_TRACE_ENABLE - - mbed_trace_init(); - trace_stream = &stream; - mbed_trace_print_function_set(arduino_print); - mbed_trace_prefix_function_set( &trace_time ); - - mbed_trace_mutex_wait_function_set(trace_wait); - mbed_trace_mutex_release_function_set(trace_release); - - mbed_cellular_trace::mutex_wait_function_set(trace_wait); - mbed_cellular_trace::mutex_release_function_set(trace_release); - -#endif - -} NetworkInterface* arduino::GSMClass::getNetwork() { return _context; diff --git a/libraries/GSM/src/GSM.h b/libraries/GSM/src/GSM.h index 8b48d3e84..278cb1e92 100644 --- a/libraries/GSM/src/GSM.h +++ b/libraries/GSM/src/GSM.h @@ -1,14 +1,17 @@ /* GSM.h - Library for GSM on mbed platforms. - Copyright (c) 2011-2021 Arduino LLC. All right reserved. + Copyright (c) 2011-2023 Arduino LLC. All right reserved. + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA @@ -34,6 +37,8 @@ #define MBED_CONF_GEMALTO_CINTERION_RX PI_9 #define MBED_CONF_GEMALTO_CINTERION_RTS PI_10 #define MBED_CONF_GEMALTO_CINTERION_CTS PI_13 +#define MBED_CONF_GEMALTO_CINTERION_RST PJ_10 +#define MBED_CONF_GEMALTO_CINTERION_ON PJ_7 #define MBED_CONF_APP_SOCK_TYPE 1 #if defined __has_include @@ -87,10 +92,12 @@ class GSMClass : public MbedSocketClass { bool setTime(unsigned long const epoch, int const timezone = 0); void enableCmux(); bool isCmuxEnable(); - void debug(Stream& stream); + void trace(Stream& stream); + void setTraceLevel(int trace_level, bool timestamp = false); int ping(const char* hostname, uint8_t ttl = 128); int ping(const String& hostname, uint8_t ttl = 128); int ping(IPAddress host, uint8_t ttl = 128); + bool isConnected(); friend class GSMClient; friend class GSMUDP; @@ -115,6 +122,7 @@ class GSMClass : public MbedSocketClass { extern GSMClass GSM; #include "GSMClient.h" +#include "GSMSSLClient.h" #include "GSMUdp.h" #endif diff --git a/libraries/GSM/src/GSMClient.cpp b/libraries/GSM/src/GSMClient.cpp new file mode 100644 index 000000000..71043da70 --- /dev/null +++ b/libraries/GSM/src/GSMClient.cpp @@ -0,0 +1,24 @@ +/* + GSMClient.cpp + Copyright (c) 2023 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "GSMClient.h" + +arduino::GSMClient::GSMClient(): MbedClient(100) { + +} diff --git a/libraries/GSM/src/GSMClient.h b/libraries/GSM/src/GSMClient.h index b2f4a9036..8ac465975 100644 --- a/libraries/GSM/src/GSMClient.h +++ b/libraries/GSM/src/GSMClient.h @@ -26,6 +26,10 @@ namespace arduino { class GSMClient : public MbedClient { +public: + GSMClient(); + +private: NetworkInterface *getNetwork() { return GSM.getNetwork(); } diff --git a/libraries/GSM/src/GSMSSLClient.cpp b/libraries/GSM/src/GSMSSLClient.cpp new file mode 100644 index 000000000..c953adb4f --- /dev/null +++ b/libraries/GSM/src/GSMSSLClient.cpp @@ -0,0 +1,24 @@ +/* + GSMSSLClient.cpp + Copyright (c) 2023 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "GSMSSLClient.h" + +arduino::GSMSSLClient::GSMSSLClient(): MbedSSLClient(100) { + +} diff --git a/libraries/GSM/src/GSMSSLClient.h b/libraries/GSM/src/GSMSSLClient.h new file mode 100644 index 000000000..2ea0ae713 --- /dev/null +++ b/libraries/GSM/src/GSMSSLClient.h @@ -0,0 +1,42 @@ +/* + GSMSSLClient.h + Copyright (c) 2023 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef GSMSSLCLIENT_H +#define GSMSSLCLIENT_H + +#include "GSM.h" +#include "MbedSSLClient.h" + +extern const char CA_CERTIFICATES[]; + +namespace arduino { + +class GSMSSLClient : public arduino::MbedSSLClient { +public: + GSMSSLClient(); + +private: + NetworkInterface *getNetwork() { + return GSM.getNetwork(); + } +}; + +} + +#endif /* GSMSSLCLIENT_H */ \ No newline at end of file diff --git a/libraries/GSM/src/GSMTrace.cpp b/libraries/GSM/src/GSMTrace.cpp new file mode 100644 index 000000000..8c65e192a --- /dev/null +++ b/libraries/GSM/src/GSMTrace.cpp @@ -0,0 +1,79 @@ +/* + GSM.h - Library for GSM on mbed platforms. + Copyright (c) 2011-2023 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include +#include + +#if MBED_CONF_MBED_TRACE_ENABLE + +static Stream* trace_stream = nullptr; +static PlatformMutex trace_mutex; +static char trace_timestamp[8]; + +static void trace_wait() { + trace_mutex.lock(); +} + +static void trace_release() { + trace_mutex.unlock(); +} + +static char* trace_time(size_t ss) { + auto ms = std::chrono::time_point_cast(rtos::Kernel::Clock::now()).time_since_epoch().count(); + snprintf(trace_timestamp, 8, "[%08llu]", ms); + return trace_timestamp; +} + +static void trace_println(const char* c) { + if (trace_stream) { + trace_stream->println(c); + } +} +#endif + +void arduino::GSMClass::setTraceLevel(int trace_level, bool timestamp) { +#if MBED_CONF_MBED_TRACE_ENABLE + switch(trace_level) { + case 0: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE); break; + case 1: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_CMD); break; + case 2: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ERROR); break; + case 3: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_WARN); break; + case 4: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO); break; + case 5: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_DEBUG); break; + case 6: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); break; + default: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); break; + } + + if (timestamp) { + mbed_trace_prefix_function_set( &trace_time ); + } +#endif +} + +void arduino::GSMClass::trace(Stream& stream) { +#if MBED_CONF_MBED_TRACE_ENABLE + trace_stream = &stream; + + mbed_trace_init(); + mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); + mbed_trace_print_function_set(trace_println); + mbed_trace_mutex_wait_function_set(trace_wait); + mbed_trace_mutex_release_function_set(trace_release); +#endif +} diff --git a/libraries/GSM/src/GSMUdp.h b/libraries/GSM/src/GSMUdp.h index 6b93bd966..519adff50 100644 --- a/libraries/GSM/src/GSMUdp.h +++ b/libraries/GSM/src/GSMUdp.h @@ -1,5 +1,5 @@ /* - WiFiUdp.h + GSMUdp.h Copyright (c) 2021 Arduino SA. All right reserved. This library is free software; you can redistribute it and/or diff --git a/libraries/SocketWrapper/src/MbedClient.cpp b/libraries/SocketWrapper/src/MbedClient.cpp index dfd856f77..49265c002 100644 --- a/libraries/SocketWrapper/src/MbedClient.cpp +++ b/libraries/SocketWrapper/src/MbedClient.cpp @@ -150,6 +150,11 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) { return 0; } + /* For TLS connection timeout needs to be configured before handshake starts + * otherwise socket timeout is not adopted. See TLSSocketWrapper::set_timeout(int timeout) + */ + sock->set_timeout(_timeout); + restart_connect: nsapi_error_t returnCode = static_cast(sock)->connect(socketAddress); int ret = 0; diff --git a/libraries/SocketWrapper/src/MbedClient.h b/libraries/SocketWrapper/src/MbedClient.h index a2132ebf3..eca0e5a34 100644 --- a/libraries/SocketWrapper/src/MbedClient.h +++ b/libraries/SocketWrapper/src/MbedClient.h @@ -49,6 +49,10 @@ class MbedClient : public arduino::Client { public: MbedClient(); + MbedClient(unsigned long timeout) { + _timeout = timeout; + } + // Copy constructor, to be used when a Client returned by server.available() // needs to "survive" event if it goes out of scope // Sample usage: Client* new_client = new Client(existing_client) diff --git a/libraries/SocketWrapper/src/MbedSSLClient.cpp b/libraries/SocketWrapper/src/MbedSSLClient.cpp index e0aa1d2dd..3233c8dba 100644 --- a/libraries/SocketWrapper/src/MbedSSLClient.cpp +++ b/libraries/SocketWrapper/src/MbedSSLClient.cpp @@ -1,5 +1,9 @@ #include "MbedSSLClient.h" +arduino::MbedSSLClient::MbedSSLClient(unsigned long timeout): MbedClient(timeout), _disableSNI{false} { + onBeforeConnect(mbed::callback(this, &MbedSSLClient::setRootCA)); +} + arduino::MbedSSLClient::MbedSSLClient(): _disableSNI{false} { onBeforeConnect(mbed::callback(this, &MbedSSLClient::setRootCA)); }; diff --git a/libraries/SocketWrapper/src/MbedSSLClient.h b/libraries/SocketWrapper/src/MbedSSLClient.h index c4705fc7b..4c010a684 100644 --- a/libraries/SocketWrapper/src/MbedSSLClient.h +++ b/libraries/SocketWrapper/src/MbedSSLClient.h @@ -33,6 +33,9 @@ class MbedSSLClient : public arduino::MbedClient { public: MbedSSLClient(); + + MbedSSLClient(unsigned long timeout); + virtual ~MbedSSLClient() { stop(); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy