nbus_slave_sensor_unicast.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  43. if (cnt == 0){
  44. setErrorResponse(nbus, DEVICE_BUSY);
  45. return;
  46. }
  47. nbus->tx_length += cnt;
  48. }
  49. break;
  50. default:
  51. {
  52. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  53. }
  54. }
  55. }
  56. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus){
  57. switch(nbus->function_code.function){
  58. case CMD_PARAM:
  59. {
  60. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  61. if (p == PARAM_NONE) {
  62. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  63. break;
  64. }
  65. nbus->tx_buffer[4] = OK_CODE;
  66. nbus->tx_length += 1;
  67. }
  68. break;
  69. case CMD_DATA:
  70. {
  71. nbus->interface->setData(&nbus->rx_buffer[3]);
  72. nbus->tx_buffer[4] = OK_CODE;
  73. nbus->tx_length += 1;
  74. }
  75. break;
  76. default:
  77. {
  78. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  79. }
  80. }
  81. }
  82. #endif