nbus_slave_sensor_unicast.c 4.8 KB

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