nbus_impl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 CMD_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_ECHO:
  84. {
  85. for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
  86. nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
  87. }
  88. nbus->tx_length += (nbus->rx_length-4);
  89. }
  90. break;
  91. case CMD_SENSOR_CNT:
  92. {
  93. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  94. nbus->tx_length += 1;
  95. }
  96. break;
  97. case CMD_SENSOR_TYPE:
  98. {
  99. //response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ...
  100. for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){
  101. nbus->tx_buffer[4+2*i] = i;
  102. nbus->tx_buffer[5+2*i] = nbus->interface->getType(i);
  103. nbus->tx_length += 2;
  104. }
  105. }
  106. break;
  107. case CMD_INFO:
  108. {
  109. switch(nbus->rx_buffer[3]){
  110. case INFO_MODULE_NAME:
  111. nbus->tx_buffer[4] = MODULE_NAME[0];
  112. nbus->tx_buffer[5] = MODULE_NAME[1];
  113. nbus->tx_buffer[6] = MODULE_NAME[2];
  114. nbus->tx_buffer[7] = MODULE_NAME[3];
  115. nbus->tx_buffer[8] = MODULE_NAME[4];
  116. nbus->tx_buffer[9] = MODULE_NAME[5];
  117. nbus->tx_buffer[10] = MODULE_NAME[6];
  118. nbus->tx_buffer[11] = MODULE_NAME[7];
  119. nbus->tx_length += 8;
  120. break;
  121. case INFO_MODULE_TYPE:
  122. nbus->tx_buffer[4] = MODULE_TYPE[0];
  123. nbus->tx_buffer[5] = MODULE_TYPE[1];
  124. nbus->tx_buffer[6] = MODULE_TYPE[2];
  125. nbus->tx_length += 3;
  126. break;
  127. case INFO_MODULE_UUID:
  128. // Reference manual: Unique device ID registers
  129. uint32_t (*unique_id_3) = (uint32_t*)(0x1FF80064); // BASE address + 0x14 0ffset
  130. *(nbus->tx_buffer) = (uint32_t)unique_id_3;
  131. nbus->tx_length += 4;
  132. break;
  133. case INFO_MODULE_FW:
  134. nbus->tx_buffer[4] = VERSION_FW[0];
  135. nbus->tx_buffer[5] = '.';
  136. nbus->tx_buffer[6] = VERSION_FW[1];
  137. nbus->tx_length += 3;
  138. break;
  139. case INFO_MODULE_HW:
  140. nbus->tx_buffer[4] = VERSION_HW[0];
  141. nbus->tx_buffer[5] = '.';
  142. nbus->tx_buffer[6] = VERSION_HW[1];
  143. nbus->tx_length += 3;
  144. break;
  145. }
  146. }
  147. break;
  148. default:
  149. {
  150. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  151. }
  152. }
  153. }
  154. void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
  155. switch(nbus->function_code.function){
  156. case CMD_PARAM:
  157. {
  158. //same as nbus_unicastToSensorSet
  159. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  160. if (p == PARAM_NONE) {
  161. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  162. break;
  163. }
  164. nbus->tx_buffer[4] = OK_CODE;
  165. nbus->tx_length += 1;
  166. }
  167. break;
  168. case CMD_DATA:
  169. {
  170. nbus->interface->setData(&nbus->rx_buffer[3]);
  171. nbus->tx_buffer[4] = OK_CODE;
  172. nbus->tx_length += 1;
  173. }
  174. break;
  175. case CMD_SLEEP:
  176. {
  177. nbus->tx_buffer[4] = OK_CODE;
  178. nbus->tx_length += 1;
  179. }
  180. break;
  181. case CMD_WAKEUP:
  182. {
  183. nbus->tx_buffer[4] = OK_CODE;
  184. nbus->tx_length += 1;
  185. }
  186. break;
  187. default:
  188. {
  189. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  190. }
  191. }
  192. }
  193. void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
  194. if(request_type == BROADCAST_SPECIFIC_SENSORS) {
  195. }
  196. if(request_type == BROADCAST_GLOBAL) {
  197. switch(nbus->function_code.function) {
  198. #if MODULE_MASTER
  199. case CMD_SYNC:
  200. {
  201. if(nbus->rx_length < 11){
  202. return;
  203. }
  204. RTC_TimeTypeDef sTime = {0};
  205. RTC_DateTypeDef sDate = {0};
  206. sTime.Hours = nbus->rx_buffer[7];
  207. sTime.Minutes = nbus->rx_buffer[8];
  208. sTime.Seconds = nbus->rx_buffer[9];
  209. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  210. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  211. HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
  212. sDate.WeekDay = nbus->rx_buffer[6];
  213. sDate.Date = nbus->rx_buffer[5];
  214. sDate.Month = nbus->rx_buffer[4];
  215. sDate.Year = nbus->rx_buffer[3];
  216. HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
  217. }
  218. break;
  219. #endif
  220. case CMD_START:
  221. {
  222. nbus->measure_active = MEASURE_RUNNING;
  223. }
  224. break;
  225. case CMD_STOP:
  226. {
  227. nbus->measure_active = MEASURE_STOPPED;
  228. }
  229. break;
  230. default:
  231. ; // nothing
  232. }
  233. }
  234. nbus->send_response = NO_RESPONSE;
  235. }