nbus_slave_sensor_unicast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "nbus_slave.h"
  2. #if MODULE_SLAVE == 1
  3. void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus)
  4. {
  5. switch (nbus->function_code.function)
  6. {
  7. case CMD_SENSOR_TYPE: {
  8. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  9. if (t == TYPE_UNKNOWN)
  10. {
  11. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  12. break;
  13. }
  14. nbus->tx_buffer[4] = t;
  15. nbus->tx_length += 1;
  16. }
  17. break;
  18. case CMD_PARAM: {
  19. if (nbus->rx_length >= 5)
  20. {
  21. if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]) == 0)
  22. {
  23. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  24. break;
  25. }
  26. nbus->tx_buffer[4] = nbus->rx_buffer[3];
  27. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
  28. nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
  29. nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
  30. nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
  31. nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
  32. nbus->tx_length += 5;
  33. }
  34. else
  35. {
  36. nBus_param_t *params = nbus_interface_allParams();
  37. for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
  38. {
  39. if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i]))
  40. {
  41. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  42. nbus->tx_buffer[4 + 5 * i] = params[i];
  43. // nbus->tx_buffer[5+2*i] =
  44. // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  45. nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
  46. nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
  47. nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
  48. nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
  49. nbus->tx_length += 5;
  50. }
  51. }
  52. }
  53. }
  54. break;
  55. case CMD_DATA: {
  56. if (nbus->measure_active == MEASURE_RUNNING)
  57. {
  58. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  59. if (cnt == 0)
  60. {
  61. setErrorResponse(nbus, DEVICE_BUSY);
  62. // return;
  63. }
  64. nbus->tx_length += cnt;
  65. }
  66. else
  67. {
  68. setErrorResponse(nbus, DEVICE_NOT_READY);
  69. }
  70. }
  71. break;
  72. case CMD_INFO: {
  73. nBus_sensorFormat_t format = nbus->interface->getFormat(nbus->sensorInfo.address);
  74. // const uint8_t *data = (const uint8_t*)&format; doesnt work, missaligned data
  75. uint16_t data = format.sign << 15 | format.variable_length << 14 | format.q_m << 7 | format.q_n;
  76. nbus->tx_buffer[4] = data >> 8;
  77. nbus->tx_buffer[5] = data & 0xFF;
  78. nbus->tx_length += 2;
  79. }
  80. break;
  81. default: {
  82. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  83. }
  84. }
  85. }
  86. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus)
  87. {
  88. int32_t param_value;
  89. switch (nbus->function_code.function)
  90. {
  91. case CMD_PARAM: {
  92. if (!nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]))
  93. {
  94. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  95. break;
  96. }
  97. param_value =
  98. nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  99. nBus_param_t p =
  100. nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3], param_value);
  101. if (p == PARAM_NONE)
  102. {
  103. setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
  104. break;
  105. }
  106. nbus->tx_buffer[4] = OK_CODE;
  107. nbus->tx_length += 1;
  108. }
  109. break;
  110. case CMD_CALIBRATE: {
  111. nbus->hw_platform->led_on();
  112. if (1 == nbus->interface->calibrate(nbus->sensorInfo.address, 0, NULL))
  113. {
  114. nbus->tx_buffer[4] = 1;
  115. nbus->tx_length += 1;
  116. }
  117. else
  118. {
  119. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  120. }
  121. nbus->hw_platform->led_off();
  122. break;
  123. }
  124. case CMD_DATA: {
  125. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  126. if (nbus->tx_buffer[4] != OK_CODE)
  127. {
  128. nbus->function_code.error = 1;
  129. }
  130. nbus->tx_length += 1;
  131. }
  132. break;
  133. case CMD_STORE: {
  134. nBus_param_t *all_params = nbus_interface_allParams();
  135. for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++)
  136. {
  137. if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i]))
  138. {
  139. sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
  140. // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
  141. // all_params[i]);
  142. // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
  143. // all_params[i], param_value);
  144. }
  145. }
  146. nbus->tx_buffer[4] = 1;
  147. nbus->tx_length += 1;
  148. }#include "nbus_slave.h"
  149. #if MODULE_SLAVE == 1
  150. void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus)
  151. {
  152. switch (nbus->function_code.function)
  153. {
  154. case CMD_SENSOR_TYPE: {
  155. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  156. if (t == TYPE_UNKNOWN)
  157. {
  158. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  159. break;
  160. }
  161. nbus->tx_buffer[4] = t;
  162. nbus->tx_length += 1;
  163. }
  164. break;
  165. case CMD_PARAM: {
  166. if (nbus->rx_length >= 5)
  167. {
  168. if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]) == 0)
  169. {
  170. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  171. break;
  172. }
  173. nbus->tx_buffer[4] = nbus->rx_buffer[3];
  174. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
  175. nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
  176. nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
  177. nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
  178. nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
  179. nbus->tx_length += 5;
  180. }
  181. else
  182. {
  183. nBus_param_t *params = nbus_interface_allParams();
  184. for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
  185. {
  186. if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i]))
  187. {
  188. int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  189. nbus->tx_buffer[4 + 5 * i] = params[i];
  190. // nbus->tx_buffer[5+2*i] =
  191. // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
  192. nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
  193. nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
  194. nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
  195. nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
  196. nbus->tx_length += 5;
  197. }
  198. }
  199. }
  200. }
  201. break;
  202. case CMD_DATA: {
  203. if (nbus->measure_active == MEASURE_RUNNING)
  204. {
  205. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  206. if (cnt == 0)
  207. {
  208. setErrorResponse(nbus, DEVICE_BUSY);
  209. // return;
  210. }
  211. nbus->tx_length += cnt;
  212. }
  213. else
  214. {
  215. setErrorResponse(nbus, DEVICE_NOT_READY);
  216. }
  217. }
  218. break;
  219. case CMD_INFO: {
  220. nBus_sensorFormat_t format = nbus->interface->getFormat(nbus->sensorInfo.address);
  221. // const uint8_t *data = (const uint8_t*)&format; doesnt work, missaligned data
  222. uint16_t data = format.sign << 15 | format.variable_length << 14 | format.q_m << 7 | format.q_n;
  223. nbus->tx_buffer[4] = data >> 8;
  224. nbus->tx_buffer[5] = data & 0xFF;
  225. nbus->tx_length += 2;
  226. }
  227. break;
  228. default: {
  229. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  230. }
  231. }
  232. }
  233. void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus)
  234. {
  235. int32_t param_value;
  236. switch (nbus->function_code.function)
  237. {
  238. case CMD_PARAM: {
  239. if (!nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]))
  240. {
  241. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  242. break;
  243. }
  244. param_value =
  245. nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  246. nBus_param_t p =
  247. nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3], param_value);
  248. if (p == PARAM_NONE)
  249. {
  250. setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
  251. break;
  252. }
  253. nbus->tx_buffer[4] = OK_CODE;
  254. nbus->tx_length += 1;
  255. }
  256. break;
  257. case CMD_CALIBRATE: {
  258. nbus->hw_platform->led_on();
  259. if (1 == nbus->interface->calibrate(nbus->sensorInfo.address, 0, NULL))
  260. {
  261. nbus->tx_buffer[4] = 1;
  262. nbus->tx_length += 1;
  263. }
  264. else
  265. {
  266. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  267. }
  268. nbus->hw_platform->led_off();
  269. break;
  270. }
  271. case CMD_DATA: {
  272. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  273. if (nbus->tx_buffer[4] != OK_CODE)
  274. {
  275. nbus->function_code.error = 1;
  276. }
  277. nbus->tx_length += 1;
  278. }
  279. break;
  280. case CMD_STORE: {
  281. nBus_param_t *all_params = nbus_interface_allParams();
  282. for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++)
  283. {
  284. if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i]))
  285. {
  286. sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
  287. // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
  288. // all_params[i]);
  289. // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
  290. // all_params[i], param_value);
  291. }
  292. }
  293. nbus->tx_buffer[4] = 1;
  294. nbus->tx_length += 1;
  295. }
  296. break;
  297. default: {
  298. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  299. }
  300. }
  301. }
  302. void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index, uint8_t param_name)
  303. {
  304. uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
  305. nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
  306. }
  307. #endif
  308. break;
  309. default: {
  310. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  311. }
  312. }
  313. }
  314. void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index, uint8_t param_name)
  315. {
  316. uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
  317. nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
  318. }
  319. #endif