summaryrefslogtreecommitdiff
path: root/src/slave.c
diff options
context:
space:
mode:
authorAditya Naik2020-06-18 17:24:49 -0400
committerAditya Naik2020-06-22 12:41:42 -0400
commit2acabd3ce6ed5522a2b3ce6728facc74bbbbd23a (patch)
tree1920dbc79c467cdbdb8cc5e1919dff3635439c5f /src/slave.c
parent0746618ebe57ba8d452ad5cc907ddd478b512898 (diff)
Optimizations for master data and command routing handling and corresponding changes for slave data and command decoding. Master no longer decodes data in order send individual datapoints in a packed data_message field individually, but stores and forwards the encoded data message and MDR to the slaves during routing. This significantly optimizes data routing by reducing the time taken to decode and encode every single packed datapoint.
Task List: None
Diffstat (limited to 'src/slave.c')
-rw-r--r--src/slave.c62
1 files changed, 52 insertions, 10 deletions
diff --git a/src/slave.c b/src/slave.c
index 7bcceac..f5096f3 100644
--- a/src/slave.c
+++ b/src/slave.c
@@ -43,6 +43,7 @@ UART_HandleTypeDef huart1;
uint8_t cmd_str[128];
uint8_t str_ptr = 0;
pb_byte_t str_buf[18];
+uint8_t data_idx = 0;
/* Function prototypes */
void SystemClock_Config(void);
@@ -150,7 +151,7 @@ int main(void)
else if (SOR_buf[0] == 2) {
uint8_t CTS_buf[] = {0x0, 0x1};
uint8_t len_buf[4], *MDR_buf, *data_buf;
- /* _datapoint datapoints[16]; */
+ _datapoint datapoints[16];
HAL_I2C_Slave_Transmit(&hi2c1, CTS_buf, 2, 10000);
sprintf((char*)debug_buf, "Sent CTS\r\n");
@@ -171,18 +172,27 @@ int main(void)
HAL_I2C_Slave_Transmit(&hi2c1, CTS_buf, 2, 10000);
HAL_I2C_Slave_Receive(&hi2c1, MDR_buf, MDR_len, 10000);
HAL_I2C_Slave_Receive(&hi2c1, data_buf, data_len, 10000);
-
- _datapoint datapoint_message;
+
s2m_MDR_response MDR_message;
+ s2m_data data_message;
+ data_idx = 0;
+ data_message.datapoints.funcs.decode = decode_data_callback;
+ data_message.datapoints.arg = (void*)datapoints;
+
pb_istream_t MDR_istream = pb_istream_from_buffer(MDR_buf, MDR_len);
pb_istream_t data_istream = pb_istream_from_buffer(data_buf, data_len);
-
+
pb_decode(&MDR_istream, s2m_MDR_response_fields, &MDR_message);
- pb_decode(&data_istream, _datapoint_fields, &datapoint_message);
-
- sprintf((char*)debug_buf, "Got data from %ld, running version %f\r\n\tdata 0: %f\r\n", MDR_message.module_id, MDR_message.MDR_version, datapoint_message.data);
- HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
- memset(debug_buf, 0, 128);
+ if (!pb_decode(&data_istream, s2m_data_fields, &data_message)){
+ sprintf((char*)debug_buf, "Data decode error\r\n");
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ memset(debug_buf, 0, 128);
+ }
+ else {
+ sprintf((char*)debug_buf, "Got data from %ld\r\n\tEntity: %ld, data : %f, unit: %ld\r\n", MDR_message.module_id, datapoints[0].entity_id, datapoints[0].data, datapoints[0].unit_id);
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ memset(debug_buf, 0, 128);
+ }
}
else if (SOR_buf[0] == 3) {
uint8_t CTS_buf[] = {0x0, 0x1}, len_buf[4], *cmd_buf;
@@ -307,7 +317,7 @@ bool encode_subscription_callback(pb_ostream_t *ostream, const pb_field_t *field
{
if(ostream!=NULL && field->tag == s2m_MDR_response_subscriptions_tag) {
_subscriptions subs;
- subs.module_id = 0xd;
+ subs.module_id = 0x4;
subs.has_module_id=true;
subs.has_entity_id=false;
subs.has_module_class=false;
@@ -332,6 +342,8 @@ bool encode_datapoint_callback(pb_ostream_t *ostream, const pb_field_t *field, v
_datapoint datapoint = _datapoint_init_zero;
datapoint.entity_id = 1;
datapoint.data = 20.70+((float)i/100);
+ datapoint.unit_id = 1;
+ datapoint.has_unit_id = true;
if (!pb_encode_tag_for_field(ostream, field))
return false;
if (!pb_encode_submessage(ostream, _datapoint_fields, &datapoint))
@@ -362,6 +374,36 @@ bool decode_cmd_string_callback(pb_istream_t *istream, const pb_field_t *field,
return true;
}
+bool decode_data_callback(pb_istream_t *istream, const pb_field_t *field, void **args)
+{
+ _datapoint loc_datapoint = _datapoint_init_zero;
+ _datapoint *datapoint = *args;
+
+ if (!pb_decode(istream, _datapoint_fields, &loc_datapoint))
+ return false;
+
+ datapoint[data_idx].data = datapoint[data_idx].entity_id = 0;
+
+ datapoint[data_idx].entity_id = loc_datapoint.entity_id;
+ datapoint[data_idx].data = loc_datapoint.data;
+
+ if (loc_datapoint.has_channel_id) {
+ datapoint[data_idx].has_channel_id = true;
+ datapoint[data_idx].channel_id = loc_datapoint.channel_id;
+ }
+ if (loc_datapoint.has_unit_id) {
+ datapoint[data_idx].has_unit_id = true;
+ datapoint[data_idx].unit_id = loc_datapoint.unit_id;
+ }
+ if (loc_datapoint.has_timestamp) {
+ datapoint[data_idx].has_timestamp = true;
+ datapoint[data_idx].timestamp = loc_datapoint.timestamp;
+ }
+
+ data_idx++;
+ return true;
+}
+
/**
* @brief System Clock Configuration
* @retval None