Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Thales shield TLS connection #754

Closed
wants to merge 13 commits into from
66 changes: 66 additions & 0 deletions libraries/GSM/examples/GSMSSLClient/GSMSSLClient.ino
Original file line number Diff line number Diff line change
@@ -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 <GSM.h>

#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);
}

}
4 changes: 4 additions & 0 deletions libraries/GSM/examples/GSMSSLClient/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define SECRET_PIN ""
#define SECRET_APN ""
#define SECRET_USERNAME ""
#define SECRET_PASSWORD ""
98 changes: 40 additions & 58 deletions libraries/GSM/src/GSM.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}

Expand All @@ -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<std::chrono::milliseconds>(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;
Expand Down
12 changes: 10 additions & 2 deletions libraries/GSM/src/GSM.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -115,6 +122,7 @@ class GSMClass : public MbedSocketClass {
extern GSMClass GSM;

#include "GSMClient.h"
#include "GSMSSLClient.h"
#include "GSMUdp.h"

#endif
24 changes: 24 additions & 0 deletions libraries/GSM/src/GSMClient.cpp
Original file line number Diff line number Diff line change
@@ -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) {

}
4 changes: 4 additions & 0 deletions libraries/GSM/src/GSMClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
namespace arduino {

class GSMClient : public MbedClient {
public:
GSMClient();

private:
NetworkInterface *getNetwork() {
return GSM.getNetwork();
}
Expand Down
24 changes: 24 additions & 0 deletions libraries/GSM/src/GSMSSLClient.cpp
Original file line number Diff line number Diff line change
@@ -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) {

}
42 changes: 42 additions & 0 deletions libraries/GSM/src/GSMSSLClient.h
Original file line number Diff line number Diff line change
@@ -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 */
Loading
Loading