Add Library
This commit is contained in:
parent
e365b9dbd9
commit
3c47103b39
318 changed files with 56465 additions and 0 deletions
140
libraries/ESP8266_MQTT_Mesh/src/Base64.cpp
Normal file
140
libraries/ESP8266_MQTT_Mesh/src/Base64.cpp
Normal file
|
@ -0,0 +1,140 @@
|
|||
/*Copyright (C) 2013 Adam Rudd
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "Base64.h"
|
||||
#include <pgmspace.h>
|
||||
const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
/* 'Private' declarations */
|
||||
inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
|
||||
inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
|
||||
inline unsigned char b64_lookup(char c);
|
||||
|
||||
int base64_encode(char *output, const char *input, int inputLen) {
|
||||
int i = 0, j = 0;
|
||||
int encLen = 0;
|
||||
unsigned char a3[3];
|
||||
unsigned char a4[4];
|
||||
|
||||
while (inputLen--) {
|
||||
a3[i++] = *(input++);
|
||||
if (i == 3) {
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 3; j++) {
|
||||
a3[j] = '\0';
|
||||
}
|
||||
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for (j = 0; j < i + 1; j++) {
|
||||
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
|
||||
}
|
||||
|
||||
while ((i++ < 3)) {
|
||||
output[encLen++] = '=';
|
||||
}
|
||||
}
|
||||
output[encLen] = '\0';
|
||||
return encLen;
|
||||
}
|
||||
|
||||
int base64_decode(char * output, const char * input, int inputLen) {
|
||||
int i = 0, j = 0;
|
||||
int decLen = 0;
|
||||
unsigned char a3[3];
|
||||
unsigned char a4[4];
|
||||
|
||||
|
||||
while (inputLen--) {
|
||||
if (*input == '=') {
|
||||
break;
|
||||
}
|
||||
|
||||
a4[i++] = *(input++);
|
||||
if (i == 4) {
|
||||
for (i = 0; i < 4; i++) {
|
||||
a4[i] = b64_lookup(a4[i]);
|
||||
}
|
||||
|
||||
a4_to_a3(a3, a4);
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
output[decLen++] = a3[i];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 4; j++) {
|
||||
a4[j] = '\0';
|
||||
}
|
||||
|
||||
for (j = 0; j < 4; j++) {
|
||||
a4[j] = b64_lookup(a4[j]);
|
||||
}
|
||||
|
||||
a4_to_a3(a3, a4);
|
||||
|
||||
for (j = 0; j < i - 1; j++) {
|
||||
output[decLen++] = a3[j];
|
||||
}
|
||||
}
|
||||
output[decLen] = '\0';
|
||||
return decLen;
|
||||
}
|
||||
|
||||
int base64_enc_len(int plainLen) {
|
||||
int n = plainLen;
|
||||
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
|
||||
}
|
||||
|
||||
int base64_dec_len(const char * input, int inputLen) {
|
||||
int i = 0;
|
||||
int numEq = 0;
|
||||
for (i = inputLen - 1; input[i] == '='; i--) {
|
||||
numEq++;
|
||||
}
|
||||
|
||||
return ((6 * inputLen) / 8) - numEq;
|
||||
}
|
||||
|
||||
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
|
||||
a4[0] = (a3[0] & 0xfc) >> 2;
|
||||
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
|
||||
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
|
||||
a4[3] = (a3[2] & 0x3f);
|
||||
}
|
||||
|
||||
inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
|
||||
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
|
||||
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
|
||||
}
|
||||
|
||||
inline unsigned char b64_lookup(char c) {
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 71;
|
||||
if (c >= '0' && c <= '9') return c + 4;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1;
|
||||
}
|
91
libraries/ESP8266_MQTT_Mesh/src/Base64.h
Normal file
91
libraries/ESP8266_MQTT_Mesh/src/Base64.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Adam Rudd.
|
||||
* See LICENSE for more information
|
||||
|
||||
|
||||
|
||||
|
||||
Copyright (C) 2013 Adam Rudd
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
#ifndef _BASE64_H
|
||||
#define _BASE64_H
|
||||
|
||||
/* b64_alphabet:
|
||||
* Description: Base64 alphabet table, a mapping between integers
|
||||
* and base64 digits
|
||||
* Notes: This is an extern here but is defined in Base64.c
|
||||
*/
|
||||
extern const char b64_alphabet[];
|
||||
|
||||
/* base64_encode:
|
||||
* Description:
|
||||
* Encode a string of characters as base64
|
||||
* Parameters:
|
||||
* output: the output buffer for the encoding, stores the encoded string
|
||||
* input: the input buffer for the encoding, stores the binary to be encoded
|
||||
* inputLen: the length of the input buffer, in bytes
|
||||
* Return value:
|
||||
* Returns the length of the encoded string
|
||||
* Requirements:
|
||||
* 1. output must not be null or empty
|
||||
* 2. input must not be null
|
||||
* 3. inputLen must be greater than or equal to 0
|
||||
*/
|
||||
int base64_encode(char *output, const char *input, int inputLen);
|
||||
|
||||
/* base64_decode:
|
||||
* Description:
|
||||
* Decode a base64 encoded string into bytes
|
||||
* Parameters:
|
||||
* output: the output buffer for the decoding,
|
||||
* stores the decoded binary
|
||||
* input: the input buffer for the decoding,
|
||||
* stores the base64 string to be decoded
|
||||
* inputLen: the length of the input buffer, in bytes
|
||||
* Return value:
|
||||
* Returns the length of the decoded string
|
||||
* Requirements:
|
||||
* 1. output must not be null or empty
|
||||
* 2. input must not be null
|
||||
* 3. inputLen must be greater than or equal to 0
|
||||
*/
|
||||
int base64_decode(char *output, const char *input, int inputLen);
|
||||
|
||||
/* base64_enc_len:
|
||||
* Description:
|
||||
* Returns the length of a base64 encoded string whose decoded
|
||||
* form is inputLen bytes long
|
||||
* Parameters:
|
||||
* inputLen: the length of the decoded string
|
||||
* Return value:
|
||||
* The length of a base64 encoded string whose decoded form
|
||||
* is inputLen bytes long
|
||||
* Requirements:
|
||||
* None
|
||||
*/
|
||||
int base64_enc_len(int inputLen);
|
||||
|
||||
/* base64_dec_len:
|
||||
* Description:
|
||||
* Returns the length of the decoded form of a
|
||||
* base64 encoded string
|
||||
* Parameters:
|
||||
* input: the base64 encoded string to be measured
|
||||
* inputLen: the length of the base64 encoded string
|
||||
* Return value:
|
||||
* Returns the length of the decoded form of a
|
||||
* base64 encoded string
|
||||
* Requirements:
|
||||
* 1. input must not be null
|
||||
* 2. input must be greater than or equal to zero
|
||||
*/
|
||||
int base64_dec_len(const char *input, int inputLen);
|
||||
|
||||
#endif // _BASE64_H
|
1131
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMesh.cpp
Normal file
1131
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMesh.cpp
Normal file
File diff suppressed because it is too large
Load diff
252
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMesh.h
Normal file
252
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMesh.h
Normal file
|
@ -0,0 +1,252 @@
|
|||
#ifndef _ESP8266MQTTMESH_H_
|
||||
#define _ESP8266MQTTMESH_H_
|
||||
|
||||
#if ! defined(MQTT_MAX_PACKET_SIZE)
|
||||
#define MQTT_MAX_PACKET_SIZE 1152
|
||||
#endif
|
||||
#if ! defined(ESP8266MESHMQTT_DISABLE_OTA) && ! defined(ESP32)
|
||||
//By default we support OTA
|
||||
#if ! defined(MQTT_MAX_PACKET_SIZE) || MQTT_MAX_PACKET_SIZE < (1024+128)
|
||||
#error "Must define MQTT_MAX_PACKET_SIZE >= 1152"
|
||||
#endif
|
||||
#define HAS_OTA 1
|
||||
#else
|
||||
#define HAS_OTA 0
|
||||
#endif
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESP32Ticker.h>
|
||||
#define USE_WIFI_ONEVENT
|
||||
#include "WiFiCompat.h"
|
||||
#else
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <Ticker.h>
|
||||
#endif
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <FS.h>
|
||||
#include <functional>
|
||||
|
||||
#ifdef ESP32
|
||||
#define _chipID ((unsigned long)ESP.getEfuseMac())
|
||||
#else
|
||||
#define _chipID ESP.getChipId()
|
||||
#endif
|
||||
|
||||
#define TOPIC_LEN 64
|
||||
|
||||
#define EMMDBG_EXTRA 0x10000000
|
||||
#define EMMDBG_MSG 0x00000001
|
||||
#define EMMDBG_MSG_EXTRA (EMMDBG_EXTRA | EMMDBG_MSG)
|
||||
#define EMMDBG_WIFI 0x00000002
|
||||
#define EMMDBG_WIFI_EXTRA (EMMDBG_EXTRA | EMMDBG_WIFI)
|
||||
#define EMMDBG_MQTT 0x00000004
|
||||
#define EMMDBG_MQTT_EXTRA (EMMDBG_EXTRA | EMMDBG_MQTT)
|
||||
#define EMMDBG_OTA 0x00000008
|
||||
#define EMMDBG_OTA_EXTRA (EMMDBG_EXTRA | EMMDBG_OTA)
|
||||
#define EMMDBG_TIMING 0x00000010
|
||||
#define EMMDBG_TIMING_EXTRA (EMMDBG_EXTRA | EMMDBG_TIMING)
|
||||
#define EMMDBG_FS 0x00000020
|
||||
#define EMMDBG_FS_EXTRA (EMMDBG_EXTRA | EMMDBG_OTA)
|
||||
#define EMMDBG_ALL 0x8FFFFFFF
|
||||
#define EMMDBG_ALL_EXTRA 0xFFFFFFFF
|
||||
#define EMMDBG_NONE 0x00000000
|
||||
|
||||
#ifndef ESP8266_NUM_CLIENTS
|
||||
#define ESP8266_NUM_CLIENTS 4
|
||||
#endif
|
||||
|
||||
enum MSG_TYPE {
|
||||
MSG_TYPE_NONE = 0xFE,
|
||||
MSG_TYPE_INVALID = 0xFF,
|
||||
MSG_TYPE_QOS_0 = 10,
|
||||
MSG_TYPE_QOS_1 = 11,
|
||||
MSG_TYPE_QOS_2 = 12,
|
||||
MSG_TYPE_RETAIN_QOS_0 = 13,
|
||||
MSG_TYPE_RETAIN_QOS_1 = 14,
|
||||
MSG_TYPE_RETAIN_QOS_2 = 15,
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
const uint8_t *cert;
|
||||
const uint8_t *key;
|
||||
const uint8_t *fingerprint;
|
||||
uint32_t cert_len;
|
||||
uint32_t key_len;
|
||||
} ssl_cert_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int len;
|
||||
byte md5[16];
|
||||
} ota_info_t;
|
||||
|
||||
typedef struct ap_t {
|
||||
struct ap_t *next;
|
||||
int32_t rssi;
|
||||
uint8_t bssid[6];
|
||||
int16_t ssid_idx;
|
||||
} ap_t;
|
||||
|
||||
typedef struct {
|
||||
const char *ssid;
|
||||
const char *password;
|
||||
const char *bssid;
|
||||
bool hidden;
|
||||
} wifi_conn;
|
||||
#define WIFI_CONN(ssid, password, bssid, hidden) \
|
||||
{ ssid, password, bssid, hidden }
|
||||
|
||||
class ESP8266MQTTMesh {
|
||||
public:
|
||||
class Builder;
|
||||
private:
|
||||
const unsigned int firmware_id;
|
||||
const char *firmware_ver;
|
||||
const wifi_conn *networks;
|
||||
|
||||
const char *mesh_ssid;
|
||||
char mesh_password[64];
|
||||
const char *mqtt_server;
|
||||
const char *mqtt_username;
|
||||
const char *mqtt_password;
|
||||
const int mqtt_port;
|
||||
const int mesh_port;
|
||||
uint32_t mesh_bssid_key;
|
||||
|
||||
const char *inTopic;
|
||||
const char *outTopic;
|
||||
#if HAS_OTA
|
||||
uint32_t freeSpaceStart;
|
||||
uint32_t freeSpaceEnd;
|
||||
uint32_t nextErase;
|
||||
uint32_t startTime;
|
||||
ota_info_t ota_info;
|
||||
#endif
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
bool mqtt_secure;
|
||||
ssl_cert_t mesh_secure;
|
||||
const uint8_t *mqtt_fingerprint;
|
||||
#endif
|
||||
AsyncServer espServer;
|
||||
AsyncClient *espClient[ESP8266_NUM_CLIENTS+1] = {0};
|
||||
uint8_t espMAC[ESP8266_NUM_CLIENTS+1][6];
|
||||
AsyncMqttClient mqttClient;
|
||||
|
||||
Ticker schedule;
|
||||
|
||||
int retry_connect;
|
||||
ap_t *ap = NULL;
|
||||
ap_t *ap_ptr;
|
||||
ap_t *ap_unused = NULL;
|
||||
char myID[10];
|
||||
char inbuffer[ESP8266_NUM_CLIENTS+1][MQTT_MAX_PACKET_SIZE];
|
||||
char *bufptr[ESP8266_NUM_CLIENTS+1];
|
||||
long lastMsg = 0;
|
||||
char msg[50];
|
||||
int value = 0;
|
||||
bool meshConnect = false;
|
||||
unsigned long lastReconnect = 0;
|
||||
unsigned long lastStatus = 0;
|
||||
bool connecting = 0;
|
||||
bool scanning = 0;
|
||||
bool AP_ready = false;
|
||||
std::function<void(const char *topic, const char *msg)> callback;
|
||||
|
||||
bool wifiConnected() { return (WiFi.status() == WL_CONNECTED); }
|
||||
void die() { while(1) {} }
|
||||
|
||||
uint32_t lfsr(uint32_t seed, uint8_t b);
|
||||
uint32_t encrypt_id(uint32_t id);
|
||||
void generate_mac(uint8_t *bssid, uint32_t id);
|
||||
bool verify_bssid(uint8_t *bssid);
|
||||
|
||||
int match_networks(const char *ssid, const char *bssid);
|
||||
void scan();
|
||||
void connect();
|
||||
static void connect(ESP8266MQTTMesh *e) { e->connect(); };
|
||||
String mac_str(uint8_t *bssid);
|
||||
const char *build_mesh_ssid(char buf[32], uint8_t *mac);
|
||||
void schedule_connect(float delay = 5.0);
|
||||
void connect_mqtt();
|
||||
void shutdown_AP();
|
||||
void setup_AP();
|
||||
void handle_client_data(int idx, char *data);
|
||||
void parse_message(const char *topic, const char *msg);
|
||||
void mqtt_callback(const char* topic, const byte* payload, unsigned int length);
|
||||
uint16_t mqtt_publish(const char *topic, const char *msg, uint8_t msgType);
|
||||
void publish(const char *topicDirection, const char *baseTopic, const char *subTopic, const char *msg, uint8_t msgType);
|
||||
bool send_message(int index, const char *topicOrMsg, const char *msg = NULL, uint8_t msgType = MSG_TYPE_NONE);
|
||||
void send_messages();
|
||||
void send_connected_msg();
|
||||
void broadcast_message(const char *topicOrMsg, const char *msg = NULL);
|
||||
void get_fw_string(char *msg, int len, const char *prefix);
|
||||
void handle_fw(const char *cmd);
|
||||
void handle_ota(const char *cmd, const char *msg);
|
||||
void parse_ota_info(const char *str);
|
||||
char * md5(const uint8_t *msg, int len);
|
||||
bool check_ota_md5();
|
||||
void assign_subdomain();
|
||||
static void assign_subdomain(ESP8266MQTTMesh *e) { e->assign_subdomain(); };
|
||||
void erase_sector();
|
||||
static void erase_sector(ESP8266MQTTMesh *e) { e->erase_sector(); };
|
||||
|
||||
void connectWiFiEvents();
|
||||
|
||||
#ifndef USE_WIFI_ONEVENT
|
||||
WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
WiFiEventHandler wifiAPConnectHandler;
|
||||
WiFiEventHandler wifiAPDisconnectHandler;
|
||||
#endif
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||
//void onDHCPTimeout();
|
||||
void onAPConnect(const WiFiEventSoftAPModeStationConnected& ip);
|
||||
void onAPDisconnect(const WiFiEventSoftAPModeStationDisconnected& ip);
|
||||
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
||||
void onMqttUnsubscribe(uint16_t packetId);
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||
void onMqttPublish(uint16_t packetId);
|
||||
|
||||
int onSslFileRequest(const char *filename, uint8_t **buf);
|
||||
void onClient(AsyncClient* c);
|
||||
void onConnect(AsyncClient* c);
|
||||
void onDisconnect(AsyncClient* c);
|
||||
void onError(AsyncClient* c, int8_t error);
|
||||
void onAck(AsyncClient* c, size_t len, uint32_t time);
|
||||
void onTimeout(AsyncClient* c, uint32_t time);
|
||||
void onData(AsyncClient* c, void* data, size_t len);
|
||||
|
||||
ESP8266MQTTMesh(const wifi_conn *networks,
|
||||
const char *mqtt_server, int mqtt_port,
|
||||
const char *mqtt_username, const char *mqtt_password,
|
||||
const char *firmware_ver, int firmware_id,
|
||||
const char *mesh_ssid, const char *mesh_password, int mesh_port,
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
bool mqtt_secure, const uint8_t *mqtt_fingerprint, ssl_cert_t mesh_secure,
|
||||
#endif
|
||||
const char *inTopic, const char *outTopic);
|
||||
public:
|
||||
void setCallback(std::function<void(const char *topic, const char *msg)> _callback);
|
||||
void begin();
|
||||
void publish(const char *subtopic, const char *msg, enum MSG_TYPE msgCmd = MSG_TYPE_NONE);
|
||||
void publish_node(const char *subtopic, const char *msg, enum MSG_TYPE msgCmd = MSG_TYPE_NONE);
|
||||
bool connected();
|
||||
static bool keyValue(const char *data, char separator, char *key, int keylen, const char **value);
|
||||
#ifdef USE_WIFI_ONEVENT
|
||||
void WiFiEventHandler(system_event_id_t event, system_event_info_t info);
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "ESP8266MQTTMeshBuilder.h"
|
||||
|
||||
#endif //_ESP8266MQTTMESH_H_
|
142
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMeshBuilder.h
Normal file
142
libraries/ESP8266_MQTT_Mesh/src/ESP8266MQTTMeshBuilder.h
Normal file
|
@ -0,0 +1,142 @@
|
|||
#ifndef _ESP8266MQTTMESHBUILDER_H_
|
||||
#define _ESP8266MQTTMESHBUILDER_H_
|
||||
|
||||
class ESP8266MQTTMesh::Builder {
|
||||
private:
|
||||
const wifi_conn *networks;
|
||||
|
||||
const char *mqtt_server;
|
||||
int mqtt_port;
|
||||
const char *mqtt_username;
|
||||
const char *mqtt_password;
|
||||
|
||||
const char *mesh_ssid;
|
||||
const char *mesh_password;
|
||||
int mesh_port;
|
||||
|
||||
const char *inTopic;
|
||||
const char *outTopic;
|
||||
|
||||
unsigned int firmware_id;
|
||||
const char *firmware_ver;
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
bool mqtt_secure;
|
||||
ssl_cert_t mesh_secure;
|
||||
const uint8_t *mqtt_fingerprint;
|
||||
void fix_mqtt_port() { if (! mqtt_port) mqtt_port = mqtt_secure ? 8883 : 1883; };
|
||||
#else
|
||||
void fix_mqtt_port() { if (! mqtt_port) mqtt_port = 1883; };
|
||||
#endif
|
||||
|
||||
public:
|
||||
Builder(const wifi_conn *networks,
|
||||
const char *mqtt_server,
|
||||
int mqtt_port = 0):
|
||||
networks(networks),
|
||||
mqtt_server(mqtt_server),
|
||||
mqtt_port(mqtt_port),
|
||||
mqtt_username(NULL),
|
||||
mqtt_password(NULL),
|
||||
firmware_id(0),
|
||||
firmware_ver(NULL),
|
||||
mesh_ssid("esp8266_mqtt_mesh"),
|
||||
mesh_password("ESP8266MQTTMesh"),
|
||||
mesh_port(1884),
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
mqtt_secure(false),
|
||||
mqtt_fingerprint(NULL),
|
||||
mesh_secure({NULL, NULL, NULL, 0, 0}),
|
||||
#endif
|
||||
inTopic("esp8266-in/"),
|
||||
outTopic("esp8266-out/")
|
||||
|
||||
{}
|
||||
Builder& setVersion(const char *firmware_ver, int firmware_id) {
|
||||
this->firmware_id = firmware_id;
|
||||
this->firmware_ver = firmware_ver;
|
||||
return *this;
|
||||
}
|
||||
Builder& setMqttAuth(const char *username, const char *password) {
|
||||
this->mqtt_username = username;
|
||||
this->mqtt_password = password;
|
||||
return *this;
|
||||
}
|
||||
Builder& setMeshSSID(const char *ssid) { this->mesh_ssid = ssid; return *this; }
|
||||
Builder& setMeshPassword(const char *password) { this->mesh_password = password; return *this; }
|
||||
Builder& setMeshPort(int port) { this->mesh_port = port; return *this; }
|
||||
Builder& setTopic(const char *inTopic, const char *outTopic) {
|
||||
this->inTopic = inTopic;
|
||||
this->outTopic = outTopic;
|
||||
return *this;
|
||||
}
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
Builder& setMqttSSL(bool enable, const uint8_t *fingerprint) {
|
||||
this->mqtt_secure = enable;
|
||||
this->mqtt_fingerprint = fingerprint;
|
||||
return *this;
|
||||
}
|
||||
Builder & setMeshSSL(const uint8_t *ssl_cert, uint32_t ssl_cert_len,
|
||||
const uint8_t *ssl_key, uint32_t ssl_key_len,
|
||||
const uint8_t *ssl_fingerprint) {
|
||||
this->mesh_secure.cert = ssl_cert;
|
||||
this->mesh_secure.key = ssl_key;
|
||||
this->mesh_secure.fingerprint = ssl_fingerprint;
|
||||
this->mesh_secure.cert_len = ssl_cert_len;
|
||||
this->mesh_secure.key_len = ssl_key_len;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
ESP8266MQTTMesh build() {
|
||||
fix_mqtt_port();
|
||||
return( ESP8266MQTTMesh(
|
||||
networks,
|
||||
|
||||
mqtt_server,
|
||||
mqtt_port,
|
||||
mqtt_username,
|
||||
mqtt_password,
|
||||
|
||||
firmware_ver,
|
||||
firmware_id,
|
||||
|
||||
mesh_ssid,
|
||||
mesh_password,
|
||||
mesh_port,
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
mqtt_secure,
|
||||
mqtt_fingerprint,
|
||||
mesh_secure,
|
||||
#endif
|
||||
|
||||
inTopic,
|
||||
outTopic));
|
||||
}
|
||||
ESP8266MQTTMesh *buildptr() {
|
||||
fix_mqtt_port();
|
||||
return( new ESP8266MQTTMesh(
|
||||
networks,
|
||||
|
||||
mqtt_server,
|
||||
mqtt_port,
|
||||
mqtt_username,
|
||||
mqtt_password,
|
||||
|
||||
firmware_ver,
|
||||
firmware_id,
|
||||
|
||||
mesh_ssid,
|
||||
mesh_password,
|
||||
mesh_port,
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
mqtt_secure,
|
||||
mqtt_fingerprint,
|
||||
mesh_secure,
|
||||
#endif
|
||||
|
||||
inTopic,
|
||||
outTopic));
|
||||
}
|
||||
};
|
||||
#endif //_ESP8266MQTTMESHBUILDER_H_
|
54
libraries/ESP8266_MQTT_Mesh/src/WiFiCompat.h
Normal file
54
libraries/ESP8266_MQTT_Mesh/src/WiFiCompat.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/* This header file defines compatible structs that are not present in ESP32 */
|
||||
#ifndef _WIFI_COMPAT_
|
||||
#define _WIFI_COMPAT_
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
typedef enum {
|
||||
STATION_IF = 0, /**< ESP32 station interface */
|
||||
SOFTAP_IF, /**< ESP32 soft-AP interface */
|
||||
MAX_IF
|
||||
} WIFI_INTERFACE;
|
||||
|
||||
static bool wifi_set_macaddr(WIFI_INTERFACE if_index, uint8_t *macaddr) {
|
||||
if (if_index == SOFTAP_IF) {
|
||||
int ok = esp_base_mac_addr_set(macaddr);
|
||||
return ok == ESP_OK;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#define WIFI_DISCONNECT_REASON_ASSOC_TOOMANY WIFI_REASON_ASSOC_TOOMANY
|
||||
struct WiFiEventStationModeGotIP
|
||||
{
|
||||
IPAddress ip;
|
||||
IPAddress mask;
|
||||
IPAddress gw;
|
||||
};
|
||||
|
||||
struct WiFiEventStationModeDisconnected
|
||||
{
|
||||
String ssid;
|
||||
uint8_t bssid[6];
|
||||
unsigned int reason;
|
||||
};
|
||||
|
||||
/*
|
||||
struct WiFiEventSoftAPModeStationConnected
|
||||
{
|
||||
uint8_t mac[6];
|
||||
uint8_t aid;
|
||||
};
|
||||
*/
|
||||
#define WiFiEventSoftAPModeStationConnected system_event_ap_staconnected_t
|
||||
/*
|
||||
struct WiFiEventSoftAPModeStationDisconnected
|
||||
{
|
||||
uint8_t mac[6];
|
||||
uint8_t aid;
|
||||
};
|
||||
*/
|
||||
#define WiFiEventSoftAPModeStationDisconnected system_event_ap_stadisconnected_t
|
||||
|
||||
#endif //_WIFI_COMPAT_
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue