From: cheng.chen Date: Thu, 18 Sep 2025 03:53:27 +0000 (+0800) Subject: 3.0版本提交 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=91856b053a48fbe6ceed64093af8c7c62a077c1e;p=mirrors%2FFX01.git 3.0版本提交 --- 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 a1dc428..3399c84 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 @@ -71,10 +71,18 @@ static int VAD_time_count = 0;//上一次VAD触发结束到下一次触发开始 static aos_thread_id AI_AMP_task_id = 0; static aos_queue_ptr AI_AMP_task_queue = NULL; static u32 AI_VC_client_websocket_handle = 0; + static u32* ai_uart_handle; + +static u32* ui_uart_handle=NULL; + + 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; static aos_thread_id AI_VC_play_task_id = 0; @@ -97,6 +105,10 @@ static void AI_VC_put_record_data(char *data,u32 len); WebSocketClient* websocket_res[WEBSOCKET_MAX_NUM] = {0}; WebSocketClient *AI_client_VC = NULL; + +WebSocketClient *AI_client_IOT = NULL; + + static u32 close_timerout = 0; static u32 VC_pingping_timerout = 0; static u32 VC_END_play_timerout = 0; @@ -108,6 +120,9 @@ static char *AI_ASR_str_tmp = NULL; static aos_timer_ptr AI_VC_1S_timer = NULL; static aos_timer_ptr sys_10S_timer = NULL; + + //static aos_timer_ptr AI_VC_60S_timer = NULL; + static int login_timer_count = 0; static int power_off_count = 60*6*24; @@ -154,6 +169,8 @@ int bk_get_iccid(char *iccid) } + + #if (BK_LOG_CFG==BK_LOG_UART) int bk_printf(const char *format, ...) { @@ -292,9 +309,11 @@ void reset_VC_cfg(void) //vc_cfg.dev_type = DEV_ADS;//奥德赛 vc_cfg.dev_type = DEV_JSWM;//金山完美 //vc_cfg.dev_type = DEV_ALY;//阿里云 - // strcpy(vc_cfg.server_ip,"amp.raisound.com");//服务器地址 - strcpy(vc_cfg.server_ip,"115.190.94.208:22080");//服务器地址 - vc_cfg.VC_mode =0;//对话模式 + strcpy(vc_cfg.server_ip,"amp.raisound.com");//服务器地址 + + // strcpy(vc_cfg.server_ip,"115.190.94.208:22080");//服务器地址 + + vc_cfg.VC_mode =2;//对话模式 strcpy(vc_cfg.power_on_tone,"你好,请问您有什么需要我帮忙的吗?");//开机提示词 vc_cfg.VC_record_TYPE = VC_record_TYPE_PCM;//录音格式 vc_cfg.VC_play_TYPE = VC_play_TYPE_PCM;//播放格式 @@ -303,7 +322,7 @@ void reset_VC_cfg(void) vc_cfg.is_VAD = IS_VAD;//=1打开vad打断 vc_cfg.is_KWS = IS_KWS;//=1打开本地语言打断 vc_cfg.speech_rate = 0;//语速配置 - vc_cfg.vad_silence = 200;//vad断句时间 ms + vc_cfg.vad_silence = 500;//vad断句时间 ms vc_cfg.VC_TTS_volume = 80;//音量 } int init_VC_cfg(void) @@ -339,7 +358,13 @@ int init_VC_cfg(void) memset(vc_cfg.dev_pass,0,sizeof(vc_cfg.dev_pass)); } fclose(fp); + + } + + vc_cfg.VC_mode =2;//对话模式 + + return 0; } @@ -364,6 +389,8 @@ int isExist_file(char *filepath){ } + +int one_receive_bin=0; int isneedsave=0; int startsave_tts=0; @@ -374,10 +401,12 @@ u8 recv_get[1024]; static int play_audio_file_event(char *file_name); +u8 *save_temp_tts=NULL; int check_text(char *text,int ret_str_len) { //bk_printf("check_text:text:%s",text); + if(ret_str_len >60) return -1; int ret = -1; @@ -406,7 +435,10 @@ int check_text(char *text,int ret_str_len) tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"配置成功,当前音量%d。",vc_cfg.VC_TTS_volume); AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY); } - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + // if(vc_cfg.VC_mode != 0) + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + + is_save_VC_cfg = 1; ret = 0; goto RET; @@ -423,7 +455,11 @@ int check_text(char *text,int ret_str_len) tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"配置成功,当前音量%d。",vc_cfg.VC_TTS_volume); AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY); } - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + + //if(vc_cfg.VC_mode != 0) + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + + is_save_VC_cfg = 1; ret = 0; goto RET; @@ -440,17 +476,24 @@ int check_text(char *text,int ret_str_len) 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); } is_save_VC_cfg = 1; ret = 0; + + IOT_update_statu(3,"",""); goto RET; } @@ -467,13 +510,96 @@ int check_text(char *text,int ret_str_len) goto RET; } - if(strstr(text,"最新版本")) + if(strstr(text,"帮我生成语音文件")) + { + + AI_AMP_MSG_put(text,strlen(text),VC_TTS_PLAY); + + ret = 0; + goto RET; + } + + //||strstr(text,"做一个惊讶的表情")||strstr(text,"做一个开心的表情") + + if(strstr(text,"做一个发怒的表情")) + { + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x02,0x01,0x04}; + uart_write(ui_uart_handle,cmd_ui, 7); + } + + ret = 0; + goto RET; + } + if(strstr(text,"做一个惊讶的表情")) { - AI_AMP_MSG_put("当前版本是最新的",strlen("当前版本是最新的"),VC_TTS_PLAY); + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x06,0x01,0x08}; + uart_write(ui_uart_handle,cmd_ui, 7); + } + ret = 0; goto RET; } + if(strstr(text,"做一个开心的表情")) + { + + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; + uart_write(ui_uart_handle,cmd_ui, 7); + } + + ret = 0; + goto RET; + } + + + + + // if(strstr(text,"导出文件")) + // { + + // int len_daochu=0,sum_len=0;; + // FILE *fp_play_local=fopen("C:/cmd_local_301.pcm","rb"); + + + // if(fp_play_local){ + + + // while(1){ + + + // len_daochu=fread(recv_get,1,1024,fp_play_local); + + + // if(len_daochu>0){ + // sum_len+=len_daochu; + // audio_data(recv_get,len_daochu,22); + + // }else{ + + // break; + + // } + + + // } + + + // bk_printf("daochu len is %d ...",sum_len); + // fclose(fp_play_local); + + // } + // ret = 0; + // goto RET; + // } if(strstr(text,"查询")) @@ -516,44 +642,46 @@ int check_text(char *text,int ret_str_len) // bk_printf("playshebei"); - isneedsave=isExist_file("C:/cmd_local_301.pcm"); + // isneedsave=isExist_file("C:/cmd_local_301.pcm"); - if(isneedsave==0){ + //isneedsave=0; - bk_printf("exist"); + // if(isneedsave==0){ - // play_audio_file_event("C:/R_POWER_1.mp3"); + // bk_printf("exist"); - FILE *fp_play_local=fopen("C:/cmd_local_301.pcm","r"); + // // play_audio_file_event("C:/R_POWER_1.mp3"); + // FILE *fp_play_local=fopen("C:/cmd_local_301.pcm","r"); - if(fp_play_local){ + // if(fp_play_local){ - while(fread(recv_get,1,1024,fp_play_local)>0){ - // fread(recv_get,1,1024,fp_play_local); - // bk_printf("readttsfile"); + // while(fread(recv_get,1,1024,fp_play_local)>0){ - AI_VC_put_play_data(recv_get,1024); + // // fread(recv_get,1,1024,fp_play_local); + // // bk_printf("readttsfile"); + + // AI_VC_put_play_data(recv_get,1024); - } + // } - fclose(fp_play_local); - } + // fclose(fp_play_local); + // } - }else{ + // }else{ - bk_printf("needsave"); + // bk_printf("needsave"); - fp_tts_save=fopen("C:/cmd_local_301.pcm","wb"); + // fp_tts_save=fopen("C:/cmd_local_301.pcm","wb"); AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY); - startsave_tts=1; + // startsave_tts=1; - } + // } ret = 0; @@ -823,6 +951,8 @@ static void AI_VC_send_satrt(char is_greeting) cJSON_AddStringToObject(outbound_attributes, "provider", "CosyVoice"); }else cJSON_AddStringToObject(outbound_attributes, "provider", "volcengine"); + + // cJSON_AddStringToObject(outbound_attributes, "provider", "test"); // 创建 outbound_attributes 下的 cfg 对象 cJSON *outbound_cfg = cJSON_CreateObject(); if(outbound_attributes_format == attributes_format_OPUS) @@ -918,6 +1048,122 @@ 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) { @@ -956,6 +1202,85 @@ static void AI_VC_send_audio_create(void) free(json_str); cJSON_Delete(root); } + + +// { +// "event": "audio.convert", +// "payload": { +// "audio_convert_request": { +// "audio_url": audio_url, +// "bit_depth": 16, +// "channels": 1, +// "sample_rate": 16000, +// "target_format": "pcm" +// } +// } +// } + + +//Music.Start 开始发送音频 +static void AI_VC_send_music_start(char *urlpath) +{ + VC_state = VC_State_SpeechStart;//开始说 + // 创建根对象 + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf("Failed to create root object.\n"); + return ; + } + + // 设置 "event" 字段 + cJSON_AddStringToObject(root, "event", "audio.convert"); + + // 创建 "payload" 对象 + cJSON *payload = cJSON_CreateObject(); + if (payload == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; + } + + cJSON *audio_convert_request = cJSON_CreateObject(); + if (payload == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; + } + + cJSON_AddStringToObject(audio_convert_request, "audio_url", urlpath); + cJSON_AddNumberToObject(audio_convert_request, "bit_depth", 16); + cJSON_AddNumberToObject(audio_convert_request, "channels", 1); + cJSON_AddNumberToObject(audio_convert_request, "sample_rate", 16000); + + if(outbound_attributes_format == attributes_format_OPUS) + { + cJSON_AddStringToObject(audio_convert_request, "target_format", "opus"); + }else{ + + cJSON_AddStringToObject(audio_convert_request, "target_format", "pcm"); + } + + + cJSON_AddItemToObject(payload, "audio_convert_request", audio_convert_request); + + // 将 "payload" 对象添加到根对象中 + cJSON_AddItemToObject(root, "payload", payload); + + // 将 JSON 对象转换为字符串 + char *json_str = cJSON_Print(root); + if (json_str == NULL) { + bk_printf("Failed to print JSON object.\n"); + cJSON_Delete(root); + + } + AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 + + + // 释放内存 + free(json_str); + cJSON_Delete(root); +} + //Audio.Completed 结束发送音频 static void AI_VC_send_audio_completed(void) { @@ -1071,7 +1396,7 @@ static void AI_VC_send_task(void *thread_input)//websocket 发送线程 if(AI_client_VC && AI_VC_send_queue_data.data) { - VC_pingping_timerout = 4; + VC_pingping_timerout = 20; if(inbound_attributes_format == attributes_format_OPUS&& AI_VC_send_queue_data.AIC_WEBSOCKET_OPCODE ==AIC_WEBSOCKET_OPCODE_BINARY_FRAME )//转码opus { char *opus_data = aic_malloc(OPUS_ENCODER_PACK_MS*16); @@ -1116,9 +1441,6 @@ static void AI_VC_send_task(void *thread_input)//websocket 发送线程 // bk_printf("send_count_pcm: %d len: %d !",send_count_pcm,send_update); - - - // audio_data(AI_VC_send_queue_data.data,AI_VC_send_queue_data.len,25); } @@ -1140,12 +1462,51 @@ static void AI_VC_send_task(void *thread_input)//websocket 发送线程 } + +int opus_encode_len=0,opus_decode_len=0; + + + + + +//#include + +long getFileSize(char* filename) { + FILE* file = fopen(filename, "rb"); + if (file == NULL) { + return -1; // 文件打开失败 + } + + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + fclose(file); + + return fileSize; +} + + + #define recv_buff_mxa_len 20*1024 + + u32 timer_ms21=0; + + 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 读线程 { int status; static u8 *recv_buff = NULL; + if(recv_buff == NULL) { recv_buff = aic_malloc(recv_buff_mxa_len); @@ -1168,9 +1529,28 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 memset(recv_buff,0,recv_buff_mxa_len); // status = aic_websocket_recv(AI_VC_client_websocket_handle, recv_buff, sizeof(recv_buff), // &code, AOS_WAIT_FOREVER,&result_val); - //bk_printf("AI_VC_read_task 1"); - status = receive_websocket_message(AI_client_VC,recv_buff,recv_buff_mxa_len,&code); - //bk_printf("AI_VC_read_task 2"); + // bk_printf("AI_VC_read_task 1"); + + // aos_sleep(50); + + u32 timer_ms11 = aos_get_tickcount(); + + bk_printf("AI_VC_read_task 12"); + + status = receive_websocket_message(AI_client_VC,recv_buff,recv_buff_mxa_len,&code); + + bk_printf("AI_VC_read_task 13"); + + u32 timer_ms22 = aos_get_tickcount(); + + int duration_temp=timer_ms22-timer_ms11; + + aos_sleep(15); + + if(duration_temp>500) + bk_printf("receive_websocket_message %d ms",timer_ms22-timer_ms11); + + if(status>0) { close_timerout = 60; @@ -1221,8 +1601,7 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 memcpy(AI_ASR_str_tmp,str_tmp,str_tmp_len); AI_ASR_str_tmp[str_tmp_len] = 0; } - - + } }else if(strstr(recv_buff,"\"asr.completed\""))//asr结束 @@ -1232,8 +1611,12 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 { int ret = check_text(AI_ASR_str_tmp,strlen(AI_ASR_str_tmp)) ; //识别到有命令词,退出 - if(ret == 0) + if(ret == 0){ + + memset(AI_ASR_str_tmp,0,strlen(AI_ASR_str_tmp)); break; + } + if(VAD_time_count <= VAD_TIMEOUT )//时间间隔短,认为是一次对话,拼接内容 { @@ -1255,6 +1638,8 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 strncpy(AI_ASR_str,AI_ASR_str_tmp,AI_ASR_STR_MAX_LEN); bk_printf("vad间隔时间长,判断为两次对话:%s",AI_ASR_str); } + + IOT_update_statu(1,"thinking",""); } } else if(strstr(recv_buff,"\"vad.active\""))//vad触发 @@ -1267,15 +1652,48 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 } else if(strstr(recv_buff,"\"tts.started\""))//tts开始 { + if(AI_VC_play_queue != NULL) + { + char count_t= 200; + AI_VC_audio_queue_t AI_VC_play_queue_data_t; + while(count_t--)//清空队列数据 + { + if(aos_recv_from_queue(AI_VC_play_queue,&AI_VC_play_queue_data_t,AOS_NO_WAIT) == AOS_SUCCESS) + { + if(AI_VC_play_queue_data_t.data) + aic_free(AI_VC_play_queue_data_t.data); + } + else{ + break; + } + } + + } + VC_state = VC_State_TTSStart; play_data_len = 0; + IOT_update_statu(1,"speaking",""); + + one_receive_bin=0; + if(outbound_attributes_format == attributes_format_OPUS){ bk_printf("opus_clean tts.started....\n"); opus_clean(); + + + opus_encode_len=0; + + opus_decode_len=0; + + // if(save_temp_tts==NULL){ + + // save_temp_tts=aic_malloc(900000); + // } + } @@ -1289,8 +1707,30 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 startsave_tts=0; - if(fp_tts_save) - fclose(fp_tts_save); + IOT_update_statu(1,"idle",""); + + // fp_tts_save=fopen("C:/cmd_local_301.pcm","wb"); + + // bk_printf("C:/cmd_local_301.pcm %d",one_receive_bin); + + // fwrite(save_temp_tts,1,one_receive_bin,fp_tts_save); + + // startsave_tts=one_receive_bin; + + + + + + // if(fp_tts_save) + // fclose(fp_tts_save); + + // fp_tts_save=NULL; + + // aos_sleep(500); + + // bk_printf("C:/cmd_local_301.pcm %d",getFileSize("C:/cmd_local_301.pcm")); + + } else if(strstr(recv_buff,"\"completed\""))//结束 @@ -1319,35 +1759,99 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 } + }else if(strstr(recv_buff,"\"event\":\"command\""))//接口报错了 + { + // char *str_tmp = NULL; + + // str_tmp = strstr(recv_buff,"\"param_json\""); + char *str_tmp = strstr(recv_buff,"\"https"); + + if(str_tmp) + { + + char mp3_path[200]; + memset(mp3_path,0,200); + //bk_printf("content!!"); + // str_tmp += strlen("\"params_json")+6; + int str_tmp_len = str_count(str_tmp+1,'\"'); + //存asr结果 + + memcpy(mp3_path,str_tmp+1,str_tmp_len-3); + //AI_ASR_str_tmp[str_tmp_len] = 0; + + bk_printf("the mp3_path is %s",mp3_path); + + AI_VC_send_music_start(mp3_path); + + } + + } } else if(code == WS_BINARY)//二进制 { - - bk_printf("read BIN status:%d,code:%d!!!!!!",status,code); - if(startsave_tts==1){ - if(fp_tts_save){ + // if(one_receive_bin==0) + // timer_ms21=aos_get_tickcount(); - // bk_printf("save tts!!!"); - fwrite(recv_buff,1,status,fp_tts_save); - } - } + // one_receive_bin+=status; + + // timer_ms22=aos_get_tickcount(); + + // u8 play_length_time=one_receive_bin/32; + + + // if((one_receive_bin/32)<(timer_ms22-timer_ms21)) + // bk_printf("read BIN status:%d,code:%d!!!!!!",status,code); + + // VC_pingping_timerout = 20; + + // if(startsave_tts==1){ - // audio_data(recv_buff,status,29); + // if(fp_tts_save){ + // bk_printf("save tts!!!"); + // fwrite(recv_buff,1,status,fp_tts_save); + // } + // } + if(outbound_attributes_format == attributes_format_OPUS) { - bk_printf("ogg_opus_decode_data"); - ogg_opus_decode_data(recv_buff,status); + + + // if(one_receive_bin+status<900000) + // memcpy(save_temp_tts+one_receive_bin,recv_buff,status); + + one_receive_bin+=status; + + bk_printf("ogg_opus_decode_data %d...",one_receive_bin); + + // memcpy(save_temp_tts); + + // audio_data(recv_buff,status,26); + + ogg_opus_decode_data(recv_buff,status); + + // if(fp_tts_save){ + + // bk_printf("save tts!!!"); + // fwrite(recv_buff,1,status,fp_tts_save); + + // } + + }else { - AI_VC_put_play_data(recv_buff,status);// + + // audio_data(recv_buff,status,23); + AI_VC_put_play_data(recv_buff,status); + play_data_len+=status; + } @@ -1419,10 +1923,12 @@ static void AI_VC_record_task(void *thread_input)//录音线程 ret = app_audio_read(APP_AUDIO_TYPE_STREAM_DUPLEX, vrecord_buf, 3200, 1010); - // audio_data(vrecord_buf,ret,23); + // audio_data(vrecord_buf,ret,23); + // u32 timer_ms = aos_get_tickcount(); // bk_printf("%d:ret=%d",timer_ms,ret); - // app_logi("AI_VC_record_task app_audio_read len = %d,VC_state = %d",ret,VC_state); + // app_logi("AI_VC_record_task app_audio_read len = %d,VC_state = %d",ret,VC_state); + if(ret<=0) { //bk_printf("AI_VC_record_task continue len = %d,VC_state = %d",ret,VC_state); @@ -1692,6 +2198,9 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 if(AI_VC_play_task_flag >=2) break; //app_logi("app_audio_write0!!\r\n"); + + // play_data_len>8*1024 || + if((play_data_len>8*1024 || outbound_attributes_format == attributes_format_OPUS)&&VC_Play_flag ==1 && AI_VC_play_queue ) { queue_ret = aos_recv_from_queue(AI_VC_play_queue,&AI_VC_play_queue_data,AOS_NO_WAIT); @@ -1822,6 +2331,252 @@ int get_websocket_res_num(void) } return WEBSOCKET_MAX_NUM-num; } + + +uint16_t modbus_crc16(const uint8_t *data, uint16_t length) { + uint16_t crc = 0xFFFF; + for (uint16_t i = 0; i < length; i++) { + crc ^= (uint16_t)data[i]; + for (uint8_t j = 0; j < 8; j++) { + if (crc & 0x0001) { + crc >>= 1; + crc ^= 0xA001; + } else { + crc >>= 1; + } + } + } + return crc; +} + + +//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) { @@ -1863,7 +2618,61 @@ static int websocket_connect_VC(void) free_all_websocket_res(); } + + 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) { @@ -2134,9 +2943,12 @@ void AI_VC_put_play_data(char *data,u32 len) if(AI_VC_play_queue == NULL) return; aos_get_queue_used_count(AI_VC_play_queue,&count_tmp); - if(count_tmp>100)//大于100个缓存 + + bk_printf("AI_VC_put_play_data:count_tmp = %d",count_tmp); + + if(count_tmp>150)//大于100个缓存 { - //bk_printf("AI_VC_put_play_data:count_tmp = %d",count_tmp); + // bk_printf("AI_VC_put_play_data:count_tmp = %d",count_tmp); aos_sleep(1); }else { @@ -2390,6 +3202,16 @@ void AI_key_press(void) { static u64 timer_ms = 0; + // IOT_update_statu(1); + + // IOT_update_statu(3,"",""); + + // IOT_heart(); + + bk_printf("AI_key_press1"); + + play_file_stream_data("C:/chenggong.pcm"); + u64 timer_ms_tmp = aos_get_tickcount(); if(timer_ms_tmp-timer_ms<1000)//每次触发小于1000ms关闭对话 { @@ -2403,11 +3225,14 @@ void AI_key_press(void) AI_VC_Close(); VC_state = VC_State_None; - VC_record_Start() ; + + + + VC_record_Start(); if(AI_VC_start(0)==0) { - play_file_stream_data("C:/chenggong.pcm"); + //audio_play_tone(3,300);//播放提示音,测试用,位置不能变,和双向流有冲突 //app_logi("audio_play_tone ret = %d\r\n",ret); aos_sleep(1); @@ -2486,6 +3311,8 @@ int32_t bk_audio_start_callback(void *p_param, uint32_t size) return 0; } char AI_VC_start_lock = 0; + +int net_status_count=0; //is_greeting,是否播放开场白 int AI_VC_start(char is_greeting) { @@ -2530,7 +3357,7 @@ int AI_VC_start(char is_greeting) // aic_gpio_output(GPIO_GPO_8,GPIO_OUTPUT_LVL_HIGH );//马达 // aic_gpio_output(GPIO_GPO_1,GPIO_OUTPUT_LVL_LOW );//马达 // aic_gpio_output(GPIO_20,GPIO_OUTPUT_LVL_HIGH );//马达 - //app_audio_set_master_volume(8); + // app_audio_set_master_volume(8); if(AI_VC_1S_timer == NULL) { @@ -2540,6 +3367,8 @@ int AI_VC_start(char is_greeting) AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); aic_assert(AI_VC_1S_timer); } + + if(inbound_attributes_format == attributes_format_OPUS ||outbound_attributes_format == attributes_format_OPUS) { if(opus_init()!= 0) @@ -2559,10 +3388,10 @@ int AI_VC_start(char is_greeting) struct VadConfig conf; conf.sil_threshold = 63; conf.energy_threshold = 10000; - conf.min_speech_length = 240; + conf.min_speech_length = 240;//240 conf.silence_detect_length = 300; conf.rule1_min_tailing_silence = 480; - conf.rule2_min_tailing_silence = vc_cfg.vad_silence ; + conf.rule2_min_tailing_silence = vc_cfg.vad_silence; conf.rule3_min_tailing_silence = vc_cfg.vad_silence; conf.vad_log_level = 0; int init_vad_ret = 0; @@ -2573,6 +3402,10 @@ int AI_VC_start(char is_greeting) imsl_nn_printf = bk_printf; imsl_nn_init_flag = 0; } + + + + init_vad_ret = init_vad(&conf); @@ -2612,6 +3445,10 @@ int AI_VC_start(char is_greeting) goto AI_VC_start_RET; } + + + IOT_update_statu(1,"listening",""); + net_status_count=0; //aos_sleep(5); AI_VC_send_satrt(is_greeting);// close_timerout = 0; @@ -2620,7 +3457,11 @@ AI_VC_start_RET: if(ret != 0) { AI_VC_Close(); + net_status_count++; play_audio_file("C:/xinhaoruo1.mp3"); + + if(net_status_count>=3) + play_audio_file("C:/khye.mp3"); }else AI_VC_stata = 1; return ret; @@ -2849,6 +3690,19 @@ static void sys_10S_timeout(void *param) { AI_AMP_MSG_put(NULL,0,AI_TIMER_EVENT); } + +static void sys_60S_timeout(void *param) +{ + // AI_AMP_MSG_put(NULL,0,AI_TIMER_EVENT); + + if(AI_client_IOT!=NULL){ + + IOT_heart(); + + } +} + + static void sys_10S_timeout_even(void) { // if (sizeof(dev_pass) == 0) @@ -2913,19 +3767,26 @@ static void sys_10S_timeout_even(void) // extern char *ota_url; - const char *bk_make_timer = "V1_2_250730_1"; + const char *bk_make_timer = "V1_2_250730_8"; + +void play_tts_my_local(char *filename) +{ + play_audio_file_event(filename); +} +// #define exptest +int auth_vad_status=0; static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 { - char count = 30; - boolean pdp_activated ; - AI_AMP_MSG_queue_t AI_AMP_msg = {0}; + char count = 30; + boolean pdp_activated ; + AI_AMP_MSG_queue_t AI_AMP_msg = {0}; - init_VC_cfg(); + init_VC_cfg(); - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); play_audio_file_event("C:/R_POWER_1.mp3"); @@ -2951,8 +3812,10 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 if(pdp_activated == TRUE) break; bk_printf("AI_AMP_2_5_init 等待注网..... \n"); + //audio_play_tone(count%15,500);//播放提示音,测试用 //audio_play_tone(3,500);//播放提示音,测试用 + aos_sleep(1000); } @@ -2972,16 +3835,21 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 // play_audio_file_event("C:/local_tts_004.wav"); - ota_check_version(); + ota_check_version(); + // play_audio_file_event("C:/upgrade.mp3"); // AI_AMP_MSG_put("正常启动",4,VC_TTS_PLAY); + // aos_sleep(5000); + ota_update_version(); // if(strlen(ota_url)>3){ - + // play_audio_file_event("test12312.mp3"); dev_authentication_init(); + + count =50; while(count --) { @@ -2992,6 +3860,7 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 bk_printf("AI_AMP_2_5_init 等待鉴权..... \n"); aos_sleep(200); } + if(sys_10S_timer == NULL) { sys_10S_timer = aos_create_timer("AI_VC_1S_timer", @@ -3000,6 +3869,80 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); aic_assert(sys_10S_timer); } + + // AI_VC_60S_timer + // if(AI_VC_60S_timer == NULL) + // { + // AI_VC_60S_timer = aos_create_timer("AI_VC_60S_timer", + // sys_60S_timeout, &AI_VC_60S_timer, + // 60000, + // AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); + // aic_assert(AI_VC_60S_timer); + // } + + bk_printf("auth_vad token0: %s..... \n",get_token()); + struct AuthInfo auth_info; + + strcpy(auth_info.dmp_token, get_token()); + + if(strlen(auth_info.dmp_token)>0){ + + for(int i=0;i<3;i++){ + int ret=auth_vad(&auth_info); + + if(ret==0){ + + bk_printf("auth_vad fail token: %s..... \n",get_token()); + + }else{ + + bk_printf("auth_vad success token: %s..... \n",get_token()); + + auth_vad_status=1; + break; + + } + + } + }else{ + + play_audio_file_event("C:/xinhaoruo1.mp3"); + } + if(auth_vad_status==0){ + + play_audio_file_event("C:/xinhaoruo1.mp3"); + + aos_sleep(2000); + + } + +#ifdef exptest + + aos_sleep(5000); + + ui_uart_handle = uart_open(2); + + if(ui_uart_handle){ + + uart_set_baud_rate(ui_uart_handle, 9600); + + bk_printf("UART 2 SUCCESSS........................"); + + // uart_write(ui_uart_handle,"123", strlen("123")); + + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x02,0x01,0x04}; + + uart_write(ui_uart_handle,cmd_ui, 7); + + }else{ + + bk_printf("UART 2 FAIL........................"); + + } + +#endif + + aic_network_enable_pdp_nic_capdump();//打开网络调试log // while(1) 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 0d3dd53..a4ac9e2 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 @@ -6,7 +6,7 @@ #define BK_LOG_USB 2//USB打印 -// #define BK_LOG_CFG BK_LOG_UART + #define BK_LOG_CFG BK_LOG_USB typedef enum { @@ -132,4 +132,8 @@ aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type); int bk_get_imei(char *imei); int save_VC_cfg(void); int play_audio_file(char *file_name); -void AI_VC_put_play_data(char *data,u32 len);//音频数据入队播放 \ No newline at end of file +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 diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.c b/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.c index 7dd5d28..3279114 100644 --- a/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.c +++ b/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.c @@ -231,6 +231,10 @@ int opus_encode_data(char *PCM_data,int data_len,int one_pack_len,char *opus_dat return ret; } +extern int opus_encode_len; + +extern int opus_decode_len; + int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) { int data_count = 0; @@ -336,7 +340,12 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) { memcpy(ogg_buf,ogg_hend,27+seg_num); bk_rb_read(OPUS_decoder_rb_data,ogg_buf+27+seg_num,seg_size);//读一整页 + + ogg_hend_len = 0;//头部已读完,认为读完一整帧 + + // audio_data(ogg_buf,27+seg_num+seg_size,25); + page_type = ogg_check_opus_page_type(ogg_buf, &channel_cnt, &sample_rate);//获取数据类型,1是头,3是音频帧 if (1 == page_type) { bk_printf("ogg头:lchannel cnt = %d, sample rate = %d", channel_cnt, sample_rate); @@ -353,7 +362,6 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) if(pcm_buf) { - dec_inparam.input = p_audio; dec_inparam.inputfrm_count = 1; dec_inparam.input_len_array[0] = frame_length; @@ -362,7 +370,10 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) dec_inparam.outbuffer_size = 4096; ret = aic_opus_decode(&dec_inparam); - //bk_printf("opus解码 输入大小:%d,输出大小:%d", frame_length,dec_inparam.outputfrms_size); + // bk_printf("opus解码 输入大小:%d,输出大小:%d", frame_length,dec_inparam.outputfrms_size); + + opus_encode_len++; + if (ret == OPUS_RESULT_ERROR) { bk_printf("opus解码 error:%d", ret); //bk_printf("ogg data:"); @@ -373,15 +384,30 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) aic_free(pcm_buf); break; } + input_tmp_len+=frame_length; ooutput_tmp_len += dec_inparam.outputfrms_size; + AI_VC_put_play_data(pcm_buf,dec_inparam.outputfrms_size);//发送数据到音频队列 + + // audio_data(pcm_buf,dec_inparam.outputfrms_size,24); + //aos_sleep(1); aic_free(pcm_buf); } } - bk_printf("ogg-opus解码 输入数据大小:%d,输出数据大小:%d,耗时:%d ms",input_tmp_len,ooutput_tmp_len,aos_get_tickcount()-timer_ms ); + + // bk_printf("opus解码 输入大小:%d,输出大小:%d", frame_length,dec_inparam.outputfrms_size); + + bk_printf("ogg-opus解码 输入数据大小:%d,输出数据大小:%d,耗时:%d ms",input_tmp_len,ooutput_tmp_len,aos_get_tickcount()-timer_ms ); + + + // opus_decode_len+=ooutput_tmp_len; + + // bk_printf("sum 输入数据大小:%d",ooutput_tmp_len); + + }else if(2 == page_type)//标签 { bk_printf("opus标签 :%s", ogg_buf+27+seg_num); 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 1a28154..93e3cd6 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 @@ -20,6 +20,7 @@ #include #include #include +#include "cJSON.h" extern int bk_printf(const char *format, ...); extern void bk_print_hex(u8 *data,int data_len); #ifdef CONFIG_CRYPTO_MBEDTLS @@ -427,6 +428,43 @@ send_websocket_message_ERROR: return ret; } + +// 发送 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; @@ -434,6 +472,79 @@ send_websocket_message_ERROR: // 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) { //u64 timer_ms = aos_get_tickcount(); //bk_printf("receive_websocket_message 1 timer_ms=%d\n",timer_ms); @@ -571,8 +682,12 @@ int receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l //bk_sleep(1); while(1)//读主体内容 { - //bk_sleep(recv_len/200+1); + // bk_sleep(recv_len/200+1); + // bk_printf("bytes_received %d %d",payload_len,bytes_received); recv_len = recv(client->sockfd, buf+bytes_received, payload_len-bytes_received, 0); + + // bk_printf("recv_len %d %d",payload_len,recv_len); + if (recv_len == 0) { bk_printf("Connection closed by server.5\n\n\n\n\n"); return 0; @@ -777,6 +892,119 @@ client_ERROR: return NULL; } + +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 01d55ea..4bdf4b7 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,13 +24,20 @@ 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) ; // 接收 WebSocket 消息 int receive_websocket_message(WebSocketClient *client, unsigned char *buf,int len,WebSocketMessageType *code); +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 9ad0993..d5b2a5c 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 @@ -357,6 +357,7 @@ void aut_active(void)//设备激活 char timestamp_str[20] ={0}; // 使用 sprintf 将 long long 类型的时间戳转换为字符串 + ret = sprintf(timestamp_str, "%lld", timestamp_ms); timestamp_str[ret] =0; // 打印转换后的字符串形式的时间戳 @@ -631,65 +632,7 @@ void update_iccid(void)//设备激活 goto aut_active_return; bk_printf("http put data update_iccid :%s",aut_buf); - - char *str_tmp = NULL; - str_tmp = strstr(aut_buf,"\"password\":\""); - //bk_printf("str_tmp:%p",str_tmp); - if(str_tmp)//解析password - { - // str_tmp += strlen("\"password\":\""); - // int count = str_count(str_tmp,'\"'); - // bk_printf("count:%d",count); - // if(count < sizeof(dev_pass_base64)) - // { - // memcpy(dev_pass_base64,str_tmp,count); - // dev_pass_base64[count] = 0; - // bk_printf("dev_pass_base64:%s",dev_pass_base64); - - // }else - // { - // bk_printf("激活获取base64失败"); - // } - } - str_tmp = NULL; - str_tmp = strstr(aut_buf,"\"nonce\":\""); - if(str_tmp)//nonce - { - // str_tmp += strlen("\"nonce\":\""); - // int count = str_count(str_tmp,'\"'); - // if(count < sizeof(enc_iv)) - // { - // if(count>=sizeof(enc_iv)) - // count = sizeof(enc_iv)-1; - // memcpy(enc_iv,str_tmp,count); - // enc_iv[count] = 0; - // bk_printf("nonce:%s",enc_iv); - - //} - // else - // { - // bk_printf("激活获取nonce失败"); - // } - } - int dev_pass_len = get_dev_pass_MAX_len(); - char *dev_pass = bk_malloc(dev_pass_len); - if(dev_pass) - { - // memset(dev_pass,0,dev_pass_len); - // if(strlen(enc_iv)>8 && strlen(dev_pass_base64)>8) - // { - // aes_cbc_pkcs5_decrypt(dev_pass_base64, dev_key, enc_iv,dev_pass,&dev_pass_len);//解密 - // dev_pass[dev_pass_len] = 0; - // bk_printf("dev_pass:%s dev_pass_len:%d",dev_pass,dev_pass_len); - // save_dev_pass(dev_pass);//保存设备密钥 - //} - // bk_free(dev_pass); - - }else - { - bk_printf("dev_pass malloc error!!!!!!!!!!!"); - } - + aut_active_return: // 释放内存 @@ -772,6 +715,135 @@ FOTA_START_BY_APP)) return 0; } +void play_tts_my_local(char *filename); + +void IOT_update_bargein(void)//OTA更新 +{ + int ret; + bk_printf("IOT_update_bargein"); + + + AIC_HTTPS_CLIENT_INSTANCE *http_client; + char deviceId[30] = {0}; + //char dev_pass_base64[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}; + + cJSON *root = NULL; + aut_buf = bk_malloc(5*1024); + if(aut_buf) + memset(aut_buf , 0,5*1024); + else + goto aut_active_return; + + bk_get_imei(deviceId); + + //http://app-api.raisound.com/dev/device/info/simple/867686073050930 + + sprintf(sign_url,"http://app-api.raisound.com/dev/device/info/simple/%s",deviceId); + + bk_printf(sign_url); + + u32 response = 0; + //创建客户端实例 + aut_active_http_handle = aic_http_client_create("HTTP_DEMO_CLIENT_NAME"); + if (aut_active_http_handle == 0) { + bk_printf("Failed to create HTTP client instance\n"); + goto aut_active_return; + } + int url_value = aic_process_https_url(aut_active_http_handle,sign_url,AIC_HTTP_HEAD); + bk_printf("url_value:%d",url_value); + http_client = (AIC_HTTPS_CLIENT_INSTANCE *)aut_active_http_handle; + http_client->login_info.server_port = 80; + aic_http_client_connect(aut_active_http_handle, 5000); + + // 创建JSON对象 + root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf( "Failed to create JSON object\n"); + goto aut_active_return; ; + } + + // 输出JSON + // bk_printf("JSON output: %s\n", json_string); + + aic_https_client_request_initialize(aut_active_http_handle,AIC_HTTPS_METHOD_GET, 0, + 0, NULL,NULL, 10000); + + + response = nx_web_http_client_request_send(http_client->https_client,10 * 1000); + + ret=0; + if (ret == 0) { + bk_printf("数据发送成功\n"); + + } else { + // 处理错误 + bk_printf("数据发送失败,错误码: %d\n", ret); + goto aut_active_return; + } + + long read_bytes = aic_https_client_response_data_get(aut_active_http_handle,aut_buf,5*1024,10000); + + bk_printf("read_bytes :%d\n",read_bytes); + + if(read_bytes<=0) + goto aut_active_return; + + bk_printf("IOT_update_bargein :%s",aut_buf); + + + char *str_tmp = NULL; + + str_tmp = strstr(aut_buf,"\"bargein\":\""); + //bk_printf("str_tmp:%p",str_tmp); + if(str_tmp)//解析password + { + str_tmp += strlen("\"bargein\":\""); + int count = str_count(str_tmp,'\"'); + bk_printf("count:%d",count); + if(count < sizeof(ota_url)) + { + memcpy(ota_url,str_tmp,count); + ota_url[count] = 0; + bk_printf("bargein:%s",ota_url); + + int temp_bag=atoi(ota_url); + if(temp_bag>=0&&temp_bag<10){ + + vc_cfg.VC_mode=temp_bag; + } + // vc_cfg.VC_mode= + + // vc_cfg.VC_TTS_volume + + + + } + else + { + bk_printf("bargein"); + } + } + +aut_active_return: + // 释放内存 + if(aut_buf) + bk_free(aut_buf); + // if(json_string) + // cJSON_free(json_string); + if(root) + cJSON_Delete(root); + if(aut_active_http_handle) + aic_http_client_delete(aut_active_http_handle); +} void ota_check_version(void)//OTA更新 @@ -866,7 +938,8 @@ void ota_check_version(void)//OTA更新 bk_printf("ota_check_version :%s",aut_buf); - char *str_tmp = NULL; + char *str_tmp = NULL; + str_tmp = strstr(aut_buf,"\"url\":\""); //bk_printf("str_tmp:%p",str_tmp); if(str_tmp)//解析password @@ -900,7 +973,9 @@ void ota_check_version(void)//OTA更新 bk_printf("OTA need update 2222....\n"); - + // play_audio_file_event("test12313.mp3"); + + play_tts_my_local("C:/upgrade.mp3"); while(count_test--){ @@ -919,7 +994,10 @@ void ota_check_version(void)//OTA更新 // fota_stop(); }else{ + // play_audio_file_event("test12312.mp3"); + // play_tts_my_local("C:/current_version.mp3"); + // aos_sleep(3000); } @@ -938,6 +1016,7 @@ aut_active_return: } + void ota_download_version(void)//OTA更新 { int ret; @@ -1152,26 +1231,6 @@ void ota_update_version(void)//OTA上报 bk_printf("http put data update_iccid :%s",aut_buf); - char *str_tmp = NULL; - str_tmp = strstr(aut_buf,"\"password\":\""); - //bk_printf("str_tmp:%p",str_tmp); - if(str_tmp)//解析password - { - // str_tmp += strlen("\"password\":\""); - // int count = str_count(str_tmp,'\"'); - // bk_printf("count:%d",count); - // if(count < sizeof(dev_pass_base64)) - // { - // memcpy(dev_pass_base64,str_tmp,count); - // dev_pass_base64[count] = 0; - // bk_printf("dev_pass_base64:%s",dev_pass_base64); - - // }else - // { - // bk_printf("激活获取base64失败"); - // } - } - aut_active_return: // 释放内存 @@ -1244,6 +1303,7 @@ void aut_login(void)//登录 bk_printf("deviceId:%s password:%s nonce:%s,dev_pass:%s", deviceId, encrypt_data, enc_iv,dev_pass); char *sign_url = "http://dmp.raisound.com/bz-api/dev/auth/sign_in"; // 登录地址 //char *headers_T = "\"Content-Type\":\"application/json\""; + bk_printf(sign_url); u32 response = 0; aut_login_http_handle = aic_http_client_create("HTTP_DEMO_CLIENT_NAME"); int url_value = aic_process_https_url(aut_login_http_handle,sign_url,AIC_HTTP_HEAD); @@ -1380,7 +1440,16 @@ static void dev_authentication_task(void *thread_input)//鉴权逻辑线程 //aut_active();//激活 aut_login(); + + // if(strlen(get_token())<8)//20秒都没更新tokey + // { + // aut_active(); + // aut_login(); + // } + + + update_iccid(); //登录 diff --git a/2.code/aicos/mm/audio/voice_proc.c b/2.code/aicos/mm/audio/voice_proc.c index 8eeacf2..c94a24e 100644 --- a/2.code/aicos/mm/audio/voice_proc.c +++ b/2.code/aicos/mm/audio/voice_proc.c @@ -266,6 +266,8 @@ int voice_proc_send(void *hvp, void *buff, u32 size) struct vp_runtime *vp_runtime = hvp; u32 out_len = VOICE_FRM_LEN_MAX; int ret; + + u8 *ref=NULL; if (!vp_runtime || !vp_runtime->send_func) { return AIC_EPERM; @@ -273,6 +275,16 @@ 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; + + audio_dump(ref,size,3; + + + audio_ec_proc(vp_runtime->haec, buff, vp_runtime->aec, size, vp_runtime->out, &out_len); audio_dump(vp_runtime->out, out_len, 7);