nbus_impl_sensor_unicast.c 2.0 KB

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