nbus_slave_sensor_unicast.c 3.1 KB

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