nbus_impl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. nbus->tx_buffer[4] = nbus->interface->getParam(nbus->rx_buffer[3], (nBus_param_t)nbus->tx_buffer[4]);
  40. nbus->tx_length += 1;
  41. }
  42. }
  43. }
  44. break;
  45. case CMD_DATA:
  46. {
  47. uint8_t cnt = nbus->interface->getData(&nbus->tx_buffer[4]);
  48. nbus->tx_length += cnt;
  49. }
  50. break;
  51. default:
  52. {
  53. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  54. }
  55. }
  56. }
  57. void nbus_unicastToSensorSet(nBus_TypeDef *nbus){
  58. switch(nbus->function_code.function){
  59. case CMD_PARAM:
  60. {
  61. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  62. if (p == PARAM_NONE) {
  63. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  64. break;
  65. }
  66. nbus->tx_buffer[4] = OK_CODE;
  67. nbus->tx_length += 1;
  68. }
  69. break;
  70. case CMD_DATA:
  71. {
  72. nbus->interface->setData(&nbus->rx_buffer[3]);
  73. nbus->tx_buffer[4] = OK_CODE;
  74. nbus->tx_length += 1;
  75. }
  76. break;
  77. default:
  78. {
  79. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  80. }
  81. }
  82. }
  83. void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
  84. switch(nbus->function_code.function){
  85. case CMD_VERSION:
  86. {
  87. nbus->tx_buffer[4] = VERSION_MAJOR;
  88. nbus->tx_buffer[5] = '.';
  89. nbus->tx_buffer[6] = VERSION_MINOR;
  90. nbus->tx_length += 3;
  91. }
  92. break;
  93. case CMD_ECHO:
  94. {
  95. for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
  96. nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
  97. }
  98. nbus->tx_length += (nbus->rx_length-4);
  99. }
  100. break;
  101. case CMD_SENSOR_CNT:
  102. {
  103. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  104. nbus->tx_length += 1;
  105. }
  106. break;
  107. case SENSOR_TYPE:
  108. {
  109. //response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ...
  110. for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){
  111. nbus->tx_buffer[4+2*i] = i;
  112. nbus->tx_buffer[5+2*i] = nbus->interface->getType(i);
  113. nbus->tx_length += 2;
  114. }
  115. }
  116. break;
  117. default:
  118. {
  119. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  120. }
  121. }
  122. }
  123. void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
  124. switch(nbus->function_code.function){
  125. case CMD_PARAM:
  126. {
  127. //same as nbus_unicastToSensorSet
  128. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  129. if (p == PARAM_NONE) {
  130. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  131. break;
  132. }
  133. nbus->tx_buffer[4] = OK_CODE;
  134. nbus->tx_length += 1;
  135. }
  136. break;
  137. case CMD_DATA:
  138. {
  139. nbus->interface->setData(&nbus->rx_buffer[3]);
  140. nbus->tx_buffer[4] = OK_CODE;
  141. nbus->tx_length += 1;
  142. }
  143. break;
  144. case CMD_SLEEP:
  145. {
  146. nbus->tx_buffer[4] = OK_CODE;
  147. nbus->tx_length += 1;
  148. }
  149. break;
  150. case CMD_WAKEUP:
  151. {
  152. nbus->tx_buffer[4] = OK_CODE;
  153. nbus->tx_length += 1;
  154. }
  155. break;
  156. default:
  157. {
  158. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  159. }
  160. }
  161. }
  162. void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
  163. if(request_type == BROADCAST_SPECIFIC_SENSORS) {
  164. }
  165. if(request_type == BROADCAST_GLOBAL) {
  166. switch(nbus->function_code.function) {
  167. #if MODULE_MASTER
  168. case CMD_SYNC:
  169. {
  170. if(nbus->rx_length < 11){
  171. return;
  172. }
  173. RTC_TimeTypeDef sTime = {0};
  174. RTC_DateTypeDef sDate = {0};
  175. sTime.Hours = nbus->rx_buffer[7];
  176. sTime.Minutes = nbus->rx_buffer[8];
  177. sTime.Seconds = nbus->rx_buffer[9];
  178. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  179. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  180. HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
  181. sDate.WeekDay = nbus->rx_buffer[6];
  182. sDate.Date = nbus->rx_buffer[5];
  183. sDate.Month = nbus->rx_buffer[4];
  184. sDate.Year = nbus->rx_buffer[3];
  185. HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
  186. }
  187. break;
  188. #endif
  189. case CMD_START:
  190. {
  191. nbus->measure_active = MEASURE_RUNNING;
  192. }
  193. break;
  194. case CMD_STOP:
  195. {
  196. nbus->measure_active = MEASURE_STOPPED;
  197. }
  198. break;
  199. default:
  200. ; // nothing
  201. }
  202. }
  203. nbus->send_response = NO_RESPONSE;
  204. }