--- /dev/null
+// #include "cat1_trans.h"
+// #include "stdio.h"
+// #include <cmsis_os2.h>
+// #include <sys/socket.h>
+// #include "wearable_log.h"
+// #include <lwip/err.h>
+// #include "lwip/sockets.h"
+// #include "lwip/netdb.h"
+// #include <lwip/dns.h>
+// #include "lwip/ip.h"
+// #include "lwip/netif.h"
+// #include "lwip/init.h"
+// #include "lwip/inet_chksum.h"
+// #include "lwip/sys.h"
+// #include "../../../../open_source/mbedtls/mbedtls_v3.1.0/include/mbedtls/sha1.h"
+// #include "../../../../open_source/mbedtls/mbedtls_v3.1.0/include/mbedtls/base64.h"
+// #include "../../../../kernel/liteos/liteos_v207.1.0/Huawei_LiteOS/open_source/musl/include/arpa/inet.h"
+
+// #define BUFFER_SIZE 1024
+// #define WEBSOCKET_KEY_LEN 24
+// #define WEBSOCKET_GUID "1234567890123456"
+// void *aic_zalloc(unsigned int len)
+// {
+// return malloc(len);
+// }
+
+// // 释放URL结构体中分配的内存
+// void free_url(BK_URL url)
+// {
+// // 释放已分配的内存
+// if (url.protocol)
+// {
+// free(url.protocol);
+// url.protocol = NULL;
+// }
+// if (url.host)
+// {
+// free(url.host);
+// url.host = NULL;
+// }
+// if (url.path)
+// {
+// free(url.path);
+// url.path = NULL;
+// }
+// if (url.query)
+// {
+// free(url.query);
+// url.query = NULL;
+// }
+// if (url.error_message)
+// {
+// free(url.error_message);
+// url.error_message = NULL;
+// }
+// }
+
+// static void dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg)
+// {
+// struct cat1_ws_init_t *ip_sem = (struct cat1_ws_init_t *)arg;
+
+// printf("hostname %s ipaddr%d \r\n", hostname, ipaddr->u_addr.ip4);
+// memcpy(&(ip_sem->ip), ipaddr, sizeof(ipaddr));
+// (void)osSemaphoreRelease(ip_sem->dns_done_sem);
+// }
+
+// // 自定义的 strdup 函数,添加错误处理
+// char *bk_strdup(const char *str)
+// {
+// if (str == NULL)
+// {
+// return NULL;
+// }
+// size_t len = strlen(str) + 1;
+// char *new_str = (char *)aic_zalloc(len);
+// if (new_str == NULL)
+// {
+// return NULL;
+// }
+// return (char *)memcpy(new_str, str, len);
+// }
+// // 设置错误信息
+// void set_url_error(BK_URL *url, const char *error_msg)
+// {
+// url->error = 1;
+// url->error_message = bk_strdup(error_msg);
+// if (!url->error_message)
+// {
+// // 若分配失败,输出提示信息
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Memory allocation failed for error message.\n");
+// }
+// }
+// BK_URL parse_url(const char *url)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "parse_url url=%s.\n", url);
+// BK_URL result = {NULL, NULL, 0, NULL, NULL, 0, NULL};
+// char *temp_url_t = bk_strdup(url);
+// char *temp_url = temp_url_t;
+// if (!temp_url)
+// {
+// set_url_error(&result, "Failed to allocate memory for temp_url");
+// return result;
+// }
+
+// // 解析协议
+// char *colon = strchr(temp_url, ':');
+// if (!colon)
+// {
+// set_url_error(&result, "Invalid URL: No protocol specified");
+// free(temp_url_t);
+// return result;
+// }
+// *colon = '\0';
+// result.protocol = bk_strdup(temp_url);
+// if (!result.protocol)
+// {
+// set_url_error(&result, "Failed to allocate memory for protocol");
+// free(temp_url_t);
+// return result;
+// }
+// temp_url = colon + 3; // 跳过 "://"
+
+// // 解析主机和端口
+// char *slash = strchr(temp_url, '/');
+// if (!slash)
+// {
+// set_url_error(&result, "Invalid URL: No path specified");
+// free(temp_url_t);
+// return result;
+// }
+// *slash = '\0';
+// char *at = strchr(temp_url, '@');
+// if (at)
+// {
+// temp_url = at + 1; // 跳过用户名和密码部分
+// }
+// char *colon_port = strchr(temp_url, ':');
+// if (colon_port)
+// {
+// *colon_port = '\0';
+// result.host = bk_strdup(temp_url);
+// if (!result.host)
+// {
+// set_url_error(&result, "Failed to allocate memory for host");
+// free(temp_url_t);
+// return result;
+// }
+// result.port = atoi(colon_port + 1);
+// if (!result.port)
+// {
+// set_url_error(&result, "Invalid port number");
+// free(temp_url_t);
+// return result;
+// }
+// }
+// else
+// {
+// result.host = bk_strdup(temp_url);
+// if (!result.host)
+// {
+// set_url_error(&result, "Failed to allocate memory for host");
+// free(temp_url_t);
+// return result;
+// }
+// if (!strcmp(result.protocol, "http") || !strcmp(result.protocol, "ws"))
+// {
+// result.port = 80;
+// }
+// else if (!strcmp(result.protocol, "https") || !strcmp(result.protocol, "wss"))
+// {
+// result.port = 443;
+// }
+// }
+// temp_url = slash + 1;
+// // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "parse_url slash=%s\n",temp_url);
+// // 解析路径和查询字符串
+// char *question = strchr(temp_url, '?');
+// if (question)
+// {
+// *question = '\0';
+// result.path = bk_strdup(temp_url);
+// if (!result.path)
+// {
+// set_url_error(&result, "Failed to allocate memory for path");
+// free(temp_url_t);
+// return result;
+// }
+// result.query = bk_strdup(question + 1);
+// if (!result.query)
+// {
+// set_url_error(&result, "Failed to allocate memory for query");
+// free(temp_url_t);
+// return result;
+// }
+// }
+// else
+// {
+// result.path = bk_strdup(temp_url);
+// if (!result.path)
+// {
+// set_url_error(&result, "Failed to allocate memory for path");
+// free(temp_url_t);
+// return result;
+// }
+// result.query = NULL;
+// }
+
+// free(temp_url_t);
+
+// return result;
+// }
+
+// // 生成 WebSocket 握手响应密钥
+// void generate_accept_key(const char *key, char *accept_key)
+// {
+// char combined[128];
+// strcpy(combined, key);
+// strcat(combined, WEBSOCKET_GUID);
+
+// unsigned char sha1_result[20];
+// mbedtls_sha1((const unsigned char *)combined, strlen(combined), sha1_result);
+
+// unsigned char encoded[64];
+// size_t olen;
+// mbedtls_base64_encode(encoded, sizeof(encoded), &olen, sha1_result, 16); // sizeof(sha1_result));
+// strncpy(accept_key, (const char *)encoded, olen);
+// accept_key[olen] = '\0';
+// }
+
+// // 生成随机的 WebSocket 密钥
+// void generate_websocket_key(char *key)
+// {
+// static const char charset[] =
+// "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+// for (int i = 0; i < WEBSOCKET_KEY_LEN - 1; ++i)
+// {
+// key[i] = charset[rand() % (sizeof(charset) - 1)];
+// }
+// key[WEBSOCKET_KEY_LEN - 1] = '\0';
+
+// unsigned char encoded[WEBSOCKET_KEY_LEN * 2];
+// size_t olen;
+// mbedtls_base64_encode(encoded, sizeof(encoded), &olen, (const unsigned char *)key, strlen(key));
+// printf("generate_websocket_key %s %d\r\n", encoded, olen);
+
+// if (olen >= (WEBSOCKET_KEY_LEN - 1))
+// olen = WEBSOCKET_KEY_LEN - 1;
+
+// strncpy(key, (const char *)encoded, olen);
+
+// key[olen] = '\0';
+// }
+
+// int send_handshake(WebSocketClient *client, const char *headers)
+// {
+// char key[WEBSOCKET_KEY_LEN];
+// printf("send_handshake1");
+// generate_websocket_key(key);
+// char accept_key[64];
+// int ret = 0;
+// generate_accept_key(key, accept_key);
+// printf("send_handshake2");
+// char *request = (char *)aic_zalloc(BUFFER_SIZE);
+// if (request == NULL)
+// {
+// printf("request aic_zalloc erro");
+// return -1;
+// }
+
+// if (headers != NULL)
+// {
+// snprintf(request, BUFFER_SIZE,
+// "GET /%s?%s HTTP/1.1\r\n"
+// "Host: %s:%d\r\n"
+// "Upgrade: websocket\r\n"
+// "Connection: Upgrade\r\n"
+// "Sec-WebSocket-Key: %s\r\n"
+// "Sec-WebSocket-Version: 13\r\n"
+// "Origin: http://%s:%d\r\n"
+// "%s\r\n"
+// "\r\n",
+// client->path, client->query, client->host, client->port, accept_key, client->host, client->port, headers);
+// }
+// else
+// {
+// snprintf(request, BUFFER_SIZE,
+// "GET /%s?%s HTTP/1.1\r\n"
+// "Host: %s:%d\r\n"
+// "Upgrade: websocket\r\n"
+// "Connection: Upgrade\r\n"
+// "Sec-WebSocket-Key: %s\r\n"
+// "Sec-WebSocket-Version: 13\r\n"
+// "Origin: http://%s:%d\r\n"
+// "\r\n",
+// client->path, client->query, client->host, client->port, accept_key, client->host, client->port);
+// }
+
+// printf("send_handshake request:\r\n%s", request);
+// if (send(client->sockfd, request, strlen(request), 0) < 0)
+// {
+// printf("send handshake failed");
+
+// ret = -1;
+// }
+
+// free(request);
+// printf("send_handshake3 %d", ret);
+// return ret;
+// }
+// WebSocketClient *init_websocket_client(const char *url, const char *headers, uint8_t use_dns)
+// {
+// // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client 1\n");
+// char *buffer = NULL;
+// WebSocketClient *client = NULL;
+// BK_URL parsed_url = {0};
+// struct sockaddr_in server_addr;
+// ssize_t bytes_received;
+// struct in_addr in_addr_t = {0};
+// ip_addr_t ip;
+// struct cat1_ws_init_t ip_sem = {0};
+
+// err_t ret = 0;
+
+// ip_sem.dns_done_sem = osSemaphoreNew(1, 0, NULL);
+
+// parsed_url = parse_url(url); // 解析url
+
+// if (parsed_url.error)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client Error: %s", parsed_url.error_message);
+// goto client_ERROR;
+// }
+// else
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Protocol: %s", parsed_url.protocol);
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Host: %s\n", parsed_url.host);
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Port: %d\n", parsed_url.port);
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Path: %s\n", parsed_url.path);
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Query: %s\n", parsed_url.query);
+// }
+// client = (WebSocketClient *)aic_zalloc(sizeof(WebSocketClient));
+// if (!client)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client alloc error");
+// goto client_ERROR;
+// }
+
+// client->sockfd = socket(AF_INET, SOCK_STREAM, 0);
+// if (client->sockfd < 0)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "socket creation failed %d \r\n", client->sockfd);
+// goto client_ERROR;
+// }
+
+// if (use_dns)
+// {
+// ret = dns_gethostbyname(parsed_url.host, &ip, dns_found, &ip_sem);
+// // if(ERR_OK==ret)
+// {
+// osSemaphoreAcquire(ip_sem.dns_done_sem, 1000 * 30);
+// }
+// osSemaphoreDelete(ip_sem.dns_done_sem);
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dns_gethostbyname %d\r\n", ip_sem.ip.u_addr.ip4);
+// memcpy(&server_addr.sin_addr.s_addr, &ip_sem.ip.u_addr.ip4, 4);
+// }
+// else
+// {
+// inet_pton(AF_INET, parsed_url.host, &server_addr.sin_addr);
+// }
+
+// server_addr.sin_family = AF_INET;
+
+// server_addr.sin_port = htons(parsed_url.port);
+
+// //
+
+// if (connect(client->sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client connection failed\r\n");
+// lwip_close(client->sockfd);
+// goto client_ERROR;
+// }
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "socket creation succ\r\n");
+
+// strcpy(client->host, parsed_url.host);
+// strcpy(client->path, parsed_url.path);
+// strcpy(client->query, parsed_url.query);
+// client->port = parsed_url.port;
+// if (send_handshake(client, headers) < 0)
+// { // 发送头
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "socket send_handshake client_ERROR");
+// lwip_close(client->sockfd);
+// goto client_ERROR;
+// }
+
+// buffer = (char *)aic_zalloc(BUFFER_SIZE);
+
+// if (buffer == NULL)
+// {
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "aic_zalloc(BUFFER_SIZE);null ");
+// goto client_ERROR;
+// }
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "recv: ??? ");
+
+// bytes_received = recv(client->sockfd, buffer, BUFFER_SIZE - 1, 0);
+
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "recv; ");
+
+// if (bytes_received > 0)
+// {
+// buffer[bytes_received] = '\0';
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received handshake response:\n%s\n", buffer);
+// }
+
+// if (strstr(buffer, "HTTP/1.1 101") == NULL)
+// {
+
+// goto client_ERROR;
+// }
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client end 1");
+// free(buffer);
+// free_url(parsed_url);
+
+// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "init_websocket_client end 2");
+// return client;
+// client_ERROR:
+// if (buffer)
+// {
+// free(buffer);
+// buffer = NULL;
+// }
+// if (client)
+// {
+// free(client);
+// client = NULL;
+// }
+// free_url(parsed_url);
+
+// return NULL;
+// }
\ No newline at end of file