Update arduino-mqtt library

Update arduino-mqtt library to 2.4.0
This commit is contained in:
Theo Arends 2018-10-11 18:19:52 +02:00
parent fcc6d5437b
commit ace8491d59
31 changed files with 141 additions and 111 deletions

View File

@ -1,48 +0,0 @@
#include <Arduino.h>
#include "system.h"
void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) {
// cast timer reference
auto t = (lwmqtt_arduino_timer_t *)ref;
// set future end time
t->end = (uint32_t)(millis() + timeout);
}
int32_t lwmqtt_arduino_timer_get(void *ref) {
// cast timer reference
auto t = (lwmqtt_arduino_timer_t *)ref;
// get difference to end time
return (int32_t)t->end - (int32_t)millis();
}
lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) {
// cast network reference
auto n = (lwmqtt_arduino_network_t *)ref;
// set timeout
n->client->setTimeout(timeout);
// read bytes
*read = n->client->readBytes(buffer, len);
if (*read <= 0) {
return LWMQTT_NETWORK_FAILED_READ;
}
return LWMQTT_SUCCESS;
}
lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) {
// cast network reference
auto n = (lwmqtt_arduino_network_t *)ref;
// write bytes
*sent = n->client->write(buffer, len);
if (*sent <= 0) {
return LWMQTT_NETWORK_FAILED_WRITE;
};
return LWMQTT_SUCCESS;
}

View File

@ -1,26 +0,0 @@
#ifndef LWMQTT_ARDUINO_H
#define LWMQTT_ARDUINO_H
#include <Arduino.h>
#include <Client.h>
extern "C" {
#include "lwmqtt/lwmqtt.h"
};
typedef struct {
uint32_t end;
} lwmqtt_arduino_timer_t;
void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout);
int32_t lwmqtt_arduino_timer_get(void *ref);
typedef struct {
Client *client;
} lwmqtt_arduino_network_t;
lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout);
lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout);
#endif // LWMQTT_ARDUINO_H

View File

@ -1,7 +1,7 @@
language: generic
env:
global:
- IDE_VERSION=1.8.5
- IDE_VERSION=1.8.7
matrix:
- EXAMPLE="AdafruitHuzzahESP8266" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80"
- EXAMPLE="AdafruitHuzzahESP8266Secure" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80"

View File

@ -10,8 +10,8 @@ include_directories(
/Users/256dpi/Development/Arduino/libraries/MKRGSM/src
/Applications/Arduino.app/Contents/Java/libraries/Bridge/src
/Users/256dpi/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src
/Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFi/src
/Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFiClientSecure/src
/Users/256dpi/Library/Arduino15/packages/esp32/libraries/WiFi/src
/Users/256dpi/Library/Arduino15/packages/esp32/libraries/WiFiClientSecure/src
src/)
include_directories(src/)
@ -33,8 +33,6 @@ set(SOURCE_FILES
examples/ESP32DevelopmentBoardSecure/ESP32DevelopmentBoardSecure.ino
src/lwmqtt
src/MQTT.h
src/MQTTClient.h
src/system.cpp
src/system.h)
src/MQTTClient.h)
add_executable(arduino-mqtt ${SOURCE_FILES})

View File

@ -1,7 +1,7 @@
all: fmt
fmt:
clang-format -i src/*.cpp src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}"
clang-format -i src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}"
update:
rm -rf ./lwmqtt

View File

@ -151,11 +151,12 @@ void setOptions(int keepAlive, bool cleanSession, int timeout);
Connect to broker using the supplied client id and an optional username and password:
```c++
bool connect(const char clientId[]);
bool connect(const char clientId[], const char username[]);
bool connect(const char clientId[], const char username[], const char password[]);
bool connect(const char clientId[], bool skip = false);
bool connect(const char clientId[], const char username[], bool skip = false);
bool connect(const char clientId[], const char username[], const char password[], bool skip = false);
```
- If the `skip` option is set to true, the client will skip the network level connection and jump to the MQTT level connection. This option can be used in order to establish and verify TLS connections manually before giving control to the MQTT client.
- This functions returns a boolean that indicates if the connection has been established successfully.
Publishes a message to the broker with an optional payload:

View File

@ -1,5 +1,5 @@
name=MQTT
version=2.3.3
version=2.4.0
author=Joel Gaehwiler <joel.gaehwiler@gmail.com>
maintainer=Joel Gaehwiler <joel.gaehwiler@gmail.com>
sentence=MQTT library for Arduino

View File

@ -5,7 +5,66 @@
#include <Client.h>
#include <Stream.h>
#include "system.h"
extern "C" {
#include "lwmqtt/lwmqtt.h"
};
typedef struct {
uint32_t end;
} lwmqtt_arduino_timer_t;
void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout);
int32_t lwmqtt_arduino_timer_get(void *ref);
typedef struct {
Client *client;
} lwmqtt_arduino_network_t;
void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) {
// cast timer reference
auto t = (lwmqtt_arduino_timer_t *)ref;
// set future end time
t->end = (uint32_t)(millis() + timeout);
}
int32_t lwmqtt_arduino_timer_get(void *ref) {
// cast timer reference
auto t = (lwmqtt_arduino_timer_t *)ref;
// get difference to end time
return (int32_t)t->end - (int32_t)millis();
}
lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) {
// cast network reference
auto n = (lwmqtt_arduino_network_t *)ref;
// set timeout
n->client->setTimeout(timeout);
// read bytes
*read = n->client->readBytes(buffer, len);
if (*read <= 0) {
return LWMQTT_NETWORK_FAILED_READ;
}
return LWMQTT_SUCCESS;
}
lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) {
// cast network reference
auto n = (lwmqtt_arduino_network_t *)ref;
// write bytes
*sent = n->client->write(buffer, len);
if (*sent <= 0) {
return LWMQTT_NETWORK_FAILED_WRITE;
};
return LWMQTT_SUCCESS;
}
class MQTTClient;
@ -70,14 +129,13 @@ class MQTTClient {
Client *netClient = nullptr;
const char *hostname = nullptr;
int port = 0;
lwmqtt_will_t will = lwmqtt_default_will;
bool hasWill = false;
lwmqtt_will_t *will = nullptr;
MQTTClientCallback callback;
lwmqtt_arduino_network_t network = {nullptr};
lwmqtt_arduino_timer_t timer1 = {0};
lwmqtt_arduino_timer_t timer2 = {0};
lwmqtt_client_t client;
lwmqtt_client_t client = {0};
bool _connected = false;
lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0;
@ -85,13 +143,25 @@ class MQTTClient {
public:
explicit MQTTClient(int bufSize = 128) {
memset(&client, 0, sizeof(client));
// reset client
memset(&this->client, 0, sizeof(lwmqtt_client_t));
// allocate buffers
this->bufSize = (size_t)bufSize;
this->readBuf = (uint8_t *)malloc((size_t)bufSize + 1);
this->writeBuf = (uint8_t *)malloc((size_t)bufSize);
}
~MQTTClient() {
// free will
this->clearWill();
// free hostname
if (this->hostname != nullptr) {
free((void *)this->hostname);
}
// free buffers
free(this->readBuf);
free(this->writeBuf);
}
@ -136,7 +206,7 @@ class MQTTClient {
void setHost(const char hostname[], int port) {
// free hostname if set
if(this->hostname != nullptr) {
if (this->hostname != nullptr) {
free((void *)this->hostname);
}
@ -150,37 +220,78 @@ class MQTTClient {
void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); }
void setWill(const char topic[], const char payload[], bool retained, int qos) {
this->hasWill = true;
this->will.topic = lwmqtt_string(topic);
this->will.payload = lwmqtt_string(payload);
this->will.retained = retained;
this->will.qos = (lwmqtt_qos_t)qos;
// return if topic is missing
if (topic == nullptr || strlen(topic) == 0) {
return;
}
// clear existing will
this->clearWill();
// allocate will
this->will = (lwmqtt_will_t *)malloc(sizeof(lwmqtt_will_t));
memset(this->will, 0, sizeof(lwmqtt_will_t));
// set topic
this->will->topic = lwmqtt_string(strdup(topic));
// set payload if available
if (payload != nullptr && strlen(payload) > 0) {
this->will->payload = lwmqtt_string(strdup(payload));
}
// set flags
this->will->retained = retained;
this->will->qos = (lwmqtt_qos_t)qos;
}
void clearWill() { this->hasWill = false; }
void clearWill() {
// return if not set
if (this->will == nullptr) {
return;
}
// free payload if set
if (this->will->payload.len > 0) {
free(this->will->payload.data);
}
// free topic if set
if (this->will->topic.len > 0) {
free(this->will->topic.data);
}
// free will
free(this->will);
this->will = nullptr;
}
void setOptions(int keepAlive, bool cleanSession, int timeout) {
// set new options
this->keepAlive = (uint16_t)keepAlive;
this->cleanSession = cleanSession;
this->timeout = (uint32_t)timeout;
}
bool connect(const char clientId[]) { return this->connect(clientId, nullptr, nullptr); }
bool connect(const char clientId[], bool skip = false) { return this->connect(clientId, nullptr, nullptr); }
bool connect(const char clientId[], const char username[]) { return this->connect(clientId, username, nullptr); }
bool connect(const char clientId[], const char username[], bool skip = false) { return this->connect(clientId, username, nullptr); }
bool connect(const char clientId[], const char username[], const char password[]) {
bool connect(const char clientId[], const char username[], const char password[], bool skip = false) {
// close left open connection if still connected
if (this->connected()) {
if (!skip && this->connected()) {
this->close();
}
// save client
this->network.client = this->netClient;
// connect to host
if (this->netClient->connect(this->hostname, (uint16_t)this->port) < 0) {
return false;
// connect to hostg
if(!skip) {
int ret = this->netClient->connect(this->hostname, (uint16_t)this->port);
if (ret <= 0) {
return false;
}
}
// prepare options
@ -198,14 +309,8 @@ class MQTTClient {
}
}
// prepare will reference
lwmqtt_will_t *will = nullptr;
if (this->hasWill) {
will = &this->will;
}
// connect to broker
this->_lastError = lwmqtt_connect(&this->client, options, will, &this->_returnCode, this->timeout);
this->_lastError = lwmqtt_connect(&this->client, options, this->will, &this->_returnCode, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
// close connection
this->close();