Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
583 changes: 296 additions & 287 deletions src/Wippersnapper_V2.cpp

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/Wippersnapper_V2.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
*/
#ifdef WS_DEBUG
#define WS_DEBUG_PRINT(...) \
{ WS_PRINTER.print(__VA_ARGS__); } /**< Print debug message to serial */
{ \
WS_PRINTER.print(__VA_ARGS__); \
} /**< Print debug message to serial */
#define WS_DEBUG_PRINTLN(...) \
{ \
WS_PRINTER.println(__VA_ARGS__); \
Expand All @@ -42,9 +44,11 @@
} /**< Print debug message in hexadecimal */
#else
#define WS_DEBUG_PRINT(...) \
{} /**< Debug print */
{ \
} /**< Debug print */
#define WS_DEBUG_PRINTLN(...) \
{} /**< Debug println */
{ \
} /**< Debug println */
#endif

/*!
Expand Down Expand Up @@ -195,7 +199,7 @@ class Wippersnapper_V2 {
bool PublishSignal(pb_size_t which_payload, void *payload);

// Checkin API
bool CreateCheckinRequest();
bool PublishCheckinRequest();
void PollCheckinResponse();

// run() loop
Expand Down
4 changes: 2 additions & 2 deletions src/components/analogIO/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ bool AnalogIOController::EncodePublishPinEvent(

// Publish the AnalogIO message to the broker
WS_DEBUG_PRINTLN("Publishing AnalogIOEvent message to broker...");
if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_analogio_event_tag,
_analogio_model->GetAnalogIOEvent())) {
WS_DEBUG_PRINTLN("ERROR: Unable to publish analogio voltage event message, "
"moving onto the next pin!");
return false;
}
} */
WS_DEBUG_PRINTLN("Published AnalogIOEvent message to broker!")

return true;
Expand Down
120 changes: 119 additions & 1 deletion src/components/checkin/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,116 @@ CheckinModel::~CheckinModel() {
memset(&_CheckinResponse, 0, sizeof(_CheckinResponse));
}

bool CheckinModel::EncodeD2bCheckinRequest(const char *hw_uid,
const char *fw_ver) {
// Validate input len
if (strlen(hw_uid) >=
sizeof(_CheckinD2B.payload.checkin_request.hardware_uid) ||
strlen(fw_ver) >=
sizeof(_CheckinD2B.payload.checkin_request.firmware_version)) {
return false;
}

// Zero-out the message envelope
memset(&_CheckinD2B, 0, sizeof(_CheckinD2B));
// Set which_payload
_CheckinD2B.which_payload =
wippersnapper_checkin_CheckinD2B_checkin_request_tag;
// Safely fill the CheckinRequest sub-message payload
strncpy(_CheckinD2B.payload.checkin_request.hardware_uid, hw_uid,
sizeof(_CheckinD2B.payload.checkin_request.hardware_uid) - 1);
_CheckinD2B.payload.checkin_request
.hardware_uid[sizeof(_CheckinD2B.payload.checkin_request.hardware_uid) -
1] = '\0';
strncpy(_CheckinD2B.payload.checkin_request.firmware_version, fw_ver,
sizeof(_CheckinD2B.payload.checkin_request.firmware_version) - 1);
_CheckinD2B.payload.checkin_request.firmware_version
[sizeof(_CheckinD2B.payload.checkin_request.firmware_version) - 1] = '\0';

// Obtain size of the CheckinD2B message
size_t CheckinD2BSz;
if (!pb_get_encoded_size(
&CheckinD2BSz, wippersnapper_checkin_CheckinD2B_fields, &_CheckinD2B))
return false;
// Create a temporary buffer for holding the CheckinD2B message
uint8_t buf[CheckinD2BSz];
// Create a stream that will write to buf
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
// Attempt to encode the message
return pb_encode(&msg_stream, wippersnapper_checkin_CheckinD2B_fields,
&_CheckinD2B);
}


bool CheckinModel::EncodeD2bCheckinComplete(bool success) {
// Zero-out the message envelope
memset(&_CheckinD2B, 0, sizeof(_CheckinD2B));
// Set which_payload
_CheckinD2B.which_payload =
wippersnapper_checkin_CheckinD2B_checkin_complete_tag;

// Obtain size of the CheckinD2B message
size_t CheckinD2BSz;
if (!pb_get_encoded_size(
&CheckinD2BSz, wippersnapper_checkin_CheckinD2B_fields, &_CheckinD2B))
return false;
// Create a temporary buffer for holding the CheckinD2B message
uint8_t buf[CheckinD2BSz];
// Create a stream that will write to buf
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
// Attempt to encode the message
return pb_encode(&msg_stream, wippersnapper_checkin_CheckinD2B_fields,
&_CheckinD2B);
}


wippersnapper_checkin_CheckinD2B*CheckinModel::getD2bCheckinComplete() {
return &_CheckinD2B;
}


bool CheckinModel::DecodeB2d(pb_istream_t *stream) {
// Zero-out the CheckinB2D message
memset(&_CheckinB2D, 0, sizeof(_CheckinB2D));
// Decode the message
if (!pb_decode(stream, wippersnapper_checkin_CheckinB2D_fields, &_CheckinB2D))
return false;

// Attempt to get the payload and dispatch to the appropriate handler
if (_CheckinB2D.which_payload == wippersnapper_checkin_CheckinB2D_checkin_response_tag) {
// Validate IO found the board's definition file
if (_CheckinB2D.payload.checkin_response.response != wippersnapper_checkin_CheckinResponse_Response_RESPONSE_OK) {
// TODO - Fix: raise and halt the app from continuing
WS_DEBUG_PRINTLN("ERROR: CheckinResponse indicates board not found!");
return false;
}

// TODO - Refactor
// Parse the CheckinResponse sub-message
_total_analog_pins = _CheckinB2D.payload.checkin_response.total_analog_pins;
_total_gpio_pins = _CheckinB2D.payload.checkin_response.total_gpio_pins;
_reference_voltage = _CheckinB2D.payload.checkin_response.reference_voltage;
WsV2.digital_io_controller->SetMaxDigitalPins(_total_gpio_pins);
WsV2.analogio_controller->SetRefVoltage(_reference_voltage);
WsV2.analogio_controller->SetTotalAnalogPins(_total_analog_pins);

// Okay, now let's look at component_adds[32] array
for (pb_size_t i = 0; i < _CheckinB2D.payload.checkin_response.component_adds_count; i++) {
// For now, just print out what component_adds we have
wippersnapper_checkin_ComponentAdd *compAdd = &_CheckinB2D.payload.checkin_response.component_adds[i];
if (compAdd->which_payload == wippersnapper_checkin_ComponentAdd_digitalio_tag) {
// Process a DigitalIOAdd component add message
WS_DEBUG_PRINTLN("INFO: Found DigitalIOAdd component_add in CheckinResponse.");
WsV2.digital_io_controller->Handle_DigitalIO_Add(&compAdd->payload.digitalio);
} else {
// Component type not found
WS_DEBUG_PRINTLN("WARNING: Unknown component_add type in CheckinResponse!");
}
}
}
return true;
}

/*!
@brief Fills and creates a CheckinRequest message
@param hardware_uid
Expand Down Expand Up @@ -116,6 +226,14 @@ wippersnapper_checkin_CheckinRequest *CheckinModel::getCheckinRequest() {
return &_CheckinRequest;
}

/*!
@brief Gets the CheckinD2B message
@returns CheckinD2B message pointer.
*/
wippersnapper_checkin_CheckinD2B *CheckinModel::getD2bCheckinRequest() {
return &_CheckinD2B;
}

/*!
@brief Sets the CheckinResponse message's total GPIO pins field
@param total_gpio_pins
Expand Down Expand Up @@ -159,4 +277,4 @@ void CheckinModel::setReferenceVoltage(float reference_voltage) {
@brief Gets the CheckinResponse message's reference voltage field
@returns Reference voltage.
*/
float CheckinModel::getReferenceVoltage() { return _reference_voltage; }
float CheckinModel::getReferenceVoltage() { return _reference_voltage; }
14 changes: 14 additions & 0 deletions src/components/checkin/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define WS_CHECKIN_MODEL_H
#include "Wippersnapper_V2.h"

class Wippersnapper_V2; ///< Forward declaration

/*!
@brief Provides an interface for creating, encoding, and parsing
messages from checkin.proto.
Expand All @@ -24,6 +26,15 @@ class CheckinModel {
public:
CheckinModel();
~CheckinModel();
// DeviceToBroker Message Envelope
bool EncodeD2bCheckinRequest(const char *hw_uid, const char *fw_ver);
wippersnapper_checkin_CheckinD2B *getD2bCheckinRequest();
bool EncodeD2bCheckinComplete(bool success);
wippersnapper_checkin_CheckinD2B *getD2bCheckinComplete();

// BrokerToDevice Message Envelope
bool DecodeB2d(pb_istream_t *stream);

// Request Message
void CreateCheckinRequest(const char *hardware_uid,
const char *firmware_version);
Expand All @@ -43,11 +54,14 @@ class CheckinModel {
float getReferenceVoltage();

private:
wippersnapper_checkin_CheckinD2B _CheckinD2B; ///< DeviceToBroker message envelope
wippersnapper_checkin_CheckinB2D _CheckinB2D; ///< BrokerToDevice message envelope
wippersnapper_checkin_CheckinRequest _CheckinRequest;
wippersnapper_checkin_CheckinResponse _CheckinResponse;
wippersnapper_checkin_CheckinResponse_Response _response;
int32_t _total_gpio_pins;
int32_t _total_analog_pins;
float _reference_voltage;
};
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
#endif // WS_CHECKIN_H
61 changes: 57 additions & 4 deletions src/components/digitalIO/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,66 @@ void DigitalIOController::SetMaxDigitalPins(uint8_t max_digital_pins) {
_max_digitalio_pins = max_digital_pins;
}


bool DigitalIOController::Handle_DigitalIO_Add(wippersnapper_digitalio_DigitalIOAdd *msg_dio_add) {
// Early-out if we have reached the maximum number of digital pins
if (_digitalio_pins.size() >= _max_digitalio_pins) {
WS_DEBUG_PRINTLN("[digitalio] ERROR: Can not add new pin, all pins have "
"already been allocated!");
return false;
}

// Strip the D/A prefix off the pin name and convert to a uint8_t pin number
int pin_name = atoi(msg_dio_add->pin_name + 1);

// Check if the provided pin is also the status LED pin
if (_dio_hardware->IsStatusLEDPin(pin_name))
ReleaseStatusPixel();

// Deinit the pin if it's already in use
if (GetPinIdx(pin_name) != -1)
_dio_hardware->deinit(pin_name);

// Attempt to configure the pin
if (!_dio_hardware->ConfigurePin(
pin_name, msg_dio_add->gpio_direction)) {
WS_DEBUG_PRINTLN(
"[digitalio] ERROR: Pin provided an invalid protobuf direction!");
return false;
}

ulong period = msg_dio_add->period;
// Create the digital pin and add it to the vector
DigitalIOPin new_pin = {
.pin_name = pin_name,
.pin_direction = msg_dio_add->gpio_direction,
.sample_mode = msg_dio_add->sample_mode,
.pin_value = msg_dio_add->value,
.pin_period = period * 1000,
.prv_pin_time = (millis() - 1) - period};
_digitalio_pins.push_back(new_pin);

// Print out the pin's details
WS_DEBUG_PRINTLN("[digitalio] Added new pin:");
WS_DEBUG_PRINT("Pin Name: ");
WS_DEBUG_PRINTLN(new_pin.pin_name);
WS_DEBUG_PRINT("Period: ");
WS_DEBUG_PRINTLN(new_pin.pin_period);
WS_DEBUG_PRINT("Sample Mode: ");
WS_DEBUG_PRINTLN(new_pin.sample_mode);
WS_DEBUG_PRINT("Direction: ");
WS_DEBUG_PRINTLN(new_pin.pin_direction);

return true;
}

/*!
@brief Add a new digital pin to the controller
@param stream
The nanopb input stream.
@return True if the digital pin was successfully added.
*/
bool DigitalIOController::Handle_DigitalIO_Add(pb_istream_t *stream) {
/* bool DigitalIOController::Handle_DigitalIO_Add(pb_istream_t *stream) {
// Early-out if we have reached the maximum number of digital pins
if (_digitalio_pins.size() >= _max_digitalio_pins) {
WS_DEBUG_PRINTLN("[digitalio] ERROR: Can not add new pin, all pins have "
Expand Down Expand Up @@ -105,7 +158,7 @@ bool DigitalIOController::Handle_DigitalIO_Add(pb_istream_t *stream) {
WS_DEBUG_PRINTLN(new_pin.pin_direction);

return true;
}
} */

/*!
@brief Removes a digital pin from the controller, if it exists
Expand Down Expand Up @@ -295,13 +348,13 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
}

// Publish the DigitalIOEvent message to the broker
if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_digitalio_event_tag,
_dio_model->GetDigitalIOEventMsg())) {
WS_DEBUG_PRINTLN("[digitalio] ERROR: Unable to publish event message, "
"moving onto the next pin!");
return false;
}
} */
WS_DEBUG_PRINTLN("[digitalio] Published DigitalIOEvent to broker!")
} else {
// let's log the event to the SD card
Expand Down
4 changes: 3 additions & 1 deletion src/components/digitalIO/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class DigitalIOController {
public:
DigitalIOController();
~DigitalIOController();
// NEW: Handler for wippersnapper_digitalio_DigitalIOAdd struct rather than stream
bool Handle_DigitalIO_Add(wippersnapper_digitalio_DigitalIOAdd *msg_dio_add);
// Called by the cbDecodeBrokerToDevice router function
bool Handle_DigitalIO_Add(pb_istream_t *stream);
// bool Handle_DigitalIO_Add(pb_istream_t *stream);
bool Handle_DigitalIO_Write(pb_istream_t *stream);
bool Handle_DigitalIO_Remove(pb_istream_t *stream);

Expand Down
8 changes: 4 additions & 4 deletions src/components/ds18x20/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
return false;
}

if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
_DS18X20_model->GetDS18x20AddedMsg())) {
WS_DEBUG_PRINTLN(
"ERROR | DS18x20: Unable to publish Ds18x20Added message!");
return false;
}
} */
}

return true;
Expand Down Expand Up @@ -260,13 +260,13 @@ void DS18X20Controller::update() {
}
// Publish the Ds18x20Event message to the broker
WS_DEBUG_PRINT("DS18x20: Publishing event to broker...");
if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
_DS18X20_model->GetDS18x20EventMsg())) {
WS_DEBUG_PRINTLN(
"ERROR | DS18x20: Failed to publish Ds18x20Event message");
continue;
}
} */
WS_DEBUG_PRINTLN("Published!");
} else {
if (!WsV2._sdCardV2->LogDS18xSensorEventToSD(
Expand Down
4 changes: 2 additions & 2 deletions src/components/gps/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ void GPSController::update() {
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to encode GPSEvent!");
} else {
// Publish the GPSEvent to IO
if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_gps_event_tag,
_gps_model->GetGPSEvent())) {
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to publish GPSEvent!");
} else {
WS_DEBUG_PRINTLN("...ok!");
}
} */
}
drv->SetPollPeriodPrv(cur_time);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/i2c/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,13 @@ bool I2cController::PublishI2cDeviceAddedorReplaced(
"[i2c] ERROR: Unable to encode I2cDeviceAddedorReplaced message!");
return false;
}
if (!WsV2.PublishSignal(
/* if (!WsV2.PublishSignal(
wippersnapper_signal_DeviceToBroker_i2c_device_added_replaced_tag,
_i2c_model->GetMsgI2cDeviceAddedOrReplaced())) {
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish I2cDeviceAddedorReplaced "
"message to IO!");
return false;
}
} */
return true;
}

Expand Down
Loading
Loading