Skip to content

Commit 57cabb9

Browse files
committed
SocketWrapper - copyable networking clients
1 parent 29b84df commit 57cabb9

14 files changed

+296
-56
lines changed

Diff for: libraries/Ethernet/src/EthernetClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#define ethernetclient_h
2222

2323
#include "Ethernet.h"
24-
#include "MbedClient.h"
24+
#include "AClient.h"
2525

2626
namespace arduino {
2727

28-
class EthernetClient : public MbedClient {
28+
class EthernetClient : public AClient {
2929
NetworkInterface *getNetwork() {
3030
return Ethernet.getNetwork();
3131
}

Diff for: libraries/Ethernet/src/EthernetSSLClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#define ETHERNETSSLCLIENT_H
2222

2323
#include "EthernetClient.h"
24-
#include "MbedSSLClient.h"
24+
#include "AClient.h"
2525

2626
extern const char CA_CERTIFICATES[];
2727

2828
namespace arduino {
2929

30-
class EthernetSSLClient : public arduino::MbedSSLClient {
30+
class EthernetSSLClient : public arduino::ASslClient {
3131
NetworkInterface *getNetwork() {
3232
return Ethernet.getNetwork();
3333
}

Diff for: libraries/GSM/src/GSMClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
#include "GSMClient.h"
2121

22-
arduino::GSMClient::GSMClient(): MbedClient(100) {
22+
arduino::GSMClient::GSMClient(): AClient(100) {
2323

2424
}

Diff for: libraries/GSM/src/GSMClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#define gsmclient_h
2222

2323
#include "GSM.h"
24-
#include "MbedClient.h"
24+
#include "AClient.h"
2525

2626
namespace arduino {
2727

28-
class GSMClient : public MbedClient {
28+
class GSMClient : public AClient {
2929
public:
3030
GSMClient();
3131

Diff for: libraries/GSM/src/GSMSSLClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
#include "GSMSSLClient.h"
2121

22-
arduino::GSMSSLClient::GSMSSLClient(): MbedSSLClient(100) {
22+
arduino::GSMSSLClient::GSMSSLClient(): ASslClient(100) {
2323

2424
}

Diff for: libraries/GSM/src/GSMSSLClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#define GSMSSLCLIENT_H
2222

2323
#include "GSM.h"
24-
#include "MbedSSLClient.h"
24+
#include "AClient.h"
2525

2626
extern const char CA_CERTIFICATES[];
2727

2828
namespace arduino {
2929

30-
class GSMSSLClient : public arduino::MbedSSLClient {
30+
class GSMSSLClient : public arduino::ASslClient {
3131
public:
3232
GSMSSLClient();
3333

Diff for: libraries/SE05X/src/WiFiSSLSE050Client.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@
1919

2020
#include "WiFiSSLSE050Client.h"
2121

22-
arduino::WiFiSSLSE050Client::WiFiSSLSE050Client() {
23-
onBeforeConnect(mbed::callback(this, &WiFiSSLSE050Client::setRootCAClientCertKey));
22+
arduino::MbedSSLSE050Client::MbedSSLSE050Client() {
23+
onBeforeConnect(mbed::callback(this, &MbedSSLSE050Client::setRootCAClientCertKey));
2424
};
2525

26-
void arduino::WiFiSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {
26+
void arduino::MbedSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {
2727

2828
_keySlot = KeySlot;
2929
_client_cert_len = certLen;
3030
_client_cert = cert;
3131
}
32+
33+
void WiFiSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {
34+
if (!client) {
35+
newMbedClient();
36+
}
37+
static_cast<MbedSSLSE050Client*>(client.get())->setEccSlot(KeySlot, cert, certLen);
38+
}
39+
40+
void WiFiSSLSE050Client::newMbedClient() {
41+
client.reset(new MbedSSLSE050Client());
42+
client->setNetwork(getNetwork());
43+
}

Diff for: libraries/SE05X/src/WiFiSSLSE050Client.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323

2424
#include "SE05X.h"
2525
#include "WiFiSSLClient.h"
26+
#include "MbedSSLClient.h"
2627

2728
extern const char CA_CERTIFICATES[];
2829

2930
namespace arduino {
3031

31-
class WiFiSSLSE050Client : public arduino::WiFiSSLClient {
32+
class MbedSSLSE050Client : public arduino::MbedSSLClient {
3233

3334
public:
34-
WiFiSSLSE050Client();
35-
virtual ~WiFiSSLSE050Client() {
36-
stop();
37-
}
35+
MbedSSLSE050Client();
36+
3837
void setEccSlot(int KeySlot, const byte cert[], int certLen);
3938

4039
private:
@@ -57,14 +56,27 @@ class WiFiSSLSE050Client : public arduino::WiFiSSLClient {
5756
return 0;
5857
}
5958

60-
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_client_cert_key((void*)_client_cert, (size_t)_client_cert_len, &_keyObject, SE05X.getDeviceCtx())) {
59+
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_client_cert_key((void*)_client_cert,
60+
(size_t)_client_cert_len,
61+
&_keyObject,
62+
SE05X.getDeviceCtx())) {
6163
return 0;
6264
}
6365

6466
return 1;
6567
}
6668
};
6769

70+
class WiFiSSLSE050Client : public arduino::WiFiSSLClient {
71+
72+
public:
73+
74+
void setEccSlot(int KeySlot, const byte cert[], int certLen);
75+
76+
protected:
77+
virtual void newMbedClient();
78+
};
79+
6880
}
6981

7082
#endif /* WIFISSLSE050CLIENT_H */

Diff for: libraries/SocketWrapper/src/AClient.cpp

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
2+
#include "AClient.h"
3+
#include "MbedSSLClient.h"
4+
5+
AClient::AClient(unsigned long timeout) {
6+
setSocketTimeout(timeout);
7+
}
8+
9+
void arduino::AClient::newMbedClient() {
10+
client.reset(new MbedClient());
11+
client->setNetwork(getNetwork());
12+
}
13+
14+
arduino::AClient::operator bool() {
15+
return client && *client;
16+
}
17+
18+
void arduino::AClient::setSocket(Socket *sock) {
19+
if (!client) {
20+
newMbedClient();
21+
}
22+
client->setSocket(sock);
23+
}
24+
25+
void arduino::AClient::setSocketTimeout(unsigned long timeout) {
26+
if (!client) {
27+
newMbedClient();
28+
}
29+
client->setSocketTimeout(timeout);
30+
}
31+
32+
int arduino::AClient::connect(IPAddress ip, uint16_t port) {
33+
if (!client) {
34+
newMbedClient();
35+
}
36+
return client->connect(ip, port);
37+
}
38+
39+
int arduino::AClient::connect(const char *host, uint16_t port) {
40+
if (!client) {
41+
newMbedClient();
42+
}
43+
return client->connect(host, port);
44+
}
45+
46+
int arduino::AClient::connectSSL(IPAddress ip, uint16_t port) {
47+
if (!client) {
48+
newMbedClient();
49+
}
50+
return client->connectSSL(ip, port);
51+
}
52+
53+
int arduino::AClient::connectSSL(const char *host, uint16_t port, bool disableSNI) {
54+
if (!client) {
55+
newMbedClient();
56+
}
57+
return client->connectSSL(host, port, disableSNI);
58+
}
59+
60+
void arduino::AClient::stop() {
61+
if (!client)
62+
return;
63+
client->stop();
64+
}
65+
66+
uint8_t arduino::AClient::connected() {
67+
if (!client)
68+
return false;
69+
return client->connected();
70+
}
71+
72+
uint8_t arduino::AClient::status() {
73+
if (!client)
74+
return false;
75+
return client->status();
76+
}
77+
78+
IPAddress arduino::AClient::remoteIP() {
79+
if (!client)
80+
return INADDR_NONE;
81+
return client->remoteIP();
82+
}
83+
84+
uint16_t arduino::AClient::remotePort() {
85+
if (!client)
86+
return 0;
87+
return client->remotePort();
88+
}
89+
90+
size_t arduino::AClient::write(uint8_t b) {
91+
if (!client)
92+
return 0;
93+
return client->write(b);
94+
}
95+
96+
size_t arduino::AClient::write(const uint8_t *buf, size_t size) {
97+
if (!client)
98+
return 0;
99+
return client->write(buf, size);
100+
}
101+
102+
void arduino::AClient::flush() {
103+
if (!client)
104+
return;
105+
client->flush();
106+
}
107+
108+
int arduino::AClient::available() {
109+
if (!client)
110+
return 0;
111+
return client->available();
112+
}
113+
114+
int arduino::AClient::read() {
115+
if (!client)
116+
return -1;
117+
return client->read();
118+
}
119+
120+
int arduino::AClient::read(uint8_t *buf, size_t size) {
121+
if (!client)
122+
return 0;
123+
return client->read(buf, size);
124+
}
125+
126+
int arduino::AClient::peek() {
127+
if (!client)
128+
return -1;
129+
return client->peek();
130+
}
131+
132+
void arduino::ASslClient::newMbedClient() {
133+
client.reset(new MbedSSLClient());
134+
client->setNetwork(getNetwork());
135+
}
136+
137+
void arduino::ASslClient::disableSNI(bool statusSNI) {
138+
if (!client) {
139+
newMbedClient();
140+
}
141+
static_cast<MbedSSLClient*>(client.get())->disableSNI(statusSNI);
142+
}
143+
144+
void arduino::ASslClient::appendCustomCACert(const char* ca_cert) {
145+
if (!client) {
146+
newMbedClient();
147+
}
148+
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert);
149+
}

Diff for: libraries/SocketWrapper/src/AClient.h

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
AClient.h - Copyable Client implementation for Mbed Core
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef MBEDACLIENT_H
20+
#define MBEDACLIENT_H
21+
22+
#include <Arduino.h>
23+
#include "MbedClient.h"
24+
25+
namespace arduino {
26+
27+
class AClient : public Client {
28+
public:
29+
30+
AClient() {}
31+
AClient(unsigned long timeout);
32+
33+
virtual int connect(IPAddress ip, uint16_t port);
34+
virtual int connect(const char *host, uint16_t port);
35+
int connectSSL(IPAddress ip, uint16_t port);
36+
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
37+
virtual void stop();
38+
39+
virtual explicit operator bool();
40+
virtual uint8_t connected();
41+
uint8_t status();
42+
43+
IPAddress remoteIP();
44+
uint16_t remotePort();
45+
46+
virtual size_t write(uint8_t);
47+
virtual size_t write(const uint8_t *buf, size_t size);
48+
virtual void flush();
49+
50+
virtual int available();
51+
virtual int read();
52+
virtual int read(uint8_t *buf, size_t size);
53+
virtual int peek();
54+
55+
using Print::write;
56+
57+
void setSocketTimeout(unsigned long timeout);
58+
59+
protected:
60+
friend class EthernetServer;
61+
friend class WiFiServer;
62+
63+
std::shared_ptr<MbedClient> client;
64+
virtual NetworkInterface* getNetwork() = 0;
65+
virtual void newMbedClient();
66+
void setSocket(Socket* sock);
67+
68+
};
69+
70+
class ASslClient : public AClient {
71+
public:
72+
73+
ASslClient() {}
74+
ASslClient(unsigned long timeout) : AClient(timeout) {}
75+
76+
void disableSNI(bool statusSNI);
77+
78+
void appendCustomCACert(const char* ca_cert);
79+
80+
protected:
81+
virtual void newMbedClient();
82+
};
83+
84+
}
85+
#endif

0 commit comments

Comments
 (0)