Skip to content

Commit e58b825

Browse files
authored
Fixing compiler warnings due to stronger compile-time checks (#69)
* Fixing issues uncovered by stronger compiler warnings * Adding const qualifier for parameter 'char * fmt' -> 'const char * fmt' thereby eliminating -Wwrite-strings warnings * Adding missing cases to switch statements * Removing unused variable 'index' * Reording initialisation order of ArduinoIoTCloud in order to eliminate '-Wreorder' warnings * Adding const qualifier for parameter 'char * fmt' -> 'const char * fmt' thereby eliminating -Wwrite-strings warnings * Prefixing 'connection' with an underscore -> '_connection' in order to be consistent with all other member variables * Preventing multiple definitions of SECRET_PASS mutually overwriting one another by prefixing the correct connection type * Changing type of timeout intervals to type unsigned long - otherwise there is a type inconsistency and probable bug when checking the connection time interval * Removing duplicate ERROR case * Adding missing cases and simplifying code * Changing type of timeout intervals to type unsigned long - otherwise there is a type inconsistency and probable bug when checking the connection time interval * Using default clause for missing switch statements (no constant defined for state DISCONNECTING) * Fix -Wreorder error * Simplifying WifiConnectionManager * Replacing '#warning' with '#pragma message' * Fixing comparison between unsigned and signed type * Ignoring error stemming from warning about deprecated functions * Rectifying signed/unsigned comparison * char * arrays should be compared via strcmp * Commenting out unused parameters in order to prevent -Wunused-parameter * Removing unused variable connection buffer * Adding missing switch cases * Correct initlisation of struct tm t * Correct printf specifier for unsigned long it is %lu * Adding missing initializer for tm_isdst * Performing cast in order to guarantuee correct comparison * Fixing formatting * Disabling '-Wunused-variable' to prevent false positives * Removing prefix 'WIFI_' and 'GSM_' because Arduino Create is looking for 'SECRET_SSID', 'SECRET_PASS', etc. when importing a example project into Arduino Create * Correcting comment * Removing prefix of parameter 'newState' wherever used, since prefixes usually indicate class members * Consting parameter 'newState' - defensive programming
1 parent 52bf7c7 commit e58b825

15 files changed

+175
-121
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/*
2-
Fill in your login credentials:
1+
#include <ConnectionManager.h>
32

4-
The following lines are used for WiFi enabled boards (MKR1000, MKR WiFi 1010)
5-
*/
6-
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
7-
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
8-
/*
9-
If you prefer using a MKR GSM 1400 comment the lines above and uncommet the following.
10-
PIN, APN, Login and Password are supplied by your Cellular Data provider.
11-
*/
12-
#define SECRET_PIN ""
13-
#define SECRET_APN ""
14-
#define SECRET_LOGIN ""
15-
#define SECRET_PASS ""
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* MKR GSM 1400 */
10+
#if defined(BOARD_HAS_GSM)
11+
#define SECRET_PIN ""
12+
#define SECRET_APN ""
13+
#define SECRET_LOGIN ""
14+
#define SECRET_PASS ""
15+
#endif
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
/* (MKR1000, MKR WiFi 1010) */
2-
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
3-
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
1+
#include <ConnectionManager.h>
42

5-
/* MKR GSM 1400 comment the lines above and uncommet the following. */
6-
#define SECRET_PIN ""
7-
#define SECRET_APN ""
8-
#define SECRET_LOGIN ""
9-
#define SECRET_PASS ""
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* MKR GSM 1400 */
10+
#if defined(BOARD_HAS_GSM)
11+
#define SECRET_PIN ""
12+
#define SECRET_APN ""
13+
#define SECRET_LOGIN ""
14+
#define SECRET_PASS ""
15+
#endif

examples/GSM_Cloud_Blink/GSM_Cloud_Blink.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
#ifndef SECRET_PIN
66
#define SECRET_PIN ""
7-
#warning "You need to define SECRET_PIN in tab/arduino_secrets.h"
7+
#pragma message "You need to define SECRET_PIN in tab/arduino_secrets.h"
88
#endif
99

1010
#ifndef SECRET_APN
1111
#define SECRET_APN ""
12-
#warning "You need to define SECRET_PIN in tab/arduino_secrets.h"
12+
#pragma message "You need to define SECRET_PIN in tab/arduino_secrets.h"
1313
#endif
1414

1515
#ifndef SECRET_USER_NAME
1616
#define SECRET_USER_NAME ""
17-
#warning "You need to define SECRET_USER_NAME in tab/arduino_secrets.h"
17+
#pragma message "You need to define SECRET_USER_NAME in tab/arduino_secrets.h"
1818
#endif
1919

2020
#ifndef SECRET_PASSWORD
2121
#define SECRET_PASSWORD ""
22-
#warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
22+
#pragma message "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
2323
#endif
2424

2525
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
@@ -76,7 +76,7 @@ void handleString() {
7676
void sendString(String stringToSend) {
7777
// send the characters one at a time
7878
char lastSentChar = 0;
79-
for (int i = 0; i < stringToSend.length(); i++) {
79+
for (unsigned int i = 0; i < stringToSend.length(); i++) {
8080
lastSentChar = stringToSend.charAt(i);
8181
CloudSerial.write(lastSentChar);
8282
}

examples/WiFi_Cloud_Blink/WiFi_Cloud_Blink.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
#ifndef SECRET_WIFI_NAME
66
#define SECRET_WIFI_NAME ""
7-
#warning "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h"
7+
#pragma message "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h"
88
#endif
99

1010
#ifndef SECRET_PASSWORD
1111
#define SECRET_PASSWORD ""
12-
#warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
12+
#pragma message "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
1313
#endif
1414

1515
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
@@ -66,7 +66,7 @@ void handleString() {
6666
void sendString(String stringToSend) {
6767
// send the characters one at a time
6868
char lastSentChar = 0;
69-
for (int i = 0; i < stringToSend.length(); i++) {
69+
for (unsigned int i = 0; i < stringToSend.length(); i++) {
7070
lastSentChar = stringToSend.charAt(i);
7171
CloudSerial.write(lastSentChar);
7272
}

src/ArduinoIoTCloud.cpp

+15-20
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ static unsigned long getTime() {
5252
}
5353

5454
ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
55+
_connection(NULL),
5556
_thing_id(""),
5657
_bearSslClient(NULL),
5758
_mqttClient(NULL),
58-
connection(NULL),
59+
_lastSyncRequestTickTime(0),
5960
_stdinTopic(""),
6061
_stdoutTopic(""),
6162
_shadowTopicOut(""),
6263
_shadowTopicIn(""),
6364
_dataTopicOut(""),
6465
_dataTopicIn(""),
6566
_otaTopic(""),
66-
_lastSyncRequestTickTime(0),
6767
_on_sync_event_callback(NULL),
6868
_on_connect_event_callback(NULL),
6969
_on_disconnect_event_callback(NULL) {
@@ -83,7 +83,7 @@ ArduinoIoTCloudClass::~ArduinoIoTCloudClass() {
8383
}
8484

8585
int ArduinoIoTCloudClass::begin(ConnectionManager *c, String brokerAddress, uint16_t brokerPort) {
86-
connection = c;
86+
_connection = c;
8787
Client &connectionClient = c->getClient();
8888
_brokerAddress = brokerAddress;
8989
_brokerPort = brokerPort;
@@ -131,8 +131,8 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
131131
if (_bearSslClient) {
132132
delete _bearSslClient;
133133
}
134-
if (connection != NULL) {
135-
_bearSslClient = new BearSSLClient(connection->getClient());
134+
if (_connection != NULL) {
135+
_bearSslClient = new BearSSLClient(_connection->getClient());
136136
} else {
137137
_bearSslClient = new BearSSLClient(*_net);
138138
}
@@ -141,8 +141,8 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
141141
_mqttClient = new MqttClient(*_bearSslClient);
142142

143143
// Bind ArduinoBearSSL callback using static "non-method" function
144-
if (connection != NULL) {
145-
getTimeConnection = connection;
144+
if (_connection != NULL) {
145+
getTimeConnection = _connection;
146146
ArduinoBearSSL.onGetTime(getTime);
147147
}
148148

@@ -211,12 +211,7 @@ bool ArduinoIoTCloudClass::disconnect() {
211211
return true;
212212
}
213213

214-
void ArduinoIoTCloudClass::update(CallbackFunc onSyncCompleteCallback) {
215-
// If user call update() without parameters use the default ones
216-
update(MAX_RETRIES, RECONNECTION_TIMEOUT, onSyncCompleteCallback);
217-
}
218-
219-
void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const reconnectionTimeoutMs, CallbackFunc onSyncCompleteCallback) {
214+
void ArduinoIoTCloudClass::update(int const /* reconnectionMaxRetries */, int const /* reconnectionTimeoutMs */, CallbackFunc onSyncCompleteCallback) {
220215
// Check if a primitive property wrapper is locally changed
221216
Thing.updateTimestampOnLocallyChangedProperties();
222217

@@ -328,7 +323,6 @@ void ArduinoIoTCloudClass::handleMessage(int length) {
328323
String topic = _mqttClient->messageTopic();
329324

330325
byte bytes[length];
331-
int index = 0;
332326

333327
for (int i = 0; i < length; i++) {
334328
bytes[i] = _mqttClient->read();
@@ -356,10 +350,10 @@ void ArduinoIoTCloudClass::requestLastValue() {
356350
}
357351

358352
void ArduinoIoTCloudClass::connectionCheck() {
359-
if (connection != NULL) {
360-
connection->check();
353+
if (_connection != NULL) {
354+
_connection->check();
361355

362-
if (connection->getStatus() != NetworkConnectionState::CONNECTED) {
356+
if (_connection->getStatus() != NetworkConnectionState::CONNECTED) {
363357
if (iotStatus == ArduinoIoTConnectionStatus::CONNECTED) {
364358
setIoTConnectionState(ArduinoIoTConnectionStatus::DISCONNECTED);
365359
}
@@ -416,15 +410,16 @@ void ArduinoIoTCloudClass::connectionCheck() {
416410
}
417411
}
418412

419-
void ArduinoIoTCloudClass::setIoTConnectionState(ArduinoIoTConnectionStatus _newState) {
420-
switch (_newState) {
413+
void ArduinoIoTCloudClass::setIoTConnectionState(ArduinoIoTConnectionStatus newState) {
414+
iotStatus = newState;
415+
switch (iotStatus) {
416+
case ArduinoIoTConnectionStatus::IDLE: break;
421417
case ArduinoIoTConnectionStatus::ERROR: debugMessage(DebugLevel::Error, "Arduino, we have a problem."); break;
422418
case ArduinoIoTConnectionStatus::CONNECTING: debugMessage(DebugLevel::Error, "Connecting to Arduino IoT Cloud..."); break;
423419
case ArduinoIoTConnectionStatus::RECONNECTING: debugMessage(DebugLevel::Error, "Reconnecting to Arduino IoT Cloud..."); break;
424420
case ArduinoIoTConnectionStatus::CONNECTED: debugMessage(DebugLevel::Error, "Connected to Arduino IoT Cloud"); break;
425421
case ArduinoIoTConnectionStatus::DISCONNECTED: debugMessage(DebugLevel::Error, "Disconnected from Arduino IoT Cloud"); break;
426422
}
427-
iotStatus = _newState;
428423
}
429424

430425
void ArduinoIoTCloudClass::printDebugInfo() {

src/ArduinoIoTCloud.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ class ArduinoIoTCloudClass {
9191
inline void update() {
9292
update(NULL);
9393
}
94-
void update(CallbackFunc onSyncCompleteCallback) __attribute__((deprecated)); /* Attention: Function is deprecated - use 'addCallback(ArduinoIoTCloudConnectionEvent::SYNC, &onSync)' for adding a onSyncCallback instead */
95-
94+
inline void update(CallbackFunc onSyncCompleteCallback) __attribute__((deprecated)) { /* Attention: Function is deprecated - use 'addCallback(ArduinoIoTCloudConnectionEvent::SYNC, &onSync)' for adding a onSyncCallback instead */
95+
update(MAX_RETRIES, RECONNECTION_TIMEOUT, onSyncCompleteCallback);
96+
}
9697
// defined for users who want to specify max reconnections reties and timeout between them
9798
inline void update(int const reconnectionMaxRetries, int const reconnectionTimeoutMs) {
9899
update(reconnectionMaxRetries, reconnectionTimeoutMs, NULL);
@@ -193,10 +194,10 @@ class ArduinoIoTCloudClass {
193194
ArduinoIoTConnectionStatus getIoTStatus() {
194195
return iotStatus;
195196
}
196-
void setIoTConnectionState(ArduinoIoTConnectionStatus _newState);
197+
void setIoTConnectionState(ArduinoIoTConnectionStatus newState);
197198
private:
198199
ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE;
199-
ConnectionManager *connection;
200+
ConnectionManager * _connection;
200201
static void onMessage(int length);
201202
void handleMessage(int length);
202203
ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED;

src/EthernetConnectionManager.cpp

+18-10
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,21 @@ void EthConnectionManager::check() {
128128
debugMessage(DebugLevel::Info, "Connected");
129129
}
130130
break;
131+
case NetworkConnectionState::DISCONNECTING: {
132+
/* Do nothing */
133+
}
134+
break;
131135
case NetworkConnectionState::DISCONNECTED: {
132136
debugMessage(DebugLevel::Error, "Connection lost.");
133137
debugMessage(DebugLevel::Info, "Attempting reconnection");
134138
changeConnectionState(NetworkConnectionState::CONNECTING);
135139
//wifiClient.stop();
136140
}
137141
break;
142+
case NetworkConnectionState::ERROR: {
143+
/* Do nothing */
144+
}
145+
break;
138146
}
139147
lastConnectionTickTime = now;
140148
}
@@ -144,17 +152,17 @@ void EthConnectionManager::check() {
144152
PRIVATE MEMBER FUNCTIONS
145153
******************************************************************************/
146154

147-
void EthConnectionManager::changeConnectionState(NetworkConnectionState _newState) {
148-
netConnectionState = _newState;
149-
int newInterval = CHECK_INTERVAL_IDLE;
150-
switch (_newState) {
151-
case NetworkConnectionState::INIT: newInterval = CHECK_INTERVAL_INIT; break;
152-
case NetworkConnectionState::CONNECTING: newInterval = CHECK_INTERVAL_CONNECTING; break;
153-
case NetworkConnectionState::GETTIME: newInterval = CHECK_INTERVAL_GETTIME; break;
154-
case NetworkConnectionState::CONNECTED: newInterval = CHECK_INTERVAL_CONNECTED; break;
155-
case NetworkConnectionState::DISCONNECTED: newInterval = CHECK_INTERVAL_DISCONNECTED; break;
155+
void EthConnectionManager::changeConnectionState(NetworkConnectionState const newState) {
156+
netConnectionState = newState;
157+
switch (netConnectionState) {
158+
case NetworkConnectionState::INIT: connectionTickTimeInterval = CHECK_INTERVAL_INIT; break;
159+
case NetworkConnectionState::CONNECTING: connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; break;
160+
case NetworkConnectionState::GETTIME: connectionTickTimeInterval = CHECK_INTERVAL_GETTIME; break;
161+
case NetworkConnectionState::CONNECTED: connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; break;
162+
case NetworkConnectionState::DISCONNECTED: connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; break;
163+
case NetworkConnectionState::ERROR: connectionTickTimeInterval = CHECK_INTERVAL_ERROR; break;
164+
default: connectionTickTimeInterval = CHECK_INTERVAL_IDLE; break;
156165
}
157-
connectionTickTimeInterval = newInterval;
158166
lastConnectionTickTime = millis();
159167
}
160168

src/EthernetConnectionManager.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,23 @@ class EthConnectionManager : public ConnectionManager {
4949

5050
private:
5151

52-
void changeConnectionState(NetworkConnectionState _newState);
52+
void changeConnectionState(NetworkConnectionState const newState);
5353

54-
const int CHECK_INTERVAL_IDLE = 100;
55-
const int CHECK_INTERVAL_INIT = 100;
56-
const int CHECK_INTERVAL_CONNECTING = 500;
57-
const int CHECK_INTERVAL_GETTIME = 100;
58-
const int CHECK_INTERVAL_CONNECTED = 10000;
59-
const int CHECK_INTERVAL_RETRYING = 5000;
60-
const int CHECK_INTERVAL_DISCONNECTED = 1000;
61-
const int CHECK_INTERVAL_ERROR = 500;
54+
const unsigned long CHECK_INTERVAL_IDLE = 100;
55+
const unsigned long CHECK_INTERVAL_INIT = 100;
56+
const unsigned long CHECK_INTERVAL_CONNECTING = 500;
57+
const unsigned long CHECK_INTERVAL_GETTIME = 100;
58+
const unsigned long CHECK_INTERVAL_CONNECTED = 10000;
59+
const unsigned long CHECK_INTERVAL_RETRYING = 5000;
60+
const unsigned long CHECK_INTERVAL_DISCONNECTED = 1000;
61+
const unsigned long CHECK_INTERVAL_ERROR = 500;
6262

63-
unsigned long lastConnectionTickTime, lastNetworkStep;
6463
uint8_t* mac;
6564
int ss_pin;
65+
unsigned long lastConnectionTickTime, lastNetworkStep;
6666
EthernetClient ethClient;
6767
EthernetUDP udp;
68-
int connectionTickTimeInterval;
68+
unsigned long connectionTickTimeInterval;
6969
};
7070

7171
#endif /* #ifdef BOARD_HAS_ETHERNET */

0 commit comments

Comments
 (0)