123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- /*
- * Copyright (c) 2024, sakumisu
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include "usbh_core.h"
- #include "usbh_bl616.h"
- #undef USB_DBG_TAG
- #define USB_DBG_TAG "usbh_bl616"
- #include "usb_log.h"
- #define DEV_FORMAT "/dev/wifi/bl616"
- #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
- #define ARR_ELE_6(e) (e)[0], (e)[1], (e)[2], (e)[3], (e)[4], (e)[5]
- USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bl616_tx_buffer[2048 + 512];
- USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bl616_rx_buffer[2048 + 512];
- static struct usbh_bl616 g_bl616_class;
- static const char *auth_to_str(uint8_t auth)
- {
- const char *table[RNM_WIFI_AUTH_MAX] = {
- [RNM_WIFI_AUTH_UNKNOWN] = "UNKNOWN",
- [RNM_WIFI_AUTH_OPEN] = "OPEN",
- [RNM_WIFI_AUTH_WEP] = "WEP",
- [RNM_WIFI_AUTH_WPA_PSK] = "WPA-PSK",
- [RNM_WIFI_AUTH_WPA2_PSK] = "WPA2-PSK",
- [RNM_WIFI_AUTH_WPA_WPA2_PSK] = "WPA2-PSK/WPA-PSK",
- [RNM_WIFI_AUTH_WPA_ENTERPRISE] = "WPA-ENT",
- [RNM_WIFI_AUTH_WPA3_SAE] = "WPA3-SAE",
- [RNM_WIFI_AUTH_WPA2_PSK_WPA3_SAE] = "WPA2-PSK/WPA3-SAE",
- };
- if (auth < RNM_WIFI_AUTH_MAX)
- return table[auth];
- else
- return table[RNM_WIFI_AUTH_UNKNOWN];
- }
- static const char *cipher_to_str(uint8_t cipher)
- {
- const char *table[RNM_WIFI_CIPHER_MAX] = {
- [RNM_WIFI_CIPHER_UNKNOWN] = "UNKNOWN",
- [RNM_WIFI_CIPHER_NONE] = "NONE",
- [RNM_WIFI_CIPHER_WEP] = "WEP",
- [RNM_WIFI_CIPHER_AES] = "AES",
- [RNM_WIFI_CIPHER_TKIP] = "TKIP",
- [RNM_WIFI_CIPHER_TKIP_AES] = "TKIP/AES",
- };
- if (cipher < RNM_WIFI_CIPHER_MAX)
- return table[cipher];
- else
- return table[RNM_WIFI_CIPHER_UNKNOWN];
- }
- static int parse_get_mac_rsp_msg(struct usbh_bl616 *bl616_class, void *buf, int buf_len)
- {
- usb_data_t *usb_hdr = buf;
- rnm_mac_addr_ind_msg_t *rsp = buf + sizeof(usb_data_t);
- if (buf_len != sizeof(usb_data_t) + sizeof(rnm_mac_addr_ind_msg_t)) {
- return -1;
- }
- if (usb_hdr->type != USBWIFI_DATA_TYPE_CMD || usb_hdr->length != sizeof(rnm_mac_addr_ind_msg_t)) {
- return -1;
- }
- if (rsp->hdr.cmd != BFLB_CMD_GET_MAC_ADDR || !(rsp->hdr.flags & RNM_MSG_FLAG_ACK)) {
- return -1;
- }
- memcpy(bl616_class->sta_mac, rsp->sta_mac, 6);
- memcpy(bl616_class->ap_mac, rsp->ap_mac, 6);
- return 0;
- }
- static int usbh_bl616_bulk_in_transfer(struct usbh_bl616 *bl616_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
- {
- int ret;
- struct usbh_urb *urb = &bl616_class->bulkin_urb;
- usbh_bulk_urb_fill(urb, bl616_class->hport, bl616_class->bulkin, buffer, buflen, timeout, NULL, NULL);
- ret = usbh_submit_urb(urb);
- if (ret == 0) {
- ret = urb->actual_length;
- }
- return ret;
- }
- static int usbh_bl616_bulk_out_transfer(struct usbh_bl616 *bl616_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
- {
- int ret;
- struct usbh_urb *urb = &bl616_class->bulkout_urb;
- usbh_bulk_urb_fill(urb, bl616_class->hport, bl616_class->bulkout, buffer, buflen, timeout, NULL, NULL);
- ret = usbh_submit_urb(urb);
- if (ret == 0) {
- ret = urb->actual_length;
- }
- return ret;
- }
- static int usbh_bl616_get_wifi_mac(struct usbh_bl616 *bl616_class)
- {
- int ret;
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *rnm_msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(rnm_msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- rnm_msg->cmd = BFLB_CMD_GET_MAC_ADDR;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- ret = usbh_bl616_bulk_out_transfer(bl616_class, g_bl616_tx_buffer, msg_len, 500);
- if (ret < 0) {
- return ret;
- }
- ret = usbh_bl616_bulk_in_transfer(bl616_class, g_bl616_rx_buffer, sizeof(g_bl616_rx_buffer), 500);
- if (ret < 0) {
- return ret;
- }
- ret = parse_get_mac_rsp_msg(bl616_class, g_bl616_rx_buffer, ret);
- return ret;
- }
- static int usbh_bl616_wifi_open(struct usbh_bl616 *bl616_class)
- {
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->cmd = BFLB_CMD_HELLO;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- return usbh_bl616_bulk_out_transfer(bl616_class, g_bl616_tx_buffer, msg_len, 500);
- }
- static int usbh_bl616_wifi_close(struct usbh_bl616 *bl616_class)
- {
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->cmd = BFLB_CMD_UNLOAD_DRV;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- return usbh_bl616_bulk_out_transfer(bl616_class, g_bl616_tx_buffer, msg_len, 500);
- }
- int usbh_bl616_wifi_sta_connect(const char *ssid,
- const int ssid_len,
- const char *password,
- const int pwd_len)
- {
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_sta_connect_msg_t *msg = (rnm_sta_connect_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_sta_connect_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_sta_connect_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->hdr.cmd = BFLB_CMD_STA_CONNECT;
- msg->hdr.msg_id = 0x0001;
- msg->hdr.session_id = 0x0002;
- msg->ssid_len = ssid_len;
- memcpy(msg->ssid, ssid, ssid_len);
- if (password) {
- memcpy(msg->password, password, pwd_len);
- }
- msg_len = sizeof(usb_data_t) + sizeof(rnm_sta_connect_msg_t);
- return usbh_bl616_bulk_out_transfer(&g_bl616_class, g_bl616_tx_buffer, msg_len, 500);
- }
- int usbh_bl616_wifi_sta_disconnect(void)
- {
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->cmd = BFLB_CMD_STA_DISCONNECT;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- return usbh_bl616_bulk_out_transfer(&g_bl616_class, g_bl616_tx_buffer, msg_len, 500);
- }
- int usbh_bl616_get_wifi_scan_result(void)
- {
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->cmd = BFLB_CMD_SCAN_RESULTS;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- return usbh_bl616_bulk_out_transfer(&g_bl616_class, g_bl616_tx_buffer, msg_len, 500);
- }
- int usbh_bl616_wifi_scan(void)
- {
- int ret;
- uint32_t msg_len;
- usb_data_t *usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- rnm_base_msg_t *msg = (rnm_base_msg_t *)(g_bl616_tx_buffer + sizeof(usb_data_t));
- memset(usb_hdr, 0, sizeof(usb_data_t));
- memset(msg, 0, sizeof(rnm_base_msg_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_CMD;
- usb_hdr->length = sizeof(rnm_base_msg_t);
- usb_hdr->payload_offset = sizeof(usb_data_t);
- msg->cmd = BFLB_CMD_SCAN;
- msg_len = sizeof(usb_data_t) + sizeof(rnm_base_msg_t);
- ret = usbh_bl616_bulk_out_transfer(&g_bl616_class, g_bl616_tx_buffer, msg_len, 500);
- if (ret < 0) {
- return ret;
- }
- usb_osal_msleep(500);
- return usbh_bl616_get_wifi_scan_result();
- }
- static int usbh_bl616_connect(struct usbh_hubport *hport, uint8_t intf)
- {
- struct usb_endpoint_descriptor *ep_desc;
- int ret = 0;
- struct usbh_bl616 *bl616_class = &g_bl616_class;
- memset(bl616_class, 0, sizeof(struct usbh_bl616));
- bl616_class->hport = hport;
- bl616_class->intf = intf;
- hport->config.intf[intf].priv = bl616_class;
- for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
- ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
- if (ep_desc->bEndpointAddress & 0x80) {
- USBH_EP_INIT(bl616_class->bulkin, ep_desc);
- } else {
- USBH_EP_INIT(bl616_class->bulkout, ep_desc);
- }
- }
- usbh_bl616_get_wifi_mac(bl616_class);
- usbh_bl616_wifi_close(bl616_class);
- usbh_bl616_wifi_open(bl616_class);
- USB_LOG_INFO("BL616 WIFI STA MAC address %02x:%02x:%02x:%02x:%02x:%02x\r\n",
- bl616_class->sta_mac[0],
- bl616_class->sta_mac[1],
- bl616_class->sta_mac[2],
- bl616_class->sta_mac[3],
- bl616_class->sta_mac[4],
- bl616_class->sta_mac[5]);
- USB_LOG_INFO("BL616 WIFI AP MAC address %02x:%02x:%02x:%02x:%02x:%02x\r\n",
- bl616_class->ap_mac[0],
- bl616_class->ap_mac[1],
- bl616_class->ap_mac[2],
- bl616_class->ap_mac[3],
- bl616_class->ap_mac[4],
- bl616_class->ap_mac[5]);
- strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
- USB_LOG_INFO("Register BL616 WIFI Class:%s\r\n", hport->config.intf[intf].devname);
- usbh_bl616_run(bl616_class);
- return ret;
- }
- static int usbh_bl616_disconnect(struct usbh_hubport *hport, uint8_t intf)
- {
- int ret = 0;
- struct usbh_bl616 *bl616_class = (struct usbh_bl616 *)hport->config.intf[intf].priv;
- if (bl616_class) {
- if (bl616_class->bulkin) {
- usbh_kill_urb(&bl616_class->bulkin_urb);
- }
- if (bl616_class->bulkout) {
- usbh_kill_urb(&bl616_class->bulkout_urb);
- }
- if (hport->config.intf[intf].devname[0] != '\0') {
- USB_LOG_INFO("Unregister BL616 WIFI Class:%s\r\n", hport->config.intf[intf].devname);
- usbh_bl616_stop(bl616_class);
- }
- memset(bl616_class, 0, sizeof(struct usbh_bl616));
- }
- return ret;
- }
- void usbh_bl616_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
- {
- int ret;
- usb_data_t *usb_hdr;
- rnm_base_msg_t *msg;
- rnm_sta_ip_update_ind_msg_t *ipmsg;
- rnm_scan_ind_msg_t *scanmsg;
- uint8_t *data;
- (void)CONFIG_USB_OSAL_THREAD_GET_ARGV;
- USB_LOG_INFO("Create bl616 wifi rx thread\r\n");
- while (1) {
- ret = usbh_bl616_bulk_in_transfer(&g_bl616_class, g_bl616_rx_buffer, sizeof(g_bl616_rx_buffer), USB_OSAL_WAITING_FOREVER);
- if (ret < 0) {
- break;
- }
- usb_hdr = (usb_data_t *)g_bl616_rx_buffer;
- if (usb_hdr->type == USBWIFI_DATA_TYPE_CMD) {
- msg = (rnm_base_msg_t *)(g_bl616_rx_buffer + usb_hdr->payload_offset);
- switch (msg->cmd) {
- case BFLB_CMD_STA_CONNECTED_IND:
- USB_LOG_INFO("AP connected\n");
- g_bl616_class.connect_status = true;
- usbh_bl616_sta_connect_callback();
- break;
- case BFLB_CMD_STA_DISCONNECTED_IND:
- if (g_bl616_class.connect_status == true) {
- g_bl616_class.connect_status = false;
- USB_LOG_INFO("AP disconnected\n");
- usbh_bl616_sta_disconnect_callback();
- }
- break;
- case BFLB_CMD_STA_IP_UPDATE_IND:
- ipmsg = (rnm_sta_ip_update_ind_msg_t *)(g_bl616_rx_buffer + usb_hdr->payload_offset);
- USB_LOG_INFO("WIFI IP update\r\n");
- USB_LOG_INFO("WIFI IPv4 Address : %d:%d:%d:%d\r\n",
- ipmsg->ip4_addr[0],
- ipmsg->ip4_addr[1],
- ipmsg->ip4_addr[2],
- ipmsg->ip4_addr[3]);
- USB_LOG_INFO("WIFI IPv4 Mask : %d:%d:%d:%d\r\n",
- ipmsg->ip4_mask[0],
- ipmsg->ip4_mask[1],
- ipmsg->ip4_mask[2],
- ipmsg->ip4_mask[3]);
- USB_LOG_INFO("WIFI IPv4 Gateway : %d:%d:%d:%d\r\n\r\n",
- ipmsg->ip4_gw[0],
- ipmsg->ip4_gw[1],
- ipmsg->ip4_gw[2],
- ipmsg->ip4_gw[3]);
- g_bl616_class.mode = BL_MODE_STA;
- usbh_bl616_sta_update_ip(ipmsg->ip4_addr, ipmsg->ip4_mask, ipmsg->ip4_gw);
- break;
- case BFLB_CMD_SCAN_RESULTS:
- scanmsg = (rnm_scan_ind_msg_t *)(g_bl616_rx_buffer + usb_hdr->payload_offset);
- USB_LOG_INFO("WIFI scan result:\r\n");
- for (uint32_t i = 0; i < scanmsg->num; ++i) {
- struct bf1b_wifi_scan_record *r = &scanmsg->records[i];
- USB_LOG_INFO("BSSID " MAC_FMT ", channel %u, rssi %d, auth %s, cipher %s, SSID %s\r\n",
- ARR_ELE_6(r->bssid), r->channel, r->rssi,
- auth_to_str(r->auth_mode), cipher_to_str(r->cipher), r->ssid);
- }
- break;
- default:
- break;
- }
- } else if (usb_hdr->type == USBWIFI_DATA_TYPE_PKT) {
- data = (uint8_t *)(g_bl616_rx_buffer + usb_hdr->payload_offset);
- usbh_bl616_eth_input(data, usb_hdr->length);
- } else {
- }
- }
- USB_LOG_INFO("Delete bl616 wifi rx thread\r\n");
- usb_osal_thread_delete(NULL);
- }
- uint8_t *usbh_bl616_get_eth_txbuf(void)
- {
- return (g_bl616_tx_buffer + sizeof(usb_data_t));
- }
- int usbh_bl616_eth_output(uint32_t buflen)
- {
- usb_data_t *usb_hdr;
- uint32_t txlen;
- if (g_bl616_class.connect_status == false) {
- return -USB_ERR_NOTCONN;
- }
- usb_hdr = (usb_data_t *)g_bl616_tx_buffer;
- memset(usb_hdr, 0, sizeof(usb_data_t));
- usb_hdr->type = USBWIFI_DATA_TYPE_PKT;
- usb_hdr->length = buflen;
- usb_hdr->payload_offset = sizeof(usb_data_t);
- txlen = buflen + sizeof(usb_data_t);
- if (!(txlen % USB_GET_MAXPACKETSIZE(g_bl616_class.bulkout->wMaxPacketSize))) {
- txlen += 1;
- }
- USB_LOG_DBG("txlen:%d\r\n", txlen);
- usbh_bulk_urb_fill(&g_bl616_class.bulkout_urb, g_bl616_class.hport, g_bl616_class.bulkout, g_bl616_tx_buffer, txlen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
- return usbh_submit_urb(&g_bl616_class.bulkout_urb);
- }
- int wifi_sta_connect(int argc, char **argv)
- {
- if (argc < 3) {
- USB_LOG_ERR("Usage: %s <ssid> <password>\r\n", argv[0]);
- return -1;
- }
- usbh_bl616_wifi_sta_connect(argv[1], strlen(argv[1]), argv[2], strlen(argv[2]));
- return 0;
- }
- int wifi_scan(int argc, char **argv)
- {
- (void)argc;
- (void)argv;
- usbh_bl616_wifi_scan();
- return 0;
- }
- __WEAK void usbh_bl616_run(struct usbh_bl616 *bl616_class)
- {
- (void)bl616_class;
- }
- __WEAK void usbh_bl616_stop(struct usbh_bl616 *bl616_class)
- {
- (void)bl616_class;
- }
- static const uint16_t bl616_id_table[][2] = {
- { 0x349b, 0x616f },
- { 0, 0 },
- };
- static const struct usbh_class_driver bl616_class_driver = {
- .driver_name = "bl616_wifi",
- .connect = usbh_bl616_connect,
- .disconnect = usbh_bl616_disconnect
- };
- CLASS_INFO_DEFINE const struct usbh_class_info bl616_class_info = {
- .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
- .bInterfaceClass = 0xff,
- .bInterfaceSubClass = 0x00,
- .bInterfaceProtocol = 0x00,
- .id_table = bl616_id_table,
- .class_driver = &bl616_class_driver
- };
|