Skip to content

Commit

Permalink
Merge pull request #25 from dgarske/fixes_pre_release
Browse files Browse the repository at this point in the history
Fix for Arduino and other fixes from recent PR's.
  • Loading branch information
kaleb-himes authored May 10, 2017
2 parents 97d18fc + 6436b72 commit 07ae0e8
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ examples/firmware/fwpush
*.opensdf
firmware.bin
*.trs
IDE/ARDUINO/wolfmqtt
IDE/ARDUINO/wolfMQTT
examples/azure/azureiothub
examples/aws/awsiot
25 changes: 16 additions & 9 deletions IDE/ARDUINO/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
### wolfMQTT with Arduino

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

To configure wolfMQTT with Arduino, enter the following from within the
To configure wolfMQTT with Arduino, enter the following from within the
IDE/ARDUINO directory:

./wolfmqtt-arduino.sh
#####Including wolfMQTT in Arduino Libraries (for Arduino version 1.6.6)

##### Including wolfMQTT in Arduino Libraries (for Arduino version 1.8.2)

1. In the Arduino IDE:
- In `Sketch -> Import Library -> Add Library` and choose the wolfMQTT/IDE/ARDUNIO/wolfmqtt folder.
- In `Sketch -> Import Library` choose wolfMQTT.
- In `Sketch -> Include Library -> Add .ZIP Library...` and choose the
`IDE/ARDUNIO/wolfMQTT` folder.
- In `Sketch -> Include Library` choose wolfMQTT.

Note: If using wolfSSL TLS then you'll need to do this for wolfSSL as well.
See `<wolfssl-root>/IDE/ARDUINO/README.md` for instructions.


An example wolfMQTT client INO sketch exists here:
`wolfmqtt_client/wolfmqtt_client.ino` to demonstrate using the wolfMQTT library.
2 changes: 1 addition & 1 deletion IDE/ARDUINO/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

EXTRA_DIST+= IDE/ARDUINO/README.md
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt-arduino.sh
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt_client.ino
EXTRA_DIST+= IDE/ARDUINO/wolfmqtt_client/wolfmqtt_client.ino
17 changes: 10 additions & 7 deletions IDE/ARDUINO/wolfmqtt-arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
DIR=${PWD##*/}

if [ "$DIR" == "ARDUINO" ]; then
mkdir wolfmqtt
cp ../../src/*.c ./wolfmqtt
cp ../../examples/*.c ./wolfmqtt
cp ../../examples/mqttclient/*.c ./wolfmqtt
cp ../../examples/mqttclient/*.h ./wolfmqtt
cp ../../examples/*.h ./wolfmqtt
echo "/* stub header file for Arduino compatibility */" >> ./wolfmqtt/wolfMQTT.h
rm -rf wolfMQTT

mkdir wolfMQTT
cp ../../src/*.c ./wolfMQTT

mkdir wolfMQTT/wolfmqtt
cp ../../wolfmqtt/*.h ./wolfMQTT/wolfmqtt

echo "/* Generated wolfMQTT header file for Arduino */" >> ./wolfMQTT/wolfMQTT.h
echo "#include <wolfmqtt/mqtt_client.h>" >> ./wolfMQTT/wolfMQTT.h
else
echo "ERROR: You must be in the IDE/ARDUINO directory to run this script"
fi
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include <wolfMQTT.h>
#include <Ethernet.h>
/* Uncomment this to enable TLS support */
/* Make sure and include the wolfSSL library */
//#define ENABLE_MQTT_TLS

#ifdef ENABLE_MQTT_TLS
#include <config.h>
#include <wolfssl.h>
#include <wolfssl/ssl.h>
#include <wolfmqtt/mqtt_client.h>
#include <examples/mqttnet.h>
#endif

#include <wolfMQTT.h>
#include <Ethernet.h>


/* Configuration */
#define DEFAULT_MQTT_HOST "iot.eclipse.org" /* broker.hivemq.com */
#define DEFAULT_CMD_TIMEOUT_MS 30000
#define DEFAULT_CON_TIMEOUT_MS 5000
#define DEFAULT_MQTT_QOS MQTT_QOS_0
Expand All @@ -19,13 +27,15 @@
#define TEST_TOPIC_COUNT 2

/* Local Variables */
#ifdef ENABLE_MQTT_TLS
static WOLFSSL_METHOD* mMethod = 0;
static WOLFSSL_CTX* mCtx = 0;
static WOLFSSL* mSsl = 0;
static const char* mTlsFile = NULL;
#endif
static word16 mPort = 0;
static const char* mHost = "iot.eclipse.org";
static int mStopRead = 0;
static const char* mTlsFile = NULL;

EthernetClient ethClient;

Expand Down Expand Up @@ -67,6 +77,7 @@ static int EthernetDisconnect(void *context)
return 0;
}

#ifdef ENABLE_MQTT_TLS
static int mqttclient_tls_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store)
{
char buffer[WOLFSSL_MAX_ERROR_SZ];
Expand Down Expand Up @@ -101,6 +112,7 @@ static int mqttclient_tls_cb(MqttClient* cli)

return rc;
}
#endif /* ENABLE_MQTT_TLS */

#define MAX_PACKET_ID ((1 << 16) - 1)
static int mPacketIdLast;
Expand Down Expand Up @@ -168,7 +180,11 @@ void loop() {
MqttClient client;
EthernetClient ethClient;
MqttNet net;
#ifdef ENABLE_MQTT_TLS
int use_tls = 1;
#else
int use_tls = 0;
#endif
MqttQoS qos = DEFAULT_MQTT_QOS;
byte clean_session = 1;
word16 keep_alive_sec = 60;
Expand Down Expand Up @@ -200,7 +216,13 @@ void loop() {

/* Connect to broker server socket */
rc = MqttClient_NetConnect(&client, mHost, mPort,
DEFAULT_CON_TIMEOUT_MS, use_tls, mqttclient_tls_cb);
DEFAULT_CON_TIMEOUT_MS, use_tls,
#ifdef ENABLE_MQTT_TLS
mqttclient_tls_cb
#else
NULL
#endif
);
Serial.print("MQTT Socket Connect: ");
Serial.print(MqttClient_ReturnCodeToString(rc));
Serial.print(" ");
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ AC_ARG_ENABLE([stdincap],

if test "x$ENABLED_STDINCAP" = "xno"
then
AM_CPPFLAGS="$AM_CPPFLAGS -DNO_STDIN_CAPTURE"
AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFMQTT_NO_STDIN_CAP"
fi

AM_CONDITIONAL([BUILD_STDINCAP], [test "x$ENABLED_STDINCAP" = "xyes"])
Expand Down
22 changes: 12 additions & 10 deletions examples/aws/awsiot.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,19 @@ int awsiot_test(MQTTCtx *mqttCtx)
/* check for test mode or stop */
if (mStopRead || mqttCtx->test_mode) {
rc = MQTT_CODE_SUCCESS;
PRINTF("MQTT Exiting...");
break;
}

/* check return code */
if (rc == MQTT_CODE_CONTINUE) {
return rc;
}
#ifdef ENABLE_STDIN_CAPTURE
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
else if (rc == MQTT_CODE_STDIN_WAKE) {
/* Get data from STDIO */
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE, stdin) != NULL) {
XMEMSET(mqttCtx->rx_buf, 0, MAX_BUFFER_SIZE);
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE - 1, stdin) != NULL) {
rc = (int)XSTRLEN((char*)mqttCtx->rx_buf);

/* Publish Topic */
Expand Down Expand Up @@ -514,8 +516,6 @@ int awsiot_test(MQTTCtx *mqttCtx)

case WMQ_DISCONNECT:
{
PRINTF("MQTT Exiting...");

/* Disconnect */
rc = MqttClient_Disconnect(&mqttCtx->client);
if (rc == MQTT_CODE_CONTINUE) {
Expand Down Expand Up @@ -556,16 +556,18 @@ int awsiot_test(MQTTCtx *mqttCtx)
disconn:
mqttCtx->stat = WMQ_NET_DISCONNECT;
mqttCtx->return_code = rc;
return MQTT_CODE_CONTINUE;
rc = MQTT_CODE_CONTINUE;

exit:

/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
if (rc != MQTT_CODE_CONTINUE) {
/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);

/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
}

return rc;
}
Expand Down
22 changes: 12 additions & 10 deletions examples/azure/azureiothub.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,19 @@ int azureiothub_test(MQTTCtx *mqttCtx)
/* check for test mode or stop */
if (mStopRead || mqttCtx->test_mode) {
rc = MQTT_CODE_SUCCESS;
PRINTF("MQTT Exiting...");
break;
}

/* check return code */
if (rc == MQTT_CODE_CONTINUE) {
return rc;
}
#ifdef ENABLE_STDIN_CAPTURE
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
else if (rc == MQTT_CODE_STDIN_WAKE) {
/* Get data from STDIO */
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE, stdin) != NULL) {
XMEMSET(mqttCtx->rx_buf, 0, MAX_BUFFER_SIZE);
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE - 1, stdin) != NULL) {
rc = (int)XSTRLEN((char*)mqttCtx->rx_buf);

/* Publish Topic */
Expand Down Expand Up @@ -494,8 +496,6 @@ int azureiothub_test(MQTTCtx *mqttCtx)

case WMQ_DISCONNECT:
{
PRINTF("MQTT Exiting...");

/* Disconnect */
rc = MqttClient_Disconnect(&mqttCtx->client);
if (rc == MQTT_CODE_CONTINUE) {
Expand Down Expand Up @@ -536,16 +536,18 @@ int azureiothub_test(MQTTCtx *mqttCtx)
disconn:
mqttCtx->stat = WMQ_NET_DISCONNECT;
mqttCtx->return_code = rc;
return MQTT_CODE_CONTINUE;
rc = MQTT_CODE_CONTINUE;

exit:

/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
if (rc != MQTT_CODE_CONTINUE) {
/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);

/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
}

return rc;
}
Expand Down
17 changes: 9 additions & 8 deletions examples/firmware/fwclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ int fwclient_test(MQTTCtx *mqttCtx)
/* check for test mode */
if (mStopRead) {
rc = MQTT_CODE_SUCCESS;
PRINTF("MQTT Exiting...");
break;
}

Expand Down Expand Up @@ -398,8 +399,6 @@ int fwclient_test(MQTTCtx *mqttCtx)

case WMQ_DISCONNECT:
{
PRINTF("MQTT Exiting...");

/* Disconnect */
rc = MqttClient_Disconnect(&mqttCtx->client);
if (rc == MQTT_CODE_CONTINUE) {
Expand Down Expand Up @@ -441,16 +440,18 @@ int fwclient_test(MQTTCtx *mqttCtx)
disconn:
mqttCtx->stat = WMQ_NET_DISCONNECT;
mqttCtx->return_code = rc;
return MQTT_CODE_CONTINUE;
rc = MQTT_CODE_CONTINUE;

exit:

/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
if (rc != MQTT_CODE_CONTINUE) {
/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);

/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
}

return rc;
}
Expand Down
25 changes: 16 additions & 9 deletions examples/firmware/fwpush.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ int fwpush_test(MQTTCtx *mqttCtx)
{
int rc;

/* check for stop */
if (mStopRead) {
rc = MQTT_CODE_SUCCESS;
PRINTF("MQTT Exiting...");
goto disconn;
}

switch(mqttCtx->stat)
{
case WMQ_BEGIN:
Expand Down Expand Up @@ -408,8 +415,6 @@ int fwpush_test(MQTTCtx *mqttCtx)

case WMQ_DISCONNECT:
{
PRINTF("MQTT Exiting...");

/* Disconnect */
rc = MqttClient_Disconnect(&mqttCtx->client);
if (rc == MQTT_CODE_CONTINUE) {
Expand Down Expand Up @@ -452,17 +457,19 @@ int fwpush_test(MQTTCtx *mqttCtx)
disconn:
mqttCtx->stat = WMQ_NET_DISCONNECT;
mqttCtx->return_code = rc;
return MQTT_CODE_CONTINUE;
rc = MQTT_CODE_CONTINUE;

exit:

/* Free resources */
if (mqttCtx->publish.buffer) WOLFMQTT_FREE(mqttCtx->publish.buffer);
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
if (rc != MQTT_CODE_CONTINUE) {
/* Free resources */
if (mqttCtx->publish.buffer) WOLFMQTT_FREE(mqttCtx->publish.buffer);
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);

/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
}

return rc;
}
Expand Down
Loading

0 comments on commit 07ae0e8

Please sign in to comment.