|
|
@@ -5,61 +5,68 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
{
|
|
|
switch (nbus->function_code.function)
|
|
|
{
|
|
|
- case CMD_FIND: {
|
|
|
- nbus->tx_buffer[4] = nbus->interface->find(nbus->rx_buffer[3]);
|
|
|
- nbus->tx_length += 1;
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
case CMD_ECHO: {
|
|
|
- for (uint8_t i = 3; i < nbus->rx_length - 1; i++)
|
|
|
+ for (uint8_t i = RX_DT; i < nbus->rx_length - 1; i++)
|
|
|
{
|
|
|
nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
|
|
|
}
|
|
|
- nbus->tx_length += (nbus->rx_length - 4);
|
|
|
+ nbus->tx_length += (nbus->rx_length - RX_META);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case CMD_PARAM: {
|
|
|
- if (nbus->rx_length == META_SIZE) // get concrete param
|
|
|
+
|
|
|
+ switch (nbus->rx_length)
|
|
|
+ {
|
|
|
+ case RX_META: // get all params
|
|
|
+ {
|
|
|
+ for (uint8_t i = 0, j = 0; i < _NBUS_PARAM_COUNT; i++)
|
|
|
+ {
|
|
|
+ if (nbus->interface->hasParam(0, i))
|
|
|
+ {
|
|
|
+ int32_t param_value = nbus->interface->getParam(0, i);
|
|
|
+ nbus->tx_buffer[4 + 5 * j] = i; // param id
|
|
|
+ nbus->tx_buffer[5 + 5 * j] = (uint8_t)(param_value & 0xFF); // param value 4B
|
|
|
+ nbus->tx_buffer[6 + 5 * j] = (uint8_t)((param_value >> 8) & 0xFF);
|
|
|
+ nbus->tx_buffer[7 + 5 * j] = (uint8_t)((param_value >> 16) & 0xFF);
|
|
|
+ nbus->tx_buffer[8 + 5 * j] = (uint8_t)((param_value >> 24) & 0xFF);
|
|
|
+ nbus->tx_length += 5;
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case RX_META + 1: // get specified parameter: 4 meta + 1 param id
|
|
|
{
|
|
|
- if (nbus->interface->hasParam(0, (nBus_param_t)nbus->rx_buffer[3]) == 0) // handle non-existing param
|
|
|
+ if (nbus->interface->hasParam(0, (nBus_param_t)nbus->rx_buffer[RX_DT]) == 0) // handle non-existing param
|
|
|
{
|
|
|
setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- nbus->tx_buffer[4] = nbus->rx_buffer[3]; // param id
|
|
|
- int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]); // param value 4B
|
|
|
+ nbus->tx_buffer[4] = nbus->rx_buffer[RX_DT]; // param id
|
|
|
+ int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[RX_DT]); // param value 4B
|
|
|
nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
|
|
|
nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
|
|
|
nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
|
|
|
nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
|
|
|
nbus->tx_length += 5;
|
|
|
}
|
|
|
- else // get all params
|
|
|
- {
|
|
|
- for (uint8_t i = 0; i < _NBUS_PARAM_COUNT; i++)
|
|
|
- {
|
|
|
- if (nbus->interface->hasParam(0, i))
|
|
|
- {
|
|
|
- int32_t param_value = nbus->interface->getParam(0, i);
|
|
|
- nbus->tx_buffer[4 + 5 * i] = i; // param id
|
|
|
- nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF); // param value 4B
|
|
|
- nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
|
|
|
- nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
|
|
|
- nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
|
|
|
+ break;
|
|
|
|
|
|
- nbus->tx_length += 5;
|
|
|
- }
|
|
|
- }
|
|
|
+ default:
|
|
|
+ setErrorResponse(nbus, ILLEGAL_FUNCTION_PARAM);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case CMD_SENSOR_CNT: {
|
|
|
- nbus->tx_buffer[4] = nbus->interface->getSensorCount();
|
|
|
- nbus->tx_length += 1;
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+ nbus->tx_buffer[4] = sensor_count.read_only_count;
|
|
|
+ nbus->tx_buffer[5] = sensor_count.read_write_count;
|
|
|
+ nbus->tx_length += 2;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -67,7 +74,7 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
if (nbus->interface->device_ready())
|
|
|
{
|
|
|
// response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
|
|
|
- uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
|
|
|
+ uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[TX_DT]);
|
|
|
if (cnt == 0)
|
|
|
{
|
|
|
setErrorResponse(nbus, DEVICE_BUSY);
|
|
|
@@ -82,11 +89,25 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
break;
|
|
|
|
|
|
case CMD_SENSOR_TYPE: {
|
|
|
+
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+
|
|
|
// response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
|
|
|
- for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++)
|
|
|
+
|
|
|
+ // add read_only sensors
|
|
|
+ uint8_t i = 0;
|
|
|
+ for (; i < sensor_count.read_only_count; i++)
|
|
|
+ {
|
|
|
+ nbus->tx_buffer[4 + 2 * i] = i + SENSOR_RO_ADDR;
|
|
|
+ nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i + SENSOR_RO_ADDR);
|
|
|
+ nbus->tx_length += 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ // add read_write sensors
|
|
|
+ for (uint8_t j = 0; j < sensor_count.read_write_count; j++, i++)
|
|
|
{
|
|
|
- nbus->tx_buffer[4 + 2 * i] = i;
|
|
|
- nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
|
|
|
+ nbus->tx_buffer[4 + 2 * i] = j + SENSOR_RW_ADDR;
|
|
|
+ nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(j + SENSOR_RW_ADDR);
|
|
|
nbus->tx_length += 2;
|
|
|
}
|
|
|
}
|
|
|
@@ -137,19 +158,36 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
break;
|
|
|
|
|
|
case CMD_FORMAT: {
|
|
|
- uint8_t sensor_cnt = nbus->interface->getSensorCount();
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+ nBus_sensorFormat_t format;
|
|
|
+ uint8_t *format_ptr;
|
|
|
|
|
|
- for (int8_t i = 0; i < sensor_cnt; ++i)
|
|
|
+ // add read_only sensors
|
|
|
+ uint8_t i = 0;
|
|
|
+ for (; i < sensor_count.read_only_count; ++i)
|
|
|
{
|
|
|
- nBus_sensorFormat_t format = nbus->interface->getSensorFormat(i + 1);
|
|
|
- uint8_t *format_ptr = (uint8_t *)&format;
|
|
|
- nbus->tx_buffer[4 * i + 4] = i + 1;
|
|
|
+ format = nbus->interface->getSensorFormat(i + SENSOR_RO_ADDR);
|
|
|
+ format_ptr = (uint8_t *)&format;
|
|
|
+ nbus->tx_buffer[4 * i + 4] = i + SENSOR_RO_ADDR;
|
|
|
nbus->tx_buffer[4 * i + 5] = format_ptr[0];
|
|
|
nbus->tx_buffer[4 * i + 6] = format_ptr[1];
|
|
|
nbus->tx_buffer[4 * i + 7] = format_ptr[2];
|
|
|
}
|
|
|
|
|
|
- nbus->tx_length += 4 * sensor_cnt;
|
|
|
+ nbus->tx_length += 4 * sensor_count.read_only_count;
|
|
|
+
|
|
|
+ // add read_write sensors
|
|
|
+ for (uint8_t j = 0; j < sensor_count.read_write_count; ++j, ++i)
|
|
|
+ {
|
|
|
+ format = nbus->interface->getSensorFormat(j + SENSOR_RW_ADDR);
|
|
|
+ format_ptr = (uint8_t *)&format;
|
|
|
+ nbus->tx_buffer[4 * i + 4] = j + SENSOR_RW_ADDR;
|
|
|
+ nbus->tx_buffer[4 * i + 5] = format_ptr[0];
|
|
|
+ nbus->tx_buffer[4 * i + 6] = format_ptr[1];
|
|
|
+ nbus->tx_buffer[4 * i + 7] = format_ptr[2];
|
|
|
+ }
|
|
|
+
|
|
|
+ nbus->tx_length += 4 * sensor_count.read_write_count;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -163,10 +201,15 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
{
|
|
|
switch (nbus->function_code.function)
|
|
|
{
|
|
|
- case CMD_START: {
|
|
|
- nbus->measure_active = MEASURE_RUNNING;
|
|
|
- nbus->interface->start();
|
|
|
- nbus->hw_platform->led_on();
|
|
|
+ case CMD_FIND: {
|
|
|
+ if (nbus->rx_buffer[RX_DT] > 1) // if wrong parameter {0, 1}
|
|
|
+ {
|
|
|
+ setErrorResponse(nbus, ILLEGAL_FUNCTION_PARAM);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ nbus->tx_buffer[TX_DT] = nbus->interface->find(nbus->rx_buffer[RX_DT]);
|
|
|
+ nbus->tx_length += 1;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -177,9 +220,16 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
+ case CMD_START: {
|
|
|
+ nbus->measure_active = MEASURE_RUNNING;
|
|
|
+ nbus->interface->start();
|
|
|
+ nbus->hw_platform->led_on();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
case CMD_PARAM: {
|
|
|
|
|
|
- uint8_t rx_payload = nbus->rx_length - META_SIZE;
|
|
|
+ uint8_t rx_payload = nbus->rx_length - RX_META;
|
|
|
|
|
|
// empty or wrong number of parameters
|
|
|
if (rx_payload == 0 || rx_payload % 5 != 0)
|
|
|
@@ -195,7 +245,7 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
for (uint8_t i = 0; i < rx_payload; i++)
|
|
|
{
|
|
|
param_id = nbus->rx_buffer[3 + 5 * i];
|
|
|
- param_value = *(int32_t *)nbus->rx_buffer[4 + 5 * i];
|
|
|
+ param_value = *(int32_t *)&nbus->rx_buffer[4 + 5 * i];
|
|
|
|
|
|
nbus->tx_buffer[4 + 2 * i] = param_id;
|
|
|
nbus->tx_buffer[5 + 2 * i] = nbus->interface->setParam(0, param_id, param_value);
|
|
|
@@ -219,7 +269,7 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
|
|
|
case CMD_CALIBRATE: {
|
|
|
nbus->hw_platform->led_on();
|
|
|
- nbus->tx_buffer[4] = nbus->interface->calibrate(0);
|
|
|
+ nbus->tx_buffer[TX_DT] = nbus->interface->calibrate(0);
|
|
|
nbus->tx_length += 1;
|
|
|
nbus->hw_platform->led_off();
|
|
|
}
|
|
|
@@ -228,7 +278,7 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
case CMD_DATA: {
|
|
|
|
|
|
nbus->tx_length +=
|
|
|
- nbus->interface->setData(&nbus->rx_buffer[3], nbus->rx_length - META_SIZE, &nbus->tx_buffer[4]);
|
|
|
+ nbus->interface->setData(&nbus->rx_buffer[RX_DT], nbus->rx_length - RX_META, &nbus->tx_buffer[TX_DT]);
|
|
|
}
|
|
|
break;
|
|
|
|