Skip to content

Commit 07ae0e8

Browse files
authored
Merge pull request #25 from dgarske/fixes_pre_release
Fix for Arduino and other fixes from recent PR's.
2 parents 97d18fc + 6436b72 commit 07ae0e8

18 files changed

Lines changed: 183 additions & 107 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ examples/firmware/fwpush
6161
*.opensdf
6262
firmware.bin
6363
*.trs
64-
IDE/ARDUINO/wolfmqtt
64+
IDE/ARDUINO/wolfMQTT
6565
examples/azure/azureiothub
6666
examples/aws/awsiot

IDE/ARDUINO/README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
### wolfMQTT with Arduino
22

33
##### Reformatting wolfMQTT as a compatible Arduino Library
4-
wolfmqtt-arduino.sh is a shell script that will re-organize the wolfMQTT
5-
library to be compatible with Arduino projects. The Arduino IDE requires a
4+
wolfmqtt-arduino.sh is a shell script that will re-organize the wolfMQTT
5+
library to be compatible with Arduino projects. The Arduino IDE requires a
66
library's source files to be in the library's root directory with a header file
7-
in the name of the library. This script moves all source files to the root
8-
wolfMQTT directory and creates a stub header file called wolfMQTT.h.
7+
in the name of the library. This script moves all source files to the
8+
`IDE/ARDUINO/wolfMQTT` directory and creates a stub header file called
9+
`wolfMQTT.h`.
910

10-
To configure wolfMQTT with Arduino, enter the following from within the
11+
To configure wolfMQTT with Arduino, enter the following from within the
1112
IDE/ARDUINO directory:
1213

1314
./wolfmqtt-arduino.sh
14-
15-
#####Including wolfMQTT in Arduino Libraries (for Arduino version 1.6.6)
15+
16+
##### Including wolfMQTT in Arduino Libraries (for Arduino version 1.8.2)
1617

1718
1. In the Arduino IDE:
18-
- In `Sketch -> Import Library -> Add Library` and choose the wolfMQTT/IDE/ARDUNIO/wolfmqtt folder.
19-
- In `Sketch -> Import Library` choose wolfMQTT.
19+
- In `Sketch -> Include Library -> Add .ZIP Library...` and choose the
20+
`IDE/ARDUNIO/wolfMQTT` folder.
21+
- In `Sketch -> Include Library` choose wolfMQTT.
2022

2123
Note: If using wolfSSL TLS then you'll need to do this for wolfSSL as well.
24+
See `<wolfssl-root>/IDE/ARDUINO/README.md` for instructions.
25+
26+
27+
An example wolfMQTT client INO sketch exists here:
28+
`wolfmqtt_client/wolfmqtt_client.ino` to demonstrate using the wolfMQTT library.

IDE/ARDUINO/include.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
EXTRA_DIST+= IDE/ARDUINO/README.md
66
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt-arduino.sh
7-
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt_client.ino
7+
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt_client/wolfmqtt_client.ino

IDE/ARDUINO/wolfmqtt-arduino.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
DIR=${PWD##*/}
88

99
if [ "$DIR" == "ARDUINO" ]; then
10-
mkdir wolfmqtt
11-
cp ../../src/*.c ./wolfmqtt
12-
cp ../../examples/*.c ./wolfmqtt
13-
cp ../../examples/mqttclient/*.c ./wolfmqtt
14-
cp ../../examples/mqttclient/*.h ./wolfmqtt
15-
cp ../../examples/*.h ./wolfmqtt
16-
echo "/* stub header file for Arduino compatibility */" >> ./wolfmqtt/wolfMQTT.h
10+
rm -rf wolfMQTT
11+
12+
mkdir wolfMQTT
13+
cp ../../src/*.c ./wolfMQTT
14+
15+
mkdir wolfMQTT/wolfmqtt
16+
cp ../../wolfmqtt/*.h ./wolfMQTT/wolfmqtt
17+
18+
echo "/* Generated wolfMQTT header file for Arduino */" >> ./wolfMQTT/wolfMQTT.h
19+
echo "#include <wolfmqtt/mqtt_client.h>" >> ./wolfMQTT/wolfMQTT.h
1720
else
1821
echo "ERROR: You must be in the IDE/ARDUINO directory to run this script"
1922
fi

IDE/ARDUINO/wolfmqtt_client.ino renamed to IDE/ARDUINO/wolfmqtt_client/wolfmqtt_client.ino

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
#include <wolfMQTT.h>
2-
#include <Ethernet.h>
1+
/* Uncomment this to enable TLS support */
2+
/* Make sure and include the wolfSSL library */
3+
//#define ENABLE_MQTT_TLS
4+
5+
#ifdef ENABLE_MQTT_TLS
6+
#include <config.h>
37
#include <wolfssl.h>
48
#include <wolfssl/ssl.h>
5-
#include <wolfmqtt/mqtt_client.h>
6-
#include <examples/mqttnet.h>
9+
#endif
10+
11+
#include <wolfMQTT.h>
12+
#include <Ethernet.h>
13+
714

815
/* Configuration */
16+
#define DEFAULT_MQTT_HOST "iot.eclipse.org" /* broker.hivemq.com */
917
#define DEFAULT_CMD_TIMEOUT_MS 30000
1018
#define DEFAULT_CON_TIMEOUT_MS 5000
1119
#define DEFAULT_MQTT_QOS MQTT_QOS_0
@@ -19,13 +27,15 @@
1927
#define TEST_TOPIC_COUNT 2
2028

2129
/* Local Variables */
30+
#ifdef ENABLE_MQTT_TLS
2231
static WOLFSSL_METHOD* mMethod = 0;
2332
static WOLFSSL_CTX* mCtx = 0;
2433
static WOLFSSL* mSsl = 0;
34+
static const char* mTlsFile = NULL;
35+
#endif
2536
static word16 mPort = 0;
2637
static const char* mHost = "iot.eclipse.org";
2738
static int mStopRead = 0;
28-
static const char* mTlsFile = NULL;
2939

3040
EthernetClient ethClient;
3141

@@ -67,6 +77,7 @@ static int EthernetDisconnect(void *context)
6777
return 0;
6878
}
6979

80+
#ifdef ENABLE_MQTT_TLS
7081
static int mqttclient_tls_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store)
7182
{
7283
char buffer[WOLFSSL_MAX_ERROR_SZ];
@@ -101,6 +112,7 @@ static int mqttclient_tls_cb(MqttClient* cli)
101112

102113
return rc;
103114
}
115+
#endif /* ENABLE_MQTT_TLS */
104116

105117
#define MAX_PACKET_ID ((1 << 16) - 1)
106118
static int mPacketIdLast;
@@ -168,7 +180,11 @@ void loop() {
168180
MqttClient client;
169181
EthernetClient ethClient;
170182
MqttNet net;
183+
#ifdef ENABLE_MQTT_TLS
184+
int use_tls = 1;
185+
#else
171186
int use_tls = 0;
187+
#endif
172188
MqttQoS qos = DEFAULT_MQTT_QOS;
173189
byte clean_session = 1;
174190
word16 keep_alive_sec = 60;
@@ -200,7 +216,13 @@ void loop() {
200216

201217
/* Connect to broker server socket */
202218
rc = MqttClient_NetConnect(&client, mHost, mPort,
203-
DEFAULT_CON_TIMEOUT_MS, use_tls, mqttclient_tls_cb);
219+
DEFAULT_CON_TIMEOUT_MS, use_tls,
220+
#ifdef ENABLE_MQTT_TLS
221+
mqttclient_tls_cb
222+
#else
223+
NULL
224+
#endif
225+
);
204226
Serial.print("MQTT Socket Connect: ");
205227
Serial.print(MqttClient_ReturnCodeToString(rc));
206228
Serial.print(" ");

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ AC_ARG_ENABLE([stdincap],
150150

151151
if test "x$ENABLED_STDINCAP" = "xno"
152152
then
153-
AM_CPPFLAGS="$AM_CPPFLAGS -DNO_STDIN_CAPTURE"
153+
AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFMQTT_NO_STDIN_CAP"
154154
fi
155155

156156
AM_CONDITIONAL([BUILD_STDINCAP], [test "x$ENABLED_STDINCAP" = "xyes"])

examples/aws/awsiot.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,17 +453,19 @@ int awsiot_test(MQTTCtx *mqttCtx)
453453
/* check for test mode or stop */
454454
if (mStopRead || mqttCtx->test_mode) {
455455
rc = MQTT_CODE_SUCCESS;
456+
PRINTF("MQTT Exiting...");
456457
break;
457458
}
458459

459460
/* check return code */
460461
if (rc == MQTT_CODE_CONTINUE) {
461462
return rc;
462463
}
463-
#ifdef ENABLE_STDIN_CAPTURE
464+
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
464465
else if (rc == MQTT_CODE_STDIN_WAKE) {
465466
/* Get data from STDIO */
466-
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE, stdin) != NULL) {
467+
XMEMSET(mqttCtx->rx_buf, 0, MAX_BUFFER_SIZE);
468+
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE - 1, stdin) != NULL) {
467469
rc = (int)XSTRLEN((char*)mqttCtx->rx_buf);
468470

469471
/* Publish Topic */
@@ -514,8 +516,6 @@ int awsiot_test(MQTTCtx *mqttCtx)
514516

515517
case WMQ_DISCONNECT:
516518
{
517-
PRINTF("MQTT Exiting...");
518-
519519
/* Disconnect */
520520
rc = MqttClient_Disconnect(&mqttCtx->client);
521521
if (rc == MQTT_CODE_CONTINUE) {
@@ -556,16 +556,18 @@ int awsiot_test(MQTTCtx *mqttCtx)
556556
disconn:
557557
mqttCtx->stat = WMQ_NET_DISCONNECT;
558558
mqttCtx->return_code = rc;
559-
return MQTT_CODE_CONTINUE;
559+
rc = MQTT_CODE_CONTINUE;
560560

561561
exit:
562562

563-
/* Free resources */
564-
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
565-
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
563+
if (rc != MQTT_CODE_CONTINUE) {
564+
/* Free resources */
565+
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
566+
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
566567

567-
/* Cleanup network */
568-
MqttClientNet_DeInit(&mqttCtx->net);
568+
/* Cleanup network */
569+
MqttClientNet_DeInit(&mqttCtx->net);
570+
}
569571

570572
return rc;
571573
}

examples/azure/azureiothub.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,19 @@ int azureiothub_test(MQTTCtx *mqttCtx)
436436
/* check for test mode or stop */
437437
if (mStopRead || mqttCtx->test_mode) {
438438
rc = MQTT_CODE_SUCCESS;
439+
PRINTF("MQTT Exiting...");
439440
break;
440441
}
441442

442443
/* check return code */
443444
if (rc == MQTT_CODE_CONTINUE) {
444445
return rc;
445446
}
446-
#ifdef ENABLE_STDIN_CAPTURE
447+
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
447448
else if (rc == MQTT_CODE_STDIN_WAKE) {
448449
/* Get data from STDIO */
449-
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE, stdin) != NULL) {
450+
XMEMSET(mqttCtx->rx_buf, 0, MAX_BUFFER_SIZE);
451+
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE - 1, stdin) != NULL) {
450452
rc = (int)XSTRLEN((char*)mqttCtx->rx_buf);
451453

452454
/* Publish Topic */
@@ -494,8 +496,6 @@ int azureiothub_test(MQTTCtx *mqttCtx)
494496

495497
case WMQ_DISCONNECT:
496498
{
497-
PRINTF("MQTT Exiting...");
498-
499499
/* Disconnect */
500500
rc = MqttClient_Disconnect(&mqttCtx->client);
501501
if (rc == MQTT_CODE_CONTINUE) {
@@ -536,16 +536,18 @@ int azureiothub_test(MQTTCtx *mqttCtx)
536536
disconn:
537537
mqttCtx->stat = WMQ_NET_DISCONNECT;
538538
mqttCtx->return_code = rc;
539-
return MQTT_CODE_CONTINUE;
539+
rc = MQTT_CODE_CONTINUE;
540540

541541
exit:
542542

543-
/* Free resources */
544-
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
545-
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
543+
if (rc != MQTT_CODE_CONTINUE) {
544+
/* Free resources */
545+
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
546+
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
546547

547-
/* Cleanup network */
548-
MqttClientNet_DeInit(&mqttCtx->net);
548+
/* Cleanup network */
549+
MqttClientNet_DeInit(&mqttCtx->net);
550+
}
549551

550552
return rc;
551553
}

examples/firmware/fwclient.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ int fwclient_test(MQTTCtx *mqttCtx)
358358
/* check for test mode */
359359
if (mStopRead) {
360360
rc = MQTT_CODE_SUCCESS;
361+
PRINTF("MQTT Exiting...");
361362
break;
362363
}
363364

@@ -398,8 +399,6 @@ int fwclient_test(MQTTCtx *mqttCtx)
398399

399400
case WMQ_DISCONNECT:
400401
{
401-
PRINTF("MQTT Exiting...");
402-
403402
/* Disconnect */
404403
rc = MqttClient_Disconnect(&mqttCtx->client);
405404
if (rc == MQTT_CODE_CONTINUE) {
@@ -441,16 +440,18 @@ int fwclient_test(MQTTCtx *mqttCtx)
441440
disconn:
442441
mqttCtx->stat = WMQ_NET_DISCONNECT;
443442
mqttCtx->return_code = rc;
444-
return MQTT_CODE_CONTINUE;
443+
rc = MQTT_CODE_CONTINUE;
445444

446445
exit:
447446

448-
/* Free resources */
449-
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
450-
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
447+
if (rc != MQTT_CODE_CONTINUE) {
448+
/* Free resources */
449+
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
450+
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
451451

452-
/* Cleanup network */
453-
MqttClientNet_DeInit(&mqttCtx->net);
452+
/* Cleanup network */
453+
MqttClientNet_DeInit(&mqttCtx->net);
454+
}
454455

455456
return rc;
456457
}

examples/firmware/fwpush.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ int fwpush_test(MQTTCtx *mqttCtx)
255255
{
256256
int rc;
257257

258+
/* check for stop */
259+
if (mStopRead) {
260+
rc = MQTT_CODE_SUCCESS;
261+
PRINTF("MQTT Exiting...");
262+
goto disconn;
263+
}
264+
258265
switch(mqttCtx->stat)
259266
{
260267
case WMQ_BEGIN:
@@ -408,8 +415,6 @@ int fwpush_test(MQTTCtx *mqttCtx)
408415

409416
case WMQ_DISCONNECT:
410417
{
411-
PRINTF("MQTT Exiting...");
412-
413418
/* Disconnect */
414419
rc = MqttClient_Disconnect(&mqttCtx->client);
415420
if (rc == MQTT_CODE_CONTINUE) {
@@ -452,17 +457,19 @@ int fwpush_test(MQTTCtx *mqttCtx)
452457
disconn:
453458
mqttCtx->stat = WMQ_NET_DISCONNECT;
454459
mqttCtx->return_code = rc;
455-
return MQTT_CODE_CONTINUE;
460+
rc = MQTT_CODE_CONTINUE;
456461

457462
exit:
458463

459-
/* Free resources */
460-
if (mqttCtx->publish.buffer) WOLFMQTT_FREE(mqttCtx->publish.buffer);
461-
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
462-
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
464+
if (rc != MQTT_CODE_CONTINUE) {
465+
/* Free resources */
466+
if (mqttCtx->publish.buffer) WOLFMQTT_FREE(mqttCtx->publish.buffer);
467+
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
468+
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
463469

464-
/* Cleanup network */
465-
MqttClientNet_DeInit(&mqttCtx->net);
470+
/* Cleanup network */
471+
MqttClientNet_DeInit(&mqttCtx->net);
472+
}
466473

467474
return rc;
468475
}

0 commit comments

Comments
 (0)