# USB HID Communication Programming [TOC] ## Debug Tool ### WireShark :::spoiler #### Install & Setting ##### Download Wireshark - [Download Page](https://www.wireshark.org/download.html) - ⏬[Install package](https://2.na.dl.wireshark.org/win64/Wireshark-win64-4.0.10.exe) - ⏬[Portable Package](https://2.na.dl.wireshark.org/win64/WiresharkPortable64_4.0.10.paf.exe)[^ref3] - 下載完後,須再下載[USBPCap](https://desowin.org/usbpcap/)安裝後重開機 - Copy `USBPcapCMD.exe` 到 `WiresharkPortable64\App\Wireshark\extcap` - 通过USBPcapCMD.exe(在文件夹\\Wireshark\\extcap内)来查看hub下挂了哪些设备 ##### Select Gear Icon ![image.png](https://hackmd.io/_uploads/SyVKIJKXT.png) ##### 如下設置 - **Unckeck Capture from all devices control**[^ref1] ![image.png](https://hackmd.io/_uploads/S1RnPkY7a.png) #### Filter Setting[^ref4] - 過濾HID control events `usb.data_len > 0` - 只抓HID 封包 `usb.bInterfaceClass == "HID"` - 只抓HID 資料 `usb.capdata || usbhid.data` ::: ### PortHelper ⏬[Download](https://github.com/billwanggithub/HidTest/tree/ad557e404c9c26e38deb6ec5741624fb9993ca35/DebugTools) ![image](https://hackmd.io/_uploads/Byib0GiQT.png) ### BusHound ⏬[Download](https://perisoft.net/bushound/) ![bus hound](https://hackmd.io/_uploads/r1W5WQsQp.png) ## Windows App Programming - [HIDSharp](https://www.zer7.com/software/hidsharp) - [Example Here](https://github.com/billwanggithub/HidTest.git) ## References ### Tools [^ref1]: [WireShark抓USB包使用教程](https://blog.csdn.net/qq_35002379/article/details/130490253?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-130490253-blog-106548320.235%5Ev38%5Epc_relevant_yljh&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-130490253-blog-106548320.235%5Ev38%5Epc_relevant_yljh&utm_relevant_index=14) [^ref2]: [wireshark的usb抓包分析- 抓取数据](https://blog.csdn.net/pq113_6/article/details/106541581) [^ref3]: [Portable Wireshark USB 使用經驗](https://niefngaofei.blogspot.com/2021/09/portable-usb-wireshark.html) [^ref4]: [wireshark的usb抓包分析- 分析数据](https://blog.csdn.net/pq113_6/article/details/106548320?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169943487016800180647152%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169943487016800180647152&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-4-106548320-null-null.142^v96^control&utm_term=wireshark%E7%9A%84usb%E6%8A%93%E5%8C%85%E5%88%86%E6%9E%90&spm=1018.2226.3001.4187) - [BusHound的使用方法详解](https://www.usbzh.com/article/detail-343.html) - [Bus Hound(USB抓包工具)](https://blog.csdn.net/xingsongyu/article/details/107822619) - [HID键盘设备数据抓包分析实践](https://www.usbzh.com/article/detail-345.html) - [Wireshark 的抓包和分析](https://blog.csdn.net/wx17343624830/article/details/132359076?ops_request_misc=&request_id=&biz_id=102&utm_term=wireshark%E7%9A%84usb%E6%8A%93%E5%8C%85%E5%88%86%E6%9E%90&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-132359076.142^v96^control&spm=1018.2226.3001.4187) - [MCU_WireShark USB抓包内容解析](https://blog.csdn.net/tanmx219/article/details/104002602) - [USB Analysis 101 (Tomasz Moń)](https://youtu.be/cUljKImph4s?si=nyP_ScIW-QXhV9dk) - [USB HID Decoding Tips](https://gist.github.com/mwwhited/263f3f871e05c934884926773d79596f#hidsharp) - [Display Filter Reference: USB HID](https://www.wireshark.org/docs/dfref/u/usbhid.html) - [USB HID 流量分析](https://linuxstory.org/usb-hid-traffic-analysis/zh-tw/) - [**標準使用者如何執行需系統管理者權限的程式**](https://daje770729.blogspot.com/2017/06/blog-post.html) `runas /savecred /user:Administrator "WiresharkPortable64.exe` - [安裝軟體不再詢問,卸載軟體不會阻止](https://ppfocus.com/0/te58c64ad.html) `gpupdate /force` ![image.png](https://hackmd.io/_uploads/B1ep4b9mp.png) ### Firmware - [STM32 bare metal USB implementation](https://github.com/CShark/stm32usb/wiki) - [Minimal usb-from-scratch serial port implementation and demo for STM32F411](https://github.com/daedaleanai/stm32f411_usb/tree/master) - [Driverless USB HID bootloader and flashing tool for STM32F10X devices](https://github.com/bootsector/stm32-hid-bootloader/tree/master) - [基于STM32 HAL库的自定义USB HID设备通信](https://blog.csdn.net/radarsmile/article/details/122811081) - [一步步实现stm32cube的usb之CUSTOM_HID当串口使用 (stm32F407discoery板)](https://blog.csdn.net/guanlizhongxintishi/article/details/87882207) - [STM32-USB学习系列](https://blog.csdn.net/laifengyuan1/category_10334116.html) - [USB复合设备的实现(MSC + HID)](https://blog.csdn.net/laifengyuan1/article/details/108243522) - [STM32 USB复合设备编写](https://blog.csdn.net/qq_20312079/article/details/104971076) - [STM32 H7 CDC+HID Composite Device](https://github.com/STMicroelectronics/STM32CubeH7/tree/dev/usb/composite/Projects/STM32H743I-EVAL/Applications/USB_Device/Composite_CustomHID_CDC)) - [STM32 F4 CDC,HID Dual Core Standlone](https://github.com/STMicroelectronics/STM32CubeF4/tree/3d6be4bd406f275728e0a321cc371c62a3100533/Projects/STM32469I_EVAL/Applications/USB_Device/DualCore_Standalone) - [I-CUBE-USBD-Composite](https://github.com/alambe94/I-CUBE-USBD-Composite#i-cube-usbd-composite) A wrapper class around ST USB stack to create STM32 USB Composite devices with ease. - [Understand the GNU linker script of cortex M4](https://community.silabs.com/s/article/understand-the-gnu-linker-script-of-cortex-m4?language=en_US) ### APP - [HID Windows Programming](https://hackmd.io/@billwang168/H1w8dzOOj) - [How to Communicate with its USB Devices using HID Protocol](https://www.codeproject.com/Articles/1244702/How-to-Communicate-with-its-USB-Devices-using-HID) Communicate with the USB devices using WinAPI in C#