nbus_slave_sensor_unicast.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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->tx_buffer[3]) == 0) {
  20. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  21. break;
  22. }
  23. nbus->tx_buffer[4] = nbus->tx_buffer[3];
  24. nbus->tx_buffer[5] = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]);
  25. nbus->tx_length += 2;
  26. } else {
  27. nBus_param_t* params = nbus_interface_allParams();
  28. for(uint8_t i=0; i < nbus_interface_allParamsCount() ; i++){
  29. if(nbus->interface->hasParam(i, params[i])){
  30. nbus->tx_buffer[4+2*i] = params[i];
  31. nbus->tx_buffer[5+2*i] = nbus->interface->getParam(i, params[i]);
  32. nbus->tx_length += 2;
  33. }
  34. }
  35. }
  36. // alebo
  37. nbus->memoryInterface->getParam(nbus->sensorInfo.address, 0x05);
  38. }
  39. break;
  40. case CMD_DATA:
  41. {
  42. if (nbus->measure_active == MEASURE_RUNNING) {
  43. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  44. if (cnt == 0){
  45. setErrorResponse(nbus, DEVICE_BUSY);
  46. //return;
  47. }
  48. nbus->tx_length += cnt;
  49. } else {
  50. setErrorResponse(nbus, DEVICE_NOT_READY);
  51. }
  52. }
  53. break;
  54. default:
  55. {
  56. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  57. }
  58. }
  59. }
  60. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus){
  61. switch(nbus->function_code.function){
  62. case CMD_PARAM:
  63. {
  64. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  65. if (p == PARAM_NONE) {
  66. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  67. break;
  68. }
  69. nbus->tx_buffer[4] = OK_CODE;
  70. nbus->tx_length += 1;
  71. }
  72. break;
  73. case CMD_DATA:
  74. {
  75. nbus->interface->setData(&nbus->rx_buffer[3]);
  76. nbus->tx_buffer[4] = OK_CODE;
  77. nbus->tx_length += 1;
  78. }
  79. break;
  80. default:
  81. {
  82. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  83. }
  84. }
  85. }
  86. #endif