usbh_bl616.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USBH_BL616_H
  7. #define USBH_BL616_H
  8. #define USBWIFI_DATA_TYPE_CMD 0xA55A
  9. #define USBWIFI_DATA_TYPE_PKT 0x6996
  10. #define USB_DATA_FLAG_AP_PKT (1u << 0)
  11. typedef enum {
  12. BFLB_CMD_REBOOT = 0,
  13. BFLB_CMD_RESET,
  14. BFLB_CMD_HELLO,
  15. BFLB_CMD_PING,
  16. BFLB_CMD_GET_MAC_ADDR,
  17. // Scan
  18. BFLB_CMD_SCAN,
  19. BFLB_CMD_SCAN_RESULTS,
  20. // STA
  21. BFLB_CMD_STA_CONNECT,
  22. BFLB_CMD_STA_DISCONNECT,
  23. BFLB_CMD_STA_CONNECTED_IND,
  24. BFLB_CMD_STA_DISCONNECTED_IND,
  25. BFLB_CMD_STA_IP_UPDATE_IND,
  26. BFLB_CMD_STA_SET_AUTO_RECONNECT,
  27. BFLB_CMD_STA_GET_LINK_STATUS,
  28. // AP
  29. BFLB_CMD_AP_START,
  30. BFLB_CMD_AP_STOP,
  31. BFLB_CMD_AP_STARTED_IND,
  32. BFLB_CMD_AP_STOPPED_IND,
  33. BFLB_CMD_AP_GET_STA_LIST,
  34. // Monitor
  35. BFLB_CMD_MONITOR_START,
  36. BFLB_CMD_MONITOR_STOP,
  37. BFLB_CMD_MONITOR_SET_CHANNEL,
  38. BFLB_CMD_MONITOR_GET_CHANNEL,
  39. BFLB_CMD_SET_LPM_MODE,
  40. // OTA
  41. BFLB_CMD_GET_DEV_VERSION,
  42. BFLB_CMD_OTA,
  43. BFLB_CMD_EXT,
  44. BFLB_CMD_USER_EXT,
  45. BFLB_CMD_UNLOAD_DRV,
  46. BFLB_CMD_MAX,
  47. } bflb_cmd_t;
  48. typedef enum {
  49. STATUS_OK,
  50. STATUS_NOMEM = 128,
  51. STATUS_INVALID_INPUT,
  52. STATUS_INVALID_MODE,
  53. STATUS_ERR_UNSPECIFIED,
  54. STATUS_NOT_IMPLEMENTED,
  55. } cmd_status_t;
  56. typedef enum {
  57. RNM_WIFI_AUTH_UNKNOWN = 0,
  58. RNM_WIFI_AUTH_OPEN,
  59. RNM_WIFI_AUTH_WEP,
  60. RNM_WIFI_AUTH_WPA_PSK,
  61. RNM_WIFI_AUTH_WPA2_PSK,
  62. RNM_WIFI_AUTH_WPA_WPA2_PSK,
  63. RNM_WIFI_AUTH_WPA_ENTERPRISE,
  64. RNM_WIFI_AUTH_WPA3_SAE,
  65. RNM_WIFI_AUTH_WPA2_PSK_WPA3_SAE,
  66. RNM_WIFI_AUTH_MAX,
  67. } rnm_wifi_auth_mode_t;
  68. typedef enum {
  69. RNM_WIFI_CIPHER_UNKNOWN = 0,
  70. RNM_WIFI_CIPHER_NONE,
  71. RNM_WIFI_CIPHER_WEP,
  72. RNM_WIFI_CIPHER_AES,
  73. RNM_WIFI_CIPHER_TKIP,
  74. RNM_WIFI_CIPHER_TKIP_AES,
  75. RNM_WIFI_CIPHER_MAX,
  76. } rnm_wifi_cipher_t;
  77. /* common header */
  78. typedef struct {
  79. uint16_t cmd;
  80. // flag ACK is used by server to indicate a response to client
  81. #define RNM_MSG_FLAG_ACK (1 << 0)
  82. // flag TRANSPARENT is never transfered to peer but used locally
  83. #define RNM_MSG_FLAG_TRANSPARENT (1 << 1)
  84. // flag ASYNC is used by server to notify client events such as STA_CONNECTED
  85. #define RNM_MSG_FLAG_ASYNC (1 << 2)
  86. uint16_t flags;
  87. uint16_t status;
  88. uint16_t msg_id;
  89. uint16_t session_id;
  90. uint16_t msg_id_replying;
  91. } rnm_base_msg_t;
  92. typedef struct {
  93. rnm_base_msg_t hdr;
  94. } rnm_ack_msg_t;
  95. typedef struct {
  96. rnm_base_msg_t hdr;
  97. uint8_t sta_mac[6];
  98. uint8_t ap_mac[6];
  99. } rnm_mac_addr_ind_msg_t;
  100. typedef struct {
  101. rnm_base_msg_t hdr;
  102. uint16_t ssid_len;
  103. uint8_t ssid[32];
  104. uint8_t password[64];
  105. } rnm_sta_connect_msg_t;
  106. typedef struct {
  107. rnm_base_msg_t hdr;
  108. uint8_t ip4_addr[4];
  109. uint8_t ip4_mask[4];
  110. uint8_t ip4_gw[4];
  111. uint8_t ip4_dns1[4];
  112. uint8_t ip4_dns2[4];
  113. uint8_t gw_mac[6];
  114. } rnm_sta_ip_update_ind_msg_t;
  115. struct bf1b_wifi_scan_record {
  116. uint8_t bssid[6];
  117. // TODO use compressed SSID encoding to save room
  118. uint8_t ssid[32 + 1];
  119. uint16_t channel;
  120. int8_t rssi;
  121. uint8_t auth_mode;
  122. uint8_t cipher;
  123. } __PACKED;
  124. typedef struct {
  125. rnm_base_msg_t hdr;
  126. uint16_t num;
  127. struct bf1b_wifi_scan_record records[];
  128. } rnm_scan_ind_msg_t;
  129. typedef enum {
  130. BL_MODE_NONE,
  131. BL_MODE_STA, // card is STA
  132. BL_MODE_AP, // card is AP
  133. BL_MODE_STA_AP, // card is STA&AP
  134. BL_MODE_SNIFFER, // card is sniffer
  135. BL_MODE_MAX,
  136. } bl_wifi_mode_t;
  137. typedef struct {
  138. uint16_t type;
  139. uint16_t length;
  140. uint16_t flags;
  141. uint16_t payload_offset;
  142. uint32_t rsvd[8];
  143. uint8_t payload[];
  144. } __attribute__((aligned(4))) usb_data_t;
  145. struct usbh_bl616 {
  146. struct usbh_hubport *hport;
  147. struct usb_endpoint_descriptor *bulkin; /* Bulk IN endpoint */
  148. struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
  149. struct usbh_urb bulkout_urb;
  150. struct usbh_urb bulkin_urb;
  151. uint8_t intf;
  152. uint8_t sta_mac[6];
  153. uint8_t ap_mac[6];
  154. uint8_t mode;
  155. bool connect_status;
  156. void *user_data;
  157. };
  158. #ifdef __cplusplus
  159. extern "C" {
  160. #endif
  161. int usbh_bl616_wifi_sta_connect(const char *ssid,
  162. const int ssid_len,
  163. const char *password,
  164. const int pwd_len);
  165. int usbh_bl616_wifi_sta_disconnect(void);
  166. int usbh_bl616_wifi_scan(void);
  167. void usbh_bl616_sta_connect_callback(void);
  168. void usbh_bl616_sta_disconnect_callback(void);
  169. void usbh_bl616_sta_update_ip(uint8_t ip4_addr[4], uint8_t ip4_mask[4], uint8_t ip4_gw[4]);
  170. uint8_t *usbh_bl616_get_eth_txbuf(void);
  171. int usbh_bl616_eth_output(uint32_t buflen);
  172. void usbh_bl616_eth_input(uint8_t *buf, uint32_t buflen);
  173. void usbh_bl616_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV);
  174. void usbh_bl616_run(struct usbh_bl616 *bl616_class);
  175. void usbh_bl616_stop(struct usbh_bl616 *bl616_class);
  176. int wifi_sta_connect(int argc, char **argv);
  177. int wifi_scan(int argc, char **argv);
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif /* USBH_BL616_H */