nbus_slave_sensor_unicast.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "nbus_slave.h"
  2. #if MODULE_SLAVE == 1
  3. void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus) {
  4. switch (nbus->function_code.function) {
  5. case CMD_SENSOR_TYPE: {
  6. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  7. if (t == TYPE_UNKNOWN) {
  8. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  9. break;
  10. }
  11. nbus->tx_buffer[4] = t;
  12. nbus->tx_length += 1;
  13. } break;
  14. case CMD_PARAM: {
  15. if (nbus->rx_length >= 5) {
  16. if (nbus->interface->hasParam(nbus->sensorInfo.address,
  17. (nBus_param_t)nbus->rx_buffer[3]) == 0) {
  18. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  19. break;
  20. }
  21. nbus->tx_buffer[4] = nbus->rx_buffer[3];
  22. int32_t param_value = nbus->interface->getParam(
  23. nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
  24. nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
  25. nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
  26. nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
  27. nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
  28. nbus->tx_length += 5;
  29. } else {
  30. nBus_param_t *params = nbus_interface_allParams();
  31. for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++) {
  32. if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i])) {
  33. int32_t param_value =
  34. nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  35. nbus->tx_buffer[4 + 5 * i] = params[i];
  36. // nbus->tx_buffer[5+2*i] =
  37. // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  38. nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
  39. nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
  40. nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
  41. nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
  42. nbus->tx_length += 5;
  43. }
  44. }
  45. }
  46. } break;
  47. case CMD_DATA: {
  48. if (nbus->measure_active == MEASURE_RUNNING) {
  49. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address,
  50. &nbus->tx_buffer[4]);
  51. if (cnt == 0) {
  52. setErrorResponse(nbus, DEVICE_BUSY);
  53. // return;
  54. }
  55. nbus->tx_length += cnt;
  56. } else {
  57. setErrorResponse(nbus, DEVICE_NOT_READY);
  58. }
  59. } break;
  60. default: {
  61. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  62. }
  63. }
  64. }
  65. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus) {
  66. switch (nbus->function_code.function) {
  67. case CMD_PARAM: {
  68. if (!nbus->interface->hasParam(nbus->sensorInfo.address,
  69. (nBus_param_t)nbus->rx_buffer[3])) {
  70. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  71. break;
  72. }
  73. int32_t param_value = nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 |
  74. nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  75. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address,
  76. (nBus_param_t)nbus->rx_buffer[3],
  77. param_value);
  78. if (p == PARAM_NONE) {
  79. setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
  80. break;
  81. }
  82. nbus->tx_buffer[4] = OK_CODE;
  83. nbus->tx_length += 1;
  84. } break;
  85. case CMD_DATA: {
  86. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  87. if (nbus->tx_buffer[4] != OK_CODE) {
  88. nbus->function_code.error = 1;
  89. }
  90. nbus->tx_length += 1;
  91. } break;
  92. default: {
  93. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  94. }
  95. }
  96. }
  97. #endif