Skip to content

Commit b43a8ba

Browse files
committed
GPS - tracking add bug
1 parent f0aefd9 commit b43a8ba

File tree

2 files changed

+116
-82
lines changed

2 files changed

+116
-82
lines changed

src/components/uart/controller.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ UARTController::~UARTController() {
3939
bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
4040
// Attempt to decode the UartAdd message
4141
WS_DEBUG_PRINTLN("[uart] Decoding UartAdd message...");
42-
if (!_uart_model->DecodeUartAdd(stream))
42+
if (!_uart_model->DecodeUartAdd(stream)) {
43+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to decode UartAdd message!");
4344
return false;
45+
}
4446
WS_DEBUG_PRINTLN("[uart] UartAdd message decoded successfully!");
4547
// Get ref. to the UartAdd message within the model
4648
wippersnapper_uart_UartAdd *add_msg = _uart_model->GetUartAddMsg();
@@ -67,9 +69,17 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
6769

6870
// Create a new UartDevice "driver" on the hardware layer (UARTHardware)
6971
wippersnapper_uart_UartDeviceConfig cfg_device = add_msg->cfg_device;
70-
71-
// TODO: Refactor this out into a factory method or similar
72+
WS_DEBUG_PRINT("cfg_device.device_type: ");
73+
WS_DEBUG_PRINTLN(cfg_device.device_type);
7274
drvUartBase *uart_driver = nullptr;
75+
76+
// tODO: Am seeing an issue where device_id causes a crash
77+
// uncomment below:
78+
// WS_DEBUG_PRINT("cfg_device.device_id: ");
79+
// WS_DEBUG_PRINTLN(cfg_device.device_id);
80+
81+
/*
82+
// TODO: Monday, Crash occurs here, no idea why yet
7383
switch (cfg_device.device_type) {
7484
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_UNSPECIFIED:
7585
WS_DEBUG_PRINTLN("[uart] ERROR: Unspecified device type!");
@@ -97,10 +107,12 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
97107
break;
98108
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GENERIC_OUTPUT:
99109
WS_DEBUG_PRINTLN("[uart] Generic Output device type not implemented!");
100-
break;
110+
delete uart_hardware; // cleanup
111+
return false;
101112
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS:
102113
WS_DEBUG_PRINTLN("[uart] GPS device type not implemented!");
103-
break;
114+
delete uart_hardware; // cleanup
115+
return false;
104116
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI:
105117
WS_DEBUG_PRINTLN("[uart] Adding PM2.5 AQI device..");
106118
// Create a new PM2.5 AQI driver instance
@@ -116,12 +128,20 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
116128
break;
117129
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_TM22XX:
118130
WS_DEBUG_PRINTLN("[uart] TM22XX device type not implemented!");
119-
break;
131+
delete uart_hardware; // cleanup
132+
return false;
120133
default:
121134
WS_DEBUG_PRINTLN("[uart] ERROR: Unknown device type!");
135+
delete uart_hardware; // cleanup
122136
return false;
123137
}
138+
124139
// Attempt to initialize the UART driver
140+
if (uart_driver == nullptr) {
141+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to create UART driver!");
142+
delete uart_hardware; // cleanup
143+
return false;
144+
}
125145
bool did_begin = false;
126146
did_begin = uart_driver->begin();
127147
if (!did_begin) {
@@ -156,7 +176,7 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
156176
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to IO!");
157177
return false;
158178
}
159-
179+
*/
160180
return true;
161181
}
162182

src/components/uart/model.cpp

Lines changed: 89 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ UARTModel::~UARTModel() {
4545
@return True if the message was decoded successfully, False otherwise.
4646
*/
4747
bool UARTModel::DecodeUartAdd(pb_istream_t *stream) {
48+
WS_DEBUG_PRINTLN("[uart] Decoding UartAdd message...");
4849
memset(&_msg_UartAdd, 0, sizeof(_msg_UartAdd));
4950
return pb_decode(stream, wippersnapper_uart_UartAdd_fields, &_msg_UartAdd);
5051
}
@@ -53,37 +54,41 @@ bool UARTModel::DecodeUartAdd(pb_istream_t *stream) {
5354
@brief Gets a pointer to the decoded UartAdd message.
5455
@return Pointer to the decoded UartAdd message.
5556
*/
56-
wippersnapper_uart_UartAdd *UARTModel::GetUartAddMsg() {
57-
return &_msg_UartAdd;
58-
}
57+
wippersnapper_uart_UartAdd *UARTModel::GetUartAddMsg() { return &_msg_UartAdd; }
5958

6059
/*!
6160
@brief Encodes a UartAdded message.
6261
@param uart_nbr
63-
The UART port number (eg: 0, 1, 2, etc.) that the device was attached to.
62+
The UART port number (eg: 0, 1, 2, etc.) that the device was
63+
attached to.
6464
@param type
65-
The category of device attached to the UART port, corresponds to its driver type.
65+
The category of device attached to the UART port, corresponds to its
66+
driver type.
6667
@param id
6768
The unique identifier string for the UART device.
6869
@param success
69-
True if the device on the UART port was successfully initialized, False otherwise.
70+
True if the device on the UART port was successfully initialized,
71+
False otherwise.
7072
@return True if the message was encoded successfully, False otherwise.
7173
*/
72-
bool UARTModel::EncodeUartAdded(int32_t uart_nbr, wippersnapper_uart_UartDeviceType type, const char *id, bool success) {
73-
_msg_UartAdded.uart_nbr = uart_nbr;
74-
_msg_UartAdded.type = type;
75-
strncpy(_msg_UartAdded.device_id, id, sizeof(_msg_UartAdded.device_id) - 1);
76-
_msg_UartAdded.device_id[sizeof(_msg_UartAdded.device_id) - 1] = '\0';
77-
_msg_UartAdded.success = success;
78-
// Calculate the size of the encoded message
79-
size_t sz_msg;
80-
if (!pb_get_encoded_size(&sz_msg, wippersnapper_uart_UartAdded_fields,
81-
&_msg_UartAdded))
82-
return false;
83-
// Attempt to encode the message into a buffer
84-
uint8_t buf[sz_msg];
85-
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
86-
return pb_encode(&msg_stream, wippersnapper_uart_UartAdded_fields, &_msg_UartAdded);
74+
bool UARTModel::EncodeUartAdded(int32_t uart_nbr,
75+
wippersnapper_uart_UartDeviceType type,
76+
const char *id, bool success) {
77+
_msg_UartAdded.uart_nbr = uart_nbr;
78+
_msg_UartAdded.type = type;
79+
strncpy(_msg_UartAdded.device_id, id, sizeof(_msg_UartAdded.device_id) - 1);
80+
_msg_UartAdded.device_id[sizeof(_msg_UartAdded.device_id) - 1] = '\0';
81+
_msg_UartAdded.success = success;
82+
// Calculate the size of the encoded message
83+
size_t sz_msg;
84+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_uart_UartAdded_fields,
85+
&_msg_UartAdded))
86+
return false;
87+
// Attempt to encode the message into a buffer
88+
uint8_t buf[sz_msg];
89+
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
90+
return pb_encode(&msg_stream, wippersnapper_uart_UartAdded_fields,
91+
&_msg_UartAdded);
8792
}
8893

8994
/*!
@@ -103,7 +108,8 @@ wippersnapper_uart_UartAdded *UARTModel::GetUartAddedMsg() {
103108
*/
104109
bool UARTModel::DecodeUartDeviceRemove(pb_istream_t *stream) {
105110
memset(&_msg_UartRemove, 0, sizeof(_msg_UartRemove));
106-
return pb_decode(stream, wippersnapper_uart_UartRemove_fields, &_msg_UartRemove);
111+
return pb_decode(stream, wippersnapper_uart_UartRemove_fields,
112+
&_msg_UartRemove);
107113
}
108114

109115
/*!
@@ -118,25 +124,30 @@ wippersnapper_uart_UartRemove *UARTModel::GetUartRemoveMsg() {
118124
@brief Clears an UartInputEvent message.
119125
*/
120126
void UARTModel::ClearUartInputEventMsg() {
121-
memset(&_msg_UartInputEvent, 0, sizeof(_msg_UartInputEvent));
127+
memset(&_msg_UartInputEvent, 0, sizeof(_msg_UartInputEvent));
122128
}
123129

124130
/*!
125131
@brief Configures an UartInputEvent message with addressing information.
126132
@param uart_nbr
127-
The UART port number (eg: 0, 1, 2, etc.) that the device is attached to.
133+
The UART port number (eg: 0, 1, 2, etc.) that the device is attached
134+
to.
128135
@param type
129-
The category of device attached to the UART port, corresponds to its driver type.
136+
The category of device attached to the UART port, corresponds to its
137+
driver type.
130138
@param device_id
131139
The unique identifier string for the UART device.
132140
*/
133-
void UARTModel::ConfigureUartInputEventMsg(uint32_t uart_nbr, wippersnapper_uart_UartDeviceType type, const char *device_id) {
134-
// Addressing information
135-
_msg_UartInputEvent.uart_nbr = uart_nbr;
136-
_msg_UartInputEvent.type = type;
137-
strncpy(_msg_UartInputEvent.device_id, device_id, sizeof(_msg_UartInputEvent.device_id) - 1);
138-
_msg_UartInputEvent.device_id[sizeof(_msg_UartInputEvent.device_id) - 1] = '\0';
139-
141+
void UARTModel::ConfigureUartInputEventMsg(
142+
uint32_t uart_nbr, wippersnapper_uart_UartDeviceType type,
143+
const char *device_id) {
144+
// Addressing information
145+
_msg_UartInputEvent.uart_nbr = uart_nbr;
146+
_msg_UartInputEvent.type = type;
147+
strncpy(_msg_UartInputEvent.device_id, device_id,
148+
sizeof(_msg_UartInputEvent.device_id) - 1);
149+
_msg_UartInputEvent.device_id[sizeof(_msg_UartInputEvent.device_id) - 1] =
150+
'\0';
140151
}
141152

142153
/*!
@@ -147,61 +158,64 @@ void UARTModel::ConfigureUartInputEventMsg(uint32_t uart_nbr, wippersnapper_uart
147158
The type of sensor that produced the event.
148159
@return True if the event was added successfully, False otherwise.
149160
*/
150-
bool UARTModel::AddUartInputEvent(sensors_event_t &event, wippersnapper_sensor_SensorType sensor_type) {
151-
if (_msg_UartInputEvent.events_count >= MAX_UART_INPUT_EVENTS) {
152-
return false; // Maximum number of events reached
153-
}
154-
155-
// Configure the sensor event
156-
wippersnapper_sensor_SensorEvent &sensor_event = _msg_UartInputEvent.events[_msg_UartInputEvent.events_count];
157-
sensor_event.type = sensor_type;
158-
159-
// Handle different sensor types
160-
switch (sensor_type) {
161-
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM10_STD:
162-
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
163-
sensor_event.value.float_value = event.pm10_std;
164-
break;
165-
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM25_STD:
166-
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
167-
sensor_event.value.float_value = event.pm25_std;
168-
break;
169-
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM100_STD:
170-
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
171-
sensor_event.value.float_value = event.pm100_std;
172-
break;
173-
// TODO: Add more cases for other sensor types
174-
default:
175-
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
176-
sensor_event.value.float_value = event.data[0];
177-
break;
178-
}
179-
180-
_msg_UartInputEvent.events_count++;
181-
return true;
161+
bool UARTModel::AddUartInputEvent(sensors_event_t &event,
162+
wippersnapper_sensor_SensorType sensor_type) {
163+
if (_msg_UartInputEvent.events_count >= MAX_UART_INPUT_EVENTS) {
164+
return false; // Maximum number of events reached
165+
}
166+
167+
// Configure the sensor event
168+
wippersnapper_sensor_SensorEvent &sensor_event =
169+
_msg_UartInputEvent.events[_msg_UartInputEvent.events_count];
170+
sensor_event.type = sensor_type;
171+
172+
// Handle different sensor types
173+
switch (sensor_type) {
174+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM10_STD:
175+
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
176+
sensor_event.value.float_value = event.pm10_std;
177+
break;
178+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM25_STD:
179+
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
180+
sensor_event.value.float_value = event.pm25_std;
181+
break;
182+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_PM100_STD:
183+
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
184+
sensor_event.value.float_value = event.pm100_std;
185+
break;
186+
// TODO: Add more cases for other sensor types
187+
default:
188+
sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
189+
sensor_event.value.float_value = event.data[0];
190+
break;
191+
}
192+
193+
_msg_UartInputEvent.events_count++;
194+
return true;
182195
}
183196

184197
/*!
185198
@brief Encodes a UartInputEvent message.
186199
@return True if the message was encoded successfully, False otherwise.
187200
*/
188201
bool UARTModel::EncodeUartInputEvent() {
189-
// Calculate the size of the encoded message
190-
size_t sz_msg;
191-
if (!pb_get_encoded_size(&sz_msg, wippersnapper_uart_UartInputEvent_fields,
192-
&_msg_UartInputEvent))
193-
return false;
194-
195-
// Attempt to encode the message into a buffer
196-
uint8_t buf[sz_msg];
197-
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
198-
return pb_encode(&msg_stream, wippersnapper_uart_UartInputEvent_fields, &_msg_UartInputEvent);
202+
// Calculate the size of the encoded message
203+
size_t sz_msg;
204+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_uart_UartInputEvent_fields,
205+
&_msg_UartInputEvent))
206+
return false;
207+
208+
// Attempt to encode the message into a buffer
209+
uint8_t buf[sz_msg];
210+
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
211+
return pb_encode(&msg_stream, wippersnapper_uart_UartInputEvent_fields,
212+
&_msg_UartInputEvent);
199213
}
200214

201215
/*!
202216
@brief Gets a pointer to the UartInputEvent message.
203217
@return Pointer to the UartInputEvent message.
204218
*/
205219
wippersnapper_uart_UartInputEvent *UARTModel::GetUartInputEventMsg() {
206-
return &_msg_UartInputEvent;
220+
return &_msg_UartInputEvent;
207221
}

0 commit comments

Comments
 (0)