Skip to content

Commit 87730b4

Browse files
committed
update esp32 ethernet to use native Arduino WiFiClientSecure
1 parent c906770 commit 87730b4

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/ThingerESP32Eth.h

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
#include <ETH.h>
99
#include <ThingerClient.h>
10-
#include <functional>
1110

12-
#ifndef _DISABLE_TLS_
13-
#include <SSLClientESP32.h>
14-
#endif
11+
#ifdef _DISABLE_TLS_
12+
typedef WiFiClient ESP32Client;
13+
#else
14+
#include <WiFiClientSecure.h>
15+
typedef WiFiClientSecure ESP32Client;
16+
#endif
1517

1618
class ThingerESP32Eth : public ThingerClient
1719

@@ -25,15 +27,8 @@ class ThingerESP32Eth : public ThingerClient
2527
ThingerClient(client_, user, device, device_credential)
2628
#ifdef THINGER_FREE_RTOS
2729
,ThingerESP32FreeRTOS(static_cast<ThingerClient&>(*this))
28-
#endif,
29-
#ifndef _DISABLE_TLS_
30-
,client_(&base_client_)
3130
#endif
3231
{
33-
34-
#ifndef _DISABLE_TLS_
35-
client_.setCACert(CA_ROOT_CERTIFICATE);
36-
#endif
3732

3833
WiFi.onEvent([](WiFiEvent_t event){
3934
switch (event) {
@@ -79,10 +74,6 @@ class ThingerESP32Eth : public ThingerClient
7974

8075
protected:
8176

82-
virtual bool network_connected(){
83-
return initialized_ ? ETH.linkUp() : false;
84-
}
85-
8677
bool init_address(){
8778
if(ip_==nullptr) return true;
8879
bool result = true;
@@ -93,9 +84,30 @@ class ThingerESP32Eth : public ThingerClient
9384
result &= dns1.fromString(dns1_);
9485
result &= dns2.fromString(dns2_);
9586
return result && ETH.config(ip, gateway, subnet, dns1, dns2);
96-
}
87+
}
88+
89+
virtual bool network_connected() override{
90+
return initialized_ ? ETH.linkUp() : false;
91+
}
9792

98-
virtual bool connect_network(){
93+
#ifndef _DISABLE_TLS_
94+
bool connect_socket() override{
95+
96+
#ifdef THINGER_INSECURE_SSL
97+
client_.setInsecure();
98+
THINGER_DEBUG("SSL/TLS", "Warning: TLS/SSL certificate will not be checked!")
99+
#else
100+
client_.setCACert(get_root_ca());
101+
#endif
102+
return client_.connect(get_host(), THINGER_SSL_PORT);
103+
}
104+
105+
bool secure_connection() override{
106+
return true;
107+
}
108+
#endif
109+
110+
bool connect_network() override{
99111
if(!initialized_){
100112
initialized_ = ETH.begin();
101113
if(initialized_){
@@ -108,20 +120,14 @@ class ThingerESP32Eth : public ThingerClient
108120
return network_connected();
109121
}
110122

111-
#ifndef _DISABLE_TLS_
112-
WiFiClient base_client_;
113-
SSLClientESP32 client_;
114-
#else
115-
WiFiClient client_;
116-
#endif
123+
ESP32Client client_;
117124
bool initialized_ = false;
118125
const char* hostname_ = "esp32-thinger";
119126
const char* ip_ = nullptr;
120127
const char* gateway_ = nullptr;
121128
const char* subnet_ = nullptr;
122129
const char* dns1_ = nullptr;
123130
const char* dns2_ = nullptr;
124-
125131
};
126132

127133
#endif

0 commit comments

Comments
 (0)