nbus_slave_sensor_unicast.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "nbus_slave.h"
  2. #if MODULE_SLAVE == 1
  3. void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus)
  4. {
  5. switch (nbus->function_code.function)
  6. {
  7. case CMD_SENSOR_TYPE: {
  8. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  9. if (t == TYPE_UNKNOWN)
  10. {
  11. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  12. break;
  13. }
  14. nbus->tx_buffer[4] = t;
  15. nbus->tx_length += 1;
  16. }
  17. break;
  18. case CMD_PARAM: {
  19. if (nbus->rx_length >= 5)
  20. {
  21. if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]) == 0)
  22. {
  23. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  24. break;
  25. }
  26. nbus->tx_buffer[4] = nbus->rx_buffer[3];
  27. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
  28. nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
  29. nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
  30. nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
  31. nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
  32. nbus->tx_length += 5;
  33. }
  34. else
  35. {
  36. nBus_param_t *params = nbus_interface_allParams();
  37. for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
  38. {
  39. if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i]))
  40. {
  41. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  42. nbus->tx_buffer[4 + 5 * i] = params[i];
  43. // nbus->tx_buffer[5+2*i] =
  44. // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  45. nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
  46. nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
  47. nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
  48. nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
  49. nbus->tx_length += 5;
  50. }
  51. }
  52. }
  53. }
  54. break;
  55. case CMD_DATA: {
  56. if (nbus->measure_active == MEASURE_RUNNING)
  57. {
  58. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  59. if (cnt == 0)
  60. {
  61. setErrorResponse(nbus, DEVICE_BUSY);
  62. // return;
  63. }
  64. nbus->tx_length += cnt;
  65. }
  66. else
  67. {
  68. setErrorResponse(nbus, DEVICE_NOT_READY);
  69. }
  70. }
  71. break;
  72. case CMD_INFO: {
  73. switch (nbus->rx_buffer[3])
  74. {
  75. case SENSOR_FORMAT: {
  76. nBus_sensorFormat_t format = nbus->interface->getSensorFormat(nbus->sensorInfo.address);
  77. nbus->tx_buffer[4] = nbus->sensorInfo.address;
  78. nbus->tx_buffer[5] = (format.sign << 7) | format.unit_multiplier;
  79. nbus->tx_buffer[6] = format.value_multiplier;
  80. nbus->tx_buffer[7] = (format.byte_length << 4) | format.samples;
  81. nbus->tx_length += 4;
  82. }
  83. }
  84. }
  85. break;
  86. default: {
  87. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  88. }
  89. }
  90. }
  91. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus)
  92. {
  93. int32_t param_value;
  94. switch (nbus->function_code.function)
  95. {
  96. case CMD_PARAM: {
  97. if (!nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]))
  98. {
  99. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  100. break;
  101. }
  102. param_value =
  103. nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  104. nBus_param_t p =
  105. nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3], param_value);
  106. if (p == PARAM_NONE)
  107. {
  108. setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
  109. break;
  110. }
  111. nbus->tx_buffer[4] = OK_CODE;
  112. nbus->tx_length += 1;
  113. }
  114. break;
  115. case CMD_CALIBRATE: {
  116. nbus->hw_platform->led_on();
  117. if (1 == nbus->interface->calibrate(nbus->sensorInfo.address, 0, NULL))
  118. {
  119. nbus->tx_buffer[4] = 1;
  120. nbus->tx_length += 1;
  121. }
  122. else
  123. {
  124. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  125. }
  126. nbus->hw_platform->led_off();
  127. break;
  128. }
  129. case CMD_DATA: {
  130. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  131. if (nbus->tx_buffer[4] != OK_CODE)
  132. {
  133. nbus->function_code.error = 1;
  134. }
  135. nbus->tx_length += 1;
  136. }
  137. break;
  138. case CMD_STORE: {
  139. nBus_param_t *all_params = nbus_interface_allParams();
  140. for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++)
  141. {
  142. if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i]))
  143. {
  144. sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
  145. // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
  146. // all_params[i]);
  147. // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
  148. // all_params[i], param_value);
  149. }
  150. }
  151. nbus->tx_buffer[4] = 1;
  152. nbus->tx_length += 1;
  153. }
  154. break;
  155. default: {
  156. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  157. }
  158. }
  159. }
  160. void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index, uint8_t param_name)
  161. {
  162. uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
  163. nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
  164. }
  165. #endif