Skip to content

Commit

Permalink
Merge pull request #584 from adafruit/pb-error-msgs
Browse files Browse the repository at this point in the history
Pb error msgs
  • Loading branch information
tyeth authored Jul 25, 2024
2 parents 9a75742 + 4d1c204 commit 92bb4b6
Show file tree
Hide file tree
Showing 17 changed files with 570 additions and 167 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ src/.vscode/settings.json

examples/Wippersnapper_demo/build/

#Platformio artifacts
# Platformio artifacts
.pio/

# Secrets
data/
47 changes: 39 additions & 8 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,22 @@ lib_deps =
; Common build environment for ESP32 platform
[common:esp32]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.03/platform-espressif32.zip
lib_ignore = WiFiNINA
lib_ignore = WiFiNINA, WiFi101
monitor_filters = esp32_exception_decoder, time

; Common build environment for ESP8266 platform
[common:esp8266]
platform = espressif8266
lib_ignore = WiFiNINA, Adafruit TinyUSB Library
lib_ignore = WiFiNINA, WiFi101, Adafruit TinyUSB Library

; Common build environment for Atmel/Microchip SAMDx platform
[common:atsamd]
platform = atmelsam
platform_packages =
platformio/framework-arduino-samd-adafruit@^1.7.13
platformio/tool-jlink@^1.78811.0
lib_ldf_mode = deep

lib_archive = no ; debug timer issues see https://community.platformio.org/t/choose-usb-stack-as-tiny-usb/22451/5

[common:rp2040]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
Expand All @@ -110,6 +113,9 @@ build_flags = -DUSE_TINYUSB
lib_ignore = WiFiNINA, WiFi101, Adafruit Zero DMA Library
lib_compat_mode = soft ; can be strict once pio detects SleepyDog on RP2040


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Individual Board Definitions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; ESP32-x Boards ;

; Adafruit ESP32 Feather
Expand Down Expand Up @@ -292,14 +298,39 @@ build_flags = -DUSE_TINYUSB=1
[env:adafruit_pyportal_m4_titano]
extends = common:atsamd
board = adafruit_pyportal_m4_titano
; build_type = debug
build_type = debug
upload_protocol = sam-ba
; upload_protocol = jlink
; debug_tool = jlink
debug_tool = jlink
monitor_port = auto
; monitor_port = jlink
; debug_init_break =
; monitor_port = socket://localhost:19021
; debug_init_break = tbreak clearConfiguration
lib_ignore = USBHost
build_flags = -DUSE_TINYUSB=1
-DADAFRUIT_PYPORTAL_M4_TITANO
build_flags = -DUSE_TINYUSB
-D__SAMD51J20A__
-DCRYSTALLESS
-DADAFRUIT_PYPORTAL_M4_TITANO
-D__SAMD51__
-D__FPU_PRESENT
-DARM_MATH_CM4
-mfloat-abi=hard
-mfpu=fpv4-sp-d16
-DCORE_DEBUG_LEVEL=5
-DARDUINO_USB_CDC_ON_BOOT=1
-DCFG_TUSB_DEBUG=1
-DDEBUG=1
-DNDEBUG=1
-DUSE_AIRLIFT=1
-g
; -DUSBCON
; -UCDC_DISABLED
; -UPLUGGABLE_USB_DISABLED
; -DCDC_ENABLED
; -DPLUGGABLE_USB_ENABLED
; -DSERIAL_DEBUG=1
; -DSERIAL_PORT=Serial1
; -DSERCOM_INSTANCE_SERIAL=1

; Adafruit Metro M4 Airlift Lite
[env:adafruit_metro_m4_airliftlite]
Expand Down
212 changes: 127 additions & 85 deletions src/Wippersnapper.cpp

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@
{} ///< Prints line from debug output.
#endif

#define WS_DELAY_WITH_WDT(timeout) \
{ \
unsigned long start = millis(); \
while (millis() - start < timeout) { \
delay(10); \
yield(); \
feedWDT(); \
if (millis() < start) { \
start = millis(); /* if rollover */ \
} \
} \
} ///< Delay function

/**************************************************************************/
/*!
@brief Retry a function until a condition is met or a timeout is reached.
@param func
The function to retry.
@param result_type
The type of the result of the function.
@param result_var
The variable to store the last result of the function.
@param condition
The condition to check the result against.
@param timeout
The maximum time to retry the function.
@param interval
The time to wait between retries.
@param ...
The arguments to pass to the function.
*/
/**************************************************************************/
#define RETRY_FUNCTION_UNTIL_TIMEOUT(func, result_type, result_var, condition, \
timeout, interval, ...) \
{ \
unsigned long startTime = millis(); \
while (millis() - startTime < timeout) { \
result_type result_var = func(__VA_ARGS__); \
if (condition(result_var)) { \
break; \
} \
if (startTime > millis()) { \
startTime = millis(); /* if rollover */ \
} \
WS_DELAY_WITH_WDT(interval); \
} \
} ///< Retry a function until a condition is met or a timeout is reached.

// Wippersnapper pb helpers
#include <nanopb/ws_pb_helpers.h>

// Wippersnapper components
#include "components/analogIO/Wippersnapper_AnalogIO.h"
#include "components/digitalIO/Wippersnapper_DigitalGPIO.h"
Expand Down Expand Up @@ -220,6 +271,7 @@ class Wippersnapper {
void disconnect();

virtual void getMacAddr();
virtual int32_t getRSSI();
virtual void setupMQTTClient(const char *clientID);

virtual ws_status_t networkStatus();
Expand Down
4 changes: 2 additions & 2 deletions src/components/analogIO/Wippersnapper_AnalogIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ bool Wippersnapper_AnalogIO::encodePinEvent(
// Encode signal message
pb_ostream_t stream =
pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!pb_encode(&stream, wippersnapper_signal_v1_CreateSignalRequest_fields,
&outgoingSignalMsg)) {
if (!ws_pb_encode(&stream, wippersnapper_signal_v1_CreateSignalRequest_fields,
&outgoingSignalMsg)) {
WS_DEBUG_PRINTLN("ERROR: Unable to encode signal message");
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions src/components/ds18x20/ws_ds18x20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ bool ws_ds18x20::addDS18x20(
memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing));
pb_ostream_t ostream =
pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!pb_encode(&ostream, wippersnapper_signal_v1_Ds18x20Response_fields,
&msgInitResp)) {
if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_Ds18x20Response_fields,
&msgInitResp)) {
WS_DEBUG_PRINTLN("ERROR: Unable to encode msg_init response message!");
return false;
}
Expand Down Expand Up @@ -257,9 +257,9 @@ void ws_ds18x20::update() {
memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing));
pb_ostream_t ostream = pb_ostream_from_buffer(
WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!pb_encode(&ostream,
wippersnapper_signal_v1_Ds18x20Response_fields,
&msgDS18x20Response)) {
if (!ws_pb_encode(&ostream,
wippersnapper_signal_v1_Ds18x20Response_fields,
&msgDS18x20Response)) {
WS_DEBUG_PRINTLN(
"ERROR: Unable to encode DS18x20 event responsemessage!");
snprintf(buffer, 100,
Expand Down Expand Up @@ -296,6 +296,8 @@ void ws_ds18x20::update() {
WS_DEBUG_PRINT("PUBLISHING -> msgDS18x20Response Event Message...");
if (!WS._mqtt->publish(WS._topic_signal_ds18_device,
WS._buffer_outgoing, msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Unable to publish DS18x20 event message - "
"MQTT Publish failed!");
return;
};
WS_DEBUG_PRINTLN("PUBLISHED!");
Expand Down
5 changes: 3 additions & 2 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ bool WipperSnapper_Component_I2C::encodePublishI2CDeviceEventMsg(
memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing));
pb_ostream_t ostream =
pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields,
msgi2cResponse)) {
if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields,
msgi2cResponse)) {
WS_DEBUG_PRINTLN(
"ERROR: Unable to encode I2C device event response message!");
return false;
Expand All @@ -873,6 +873,7 @@ bool WipperSnapper_Component_I2C::encodePublishI2CDeviceEventMsg(
WS_DEBUG_PRINT("PUBLISHING -> I2C Device Sensor Event Message...");
if (!WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: MQTT Publish failed!");
return false;
};
WS_DEBUG_PRINTLN("PUBLISHED!");
Expand Down
4 changes: 2 additions & 2 deletions src/components/pixels/ws_pixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void ws_pixels::publishAddStrandResponse(bool is_success,
memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing));
pb_ostream_t ostream =
pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing));
if (!pb_encode(&ostream, wippersnapper_signal_v1_PixelsResponse_fields,
&msgInitResp)) {
if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_PixelsResponse_fields,
&msgInitResp)) {
WS_DEBUG_PRINTLN("ERROR: Unable to encode "
"wippersnapper_signal_v1_PixelsResponse message!");
return;
Expand Down
11 changes: 6 additions & 5 deletions src/components/register/Wippersnapper_Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool Wippersnapper::encodePubRegistrationReq() {
pb_ostream_t _msg_stream =
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));

_status = pb_encode(
_status = ws_pb_encode(
&_msg_stream,
wippersnapper_description_v1_CreateDescriptionRequest_fields, &_message);
size_t _message_len = _msg_stream.bytes_written;
Expand Down Expand Up @@ -107,9 +107,10 @@ void Wippersnapper::decodeRegistrationResp(char *data, uint16_t len) {
// create input stream for buffer
pb_istream_t stream = pb_istream_from_buffer(buffer, len);
// decode the stream
if (!pb_decode(&stream,
wippersnapper_description_v1_CreateDescriptionResponse_fields,
&message)) {
if (!ws_pb_decode(
&stream,
wippersnapper_description_v1_CreateDescriptionResponse_fields,
&message)) {
WS.haltError("Could not decode registration response");
}
// Decode registration response message
Expand Down Expand Up @@ -140,7 +141,7 @@ void Wippersnapper::decodeRegistrationResp(char *data, uint16_t len) {
pb_ostream_t _msg_stream =
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));

bool _status = pb_encode(
bool _status = ws_pb_encode(
&_msg_stream, wippersnapper_description_v1_RegistrationComplete_fields,
&msg);
size_t _message_len = _msg_stream.bytes_written;
Expand Down
4 changes: 2 additions & 2 deletions src/components/uart/drivers/ws_uart_drv_pm25aqi.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
uint8_t mqttBuffer[512] = {0};
pb_ostream_t ostream =
pb_ostream_from_buffer(mqttBuffer, sizeof(mqttBuffer));
if (!pb_encode(&ostream, wippersnapper_signal_v1_UARTResponse_fields,
&msgUARTResponse)) {
if (!ws_pb_encode(&ostream, wippersnapper_signal_v1_UARTResponse_fields,
&msgUARTResponse)) {
Serial.println("[ERROR, UART]: Unable to encode device response!");
return;
}
Expand Down
61 changes: 61 additions & 0 deletions src/nanopb/ws_pb_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*!
* @file ws_pb_helpers.cpp
*
* Protobuf encode/decode helpers with error logging for Wippersnapper.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/

#include "ws_pb_helpers.h"
#include "../Wippersnapper.h"

// *****************************************************************************
/*!
@brief Decodes a protobuf message from a stream and prints any error.
@param stream
The stream to decode from.
@param fields
The protobuf message fields.
@param dest_struct
The destination struct to decode into.
@return True if decode was successful, false otherwise.
!*/
// *****************************************************************************
bool ws_pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields,
void *dest_struct) {
bool status = pb_decode(stream, fields, dest_struct);
if (!status) {
WS_DEBUG_PRINT("Protobuf decode error: ");
WS_DEBUG_PRINTLN(PB_GET_ERROR(stream));
}
return status;
}

// *****************************************************************************
/*!
@brief Encodes a protobuf message to a stream and prints any error.
@param stream
The stream to encode to.
@param fields
The protobuf message fields.
@param src_struct
The source struct to encode from.
@return True if encode was successful, false otherwise.
!*/
// *****************************************************************************
bool ws_pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields,
const void *src_struct) {
bool status = pb_encode(stream, fields, src_struct);
if (!status) {
WS_DEBUG_PRINT("Protobuf encode error: ");
WS_DEBUG_PRINTLN(PB_GET_ERROR(stream));
}
return status;
}
28 changes: 28 additions & 0 deletions src/nanopb/ws_pb_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* @file ws_pb_helpers.h
*
* Protobuf encode/decode helpers with error logging for Wippersnapper.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#ifndef WS_PB_ENCODE_H
#define WS_PB_ENCODE_H

#include "pb.h"
#include "pb_decode.h"
#include "pb_encode.h"

bool ws_pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields,
void *dest_struct);

bool ws_pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields,
const void *src_struct);

#endif // WS_PB_ENCODE_H
Loading

0 comments on commit 92bb4b6

Please sign in to comment.