mdf的router加websocket协议问题
Posted: Thu Jun 09, 2022 8:07 am
esp-mdf\examples\function_demo\mwifi\router组网没问题,esp-mdf\esp-idf\examples\protocols\websocket测试也没问题后,把websocket里边几个函数移植到router,在router的void tcp_client_read_task(void *arg)把socket_tcp_client_create屏蔽,增加websocket_app_start,结果连不上服务器,报错如下:
I (9436) [WEBSOCKET, 344]: Root obtains the IP address. It is posted by LwIP stack automatically
I (9446) [WEBSOCKET, 206]: TCP client read task is running
I (9452) WEBSOCKET: Connecting to ws://106.122.194.252.9999...
E (9482) TRANS_TCP: DNS lookup failed err=202 res=0x0
E (9483) TRANSPORT_WS: Error connecting to host 106.122.194.252.9999:80
E (9485) WEBSOCKET_CLIENT: Error transport connect
不知道哪里没配置好,还是修改函数没修改对,只是修改下边这个函数
void tcp_client_read_task(void *arg)
{
mdf_err_t ret = MDF_OK;
char *data = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
size_t size = MWIFI_PAYLOAD_LEN;
uint8_t dest_addr[MWIFI_ADDR_LEN] = {0x0};
mwifi_data_type_t data_type = {0x0};
cJSON *json_root = NULL;
cJSON *json_addr = NULL;
cJSON *json_group = NULL;
cJSON *json_data = NULL;
cJSON *json_dest_addr = NULL;
MDF_LOGI("TCP client read task is running");
while (mwifi_is_connected()) {
if (g_sockfd == -1) {
// g_sockfd = socket_tcp_client_create(CONFIG_SERVER_IP, CONFIG_SERVER_PORT);//屏蔽掉,改为下边
websocket_app_start(); //新增加
if (g_sockfd == -1) {
vTaskDelay(500 / portTICK_RATE_MS);
continue;
}
}
memset(data, 0, MWIFI_PAYLOAD_LEN);
ret = read(g_sockfd, data, size);
MDF_LOGD("TCP read, %d, size: %d, data: %s", g_sockfd, size, data);
if (ret <= 0) {
MDF_LOGW("<%s> TCP read", strerror(errno));
close(g_sockfd);
g_sockfd = -1;
continue;
}
json_root = cJSON_Parse(data);
MDF_ERROR_CONTINUE(!json_root, "cJSON_Parse, data format error");
/**
* @brief Check if it is a group address. If it is a group address, data_type.group = true.
*/
json_addr = cJSON_GetObjectItem(json_root, "dest_addr");
json_group = cJSON_GetObjectItem(json_root, "group");
if (json_addr) {
data_type.group = false;
json_dest_addr = json_addr;
} else if (json_group) {
data_type.group = true;
json_dest_addr = json_group;
} else {
MDF_LOGW("Address not found");
cJSON_Delete(json_root);
continue;
}
/**
* @brief Convert mac from string format to binary
*/
do {
uint32_t mac_data[MWIFI_ADDR_LEN] = {0};
sscanf(json_dest_addr->valuestring, MACSTR,
mac_data, mac_data + 1, mac_data + 2,
mac_data + 3, mac_data + 4, mac_data + 5);
for (int i = 0; i < MWIFI_ADDR_LEN; i++) {
dest_addr = mac_data;
}
} while (0);
json_data = cJSON_GetObjectItem(json_root, "data");
char *send_data = cJSON_PrintUnformatted(json_data);
ret = mwifi_write(dest_addr, &data_type, send_data, strlen(send_data), true);
MDF_ERROR_GOTO(ret != MDF_OK, FREE_MEM, "<%s> mwifi_root_write", mdf_err_to_name(ret));
FREE_MEM:
MDF_FREE(send_data);
cJSON_Delete(json_root);
}
MDF_LOGI("TCP client read task is exit");
close(g_sockfd);
g_sockfd = -1;
MDF_FREE(data);
vTaskDelete(NULL);
}
I (9436) [WEBSOCKET, 344]: Root obtains the IP address. It is posted by LwIP stack automatically
I (9446) [WEBSOCKET, 206]: TCP client read task is running
I (9452) WEBSOCKET: Connecting to ws://106.122.194.252.9999...
E (9482) TRANS_TCP: DNS lookup failed err=202 res=0x0
E (9483) TRANSPORT_WS: Error connecting to host 106.122.194.252.9999:80
E (9485) WEBSOCKET_CLIENT: Error transport connect
不知道哪里没配置好,还是修改函数没修改对,只是修改下边这个函数
void tcp_client_read_task(void *arg)
{
mdf_err_t ret = MDF_OK;
char *data = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
size_t size = MWIFI_PAYLOAD_LEN;
uint8_t dest_addr[MWIFI_ADDR_LEN] = {0x0};
mwifi_data_type_t data_type = {0x0};
cJSON *json_root = NULL;
cJSON *json_addr = NULL;
cJSON *json_group = NULL;
cJSON *json_data = NULL;
cJSON *json_dest_addr = NULL;
MDF_LOGI("TCP client read task is running");
while (mwifi_is_connected()) {
if (g_sockfd == -1) {
// g_sockfd = socket_tcp_client_create(CONFIG_SERVER_IP, CONFIG_SERVER_PORT);//屏蔽掉,改为下边
websocket_app_start(); //新增加
if (g_sockfd == -1) {
vTaskDelay(500 / portTICK_RATE_MS);
continue;
}
}
memset(data, 0, MWIFI_PAYLOAD_LEN);
ret = read(g_sockfd, data, size);
MDF_LOGD("TCP read, %d, size: %d, data: %s", g_sockfd, size, data);
if (ret <= 0) {
MDF_LOGW("<%s> TCP read", strerror(errno));
close(g_sockfd);
g_sockfd = -1;
continue;
}
json_root = cJSON_Parse(data);
MDF_ERROR_CONTINUE(!json_root, "cJSON_Parse, data format error");
/**
* @brief Check if it is a group address. If it is a group address, data_type.group = true.
*/
json_addr = cJSON_GetObjectItem(json_root, "dest_addr");
json_group = cJSON_GetObjectItem(json_root, "group");
if (json_addr) {
data_type.group = false;
json_dest_addr = json_addr;
} else if (json_group) {
data_type.group = true;
json_dest_addr = json_group;
} else {
MDF_LOGW("Address not found");
cJSON_Delete(json_root);
continue;
}
/**
* @brief Convert mac from string format to binary
*/
do {
uint32_t mac_data[MWIFI_ADDR_LEN] = {0};
sscanf(json_dest_addr->valuestring, MACSTR,
mac_data, mac_data + 1, mac_data + 2,
mac_data + 3, mac_data + 4, mac_data + 5);
for (int i = 0; i < MWIFI_ADDR_LEN; i++) {
dest_addr = mac_data;
}
} while (0);
json_data = cJSON_GetObjectItem(json_root, "data");
char *send_data = cJSON_PrintUnformatted(json_data);
ret = mwifi_write(dest_addr, &data_type, send_data, strlen(send_data), true);
MDF_ERROR_GOTO(ret != MDF_OK, FREE_MEM, "<%s> mwifi_root_write", mdf_err_to_name(ret));
FREE_MEM:
MDF_FREE(send_data);
cJSON_Delete(json_root);
}
MDF_LOGI("TCP client read task is exit");
close(g_sockfd);
g_sockfd = -1;
MDF_FREE(data);
vTaskDelete(NULL);
}