Skip to content

Commit abff857

Browse files
committed
Merge remote-tracking branch 'riz/ssl_fix_pr' into debug_ide2
Former-commit-id: edb13f7
2 parents b702027 + 1a73980 commit abff857

File tree

6 files changed

+182
-50
lines changed

6 files changed

+182
-50
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
TLS WiFi Web client
3+
4+
Remeber to update the CA certificates using CertificateUploader sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include "WiFiS3.h"
10+
#include "WiFiSSLClient.h"
11+
#include "IPAddress.h"
12+
13+
#include "arduino_secrets.h"
14+
15+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
16+
char ssid[] = SECRET_SSID; // your network SSID (name)
17+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
18+
19+
int status = WL_IDLE_STATUS;
20+
// if you don't want to use DNS (and reduce your sketch size)
21+
// use the numeric IP instead of the name for the server:
22+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
23+
char server[] = "www.google.com"; // name address for Google (using DNS)
24+
25+
// Initialize the Ethernet client library
26+
// with the IP address and port of the server
27+
// that you want to connect to (port 80 is default for HTTP):
28+
WiFiSSLClient client;
29+
30+
/* -------------------------------------------------------------------------- */
31+
void setup() {
32+
/* -------------------------------------------------------------------------- */
33+
//Initialize serial and wait for port to open:
34+
Serial.begin(115200);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
// check for the WiFi module:
40+
if (WiFi.status() == WL_NO_MODULE) {
41+
Serial.println("Communication with WiFi module failed!");
42+
// don't continue
43+
while (true);
44+
}
45+
46+
String fv = WiFi.firmwareVersion();
47+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
48+
Serial.println("Please upgrade the firmware");
49+
}
50+
51+
// attempt to connect to WiFi network:
52+
while (status != WL_CONNECTED) {
53+
Serial.print("Attempting to connect to SSID: ");
54+
Serial.println(ssid);
55+
// Connect to WPA/WPA2 network.
56+
status = WiFi.begin(ssid, pass);
57+
58+
// wait 10 seconds for connection:
59+
delay(10000);
60+
}
61+
62+
printWifiStatus();
63+
64+
Serial.println("\nStarting connection to server...");
65+
// if you get a connection, report back via serial:
66+
67+
if (client.connect(server, 443)) {
68+
Serial.println("connected to server");
69+
// Make a HTTP request:
70+
client.println("GET / HTTP/1.1");
71+
client.println("Host: www.google.com");
72+
client.println("Connection: close");
73+
client.println();
74+
}
75+
}
76+
77+
/* just wrap the received data up to 80 columns in the serial print*/
78+
/* -------------------------------------------------------------------------- */
79+
void read_response() {
80+
/* -------------------------------------------------------------------------- */
81+
uint32_t received_data_num = 0;
82+
while (client.available()) {
83+
/* actual data reception */
84+
char c = client.read();
85+
/* print data to serial port */
86+
Serial.print(c);
87+
/* wrap data to 80 columns*/
88+
received_data_num++;
89+
if(received_data_num % 80 == 0) {
90+
Serial.println();
91+
}
92+
}
93+
}
94+
95+
/* -------------------------------------------------------------------------- */
96+
void loop() {
97+
/* -------------------------------------------------------------------------- */
98+
read_response();
99+
100+
// if the server's disconnected, stop the client:
101+
if (!client.connected()) {
102+
Serial.println();
103+
Serial.println("disconnecting from server.");
104+
client.stop();
105+
106+
// do nothing forevermore:
107+
while (true);
108+
}
109+
}
110+
111+
/* -------------------------------------------------------------------------- */
112+
void printWifiStatus() {
113+
/* -------------------------------------------------------------------------- */
114+
// print the SSID of the network you're attached to:
115+
Serial.print("SSID: ");
116+
Serial.println(WiFi.SSID());
117+
118+
// print your board's IP address:
119+
IPAddress ip = WiFi.localIP();
120+
Serial.print("IP Address: ");
121+
Serial.println(ip);
122+
123+
// print the received signal strength:
124+
long rssi = WiFi.RSSI();
125+
Serial.print("signal strength (RSSI):");
126+
Serial.print(rssi);
127+
Serial.println(" dBm");
128+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

libraries/WiFiS3/src/WiFiClient.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size){
8888
}
8989

9090
/* -------------------------------------------------------------------------- */
91-
int WiFiClient::available(){
91+
int WiFiClient::available() {
9292
/* -------------------------------------------------------------------------- */
9393
int rv = 0;
9494
if(_sock >= 0 && rx_buffer != nullptr) {
@@ -100,8 +100,10 @@ int WiFiClient::available(){
100100
modem.begin();
101101
if(modem.write(string(PROMPT(_AVAILABLE)),res, "%s%d\r\n" , CMD_WRITE(_AVAILABLE), _sock)) {
102102
rv = atoi(res.c_str());
103-
104-
}
103+
if (rv < 0) {
104+
return 0;
105+
}
106+
}
105107
}
106108
}
107109
return rv;
@@ -252,4 +254,4 @@ uint16_t WiFiClient::remotePort(){
252254
}
253255
}
254256
return rv;
255-
}
257+
}

libraries/WiFiS3/src/WiFiClient.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class WiFiClient : public Client {
7272
bool destroy_at_distructor;
7373

7474

75-
7675
};
7776

7877
#endif

libraries/WiFiS3/src/WiFiSSLClient.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
using namespace std;
44

55
/* -------------------------------------------------------------------------- */
6-
WiFiSSLClient::WiFiSSLClient() : _sock(-1){
6+
WiFiSSLClient::WiFiSSLClient() : _sock(-1) {
7+
/* -------------------------------------------------------------------------- */
8+
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
9+
}
10+
11+
/* -------------------------------------------------------------------------- */
12+
WiFiSSLClient::~WiFiSSLClient() {
713
/* -------------------------------------------------------------------------- */
814

915
}
@@ -24,6 +30,7 @@ void WiFiSSLClient::getSocket() {
2430
int WiFiSSLClient::connect(IPAddress ip, uint16_t port) {
2531
/* -------------------------------------------------------------------------- */
2632
getSocket();
33+
2734
string res = "";
2835
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTIP)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTIP), _sock, ip.toString(), port)) {
2936
return 1;
@@ -35,6 +42,9 @@ int WiFiSSLClient::connect(IPAddress ip, uint16_t port) {
3542
int WiFiSSLClient::connect(const char* host, uint16_t port) {
3643
/* -------------------------------------------------------------------------- */
3744
getSocket();
45+
if (!_custom_root) {
46+
setCACert();
47+
}
3848
string res = "";
3949
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTNAME), _sock, host, port)) {
4050
return 1;
@@ -47,27 +57,16 @@ void WiFiSSLClient::setCACert(const char* root_ca, size_t size) {
4757
/* -------------------------------------------------------------------------- */
4858
getSocket();
4959
string res = "";
50-
Serial.print("size ");
51-
Serial.println(size);
5260
if(size > 0) {
5361
modem.write_nowait(string(PROMPT(_SETCAROOT)),res, "%s%d,%d\r\n" , CMD_WRITE(_SETCAROOT), _sock, size);
5462
if(modem.passthrough((uint8_t *)root_ca, size)) {
55-
return;
63+
_custom_root = true;
5664
}
5765
} else {
5866
modem.write(string(PROMPT(_SETCAROOT)),res, "%s%d\r\n" , CMD_WRITE(_SETCAROOT), _sock);
5967
}
6068
}
6169

62-
/* -------------------------------------------------------------------------- */
63-
void WiFiSSLClient::setInsecure() {
64-
/* -------------------------------------------------------------------------- */
65-
getSocket();
66-
string res = "";
67-
modem.write(string(PROMPT(_SSLSETINSERCURE)),res, "%s%d\r\n" , CMD_WRITE(_SSLSETINSERCURE), _sock);
68-
69-
}
70-
7170
/* -------------------------------------------------------------------------- */
7271
size_t WiFiSSLClient::write(uint8_t b){
7372
/* -------------------------------------------------------------------------- */
@@ -94,15 +93,18 @@ size_t WiFiSSLClient::write(const uint8_t *buf, size_t size){
9493
int WiFiSSLClient::available(){
9594
/* -------------------------------------------------------------------------- */
9695
int rv = 0;
97-
if(_sock >= 0) {
98-
if(rx_buffer.available() > 0) {
99-
return rx_buffer.available();
96+
if(_sock >= 0 && rx_buffer != nullptr) {
97+
if(rx_buffer->available() > 0) {
98+
return rx_buffer->available();
10099
}
101100
else {
102101
string res = "";
103102
modem.begin();
104103
if(modem.write(string(PROMPT(_SSLAVAILABLE)),res, "%s%d\r\n" , CMD_WRITE(_SSLAVAILABLE), _sock)) {
105104
rv = atoi(res.c_str());
105+
if (rv < 0) {
106+
return 0;
107+
}
106108
}
107109
}
108110
}
@@ -115,15 +117,15 @@ int WiFiSSLClient::_read() {
115117
int rv = -1;
116118
if(_sock >= 0) {
117119
string res = "";
118-
uint32_t size = rx_buffer.freePositions() - 1;
120+
uint32_t size = rx_buffer->freePositions() - 1;
119121
modem.begin();
120122

121123
/* important - it works one shot */
122124
modem.avoid_trim_results();
123125
modem.read_using_size();
124126
if(modem.write(string(PROMPT(_SSLCLIENTRECEIVE)),res, "%s%d,%d\r\n" , CMD_WRITE(_SSLCLIENTRECEIVE), _sock, size)) {
125127
for(int i = 0, rv = 0; i < size && i < res.size(); i++) {
126-
rx_buffer.store((uint8_t)res[i]);
128+
rx_buffer->store((uint8_t)res[i]);
127129
rv++;
128130
}
129131
}
@@ -134,7 +136,7 @@ int WiFiSSLClient::_read() {
134136
/* -------------------------------------------------------------------------- */
135137
bool WiFiSSLClient::read_needed(size_t s) {
136138
/* -------------------------------------------------------------------------- */
137-
if((size_t)rx_buffer.available() < s) {
139+
if((size_t)rx_buffer->available() < s) {
138140
_read();
139141
}
140142
}
@@ -157,7 +159,7 @@ int WiFiSSLClient::read(uint8_t *buf, size_t size) {
157159
bool go_on = true;
158160
for(int i = 0; i < size && go_on; i++) {
159161
bool is_read = false;
160-
*(buf+i) = rx_buffer.read(&is_read);
162+
*(buf+i) = rx_buffer->read(&is_read);
161163
if(is_read) {
162164
rv++;
163165
}

libraries/WiFiS3/src/WiFiSSLClient.h

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,36 @@ class WiFiSSLClient : public WiFiClient {
3030

3131
public:
3232
WiFiSSLClient();
33-
33+
~WiFiSSLClient();
3434
virtual int connect(IPAddress ip, uint16_t port);
3535
virtual int connect(const char* host, uint16_t port);
3636
void setCACert(const char* root_ca = NULL, size_t size = 0);
37-
void setInsecure();
38-
virtual size_t write(uint8_t);
39-
virtual size_t write(const uint8_t *buf, size_t size);
40-
virtual int available();
41-
virtual int read();
42-
virtual int read(uint8_t *buf, size_t size);
43-
virtual int peek();
44-
virtual void flush();
45-
virtual void stop();
46-
virtual uint8_t connected();
47-
virtual operator bool() {
48-
return _sock != -1;
49-
}
37+
virtual size_t write(uint8_t);
38+
virtual size_t write(const uint8_t *buf, size_t size);
39+
virtual int available();
40+
virtual int read();
41+
virtual int read(uint8_t *buf, size_t size);
42+
virtual int peek();
43+
virtual void flush();
44+
virtual void stop();
45+
virtual uint8_t connected();
46+
virtual operator bool() {
47+
return _sock != -1;
48+
}
49+
50+
virtual IPAddress remoteIP();
51+
virtual uint16_t remotePort();
5052

51-
virtual IPAddress remoteIP();
52-
virtual uint16_t remotePort();
53+
friend class WiFiServer;
5354

54-
friend class WiFiServer;
55-
56-
using Print::write;
55+
using Print::write;
5756

58-
protected:
59-
int _sock;
60-
void getSocket();
61-
FifoBuffer<uint8_t,RX_BUFFER_DIM> rx_buffer;
62-
int _read();
63-
bool read_needed(size_t s);
57+
private:
58+
int _sock;
59+
bool _custom_root = false;
60+
void getSocket();
61+
int _read();
62+
bool read_needed(size_t s);
6463

6564
private:
6665
void upload_default_Cert();

0 commit comments

Comments
 (0)