commands_api.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /********************************* (C) РОТЕК ***********************************
  2. * @module commands_api
  3. * @file commands_api.c
  4. * @version 1.0.0
  5. * @date XX.XX.XXXX
  6. * $brief commands_api
  7. *******************************************************************************
  8. * @history Version Author Comment
  9. * XX.XX.XXXX 1.0.0 Telenkov D.A. First release.
  10. *******************************************************************************
  11. */
  12. #include "common_config.h"
  13. #include "stm32f4xx.h"
  14. #include "commands_api.h"
  15. #include "settings_api.h"
  16. #include "sys_api.h"
  17. #include "buttons.h"
  18. #include "led.h"
  19. #include "usart.h"
  20. #include "parameters.h"
  21. #include "bt_6701_settings.h"
  22. #include "bt_6701_commands.h"
  23. #include "netconf.h"
  24. #include "stm32f4x7_eth.h"
  25. #include "http_server.h"
  26. #include "config_service.h"
  27. #include "rtc.h"
  28. #include "port_microrl.h"
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #include "lwip/opt.h"
  32. #include "lwip/api.h"
  33. #include "lwip/sys.h"
  34. #ifdef PRINTF_STDLIB
  35. #include <stdio.h>
  36. #endif
  37. #ifdef PRINTF_CUSTOM
  38. #include "tinystdio.h"
  39. #endif
  40. #define UDP_PORT 49049
  41. TESTING_STATE_t TESTING_STATE = TEST_IDLE;
  42. static TEST_t testState = TEST_WAIT;
  43. bool ifTest_DEF = false;
  44. bool ifTest_SET = false;
  45. bool macWaiting = false;
  46. bool macIsInstalled = false;
  47. bool fUpdatable = false;
  48. bool fDefPressed = false;
  49. bool testEthernet = false;
  50. bool testRtc = false;
  51. bool testSerno = false;
  52. bool testT2Ready = false;
  53. bool testSet = false;
  54. bool testRS485 = false;
  55. #define MSG_LEN 300
  56. char msg[MSG_LEN];
  57. extern char STM_ID[33];
  58. extern char STM_ID_HEX[12];
  59. uint8_t MAC[6];
  60. /**
  61. * @brief Общая структура настроек
  62. */
  63. extern SETTINGS_t sSettings;
  64. /**
  65. * @brief
  66. */
  67. void vTestCommands(void *params)
  68. {
  69. for (;;)
  70. {
  71. if (fUpdatable) {
  72. if(RTC_GetUnixTime() <= 946685800)
  73. {
  74. memset(SETTINGS_GetTestState(), 0, 16);
  75. SETTINGS_SetTest(SETTINGS_GetTestState());
  76. #ifdef LED_ALARM
  77. LED_On(LED_ALARM);
  78. #endif
  79. //xTaskCreate(vTaskServiceLedBlinkErr, "LED_Blink_Err", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  80. xTaskCreate(vTaskServiceLedBlink, "LED_Blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  81. while(1){
  82. vTaskDelay(10);
  83. }
  84. }
  85. //COM_TestEthernet();
  86. SETTINGS_SetInfoDef();
  87. Test_Ethernet();
  88. vTaskDelay(8000);
  89. /* Стартует таск для опроса кнопки DEF */
  90. //xTaskCreate(vBootloaderButton, "DEF_Bootloader", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  91. xTaskCreate(vTaskButtons, "DEF_Waiting", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  92. xTaskCreate(vTaskServiceLedBlink, "LED_Blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  93. while (!fDefPressed) {
  94. vTaskDelay(10);
  95. }
  96. #ifdef LED_INIT_ERR
  97. LED_On(LED_INIT_ERR);
  98. #endif
  99. #ifdef LED_ALARM
  100. LED_On(LED_ALARM);
  101. #endif
  102. #ifdef LED_RED_MINOR
  103. LED_On(LED_RED_MINOR);
  104. #endif
  105. //sSettings.bootParams.loadMode = 1;
  106. SetLoadMode();
  107. COM_TestSerno();
  108. COM_SetTestState(T2READY);
  109. vTaskDelay(1000);
  110. NVIC_SystemReset();
  111. while (1) {};
  112. }
  113. switch (testState)
  114. {
  115. case TEST_WAIT :
  116. break;
  117. case TEST_U232 :
  118. Test_U232();
  119. break;
  120. #ifdef DALLAS_SENSOR_ENABLE
  121. case TEST_OW :
  122. COM_TestOW();
  123. break;
  124. #endif
  125. #ifdef DINS_ENABLE || DOUTS_ENABLE
  126. case TEST_DRY :
  127. Test_DRY();
  128. break;
  129. #endif
  130. case TEST_DEF :
  131. if (!ifTest_DEF) {
  132. ifTest_DEF = true;
  133. xTaskCreate(vTest_DEF, "DEF_Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  134. }
  135. break;
  136. case TEST_SET :
  137. if (!ifTest_SET) {
  138. ifTest_SET = true;
  139. xTaskCreate(vTest_SET, "SET_Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  140. }
  141. break;
  142. case TEST_RTC :
  143. COM_TestRtc();
  144. break;
  145. case TEST_SPIFLASH :
  146. COM_TestSPIflash();
  147. break;
  148. case TEST_ETHERNET :
  149. COM_SetMAC();
  150. Test_Ethernet();
  151. break;
  152. case TEST_SERNO :
  153. Test_Serno();
  154. break;
  155. default :
  156. break;
  157. }
  158. vTaskDelay(200);
  159. //TestProcessing();
  160. }
  161. }
  162. /**
  163. * @brief
  164. */
  165. void TEST_SetTest(TEST_t state)
  166. {
  167. // Изменяем стейт только в том случае если контроллер находится в режиме ожидания
  168. if (testState == TEST_WAIT)
  169. testState = state;
  170. // На TEST_WAIT можно изменить всегда
  171. if (state == TEST_WAIT)
  172. testState = state;
  173. }
  174. #ifdef PORTGW_ENABLE
  175. /**
  176. * @brief Отправка данных по интерфейсу RS485
  177. */
  178. void COM_TestRS485(char *str, uint8_t len)
  179. {
  180. if (str) {
  181. for (uint8_t i = 0; i < len; i ++) {
  182. str[i] = ~str[i];
  183. }
  184. uint8_t sent = rs485_send_block((uint8_t *)str, len);
  185. sprintf(msg, "RS485 sent %d bytes", sent);
  186. print(msg);
  187. }
  188. print("\r\n");
  189. testRS485 = false;
  190. return;
  191. }
  192. #endif
  193. /**
  194. * @brief Считать статут тестирования (T0, T1, etc)
  195. */
  196. void COM_ReadTestState(void)
  197. {
  198. if (strncmp(SETTINGS_GetTestState(), "T0READY", 5) == 0) {
  199. TESTING_STATE = T0READY;
  200. printf("T0READY\r\n");
  201. }
  202. else if (strncmp(SETTINGS_GetTestState(), "T0OK", 5) == 0)
  203. TESTING_STATE = T0OK;
  204. else if (strncmp(SETTINGS_GetTestState(), "T1OK", 5) == 0) {
  205. TESTING_STATE = T1OK;
  206. // TODO Если осуществлять поддержку старых версий UCN нужно учесть этот флаг
  207. fUpdatable = true;
  208. }
  209. else if (strncmp(SETTINGS_GetTestState(), "T2READY", 8) == 0) {
  210. TESTING_STATE = T2READY;
  211. }
  212. else
  213. TESTING_STATE = TEST_IDLE;
  214. }
  215. /**
  216. * @brief Сохранение Serno и отправка сообщения на сервер
  217. */
  218. void COM_TestSerno(void)
  219. {
  220. struct netconn *conn;
  221. struct netbuf *buf;
  222. char *data;
  223. err_t err;
  224. char str[20] = {0};
  225. uint8_t len;
  226. /* Отправляем сообщение на сервер по UDP */
  227. memset(msg, 0, MSG_LEN);
  228. conn = netconn_new( NETCONN_UDP );
  229. if (conn!= NULL)
  230. {
  231. err = netconn_bind(conn, IP_ADDR_ANY, UDP_PORT);
  232. if (err == ERR_OK)
  233. {
  234. netconn_connect(conn, IP_ADDR_BROADCAST, UDP_PORT);
  235. memset(str, 0, 20);
  236. GetModelStr(str, &len);
  237. //strcpy(msg, sSettings.sSnmp.sysName);
  238. strcpy(msg, str);
  239. strcat(msg, ";");
  240. memset(str, 0, 20);
  241. GetSerialNumberStr(str, &len);
  242. //strcat(msg, sSettings.sInfo.serialNumber);
  243. strcat(msg, str);
  244. strcat(msg, ";");
  245. /* empty column (format reqs)*/
  246. strcat(msg, ";");
  247. strcat(msg, VERSION_TEST);
  248. strcat(msg, ";");
  249. /* empty column (format reqs)*/
  250. strcat(msg, ";");
  251. /* empty column (format reqs)*/
  252. strcat(msg, ";");
  253. strcat(msg, STM_ID);
  254. strcat(msg, ";");
  255. /* empty column (format reqs)*/
  256. strcat(msg, ";");
  257. /* empty column (format reqs)*/
  258. strcat(msg, ";");
  259. if (TESTING_STATE == T0OK)
  260. strcat(msg, "T0OK");
  261. else if (TESTING_STATE == T1OK)
  262. strcat(msg, "T1OK");
  263. else if (TESTING_STATE == T0READY)
  264. strcat(msg, "T0READY");
  265. else if (TESTING_STATE == T2READY)
  266. strcat(msg, "T2READY");
  267. strcat(msg, ";");
  268. /* empty column (format reqs)*/
  269. strcat(msg, ";");
  270. buf = netbuf_new();
  271. data = netbuf_alloc(buf, strlen(msg));
  272. memcpy(data, msg, strlen(msg));
  273. netconn_send(conn, buf);
  274. netbuf_delete(buf);
  275. }
  276. else
  277. netconn_delete(conn);
  278. }
  279. }
  280. /**
  281. * @brief Устанавливает mac адрес исходя из unique id
  282. */
  283. void COM_SetMAC(void)
  284. {
  285. uint8_t octet1 = 0;
  286. uint8_t octet2 = 0;
  287. uint8_t octet3 = 0;
  288. for (uint8_t i = 0; i < 4; i++)
  289. {
  290. octet1 ^= STM_ID_HEX[i];
  291. octet2 ^= STM_ID_HEX[i + 4];
  292. octet3 ^= STM_ID_HEX[i + 8];
  293. }
  294. /* Восьмой бит первого октета должен быть равен 0 */
  295. /* MAC[0] = octet1 & 0xFE;
  296. MAC[1] = octet2;
  297. MAC[2] = octet3;
  298. MAC[3] = 0x00;
  299. MAC[4] = 0x00;
  300. MAC[5] = 0x00;*/
  301. /* Устанавливаем MAC, но не сохраняем */
  302. snprintf(sSettings.sInfo.mac, MAC_LEN, "%02X-%02X-%02X-00-00-00", octet1 & 0xFE, octet2, octet3);
  303. }
  304. /**
  305. * @brief Установить статут тестирования (T0, T1, etc)
  306. */
  307. void COM_SetTestState(TESTING_STATE_t status)
  308. {
  309. switch (status)
  310. {
  311. case T0READY :
  312. //memset(sSettings.sFlags.testState, 0, 16);
  313. //strncpy(sSettings.sFlags.testState, "T0READY", 7);
  314. memset(SETTINGS_GetTestState(), 0, 16);
  315. strncpy(SETTINGS_GetTestState(), "T0READY", 7);
  316. TESTING_STATE = T0READY;
  317. break;
  318. case T0OK :
  319. //memset(sSettings.sFlags.testState, 0, 16);
  320. //strncpy(sSettings.sFlags.testState, "T0OK", 4);
  321. memset(SETTINGS_GetTestState(), 0, 16);
  322. strncpy(SETTINGS_GetTestState(), "T0OK", 4);
  323. TESTING_STATE = T0OK;
  324. break;
  325. case T1OK :
  326. //memset(sSettings.sFlags.testState, 0, 16);
  327. //strncpy(sSettings.sFlags.testState, "T1OK", 4);
  328. memset(SETTINGS_GetTestState(), 0, 16);
  329. strncpy(SETTINGS_GetTestState(), "T1OK", 4);
  330. TESTING_STATE = T1OK;
  331. break;
  332. case T2READY :
  333. //memset(sSettings.sFlags.testState, 0, 16);
  334. //strncpy(sSettings.sFlags.testState, "T2READY", 7);
  335. memset(SETTINGS_GetTestState(), 0, 16);
  336. strncpy(SETTINGS_GetTestState(), "T2READY", 7);
  337. TESTING_STATE = T2READY;
  338. break;
  339. }
  340. SETTINGS_SetTest(SETTINGS_GetTestState());
  341. }
  342. /********************************* (C) РОТЕК **********************************/