update espMqttClient

This commit is contained in:
technyon
2024-07-25 13:26:01 +02:00
parent f1f1612cd4
commit d64031d710
22 changed files with 589 additions and 812 deletions

View File

@@ -1,34 +0,0 @@
---
name: Bug report
about: Create a report to help us improve espMqttClient
title: "[BUG]"
labels: ''
assignees: ''
---
#### Do not use to discuss topics!
**Describe the bug**
A clear and concise description of what the bug is.
Which platform, esp8266 or esp32?
Do you use TLS or not?
Do you use an IDE (Arduino, Platformio...)?
Which version of the Arduino framework?
Please include any debug output and/or decoded stack trace if applicable.
**Expected behaviour**
A clear and concise description of what you expected to happen.
**To Reproduce**
Steps to reproduce the behaviour:
**Example code**
```cpp
// Put code here to reproduce the bug, if possible
```
**Additional context**
Add any other context about the problem here.

View File

@@ -1,2 +0,0 @@
listener 1883
allow_anonymous true

View File

@@ -1,9 +0,0 @@
name: Arduino Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1

View File

@@ -1,62 +0,0 @@
name: Build with Arduino IDE
on:
- push
- pull_request
jobs:
build-for-esp8266:
runs-on: ubuntu-latest
strategy:
matrix:
fqbn:
- esp8266:esp8266:generic
steps:
- uses: actions/checkout@v3
- uses: arduino/compile-sketches@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.fqbn }}
enable-deltas-report: true
platforms: |
- name: esp8266:esp8266
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
sketch-paths: |
- examples/largepayload-esp8266
- examples/simple-esp8266
- examples/simpleAsync-esp8266
- examples/tls-esp8266
libraries: |
- name: espMqttClient
source-path: ./
- name: ESPAsyncTCP
source-url: https://github.com/me-no-dev/ESPAsyncTCP.git
build-for-esp32:
runs-on: ubuntu-latest
strategy:
matrix:
fqbn:
- esp32:esp32:esp32
steps:
- uses: actions/checkout@v3
- uses: arduino/compile-sketches@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.fqbn }}
enable-deltas-report: true
platforms: |
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
sketch-paths: |
- examples/simple-esp32
- examples/simpleAsync-esp32
- examples/tls-esp32
libraries: |
- name: espMqttClient
source-path: ./
- name: AsyncTCP
source-url: https://github.com/me-no-dev/AsyncTCP.git

View File

@@ -1,90 +0,0 @@
name: Build with Platformio
on: [push, pull_request]
jobs:
build-for-esp8266:
runs-on: ubuntu-latest
strategy:
matrix:
example: [
examples/largepayload-esp8266/largepayload-esp8266.ino,
examples/ota-esp8266/ota-esp8266.ino,
examples/simple-esp8266/simple-esp8266.ino,
examples/simpleAsync-esp8266/simpleAsync-esp8266.ino,
examples/tls-esp8266/tls-esp8266.ino
]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Download external libraries
run: pio pkg install --global --library me-no-dev/ESPAsyncTCP
- name: Build PlatformIO examples
run: pio ci --lib="." --board=d1_mini
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
build-for-esp32:
runs-on: ubuntu-latest
strategy:
matrix:
example: [
examples/notask-esp32/notask-esp32.ino,
examples/simple-esp32/simple-esp32.ino,
examples/simpleAsync-esp32/simpleAsync-esp32.ino,
examples/tls-esp32/tls-esp32.ino
]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Download external libraries
run: pio pkg install --global --library me-no-dev/AsyncTCP
- name: Build PlatformIO examples
run: pio ci --lib="." --board=lolin32
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
build-for-linux:
runs-on: ubuntu-latest
strategy:
matrix:
example: [
examples/simple-linux/main.cpp
]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Build PlatformIO examples
run: pio ci --lib="." --project-conf="./examples/simple-linux/platformio.ini"
env:
PLATFORMIO_CI_SRC: ${{ matrix.example }}

View File

@@ -1,22 +0,0 @@
name: Cppcheck
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install platformio
- name: Cppcheck
run: |
pio check --fail-on-defect=medium --fail-on-defect=high --flags "--inline-suppr --enable=warning --enable=style --enable=performance --suppress=unusedFunction --suppress=preprocessorErrorDirective" --skip-packages

View File

@@ -1,22 +0,0 @@
name: cpplint
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install cpplint
- name: Linting
run: |
cpplint --repository=. --recursive --filter=-whitespace/line_length,-build/include ./src

View File

@@ -1,29 +0,0 @@
name: Test with Platformio
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
sudo apt update && sudo apt install -y valgrind
python -m pip install --upgrade pip
pip install platformio
- name: Start Mosquitto
uses: namoshek/mosquitto-github-action@v1
with:
version: '2.0'
ports: '1883:1883'
config: ${{ github.workspace }}/.github/etc/mosquitto.conf
- name: Test
run: |
pio test -e native -v

View File

@@ -1,5 +0,0 @@
.pio
.vscode
cov
*cov.info
.idea

View File

@@ -11,13 +11,13 @@ the LICENSE file.
#pragma once #pragma once
#include "MqttClient.h"
#if EMC_MULTIPLE_CALLBACKS #if EMC_MULTIPLE_CALLBACKS
#include <list> #include <list>
#include <utility> #include <utility>
#endif #endif
#include "MqttClient.h"
template <typename T> template <typename T>
class MqttClientSetup : public MqttClient { class MqttClientSetup : public MqttClient {
public: public:

View File

@@ -27,12 +27,12 @@ bool ClientPosix::connect(IPAddress ip, uint16_t port) {
_sockfd = ::socket(AF_INET, SOCK_STREAM, 0); _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_sockfd < 0) { if (_sockfd < 0) {
emc_log_e("Error %d opening socket", errno); emc_log_e("Error %d: \"%s\" opening socket", errno, strerror(errno));
} }
int flag = 1; int flag = 1;
if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) { if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) {
emc_log_e("Error %d disabling nagle", errno); emc_log_e("Error %d: \"%s\" disabling nagle", errno, strerror(errno));
} }
memset(&_host, 0, sizeof(_host)); memset(&_host, 0, sizeof(_host));
@@ -47,15 +47,17 @@ bool ClientPosix::connect(IPAddress ip, uint16_t port) {
return false; return false;
} }
emc_log_i("Connected"); emc_log_i("Socket connected");
return true; return true;
} }
bool ClientPosix::connect(const char* host, uint16_t port) { bool ClientPosix::connect(const char* hostname, uint16_t port) {
// tbi IPAddress ipAddress = _hostToIP(hostname);
(void) host; if (ipAddress == IPAddress(0)) {
(void) port; emc_log_e("No such host '%s'", hostname);
return false; return false;
}
return connect(ipAddress, port);
} }
size_t ClientPosix::write(const uint8_t* buf, size_t size) { size_t ClientPosix::write(const uint8_t* buf, size_t size) {
@@ -87,6 +89,42 @@ bool ClientPosix::disconnected() {
return _sockfd < 0; return _sockfd < 0;
} }
IPAddress ClientPosix::_hostToIP(const char* hostname) {
IPAddress returnIP(0);
struct addrinfo hints, *servinfo, *p;
struct sockaddr_in *h;
int rv;
// Set up request addrinfo struct
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
emc_log_i("Looking for '%s'", hostname);
// ask for host data
if ((rv = getaddrinfo(hostname, NULL, &hints, &servinfo)) != 0) {
emc_log_e("getaddrinfo: %s", gai_strerror(rv));
return returnIP;
}
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
h = (struct sockaddr_in *)p->ai_addr;
returnIP = ::htonl(h->sin_addr.s_addr);
if (returnIP != IPAddress(0)) break;
}
// Release allocated memory
freeaddrinfo(servinfo);
if (returnIP != IPAddress(0)) {
emc_log_i("Host '%s' = %u", hostname, (uint32_t)returnIP);
} else {
emc_log_e("No IP for '%s' found", hostname);
}
return returnIP;
}
} // namespace espMqttClientInternals } // namespace espMqttClientInternals
#endif #endif

View File

@@ -19,6 +19,7 @@ the LICENSE file.
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netdb.h> #include <netdb.h>
#include <arpa/inet.h>
#include "Transport.h" // includes IPAddress #include "Transport.h" // includes IPAddress
#include "../Logging.h" #include "../Logging.h"
@@ -34,7 +35,7 @@ class ClientPosix : public Transport {
ClientPosix(); ClientPosix();
~ClientPosix(); ~ClientPosix();
bool connect(IPAddress ip, uint16_t port) override; bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override; bool connect(const char* hostname, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override; size_t write(const uint8_t* buf, size_t size) override;
int read(uint8_t* buf, size_t size) override; int read(uint8_t* buf, size_t size) override;
void stop() override; void stop() override;
@@ -44,6 +45,8 @@ class ClientPosix : public Transport {
protected: protected:
int _sockfd; int _sockfd;
sockaddr_in _host; sockaddr_in _host;
IPAddress _hostToIP(const char* hostname);
}; };
} // namespace espMqttClientInternals } // namespace espMqttClientInternals

View File

@@ -29,4 +29,12 @@ IPAddress::operator uint32_t() {
return _address; return _address;
} }
bool IPAddress::operator==(IPAddress other) {
return _address == other._address;
}
bool IPAddress::operator!=(IPAddress other) {
return _address != other._address;
}
#endif #endif

View File

@@ -18,8 +18,10 @@ class IPAddress {
public: public:
IPAddress(); IPAddress();
IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3); IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3);
explicit IPAddress(uint32_t address); IPAddress(uint32_t address); // NOLINT(runtime/explicit)
operator uint32_t(); operator uint32_t();
bool operator==(IPAddress other);
bool operator!=(IPAddress other);
protected: protected:
uint32_t _address; uint32_t _address;

View File

@@ -16,8 +16,9 @@ uint32_t onPublishCbId = 6;
std::atomic_bool exitProgram(false); std::atomic_bool exitProgram(false);
std::thread t; std::thread t;
const IPAddress broker(127,0,0,1); //const IPAddress broker(127,0,0,1);
//const char* broker = "localhost"; const char* broker = "mqtt";
//const char* broker = "test.mosquitto.org";
const uint16_t broker_port = 1883; const uint16_t broker_port = 1883;
/* /*