nbus_slave_sensor_unicast.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int32_t param_value;
  67. switch (nbus->function_code.function) {
  68. case CMD_PARAM: {
  69. if (!nbus->interface->hasParam(nbus->sensorInfo.address,
  70. (nBus_param_t)nbus->rx_buffer[3])) {
  71. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  72. break;
  73. }
  74. param_value = nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 |
  75. nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  76. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address,
  77. (nBus_param_t)nbus->rx_buffer[3],
  78. param_value);
  79. if (p == PARAM_NONE) {
  80. setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
  81. break;
  82. }
  83. nbus->tx_buffer[4] = OK_CODE;
  84. nbus->tx_length += 1;
  85. } break;
  86. case CMD_DATA: {
  87. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  88. if (nbus->tx_buffer[4] != OK_CODE) {
  89. nbus->function_code.error = 1;
  90. }
  91. nbus->tx_length += 1;
  92. } break;
  93. case CMD_STORE: {
  94. nBus_param_t *all_params = nbus_interface_allParams();
  95. for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++) {
  96. if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i])) {
  97. sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
  98. // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
  99. // all_params[i]);
  100. // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
  101. // all_params[i], param_value);
  102. }
  103. }
  104. nbus->tx_buffer[4] = 1;
  105. nbus->tx_length += 1;
  106. } break;
  107. default: {
  108. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  109. }
  110. }
  111. }
  112. void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index,
  113. uint8_t param_name) {
  114. uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
  115. nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
  116. }
  117. #endif