mcu_ctrl_board.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "mcu_ctrl_board.h"
  2. #include "../../rtu_linux_modules/swapi/subjects/serial/serial.h"
  3. #include "lwjson/lwjson.h"
  4. static const char MODULE_NAME[] = "MCUCtrlBoard";
  5. // 串口接收空闲超时毫秒
  6. #define RECV_IDLE_TIMEOUT 100
  7. // 判断是否为可打印字符
  8. #define xisprint(c) ((c) >= 0x20 && (c) <= 0x7E)
  9. // 定义串口数据接收模式
  10. typedef enum
  11. {
  12. IDLE = 0,
  13. JSON = 1,
  14. TEXT = 2
  15. } ERecvMode;
  16. // 定义TEXT数据接收缓存
  17. typedef struct
  18. {
  19. unsigned char buf[1024];
  20. int len;
  21. bool overflow; // 缓存是否溢出
  22. } STextCache;
  23. // 与单片机通讯的结构体
  24. typedef struct
  25. {
  26. void *h;
  27. ERecvMode recvMode;
  28. lwjson_stream_parser_t parser;
  29. unsigned long jsonLastCharTick; // JSON数据包最后接收数据时的单调计时, 用于超时判断
  30. unsigned long textLastCharTick; // TEXT数据包最后接收数据时的单调计时, 用于超时判断
  31. STextCache textCache;
  32. } SMCBCom;
  33. static SMCBCom s_myCom = { 0 };
  34. // 声明Go中处理串口请求并返回应答结果的函数(供C调用)
  35. extern char *go_handle_serial_json_msg(const char *jsonStr);
  36. extern char *go_handle_serial_text_msg(const char *textStr);
  37. // 消费一次来自MCU控制板的文本报文帧, (串口数据处理)
  38. static void consume_cached_text_data(const void *pSerial/*打开的串口句柄*/, STextCache *pTextCache /*缓存地址*/)
  39. {
  40. const char *log_prefix = serial_get_log_prefix(pSerial);
  41. if(pTextCache->overflow)
  42. {
  43. sw_log_warn("[%s] %s discarded an oversized text message(%d bytes)!!", MODULE_NAME, log_prefix, pTextCache->len);
  44. memset(pTextCache, 0, sizeof(STextCache));
  45. return;
  46. }
  47. sw_log_debug("[%s] %s received a text(%d bytes) message: %s", MODULE_NAME, log_prefix, pTextCache->len, pTextCache->buf);
  48. char *pResult = go_handle_serial_text_msg((const char *)pTextCache->buf); // 处理TEXT消息
  49. if(pResult)
  50. {
  51. int nResultBytes = strlen(pResult);
  52. int sendBytes = serial_send_data(pSerial, (const unsigned char *)pResult, nResultBytes);
  53. if(sendBytes == nResultBytes) sw_log_debug("[%s] %s sent a reply(%d bytes): %s", MODULE_NAME, log_prefix, nResultBytes, pResult);
  54. else sw_log_error("[%s] %s failed to send a reply(%d bytes, ret=%d): %s!!", MODULE_NAME, log_prefix, nResultBytes, sendBytes, pResult);
  55. free(pResult); // 释放Go侧C.CString()分配的堆内存
  56. }
  57. memset(pTextCache, 0, sizeof(STextCache));
  58. }
  59. // 接收处理来自MCU控制板的数据报文帧, (串口数据回调)
  60. static int comio_data_recv_proc(unsigned long wParam/*传递打开的串口句柄*/, unsigned long lParam/*保留暂未使用*/)
  61. {
  62. SMCBCom *pComIO = &s_myCom; void *pSerial = pComIO->h; STextCache *pTextCache = &pComIO->textCache;
  63. const unsigned char *pRecvBuf = serial_get_recv_buffer(pSerial); int nRecvBytes = serial_get_recv_buffer_bytes(pSerial);
  64. unsigned char ch = pRecvBuf[nRecvBytes - 1]; const char *log_prefix = serial_get_log_prefix(pSerial); lwjsonr_t ret;
  65. // 1, 接收状态机处理
  66. switch(pComIO->recvMode)
  67. {
  68. case IDLE:
  69. if(ch == '{' || ch == '[')
  70. { // IDLE -> JSON
  71. if(pTextCache->len > 0) consume_cached_text_data(pSerial, pTextCache);
  72. json_p:
  73. ret = lwjson_stream_parse(&pComIO->parser, ch); // JSON 1: 已开始解析
  74. if(ret != lwjsonSTREAMINPROG)
  75. {
  76. sw_log_error("[%s] failed to start JSON parsing, lwjson_stream_parse() returned: %d!!", MODULE_NAME, ret);
  77. serial_clear_recv_buffer(pSerial); lwjson_stream_reset(&pComIO->parser); goto recv_p;
  78. }
  79. pComIO->recvMode = JSON; xgettickcount(&pComIO->jsonLastCharTick); goto recv_p;
  80. }
  81. else if(xisprint(ch))
  82. { // IDLE -> TEXT
  83. text_p:
  84. if(pTextCache->len < sizeof(pTextCache->buf)-1) pTextCache->buf[pTextCache->len++] = ch;
  85. else { pTextCache->overflow = true; sw_log_warn("[%s] TEXT cache overflow, discard byte: 0x%02X!!", MODULE_NAME, ch); }
  86. serial_clear_recv_buffer(pSerial); // 已转存到TEXT缓存, 清除串口缓存
  87. pComIO->recvMode = TEXT; xgettickcount(&pComIO->textLastCharTick);
  88. }
  89. else
  90. { // IDLE -> IDLE
  91. drop_p:
  92. sw_log_warn("[%s] %s discarding invalid byte: 0x%02X!", MODULE_NAME, log_prefix, ch);
  93. serial_clear_recv_buffer(pSerial);
  94. }
  95. break;
  96. case JSON:
  97. xgettickcount(&pComIO->jsonLastCharTick);
  98. break;
  99. case TEXT:
  100. if(ch == '{' || ch == '[')
  101. { // TEXT -> JSON
  102. if(pTextCache->len > 0) consume_cached_text_data(pSerial, pTextCache);
  103. pComIO->textLastCharTick = 0; goto json_p;
  104. }
  105. else if(xisprint(ch)) goto text_p;
  106. else goto drop_p;
  107. break;
  108. default:
  109. sw_log_fatal("[%s] invalid recvMode: %d, data receive thread will exit!!!", MODULE_NAME, pComIO->recvMode);
  110. return -1;
  111. }
  112. // 2, 解析处理 JSON
  113. if(pComIO->recvMode != JSON) goto recv_p;
  114. ret = lwjson_stream_parse(&pComIO->parser, ch);
  115. switch(ret)
  116. {
  117. case lwjsonSTREAMINPROG: // JSON 2: 正在解析中
  118. goto recv_p;
  119. case lwjsonSTREAMDONE: // JSON 3: 解析已完成
  120. sw_log_debug("[%s] %s received a json(%d bytes) message: %s", MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
  121. char *pResult = go_handle_serial_json_msg((const char *)pRecvBuf); // 处理JSON消息
  122. if(!pResult) goto idle_p; // 无应答时
  123. int nResultBytes = strlen(pResult);
  124. int sendBytes = serial_send_data(pSerial, (const unsigned char *)pResult, nResultBytes);
  125. if(sendBytes == nResultBytes) sw_log_debug("[%s] %s sent a reply(%d bytes): %s", MODULE_NAME, log_prefix, nResultBytes, pResult);
  126. else sw_log_error("[%s] %s failed to send a reply(%d bytes, ret=%d): %s!!", MODULE_NAME, log_prefix, nResultBytes, sendBytes, pResult);
  127. free(pResult); // 释放Go侧C.CString()分配的堆内存
  128. goto idle_p;
  129. default: // JSON 4: 发生错误时
  130. sw_log_error("[%s] JSON stream parsing failed, lwjson_stream_parse() returned: %d!!", MODULE_NAME, ret);
  131. goto idle_p;
  132. }
  133. idle_p:
  134. pComIO->jsonLastCharTick = 0; lwjson_stream_reset(&pComIO->parser); serial_clear_recv_buffer(pSerial);
  135. pComIO->recvMode = IDLE; // JSON 5: 转换到IDLE
  136. // 3, 继续数据的接收
  137. recv_p:
  138. return 1;
  139. }
  140. // 接收处理来自MCU控制板的数据报文帧, (串口空闲回调)
  141. static int comio_data_idle_proc(unsigned long wParam/*传递打开的串口句柄*/, unsigned long lParam/*保留暂未使用*/)
  142. {
  143. SMCBCom *pComIO = &s_myCom; void *pSerial = pComIO->h; STextCache *pTextCache = &pComIO->textCache;
  144. const unsigned char *pRecvBuf = serial_get_recv_buffer(pSerial); int nRecvBytes = serial_get_recv_buffer_bytes(pSerial);
  145. const char *log_prefix = serial_get_log_prefix(pSerial); unsigned long nowTick;
  146. switch(pComIO->recvMode)
  147. {
  148. case JSON:
  149. xgettickcount(&nowTick);
  150. if(pComIO->jsonLastCharTick > 0 && (nowTick - pComIO->jsonLastCharTick) > RECV_IDLE_TIMEOUT)
  151. { // JSON -> IDLE
  152. sw_log_warn("[%s] %s timeout occurred while receiving a json message(%d bytes): %s!",
  153. MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
  154. pComIO->jsonLastCharTick = 0; lwjson_stream_reset(&pComIO->parser); serial_clear_recv_buffer(pSerial);
  155. pComIO->recvMode = IDLE;
  156. }
  157. break;
  158. case TEXT:
  159. xgettickcount(&nowTick);
  160. if(pComIO->textLastCharTick > 0 && (nowTick - pComIO->textLastCharTick) > RECV_IDLE_TIMEOUT)
  161. { // TEXT -> IDLE
  162. if(pTextCache->len > 0) { consume_cached_text_data(pSerial, pTextCache); } pComIO->textLastCharTick = 0;
  163. pComIO->recvMode = IDLE;
  164. }
  165. break;
  166. }
  167. return 1;
  168. }
  169. // 打开与MCU控制板的串口通讯, 返回: 0成功, <0时失败
  170. int MCU_ComInit()
  171. {
  172. lwjson_stream_init(&s_myCom.parser, NULL);
  173. const char *serialName = "/dev/ttymxc2"; int baudrate = 115200;
  174. const char *parityCheck = "none"; // 无校检
  175. s_myCom.h = serial_open(serialName, baudrate, parityCheck, \
  176. comio_data_recv_proc, comio_data_idle_proc, NULL);
  177. if(!s_myCom.h)
  178. {
  179. sw_log_error("[%s] failed to open the \"%s:%d(%s parity)\" device!!", \
  180. MODULE_NAME, serialName, baudrate, parityCheck);
  181. MCU_ComExit(); return -1;
  182. }
  183. return 0;
  184. }
  185. // 关闭与MCU控制板的串口通讯, 返回: 0成功, <0时失败
  186. int MCU_ComExit()
  187. {
  188. if(s_myCom.h) serial_close(s_myCom.h, WAITTHRD_SAFEEXIT_TIMEOUT);
  189. memset(&s_myCom, 0, sizeof(s_myCom));
  190. return 0;
  191. }
  192. // 主动的发送指令给MCU控制板, 返回: 0成功, <0时失败
  193. int MCU_ComSendCmd(const char *cmd)
  194. {
  195. if(!s_myCom.h || !cmd) return -1;
  196. int nBytes = strlen(cmd);
  197. int ret = serial_send_data(s_myCom.h, (const unsigned char *)cmd, nBytes);
  198. if(ret != nBytes)
  199. {
  200. sw_log_error("[%s] failed to send a command(%d bytes, ret=%d): %s!!", MODULE_NAME, nBytes, ret, cmd);
  201. return -2;
  202. }
  203. sw_log_debug("[%s] sent a command(%d bytes): %s", MODULE_NAME, nBytes, cmd);
  204. return 0;
  205. }