From: p15692025051 <846603105@qq.com> Date: Thu, 18 Sep 2025 09:45:20 +0000 (+0800) Subject: iot框架重构 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=5d5b27d9ae5cc1fb3eee6f1ec38b396061a81b19;p=mirrors%2FFX01.git iot框架重构 --- diff --git a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c index 3399c84..a440f87 100644 --- a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c +++ b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c @@ -81,7 +81,7 @@ static aos_thread_id AI_VC_send_task_id = 0; char AI_VC_send_task_flag = 0; static aos_thread_id AI_VC_read_task_id = 0; -static aos_thread_id AI_VC_read_IOT_id = 0; + volatile char AI_VC_read_task_flag = 0; static aos_queue_ptr AI_VC_send_queue = NULL; @@ -106,7 +106,7 @@ WebSocketClient* websocket_res[WEBSOCKET_MAX_NUM] = {0}; WebSocketClient *AI_client_VC = NULL; -WebSocketClient *AI_client_IOT = NULL; + static u32 close_timerout = 0; @@ -1049,121 +1049,7 @@ static void AI_VC_send_satrt(char is_greeting) cJSON_Delete(root); } -//{"cmd":"renew_device"} - -void deal_iot_receive(char *json_data) -{ - - bk_printf(json_data); - - // vc_cfg.VC_TTS_volume = 100; - // AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); - - if(strstr(json_data,"get_device_state")){ - - bk_printf("get_device_state"); - - IOT_update_statu(3,"",""); - - // AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); - - } - - if(strstr(json_data,"find_device")){ - - bk_printf("find_device"); - - AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); - - } - - if(strstr(json_data,"renew_device")){ - - bk_printf("renew_device"); - - IOT_update_bargein(); - - if(vc_cfg.VC_mode == 0) - { - AI_AMP_MSG_put("已切换到连续对话模式。",strlen("已切换到连续对话模式。"),VC_TTS_PLAY); - - //app_audio_set_master_volume(15*80/100); - - }else if(vc_cfg.VC_mode == 1) - { - AI_AMP_MSG_put("已切换到单对话模式。长按按钮说话,说完松开即可。",strlen("已切换到单对话模式。长按按钮说话,说完松开即可。"),VC_TTS_PLAY); - - // app_audio_set_master_volume(15*80/100); - }else - { - - // app_audio_set_master_volume(15*80/100); - AI_AMP_MSG_put("已切换到按键打断模式。我说话的时候需要短按按钮打断。",strlen("已切换到按键打断模式。我说话的时候需要短按按钮打断。"),VC_TTS_PLAY); - } - - save_VC_cfg(); - - } - - if(strstr(json_data,"switch_volume")) - { - - // bk_printf("switch_volume"); - - char *str_tmp = NULL; - - char volume_str[5]={0}; - - str_tmp = strstr(json_data,"\"target\":\""); - - //bk_printf("str_tmp:%p",str_tmp); - if(str_tmp)//解析password - { - str_tmp += strlen("\"target\":\""); - int count = str_count(str_tmp,'\"'); - bk_printf("count:%d",count); - if(count < sizeof(volume_str)) - { - - - memcpy(volume_str,str_tmp,count); - volume_str[count] = 0; - bk_printf("volume_str:%s",volume_str); - - int temp_vol=atoi(volume_str); - - if(temp_vol>=0&&temp_vol<=100) - { - - vc_cfg.VC_TTS_volume = temp_vol; - - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - - char tts_temp[200]={0}; - - - sprintf(tts_temp,"设置音量%d成功。",vc_cfg.VC_TTS_volume); - AI_AMP_MSG_put(tts_temp,strlen(tts_temp),VC_TTS_PLAY); - - - //is_save_VC_cfg = 1; - - save_VC_cfg(); - } - - - - } - else - { - bk_printf("获取volume失败"); - } - } - } - - -} //Audio.Create 开始发送音频 static void AI_VC_send_audio_create(void) { @@ -1492,13 +1378,7 @@ long getFileSize(char* filename) { u32 timer_ms22=0; - static void AI_VC_IOT_task(void *thread_input)//websocket 读线程 - { - - - receive_iot_message(AI_client_IOT); - } static void AI_VC_read_task(void *thread_input)//websocket 读线程 @@ -2350,233 +2230,7 @@ uint16_t modbus_crc16(const uint8_t *data, uint16_t length) { } -//Music.Start 开始发送音频 -void IOT_update_statu(int type,char *type_str,char *content) -{ - // VC_state = VC_State_SpeechStart;//开始说 - - - // 创建根对象 - cJSON *root = cJSON_CreateObject(); - if (root == NULL) { - bk_printf("Failed to create root object.\n"); - return ; - } - - cJSON *cmd_data = cJSON_CreateObject(); - - if (cmd_data == NULL) { - bk_printf("Failed to create payload object.\n"); - cJSON_Delete(root); - return ; - } - // 设置 "event" - - if(type==1){ - - cJSON_AddStringToObject(root, "cmd", "chat_state"); - - cJSON_AddStringToObject(cmd_data, "state", type_str); - - cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); - - } - - if(type==2){ - - long timestamp; - - timestamp = time(NULL); - bk_printf("2时间戳: %ld\n", (long)timestamp); - long long timestamp_ms = (long long)timestamp * 1000; - char timestamp_str[20] ={0}; - - // 使用 sprintf 将 long long 类型的时间戳转换为字符串 - int ret12 = sprintf(timestamp_str, "%lld", timestamp_ms); - timestamp_str[ret12] =0; - // 打印转换后的字符串形式的时间戳 - bk_printf("2时间戳(ms 转换为字符串): %s\n", timestamp_str); - - cJSON_AddStringToObject(root, "cmd", "chat_text"); - - cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); - - cJSON_AddStringToObject(cmd_data, "type", type_str); - - cJSON_AddStringToObject(cmd_data, "text", content); - - cJSON_AddStringToObject(cmd_data, "createTime", timestamp_str); - - - } - - - if(type==3){ - - char tmp_bat[20]={0},tmp_sig[20]={0},tmp_vol[20]={0},tmp_bargein[20]={0}; - - cJSON_AddStringToObject(root, "cmd", "device_info"); - - cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); - - ts_radio_signal_info_t p_signal_info; - int32_t ret = 0; - - ret = aic_srv_tele_radio_get_signal_info_sync(current_card_id, &p_signal_info); - - // p_signal_info.signal_level_val - - struct psy_info psy_info; - get_psy_info(&psy_info); - - // psy_info.bat_capacity - - sprintf(tmp_bat,"%d",psy_info.bat_capacity); - - sprintf(tmp_sig,"%d",p_signal_info.signal_level_val); - - sprintf(tmp_vol,"%d",vc_cfg.VC_TTS_volume); - - sprintf(tmp_bargein,"%d",vc_cfg.VC_mode); - - cJSON_AddStringToObject(cmd_data, "battery", tmp_bat); - - cJSON_AddStringToObject(cmd_data, "wifi_rssi", tmp_sig); - - cJSON_AddStringToObject(cmd_data, "volume", tmp_vol); - - cJSON_AddStringToObject(cmd_data, "current_role_name", "fx01"); - - cJSON_AddStringToObject(cmd_data, "version", "V1.2"); - - cJSON_AddStringToObject(cmd_data, "bargein", tmp_bargein); - - // bargein - - - - } - - - // 将 "payload" 对象添加到根对象中 - - cJSON_AddItemToObject(root, "data", cmd_data); - - - - // 将 JSON 对象转换为字符串 - - //cJSON_PrintUnformatted - - char *json_str = cJSON_PrintUnformatted(root); //cJSON_Print - if (json_str == NULL) { - bk_printf("Failed to print JSON object.\n"); - cJSON_Delete(root); - - } - - - bk_printf(json_str); - - unsigned char frame[500]={0}; - - frame[0]=0xFF; - frame[1]=0xFE; - frame[2]=0x03; - // AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - - short temp=strlen(json_str); - memcpy(frame+3,&temp,2); - - memcpy(frame+5,json_str,strlen(json_str)); - - uint16_t crc = modbus_crc16(json_str,strlen(json_str)); - - uint8_t crc_low = crc & 0xFF; - - uint8_t crc_high = (crc >> 8) & 0xFF; - - frame[5+strlen(json_str)]=crc_low; - - frame[6+strlen(json_str)]=crc_high; - - bk_printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); - - bk_printf("the send str is %d ",7+strlen(json_str)); - - send_iot_message(AI_client_IOT,frame,7+strlen(json_str)); - - // 释放内存 - free(json_str); - cJSON_Delete(root); -} - - -void IOT_heart(){ - - unsigned char frame[10]={0}; - - frame[0]=0xFF; - frame[1]=0xFE; - frame[2]=0x02; - - int ret=send_iot_message(AI_client_IOT,frame,3); - - if(ret==-1&&AI_client_IOT!=NULL){ - - bk_printf("发送IOT消息失败"); - close(AI_client_IOT->sockfd); - - AI_client_IOT=NULL; - - } - -} - -// #include -// #include - -// int main() { -// uint8_t data[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01}; -// uint16_t length = sizeof(data) / sizeof(data[0]); -// uint16_t crc = modbus_crc16(data, length); -// uint8_t crc_low = crc & 0xFF; -// uint8_t crc_high = (crc >> 8) & 0xFF; -// printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); -// return 0; -// } - -static void IOT_login(int type) -{ - unsigned char frame[200]={0}; - - //0xFFFE - frame[0]=0xFF; - frame[1]=0xFE; - frame[2]=0x00; - memcpy(frame+3,vc_cfg.dev_imei,strlen(vc_cfg.dev_imei)); - - memcpy(frame+23,get_token(),strlen(get_token())); - - uint16_t crc = modbus_crc16(get_token(),strlen(get_token())); - - uint8_t crc_low = crc & 0xFF; - - uint8_t crc_high = (crc >> 8) & 0xFF; - - frame[59]=crc_low; - - frame[60]=crc_high; - - bk_printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); - - send_iot_message(AI_client_IOT,frame,61); - -} - - -// #define IOT //连接websocket static int websocket_connect_VC(void) { @@ -2619,60 +2273,10 @@ static int websocket_connect_VC(void) } - char *url_socket="iot.socket.thisiot.com:60501"; + AI_client_VC = init_websocket_client(url,NULL); -#ifdef IOT - if(AI_client_IOT==NULL){ - bk_printf("create AI_client_IOT...\n"); - AI_client_IOT = init_websocket_IOT(url_socket,NULL); - - - - if(AI_client_IOT){ - bk_printf("IOT读线程 AI_VC_read_task_id:%d",AI_VC_read_IOT_id); - //创建读线程 - if(AI_VC_read_IOT_id == 0) - { - AI_VC_read_IOT_id = aos_create_thread( - "AI_VC_read", - AI_VC_IOT_task, - &AI_VC_read_IOT_id, - 4096u, - 0, - NULL, - ai_VC_task_priority, - AOS_AUTO_START); - bk_printf("IOT读线程 AI_VC_read_task_id:%d",AI_VC_read_IOT_id); - } - - aos_sleep(100); - - IOT_login(1); - - aos_sleep(200); - - // char *iotmessage="{\"cmd\":\"get_bot\",\"data\":\"\"}"; - - IOT_update_statu(1,"idle",""); - - aos_sleep(200); - IOT_update_statu(2,"res","12312312"); - - aos_sleep(200); - IOT_update_statu(3,"",""); - - aos_sleep(200); - - IOT_heart(); - - // send_iot_message(AI_client_IOT,iotmessage,strlen(iotmessage)); - - } - - } - #endif if(!AI_client_VC) { @@ -3695,11 +3299,10 @@ static void sys_60S_timeout(void *param) { // AI_AMP_MSG_put(NULL,0,AI_TIMER_EVENT); - if(AI_client_IOT!=NULL){ - IOT_heart(); - } + IOT_heart(); + } @@ -3941,8 +3544,9 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 } #endif - - +#if BK_SOCKET_IOT + bk_iot_init();//iot初始化 +#endif aic_network_enable_pdp_nic_capdump();//打开网络调试log // while(1) @@ -4112,7 +3716,7 @@ int AI_AMP_2_5_init(void) sizeof(AI_AMP_MSG_queue_t), 200); } - bk_key_init(); + AI_AMP_task_id = aos_create_thread( "AI_AMP_2_5", AI_AMP_2_5_task, @@ -4122,7 +3726,8 @@ int AI_AMP_2_5_init(void) NULL, ai_VC_task_priority, AOS_AUTO_START); - + bk_key_init(); + return AIC_SUCCESS; } diff --git a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.h b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.h index a4ac9e2..4eb7bb9 100644 --- a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.h +++ b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.h @@ -1,6 +1,9 @@ #include "aic_osa.h" #define SUPPORT_OPUS 1 + +#define BK_SOCKET_IOT 1 + #define BK_LOG_NONE 0//不打印 #define BK_LOG_UART 1//串口打印 #define BK_LOG_USB 2//USB打印 @@ -121,6 +124,7 @@ typedef struct char *data; u32 len; }AI_VC_audio_queue_t; +uint16_t modbus_crc16(const uint8_t *data, uint16_t length) ; int AI_VC_start(char is_greeting); void AI_VC_Close(void); @@ -134,6 +138,4 @@ int save_VC_cfg(void); int play_audio_file(char *file_name); void AI_VC_put_play_data(char *data,u32 len);//音频数据入队播放 -void IOT_update_statu(int type,char *type_str,char *content); - -void IOT_heart(); \ No newline at end of file +int bk_get_iccid(char *iccid); \ No newline at end of file diff --git a/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt b/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt index 0ed5dfa..ccbf66c 100644 --- a/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt +++ b/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt @@ -11,6 +11,7 @@ aic_library_sources_ifdef( ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/bk_key.c ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/dev_authentication.c ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/bk_opus_coder.c + ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/bk_iot.c ) diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_iot.c b/2.code/aicos/app/AI_AMP_2_5/bk_iot.c new file mode 100644 index 0000000..c6173f9 --- /dev/null +++ b/2.code/aicos/app/AI_AMP_2_5/bk_iot.c @@ -0,0 +1,662 @@ +#include "bk_iot.h" +#include "AI_AMP_2_5.h" +#include "bk_websocket.h" +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include "cJSON.h" +#include "aic_srv_tele_radio.h" +#include "power_supply.h" +#include "dev_authentication.h" +#include "app_audio.h" +#include "aic_srv_tele_ex.h" +#if BK_SOCKET_IOT +static aos_timer_ptr IOT_timer = NULL; +static aos_thread_id AI_VC_read_IOT_id = 0; +int AI_client_IOT = 0; +void deal_iot_receive(char *json_data); +static aos_queue_ptr IOT_task_queue = NULL; +static aos_thread_id IOT_task_id = 0; + +int send_iot_message( const unsigned char *message, int message_len) ; + +#define url_socket "iot.socket.thisiot.com" +#define iot_oprt "60501" + +int init_websocket_IOT(const char *url, const char *port_str) { + bk_printf("init_websocket_client 1\n"); + // char *buffer = NULL; + int client_id =0; + + + struct addrinfo hints, *addr_list, *cur; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + //if(getaddrinfo( parsed_url.host, parsed_url.port_str, &hints, &addr_list ) != 0 ) + + //iot.socket.thisiot.com:60501 + + bk_printf("init_websocket_client 2\n"); + + if( getaddrinfo(url, port_str, &hints, &addr_list ) != 0 ) + { + bk_printf("init_websocket_IOT getaddrinfo error"); + goto client_ERROR; + } + + bk_printf("init_websocket_client 3\n"); + + for( cur = addr_list; cur != NULL; cur = cur->ai_next ) + { + client_id = (int) socket( cur->ai_family, cur->ai_socktype, + cur->ai_protocol ); + if( client_id < 0 ) + { + continue; + } + + bk_printf("init_websocket_client 31\n"); + if(connect( client_id, cur->ai_addr, (int)cur->ai_addrlen ) == 0 ) + { + + break; + } + + bk_printf("init_websocket_client 32\n"); + close( client_id ); + + + } + + bk_printf("init_websocket_client 4\n"); + + freeaddrinfo(addr_list); // 释放资源 + if(client_id<0) + { + bk_printf("init_websocket_IOT connect error"); + goto client_ERROR; + } + + bk_printf("init_websocket_IOT connect ok,sockfd:%d",client_id); + + bk_printf("init_websocket_client 5\n"); + return client_id; +client_ERROR: + //if(buffer) + // aic_free(buffer); + + // free_url(parsed_url); + return 0; +} +unsigned char frame[200]={0}; +unsigned char json_data[200]={0}; +static void AI_VC_IOT_read_task(void *thread_input)//websocket 读线程 +{ + + int bytes_received = 0; + + while(1) + { + if(AI_client_IOT>0) + { + bytes_received = recv(AI_client_IOT, frame, 200, 0); + if(bytes_received>0){ + + bk_printf("get iot message................................."); + bk_printf("bytes_received %02X %02X %02X %02X %02X %02X %02X %02X %02X ...\n",frame[0],frame[1],frame[2],frame[3],frame[4],frame[5],frame[6],frame[7],frame[8]); + + if(frame[2]==0x03) + { + + int length_data=frame[3]; + + memset(json_data,0,200); + memcpy(json_data,frame+5,length_data); + bk_printf("length %d,json_data %s ...\n",length_data,json_data); + deal_iot_receive(json_data); + + } + + }else//断线了,要重连,也要防止没网络导致异常。重连没实现 + { + bk_iot_disconnect(); + aos_sleep(100); + break; + } + }else//没初始化 + { + bk_iot_disconnect(); + aos_sleep(1000); + break; + } + + // if(bytes_received<0) + // break; + + memset(frame,0,200); + //aos_sleep(200); + + } +} +//连接socket iot服务器 +int bk_iot_connect(void) +{ + if(AI_client_IOT==0 && aic_is_default_pdp_activated() == TRUE)//必须联网成功 + { + bk_printf("create AI_client_IOT...\n"); + AI_client_IOT = init_websocket_IOT(url_socket,iot_oprt); + bk_printf("IOT读线程 AI_VC_read_task_id:%d",AI_VC_read_IOT_id); + //创建读线程 + if(AI_VC_read_IOT_id != 0) + { + aos_delete_thread(AI_VC_read_IOT_id); + AI_VC_read_IOT_id = 0; + } + AI_VC_read_IOT_id = aos_create_thread( + "AI_VC_read", + AI_VC_IOT_read_task, + &AI_VC_read_IOT_id, + 4096u, + 0, + NULL, + 35, + AOS_AUTO_START); + bk_printf("IOT读线程 AI_VC_read_task_id:%d",AI_VC_read_IOT_id); + + } + else + return -1; + return 0; +} +//断开iot服务器 +void bk_iot_disconnect(void) +{ + if(AI_client_IOT>0) + soc_close(AI_client_IOT); + AI_client_IOT=0; + if(AI_VC_read_IOT_id != 0) + { + aos_delete_thread(AI_VC_read_IOT_id); + AI_VC_read_IOT_id = 0; + } +} +//{"cmd":"renew_device"} +//解析返回数据 +void deal_iot_receive(char *json_data) +{ + + bk_printf(json_data); + + // vc_cfg.VC_TTS_volume = 100; + // AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); + + if(strstr(json_data,"get_device_state")){ + + bk_printf("get_device_state"); + + IOT_update_statu(3,"",""); + + // AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + + } + + if(strstr(json_data,"find_device")){ + + bk_printf("find_device"); + + AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + + } + + if(strstr(json_data,"renew_device")){ + + bk_printf("renew_device"); + + IOT_update_bargein(); + + if(vc_cfg.VC_mode == 0) + { + AI_AMP_MSG_put("已切换到连续对话模式。",strlen("已切换到连续对话模式。"),VC_TTS_PLAY); + + //app_audio_set_master_volume(15*80/100); + + }else if(vc_cfg.VC_mode == 1) + { + AI_AMP_MSG_put("已切换到单对话模式。长按按钮说话,说完松开即可。",strlen("已切换到单对话模式。长按按钮说话,说完松开即可。"),VC_TTS_PLAY); + + // app_audio_set_master_volume(15*80/100); + }else + { + + // app_audio_set_master_volume(15*80/100); + AI_AMP_MSG_put("已切换到按键打断模式。我说话的时候需要短按按钮打断。",strlen("已切换到按键打断模式。我说话的时候需要短按按钮打断。"),VC_TTS_PLAY); + } + + save_VC_cfg(); + + } + + if(strstr(json_data,"switch_volume")) + { + + // bk_printf("switch_volume"); + + char *str_tmp = NULL; + + char volume_str[5]={0}; + + str_tmp = strstr(json_data,"\"target\":\""); + + //bk_printf("str_tmp:%p",str_tmp); + if(str_tmp)//解析password + { + str_tmp += strlen("\"target\":\""); + int count = str_count(str_tmp,'\"'); + bk_printf("count:%d",count); + if(count < sizeof(volume_str)) + { + + + memcpy(volume_str,str_tmp,count); + volume_str[count] = 0; + bk_printf("volume_str:%s",volume_str); + + int temp_vol=atoi(volume_str); + + if(temp_vol>=0&&temp_vol<=100) + { + + vc_cfg.VC_TTS_volume = temp_vol; + + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + + char tts_temp[200]={0}; + + + sprintf(tts_temp,"设置音量%d成功。",vc_cfg.VC_TTS_volume); + + AI_AMP_MSG_put(tts_temp,strlen(tts_temp),VC_TTS_PLAY); + + + //is_save_VC_cfg = 1; + + save_VC_cfg(); + } + + + + } + else + { + bk_printf("获取volume失败"); + } + } + } + + +} + +// 发送 iot数据 消息 +int send_iot_message( const unsigned char *message, int message_len) +{ + return IOT_MSG_put(message,message_len,WRITE_DATA); +} + + + +// +void IOT_update_statu(int type,char *type_str,char *content) +{ + // VC_state = VC_State_SpeechStart;//开始说 + + + // 创建根对象 + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf("Failed to create root object.\n"); + return ; + } + + cJSON *cmd_data = cJSON_CreateObject(); + + if (cmd_data == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; + } + // 设置 "event" + + if(type==1){ + + cJSON_AddStringToObject(root, "cmd", "chat_state"); + + cJSON_AddStringToObject(cmd_data, "state", type_str); + + cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); + + } + + if(type==2){ + + long timestamp; + + timestamp = time(NULL); + bk_printf("2时间戳: %ld\n", (long)timestamp); + long long timestamp_ms = (long long)timestamp * 1000; + char timestamp_str[20] ={0}; + + // 使用 sprintf 将 long long 类型的时间戳转换为字符串 + int ret12 = sprintf(timestamp_str, "%lld", timestamp_ms); + timestamp_str[ret12] =0; + // 打印转换后的字符串形式的时间戳 + bk_printf("2时间戳(ms 转换为字符串): %s\n", timestamp_str); + + cJSON_AddStringToObject(root, "cmd", "chat_text"); + + cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); + + cJSON_AddStringToObject(cmd_data, "type", type_str); + + cJSON_AddStringToObject(cmd_data, "text", content); + + cJSON_AddStringToObject(cmd_data, "createTime", timestamp_str); + + + } + + + if(type==3){ + + char tmp_bat[20]={0},tmp_sig[20]={0},tmp_vol[20]={0},tmp_bargein[20]={0}; + + cJSON_AddStringToObject(root, "cmd", "device_info"); + + cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); + + ts_radio_signal_info_t p_signal_info; + int32_t ret = 0; + int current_card_id_t = aic_srv_tele_sim_get_current_card_sync(); + ret = aic_srv_tele_radio_get_signal_info_sync(current_card_id_t, &p_signal_info); + + // p_signal_info.signal_level_val + + struct psy_info psy_info; + get_psy_info(&psy_info); + + // psy_info.bat_capacity + + sprintf(tmp_bat,"%d",psy_info.bat_capacity); + + sprintf(tmp_sig,"%d",p_signal_info.signal_level_val); + + sprintf(tmp_vol,"%d",vc_cfg.VC_TTS_volume); + + sprintf(tmp_bargein,"%d",vc_cfg.VC_mode); + + cJSON_AddStringToObject(cmd_data, "battery", tmp_bat); + + cJSON_AddStringToObject(cmd_data, "wifi_rssi", tmp_sig); + + cJSON_AddStringToObject(cmd_data, "volume", tmp_vol); + + cJSON_AddStringToObject(cmd_data, "current_role_name", "fx01"); + + cJSON_AddStringToObject(cmd_data, "version", "V1.2"); + + cJSON_AddStringToObject(cmd_data, "bargein", tmp_bargein); + + // bargein + + + + } + + + // 将 "payload" 对象添加到根对象中 + + cJSON_AddItemToObject(root, "data", cmd_data); + + + + // 将 JSON 对象转换为字符串 + + //cJSON_PrintUnformatted + + char *json_str = cJSON_PrintUnformatted(root); //cJSON_Print + if (json_str == NULL) { + bk_printf("Failed to print JSON object.\n"); + cJSON_Delete(root); + + } + + + bk_printf(json_str); + + unsigned char frame[500]={0}; + + frame[0]=0xFF; + frame[1]=0xFE; + frame[2]=0x03; + // AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 + + short temp=strlen(json_str); + memcpy(frame+3,&temp,2); + + memcpy(frame+5,json_str,strlen(json_str)); + + uint16_t crc = modbus_crc16(json_str,strlen(json_str)); + + uint8_t crc_low = crc & 0xFF; + + uint8_t crc_high = (crc >> 8) & 0xFF; + + frame[5+strlen(json_str)]=crc_low; + + frame[6+strlen(json_str)]=crc_high; + + bk_printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); + + bk_printf("the send str is %d ",7+strlen(json_str)); + + send_iot_message(frame,7+strlen(json_str)); + + // 释放内存 + free(json_str); + cJSON_Delete(root); +} + +//iot心跳 +void IOT_heart(){ + + unsigned char frame[10]={0}; + + frame[0]=0xFF; + frame[1]=0xFE; + frame[2]=0x02; + + send_iot_message(frame,3); + + + +} + +// #include +// #include + +// int main() { +// uint8_t data[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01}; +// uint16_t length = sizeof(data) / sizeof(data[0]); +// uint16_t crc = modbus_crc16(data, length); +// uint8_t crc_low = crc & 0xFF; +// uint8_t crc_high = (crc >> 8) & 0xFF; +// printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); +// return 0; +// } +//登录 +static void IOT_login(int type) +{ + unsigned char frame[200]={0}; + + //0xFFFE + frame[0]=0xFF; + frame[1]=0xFE; + frame[2]=0x00; + + memcpy(frame+3,vc_cfg.dev_imei,strlen(vc_cfg.dev_imei)); + + memcpy(frame+23,get_token(),strlen(get_token())); + + uint16_t crc = modbus_crc16(get_token(),strlen(get_token())); + + uint8_t crc_low = crc & 0xFF; + + uint8_t crc_high = (crc >> 8) & 0xFF; + + frame[59]=crc_low; + + frame[60]=crc_high; + + bk_printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); + + send_iot_message(frame,61); + +} + +aos_status IOT_MSG_put(const char *data,u32 len,IOT_MSG_Type MSG_Type) +{ + IOT_MSG_queue_t IOT_MSG_queue_data = {0}; + //bk_msg_printf("IOT_MSG_put1 MSG_Type:%d",MSG_Type); + if(len >0) + { + IOT_MSG_queue_data.data = aic_malloc(len+1); + memset(IOT_MSG_queue_data.data ,0,len+1); + if(!IOT_MSG_queue_data.data) + { + //bk_msg_printf("AI_VC_send_data aic_malloc ERROR!!!!!!!!!!"); + return AOS_ERROR; + } + memcpy(IOT_MSG_queue_data.data,data,len); + IOT_MSG_queue_data.len=len; + } + + IOT_MSG_queue_data.MSG_Type=MSG_Type; + if(IOT_task_queue && AOS_SUCCESS!=aos_send_to_queue(IOT_task_queue,&IOT_MSG_queue_data,AOS_NO_WAIT)) + { + bk_printf("IOT_MSG_put :aos_send_to_queue ERROR\n\n"); + if(!IOT_MSG_queue_data.data) + aic_free(IOT_MSG_queue_data.data); + return AOS_ERROR; + } + //bk_msg_printf("IOT_MSG_put2 MSG_Type:%d",MSG_Type); + return AOS_SUCCESS; +} +static void IOT_task(void *thread_input)//做逻辑线程 +{ + while (1) { + if(IOT_task_queue) + { + IOT_MSG_queue_t IOT_msg = {0}; + aos_recv_from_queue(IOT_task_queue,&IOT_msg,AOS_WAIT_FOREVER) ; + switch (IOT_msg.MSG_Type) + { + + case WRITE_DATA://发送数据,可能会阻塞 + if(AI_client_IOT>0) + send(AI_client_IOT, IOT_msg.data, IOT_msg.len, 0) ; + break; + case RECONNECT://重连服务器 + bk_iot_disconnect(); + bk_iot_connect(); + break; + case IOT_HEART://心跳 + IOT_heart(); + break; + + default: + break; + } + if(IOT_msg.data) + aic_free(IOT_msg.data); + }else + { + aos_sleep(10); + } + + + } +} +static void IOT_timeout(void *param)//5分钟一次 +{ + if(AI_client_IOT<=0)//掉线重连 + { + IOT_MSG_put(NULL,0,RECONNECT); + } + IOT_MSG_put(NULL,0,IOT_HEART);//发送心跳 +} +int bk_iot_init(void) +{ + if(IOT_timer == NULL) + { + IOT_timer = aos_create_timer("IOT_timer", + IOT_timeout, &IOT_timer, + 60*5*1000, + AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); + aic_assert(IOT_timer); + } + //创建IOT_task队列 + if(IOT_task_queue == NULL) + { + IOT_task_queue = aos_create_queue("IOT_task_queue", + sizeof(IOT_MSG_queue_t), + 200); + } + + IOT_task_id = aos_create_thread( + "IOT_task_id", + IOT_task, + &IOT_task_id, + 4096*2, + 0, + NULL, + 35, + AOS_AUTO_START); + if(bk_iot_connect()!=0) + { + return -1; + + } + IOT_login(1); + + // char *iotmessage="{\"cmd\":\"get_bot\",\"data\":\"\"}"; + + IOT_update_statu(1,"idle",""); + + IOT_update_statu(2,"res","12312312"); + + IOT_update_statu(3,"",""); + + IOT_heart(); + return 0; +} + +#endif \ No newline at end of file diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_iot.h b/2.code/aicos/app/AI_AMP_2_5/bk_iot.h new file mode 100644 index 0000000..65a9d4b --- /dev/null +++ b/2.code/aicos/app/AI_AMP_2_5/bk_iot.h @@ -0,0 +1,23 @@ +#ifndef __BK_IOT_H__ +#define __BK_IOT_H__ +#include "aic_osa.h" +typedef enum IOT_MSG +{ + WRITE_DATA,//发送数据 + RECONNECT,//重连 + IOT_HEART, + +} IOT_MSG_Type; +typedef struct +{ + char *data; + u32 len; + IOT_MSG_Type MSG_Type; +}IOT_MSG_queue_t; +static void IOT_login(int type); +void bk_iot_disconnect(void); +int bk_iot_connect(void); +void IOT_heart(); +void IOT_update_statu(int type,char *type_str,char *content); +aos_status IOT_MSG_put(const char *data,u32 len,IOT_MSG_Type MSG_Type); +#endif diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_websocket.c b/2.code/aicos/app/AI_AMP_2_5/bk_websocket.c index 93e3cd6..2b9812e 100644 --- a/2.code/aicos/app/AI_AMP_2_5/bk_websocket.c +++ b/2.code/aicos/app/AI_AMP_2_5/bk_websocket.c @@ -429,120 +429,10 @@ send_websocket_message_ERROR: } -// 发送 WebSocket 消息 -int send_iot_message(WebSocketClient *client, const unsigned char *message, size_t message_len) -{ - if(!client) - return -1; - unsigned char *frame=aic_zalloc(message_len + 50); - int offset = 0; - int ret = -1; - if(!frame) - { - bk_printf("send_websocket_message zalloc error"); - return -1; - } - - for (size_t i = 0; i < message_len; ++i) { - frame[offset + i] = message[i] ; - } - offset += message_len; - - // audio_data(message,message_len,27); - // 发送帧 - if (send(client->sockfd, frame, offset, 0) < 0) { - bk_printf("send message failed"); - ret = -1; - goto send_websocket_message_ERROR; - } - bk_printf("send message success"); - ret = 0; -send_websocket_message_ERROR: - aic_free(frame); - bk_sleep(message_len/1000+1); - return ret; -} -// 接收 WebSocket 消息,返回读到长度 -// char websocket_tmp_b[8*1024] = {0}; -// int websocket_tmp_b_len = 0; -// char frame_t[20] = {0}; -// int frame_t_len = 0; -// int bytes_received_t_1 = 0; -// int bytes_received_t = 0; -void deal_iot_receive(char *json); -int receive_iot_message(WebSocketClient *client) - { - - if(!client ) - return -1; - unsigned char frame[200]={0}; - - unsigned char json_data[200]={0}; - unsigned char masking_key[4]; - int bytes_received = 0; - int recv_len = 0 ; - static u8 last_byte = 0; - static u32 error_count = 0; - // while(1) - // { - // recv_len = recv(client->sockfd, frame+bytes_received, 2-bytes_received, 0); - // bytes_received +=recv_len; - // if(bytes_received>=2) - // break; - // } - - while(1) - { - bytes_received = recv(client->sockfd, frame, 200, 0); - - if(bytes_received>0){ - - bk_printf("get iot message................................."); - bk_printf("bytes_received %02X %02X %02X %02X %02X %02X %02X %02X %02X ...\n",frame[0],frame[1],frame[2],frame[3],frame[4],frame[5],frame[6],frame[7],frame[8]); - - if(frame[2]==0x03){ - - int length_data=frame[3]; - - memset(json_data,0,200); - memcpy(json_data,frame+5,length_data); - bk_printf("length %d,json_data %s ...\n",length_data,json_data); - - // cJSON *root = cJSON_CreateString(json_data); - - //cJSON *cmd_obj=cJSON_GetObjectItem(root,"cmd"); - - //bk_printf(cJSON_GetStringValue(cmd_obj)); - - //switch_volume - - - - deal_iot_receive(json_data); - - - - // cJSON_GetStringValue - - } - - } - - if(bytes_received<0) - break; - - memset(frame,0,200); - aos_sleep(200); - - } - - bk_printf("AI_VC_IOT_task exit...\n"); - -} int receive_websocket_message(WebSocketClient *client, unsigned char *buf,int len,WebSocketMessageType *code) { @@ -893,118 +783,7 @@ client_ERROR: } -WebSocketClient *init_websocket_IOT(const char *url, const char *headers) { - bk_printf("init_websocket_client 1\n"); - // char *buffer = NULL; - WebSocketClient *client =NULL; - // BK_URL parsed_url = {0}; - // parsed_url = parse_url(url);//解析url - //bk_printf("url: %s", url); - // if (parsed_url.error) { - // bk_printf("Error: %s", parsed_url.error_message); - // goto client_ERROR; - // } else { - // bk_printf("Protocol: %s", parsed_url.protocol); - // bk_printf("Host: %s", parsed_url.host); - // bk_printf("Port: %d", parsed_url.port); - // bk_printf("Path: %s", parsed_url.path); - // bk_printf("Query: %s", parsed_url.query); - - // } - - - client = aic_zalloc(sizeof(WebSocketClient)) ; - if(!client) - { - bk_printf("init_websocket_IOT alloc error"); - goto client_ERROR; - } - - struct addrinfo hints, *addr_list, *cur; - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - //if(getaddrinfo( parsed_url.host, parsed_url.port_str, &hints, &addr_list ) != 0 ) - - //iot.socket.thisiot.com:60501 - - bk_printf("init_websocket_client 2\n"); - - if( getaddrinfo("iot.socket.thisiot.com", "7777", &hints, &addr_list ) != 0 ) - { - bk_printf("init_websocket_IOT getaddrinfo error"); - goto client_ERROR; - } - - bk_printf("init_websocket_client 3\n"); - for( cur = addr_list; cur != NULL; cur = cur->ai_next ) - { - client->sockfd = (int) socket( cur->ai_family, cur->ai_socktype, - cur->ai_protocol ); - if( client->sockfd < 0 ) - { - continue; - } - - bk_printf("init_websocket_client 31\n"); - if(connect( client->sockfd , cur->ai_addr, (int)cur->ai_addrlen ) == 0 ) - { - - break; - } - - bk_printf("init_websocket_client 32\n"); - close( client->sockfd ); - - - } - - bk_printf("init_websocket_client 4\n"); - - freeaddrinfo(addr_list); // 释放资源 - if(client->sockfd<0) - { - bk_printf("init_websocket_IOT connect error"); - goto client_ERROR; - } - - bk_printf("init_websocket_IOT connect ok,sockfd:%d",client->sockfd); - - -// client->sockfd = socket(AF_INET, SOCK_STREAM, 0); -// if (client->sockfd < 0) { -// bk_printf("socket creation failed"); -// goto client_ERROR; -// } - -// // bk_printf("init_websocket_client 3\n"); -// struct sockaddr_in server_addr; -// server_addr.sin_family = AF_INET; -// server_addr.sin_port = htons(parsed_url.port); -// inet_pton(AF_INET, parsed_url.host, &server_addr.sin_addr); - -// if (connect(client->sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { -// bk_printf("connection failed"); -// close(client->sockfd); -// goto client_ERROR; -// } - - - - // aic_free(buffer); - // free_url(parsed_url); - - bk_printf("init_websocket_client 5\n"); - return client; -client_ERROR: - //if(buffer) - // aic_free(buffer); - if(client) - aic_free(client); - // free_url(parsed_url); - return NULL; -} // 关闭 WebSocket 客户端 void close_websocket_client(WebSocketClient *client) { if(client) diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_websocket.h b/2.code/aicos/app/AI_AMP_2_5/bk_websocket.h index 4bdf4b7..2252024 100644 --- a/2.code/aicos/app/AI_AMP_2_5/bk_websocket.h +++ b/2.code/aicos/app/AI_AMP_2_5/bk_websocket.h @@ -24,7 +24,7 @@ typedef struct { // 初始化 WebSocket 客户端 WebSocketClient *init_websocket_client(const char *url, const char *headers); -WebSocketClient *init_websocket_IOT(const char *url, const char *headers); + // 关闭 WebSocket 客户端 void close_websocket_client(WebSocketClient *client) ; @@ -37,7 +37,6 @@ int receive_iot_message(WebSocketClient *client); // 发送 WebSocket 消息 int send_websocket_message(WebSocketClient *client, const unsigned char *message, size_t message_len, WebSocketMessageType type) ; -int send_iot_message(WebSocketClient *client, const unsigned char *message, size_t message_len); int bk_websocket_demo(void); #endif diff --git a/2.code/aicos/app/AI_AMP_2_5/dev_authentication.c b/2.code/aicos/app/AI_AMP_2_5/dev_authentication.c index d5b2a5c..9316274 100644 --- a/2.code/aicos/app/AI_AMP_2_5/dev_authentication.c +++ b/2.code/aicos/app/AI_AMP_2_5/dev_authentication.c @@ -523,7 +523,7 @@ void update_iccid(void)//设备激活 u32 aut_active_http_handle = 0; char *aut_buf =NULL; char *json_string =NULL; - char form_data_string[200]={0}; + char sign_url[200]={0}; @@ -1123,13 +1123,12 @@ void ota_update_version(void)//OTA上报 //char dev_pass_base64[100] ={0}; // char enc_iv[65] = {0}; - char dev_iccid[100] ={0}; + // http创建客户端句柄 u32 aut_active_http_handle = 0; char *aut_buf =NULL; char *json_string =NULL; - char form_data_string[200]={0}; char sign_url[200]={0}; diff --git a/2.code/aicos/app/AI_AMP_2_5/dev_authentication.h b/2.code/aicos/app/AI_AMP_2_5/dev_authentication.h index 024ca15..af64ac4 100644 --- a/2.code/aicos/app/AI_AMP_2_5/dev_authentication.h +++ b/2.code/aicos/app/AI_AMP_2_5/dev_authentication.h @@ -24,4 +24,5 @@ typedef struct dev_authentication_MSG_Type MSG_Type; }dev_authentication_MSG_queue_t; aos_status dev_authentication_MSG_put(const char *data,u32 len,dev_authentication_MSG_Type MSG_Type); +void IOT_update_bargein(void);//OTA更新 #endif \ No newline at end of file diff --git a/2.code/aicos/mm/audio/voice_proc.c b/2.code/aicos/mm/audio/voice_proc.c index c94a24e..5ce7529 100644 --- a/2.code/aicos/mm/audio/voice_proc.c +++ b/2.code/aicos/mm/audio/voice_proc.c @@ -267,7 +267,7 @@ int voice_proc_send(void *hvp, void *buff, u32 size) u32 out_len = VOICE_FRM_LEN_MAX; int ret; - u8 *ref=NULL; + //u8 *ref=NULL; if (!vp_runtime || !vp_runtime->send_func) { return AIC_EPERM; @@ -276,12 +276,12 @@ int voice_proc_send(void *hvp, void *buff, u32 size) /* aec */ audio_dump(buff, size, 6); - if (vp_runtime->refsample) - ref =(u8*) buff + size; - else - ref =(u8 *) vp_runtime->aec; + // if (vp_runtime->refsample) + // ref =(u8*) buff + size; + // else + // ref =(u8 *) vp_runtime->aec; - audio_dump(ref,size,3; + // audio_dump(ref,size,3);