Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor size_t #11

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
291d6ff
refactoring using clang-format
hmueller01 Feb 28, 2025
f23b2a2
refactoring using clang-format
hmueller01 Feb 28, 2025
2115925
refactoring using clang-format
hmueller01 Feb 28, 2025
fe0fae5
refactoring using clang-format, fixed casting, fixed alignment of _ad…
hmueller01 Feb 28, 2025
0f42c08
refactoring using clang-format, fixed casting
hmueller01 Feb 28, 2025
5c068f0
refactoring using clang-format, fixed casting (errors with -Werror)
hmueller01 Feb 28, 2025
c7f6571
fixed type of millis variable
hmueller01 Feb 28, 2025
8e5da77
strict / pedantic checking resulting in error
hmueller01 Feb 28, 2025
77a204b
refactoring using clang-format, fixed casting (errors with -Werror), …
hmueller01 Feb 28, 2025
e4bed08
refactoring using size_t
hmueller01 Feb 28, 2025
ec8c9b7
refactoring using size_t, fixed potential buffer overflow, fixed cast…
hmueller01 Feb 28, 2025
cc6bc6c
added function declarations
hmueller01 Feb 28, 2025
9524e9d
added function declarations
hmueller01 Feb 28, 2025
f14c3a3
fixed function declarations
hmueller01 Feb 28, 2025
b46a29d
fixed function declarations
hmueller01 Feb 28, 2025
f0f3b8c
included tests folder
hmueller01 Feb 28, 2025
f5c7374
added Arduino Uno
hmueller01 Feb 28, 2025
7043424
clang code formatting
hmueller01 Feb 28, 2025
c6e2b54
changed output
hmueller01 Feb 28, 2025
9693ead
exclude esp8266 examples from Arduino Uno
hmueller01 Feb 28, 2025
5b26d17
exclude esp8266 examples from Arduino Uno
hmueller01 Feb 28, 2025
d4a993b
updated callback signature
hmueller01 Feb 28, 2025
24e5d0c
added -Wnull-dereference
hmueller01 Mar 1, 2025
c90e5cd
using static_cast<unsigned long>, optimized buildHeader(), size_t is …
hmueller01 Mar 1, 2025
6efe095
sorted flags, added -Wreorder
hmueller01 Mar 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/build-arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ arduino-cli config set library.enable_unsafe_install true
arduino-cli core update-index
#arduino-cli core search
# Install Arduino cores
#arduino-cli core install arduino:avr
arduino-cli core install arduino:avr
arduino-cli core install esp8266:esp8266
arduino-cli core install esp32:esp32
#arduino-cli board listall
Expand All @@ -27,6 +27,15 @@ arduino-cli lib install --git-url https://github.com/ennui2342/arduino-sram
# Link the project to the Arduino library
ln -s $GITHUB_WORKSPACE $HOME/Arduino/libraries/CI_Test_Library

# Compile all *.ino files for the Arduino
for f in **/*.ino ; do
if [[ "$f" != *mqtt_esp8266.ino && "$f" != *mqtt_large_message.ino ]]; then
echo "################################################################"
echo "Arduino Uno compiling file ${f}"
arduino-cli compile -b arduino:avr:uno $f
fi
done

# Compile all *.ino files for the ESP32
for f in **/*.ino ; do
if [[ "$f" != *"mqtt_stream.ino" ]]; then
Expand Down
4 changes: 2 additions & 2 deletions .github/clang-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ cd $GITHUB_WORKSPACE
sudo apt-get -y install clang-format-19
# Check clang-format output
for f in **/*.{h,c,hpp,cpp,ino} ; do
if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then
#if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then
if [ -f "$f" ]; then
echo "################################################################"
echo "Checking file ${f}"
diff $f <(clang-format-19 -assume-filename=main.cpp $f) 1>&2
echo -e "################################################################\n"
fi
done
2 changes: 1 addition & 1 deletion examples/mqtt_auth/mqtt_auth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED};
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
// handle message arrived
}

Expand Down
4 changes: 2 additions & 2 deletions examples/mqtt_basic/mqtt_basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED};
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Expand Down
4 changes: 2 additions & 2 deletions examples/mqtt_esp8266/mqtt_esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void setup_wifi() {
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Expand Down
8 changes: 4 additions & 4 deletions examples/mqtt_large_message/mqtt_large_message.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ void setup_wifi() {
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Expand All @@ -90,8 +90,8 @@ void callback(char* topic, byte* payload, unsigned int length) {

if (bottleCount > 0) {
// Work out how big our resulting message will be
int msgLen = 0;
for (int i = bottleCount; i > 0; i--) {
size_t msgLen = 0;
for (size_t i = bottleCount; i > 0; i--) {
String numBottles(i);
msgLen += 2 * numBottles.length();
if (i == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
void callback(char* topic, uint8_t* payload, size_t length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
// In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED};
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
// handle message arrived
}

Expand Down Expand Up @@ -46,7 +46,7 @@ void setup() {

void loop() {
if (!client.connected()) {
long now = millis();
unsigned long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
Expand Down
4 changes: 2 additions & 2 deletions examples/mqtt_stream/mqtt_stream.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ IPAddress server(172, 16, 0, 2);

SRAM sram(4, SRAM_1024);

void callback(char* topic, byte* payload, unsigned int length) {
void callback(char* topic, uint8_t* payload, size_t length) {
sram.seek(1);

// do something with the message
for (uint8_t i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
Serial.write(sram.read());
}
Serial.println();
Expand Down
83 changes: 41 additions & 42 deletions src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
if (result == 1) {
nextMsgId = 1;
// Leave room in the buffer for header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
unsigned int j;
size_t length = MQTT_MAX_HEADER_SIZE;

#if MQTT_VERSION == MQTT_VERSION_3_1
uint8_t d[9] = {0x00, 0x06, 'M', 'Q', 'I', 's', 'd', 'p', MQTT_VERSION};
Expand All @@ -143,7 +142,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
uint8_t d[7] = {0x00, 0x04, 'M', 'Q', 'T', 'T', MQTT_VERSION};
#define MQTT_HEADER_VERSION_LENGTH 7
#endif
for (j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) {
for (size_t j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) {
this->buffer[length++] = d[j];
}

Expand Down Expand Up @@ -193,7 +192,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
while (!_client->available()) {
yield();
unsigned long t = millis();
if (t - lastInActivity >= ((int32_t)this->socketTimeout * 1000UL)) {
if (t - lastInActivity >= static_cast<unsigned long>(this->socketTimeout * 1000UL)) {
DEBUG_PSC_PRINTF("connect aborting due to timeout\n");
_state = MQTT_CONNECTION_TIMEOUT;
_client->stop();
Expand Down Expand Up @@ -225,11 +224,11 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c

// reads a byte into result
bool PubSubClient::readByte(uint8_t* result) {
uint32_t previousMillis = millis();
unsigned long previousMillis = millis();
while (!_client->available()) {
yield();
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= ((int32_t)this->socketTimeout * 1000)) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= static_cast<unsigned long>(this->socketTimeout * 1000UL)) {
return false;
}
}
Expand Down Expand Up @@ -330,7 +329,7 @@ bool PubSubClient::loop() {
}
if (_client->available()) {
uint8_t llen;
uint16_t len = readPacket(&llen);
uint32_t len = readPacket(&llen);
uint16_t msgId = 0;
uint8_t* payload;
if (len > 0) {
Expand Down Expand Up @@ -386,23 +385,22 @@ bool PubSubClient::publish(const char* topic, const char* payload, bool retained
return publish(topic, (const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
}

bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength) {
return publish(topic, payload, plength, false);
}

bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) {
bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
if (connected()) {
if (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2 + strnlen(topic, this->bufferSize) + plength) {
// Too long
return false;
}
// Leave room in the buffer for header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
size_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic, this->buffer, length);

// Add payload
unsigned int i;
for (i = 0; i < plength; i++) {
for (size_t i = 0; i < plength; i++) {
this->buffer[length++] = payload[i];
}

Expand All @@ -420,16 +418,15 @@ bool PubSubClient::publish_P(const char* topic, const char* payload, bool retain
return publish_P(topic, (const uint8_t*)payload, payload ? strnlen_P(payload, MQTT_MAX_POSSIBLE_PACKET_SIZE) : 0, retained);
}

bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) {
bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
uint8_t llen = 0;
uint8_t digit;
unsigned int rc = 0;
uint16_t tlen;
unsigned int pos = 0;
unsigned int i;
size_t rc = 0;
size_t tlen;
size_t pos = 0;
uint8_t header;
unsigned int len;
unsigned int expectedLength;
size_t len;
size_t expectedLength;

if (!connected()) {
return false;
Expand Down Expand Up @@ -457,8 +454,8 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned

rc += _client->write(this->buffer, pos);

for (i = 0; i < plength; i++) {
rc += _client->write((char)pgm_read_byte_near(payload + i));
for (size_t i = 0; i < plength; i++) {
rc += _client->write((uint8_t)pgm_read_byte_near(payload + i));
}

lastOutActivity = millis();
Expand All @@ -468,17 +465,17 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned
return (rc == expectedLength);
}

bool PubSubClient::beginPublish(const char* topic, unsigned int plength, bool retained) {
bool PubSubClient::beginPublish(const char* topic, size_t plength, bool retained) {
if (connected()) {
// Send the header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
size_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic, this->buffer, length);
uint8_t header = MQTTPUBLISH;
if (retained) {
header |= 1;
}
size_t hlen = buildHeader(header, this->buffer, plength + length - MQTT_MAX_HEADER_SIZE);
uint16_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen));
size_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen));
lastOutActivity = millis();
return (rc == (length - (MQTT_MAX_HEADER_SIZE - hlen)));
}
Expand All @@ -499,37 +496,39 @@ size_t PubSubClient::write(const uint8_t* buffer, size_t size) {
return _client->write(buffer, size);
}

size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) {
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, size_t length) {
uint8_t lenBuf[4];
uint8_t llen = 0;
uint8_t digit;
uint8_t pos = 0;
uint16_t len = length;
size_t len = length;
do {
digit = len & 127; // digit = len % 128
len >>= 7; // len = len / 128
if (len > 0) {
digit |= 0x80;
}
lenBuf[pos++] = digit;
llen++;
} while (len > 0);
lenBuf[llen++] = digit;
} while (len > 0 && llen < 4);

if (len > 0) {
DEBUG_PSC_PRINTF("length too big %u, left %u, should be 0\r\n", length, len);
}

buf[4 - llen] = header;
for (int i = 0; i < llen; i++) {
for (uint8_t i = 0; i < llen; i++) {
buf[MQTT_MAX_HEADER_SIZE - llen + i] = lenBuf[i];
}
return llen + 1; // Full header size is variable length bit plus the 1-byte fixed header
}

bool PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
uint16_t rc;
uint8_t hlen = buildHeader(header, buf, length);
bool PubSubClient::write(uint8_t header, uint8_t* buf, size_t length) {
size_t rc;
size_t hlen = buildHeader(header, buf, length);

#ifdef MQTT_MAX_TRANSFER_SIZE
uint8_t* writeBuf = buf + (MQTT_MAX_HEADER_SIZE - hlen);
uint16_t bytesRemaining = length + hlen; // Match the length type
uint16_t bytesToWrite;
size_t bytesRemaining = length + hlen; // Match the length type
size_t bytesToWrite;
bool result = true;
while ((bytesRemaining > 0) && result) {
yield();
Expand Down Expand Up @@ -572,7 +571,7 @@ bool PubSubClient::subscribe(const char* topic, uint8_t qos) {
}
this->buffer[length++] = (nextMsgId >> 8);
this->buffer[length++] = (nextMsgId & 0xFF);
length = writeString((char*)topic, this->buffer, length);
length = writeString(topic, this->buffer, length);
this->buffer[length++] = qos;
return write(MQTTSUBSCRIBE | MQTTQOS1, this->buffer, length - MQTT_MAX_HEADER_SIZE);
}
Expand Down Expand Up @@ -613,12 +612,12 @@ void PubSubClient::disconnect() {
lastInActivity = lastOutActivity = millis();
}

uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
size_t PubSubClient::writeString(const char* string, uint8_t* buf, size_t pos) {
const char* idp = string;
uint16_t i = 0;
pos += 2;
while (*idp) {
buf[pos++] = *idp++;
buf[pos++] = (uint8_t)*idp++;
i++;
}
buf[pos - i - 2] = (i >> 8);
Expand Down Expand Up @@ -683,7 +682,7 @@ int PubSubClient::state() {
return this->_state;
}

bool PubSubClient::setBufferSize(uint16_t size) {
bool PubSubClient::setBufferSize(size_t size) {
if (size == 0) {
// Cannot set it back to 0
return false;
Expand All @@ -702,7 +701,7 @@ bool PubSubClient::setBufferSize(uint16_t size) {
return (this->buffer != NULL);
}

uint16_t PubSubClient::getBufferSize() {
size_t PubSubClient::getBufferSize() {
return this->bufferSize;
}

Expand Down
Loading