nbus_impl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "nbus_impl.h"
  2. #include "app_bridge.h"
  3. inline static void setErrorResponse(nBus_TypeDef *nbus, uint8_t code){
  4. nbus->function_code.error = 1;
  5. nbus->tx_buffer[4] = code;
  6. nbus->tx_length += 1;
  7. }
  8. void nbus_unicastToSensorGet(nBus_TypeDef *nbus){
  9. switch(nbus->function_code.function){
  10. case SENSOR_TYPE:
  11. {
  12. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  13. if (t == TYPE_UNKNOWN) {
  14. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  15. break;
  16. }
  17. nbus->tx_buffer[4] = t;
  18. nbus->tx_length += 1;
  19. }
  20. break;
  21. case CMD_PARAM:
  22. {
  23. if (nbus->rx_length >= 5){
  24. if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]) == 0) {
  25. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  26. break;
  27. }
  28. nbus->tx_buffer[4] = nbus->tx_buffer[3];
  29. nbus->tx_buffer[5] = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]);
  30. nbus->tx_length += 2;
  31. } else {
  32. nBus_param_t* params = nbus_interface_allParams();
  33. for(uint8_t i=0; i < nbus_interface_allParamsCount() ; i++){
  34. if(nbus->interface->hasParam(i, params[i])){
  35. nbus->tx_buffer[4+2*i] = params[i];
  36. nbus->tx_buffer[5+2*i] = nbus->interface->getParam(i, params[i]);
  37. nbus->tx_length += 2;
  38. }
  39. }
  40. }
  41. }
  42. break;
  43. case CMD_DATA:
  44. {
  45. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  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. }
  81. void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
  82. switch(nbus->function_code.function){
  83. case CMD_VERSION:
  84. {
  85. nbus->tx_buffer[4] = VERSION_MAJOR;
  86. nbus->tx_buffer[5] = '.';
  87. nbus->tx_buffer[6] = VERSION_MINOR;
  88. nbus->tx_length += 3;
  89. }
  90. break;
  91. case CMD_ECHO:
  92. {
  93. for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
  94. nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
  95. }
  96. nbus->tx_length += (nbus->rx_length-4);
  97. }
  98. break;
  99. case CMD_SENSOR_CNT:
  100. {
  101. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  102. nbus->tx_length += 1;
  103. }
  104. break;
  105. case SENSOR_TYPE:
  106. {
  107. //response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ...
  108. for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){
  109. nbus->tx_buffer[4+2*i] = i;
  110. nbus->tx_buffer[5+2*i] = nbus->interface->getType(i);
  111. nbus->tx_length += 2;
  112. }
  113. }
  114. break;
  115. default:
  116. {
  117. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  118. }
  119. }
  120. }
  121. void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
  122. switch(nbus->function_code.function){
  123. case CMD_PARAM:
  124. {
  125. //same as nbus_unicastToSensorSet
  126. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  127. if (p == PARAM_NONE) {
  128. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  129. break;
  130. }
  131. nbus->tx_buffer[4] = OK_CODE;
  132. nbus->tx_length += 1;
  133. }
  134. break;
  135. case CMD_DATA:
  136. {
  137. nbus->interface->setData(&nbus->rx_buffer[3]);
  138. nbus->tx_buffer[4] = OK_CODE;
  139. nbus->tx_length += 1;
  140. }
  141. break;
  142. case CMD_SLEEP:
  143. {
  144. nbus->tx_buffer[4] = OK_CODE;
  145. nbus->tx_length += 1;
  146. }
  147. break;
  148. case CMD_WAKEUP:
  149. {
  150. nbus->tx_buffer[4] = OK_CODE;
  151. nbus->tx_length += 1;
  152. }
  153. break;
  154. default:
  155. {
  156. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  157. }
  158. }
  159. }
  160. void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
  161. if(request_type == BROADCAST_SPECIFIC_SENSORS) {
  162. }
  163. if(request_type == BROADCAST_GLOBAL) {
  164. switch(nbus->function_code.function) {
  165. #if MODULE_MASTER
  166. case CMD_SYNC:
  167. {
  168. if(nbus->rx_length < 11){
  169. return;
  170. }
  171. RTC_TimeTypeDef sTime = {0};
  172. RTC_DateTypeDef sDate = {0};
  173. sTime.Hours = nbus->rx_buffer[7];
  174. sTime.Minutes = nbus->rx_buffer[8];
  175. sTime.Seconds = nbus->rx_buffer[9];
  176. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  177. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  178. HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
  179. sDate.WeekDay = nbus->rx_buffer[6];
  180. sDate.Date = nbus->rx_buffer[5];
  181. sDate.Month = nbus->rx_buffer[4];
  182. sDate.Year = nbus->rx_buffer[3];
  183. HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
  184. }
  185. break;
  186. #endif
  187. case CMD_START:
  188. {
  189. nbus->measure_active = MEASURE_RUNNING;
  190. }
  191. break;
  192. case CMD_STOP:
  193. {
  194. nbus->measure_active = MEASURE_STOPPED;
  195. }
  196. break;
  197. default:
  198. ; // nothing
  199. }
  200. }
  201. nbus->send_response = NO_RESPONSE;
  202. }