windows_driver_serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <windows.h>
  4. #include "windows_driver_serial.h"
  5. #include "drivers/hci_driver.h"
  6. #include "drivers/hci_h4.h"
  7. #include "logging/bt_log_impl.h"
  8. #include "common/bt_buf.h"
  9. // https://blog.csdn.net/u010835747/article/details/117357403
  10. BOOL synchronizeflag;
  11. PORT OpenPort(int idx, BOOL sync)
  12. {
  13. HANDLE hComm;
  14. TCHAR comname[100];
  15. synchronizeflag = sync;
  16. wsprintf(comname, TEXT("\\\\.\\COM%d"), idx);
  17. if (synchronizeflag)
  18. {
  19. hComm = CreateFile(comname, // port name
  20. GENERIC_READ | GENERIC_WRITE, // Read/Write
  21. 0, // No Sharing
  22. NULL, // No Security
  23. OPEN_EXISTING, // Open existing port only
  24. 0, // Non Overlapped I/O
  25. NULL); // Null for Comm Devices
  26. }
  27. else
  28. {
  29. hComm = CreateFileA(comname, //串口名
  30. GENERIC_READ | GENERIC_WRITE, //支持读写
  31. 0, //独占方式,串口不支持共享
  32. NULL, //安全属性指针,默认值为NULL
  33. OPEN_EXISTING, //打开现有的串口文件
  34. FILE_FLAG_OVERLAPPED, // 0:同步方式,FILE_FLAG_OVERLAPPED:异步方式
  35. NULL); //用于复制文件句柄,默认值为NULL,对串口而言该参数必须置为NULL
  36. }
  37. if (hComm == INVALID_HANDLE_VALUE)
  38. return NULL;
  39. // unit ms
  40. COMMTIMEOUTS timeouts = {0};
  41. timeouts.ReadIntervalTimeout = 50;
  42. timeouts.ReadTotalTimeoutConstant = 50;
  43. timeouts.ReadTotalTimeoutMultiplier = 10;
  44. timeouts.WriteTotalTimeoutConstant = 50;
  45. timeouts.WriteTotalTimeoutMultiplier = 10;
  46. if (!SetupComm(hComm, 1024, 1024))
  47. {
  48. return false;
  49. }
  50. if (SetCommTimeouts(hComm, &timeouts) == FALSE)
  51. return NULL;
  52. if (SetCommMask(hComm, EV_RXCHAR) == FALSE)
  53. return NULL;
  54. PurgeComm(hComm, PURGE_TXCLEAR | PURGE_RXCLEAR);
  55. // printk("open%d ok\n", idx);
  56. return hComm;
  57. }
  58. void ClosePort(PORT com_port)
  59. {
  60. CloseHandle(com_port);
  61. }
  62. enum FlowControl
  63. {
  64. NoFlowControl,
  65. CtsRtsFlowControl,
  66. CtsDtrFlowControl,
  67. DsrRtsFlowControl,
  68. DsrDtrFlowControl,
  69. XonXoffFlowControl
  70. };
  71. PORT serial_init(int idx, int rate, int databits, int stopbits, int parity, bool flowcontrol)
  72. {
  73. PORT com_port;
  74. com_port = OpenPort(idx, TRUE);
  75. if (com_port == INVALID_HANDLE_VALUE)
  76. {
  77. printk("open COM%d fail\n", idx);
  78. return NULL;
  79. }
  80. // 配置参数
  81. DCB dcb = {0};
  82. if (!GetCommState(com_port, &dcb))
  83. {
  84. // 获取参数失败
  85. return false;
  86. }
  87. dcb.DCBlength = sizeof(dcb);
  88. dcb.BaudRate = rate; // 波特率
  89. dcb.ByteSize = databits; // 数据位
  90. switch (parity) //校验位
  91. {
  92. case 0:
  93. dcb.Parity = NOPARITY; //无校验
  94. break;
  95. case 1:
  96. dcb.Parity = ODDPARITY; //奇校验
  97. break;
  98. case 2:
  99. dcb.Parity = EVENPARITY; //偶校验
  100. break;
  101. case 3:
  102. dcb.Parity = MARKPARITY; //标记校验
  103. break;
  104. }
  105. switch (stopbits) //停止位
  106. {
  107. case 1:
  108. dcb.StopBits = ONESTOPBIT; // 1位停止位
  109. break;
  110. case 2:
  111. dcb.StopBits = TWOSTOPBITS; // 2位停止位
  112. break;
  113. case 3:
  114. dcb.StopBits = ONE5STOPBITS; // 1.5位停止位
  115. break;
  116. }
  117. //流控设置
  118. dcb.fDsrSensitivity = FALSE;
  119. dcb.fTXContinueOnXoff = FALSE;
  120. dcb.fRtsControl = RTS_CONTROL_DISABLE;
  121. dcb.fDtrControl = DTR_CONTROL_ENABLE;
  122. int fc = CtsRtsFlowControl;
  123. switch (fc)
  124. {
  125. //不流控
  126. case NoFlowControl:
  127. {
  128. dcb.fOutxCtsFlow = FALSE;
  129. dcb.fOutxDsrFlow = FALSE;
  130. dcb.fOutX = FALSE;
  131. dcb.fInX = FALSE;
  132. break;
  133. }
  134. //硬件CtsRts流控
  135. case CtsRtsFlowControl:
  136. {
  137. dcb.fOutxCtsFlow = TRUE;
  138. dcb.fOutxDsrFlow = FALSE;
  139. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
  140. dcb.fOutX = FALSE;
  141. dcb.fInX = FALSE;
  142. break;
  143. }
  144. //硬件 CtsDtr流控
  145. case CtsDtrFlowControl:
  146. {
  147. dcb.fOutxCtsFlow = TRUE;
  148. dcb.fOutxDsrFlow = FALSE;
  149. dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
  150. dcb.fOutX = FALSE;
  151. dcb.fInX = FALSE;
  152. break;
  153. }
  154. //硬件DsrRts流控
  155. case DsrRtsFlowControl:
  156. {
  157. dcb.fOutxCtsFlow = FALSE;
  158. dcb.fOutxDsrFlow = TRUE;
  159. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
  160. dcb.fOutX = FALSE;
  161. dcb.fInX = FALSE;
  162. break;
  163. }
  164. //硬件DsrDtr流控
  165. case DsrDtrFlowControl:
  166. {
  167. dcb.fOutxCtsFlow = FALSE;
  168. dcb.fOutxDsrFlow = TRUE;
  169. dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
  170. dcb.fOutX = FALSE;
  171. dcb.fInX = FALSE;
  172. break;
  173. }
  174. //软件流控
  175. case XonXoffFlowControl:
  176. {
  177. dcb.fOutxCtsFlow = FALSE;
  178. dcb.fOutxDsrFlow = FALSE;
  179. dcb.fOutX = TRUE;
  180. dcb.fInX = TRUE;
  181. dcb.XonChar = 0x11;
  182. dcb.XoffChar = 0x13;
  183. dcb.XoffLim = 100;
  184. dcb.XonLim = 100;
  185. break;
  186. }
  187. }
  188. if (!SetCommState(com_port, &dcb))
  189. {
  190. // 设置参数失败
  191. return false;
  192. }
  193. return com_port;
  194. }
  195. int Serial_SendData(PORT com_port, const char *data, int len)
  196. {
  197. DWORD dNoOfBytesWritten;
  198. BOOL Status;
  199. if (synchronizeflag)
  200. {
  201. // sync
  202. Status = WriteFile(com_port, data, len, &dNoOfBytesWritten, NULL);
  203. if (Status == FALSE)
  204. {
  205. return -1;
  206. }
  207. else
  208. {
  209. // printk("send ok\n");
  210. }
  211. return dNoOfBytesWritten;
  212. }
  213. else
  214. {
  215. //异步方式
  216. DWORD dwErrorFlags; //错误标志
  217. COMSTAT comStat; //通讯状态
  218. OVERLAPPED m_osWrite; //异步输入输出结构体
  219. //创建一个用于OVERLAPPED的事件处理,不会真正用到,但系统要求这么做
  220. memset(&m_osWrite, 0, sizeof(m_osWrite));
  221. m_osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, "WriteEvent");
  222. ClearCommError(com_port, &dwErrorFlags, &comStat); //清除通讯错误,获得设备当前状态
  223. BOOL bWriteStat = WriteFile(com_port, //串口句柄
  224. data, //数据首地址
  225. len, //要发送的数据字节数
  226. &dNoOfBytesWritten, // DWORD*,用来接收返回成功发送的数据字节数
  227. &m_osWrite); // NULL为同步发送,OVERLAPPED*为异步发送
  228. // printk("bWriteStat, %d, dNoOfBytesWritten: %d\n", bWriteStat, dNoOfBytesWritten);
  229. if (!bWriteStat)
  230. {
  231. if (GetLastError() == ERROR_IO_PENDING) //如果串口正在写入
  232. {
  233. WaitForSingleObject(m_osWrite.hEvent, 1000); //等待写入事件1秒钟
  234. dNoOfBytesWritten = len;
  235. }
  236. else
  237. {
  238. ClearCommError(com_port, &dwErrorFlags, &comStat); //清除通讯错误
  239. CloseHandle(m_osWrite.hEvent); //关闭并释放hEvent内存
  240. return -1;
  241. }
  242. }
  243. // printk("dNoOfBytesWritten, %d\n", dNoOfBytesWritten);
  244. return dNoOfBytesWritten;
  245. }
  246. return 0;
  247. }
  248. int Serial_ReciveData(PORT com_port, char *data, int len)
  249. {
  250. DWORD NoBytesRead;
  251. // BOOL Status;
  252. if (synchronizeflag)
  253. {
  254. //同步方式
  255. BOOL bReadStat = ReadFile(com_port, //串口句柄
  256. data, //数据首地址
  257. len, //要读取的数据最大字节数
  258. &NoBytesRead, // DWORD*,用来接收返回成功读取的数据字节数
  259. NULL); // NULL为同步发送,OVERLAPPED*为异步发送
  260. ARG_UNUSED(bReadStat);
  261. }
  262. else
  263. {
  264. //异步方式
  265. DWORD dwErrorFlags; //错误标志
  266. COMSTAT comStat; //通讯状态
  267. OVERLAPPED m_osRead; //异步输入输出结构体
  268. //创建一个用于OVERLAPPED的事件处理,不会真正用到,但系统要求这么做
  269. memset(&m_osRead, 0, sizeof(m_osRead));
  270. m_osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, "ReadEvent");
  271. ClearCommError(com_port, &dwErrorFlags, &comStat); //清除通讯错误,获得设备当前状态
  272. // if (!comStat.cbInQue)
  273. // return 0; //如果输入缓冲区字节数为0,则返回false
  274. // std::cout << comStat.cbInQue << std::endl;
  275. BOOL bReadStat = ReadFile(com_port, //串口句柄
  276. data, //数据首地址
  277. len, //要读取的数据最大字节数
  278. &NoBytesRead, // DWORD*,用来接收返回成功读取的数据字节数
  279. &m_osRead); // NULL为同步发送,OVERLAPPED*为异步发送
  280. if (!bReadStat)
  281. {
  282. if (GetLastError() == ERROR_IO_PENDING) //如果串口正在读取中
  283. {
  284. // GetOverlappedResult函数的最后一个参数设为TRUE
  285. //函数会一直等待,直到读操作完成或由于错误而返回
  286. GetOverlappedResult(com_port, &m_osRead, &NoBytesRead, TRUE);
  287. }
  288. else
  289. {
  290. ClearCommError(com_port, &dwErrorFlags, &comStat); //清除通讯错误
  291. CloseHandle(m_osRead.hEvent); //关闭并释放hEvent的内存
  292. return -1;
  293. }
  294. }
  295. }
  296. // printk("rcv ok, %ld\n", NoBytesRead);
  297. return NoBytesRead;
  298. }
  299. int Serial_AsyncSendData(PORT com_port, const char *data, int len)
  300. {
  301. DWORD dNoOfBytesWritten;
  302. BOOL Status = WriteFile(com_port, data, len, &dNoOfBytesWritten, NULL);
  303. if (Status == FALSE)
  304. return -1;
  305. else
  306. // printk("send ok\n");
  307. return 0;
  308. }
  309. int Serial_AsyncReciveData(PORT com_port, char *data, int len)
  310. {
  311. // DWORD dwEventMask;
  312. DWORD NoBytesRead;
  313. BOOL Status;
  314. // BOOL Status = WaitCommEvent(com_port, &dwEventMask, NULL);
  315. // if (Status == FALSE)
  316. // {
  317. // return -1;
  318. // }
  319. Status = ReadFile(com_port, data, len, &NoBytesRead, NULL);
  320. data[NoBytesRead] = 0;
  321. if (Status == FALSE)
  322. return -1;
  323. else
  324. printk("rcv ok\n");
  325. return NoBytesRead;
  326. }
  327. static bool is_enable;
  328. static PORT serial;
  329. static int hci_driver_h4_open(void)
  330. {
  331. return 0;
  332. }
  333. static int hci_driver_h4_send(uint8_t *buf, uint16_t len)
  334. {
  335. return Serial_SendData(serial, (char *)buf, len);
  336. }
  337. static int hci_driver_h4_recv(uint8_t *buf, uint16_t len)
  338. {
  339. return Serial_ReciveData(serial, (char *)buf, len);
  340. }
  341. static const struct bt_hci_h4_driver h4_drv = {
  342. .open = hci_driver_h4_open,
  343. .send = hci_driver_h4_send,
  344. .recv = hci_driver_h4_recv,
  345. };
  346. static void hci_driver_h4_init(void)
  347. {
  348. hci_h4_init(&h4_drv);
  349. }
  350. int serial_open_process(int idx, int rate, int databits, int stopbits, int parity, bool flowcontrol)
  351. {
  352. printk("serial_open_process idx: %d, rate: %d, databits: %d, stopbits: %d, parity: %d, "
  353. "flowcontrol: %d\n",
  354. idx, rate, databits, stopbits, parity, flowcontrol);
  355. serial = serial_init(idx, rate, databits, stopbits, parity, flowcontrol);
  356. is_enable = true;
  357. return 0;
  358. }
  359. int bt_hci_init_serial_device(int idx, int rate, int databits, int stopbits, int parity, bool flowcontrol)
  360. {
  361. int ret = serial_open_process(idx, rate, databits, stopbits, parity, flowcontrol);
  362. if (ret < 0)
  363. {
  364. return ret;
  365. }
  366. hci_driver_h4_init();
  367. return (0);
  368. }