Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit eabd917

Browse files
authored
v1.0.2
### New in v1.0.2 1. Fix crashing bug when Client timeout. 2. Make code more error-proof. 3. Drop support to ESP8266_AT_Webserver. 4. Enhance examples
1 parent 739f814 commit eabd917

14 files changed

+399
-129
lines changed

README.md

Lines changed: 183 additions & 70 deletions
Large diffs are not rendered by default.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MySQL_MariaDB_Generic
2-
version=1.0.1
2+
version=1.0.2
33
author=Dr. Charles Bell <[email protected]>, Khoi Hoang <[email protected]>
44
maintainer=Khoi Hoang <[email protected]>
55
sentence=Connects to MySQL or MariaDB using ESP8266/ESP32, nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, SAM DUE, Mega, etc. board with W5x00 /ENC28J60 / LAN8742A Ethernet, WiFiNINA or ESP8266/ESP32-AT-command modules/shields. W5x00 can use Ethernet, EthernetLarge, Ethernet2 or Ethernet3 library.

src/MySQL_Generic_Connection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
1414
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1515
Licensed under MIT license
16-
Version: 1.0.1
16+
Version: 1.0.2
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2121
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2222
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
23+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2324
**********************************************************************************************************************************/
2425

2526
/*********************************************************************************************************************************
@@ -58,7 +59,6 @@ class MySQL_Connection : public MySQL_Packet
5859

5960
bool connect(IPAddress server, int port, char *user, char *password, char *db = NULL);
6061

61-
// KH New
6262
Connection_Result connectNonBlocking(IPAddress server, int port, char *user, char *password, char *db = NULL);
6363

6464
int connected()

src/MySQL_Generic_Connection_Impl.h

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
1414
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1515
Licensed under MIT license
16-
Version: 1.0.1
16+
Version: 1.0.2
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2121
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2222
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
23+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2324
**********************************************************************************************************************************/
2425

2526
/*********************************************************************************************************************************
@@ -68,21 +69,21 @@
6869
bool MySQL_Connection::connect(IPAddress server, int port, char *user, char *password, char *db)
6970
{
7071
int connected = 0;
71-
int retries = MAX_CONNECT_ATTEMPTS;
72+
int retries = 0;
7273

7374
MYSQL_LOGERROR3("Connecting to Server:", server, ", Port = ", port);
7475

7576
if (db)
7677
MYSQL_LOGERROR1("Using Database:", db);
7778

7879
// Retry up to MAX_CONNECT_ATTEMPTS times.
79-
while (retries--)
80+
while (retries++ < MAX_CONNECT_ATTEMPTS)
8081
{
8182
connected = client->connect(server, port);
8283

8384
if (connected != SUCCESS)
8485
{
85-
MYSQL_LOGERROR2("Got : ", connected, ". Retrying...");
86+
MYSQL_LOGERROR1("Can't connect. Retry #", retries);
8687
delay(CONNECT_DELAY_MS);
8788
}
8889
else
@@ -96,7 +97,11 @@ bool MySQL_Connection::connect(IPAddress server, int port, char *user, char *pas
9697

9798
MYSQL_LOGERROR("Connect OK. Try reading packets");
9899

99-
read_packet();
100+
if ( !read_packet() )
101+
{
102+
MYSQL_LOGERROR("Can't connect. Error reading packets");
103+
return false;
104+
}
100105

101106
MYSQL_LOGERROR("Try parsing packets");
102107

@@ -105,15 +110,19 @@ bool MySQL_Connection::connect(IPAddress server, int port, char *user, char *pas
105110
MYSQL_LOGERROR("Try send_authentication packets");
106111

107112
send_authentication_packet(user, password, db);
108-
read_packet();
113+
114+
if ( !read_packet() )
115+
{
116+
MYSQL_LOGERROR("Can't connect. Error reading auth packets");
117+
return false;
118+
}
109119

110120
if (get_packet_type() != MYSQL_OK_PACKET)
111121
{
112122
parse_error_packet();
113123
return false;
114124
}
115125

116-
// KH Test
117126
MYSQL_LOGERROR1("Connected. Server Version =", server_version);
118127

119128
free(server_version); // don't need it anymore
@@ -125,7 +134,7 @@ bool MySQL_Connection::connect(IPAddress server, int port, char *user, char *pas
125134
Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int port, char *user, char *password, char *db)
126135
{
127136
int connected = 0;
128-
int retries = MAX_CONNECT_ATTEMPTS;
137+
int retries = 0;
129138

130139
long now = 0;
131140

@@ -134,7 +143,7 @@ Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int por
134143
if (db)
135144
MYSQL_LOGERROR1("Using Database:", db);
136145

137-
while (retries--)
146+
while (retries++ < MAX_CONNECT_ATTEMPTS)
138147
{
139148
if ( (now == 0) || ( millis() - now ) > CONNECT_DELAY_MS )
140149
{
@@ -148,7 +157,7 @@ Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int por
148157
}
149158
else
150159
{
151-
MYSQL_LOGERROR2("Got : ", connected, ". Retrying...");
160+
MYSQL_LOGERROR1("Can't connect. Retry #", retries);
152161
//return RESULT_PENDING;
153162
}
154163
}
@@ -161,7 +170,11 @@ Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int por
161170

162171
MYSQL_LOGERROR("Connect OK. Try reading packets");
163172

164-
read_packet();
173+
if ( !read_packet() )
174+
{
175+
MYSQL_LOGERROR("Can't connect. Error reading packets");
176+
return RESULT_FAIL;
177+
}
165178

166179
MYSQL_LOGERROR("Try parsing packets");
167180

@@ -170,15 +183,19 @@ Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int por
170183
MYSQL_LOGERROR("Try send_authentication packets");
171184

172185
send_authentication_packet(user, password, db);
173-
read_packet();
186+
187+
if ( !read_packet() )
188+
{
189+
MYSQL_LOGERROR("Can't connect. Error reading auth packets");
190+
return RESULT_FAIL;
191+
}
174192

175193
if (get_packet_type() != MYSQL_OK_PACKET)
176194
{
177195
parse_error_packet();
178196
return RESULT_FAIL;
179197
}
180198

181-
// KH Test
182199
MYSQL_LOGERROR1("Connected. Server Version =", server_version);
183200

184201
free(server_version); // don't need it anymore

src/MySQL_Generic_Debug.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
1414
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1515
Licensed under MIT license
16-
Version: 1.0.1
16+
Version: 1.0.2
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2121
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2222
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
23+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2324
**********************************************************************************************************************************/
2425

2526
/*********************************************************************************************************************************
@@ -101,5 +102,12 @@ const char NOT_CONNECTED[] /*PROGMEM*/ = "ERROR: Class requires connected serve
101102
#define MYSQL_LOGDEBUG2(x,y,z) if(_MYSQL_LOGLEVEL_>3) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.print(x); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(y); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.println(z); }
102103
#define MYSQL_LOGDEBUG3(x,y,z,w) if(_MYSQL_LOGLEVEL_>3) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.print(x); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(y); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(z); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.println(w); }
103104

105+
#define MYSQL_LOGLEVEL5(x) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.println(x); }
106+
#define MYSQL_LOGLEVEL5_0(x) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.print(x); }
107+
#define MYSQL_LOGLEVEL5_0LN(x) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.println(x); }
108+
#define MYSQL_LOGLEVEL5_1(x,y) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.print(x); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.println(y); }
109+
#define MYSQL_LOGLEVEL5_2(x,y,z) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.print(x); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(y); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.println(z); }
110+
#define MYSQL_LOGLEVEL5_3(x,y,z,w) if(_MYSQL_LOGLEVEL_>4) { MYSQL_DEBUG_OUTPUT.print("[SQL] "); MYSQL_DEBUG_OUTPUT.print(x); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(y); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.print(z); MYSQL_DEBUG_OUTPUT.print(" "); MYSQL_DEBUG_OUTPUT.println(w); }
111+
104112

105113
#endif // MYSQL_GENERIC_DEBUG_H

src/MySQL_Generic_Encrypt_Sha1.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
1515
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1616
Licensed under MIT license
17-
Version: 1.0.1
17+
Version: 1.0.2
1818
1919
Version Modified By Date Comments
2020
------- ----------- ---------- -----------
2121
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2222
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2323
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
24+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2425
**********************************************************************************************************************************/
2526

2627
#ifndef MYSQL_GENERIC_ENCRYPT_SHA1_H

src/MySQL_Generic_Encrypt_Sha1_Impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
1515
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1616
Licensed under MIT license
17-
Version: 1.0.1
17+
Version: 1.0.2
1818
1919
Version Modified By Date Comments
2020
------- ----------- ---------- -----------
2121
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2222
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2323
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
24+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2425
**********************************************************************************************************************************/
2526

2627
#ifndef MYSQL_GENERIC_ENCRYPT_SHA1_IMPL_H

src/MySQL_Generic_Ethernet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
1212
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1313
Licensed under MIT license
14-
Version: 1.0.1
14+
Version: 1.0.2
1515
1616
Version Modified By Date Comments
1717
------- ----------- ---------- -----------
1818
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
1919
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2020
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
21+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2122
**********************************************************************************************************************************/
2223

2324
#ifndef MYSQL_GENERIC_ETHERNET_H

src/MySQL_Generic_Packet.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
1414
Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1515
Licensed under MIT license
16-
Version: 1.0.1
16+
Version: 1.0.2
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
2020
1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
2121
(Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2222
1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
23+
1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2324
**********************************************************************************************************************************/
2425

2526
/*********************************************************************************************************************************
@@ -47,7 +48,11 @@
4748
#define MYSQL_EOF_PACKET 0xfe
4849
#define MYSQL_ERROR_PACKET 0xff
4950

50-
#define MYSQL_GENERIC_VERSION "1.0.1"
51+
#define MYSQL_GENERIC_VERSION "1.0.2"
52+
53+
// KH, for validating packet size
54+
#define MAX_TRANSMISSION_UNIT 1500
55+
//////
5156

5257
class MySQL_Packet
5358
{
@@ -68,7 +73,10 @@ class MySQL_Packet
6873
void send_authentication_packet(char *user, char *password, char *db = NULL);
6974
void parse_handshake_packet();
7075
bool scramble_password(char *password, byte *pwd_hash);
71-
void read_packet();
76+
77+
// KH, mod from v1.0.2. Return true if valid packet
78+
bool read_packet();
79+
7280
int get_packet_type();
7381
void parse_error_packet();
7482
int get_lcb_len(int offset);

0 commit comments

Comments
 (0)