From: cheng.chen Date: Wed, 10 Jun 2026 02:49:26 +0000 (+0800) Subject: 爱喵喵最新版本 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=4a896bcfb24a4fda73393894a252100862451a8b;p=mirrors%2FFX01.git 爱喵喵最新版本 --- 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 3c233c4..582f2b3 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 @@ -35,19 +35,15 @@ #include "app_msg.h" #include "aic_ogg.h" #include "efuse.h" -#if SUPPORT_MOT -#include "mot_dev.h" -#endif -#include "bk_make_time.h" -static aos_thread_id UI_UART_read_id = 0; +#include + +// #include "bk_make_time.h" // #include "opus/include/opus.h" char FXH01_Version[100]; u8 current_card_id = 0; - -u8 chumo_exp_open = 1; u8 VC_TTS_PLAY_EVENT = 0; typedef enum { @@ -56,6 +52,10 @@ typedef enum }attributes_format_type; attributes_format_type inbound_attributes_format = attributes_format_PCM; attributes_format_type outbound_attributes_format = attributes_format_OPUS; + + +u8 codec_tts=1;//1 opus 2 ogg + #define OPUS_ENCODER_PACK_MS 60 //多少ms一帧数据 @@ -84,7 +84,14 @@ static u32* ai_uart_handle; static u32* ui_uart_handle=NULL; -static u8 is_dialog_status=0; +static u32 sleep_chumo_timerout = 0; + +static u8 is_dialog_status=1; + +int is_offlinecmd_status=1; + +int chumotype=0; +static int need_start_cmd=0; static aos_thread_id AI_VC_send_task_id = 0; char AI_VC_send_task_flag = 0; @@ -92,6 +99,8 @@ static aos_thread_id AI_VC_read_task_id = 0; static aos_thread_id AI_VC_read_IOT_id = 0; +static aos_thread_id UI_UART_read_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; @@ -117,36 +126,43 @@ WebSocketClient *AI_client_VC = NULL; WebSocketClient *AI_client_IOT = NULL; - static u8 issend_start_up=0; -static u8 random_mt_test=0; +//static u8 issend_start_up=0; -static u32 close_timerout = 0; + static u8 is_music_status=0; -static u32 sleep_chumo_timerout = 0; + static u8 need_play_is_music_status=0; -static u32 tts_start_timerout = 0; + static u8 need_express_cmd=0; +static u8 is_newsession=true; + +static u8 delay_welcome=0; + +static u32 close_timerout = 0; + + +static u32 silence_timerout = 0; static u32 VC_pingping_timerout = 0; static u32 VC_END_play_timerout = 0; static char *AI_TTS_str = NULL; static char *AI_ASR_str = NULL; #define AI_ASR_STR_MAX_LEN 1024*2 static char *AI_ASR_str_tmp = NULL; - -static int has_tmp_result=0; #define AI_ASR_STR_MAX_TMP_LEN 1024 static aos_timer_ptr AI_VC_1S_timer = NULL; +static aos_timer_ptr AI_VC_1S_uart_timer = NULL; + static aos_timer_ptr sys_10S_timer = NULL; static aos_timer_ptr AI_VC_60S_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; +static int power_off_count = 60*60*24; + +u8 is_need_tip=0; static void play_file_stop(void); @@ -158,8 +174,6 @@ VC_state_type VC_state = VC_State_None; static u8 uid[50]; static int uid_len =0; -//int type_test=1; - //关闭录音 void VC_record_End(void); //打开录音 @@ -176,6 +190,8 @@ void VC_Play_End(void); //打开音频播放 void VC_Play_Start(void); + + int bk_get_imei(char *imei) { if(imei == NULL) @@ -193,6 +209,8 @@ int bk_get_iccid(char *iccid) } + + #if (BK_LOG_CFG==BK_LOG_UART) int bk_printf(const char *format, ...) { @@ -331,27 +349,31 @@ void reset_VC_cfg(void) //vc_cfg.dev_type = DEV_FX1_H;//产品 //vc_cfg.dev_type = DEV_ADS;//奥德赛 vc_cfg.dev_type = DEV_JSWM;//金山完美 - //vc_cfg.dev_type = DEV_ALY;//阿里云 + // vc_cfg.dev_type = DEV_ALY;//阿里云 strcpy(vc_cfg.server_ip,"amp.raisound.com");//服务器地址 - // strcpy(vc_cfg.server_ip,"115.190.94.208:22080");//服务器地址 - // strcpy(vc_cfg.server_ip,"118.145.158.207:22080");//服务器地址 - vc_cfg.VC_mode =2; //对话模式 + // ws://118.145.159.222:30700 + + // 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;//播放格式 vc_cfg.VC_record_samplerate = 16000;//录音采样率 vc_cfg.VC_play_samplerate = 16000;//播放采样率 - vc_cfg.is_VAD = IS_VAD; //=1打开vad打断 - vc_cfg.is_KWS = IS_KWS; //=1打开本地语言打断 - vc_cfg.speech_rate = 0; //语速配置 - vc_cfg.vad_silence = 800; //vad断句时间 ms 500 - vc_cfg.VC_TTS_volume = 80; //音量 -} + vc_cfg.is_VAD = IS_VAD;//=1打开vad打断 + vc_cfg.is_KWS = IS_KWS;//=1打开本地语言打断 + vc_cfg.speech_rate = 0;//语速配置 + vc_cfg.vad_silence = 800;//vad断句时间 ms + vc_cfg.VC_TTS_volume = 80;//音量 + + vc_cfg.alarm_count=0; +} int init_VC_cfg(void) { FILE *fp = NULL; @@ -366,6 +388,7 @@ int init_VC_cfg(void) // } reset_VC_cfg(); + if(fp) { int fread_len ; @@ -392,6 +415,8 @@ int init_VC_cfg(void) vc_cfg.VC_mode =2;//对话模式 reset_VC_cfg(); + // strcpy(vc_cfg.server_ip,"118.145.158.207:22080");//服务器地址 + return 0; } @@ -425,287 +450,578 @@ int startsave_tts=0; FILE *fp_tts_save = NULL; -u8 recv_get[1024]; +u8 recv_get[1024*6]; static int play_audio_file_event(char *file_name); u8 *save_temp_tts=NULL; -int is_guanji=0; - -int ddh_is_close_dialog=0; - -void aic_https_demotest(); - -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; - unsigned char is_save_VC_cfg = 0; - char tmp_data[70] = {0}; - int tmp_data_len = 0; - if(strstr(text,"声音改")) - { - if(strstr(text,"男")) - { - - } - } - - if(strstr(text,"音量查询") || strstr(text,"查询音量")) - { - - 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); - ret = 0; - goto RET; - } - // if(strstr(text,"加")||(strstr(text,"大")&&!strstr(text,"太")) || (strstr(text,"太小"))) - // { - // vc_cfg.VC_TTS_volume +=10; - // if(vc_cfg.VC_TTS_volume > 100) - // { - // vc_cfg.VC_TTS_volume = 100; - // AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); - // //AI_TTS_str - // }else{ - - // 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); - // } - // // if(vc_cfg.VC_mode != 0) - // app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - // // else{ - // // if(vc_cfg.VC_TTS_volume > 80) - // // { - // // app_audio_set_master_volume(15*50/100); - // // }else{ - - // // app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - // // } - // // } - - - // is_save_VC_cfg = 1; - // ret = 0; - // goto RET; - // }else if(strstr(text,"减")||(strstr(text,"小")&&!strstr(text,"太")) || (strstr(text,"太大"))) - // { - // vc_cfg.VC_TTS_volume -=10; - // if(vc_cfg.VC_TTS_volume < 0) - // { - // vc_cfg.VC_TTS_volume = 0; - // AI_AMP_MSG_put("音量已最小。",strlen("音量已最小。"),VC_TTS_PLAY); - // //AI_TTS_str - // }else{ - - // 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); - // } +void send_express_aimiaomiao(char *exp_str){ - // // if(vc_cfg.VC_mode != 0) - // app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - // // else{ + if(ui_uart_handle){ - // // if(vc_cfg.VC_TTS_volume > 80) - // // { + int send_aimiaomiao=0; + u8 cmd_ui[7]={0xAA,0x55,0xA5,0x02,0xA0,0x01,0x48}; + + + //AA 55 5A 02 81 01 DE + if(strstr(exp_str,"鲁鲁")||strstr(exp_str,"露露")||strstr(exp_str,"卡卡")||strstr(exp_str,"Neutral")||strstr(exp_str,"Relaxed")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x01; + cmd_ui[6]=0x48; + send_aimiaomiao=1; - // // app_audio_set_master_volume(15*50/100); - // // }else{ + }else + if(strstr(exp_str,"唱歌")||strstr(exp_str,"Winking")||strstr(exp_str,"Tired")||strstr(exp_str,"Smiling")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x02; + cmd_ui[6]=0x49; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"好丑")||strstr(exp_str,"Surprised")||strstr(exp_str,"Sad")||strstr(exp_str,"Pensive")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x03; + cmd_ui[6]=0x4A; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"好可爱")||strstr(exp_str,"Happy")||strstr(exp_str,"Grimacing")||strstr(exp_str,"Frowning")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x04; + cmd_ui[6]=0x4B; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"睡觉")) + { + cmd_ui[4]=0xA1; + cmd_ui[5]=0x05; + cmd_ui[6]=0x4D; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"摇头")||strstr(exp_str,"Doubtful")||strstr(exp_str,"dizzy")||strstr(exp_str,"Disgusted")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x06; + cmd_ui[6]=0x4D; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"点头")||strstr(exp_str,"Curious")||strstr(exp_str,"Confused")||strstr(exp_str,"Caring")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x07; + cmd_ui[6]=0x4E; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"摇尾巴")||strstr(exp_str,"Afraid")||strstr(exp_str,"Amused")||strstr(exp_str,"Angry")) + { + cmd_ui[4]=0xA0; + cmd_ui[5]=0x08; + cmd_ui[6]=0x4F; + send_aimiaomiao=1; + }else + if(strstr(exp_str,"ttsfinish")) + { + cmd_ui[4]=0xA2; + cmd_ui[5]=0x01; + cmd_ui[6]=0x4A; + send_aimiaomiao=1; + } + - // // app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - // // } - // // } + if(send_aimiaomiao==1){ - // is_save_VC_cfg = 1; - // ret = 0; - // goto RET; - // } - // } + bk_printf("send_express_aimiaomiao %s",exp_str); + bk_printf("uart_send %02X %02X %02X %02X %02X %02X %02X ...\n",cmd_ui[0],cmd_ui[1],cmd_ui[2],cmd_ui[3],cmd_ui[4],cmd_ui[5],cmd_ui[6]); - if(strstr(text,"切换")) - { - if(strstr(text,"对话模式") || strstr(text,"兑换模式")) - { - vc_cfg.VC_mode+=1; - if(vc_cfg.VC_mode > 2) - vc_cfg.VC_mode = 0; - bk_printf("切换对话模式:vc_cfg.VC_mode :%d",vc_cfg.VC_mode ); - 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 - { + uart_write(ui_uart_handle,cmd_ui, 7); - app_audio_set_master_volume(15*80/100); - AI_AMP_MSG_put("已切换到按键打断模式。我说话的时候需要短按按钮打断。",strlen("已切换到按键打断模式。我说话的时候需要短按按钮打断。"),VC_TTS_PLAY); - } - is_save_VC_cfg = 1; - ret = 0; - #if BK_SOCKET_IOT - IOT_update_statu(3,"",""); - #endif - goto RET; + } } - - } - - - // status +} +void send_express_str(char *exp_str){ - if(strstr(text,"关闭显示屏")) - { + #if 1 + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; + + if(strstr(exp_str,"不理解")) + { + cmd_ui[4]=0x01; + cmd_ui[6]=0x03; + }else + if(strstr(exp_str,"发怒")) + { + cmd_ui[4]=0x02; + cmd_ui[6]=0x04; + }else + if(strstr(exp_str,"搞怪")) + { + cmd_ui[4]=0x03; + cmd_ui[6]=0x05; + }else + if(strstr(exp_str,"花痴")) + { + cmd_ui[4]=0x04; + cmd_ui[6]=0x05; + }else + if(strstr(exp_str,"惊喜")) + { + cmd_ui[4]=0x05; + cmd_ui[6]=0x07; + }else + if(strstr(exp_str,"惊讶")) + { + cmd_ui[4]=0x06; + cmd_ui[6]=0x08; + }else + if(strstr(exp_str,"卡通")) + { + cmd_ui[4]=0x07; + cmd_ui[6]=0x09; + }else + if(strstr(exp_str,"开心")||strstr(exp_str,"微笑")) + { + cmd_ui[4]=0x08; + cmd_ui[6]=0x0a; + }else + if(strstr(exp_str,"生气")) + { + cmd_ui[4]=0x09; + cmd_ui[6]=0x0b; + }else + if(strstr(exp_str,"眨眼")) + { + cmd_ui[4]=0x0a; + cmd_ui[6]=0x0c; + }else + if(strstr(exp_str,"很开心")) + { + cmd_ui[4]=0x0b; + cmd_ui[6]=0x0d; + }else + if(strstr(exp_str,"中立")) + { + cmd_ui[4]=0x0c; + cmd_ui[6]=0x0e; + } - bk_printf("关闭显示屏"); + uart_write(ui_uart_handle,cmd_ui, 7); + } - aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_LOW );//lcd open ldo - ret = 0; - goto RET; - } + #else - if(strstr(text,"打开显示屏")) - { + // 淡定:Calm + // 尴尬:Embarrass + // 开心:Happy + // 可爱:Cute - bk_printf("打开显示屏"); - aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH );//lcd open ldo - ret = 0; - goto RET; - } + + // 孝豚 + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; + + // if(strstr(exp_str,"Calm")) + // { + // cmd_ui[4]=0x01; + // cmd_ui[6]=0x03; + // }else + // if(strstr(exp_str,"Embarrass")) + // { + // cmd_ui[4]=0x02; + // cmd_ui[6]=0x04; + // }else + // if(strstr(exp_str,"Happy")) + // { + // cmd_ui[4]=0x03; + // cmd_ui[6]=0x05; + // }else + // if(strstr(exp_str,"Cute")) + // { + // cmd_ui[4]=0x04; + // cmd_ui[6]=0x06; + // } - if(strstr(text,"升级版本")) - { + // 01-Afraid 害怕-右 +// 02-Amused 有趣-右 +// 03-Angry 愤怒-右 +// 04-Bored 无聊-右 +// 05-Caring 关爱-右 +// 06-Confused 困惑-右 +// 07-Curious 惊喜-右 +// 08-Disgusted 厌恶-右 +// 09-dizzy 蚊香眼-右 +// 10-Doubtful 存疑-右 +// 11-Frowning 皱眉头-右 +// 12-Grimacing 苦着脸-右 +// 13-Happy 开心-右 +// 14-Neutral 中立-右 +// 15-Pensive 悲伤-右 +// 16-Relaxed 放松-右 +// 17-Sad 伤心-右 +// 18-Smiling 微笑-右 +// 19-Surprised 惊讶-右 +// 20-Tired 疲惫-右 +// 21-Winking 眨眼-右 - AI_AMP_MSG_put("开始升级版本,大概需要几分钟",strlen("开始升级版本,大概需要几分钟"),VC_TTS_PLAY); - ret = 0; - goto RET; - } -//aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH );//lcd open ldo - if(strstr(text,"结束对话")||strstr(text,"关闭对话")||strstr(text,"退出对话")) - { +// 01-Afraid 害怕-右 01 +// 02-Amused 有趣-右 02 +// 03-Angry 愤怒-右 15 +// 04-Bored 无聊-右 16 +// 05-Caring 关爱-右 05 +// 06-Confused 困惑-右 17 +// 07-Curious 惊喜-右 19 +// 08-Disgusted 厌恶-右 20 +// 09-dizzy 蚊香眼-右 21 +// 10-Doubtful 存疑-右 18 +// 11-Frowning 皱眉头-右 03 +// 12-Grimacing 苦着脸-右 04 +// 13-Happy 开心-右 12 +// 14-Neutral 中立-右 06 +// 15-Pensive 悲伤-右 07 +// 16-Relaxed 放松-右 08 +// 17-Sad 伤心-右 09 +// 18-Smiling 微笑-右 10 +// 19-Surprised 惊讶-右 11 +// 20-Tired 疲惫-右 13 +// 21-Winking 眨眼-右 14 - // AI_AMP_MSG_put("拜拜",strlen("拜拜"),VC_TTS_PLAY); + bk_printf("send_express_str:text:%s",exp_str); - // aos_sleep(1000); + if(strstr(exp_str,"Afraid")||strstr(exp_str,"害怕")) + { + cmd_ui[4]=0x01; + cmd_ui[6]=0x03; + }else + if(strstr(exp_str,"Amused")||strstr(exp_str,"有趣")) + { + cmd_ui[4]=0x02; + cmd_ui[6]=0x04; + }else + if(strstr(exp_str,"Angry")||strstr(exp_str,"愤怒")) + { + cmd_ui[4]=0x03; + cmd_ui[6]=0x05; + }else + if(strstr(exp_str,"Bored")||strstr(exp_str,"无聊")) + { + cmd_ui[4]=0x04; + cmd_ui[6]=0x06; + }else + if(strstr(exp_str,"Caring")||strstr(exp_str,"关爱")) + { + cmd_ui[4]=0x05; + cmd_ui[6]=0x07; + }else + if(strstr(exp_str,"Confused")||strstr(exp_str,"困惑")) + { + cmd_ui[4]=0x06; + cmd_ui[6]=0x08; + }else + if(strstr(exp_str,"Curious")||strstr(exp_str,"惊喜")) + { + cmd_ui[4]=0x07; + cmd_ui[6]=0x09; + }else + if(strstr(exp_str,"Disgusted")||strstr(exp_str,"厌恶")) + { + cmd_ui[4]=0x08; + cmd_ui[6]=0x0a; + }else + if(strstr(exp_str,"dizzy")||strstr(exp_str,"蚊香眼")) + { + cmd_ui[4]=0x09; + cmd_ui[6]=0x0b; + }else + if(strstr(exp_str,"Doubtful")||strstr(exp_str,"存疑")) + { + cmd_ui[4]=0x0a; + cmd_ui[6]=0x0c; + }else + if(strstr(exp_str,"Frowning")||strstr(exp_str,"皱眉头")) + { + cmd_ui[4]=0x0b; + cmd_ui[6]=0x0d; + }else + if(strstr(exp_str,"Grimacing")||strstr(exp_str,"苦着脸")) + { + cmd_ui[4]=0x0c; + cmd_ui[6]=0x0e; + }else + if(strstr(exp_str,"Happy")||strstr(exp_str,"开心")) + { + cmd_ui[4]=0x0d; + cmd_ui[6]=0x0f; + }else + if(strstr(exp_str,"Neutral")||strstr(exp_str,"中立")) + { + cmd_ui[4]=0x0e; + cmd_ui[6]=0x10; + }else + if(strstr(exp_str,"Pensive")||strstr(exp_str,"悲伤")) + { + cmd_ui[4]=0x0f; + cmd_ui[6]=0x11; + }else + if(strstr(exp_str,"Relaxed")||strstr(exp_str,"放松")) + { + cmd_ui[4]=0x10; + cmd_ui[6]=0x12; + }else + if(strstr(exp_str,"Sad")||strstr(exp_str,"伤心")) + { + cmd_ui[4]=0x11; + cmd_ui[6]=0x13; + }else + if(strstr(exp_str,"Smiling")||strstr(exp_str,"微笑")) + { + cmd_ui[4]=0x12; + cmd_ui[6]=0x14; + }else + if(strstr(exp_str,"Surprised")||strstr(exp_str,"惊讶")) + { + cmd_ui[4]=0x13; + cmd_ui[6]=0x15; + }else + if(strstr(exp_str,"Tired")||strstr(exp_str,"疲惫")) + { + cmd_ui[4]=0x14; + cmd_ui[6]=0x16; + }else + if(strstr(exp_str,"Winking")||strstr(exp_str,"眨眼")) + { + cmd_ui[4]=0x15; + cmd_ui[6]=0x17; + } - is_dialog_status=1; + uart_write(ui_uart_handle,cmd_ui, 7); + } - ddh_is_close_dialog=1; - // AI_AMP_MSG_put(0,0,VC_CLOSE); + #endif + - // ret = 0; - // goto RET; - } +} - if(strstr(text,"帮我生成语音文件")) - { +static void AI_VC_send_music_start(char *urlpath); - AI_AMP_MSG_put(text,strlen(text),VC_TTS_PLAY); +void aic_https_demotest(); - ret = 0; - goto RET; - } +int32_t play_audio_done_duplex(const char *buf, uint32_t buf_len, uint32_t sample_rate, uint32_t is_file); - //||strstr(text,"做一个惊讶的表情")||strstr(text,"做一个开心的表情") +int mp3_test(); - if(strstr(text,"做一个")&&strstr(text,"表情")) - { - // need_express_cmd=1; - AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); +void resetClose_time(){ - send_express_str(text); + close_timerout=360; +} +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; + unsigned char is_save_VC_cfg = 0; + char tmp_data[70] = {0}; + int tmp_data_len = 0; + if(strstr(text,"声音改")) + { + if(strstr(text,"男")) + { - ret = 0; - goto RET; + } } - - if(strstr(text,"做一个发怒的表情")) + if(strstr(text,"音量") || strstr(text,"声音")) { - AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + if(strstr(text,"加")||(strstr(text,"大")&&!strstr(text,"太")) || (strstr(text,"太小"))) + { + vc_cfg.VC_TTS_volume +=10; + if(vc_cfg.VC_TTS_volume > 100) + { + vc_cfg.VC_TTS_volume = 100; + AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); + //AI_TTS_str + }else{ - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x02,0x01,0x04}; - uart_write(ui_uart_handle,cmd_ui, 7); - } + 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); + } + // if(vc_cfg.VC_mode != 0) + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - ret = 0; - goto RET; + + is_save_VC_cfg = 1; + ret = 0; + goto RET; + }else if(strstr(text,"减")||(strstr(text,"小")&&!strstr(text,"太")) || (strstr(text,"太大"))) + { + vc_cfg.VC_TTS_volume -=10; + if(vc_cfg.VC_TTS_volume < 0) + { + vc_cfg.VC_TTS_volume = 0; + AI_AMP_MSG_put("音量已最小。",strlen("音量已最小。"),VC_TTS_PLAY); + //AI_TTS_str + }else{ + + 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); + } + + //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; + } } - if(strstr(text,"做一个惊讶的表情")) + if(strstr(text,"切换")) { + if(strstr(text,"对话模式") || strstr(text,"兑换模式")) + { + // vc_cfg.VC_mode+=1; + if(vc_cfg.VC_mode > 2) + vc_cfg.VC_mode = 0; + bk_printf("切换对话模式:vc_cfg.VC_mode :%d",vc_cfg.VC_mode ); + if(vc_cfg.VC_mode == 0) + { + AI_AMP_MSG_put("已切换到连续对话模式。",strlen("已切换到连续对话模式。"),VC_TTS_PLAY); - 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 + { - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x06,0x01,0x08}; - uart_write(ui_uart_handle,cmd_ui, 7); + 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,"",""); + IOT_V3_update_statu(3,"",""); + goto RET; } + + } + + + // status + if(strstr(text,"升级版本")) + { + + AI_AMP_MSG_put("开始升级版本,大概需要几分钟",strlen("开始升级版本,大概需要几分钟"),VC_TTS_PLAY); ret = 0; goto RET; } - - if(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,0x03,0x01,0x05}; - uart_write(ui_uart_handle,cmd_ui, 7); - } + AI_AMP_MSG_put(text,strlen(text),VC_TTS_PLAY); ret = 0; goto RET; } - if(strstr(text,"做一个卡通的表情")) + + if(strstr(text,"结束对话")||strstr(text,"关闭对话")||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,0x07,0x01,0x09}; - uart_write(ui_uart_handle,cmd_ui, 7); - } + // aos_sleep(1000); - ret = 0; - goto RET; + is_dialog_status=1; + + // AI_AMP_MSG_put(0,0,VC_CLOSE); + + // ret = 0; + // goto RET; } - if(strstr(text,"做一个花痴的表情")) + //||strstr(text,"做一个惊讶的表情")||strstr(text,"做一个开心的表情") + + // 淡定:Calm + // 尴尬:Embarrass + // 开心:Happy + // 可爱:Cute + + + + + if(strstr(text,"做一个")&&strstr(text,"表情")) { + need_express_cmd=1; + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + + send_express_str(text); - AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + 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,0x01,0x01,0x03}; + 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,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); + if(ui_uart_handle){ + u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x03,0x01,0x05}; + 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,0x04,0x01,0x06}; uart_write(ui_uart_handle,cmd_ui, 7); } - - ret = 0; - goto RET; + ret = 0; + goto RET; } - if(strstr(text,"做一个惊喜的表情")) + + + if(strstr(text,"做一个惊喜的表情")) { AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); @@ -808,177 +1124,119 @@ int check_text(char *text,int ret_str_len) u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x01,0x01,0x03}; uart_write(ui_uart_handle,cmd_ui, 7); } - #if SUPPORT_MOT + #if SUPPORT_MOT mot_nod_exp(8,2); #endif ret = 0; goto RET; } - if(strstr(text,"打开触摸动作")) + if(strstr(text,"做一个发怒的表情")) { + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); - chumo_exp_open=1; - AI_AMP_MSG_put("触摸动作已打开",strlen("触摸动作已打开"),VC_TTS_PLAY); - - ret = 0; - goto RET; + send_express_str(text); + ret = 0; + goto RET; } - - if(strstr(text,"关闭触摸动作")) + if(strstr(text,"做一个惊讶的表情")) { - chumo_exp_open=0; - AI_AMP_MSG_put("触摸动作已关闭",strlen("触摸动作已关闭"),VC_TTS_PLAY); + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); - ret = 0; - goto RET; + send_express_str(text); + ret = 0; + goto RET; } + if(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,0x01,0x01,0x03}; - // uart_write(ui_uart_handle,cmd_ui, 7); - // } - - // bk_printf("摇头"); - // #if SUPPORT_MOT - // mot_nod_diantou(2); - // #endif - // 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,0x08,0x01,0x0a}; - // // uart_write(ui_uart_handle,cmd_ui, 7); - // // } - // bk_printf("点头"); - // #if SUPPORT_MOT - // mot_nod_yaotou(2); - // #endif - // ret = 0; - // goto RET; - - // } - - - - - // if(strstr(text,"抬头")) - // { - // AI_AMP_MSG_put("开始抬头了",strlen("开始抬头了"),VC_TTS_PLAY); - - // bk_printf("抬头"); - // #if SUPPORT_MOT - // mot_nod_ditou(2); - // #endif - // ret = 0; - // goto RET; - - // } - - // if(strstr(text,"低头")) - // { - // AI_AMP_MSG_put("开始低头了",strlen("开始低头了"),VC_TTS_PLAY); + AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); - // bk_printf("低头"); - // #if SUPPORT_MOT - // mot_nod_taitou(2); - // #endif - // ret = 0; - // goto RET; + send_express_str(text); - // } + ret = 0; + goto RET; + } - // if(strstr(text,"向左转")) - // { - // AI_AMP_MSG_put("开始向左转",strlen("开始向左转"),VC_TTS_PLAY); - // bk_printf("向左转"); - // #if SUPPORT_MOT - // mot_nod_exp(15,2); - // #endif - // ret = 0; - // goto RET; + // if(strstr(text,"播放音乐")) + // { - // } + // AI_AMP_MSG_put("开始做表情了",strlen("开始做表情了"),VC_TTS_PLAY); + // send_express_str(text);浦幼绘本视频 + // AI_VC_send_music_start("https://video.teacherasked.cn/Read_bank/read_xspy202504/%E6%B5%A6%E5%B9%BC%E7%BB%98%E6%9C%AC%E8%A7%86%E9%A2%91/a20f2c5d-e41c-414c-b407-92ca37d54391.opus"); - // if(strstr(text,"向右转")) - // { - // AI_AMP_MSG_put("开始向右转",strlen("开始向右转"),VC_TTS_PLAY); + // AI_VC_send_music_start(""); + // ret = 0; + // goto RET; + // } - // bk_printf("向右转"); - // #if SUPPORT_MOT - // mot_nod_exp(16,2); - // #endif - // ret = 0; - // goto RET; + if(strstr(text,"取消所有闹钟")) + { - // } + AI_AMP_MSG_put("好的",strlen("好的"),VC_TTS_PLAY); + // send_express_str(text);浦幼绘本视频 + // AI_VC_send_music_start("https://video.teacherasked.cn/Read_bank/read_xspy202504/%E6%B5%A6%E5%B9%BC%E7%BB%98%E6%9C%AC%E8%A7%86%E9%A2%91/a20f2c5d-e41c-414c-b407-92ca37d54391.opus"); + vc_cfg.alarm_count=0; + save_VC_cfg(); + // AI_VC_send_music_start(""); + ret = 0; + goto RET; + } - if(strstr(text,"查询马达状态")) + if(strstr(text,"获取设备状态")) { - mot_nod_status(); + // mot_nod_status(); - // aic_http_demotest(); + aic_https_demotest_status(); 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){ + + if(strstr(text,"播放文件")) + { + int len_daochu=0,sum_len=0;; + // FILE *fp_play_local=fopen("C:/test12312.mp3","rb"); - // len_daochu=fread(recv_get,1,1024,fp_play_local); + // if(fp_play_local){ + // while(1){ - // if(len_daochu>0){ - // sum_len+=len_daochu; - // audio_data(recv_get,len_daochu,22); + // len_daochu=fread(recv_get,1,1024*6,fp_play_local); - // }else{ + // bk_printf("read mp3 len is %d ...",len_daochu); - // break; + // if(len_daochu>0){ + // sum_len+=len_daochu; + // // audio_data(recv_get,len_daochu,22); + // // play_audio_done_duplex((const char *)recv_get, len_daochu, 44100, 1); - // } + // }else{ - - // } + // break; + // } + // } - // bk_printf("daochu len is %d ...",sum_len); - // fclose(fp_play_local); + // bk_printf("daochu len is %d ...",sum_len); + // fclose(fp_play_local); - // } - // ret = 0; - // goto RET; - // } + // } + bk_printf("mp3_test start.....\n"); + mp3_play_file("C:/test12312.mp3"); + ret = 0; + goto RET; + } if(strstr(text,"查询")) @@ -1163,25 +1421,24 @@ int check_text(char *text,int ret_str_len) } if(ret_str_len<16 && (strstr(text,"再见") || strstr(text,"拜拜") || strstr(text,"关闭对话"))) { - // tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"再见了,我会想你的。"); - // AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY_CLOSE); - is_dialog_status=1; - ddh_is_close_dialog=1; + tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"再见了,我会想你的。"); + AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY_CLOSE); } - if(ret_str_len<16 && (strstr(text,"关机") )) - { - tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"正在关机。"); - - is_guanji=1; - AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY_POWER_OFF); + if(ret_str_len<10 && (strstr(text,"关机") )) + { + // bk_printf("the guanji strlen is %d...\n",strlen(text)); + // tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"正在关机。"); + // AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY_POWER_OFF); } - if(ret_str_len<16 && (strstr(text,"重启") )) + + if(ret_str_len<10 && (strstr(text,"重启") )) { tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"正在重启。"); AI_AMP_MSG_put(tmp_data,tmp_data_len,VC_TTS_PLAY_RESET); } + if(ret_str_len<24 && (strstr(text,"恢复出厂") )) { reset_VC_cfg(); @@ -1242,12 +1499,26 @@ static void AI_client_VC_synthesis(char *str) static void AI_VC_send_satrt(char is_greeting) { // 创建根对象 + + is_music_status=0; + + // is_offlinecmd_status=0; + cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "event", "start"); // 创建 payload 对象 cJSON *payload = cJSON_CreateObject(); cJSON_AddStringToObject(payload, "device_id", vc_cfg.dev_imei); + + if(is_newsession) + cJSON_AddBoolToObject(payload, "is_new_session", TRUE); + else + cJSON_AddBoolToObject(payload, "is_new_session", FALSE); + + is_newsession=false; + + cJSON_AddStringToObject(payload, "user_agent", "AudioClient/1.0"); cJSON_AddBoolToObject(payload, "simple_response", FALSE); if(AI_ASR_str && VAD_time_count !=0 && VAD_time_count <= VAD_TIMEOUT )//时间间隔短,认为是一次对话,加上上一次内容 @@ -1276,20 +1547,18 @@ static void AI_VC_send_satrt(char is_greeting) // 创建 custom_variables 对象 cJSON *custom_variables = cJSON_CreateObject(); - cJSON_AddStringToObject(custom_variables, "name", "食梦貘"); - - // cJSON_AddStringToObject(custom_variables, "coze_current_time", "23:00"); - - // cJSON_AddStringToObject(custom_variables, "coze_current_date", "2025-10-01"); - cJSON_AddItemToObject(dialog_cfg, "custom_variables", custom_variables); // 创建 greeting 对象 cJSON *greeting = cJSON_CreateObject(); + + + // is_greeting=0; + if(is_greeting == 1) { - cJSON_AddBoolToObject(greeting, "enabled", FALSE);//是否播放提示词 + cJSON_AddBoolToObject(greeting, "enabled", FALSE);//是否播放提示词 //TRUE cJSON_AddStringToObject(greeting, "content", "你好"); cJSON_AddItemToObject(dialog_cfg, "greeting", greeting); }else @@ -1316,18 +1585,40 @@ static void AI_VC_send_satrt(char is_greeting) }else { cJSON_AddBoolToObject(bot_option, "use_workflow", 1); - cJSON_AddStringToObject(bot_option, "bot_id", "7491256313561825332");//食梦貘 + cJSON_AddStringToObject(bot_option, "bot_id", "7491256313561825332");//食梦貘 7491256313561825332 + + //7574309992161886227 //7491256313561825332 cJSON_AddStringToObject(bot_option, "workflow_id", "7508598451564150820"); } cJSON_AddItemToObject(dialog_cfg, "bot_option", bot_option); - cJSON *dify_option = cJSON_CreateObject(); + + // "dialog_attributes": { + // "cfg": { + // "dify_option": { + // "inputs": { + // "devToken": "xxxxxxxxxxxxx", + // "deviceId": "xxxxxxxxxx" + // } + // } + // } + // }, + + cJSON *dify_option = cJSON_CreateObject(); cJSON *inputs_option = cJSON_CreateObject(); cJSON_AddStringToObject(inputs_option, "devToken", get_token());//奥德赛 cJSON_AddStringToObject(inputs_option, "deviceId", vc_cfg.dev_imei); + // if(is_dialog_status==0) + // cJSON_AddStringToObject(inputs_option, "open_cmd", "1"); + // else{ + + cJSON_AddStringToObject(inputs_option, "open_cmd", "1"); + + // } + // cJSON_AddStringToObject(custom_variables, "coze_current_time", "23:00"); // cJSON_AddStringToObject(inputs_option, "coze_current_date", "2025-10-01"); @@ -1369,11 +1660,12 @@ static void AI_VC_send_satrt(char is_greeting) { cJSON_AddStringToObject(outbound_cfg, "format", "opus");//ogg_opus opus - // if(codec_tts==1) + if(codec_tts==1) cJSON_AddStringToObject(outbound_cfg, "codec", "raw");// ogg - // else - // cJSON_AddStringToObject(outbound_cfg, "codec", "ogg");// ogg - cJSON_AddNumberToObject(outbound_cfg, "compression_rate", 20); + else + cJSON_AddStringToObject(outbound_cfg, "codec", "ogg");// ogg + + cJSON_AddNumberToObject(outbound_cfg, "compression_rate", 20); } else cJSON_AddStringToObject(outbound_cfg, "format", "pcm"); @@ -1464,137 +1756,315 @@ static void AI_VC_send_satrt(char is_greeting) cJSON_Delete(root); } +//{"cmd":"renew_device"} -//Audio.Create 开始发送音频 -static void AI_VC_send_audio_create(void) -{ - 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.create"); + char mp3_path_str[500]; - // 创建 "payload" 对象 - cJSON *payload = cJSON_CreateObject(); - if (payload == NULL) { - bk_printf("Failed to create payload object.\n"); - cJSON_Delete(root); - return ; - } - // 将 "payload" 对象添加到根对象中 - cJSON_AddItemToObject(root, "payload", payload); + void aimaimiao_hande_cmd(char *cmdstr){ - // 将 JSON 对象转换为字符串 - char *json_str = cJSON_Print(root); - if (json_str == NULL) { - bk_printf("Failed to print JSON object.\n"); - cJSON_Delete(root); + // if(strstr(cmdstr,"鲁鲁")||strstr(text,"露露")||strstr(text,"卡卡")||strstr(text,"唱歌")||strstr(text,"好丑")||strstr(text,"好可爱")||strstr(text,"睡觉")||strstr(text,"点头")||strstr(text,"摇头")||strstr(text,"摇尾巴")){ - } - AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - + // if(ret_str_len<20){ + + need_express_cmd=1; + + send_express_aimiaomiao(cmdstr); + + if(strstr(cmdstr,"鲁鲁")||strstr(cmdstr,"露露")||strstr(cmdstr,"卡卡")) + { + mp3_play_file("C:/am_003.mp3"); + }else + if(strstr(cmdstr,"唱歌")) + { + mp3_play_file("C:/am_007.mp3"); + }else + if(strstr(cmdstr,"好丑")) + { + mp3_play_file("C:/am_008.mp3"); + }else + if(strstr(cmdstr,"好可爱")) + { + mp3_play_file("C:/am_012.mp3"); + }else + if(strstr(cmdstr,"睡觉")) + { + mp3_play_file("C:/am_006.mp3"); + }else + if(strstr(cmdstr,"点头")) + { + mp3_play_file("O:/am_002.mp3"); + }else + if(strstr(cmdstr,"摇头")) + { + mp3_play_file("O:/am_004.mp3"); + }else + if(strstr(cmdstr,"摇尾巴")) + { + mp3_play_file("C:/am_005.mp3"); + }else{ + + mp3_play_file("C:/am_003.mp3"); + } - // 释放内存 - free(json_str); - cJSON_Delete(root); -} + // is_offlinecmd_status=1; + // ret = 0; + play_data_len = 10*1024;//让音频播放起来 + VC_state = VC_State_End; + //goto RET; -// { -// "event": "audio.convert", -// "payload": { -// "audio_convert_request": { -// "audio_url": audio_url, -// "bit_depth": 16, -// "channels": 1, -// "sample_rate": 16000, -// "target_format": "pcm" -// } -// } -// } + // } + // } -static void AI_VC_send_startup(void) + } + +void deal_iot_receive(char *json_data) { - 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", "prompt"); + bk_printf(json_data); - // 创建 "payload" 对象 - cJSON *payload = cJSON_CreateObject(); - if (payload == NULL) { - bk_printf("Failed to create payload object.\n"); - cJSON_Delete(root); - return ; - } + // vc_cfg.VC_TTS_volume = 100; + // AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); - + //{"cmd":"emotion","msg":"AI指令:emotion","data":"Cute","ts":1764914695122,"targetDeviceId":"865530078570777"} - cJSON *promat = cJSON_CreateObject(); - if (payload == NULL) { - bk_printf("Failed to create payload object.\n"); - cJSON_Delete(root); - return ; - } + if(strstr(json_data,"action")){ + bk_printf("action"); - cJSON *interrupt_keywords = cJSON_CreateArray(); - // cJSON_AddStringToObject(promat, "event", "startup"); + char *str_tmp = NULL; - //cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("停止")); - //cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("打断")); - // cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("等一下")); + char action_str[500]={0}; - cJSON *content = cJSON_CreateObject(); + str_tmp = strstr(json_data,"\"data\":\""); + + // bk_printf("str_tmp:%p",str_tmp); + if(str_tmp)//解析password + { + str_tmp += strlen("\"data\":\""); + int count = str_count(str_tmp,'\"'); + bk_printf("count:%d",count); + if(count < sizeof(action_str)) + { - cJSON_AddStringToObject(content, "role", "user"); + + memcpy(action_str,str_tmp,count); + action_str[count] = 0; + bk_printf("action_str:%s",action_str); - cJSON_AddStringToObject(content, "content", "{\"event\":\"startup\"}"); + // send_express_str(action_str); - cJSON_AddStringToObject(content, "content_type", "text"); + aimaimiao_hande_cmd(action_str); + } - cJSON_AddItemToArray(interrupt_keywords, content); + } + } - cJSON_AddItemToObject(payload, "prompt", interrupt_keywords); - //cJSON_AddItemToObject(inbound_attributes, "interrupt_options", interrupt_options); + if(strstr(json_data,"emotion")){ - + bk_printf("emotion"); + char *str_tmp = NULL; - // cJSON_AddItemToObject(payload, "prompt", promat); - // 将 "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); + char emotion_str[500]={0}; + + str_tmp = strstr(json_data,"\"data\":\""); + + bk_printf("str_tmp:%p",str_tmp); + + if(str_tmp)//解析password + { + + str_tmp += strlen("\"data\":\""); + int count = str_count(str_tmp,'\"'); + bk_printf("count:%d",count); + if(count < sizeof(emotion_str)) + { + + + memcpy(emotion_str,str_tmp,count); + emotion_str[count] = 0; + bk_printf("emotion_str:%s",emotion_str); + + if(need_express_cmd==0&&is_offlinecmd_status==0) + { + send_express_aimiaomiao(emotion_str); + } + // send_express_str(emotion_str); + } + + } + + } + + if(strstr(json_data,"tts_audio_url")){ + + bk_printf("tts_audio_url"); + + char *str_tmp = NULL; + + memset(mp3_path_str,0,500); + + str_tmp = strstr(json_data,"\"data\":\""); + // bk_printf("str_tmp:%p",str_tmp); + + if(str_tmp)//解析password + { + + str_tmp += strlen("\"data\":\""); + int count = str_count(str_tmp,'\"'); + bk_printf("count:%d",count); + if(count < sizeof(mp3_path_str)) + { + + + memcpy(mp3_path_str,str_tmp,count); + mp3_path_str[count] = 0; + bk_printf("mp3_path_str:%s",mp3_path_str); + + // is_music_status=1; + + // AI_AMP_MSG_put("请稍后,现在为你查找音乐",strlen("请稍后,现在为你查找音乐"),VC_TTS_PLAY); + + // aos_sleep(4000); + + need_play_is_music_status=1; + + AI_AMP_MSG_put(0,0,VC_Cancel);//发送打断 + // AI_VC_send_music_start(mp3_path_str); + + } + } + + // IOT_update_statu(3,"",""); + + // AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + } - AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - - // 释放内存 - free(json_str); - cJSON_Delete(root); -} -//Music.Start 开始发送音频 -static void AI_VC_send_music_start(char *urlpath) + if(strstr(json_data,"get_device_state")){ + + bk_printf("get_device_state"); + + // IOT_update_statu(3,"",""); + + IOT_V3_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); + } + + IOT_V3_update_statu(3,"",""); + save_VC_cfg(); + + } + + + if(strstr(json_data,"alarm_add")||strstr(json_data,"alarm_del")||strstr(json_data,"alarm_clear")) + { + + AI_AMP_MSG_put(0,0,AI_ALARM_UPDATE); + + } + + 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) { VC_state = VC_State_SpeechStart;//开始说 // 创建根对象 @@ -1605,7 +2075,7 @@ static void AI_VC_send_music_start(char *urlpath) } // 设置 "event" 字段 - cJSON_AddStringToObject(root, "event", "audio.convert"); + cJSON_AddStringToObject(root, "event", "audio.create"); // 创建 "payload" 对象 cJSON *payload = cJSON_CreateObject(); @@ -1614,30 +2084,6 @@ static void AI_VC_send_music_start(char *urlpath) 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); @@ -1656,10 +2102,25 @@ static void AI_VC_send_music_start(char *urlpath) cJSON_Delete(root); } -//Audio.Completed 结束发送音频 -static void AI_VC_send_audio_completed(void) + +// { +// "event": "prompt", +// "payload": { +// "prompt": [ +// { +// "role": "user", +// "content": "今天是礼拜几?", +// "content_type": "text", // 可以不提交 +// "type": "question" // 可以不提交 +// } +// ] +// } +// } + +//Audio.Create 开机json {"event":"startup"} +static void AI_VC_send_startup(void) { - VC_state = VC_State_SpeechEnd;//说结束 + VC_state = VC_State_SpeechStart;//开始说 // 创建根对象 cJSON *root = cJSON_CreateObject(); if (root == NULL) { @@ -1668,7 +2129,7 @@ static void AI_VC_send_audio_completed(void) } // 设置 "event" 字段 - cJSON_AddStringToObject(root, "event", "audio.completed"); + cJSON_AddStringToObject(root, "event", "prompt"); // 创建 "payload" 对象 cJSON *payload = cJSON_CreateObject(); @@ -1677,21 +2138,185 @@ static void AI_VC_send_audio_completed(void) cJSON_Delete(root); return ; } - // 将 "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 *promat = cJSON_CreateObject(); + if (payload == NULL) { + bk_printf("Failed to create payload object.\n"); cJSON_Delete(root); - + return ; } - AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - // 释放内存 - free(json_str); - cJSON_Delete(root); -} + + + cJSON *interrupt_keywords = cJSON_CreateArray(); + // cJSON_AddStringToObject(promat, "event", "startup"); + + //cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("停止")); + //cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("打断")); + // cJSON_AddItemToArray(interrupt_keywords, cJSON_CreateString("等一下")); + + cJSON *content = cJSON_CreateObject(); + + + + cJSON_AddStringToObject(content, "role", "user"); + + cJSON_AddStringToObject(content, "content", "{\"event\":\"startup\"}"); + + cJSON_AddStringToObject(content, "content_type", "text"); + + cJSON_AddItemToArray(interrupt_keywords, content); + + + cJSON_AddItemToObject(payload, "prompt", interrupt_keywords); + + //cJSON_AddItemToObject(inbound_attributes, "interrupt_options", interrupt_options); + + + + + // cJSON_AddItemToObject(payload, "prompt", promat); + // 将 "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); +} + + + + +// { +// "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) +{ + + is_music_status=1; + + VC_state = VC_State_End;//开始说 + // 创建根对象 + 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");//opus//pcm + //}else{ + + cJSON_AddStringToObject(audio_convert_request, "target_format", "opus"); + // } + + + 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) +{ + + VC_state = VC_State_SpeechEnd;//说结束 + // 创建根对象 + + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf("Failed to create root object.\n"); + return ; + } + + // 设置 "event" 字段 + cJSON_AddStringToObject(root, "event", "audio.completed"); + + // 创建 "payload" 对象 + cJSON *payload = cJSON_CreateObject(); + if (payload == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; + } + // 将 "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); +} // //打断 // static void AI_VC_send_cancel(void) // { @@ -1748,8 +2373,6 @@ static void VC_TTS_Play(const char *str) //pinpin发送 void AI_VC_send_ping_ping(void) { - - // mot_nod_status(); AI_VC_send_data("ping",strlen("ping"),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 } @@ -1807,626 +2430,183 @@ static void AI_VC_send_task(void *thread_input)//websocket 发送线程 } if(AI_VC_send_queue_data.AIC_WEBSOCKET_OPCODE ==AIC_WEBSOCKET_OPCODE_TEXT_FRAME ) - bk_printf("send ret=%d,len=%d,str=%s",ret,AI_VC_send_queue_data.len,AI_VC_send_queue_data.data); - else if(AI_VC_send_queue_data.AIC_WEBSOCKET_OPCODE ==AIC_WEBSOCKET_OPCODE_BINARY_FRAME ) - { - bk_printf("send ret=%d,len=%d",ret,AI_VC_send_queue_data.len); - - // send_update+=AI_VC_send_queue_data.len; - - // send_count_pcm++; - - // 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); - - } - if(AI_VC_send_queue_data.data) - aic_free(AI_VC_send_queue_data.data); - }else - { - bk_printf("AI_VC_send ERROR:close_websocket_client = %d",AI_client_VC); - break; - } - - }else - { - aos_sleep(1); - } - } - bk_printf("AI_VC_send_task exit......"); - AI_VC_send_task_flag = 3; -} - - - -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; - - - - void send_express_str_chumo(int type,int mode){ - - - if(!ui_uart_handle) - return ; - - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; - if(type==1&&mode==0){ - - #if SUPPORT_MOT - mot_nod_exp(11,2); - #endif - - // sleep - cmd_ui[4]=0x07; - cmd_ui[6]=0x09; - - } - - if(type==1&&mode==1){ - - #if SUPPORT_MOT - mot_nod_exp(12,2); - #endif - - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; - - } - - if(type==2&&mode==0){ - - #if SUPPORT_MOT - mot_nod_exp(13,2); - #endif - - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - - } - - if(type==2&&mode==1){ - - #if SUPPORT_MOT - mot_nod_exp(14,2); - #endif - - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - - } - - if(type==0&&mode==0){ - - #if SUPPORT_MOT - mot_nod_exp(9,2); - #endif - - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; - - } - - if(type==0&&mode==1){ - - #if SUPPORT_MOT - mot_nod_exp(10,2); - #endif - - cmd_ui[4]=0x03; - cmd_ui[6]=0x05; - - } - - - - uart_write(ui_uart_handle,cmd_ui, 7); - } - -void back_middle_statu_mot(){ - - aos_sleep(1000); - mot_nod_exp(7,2); - -} - -void random_statu_mot(){ - - //aos_sleep(1000); - u32 count_tmp = 0; - aos_get_queue_used_count(AI_VC_play_queue,&count_tmp); - - bk_printf("random_statu_mot....\n"); - - if(count_tmp<=0) - return ; - //VC_END_play_timerout = 1; - - if(random_mt_test%3==0) - mot_nod_exp(8,2); - - if(random_mt_test%3==1) - mot_nod_exp(15,2); - - if(random_mt_test%3==2) - mot_nod_exp(16,2); - - random_mt_test++; - - -} - - -void send_express_str_xinxinmao(char *exp_str){ - - #if 1 - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; - - if(strstr(exp_str,"不理解")) - { - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - }else - if(strstr(exp_str,"发怒")) - { - cmd_ui[4]=0x02; - cmd_ui[6]=0x04; - }else - if(strstr(exp_str,"搞怪")) - { - cmd_ui[4]=0x03; - cmd_ui[6]=0x05; - }else - if(strstr(exp_str,"花痴")) - { - cmd_ui[4]=0x04; - cmd_ui[6]=0x05; - }else - if(strstr(exp_str,"惊喜")) - { - cmd_ui[4]=0x05; - cmd_ui[6]=0x07; - }else - if(strstr(exp_str,"惊讶")) - { - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; - }else - if(strstr(exp_str,"卡通")) - { - cmd_ui[4]=0x07; - cmd_ui[6]=0x09; - }else - if(strstr(exp_str,"开心")||strstr(exp_str,"微笑")) - { - cmd_ui[4]=0x08; - cmd_ui[6]=0x0a; - }else - if(strstr(exp_str,"生气")) - { - cmd_ui[4]=0x09; - cmd_ui[6]=0x0b; - }else - if(strstr(exp_str,"眨眼")) - { - cmd_ui[4]=0x0a; - cmd_ui[6]=0x0c; - }else - if(strstr(exp_str,"很开心")) - { - cmd_ui[4]=0x0b; - cmd_ui[6]=0x0d; - }else - if(strstr(exp_str,"中立")) - { - cmd_ui[4]=0x0c; - cmd_ui[6]=0x0e; - } - - uart_write(ui_uart_handle,cmd_ui, 7); - } - - #else - - // 淡定:Calm - // 尴尬:Embarrass - // 开心:Happy - // 可爱:Cute - - - - // 孝豚 - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; - - // if(strstr(exp_str,"Calm")) - // { - // cmd_ui[4]=0x01; - // cmd_ui[6]=0x03; - // }else - // if(strstr(exp_str,"Embarrass")) - // { - // cmd_ui[4]=0x02; - // cmd_ui[6]=0x04; - // }else - // if(strstr(exp_str,"Happy")) - // { - // cmd_ui[4]=0x03; - // cmd_ui[6]=0x05; - // }else - // if(strstr(exp_str,"Cute")) - // { - // cmd_ui[4]=0x04; - // cmd_ui[6]=0x06; - // } - - // 01-Afraid 害怕-右 -// 02-Amused 有趣-右 -// 03-Angry 愤怒-右 -// 04-Bored 无聊-右 -// 05-Caring 关爱-右 -// 06-Confused 困惑-右 -// 07-Curious 惊喜-右 -// 08-Disgusted 厌恶-右 -// 09-dizzy 蚊香眼-右 -// 10-Doubtful 存疑-右 -// 11-Frowning 皱眉头-右 -// 12-Grimacing 苦着脸-右 -// 13-Happy 开心-右 -// 14-Neutral 中立-右 -// 15-Pensive 悲伤-右 -// 16-Relaxed 放松-右 -// 17-Sad 伤心-右 -// 18-Smiling 微笑-右 -// 19-Surprised 惊讶-右 -// 20-Tired 疲惫-右 -// 21-Winking 眨眼-右 - -// 01-Afraid 害怕-右 01 -// 02-Amused 有趣-右 02 -// 03-Angry 愤怒-右 15 -// 04-Bored 无聊-右 16 -// 05-Caring 关爱-右 05 -// 06-Confused 困惑-右 17 -// 07-Curious 惊喜-右 19 -// 08-Disgusted 厌恶-右 20 -// 09-dizzy 蚊香眼-右 21 -// 10-Doubtful 存疑-右 18 -// 11-Frowning 皱眉头-右 03 -// 12-Grimacing 苦着脸-右 04 -// 13-Happy 开心-右 12 -// 14-Neutral 中立-右 06 -// 15-Pensive 悲伤-右 07 -// 16-Relaxed 放松-右 08 -// 17-Sad 伤心-右 09 -// 18-Smiling 微笑-右 10 -// 19-Surprised 惊讶-右 11 -// 20-Tired 疲惫-右 13 -// 21-Winking 眨眼-右 14 - - bk_printf("send_express_str:text:%s",exp_str); - - if(strstr(exp_str,"Afraid")||strstr(exp_str,"害怕")) - { - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - }else - if(strstr(exp_str,"Amused")||strstr(exp_str,"有趣")) - { - cmd_ui[4]=0x02; - cmd_ui[6]=0x04; - }else - if(strstr(exp_str,"Angry")||strstr(exp_str,"愤怒")) - { - cmd_ui[4]=0x0f; - cmd_ui[6]=0x11; - }else - if(strstr(exp_str,"Bored")||strstr(exp_str,"无聊")) - { - cmd_ui[4]=0x10; - cmd_ui[6]=0x12; - }else - if(strstr(exp_str,"Caring")||strstr(exp_str,"关爱")) - { - cmd_ui[4]=0x05; - cmd_ui[6]=0x07; - }else - if(strstr(exp_str,"Confused")||strstr(exp_str,"困惑")) - { - cmd_ui[4]=0x11; - cmd_ui[6]=0x13; - }else - if(strstr(exp_str,"Curious")||strstr(exp_str,"惊喜")) - { - cmd_ui[4]=0x13; - cmd_ui[6]=0x15; - }else - if(strstr(exp_str,"Disgusted")||strstr(exp_str,"厌恶")) - { - cmd_ui[4]=0x14; - cmd_ui[6]=0x16; - }else - if(strstr(exp_str,"dizzy")||strstr(exp_str,"蚊香眼")) - { - cmd_ui[4]=0x15; - cmd_ui[6]=0x17; - }else - if(strstr(exp_str,"Doubtful")||strstr(exp_str,"存疑")) - { - cmd_ui[4]=0x12; - cmd_ui[6]=0x14; - }else - if(strstr(exp_str,"Frowning")||strstr(exp_str,"皱眉头")) - { - cmd_ui[4]=0x03; - cmd_ui[6]=0x05; - }else - if(strstr(exp_str,"Grimacing")||strstr(exp_str,"苦着脸")) - { - cmd_ui[4]=0x04; - cmd_ui[6]=0x06; - }else - if(strstr(exp_str,"Happy")||strstr(exp_str,"开心")) - { - cmd_ui[4]=0x0c; - cmd_ui[6]=0x0e; - }else - if(strstr(exp_str,"Neutral")||strstr(exp_str,"中立")) - { - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; - }else - if(strstr(exp_str,"Pensive")||strstr(exp_str,"悲伤")) - { - cmd_ui[4]=0x07; - cmd_ui[6]=0x09; - }else - if(strstr(exp_str,"Relaxed")||strstr(exp_str,"放松")) - { - cmd_ui[4]=0x08; - cmd_ui[6]=0x0a; - }else - if(strstr(exp_str,"Sad")||strstr(exp_str,"伤心")) - { - cmd_ui[4]=0x09; - cmd_ui[6]=0x0b; - }else - if(strstr(exp_str,"Smiling")||strstr(exp_str,"微笑")) - { - cmd_ui[4]=0x0a; - cmd_ui[6]=0x0c; - }else - if(strstr(exp_str,"Surprised")||strstr(exp_str,"惊讶")) - { - cmd_ui[4]=0x0b; - cmd_ui[6]=0x0d; - }else - if(strstr(exp_str,"Tired")||strstr(exp_str,"疲惫")) - { - cmd_ui[4]=0x0d; - cmd_ui[6]=0x0f; - }else - if(strstr(exp_str,"Winking")||strstr(exp_str,"眨眼")) - { - cmd_ui[4]=0x0c; - cmd_ui[6]=0x0e; - } + bk_printf("send ret=%d,len=%d,str=%s",ret,AI_VC_send_queue_data.len,AI_VC_send_queue_data.data); + else if(AI_VC_send_queue_data.AIC_WEBSOCKET_OPCODE ==AIC_WEBSOCKET_OPCODE_BINARY_FRAME ) + { - uart_write(ui_uart_handle,cmd_ui, 7); - } + send_update+=AI_VC_send_queue_data.len; + bk_printf("send ret=%d,len=%d,%d",ret,AI_VC_send_queue_data.len,send_update); - #endif - + + // send_count_pcm++; + + // 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); + + } + if(AI_VC_send_queue_data.data) + aic_free(AI_VC_send_queue_data.data); + }else + { + bk_printf("AI_VC_send ERROR:close_websocket_client = %d",AI_client_VC); + break; + } + + }else + { + aos_sleep(1); + } + } + bk_printf("AI_VC_send_task exit......"); + AI_VC_send_task_flag = 3; } -void send_express_str(char *exp_str){ - // texts = ["neutral","happy","laughing","funny","sad","angry", - // "crying","loving","embarrassed","surprised","shocked","thinking", - // "winking","cool","relaxed","","confused",""] - /// happy 🙂 -//cue 😸 -//surprised 😲 -//angry 😠 -//crying 😭 -//smirk😏 -//dissatisfied😒 +int opus_encode_len=0,opus_decode_len=0; -// 001序列帧 开心002序列帧 卖萌003序列帧 惊喜004序列帧 生气004序列帧 右眼生气005序列帧 哭泣006序列帧 坏笑007序列帧 不满 - #if 1 - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x01,0x01,0x03}; - - if(strstr(exp_str,"开心")||strstr(exp_str,"happy")) - { - - #if SUPPORT_MOT - mot_nod_exp(8,2); - #endif - // sleep - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - }else - if(strstr(exp_str,"卖萌")||strstr(exp_str,"cue")) - { - - #if SUPPORT_MOT - mot_nod_exp(5,2); // mot_nod_exp(7,2); - #endif - cmd_ui[4]=0x02; - cmd_ui[6]=0x04; - }else - if(strstr(exp_str,"惊喜")||strstr(exp_str,"surprised")) - { - cmd_ui[4]=0x03; - cmd_ui[6]=0x05; +//#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; +} - #if SUPPORT_MOT - mot_nod_diantou(2); - #endif - }else - if(strstr(exp_str,"生气")||strstr(exp_str,"angry")) - { - cmd_ui[4]=0x04; - cmd_ui[6]=0x06; - #if SUPPORT_MOT - mot_nod_exp(6,2); - #endif - }else - if(strstr(exp_str,"哭泣")||strstr(exp_str,"crying")) - { - cmd_ui[4]=0x05; - cmd_ui[6]=0x07; +#define recv_buff_mxa_len 20*1024 - #if SUPPORT_MOT - mot_nod_taitou(2); - #endif + u32 timer_ms21=0; - }else - if(strstr(exp_str,"坏笑")||strstr(exp_str,"smirk")) - { - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; + u32 timer_ms22=0; - #if SUPPORT_MOT - mot_nod_exp(5,2); - #endif - }else - if(strstr(exp_str,"不满")||strstr(exp_str,"dissatisfied")) - { - cmd_ui[4]=0x07; - cmd_ui[6]=0x09; + static void AI_VC_IOT_task(void *thread_input)//websocket 读线程 + { + - #if SUPPORT_MOT - mot_nod_exp(4,2); - #endif + receive_iot_message(AI_client_IOT); - }else if(strstr(exp_str,"点头")){ + } - mot_nod_yaotou(2); - - }else if(strstr(exp_str,"摇头")){ - mot_nod_diantou(2); - - }else if(strstr(exp_str,"抬头")){ + static void AI_IOT_read_task(void *thread_input)//websocket 读线程 + { - mot_nod_ditou(2); - - }else if(strstr(exp_str,"低头")){ + int status; + static u8 *recv_buff = NULL; + + + if(recv_buff == NULL) + { + recv_buff = aic_malloc(recv_buff_mxa_len); + } + if(recv_buff == NULL) + { + bk_printf("AI_VC_read_task aic_malloc ERROR!!!!!!!!!!!!!!!!!!......"); + return; + } + WebSocketMessageType code; + bk_printf("AI_IOT_read_task go......"); + // AI_VC_read_task_flag = 1; + while (1) { + + if(AI_client_IOT) + { - mot_nod_taitou(2); - - }else if(strstr(exp_str,"向左转")){ + 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"); - mot_nod_exp(15,2); + // aos_sleep(50); - - }else if(strstr(exp_str,"向右转")){ + // u32 timer_ms11 = aos_get_tickcount(); - mot_nod_exp(16,2); + // bk_printf("IOT_read_task...... 12"); - - } - + status = receive_websocket_message_V3(AI_client_IOT,recv_buff,recv_buff_mxa_len,&code); - uart_write(ui_uart_handle,cmd_ui, 7); - } - #else + // bk_printf("get iot message................................."); - if(ui_uart_handle){ - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x08,0x01,0x0a}; - - if(strstr(exp_str,"伤心")||strstr(exp_str,"sad")) - { - cmd_ui[4]=0x01; - cmd_ui[6]=0x03; - }else - if(strstr(exp_str,"生气")||strstr(exp_str,"angry")) - { - cmd_ui[4]=0x03; - cmd_ui[6]=0x05; - }else - if(strstr(exp_str,"困惑")||strstr(exp_str,"confused")||strstr(exp_str,"embarrassed")) - {//embarrassed - cmd_ui[4]=0x06; - cmd_ui[6]=0x08; - }else - if(strstr(exp_str,"失望")||strstr(exp_str,"crying")) - { - cmd_ui[4]=0x08; - cmd_ui[6]=0x0a; - }else - if(strstr(exp_str,"开心")||strstr(exp_str,"happy")||strstr(exp_str,"loving") - ||strstr(exp_str,"funny")||strstr(exp_str,"laughing")||strstr(exp_str,"cool")) //laughing "funny loving - { //cool - cmd_ui[4]=0x0d; - cmd_ui[6]=0x0f; - }else - if(strstr(exp_str,"默认")||strstr(exp_str,"neutral")||strstr(exp_str,"thinking") - ||strstr(exp_str,"winking")||strstr(exp_str,"relaxed")) - {//thinking winking relaxed - cmd_ui[4]=0x0e; - cmd_ui[6]=0x10; - }else - if(strstr(exp_str,"惊恐")||strstr(exp_str,"surprised")||strstr(exp_str,"shocked")) - {//shocked - cmd_ui[4]=0x13; - cmd_ui[6]=0x15; - } + // bk_printf("bytes_received %02X %02X %02X %02X %02X %02X %02X %02X %02X ...\n",recv_buff[0],recv_buff[1],recv_buff[2],recv_buff[3],recv_buff[4],recv_buff[5],recv_buff[6],recv_buff[7],recv_buff[8]); - uart_write(ui_uart_handle,cmd_ui, 7); - } - #endif + // bk_printf("IOT_read_task...... 13"); -} + if(code == WS_TEXT)//字符串 + { + //bk_printf("AI_VC_read_task 3"); + // bk_printf("read TEXT:status:%d,data:%s",status,recv_buff); + + }else{ + + + if(status>8){ + + // bk_printf("read TEXT:status:%d",status); + + char jsonstr[500]={0}; + + memcpy(jsonstr,recv_buff+8,status-8); + + // bk_printf("read json TEXT:status:%s",jsonstr); + + // deal_iot_receive(jsonstr); + + AI_AMP_MSG_put(jsonstr,strlen(jsonstr),IOT_MESSAGE_MOT); + + } + + } + + if(status>0) + { + + + }else{ + + break; + } + + aos_sleep(100); + }else{ + + break; + } + + } + + if(recv_buff) + { + aic_free(recv_buff); + recv_buff = NULL; + } + // bk_printf("AI_VC_read_task exit\n"); + // AI_VC_read_task_flag=3; + } static void AI_VC_read_task(void *thread_input)//websocket 读线程 { int status; static u8 *recv_buff = NULL; - AI_VC_stata=1; if(recv_buff == NULL) { @@ -2454,56 +2634,49 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 // aos_sleep(50); - u32 timer_ms11 = aos_get_tickcount(); + // u32 timer_ms11 = aos_get_tickcount(); - //bk_printf("AI_VC_read_task 12"); + // 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"); + // bk_printf("AI_VC_read_task 13"); - u32 timer_ms22 = aos_get_tickcount(); + // u32 timer_ms22 = aos_get_tickcount(); - int duration_temp=timer_ms22-timer_ms11; + // int duration_temp=timer_ms22-timer_ms11; - aos_sleep(15); + // aos_sleep(15); - if(duration_temp>500) - bk_printf("receive_websocket_message %d ms",timer_ms22-timer_ms11); + // if(duration_temp>500) + // bk_printf("receive_websocket_message %d ms",timer_ms22-timer_ms11); + if(status>0) { + close_timerout = 360; - - close_timerout = 305; + silence_timerout=0; if(is_dialog_status==0){ - - sleep_chumo_timerout=300; - - }else{ - - if(vc_cfg.VC_mode==1){ - - if(ddh_is_close_dialog==0) - close_timerout=5; - - // else{ - // } + + sleep_chumo_timerout=180; - } } if(code == WS_TEXT)//字符串 { //bk_printf("AI_VC_read_task 3"); - bk_printf("read TEXT:status:%d,data:%s",status,recv_buff); + + if(status<200) + bk_printf("read TEXT:status:%d,data:%s",status,recv_buff); + + if(strstr(recv_buff,"\"started\""))//开始 { - - tts_start_timerout=0; - if(issend_start_up==0){ + + if(issend_start_up==0){ issend_start_up=1; @@ -2512,9 +2685,43 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 } + if(need_play_is_music_status==1){ + + need_play_is_music_status=0; + + AI_VC_send_music_start(mp3_path_str); + + } + + // if(is_need_tip){ + + // is_need_tip=0; + + // if(vc_cfg.VC_mode == 0) + // { + // AI_AMP_MSG_put("当前为连续对话模式。",strlen("当前为连续对话模式。"),VC_TTS_PLAY); + + // }else if(vc_cfg.VC_mode == 1) + // { + // AI_AMP_MSG_put("当前为单对话模式。",strlen("当前为单对话模式。"),VC_TTS_PLAY); + + // }else + // { + // AI_AMP_MSG_put("当前为按键打断模式。",strlen("当前为按键打断模式。"),VC_TTS_PLAY); + // } + + // //AI_AMP_MSG_put("提醒对话模式。",strlen("提醒对话模式。"),VC_TTS_PLAY); + // } + + if(AI_TTS_str) { + //if(is_offlinecmd_status==1) + // { + // AI_AMP_MSG_put("",strlen(""),VC_TTS_PLAY_CMD); + // } + // else AI_client_VC_synthesis(AI_TTS_str); aic_free(AI_TTS_str); AI_TTS_str = NULL; @@ -2529,21 +2736,10 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 VC_state = VC_State_Listening; else if(vc_cfg.VC_mode == 2 )//连续对话模式,按键打断 VC_state = VC_State_Listening; - - // back_middle_statu_mot(); - - // AI_AMP_MSG_put(0,0,AI_MOT_BACK_MID); + } - }else if(strstr(recv_buff,"\"asr.started\""))//asr开始 { - - // if(AI_ASR_str_tmp) - // { - // memset(AI_ASR_str_tmp,0,AI_ASR_STR_MAX_LEN); - // } - - has_tmp_result=0; if(vc_cfg.VC_mode == 1 || vc_cfg.is_VAD ==1)//长按模式,或开机本地vad模式 VC_state = VC_State_SpeechIng; }else if(strstr(recv_buff,"\"asr.result\"")) @@ -2554,9 +2750,6 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 if(str_tmp) { //bk_printf("content!!"); - - has_tmp_result=1; - str_tmp += strlen("\"content\":\""); int str_tmp_len = str_count(str_tmp,'\"'); //存asr结果 @@ -2574,10 +2767,10 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 { VC_state = VC_State_ASR_End; - bk_printf("AI_ASR_str%s AI_ASR_str_tmp %s",AI_ASR_str,AI_ASR_str_tmp); + // play_file_stream_data("C:/vadend.pcm"); C:/romvadend.pcm if(AI_ASR_str && AI_ASR_str_tmp) { - int ret = check_text(AI_ASR_str_tmp,strlen(AI_ASR_str_tmp)) ; + int ret = check_text(AI_ASR_str_tmp,strlen(AI_ASR_str_tmp)); //识别到有命令词,退出 if(ret == 0){ @@ -2585,19 +2778,17 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 break; } - if(VAD_time_count <= VAD_TIMEOUT)//时间间隔短,认为是一次对话,拼接内容 + if(VAD_time_count <= VAD_TIMEOUT )//时间间隔短,认为是一次对话,拼接内容 { - if(strlen(AI_ASR_str_tmp)==0){ + if(strlen(AI_ASR_str_tmp)==0){ - // VC_state=VC_State_Listening; + VC_state=VC_State_Listening; - bk_printf("没有识别结果"); + bk_printf("没有识别结果"); - AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); - - } + } if(strlen(AI_ASR_str) +strlen(AI_ASR_str_tmp)> AI_ASR_STR_MAX_LEN-1)//超出缓存长度,清零 { @@ -2609,53 +2800,40 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 bk_printf("vad间隔时间短拼接asr结果:%s",AI_ASR_str); + + // IOT_V3_update_statu(1,"thinking",""); + // play_file_stream_data("C:/vadend.pcm"); }else//超时,不用拼接 { - strncpy(AI_ASR_str,AI_ASR_str_tmp,AI_ASR_STR_MAX_LEN); - - if(strlen(AI_ASR_str_tmp)==0){ + strncpy(AI_ASR_str,AI_ASR_str_tmp,AI_ASR_STR_MAX_LEN); - // VC_state=VC_State_Listening; + if(strlen(AI_ASR_str_tmp)==0){ - bk_printf("没有识别结果"); + VC_state=VC_State_Listening; - AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); + bk_printf("没有识别结果"); } - - bk_printf("vad间隔时间长,判断为两次对话:%s",AI_ASR_str); + bk_printf("vad间隔时间长,判断为两次对话:%s",AI_ASR_str); } - #if BK_SOCKET_IOT - IOT_update_statu(1,"thinking",""); - #endif - - - } + IOT_V3_update_statu(1,"thinking",""); - bk_printf("002AI_ASR_str%s AI_ASR_str_tmp %s",AI_ASR_str,AI_ASR_str_tmp); - bk_printf("play_file_stream_data:%d",VC_Play_flag); + if(VC_Play_flag==1&&strlen(AI_ASR_str_tmp)!=0&&is_offlinecmd_status==0) //&&is_offlinecmd_status==0 + play_file_stream_data("O:/romoutput.pcm");//C:/vadend.pcm//&&is_dialog_status==0 - if(has_tmp_result==0) - AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); - else - if(VC_Play_flag==1&&strlen(AI_ASR_str_tmp)!=0)//VC_Play_flag==1&& - play_file_stream_data("C:/vadend.pcm"); - //else{ - - // if(strlen(AI_ASR_str_tmp)==0){ + // if (is_offlinecmd_status==1) + // { + /* code */ - // // VC_state=VC_State_Listening; - - // bk_printf("没有识别结果"); - - // AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); - - // } - // play_audio_file("C:/vadend.mp3"); - // } + // VC_state = VC_State_Listening; + //} + + // else{ + // } + } } else if(strstr(recv_buff,"\"vad.active\""))//vad触发 { @@ -2667,6 +2845,21 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 } else if(strstr(recv_buff,"\"tts.started\""))//tts开始 { + + // if(is_offlinecmd_status==1){ + + // continue; + // } + + + + if(is_dialog_status==0) + is_offlinecmd_status=0; + + u32 timer_ms22 = aos_get_tickcount(); + + bk_printf("tts.started_vad:%d",timer_ms22); + if(AI_VC_play_queue != NULL) { char count_t= 200; @@ -2687,9 +2880,11 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 VC_state = VC_State_TTSStart; play_data_len = 0; - #if BK_SOCKET_IOT - IOT_update_statu(1,"speaking",""); - #endif + + issend_start_up=2; + + IOT_V3_update_statu(1,"speaking",""); + one_receive_bin=0; if(outbound_attributes_format == attributes_format_OPUS){ @@ -2698,22 +2893,23 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 opus_clean(); - - + opus_encode_len=0; opus_decode_len=0; - // if(save_temp_tts==NULL){ + // if(save_temp_tts==NULL){ - // save_temp_tts=aic_malloc(900000); - // } + // save_temp_tts=aic_malloc(900000); + // } } - AI_AMP_MSG_put(0,0,VC_PLAY_START);//打开声音 + + + }else if(strstr(recv_buff,"\"tts.completed\""))//tts结束 { @@ -2721,46 +2917,73 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 // AI_AMP_MSG_put(0,0,VC_PLAY_START);//打开声音 startsave_tts=0; - #if BK_SOCKET_IOT - IOT_update_statu(1,"idle",""); - #endif - // fp_tts_save=fopen("C:/cmd_local_301.pcm","wb"); - // bk_printf("C:/cmd_local_301.pcm %d",one_receive_bin); + if(is_offlinecmd_status==1&&need_start_cmd==1) + { + + need_start_cmd=0; - // fwrite(save_temp_tts,1,one_receive_bin,fp_tts_save); + bk_printf("VC_TTS_PLAY_CMD"); - // startsave_tts=one_receive_bin; + // play_file_stream_data("C:/vadend.pcm"); - VC_state = VC_State_End; - - // if(fp_tts_save) - // fclose(fp_tts_save); + // bk_printf("VC_TTS_PLAY_CMD"); + AI_AMP_MSG_put("",strlen(""),VC_TTS_PLAY_CMD); + } + + // IOT_V3_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; + // fp_tts_save=NULL; // aos_sleep(500); // bk_printf("C:/cmd_local_301.pcm %d",getFileSize("C:/cmd_local_301.pcm")); - + VC_state = VC_State_End; + } else if(strstr(recv_buff,"\"completed\""))//结束 { - VC_state = VC_State_End;// - if(vc_cfg.VC_mode==0&&strlen(AI_ASR_str_tmp)==0){ + // if(VC_state != VC_State_Listening) + // VC_state = VC_State_End; + + if(is_offlinecmd_status==1){ + + if(is_dialog_status==0) + is_offlinecmd_status=0; + + VC_state = VC_State_Listening; + + continue; - u32 count_tmp = 0; - aos_get_queue_used_count(AI_VC_play_queue,&count_tmp); - - bk_printf("连续对话模式没有语音....\n"); - if(count_tmp<=0) - VC_END_play_timerout = 1; - } + + if(VC_state!=VC_State_End) + VC_state = VC_State_Listening; + if(issend_start_up==0){ + issend_start_up=1; + + AI_VC_send_startup(); + + } //AI_AMP_MSG_put(0,0,VC_RECORD_START);//打开录音 }else if(strstr(recv_buff,"\"event\":\"failed\""))//接口报错了 { @@ -2778,7 +3001,7 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 { // str_tmp+=strlen("\"message\":\""); // int count = strlen(str_tmp); - AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); + AI_AMP_MSG_put("服务异常了,请重试",strlen("服务异常了,请重试"),VC_CONNECT_ERROR); } } @@ -2790,9 +3013,6 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 // str_tmp = strstr(recv_buff,"\"param_json\""); char *str_tmp = strstr(recv_buff,"\"https"); - char *str_tmp_motion = strstr(recv_buff,"\"emotion"); - - if(str_tmp) { @@ -2808,34 +3028,114 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 bk_printf("the mp3_path is %s",mp3_path); - AI_VC_send_music_start(mp3_path); + AI_VC_send_music_start("http://qipaiai-oss1.tos-cn-beijing.volces.com/qi_pai_ringtone/2025_11_07/774578468192129024default_ring.pcm"); } - if(str_tmp_motion){ + str_tmp = strstr(recv_buff,"\"alarm_timer"); - char emotion_str[200]; - memset(emotion_str,0,200); + if(str_tmp) + { - char *str_tmp1 = strstr(recv_buff,"\"text"); - //bk_printf("content!!"); - // str_tmp += strlen("\"params_json")+6; - int str_tmp_len = str_count(str_tmp1+15,'\"'); + char mp3_path[200]; + + char alarm_content[200]; + memset(mp3_path,0,200); + + memset(alarm_content,0,200); + // bk_printf("content!!"); + // str_tmp += strlen("\"params_json")+6; + int str_tmp_len = str_count(str_tmp+22,'\"'); //存asr结果 - if(str_tmp_len>3){ - memcpy(emotion_str,str_tmp1+15,str_tmp_len-3); + if(str_tmp_len>3) + memcpy(mp3_path,str_tmp+22,str_tmp_len-3); //AI_ASR_str_tmp[str_tmp_len] = 0; - bk_printf("the emotion text len is %s %d",emotion_str,str_tmp_len); + + bk_printf("the alarm_timer is %s",mp3_path); - send_express_str(emotion_str); + // vc_cfg.alarm_name[vc_cfg.alarm_count]={0}; + + str_tmp = strstr(recv_buff,"\"alarm_content"); + + str_tmp_len = str_count(str_tmp+24,'\"'); + //存asr结果 + + if(str_tmp_len>3) + memcpy(alarm_content,str_tmp+24,str_tmp_len-3); + + // AI_ASR_str_tmp[str_tmp_len] = 0; + + bk_printf("the alarm_content is %s",alarm_content); - } + if(vc_cfg.alarm_count<5){ + + memset(vc_cfg.alarm_name[vc_cfg.alarm_count],0,50); + + memcpy(vc_cfg.alarm_name[vc_cfg.alarm_count],mp3_path,strlen(mp3_path)); + + memset(vc_cfg.alarm_content[vc_cfg.alarm_count],0,200); + + memcpy(vc_cfg.alarm_content[vc_cfg.alarm_count],alarm_content,strlen(alarm_content)); + + vc_cfg.alarm_count++; + + save_VC_cfg(); + + } + + + + // memcpy(); + + + play_file_stream_data("C:/alarm.pcm"); + + // alarm.pcm + + // AI_VC_send_music_start("http://qipaiai-oss1.tos-cn-beijing.volces.com/qi_pai_ringtone/2025_11_07/774578468192129024default_ring.pcm"); + } + }else if(strstr(recv_buff,"\"event\":\"llm.completed\""))//接口报错了 + { + + // char *str_tmp = NULL; + + // bk_printf("the express_str exist1"); + + // str_tmp = strstr(recv_buff,"\"param_json\""); + // char *str_tmp = strstr(recv_buff,"("); + + // if(str_tmp) + // { + + // bk_printf("the express_str exist2 \n"); + // char express_str[200]; + // memset(express_str,0,200); + // //bk_printf("content!!"); + // // str_tmp += strlen("\"params_json")+6; + + // char *str_tmp1=strstr(recv_buff,")"); + // // int str_tmp_len = str_count(str_tmp+1,')'); + + // int str_tmp_len = str_tmp1-str_tmp; + // //存asr结果 + + // memcpy(express_str,str_tmp+1,str_tmp_len); + // // //AI_ASR_str_tmp[str_tmp_len] = 0; + + // bk_printf("the express_str3 is [%s]",express_str); + + // // AI_VC_send_music_start(mp3_path); + + // send_express_str(express_str); + + // } + } } @@ -2844,23 +3144,28 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 - // if(one_receive_bin==0) - // timer_ms21=aos_get_tickcount(); + if(is_offlinecmd_status==1) + continue; + + + if(one_receive_bin==0) + timer_ms21=aos_get_tickcount(); - // one_receive_bin+=status; + // one_receive_bin+=status; - // timer_ms22=aos_get_tickcount(); + timer_ms22=aos_get_tickcount(); // u8 play_length_time=one_receive_bin/32; + one_receive_bin++; - // if((one_receive_bin/32)<(timer_ms22-timer_ms21)) - // bk_printf("read BIN status:%d,code:%d!!!!!!",status,code); + // if((one_receive_bin/32)<(timer_ms22-timer_ms21)) + // bk_printf("read BIN status:%d,code:%d!!!!!!",one_receive_bin*60,timer_ms22-timer_ms21); - // VC_pingping_timerout = 20; + // VC_pingping_timerout = 20; - // if(startsave_tts==1){ + // if(startsave_tts==1){ // if(fp_tts_save){ @@ -2876,18 +3181,27 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 // if(one_receive_bin+status<900000) // memcpy(save_temp_tts+one_receive_bin,recv_buff,status); - one_receive_bin+=status; + // one_receive_bin+=status; + + // u32 timer_ms22 = aos_get_tickcount(); - // bk_printf("ogg_opus_decode_data %d...",one_receive_bin); + bk_printf("ogg_opus_decode_data vad %d ...",status); + + // bk_printf("sum_opus_decode_data %d...",one_receive_bin); // memcpy(save_temp_tts); // audio_data(recv_buff,status,26); - //ogg_opus_decode_data(recv_buff,status); + // ogg_opus_decode_data(recv_buff,status); + + if(codec_tts==1) + my_opus_decode_data(recv_buff,status); + else + ogg_opus_decode_data(recv_buff,status); - my_opus_decode_data(recv_buff,status); + play_data_len+=1920; // if(fp_tts_save){ // bk_printf("save tts!!!"); @@ -2914,7 +3228,7 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 if(AI_client_VC != NULL) { bk_printf("AI_VC_read_task:vc连接断开了"); - AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); + AI_AMP_MSG_put("vc连接断开了",strlen("vc连接断开了"),VC_CONNECT_ERROR); }else{ bk_printf("AI_VC_read_task:客户端主动断开连接了"); } @@ -2937,8 +3251,6 @@ static void AI_VC_read_task(void *thread_input)//websocket 读线程 } bk_printf("AI_VC_read_task exit\n"); AI_VC_read_task_flag=3; - - AI_VC_stata=0; } @@ -2952,6 +3264,7 @@ static void AI_VC_record_task(void *thread_input)//录音线程 static char vad_end_flag = 0; int ret = 0; bk_printf("AI_VC_record_task go......"); + if(app_audio_flag == 0) { app_audio_param_t param; @@ -2967,6 +3280,7 @@ static void AI_VC_record_task(void *thread_input)//录音线程 bk_printf("app_audio_start ret=%d",ret); app_logi("AI_VC_record_task app_audio_start ret=%d",ret); } + AI_VC_audio_queue_t AI_VC_play_record_data; AI_VC_record_task_flag = 1; while(1) @@ -2977,7 +3291,7 @@ 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); @@ -2989,12 +3303,13 @@ static void AI_VC_record_task(void *thread_input)//录音线程 aos_sleep(1); continue; } + - if(is_dialog_status==1){ + if(is_dialog_status==1){ if(vc_cfg.is_KWS) { - int KWS_time = aos_get_tickcount(); + // int KWS_time = aos_get_tickcount(); // bk_printf("run_kws1: KWS_time:%d,data_len:%d",KWS_time,ret); run_kws((int16_t *)vrecord_buf,ret/2); int kws_status = kws_detect(); @@ -3002,26 +3317,32 @@ static void AI_VC_record_task(void *thread_input)//录音线程 if (kws_status == 1) { is_dialog_status=0; + + // is_offlinecmd_status=0; + + sleep_chumo_timerout=90; + bk_printf("!!!!!!!!!!!!!!!!!!!!!!离线语音识别打断了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"); kws_clear(); - AI_AMP_MSG_put("我来了。",strlen("我来了。"),VC_TTS_PLAY); + AI_client_VC_synthesis("爱喵喵来了。"); + // AI_AMP_MSG_put("我来了。",strlen("我来了。"),VC_TTS_PLAY); } } - continue; + // continue; } - + //&& is_dialog_status==0 if((vc_cfg.VC_mode == 0 || vc_cfg.VC_mode == 2)&& vc_cfg.is_VAD == 1)//VAD逻辑:先判断有没有有效数据,触发了有效数据才发送 { - if(vc_cfg.VC_mode == 2 && VC_state > VC_State_SpeechEnd )//按钮打断模式,tts播放时禁止打断,不输入语音 - { + if(vc_cfg.VC_mode == 2 && VC_state > VC_State_SpeechEnd )//按钮打断模式,tts播放时禁止打断,不输入语音 + { - if(vc_cfg.is_KWS) + if(vc_cfg.is_KWS&&is_dialog_status==0) { - int KWS_time = aos_get_tickcount(); + // int KWS_time = aos_get_tickcount(); // bk_printf("run_kws1: KWS_time:%d,data_len:%d",KWS_time,ret); run_kws((int16_t *)vrecord_buf,ret/2); int kws_status = kws_detect(); @@ -3032,11 +3353,20 @@ static void AI_VC_record_task(void *thread_input)//录音线程 bk_printf("!!!!!!!!!!!!!!!!!!!!!!离线语音识别打断了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"); kws_clear(); - AI_AMP_MSG_put("我在呢。",strlen("我在呢。"),VC_TTS_PLAY); + AI_AMP_MSG_put("爱喵喵来了。",strlen("爱喵喵来了。"),VC_TTS_PLAY); } } continue; - } + } + + + if(is_music_status==1) + continue; + + if(issend_start_up<2) { + + continue; + } // 每次获得语音后调用, 语音长度最好小于100ms // wave为u8*或void*指针时需要 / 2 //int vad_test_time = aos_get_tickcount(); @@ -3048,6 +3378,8 @@ static void AI_VC_record_task(void *thread_input)//录音线程 // audio_data(vrecord_buf,ret,23); if (vad_status_t == 2) {//说话停止 + + bk_printf("说话停止......"); vad_clear(); if(VAD_status == 1)//清空buff { @@ -3062,9 +3394,7 @@ static void AI_VC_record_task(void *thread_input)//录音线程 } ret = sizeof(vrecord_buf); } - - - AI_VC_put_record_data(vrecord_buf,ret); + AI_VC_put_record_data(vrecord_buf,ret); } VAD_status = 2; // audio_data(vrecord_buf,ret,21); @@ -3088,11 +3418,9 @@ static void AI_VC_record_task(void *thread_input)//录音线程 { unsigned int r_len = bk_rb_read(VAD_rb_data,vad_r_tmp,2560); - audio_data(vad_r_tmp,r_len,21); + // audio_data(vad_r_tmp,r_len,21); //AI_VC_send_data(vad_r_tmp,r_len,AIC_WEBSOCKET_OPCODE_BINARY_FRAME);//发送缓冲数据 - - AI_VC_put_record_data(vad_r_tmp,r_len);//入队缓冲数据 - + AI_VC_put_record_data(vad_r_tmp,r_len);//入队缓冲数据 // if(vc_cfg.is_KWS) // { // int KWS_time = aos_get_tickcount(); @@ -3101,8 +3429,6 @@ static void AI_VC_record_task(void *thread_input)//录音线程 // int kws_status = kws_detect(); // bk_printf("run_kws2:end_time:%d ,kws_status:%d",aos_get_tickcount() - KWS_time,kws_status); // if (kws_status == 1) { - - // bk_printf("!!!!!!!!!!!!!!!!!!!!!!离线语音识别打断了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"); // kws_clear(); // } @@ -3119,11 +3445,9 @@ static void AI_VC_record_task(void *thread_input)//录音线程 VAD_status = 1; } - - AI_VC_put_record_data(vrecord_buf,ret); - - audio_data(vrecord_buf,ret,21); + AI_VC_put_record_data(vrecord_buf,ret); + // audio_data(vrecord_buf,ret,21); // if(vc_cfg.is_KWS) // { // int KWS_time = aos_get_tickcount(); @@ -3132,14 +3456,11 @@ static void AI_VC_record_task(void *thread_input)//录音线程 // int kws_status = kws_detect(); // bk_printf("run_kws2:end_time:%d ,kws_status:%d",aos_get_tickcount() - KWS_time,kws_status); // if (kws_status == 1) { - - // bk_printf("!!!!!!!!!!!!!!!!!!!!!!离线语音识别打断了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"); // kws_clear(); // } // } //bk_printf("VAD:检测到说话了"); - }else if(vad_status_t == 0 )//m没有说话 { @@ -3170,36 +3491,12 @@ static void AI_VC_record_task(void *thread_input)//录音线程 if(count_tmp>0)//有有效录音数据 { - if(VC_state >= VC_State_TTSStart) + if(VC_state >= VC_State_TTSStart)// { - char count_tmp12 = count_tmp; - - bk_printf("count_tmp:%d,VC_state:%d",count_tmp,VC_state); - - while(count_tmp12 --) - { - - if(count_tmp12<15) - break; - - if(aos_recv_from_queue(AI_VC_record_tmp_queue,&AI_VC_play_record_data,AOS_NO_WAIT) == AOS_SUCCESS)//有有效录音数据 - { - // audio_data(AI_VC_play_record_data.data,AI_VC_play_record_data.len,22); - - // AI_VC_send_data(AI_VC_play_record_data.data,AI_VC_play_record_data.len,AIC_WEBSOCKET_OPCODE_BINARY_FRAME); - if(AI_VC_play_record_data.data) - aic_free(AI_VC_play_record_data.data); - }else - break; - } - VC_state = VC_State_None; - bk_printf("VAD:vad_status_t:%d",vad_status_t); + bk_printf("VAD:vad_status_t:%d",vad_status_t); bk_printf("VAD:发送打断对话"); AI_AMP_MSG_put(0,0,VC_Cancel);//发送打断 - - AI_AMP_MSG_put(0,0,AI_MOT_BACK_MID); - } else if(VC_state == VC_State_SpeechIng )//可以发数据了 { @@ -3217,29 +3514,20 @@ static void AI_VC_record_task(void *thread_input)//录音线程 break; } } - else if(VC_state == VC_State_Listening) //|| (VC_state >= VC_State_ASR_End && VC_state < VC_State_TTSStart) + else if(VC_state == VC_State_Listening || (VC_state >= VC_State_ASR_End && VC_state < VC_State_TTSStart)) { AI_VC_send_audio_create();//发送开始音频 - bk_printf("VAD:发送开始音频"); - }else { if(VC_state > VC_State_SpeechStart) - { bk_printf("VAD:有数据还没发:VC_state:%d\r\n\r\n",VC_state); - - // AI_AMP_MSG_put("没听清,请再说一遍",strlen("没听清,请再说一遍"),VC_CONNECT_ERROR); - } - // AI_VC_send_audio_create();//发送开始音频 - // bk_printf("VAD:发送开始音频"); - // char count_tmp = 200; - + // AI_VC_send_audio_create();//发送开始音频 + // bk_printf("VAD:发送开始音频"); } vad_end_flag = 1; - } - else//没录音数据 + }else//没录音数据 { if(VAD_status) { @@ -3247,12 +3535,21 @@ static void AI_VC_record_task(void *thread_input)//录音线程 } if(VC_state == VC_State_SpeechIng || vad_end_flag ==1)//开始说话,且没数据 { + vad_end_flag = 0; + // if(VC_Play_flag==1) + // play_file_stream_data("C:/vadend.pcm"); + + u32 timer_ms22 = aos_get_tickcount(); + + bk_printf("VAD:AI_VC_send_audio_completed:%d",timer_ms22); AI_VC_send_audio_completed();//发送停止音频、、目前还是会有数据下发 bk_printf("VAD:发送停止音频"); bk_printf("VAD:vad_status_t:%d",vad_status_t); vad_end_flag = 0; if(inbound_attributes_format == attributes_format_OPUS) opus_clean(); + + } } @@ -3262,7 +3559,7 @@ static void AI_VC_record_task(void *thread_input)//录音线程 } - // AI_VC_put_play_data(vrecord_buf,ret);//音频调试0 + //AI_VC_put_play_data(vrecord_buf,ret);//音频调试0 if(AI_VC_record_tmp_queue == NULL) continue; @@ -3334,22 +3631,28 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 bk_printf("app_audio_start ret=%d",ret); app_logi("AI_VC_play_task app_audio_start ret=%d",ret); } - - aos_sleep(200); - while(1) { if(AI_VC_play_task_flag >=2) break; - //app_logi("app_audio_write0!!\r\n"); + // app_logi("app_audio_write0!!\r\n"); + + // play_data_len>8*1024 || - // play_data_len>8*1024 || + //(play_data_len>8*1024 || outbound_attributes_format == attributes_format_OPUS) - if((play_data_len>8*1024 || outbound_attributes_format == attributes_format_OPUS)&&VC_Play_flag ==1 && AI_VC_play_queue ) + //|| outbound_attributes_format == attributes_format_OPUS + + if((play_data_len>8*1024 )&&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); + + // bk_printf("播放数据123 %d!!!!!!!!!!!",queue_ret); + if( queue_ret== AOS_SUCCESS) { + // bk_printf("播放数据 %d!!!!!!!!!!!",AI_VC_play_queue_data.len); + if(AI_VC_play_queue_data.len> 32*1024)//数据太大,可能是出错了 { bk_printf("AI_VC_play_queue 数据太大!!!!!!!!!!!"); @@ -3357,6 +3660,13 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 } if( AI_VC_play_queue_data.data) { + + // audio_data(AI_VC_play_queue_data.data,AI_VC_play_queue_data.len,36); + + // u32 timer_ms22 = aos_get_tickcount(); + + // bk_printf("start_play:%d",timer_ms22); + ret = app_audio_write(APP_AUDIO_TYPE_STREAM_DUPLEX, AI_VC_play_queue_data.data, AI_VC_play_queue_data.len, 2000); // bk_printf("app_audio_write len=%d\n", ret); @@ -3366,7 +3676,6 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 bk_printf("app_audio_write error, ret=%d\n", ret); } - if(AI_VC_play_queue_data.data) aic_free(AI_VC_play_queue_data.data); if(VC_state == VC_State_End) @@ -3379,8 +3688,20 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 if(count_tmp <= 0 )//播放完了 { - bk_printf("VC_State_End:count_tmp = %d",count_tmp); - VC_END_play_timerout = 1; + + bk_printf("VC_State_End:count_tmp = %d",count_tmp); + + send_express_aimiaomiao("ttsfinish"); + + need_express_cmd=0; + is_music_status=0; + + if(is_dialog_status==1) + is_offlinecmd_status=1; + + IOT_V3_update_statu(1,"idle",""); + VC_END_play_timerout = 1; + } } continue;//正常有数据就不执行下面写0 @@ -3395,9 +3716,12 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 } //没数据的时候也要写0x00 { - //bk_printf("AI_VC_play_task no data "); + // bk_printf("AI_VC_play_task no data "); memset(play_tmp_d,0,sizeof(play_tmp_d)); //app_logi("app_audio_write0:write 0"); + + // audio_data(play_tmp_d,sizeof(play_tmp_d),36); + ret = app_audio_write(APP_AUDIO_TYPE_STREAM_DUPLEX, play_tmp_d, sizeof(play_tmp_d), AOS_WAIT_FOREVER); if (ret <= 0) @@ -3497,404 +3821,383 @@ uint16_t modbus_crc16(const uint8_t *data, uint16_t length) { } -void deal_iot_receive(char *json_data) +//Music.Start 开始发送音频 +void IOT_update_statu(int type,char *type_str,char *content) { + // VC_state = VC_State_SpeechStart;//开始说 - bk_printf(json_data); + return ; + // 创建根对象 + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf("Failed to create root object.\n"); + return ; + } - // vc_cfg.VC_TTS_volume = 100; - // AI_AMP_MSG_put("音量已最大。",strlen("音量已最大。"),VC_TTS_PLAY); + cJSON *cmd_data = cJSON_CreateObject(); - //{"cmd":"emotion","msg":"AI指令:emotion","data":"Cute","ts":1764914695122,"targetDeviceId":"865530078570777"} + if (cmd_data == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; + } + // 设置 "event" + - if(strstr(json_data,"action")){ + // { + // "cmd":"post_heart_beat", + // "ts":1738283829, + // "msg":"心跳", + // "data":{ + // "battery": 20 + // } + // } - tts_start_timerout=0; - bk_printf("action"); + if(type==4){ - char *str_tmp = NULL; + cJSON_AddStringToObject(root, "cmd", "post_heart_beat"); - char action_str[500]={0}; + cJSON_AddNumberToObject(root, "ts", 1738283829); - str_tmp = strstr(json_data,"\"data\":\""); - - // bk_printf("str_tmp:%p",str_tmp); + cJSON_AddStringToObject(root, "msg", "心跳"); - if(str_tmp)//解析password - { + cJSON_AddNumberToObject(cmd_data, "battery", 20); - str_tmp += strlen("\"data\":\""); - int count = str_count(str_tmp,'\"'); - bk_printf("count:%d",count); - if(count < sizeof(action_str)) - { + // cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); - - memcpy(action_str,str_tmp,count); - action_str[count] = 0; - bk_printf("action_str:%s",action_str); - if(is_guanji==0) - send_express_str(action_str); - } + } - } + + 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(strstr(json_data,"set_vol")){ + if(type==2){ + long timestamp; - char *str_tmp = NULL; + timestamp = time(NULL); + bk_printf("2时间戳: %ld\n", (long)timestamp); + long long timestamp_ms = (long long)timestamp * 1000; + char timestamp_str[20] ={0}; - char action_str[500]={0}; + // 使用 sprintf 将 long long 类型的时间戳转换为字符串 + int ret12 = sprintf(timestamp_str, "%lld", timestamp_ms); + timestamp_str[ret12] =0; + // 打印转换后的字符串形式的时间戳 + bk_printf("2时间戳(ms 转换为字符串): %s\n", timestamp_str); - str_tmp = strstr(json_data,"\"data\":\""); + cJSON_AddStringToObject(root, "cmd", "chat_text"); - if(str_tmp)//解析password - { + cJSON_AddStringToObject(cmd_data, "device_id", vc_cfg.dev_imei); - str_tmp += strlen("\"data\":\""); - int count = str_count(str_tmp,'\"'); - bk_printf("count:%d",count); - if(count < sizeof(action_str)) - { + cJSON_AddStringToObject(cmd_data, "type", type_str); - - memcpy(action_str,str_tmp,count); - action_str[count] = 0; - bk_printf("set_vol:%s",action_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 - if(strstr(action_str,"+10")){ + struct psy_info psy_info; + get_psy_info(&psy_info); - if(vc_cfg.VC_TTS_volume<=90){ - vc_cfg.VC_TTS_volume+=10; + // psy_info.bat_capacity - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - } + sprintf(tmp_bat,"%d",psy_info.bat_capacity); - }else if(strstr(action_str,"-10")){ + sprintf(tmp_sig,"%d",p_signal_info.signal_level_val); - if(vc_cfg.VC_TTS_volume>10){ - vc_cfg.VC_TTS_volume-=10; - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - } + sprintf(tmp_vol,"%d",vc_cfg.VC_TTS_volume); - }else{ + sprintf(tmp_bargein,"%d",vc_cfg.VC_mode); - int temp_vol=atoi(action_str); + cJSON_AddStringToObject(cmd_data, "battery", tmp_bat); - if(temp_vol>0&&temp_vol<=100){ + cJSON_AddStringToObject(cmd_data, "wifi_rssi", tmp_sig); - bk_printf("temp_vol:%d",temp_vol); - vc_cfg.VC_TTS_volume=temp_vol; + cJSON_AddStringToObject(cmd_data, "volume", tmp_vol); - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + cJSON_AddStringToObject(cmd_data, "current_role_name", "fx01"); + cJSON_AddStringToObject(cmd_data, "version", "V1.2"); - } + cJSON_AddStringToObject(cmd_data, "bargein", tmp_bargein); - } - // if(is_guanji==0) - // send_express_str(action_str); - } + // bargein - } + - } + } + + + // 将 "payload" 对象添加到根对象中 - if(strstr(json_data,"emotion")){ + cJSON_AddItemToObject(root, "data", cmd_data); - bk_printf("emotion"); - tts_start_timerout=0; - char *str_tmp = NULL; + // 将 JSON 对象转换为字符串 - char emotion_str[500]={0}; + //cJSON_PrintUnformatted - str_tmp = strstr(json_data,"\"data\":\""); + 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("str_tmp:%p",str_tmp); + } - if(str_tmp)//解析password - { - str_tmp += strlen("\"data\":\""); - int count = str_count(str_tmp,'\"'); - bk_printf("count:%d",count); - if(count < sizeof(emotion_str)) - { + bk_printf(json_str); - - memcpy(emotion_str,str_tmp,count); - emotion_str[count] = 0; - bk_printf("emotion_str:%s",emotion_str); - if(is_guanji==0) - send_express_str(emotion_str); - } + if(type==4){ - } + bk_printf("SEND iot_v3 message"); - } + int ret=send_websocket_message(AI_client_IOT,json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME); - if(strstr(json_data,"tts_audio_url")){ + bk_printf("SEND iot_v3 success message: %d",ret); - bk_printf("tts_audio_url"); + }else{ - char *str_tmp = NULL; + unsigned char frame[500]={0}; - char mp3_path_str[500]={0}; + frame[0]=0xFF; + frame[1]=0xFE; + frame[2]=0x03; - str_tmp = strstr(json_data,"\"data\":\""); - - // bk_printf("str_tmp:%p",str_tmp); + // AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - if(str_tmp)//解析password - { + short temp=strlen(json_str); + memcpy(frame+3,&temp,2); - str_tmp += strlen("\"data\":\""); - int count = str_count(str_tmp,'\"'); - bk_printf("count:%d",count); - if(count < sizeof(mp3_path_str)) - { + memcpy(frame+5,json_str,strlen(json_str)); - - memcpy(mp3_path_str,str_tmp,count); - mp3_path_str[count] = 0; - bk_printf("mp3_path_str:%s",mp3_path_str); + uint16_t crc = modbus_crc16(json_str,strlen(json_str)); - //is_music_status=1; + uint8_t crc_low = crc & 0xFF; - // AI_AMP_MSG_put("请稍后,现在为你查找音乐",strlen("请稍后,现在为你查找音乐"),VC_TTS_PLAY); + uint8_t crc_high = (crc >> 8) & 0xFF; - // aos_sleep(4000); + frame[5+strlen(json_str)]=crc_low; - AI_VC_send_music_start(mp3_path_str); + frame[6+strlen(json_str)]=crc_high; - } - } + bk_printf("CRC16 校验值(小端字节序): 0x%02X%02X\n", crc_low, crc_high); - // IOT_update_statu(3,"",""); + bk_printf("the send str is %d ",7+strlen(json_str)); - // AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + send_iot_message(AI_client_IOT,frame,7+strlen(json_str)); } - if(strstr(json_data,"get_device_state")){ + // 释放内存 + free(json_str); + cJSON_Delete(root); +} - bk_printf("get_device_state"); - // IOT_update_statu(3,"",""); +void IOT_V3_update_statu(int type,char *type_str,char *content) +{ + + // 创建根对象 + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + bk_printf("Failed to create root object.\n"); + return ; + } - // AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + cJSON *cmd_data = cJSON_CreateObject(); + if (cmd_data == NULL) { + bk_printf("Failed to create payload object.\n"); + cJSON_Delete(root); + return ; } - - if(strstr(json_data,"find_device")){ + // 设置 "event" + long timestamp; - bk_printf("find_device"); + timestamp = time(NULL); + bk_printf("2时间戳: %ld\n", (long)timestamp); + long long timestamp_ms = (long long)timestamp * 1000; + char timestamp_str[20] ={0}; - AI_AMP_MSG_put("正在查找设备。",strlen("正在查找设备。"),VC_TTS_PLAY); + // 使用 sprintf 将 long long 类型的时间戳转换为字符串 + int ret12 = sprintf(timestamp_str, "%lld", timestamp_ms); + timestamp_str[ret12] =0; - } + if(type==1){ - if(strstr(json_data,"renew_device")){ + cJSON_AddStringToObject(root, "cmd", "chat_state"); - bk_printf("renew_device"); + cJSON_AddStringToObject(root, "msg", "当前服务状态上传"); - IOT_update_bargein(); + cJSON_AddNumberToObject(root, "ts", timestamp_ms); - if(vc_cfg.VC_mode == 0) - { - AI_AMP_MSG_put("已切换到连续对话模式。",strlen("已切换到连续对话模式。"),VC_TTS_PLAY); + cJSON_AddStringToObject(cmd_data, "state", type_str); - //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 - { + cJSON_AddStringToObject(cmd_data, "deviceId", vc_cfg.dev_imei); - // app_audio_set_master_volume(15*80/100); - AI_AMP_MSG_put("已切换到按键打断模式。我说话的时候需要短按按钮打断。",strlen("已切换到按键打断模式。我说话的时候需要短按按钮打断。"),VC_TTS_PLAY); - } + // cJSON_AddStringToObject(cmd_data, "ts", timestamp_str); - // IOT_V3_update_statu(3,"",""); - save_VC_cfg(); + // cJSON_AddNumberToObject(cmd_data, "ts", timestamp_ms); - } + } - if(strstr(json_data,"switch_volume")) - { + if(type==2){ - // bk_printf("switch_volume"); + + // 打印转换后的字符串形式的时间戳 + bk_printf("2时间戳(ms 转换为字符串): %s\n", timestamp_str); - char *str_tmp = NULL; + cJSON_AddStringToObject(root, "cmd", "chat_text"); - char volume_str[5]={0}; + cJSON_AddStringToObject(cmd_data, "deviceId", vc_cfg.dev_imei); - 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)) - { + cJSON_AddStringToObject(cmd_data, "type", type_str); - - memcpy(volume_str,str_tmp,count); - volume_str[count] = 0; - bk_printf("volume_str:%s",volume_str); + cJSON_AddStringToObject(cmd_data, "text", content); - int temp_vol=atoi(volume_str); - - if(temp_vol>=0&&temp_vol<=100) - { + cJSON_AddStringToObject(cmd_data, "createTime", timestamp_str); + + } + - vc_cfg.VC_TTS_volume = temp_vol; - - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + if(type==3){ - char tts_temp[200]={0}; + char tmp_bat[20]={0},tmp_sig[20]={0},tmp_vol[20]={0},tmp_bargein[20]={0},tmp_charge[20]={0}; + cJSON_AddStringToObject(root, "cmd", "device_info"); - sprintf(tts_temp,"设置音量%d成功。",vc_cfg.VC_TTS_volume); + cJSON_AddNumberToObject(root, "ts", timestamp_ms); - AI_AMP_MSG_put(tts_temp,strlen(tts_temp),VC_TTS_PLAY); + cJSON_AddStringToObject(root, "msg", "上报设备信息"); + cJSON_AddStringToObject(cmd_data, "deviceId", vc_cfg.dev_imei); - //is_save_VC_cfg = 1; + ts_radio_signal_info_t p_signal_info; + int32_t ret = 0; - save_VC_cfg(); - } + ret = aic_srv_tele_radio_get_signal_info_sync(current_card_id, &p_signal_info); - - - } - else - { - bk_printf("获取volume失败"); - } - } - } + // 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); -static void AI_IOT_read_task(void *thread_input)//websocket 读线程 - { + sprintf(tmp_sig,"%d",p_signal_info.signal_level_val); - int status; - static u8 *recv_buff = NULL; - - - if(recv_buff == NULL) - { - recv_buff = aic_malloc(recv_buff_mxa_len); - } - if(recv_buff == NULL) - { - bk_printf("AI_VC_read_task aic_malloc ERROR!!!!!!!!!!!!!!!!!!......"); - return; - } - WebSocketMessageType code; - bk_printf("AI_IOT_read_task go......"); - // AI_VC_read_task_flag = 1; - while (1) { - - if(AI_client_IOT) - { + sprintf(tmp_vol,"%d",vc_cfg.VC_TTS_volume); - 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"); + sprintf(tmp_bargein,"%d",vc_cfg.VC_mode); - // aos_sleep(50); + sprintf(tmp_charge,"%d",psy_info.bat_chg_status); - // u32 timer_ms11 = aos_get_tickcount(); + cJSON_AddStringToObject(cmd_data, "battery", tmp_bat); - // bk_printf("IOT_read_task...... 12"); + cJSON_AddStringToObject(cmd_data, "wifi_rssi", tmp_sig); - status = receive_websocket_message_V3(AI_client_IOT,recv_buff,recv_buff_mxa_len,&code); + cJSON_AddStringToObject(cmd_data, "volume", tmp_vol); - if(AI_VC_stata==0) - { - aos_sleep(100); - continue; + // cJSON_AddStringToObject(cmd_data, "bargein", "1"); - } - - + bk_printf("the vc_mode is %d",vc_cfg.VC_mode); - // bk_printf("get iot message................................."); - // bk_printf("bytes_received %02X %02X %02X %02X %02X %02X %02X %02X %02X ...\n",recv_buff[0],recv_buff[1],recv_buff[2],recv_buff[3],recv_buff[4],recv_buff[5],recv_buff[6],recv_buff[7],recv_buff[8]); + // if(vc_cfg.VC_mode==0) + // cJSON_AddStringToObject(cmd_data, "bargein", "1"); + // else + cJSON_AddStringToObject(cmd_data, "bargein", tmp_bargein); - // bk_printf("IOT_read_task...... 13"); + cJSON_AddStringToObject(cmd_data, "currentRoleName", "fx01"); - if(code == WS_TEXT)//字符串 - { - //bk_printf("AI_VC_read_task 3"); - // bk_printf("read TEXT:status:%d,data:%s",status,recv_buff); - - }else{ + cJSON_AddStringToObject(cmd_data, "version", "V1.2"); + cJSON_AddStringToObject(cmd_data, "charge", tmp_charge); - if(status>8){ + + // bargein - // bk_printf("read TEXT:status:%d",status); + } + + + // 将 "payload" 对象添加到根对象中 - char jsonstr[500]={0}; + cJSON_AddItemToObject(root, "data", cmd_data); - memcpy(jsonstr,recv_buff+8,status-8); - // bk_printf("read json TEXT:status:%s",jsonstr); + // 将 JSON 对象转换为字符串 - deal_iot_receive(jsonstr); + // 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); + + } - } - - if(status>0) - { + // bk_printf(json_str); - }else{ - break; - } + unsigned char frame[500]={0}; + frame[0]=0xFF; + frame[1]=0xFE; + frame[2]=0x01; + frame[3]=0x03; - aos_sleep(100); - }else{ + // AI_VC_send_data(json_str,strlen(json_str),AIC_WEBSOCKET_OPCODE_TEXT_FRAME);//发送数据 - break; - } + short temp=strlen(json_str); + // memcpy(frame+3,&temp,2); - } + frame[7]=temp; - if(recv_buff) - { - aic_free(recv_buff); - recv_buff = NULL; - } + memcpy(frame+8,json_str,strlen(json_str)); - AI_client_IOT=NULL; - - AI_VC_read_IOT_id=0; - bk_printf("AI_IOT_read_task exit\n"); - // AI_VC_read_task_flag=3; + // bk_printf("start SEND123 iot_v3 success message: %d",strlen(json_str)); + + int ret=send_websocket_message(AI_client_IOT,frame,strlen(json_str)+8,AIC_WEBSOCKET_OPCODE_BINARY_FRAME); + + // int ret=send_iot_message(AI_client_IOT,json_str,strlen(json_str)); + + // bk_printf("SEND iot_v3 success message: %d",ret); - } + + // 释放内存 + free(json_str); + cJSON_Delete(root); +} void IOT_V3_heart() { @@ -3957,7 +4260,7 @@ void IOT_V3_heart() } - bk_printf(json_str); + //bk_printf(json_str); // if(type==4){ @@ -3983,7 +4286,7 @@ void IOT_V3_heart() // int ret=send_iot_message(AI_client_IOT,json_str,strlen(json_str)); - bk_printf("SEND iot_v3 success message: %d",ret); + // bk_printf("SEND iot_v3 success message: %d",ret); // }else{ @@ -3996,8 +4299,76 @@ void IOT_V3_heart() cJSON_Delete(root); } -#define IOT +void IOT_heart(){ + + + return ; + + 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) +{ + + return ; + 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) { @@ -4028,8 +4399,12 @@ static int websocket_connect_VC(void) goto CONNECT_RET; } - //provider=coze& - //provider=coze& + //provider=coze& + + //provider=coze& + + //provider=coze& + if(vc_cfg.dev_type == DEV_JSWM) url_len =snprintf(url,511,"ws://%s/vc/agent?is_auth=1&dt=%s",vc_cfg.server_ip,get_token()); @@ -4046,8 +4421,13 @@ static int websocket_connect_VC(void) free_all_websocket_res(); } + // ws://118.145.159.222:30700/ws-hub/dev/v1?bz-dt=%s + + // char *url_socket="s是的"; + // char *ws_iot_url="iot.socket.thisiot.com:60501"; + + - AI_client_VC = init_websocket_client(url,NULL); #ifdef IOT @@ -4062,11 +4442,11 @@ static int websocket_connect_VC(void) // wss://wsiot.raisound.com/iot-api/ws-hub/dev/v1?bz-dt=xxx // ws(s)://wsiot-nocdn.raisound.com/ws-hub/dev/v1?bz-dt= -#if 1 +#if 1 sprintf(url_iot,"ws://wsiot-nocdn.raisound.com/ws-hub/dev/v1?bz-dt=%s",get_token()); #else - sprintf(url_iot,"ws://dmp-dev.raisound.com/iot-api/ws-hub/dev/v1?bz-dt=%s",get_token()); + sprintf(url_iot,"ws://dmp-dev.raisound.com/iot-api/ws-hub/dev/v1?bz-dt=%s",get_token()); #endif AI_client_IOT = init_websocket_IOT_V3(url_iot,NULL);// ws_iot @@ -4081,7 +4461,7 @@ static int websocket_connect_VC(void) { AI_VC_read_IOT_id = aos_create_thread( "AI_VC_read", - AI_IOT_read_task,// AI_VC_IOT_task //AI_IOT_read_task + AI_IOT_read_task,// AI_VC_IOT_task,//AI_IOT_read_task &AI_VC_read_IOT_id, 4096u, 0, @@ -4095,14 +4475,30 @@ static int websocket_connect_VC(void) // IOT_login(1); - + // aos_sleep(200); + + // // char *iotmessage="{\"cmd\":\"get_bot\",\"data\":\"\"}"; + + // IOT_V3_update_statu(1,"idle",""); + + // aos_sleep(200); + // IOT_update_statu(2,"res","12312312"); + + // aos_sleep(200); IOT_V3_heart(); + // aos_sleep(200); + // IOT_V3_update_statu(1,"idle",""); + // aos_sleep(200); + + // IOT_heart(); // send_iot_message(AI_client_IOT,iotmessage,strlen(iotmessage)); } + AI_AMP_MSG_put(0,0,AI_ALARM_UPDATE); + } #endif @@ -4176,18 +4572,12 @@ static void websocket_deconnect_VC(void) // aic_websocket_delete(AI_VC_client_websocket_handle,5000); // AI_VC_client_websocket_handle = 0; // } - - // AI_client_IOT if(AI_client_VC)//先断开再关闭读线程 { delete_websocket_res(AI_client_VC);//删除该对象ws资源管控 close_websocket_client(AI_client_VC); AI_client_VC = NULL; - - // AI_client_IOT } - - //销毁发送线程 if(AI_VC_send_task_id != 0) { @@ -4309,6 +4699,7 @@ static void play_file_stop(void) static int play_audio_file_event(char *file_name) { bk_printf("play_audio_file_event:\"%s\"",file_name); + if(app_audio_flag != 0) { bk_printf("play_audio_file:ERROR.音频资源忙"); @@ -4325,6 +4716,10 @@ void play_file_stream_data(char *file_name) bk_printf("play_file_stream_data:%s",file_name); AI_AMP_MSG_put(file_name,strlen(file_name)+1,PLAY_FILE_STREAM_DATA); } + +// play_file_stream_data("C:/vadend.pcm"); + +// play_file_stream_data_event("C:/vadend.pcm"); static int play_file_stream_data_event(char *file_name) { //bk_printf("play_file_stream_data:\"%s\"",file_name); @@ -4374,6 +4769,8 @@ static int play_file_stream_data_event(char *file_name) void AI_VC_put_play_data(char *data,u32 len) { u32 count_tmp = 0; + + // bk_printf("AI_VC_put_play_data:count_tmp = %d",len); if(len <= 0) return ; while(1) @@ -4483,7 +4880,7 @@ void VC_Play_End(void) // AI_VC_play_queue = NULL; } - // app_audio_stop(APP_AUDIO_TYPE_STREAM_DUPLEX, NULL); + //app_audio_stop(APP_AUDIO_TYPE_STREAM_DUPLEX, NULL); } static void AI_VC_put_record_data(char *data,u32 len) @@ -4516,14 +4913,9 @@ static void AI_VC_put_record_data(char *data,u32 len) return; PUT_RECORD_ERROR: - if(AI_VC_record_queue_data.data){ - + if(AI_VC_record_queue_data.data) aic_free(AI_VC_record_queue_data.data); - // AI_AMP_MSG_put(0,0,VC_Cancel); - - } - } @@ -4533,8 +4925,6 @@ PUT_RECORD_ERROR: void VC_record_Start(void) { bk_printf("VC_record_Start"); - - // is_dialog_status=0; //创建录音线程 if(AI_VC_record_task_id == 0) { @@ -4587,33 +4977,40 @@ void VC_record_End(void) } static void AI_VC_1S_timeout(void *param) { - // bk_msg_printf("AI_VC_1S_timeout 1!!!!!!!!!"); + //bk_msg_printf("AI_VC_1S_timeout 1!!!!!!!!!"); if(close_timerout>0) { close_timerout--; if(close_timerout<=0) { - AI_AMP_MSG_put(0,0,VC_CLOSE); - AI_AMP_MSG_put(0,0,CLOSE_IOT); + is_need_tip=1; + AI_AMP_MSG_put(0,0,VC_CLOSE); } - if(VC_state>=VC_State_TTSStart&&VC_state0){ - if(tts_start_timerout==15) - { - tts_start_timerout=0; + // if(VC_state<=VC_State_Listening){ + + silence_timerout++; - AI_AMP_MSG_put(0,0,RANDOM_MT); - } - }else - { - tts_start_timerout=0; + if(silence_timerout==30){ + + silence_timerout=0;//SILENCE_MESSAGE + + AI_AMP_MSG_put(0,0,SILENCE_MESSAGE); + } + + + // }else{ + + // silence_timerout=0; + + // } } } + if(sleep_chumo_timerout>0) { sleep_chumo_timerout--; @@ -4622,27 +5019,23 @@ static void AI_VC_1S_timeout(void *param) { is_dialog_status=1; - // VC_state = VC_State_End; - if(vc_cfg.VC_mode==0||vc_cfg.VC_mode==2) - AI_client_VC_synthesis("你好,我要去休息了,唤醒请说你好小七"); - else{ - - AI_AMP_MSG_put(0,0,VC_CLOSE); - // AI_AMP_MSG_put(0,0,CLOSE_IOT); + // VC_state = VC_State_End; + + AI_client_VC_synthesis("你好,爱喵喵去休息了,唤醒请说你好爱喵喵"); + + // AI_AMP_MSG_put("你好,爱喵喵去休息了,唤醒请说你好小七",strlen("你好,爱喵喵去休息了,唤醒请说你好小七"),VC_TTS_PLAY); + - close_timerout=2; - } - // if() } } - if(VC_pingping_timerout>0) { VC_pingping_timerout--; if(VC_pingping_timerout <= 0) { + issend_start_up=2; AI_VC_send_ping_ping(); } } @@ -4676,23 +5069,12 @@ static void AI_VC_1S_timeout(void *param) if (VC_TTS_PLAY_EVENT == VC_TTS_PLAY_POWER_OFF) { AI_AMP_MSG_put(0,0,POWER_OFF); - }else if (VC_TTS_PLAY_EVENT == VC_TTS_PLAY_RESET) - { - AI_AMP_MSG_put(0,0,POWER_RESET); } - VAD_status = 0; - - ddh_is_close_dialog=0; + AI_AMP_MSG_put(0,0,VC_CLOSE); - // AI_AMP_MSG_put(0,0,VC_CLOSE); - } - + } //AI_AMP_MSG_put(0,0,VC_RECORD_START);//开启录音 - - // AI_AMP_MSG_put(0,0,AI_MOT_BACK_MID); - bk_msg_printf("VC_END_play_timerout=%d",VC_END_play_timerout); - - AI_AMP_MSG_put(0,0,AI_MOT_BACK_MID); + bk_msg_printf("VC_END_play_timerout=%d",VC_END_play_timerout); } } // if(AI_VC_play_queue) @@ -4703,62 +5085,48 @@ static void AI_VC_1S_timeout(void *param) // } } char AI_key_long_flag = 0; + + 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"); - - IOT_V3_heart(); - - u64 timer_ms_tmp = aos_get_tickcount(); + u64 timer_ms_tmp = aos_get_tickcount(); if(timer_ms_tmp-timer_ms<1000)//每次触发小于1000ms关闭对话 { if(vc_cfg.VC_mode ==0 || vc_cfg.VC_mode == 2)//关闭服务 - { - - if(is_dialog_status==0){ - - is_dialog_status=1; - - AI_client_VC_synthesis("你好,我要去休息了,唤醒请说你好小七"); - - } - - } - // AI_VC_Close(); + AI_VC_Close(); return; } timer_ms = timer_ms_tmp; + issend_start_up=2; - if(vc_cfg.VC_mode == 0&&AI_VC_stata==1&&is_dialog_status==0){ - - return ; - }else{ + send_update=0; - is_dialog_status=0; + IOT_V3_update_statu(3,"",""); - } + IOT_V3_heart(); - if(vc_cfg.VC_mode == 1) - AI_key_long_flag = 1; + // IOT_V3_update_statu(4,"",""); + + bk_printf("AI_key_press1"); play_file_stream_data("C:/chenggong.pcm"); + + // is_need_tip=1; + bk_printf("AI_key_press"); - AI_VC_Close(); - + VC_state = VC_State_None; + + VC_record_Start(); if(AI_VC_start(0)==0) @@ -4774,9 +5142,6 @@ void AI_key_press(void) void AI_key_release(void) { - - AI_key_long_flag = 0; - if(vc_cfg.VC_mode ==1) { AI_AMP_MSG_put("请您按住按钮讲话,讲完松开即可。",strlen("请您按住按钮讲话,讲完松开即可。"),VC_TTS_PLAY); @@ -4825,6 +5190,7 @@ int32_t bk_audio_stop_callback(void *p_param, uint32_t size) return 0; } + int32_t bk_audio_start_callback(void *p_param, uint32_t size) { app_event_audio_t * p_app_event; @@ -4847,9 +5213,6 @@ int32_t bk_audio_start_callback(void *p_param, uint32_t size) char AI_VC_start_lock = 0; int net_status_count=0; - -extern int dmp_log_level; - //is_greeting,是否播放开场白 int AI_VC_start(char is_greeting) { @@ -4859,8 +5222,6 @@ int AI_VC_start(char is_greeting) goto AI_VC_start_RET; AI_VC_start_lock = 1; - // - bk_printf("AI_VC_start !!!!!!!! "); //play_file_stop();//先关闭一次本地文件音频播放 VC_state = VC_State_None; @@ -4893,8 +5254,8 @@ int AI_VC_start(char is_greeting) // aic_gpio_output(GPIO_25,GPIO_OUTPUT_LVL_HIGH );//led // aic_gpio_output(GPIO_30,GPIO_OUTPUT_LVL_HIGH );//led // aic_gpio_output(GPIO_31,GPIO_OUTPUT_LVL_HIGH );//led - - + // 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); @@ -4924,20 +5285,16 @@ int AI_VC_start(char is_greeting) // 启用VAD时初始化 - struct VadConfig conf; conf.sil_threshold = 63; - conf.energy_threshold = 10000;//10000 2000; - conf.min_speech_length = 240;//240 - conf.silence_detect_length = 300; - conf.rule1_min_tailing_silence = 480; + conf.energy_threshold = 10000; //2000 10000 + conf.min_speech_length = 240;//pre 240 + conf.silence_detect_length = 300;//pre 300 + conf.rule1_min_tailing_silence = 480;//pre 480 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; - - conf.auth_info.server_addr=2; - strcpy(conf.auth_info.dmp_token, get_token()); static int imsl_nn_init_flag = -1; if (imsl_nn_init_flag != 0) { @@ -4946,9 +5303,6 @@ int AI_VC_start(char is_greeting) imsl_nn_init_flag = 0; } - - - init_vad_ret = init_vad(&conf); @@ -4967,6 +5321,7 @@ int AI_VC_start(char is_greeting) } } + if(vc_cfg.is_KWS == 1) { struct KwsConfig kws_conf; @@ -4989,12 +5344,22 @@ int AI_VC_start(char is_greeting) } - #if BK_SOCKET_IOT - IOT_update_statu(1,"listening",""); - #endif + + IOT_V3_update_statu(1,"listening",""); + net_status_count=0; - //aos_sleep(5); - AI_VC_send_satrt(is_greeting);// + + + + //else{ + + // aos_sleep(50); + AI_VC_send_satrt(is_greeting); + // } + + // aos_sleep(50); + + // AI_VC_send_satrt(is_greeting);// close_timerout = 0; AI_VC_start_RET: AI_VC_start_lock = 0; @@ -5004,15 +5369,10 @@ AI_VC_start_RET: net_status_count++; play_audio_file("C:/xinhaoruo1.mp3"); - if(net_status_count>=3){ - - play_audio_file("C:/khye.mp3"); - AI_AMP_MSG_put(0,0,POWER_RESET); - - } + if(net_status_count>=3) + play_audio_file("C:/khye.mp3"); }else AI_VC_stata = 1; - return ret; } char AI_VC_Close_lock = 0; @@ -5025,17 +5385,10 @@ void AI_VC_Close(void) // aic_gpio_output(GPIO_25,GPIO_OUTPUT_LVL_LOW ); // aic_gpio_output(GPIO_30,GPIO_OUTPUT_LVL_LOW ); // aic_gpio_output(GPIO_31,GPIO_OUTPUT_LVL_LOW ); - // aic_gpio_output(MOT_EN_FWD,GPIO_OUTPUT_LVL_LOW ); - // aic_gpio_output(MOT_EN_REV,GPIO_OUTPUT_LVL_LOW); + // aic_gpio_output(GPIO_GPO_8,GPIO_OUTPUT_LVL_LOW ); + // aic_gpio_output(GPIO_GPO_1,GPIO_OUTPUT_LVL_LOW); // aic_gpio_output(GPIO_20,GPIO_OUTPUT_LVL_LOW ); - #if SUPPORT_MOT - - // MTO_stop(); - // MTO_restert(); - // MTO_restert_tb(); - - #endif - + bk_printf("AI_VC_Close!!!!!!!!!!!!"); websocket_deconnect_VC();//断开连接 //销毁录音线程 @@ -5206,15 +5559,11 @@ void AI_VC_Close(void) AI_ASR_str =NULL; } //app_logi("app_audio_stop2"); - - VC_state = VC_State_None; AI_VC_stata = 0; AI_VC_Close_lock = 0; - - // aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH); } -int MSG_Type_error = 0; + aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type) { AI_AMP_MSG_queue_t AI_AMP_MSG_queue_data = {0}; @@ -5235,18 +5584,11 @@ aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type) AI_AMP_MSG_queue_data.MSG_Type=MSG_Type; if(AI_AMP_task_queue && AOS_SUCCESS!=aos_send_to_queue(AI_AMP_task_queue,&AI_AMP_MSG_queue_data,AOS_NO_WAIT)) { - //bk_msg_printf("AI_AMP_MSG_put :aos_send_to_queue ERROR MSG_Type:%d\n\n",MSG_Type); - MSG_Type_error = MSG_Type; + bk_printf("AI_AMP_MSG_put :aos_send_to_queue ERROR\n\n"); if(!AI_AMP_MSG_queue_data.data) aic_free(AI_AMP_MSG_queue_data.data); return AOS_ERROR; } - if(MSG_Type_error!=0) - { - bk_msg_printf("AI_AMP_MSG_put :aos_send_to_queue ERROR MSG_Type:%d\n\n",MSG_Type_error); - MSG_Type_error = 0; - } - //bk_msg_printf("AI_AMP_MSG_put2 MSG_Type:%d",MSG_Type); return AOS_SUCCESS; } @@ -5258,28 +5600,38 @@ 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); - AI_AMP_MSG_put(NULL,0,AI_TIMER_EVENT_IOT); +static void uart_1S_timeout(void *param) +{ + AI_AMP_MSG_put(NULL,0,UART_TIMER_EVENT); +} +static void sys_60S_timeout(void *param) +{ + AI_AMP_MSG_put(NULL,0,AI_TIMER_EVENT_IOT); - //IOT_heart(); - + } - static void sys_60S_timeout_even(void) { - // bk_printf("sys_60S_timeout_even"); + bk_printf("sys_60S_timeout_even"); - if(AI_client_IOT!=NULL&&close_timerout>0){ + if(AI_client_IOT!=NULL){ IOT_V3_heart(); + + IOT_V3_update_statu(3,"",""); } } +void uart_recv_UI_handel_once_dir(); + +static void uart_1S_timeout_even(void){ + + uart_recv_UI_handel_once_dir(); +} + static void sys_10S_timeout_even(void) { // if (sizeof(dev_pass) == 0) @@ -5288,7 +5640,60 @@ static void sys_10S_timeout_even(void) // aut_login(); static u8 chg_status_tmp = 0; static u8 is_Low_power_warning = 0; + + char timeString[50]={0}; + time_t currentTime; + login_timer_count++; + + //time_t currentTime; + //time(¤tTime); + + /* 将时间转换为可读性更好的字符串 */ + + // strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", localtime(¤tTime)); + + // time(¤tTime); + + /* 将秒数转换为本地时间 */ + // struct tm* localTime = localtime(¤tTime); + + /* 打印日期和时间 */ + + + +// if(vc_cfg.alarm_count>0){ + +// aos_systime test; +// aos_get_systime(&test); +// sprintf(timeString,"%d:%02d",test.hour,test.minute); +// //bk_printf("系统时间为:%s",timeString); +// // bk_printf("闹钟个数为:%d",vc_cfg.alarm_count); +// for(int i=0;i0){ +// if(test.second<30){ +// char tipstr[300]={0}; +// // bk_printf("系统时间为:%d %d",test.hour,test.minute); +// sprintf(tipstr,"%s时间到了,请安排好行程",vc_cfg.alarm_content[i]); +// AI_AMP_MSG_put(tipstr,strlen(tipstr),VC_TTS_PLAY); +// } +// } +// } +// } + + // if(test.hour==10&&test.minute==45&&test.second<20){ + + // bk_printf("系统时间为:%d %d",test.hour,test.minute); + + // AI_AMP_MSG_put("闹钟时间到了,请安排好行程",strlen("闹钟时间到了,请安排好行程"),VC_TTS_PLAY); + + // // if(play_f) + // // play_file_stream_data("C:/alarm.pcm"); + + // } + if(login_timer_count>60*6) { login_timer_count = 0; @@ -5311,14 +5716,14 @@ static void sys_10S_timeout_even(void) if(psy_info.bat_capacity> 15 &&psy_info.bat_capacity<=25 && AI_VC_stata == 0 && is_Low_power_warning == 0 )//只响一次 { - play_audio_file("C:/Low_power_warning.mp3"); + // play_audio_file("C:/Low_power_warning.mp3"); is_Low_power_warning =1; }else if(psy_info.bat_capacity<=15) { // AI_AMP_MSG_put(NULL,0,VC_CLOSE);//关闭对话 // play_audio_file("C:/didianguanji.mp3");//播放低电关机 - AI_AMP_MSG_put("C:/didianguanji.mp3",strlen("C:/didianguanji.mp3"),POWER_OFF); + // AI_AMP_MSG_put("C:/didianguanji.mp3",strlen("C:/didianguanji.mp3"),POWER_OFF); } }else if(psy_info.bat_chg_status == 1)//充电状态 @@ -5330,21 +5735,51 @@ static void sys_10S_timeout_even(void) is_Low_power_warning =0; } chg_status_tmp = psy_info.bat_chg_status; - bk_printf("电量查询:psy_name:%s bat_vol:%d bat_cap:%d chg_status%d chg_type:%d bat_min_voltage:%d bat_max_voltage:%d\n", - - psy_info.psy_name, - psy_info.bat_current_voltage, - psy_info.bat_capacity, - psy_info.bat_chg_status, - psy_info.bat_chg_type, - psy_info.bat_min_voltage, - psy_info.bat_max_voltage); - // tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"当前电量为百分之%d。",psy_info.bat_capacity); + // bk_printf("电量查询:psy_name:%s bat_vol:%d bat_cap:%d chg_status%d chg_type:%d bat_min_voltage:%d bat_max_voltage:%d\n", + // psy_info.psy_name, + // psy_info.bat_current_voltage, + // psy_info.bat_capacity, + // psy_info.bat_chg_status, + // psy_info.bat_chg_type, + // psy_info.bat_min_voltage, + // psy_info.bat_max_voltage); + // tmp_data_len = snprintf(tmp_data,sizeof(tmp_data),"当前电量为百分之%d。",psy_info.bat_capacity); + + aos_systime test; + aos_get_systime(&test); + + + sprintf(timeString,"%d:%02d",test.hour,test.minute); + bk_printf("系统时间为:%s",timeString); + // bk_printf("系统时间为:%s",timeString); + + bk_printf("闹钟个数为:%d",vc_cfg.alarm_count); + + if(vc_cfg.alarm_count>0){ + + // bk_printf("闹钟个数为:%d",vc_cfg.alarm_count); + for(int i=0;i0){ + if(test.second<30){ + char tipstr[300]={0}; + // bk_printf("系统时间为:%d %d",test.hour,test.minute); + sprintf(tipstr,"%s闹钟时间到了,请安排好行程",vc_cfg.alarm_content[i]); + AI_AMP_MSG_put(tipstr,strlen(tipstr),VC_TTS_PLAY); + + break; + } + } + } + } + } // extern char *ota_url; - //const char *bk_make_timer = "V1_2_250730_8"; + void play_tts_my_local(char *filename) { @@ -5352,20 +5787,9 @@ void play_tts_my_local(char *filename) play_audio_file_event(filename); } + #define exptest -//void send_express_str(char *exp_str){ - - // texts = ["neutral","happy","laughing","funny","sad","angry", - // "crying","loving","embarrassed","surprised","shocked","thinking", - // "winking","cool","relaxed","","confused",""] - - /// happy 🙂 -//cue 😸 -//surprised 😲 -//angry 😠 -//crying 😭 -//smirk😏 -//dissatisfied😒 +int auth_vad_status=0; int random_mada=0; @@ -5391,49 +5815,45 @@ char *str_huashu[3][2][5]={ void random_chumo(int type,int mode){ - // mot_nod_status(); - - // play_file_stream_data("C:/chumo.pcm"); - // return ; - + int currenttick=aos_get_tickcount(); - int currenttick=aos_get_tickcount(); + // is_dialog_status=0; + // is_offlinecmd_status=0; - if(currenttick-chumo_tickcount<6000){ + if(currenttick-chumo_tickcount<3000){ bk_printf("太频繁.....\n"); return ; } - chumo_tickcount=currenttick; - - // is_dialog_status - - // is_dialog_status=0; - - // ||is_dialog_status==1 - - //if((VC_state==VC_State_Listening&&VAD_status==0)||VC_state==VC_State_End) //||VC_state==VC_State_None - // { - - + chumo_tickcount=currenttick; + bk_printf("VC_state11 %d.....\n",VC_state); // aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH); - if(type==0&&mode==0) - play_file_stream_data("C:/chumo.pcm"); + + + // if(type==0&&mode==0) + // play_file_stream_data("C:/chumo.pcm"); + + if(VC_state>VC_State_Listening&&is_dialog_status==0){ + return ; + } int index_temp=count_chumo_index[type][mode]; - if(index_temp<5) + if(index_temp<5&&is_dialog_status==0) { - app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); + AI_AMP_MSG_put(str_huashu[type][mode][index_temp],strlen(str_huashu[type][mode][index_temp]),VC_TTS_PLAY); + } - AI_AMP_MSG_put(str_huashu[type][mode][index_temp],strlen(str_huashu[type][mode][index_temp]),VC_TTS_PLAY); + if(is_dialog_status==1){ + + need_start_cmd=1; + AI_AMP_MSG_put("指令模式",strlen("指令模式"),VC_TTS_PLAY); - is_dialog_status=0; } // AI_client_VC_synthesis(str_huashu[type][mode][index_temp]); @@ -5441,100 +5861,85 @@ void random_chumo(int type,int mode){ count_chumo_index[type][mode]++; if(strlen(str_huashu[type][mode][index_temp+1])<5) - count_chumo_index[type][mode]=0; - - // aic_free(AI_TTS_str); - // AI_TTS_str = NULL; - VC_state = VC_State_TTSStart; - // play_audio_file("C:/chumo.mp3"); - - // return ; - // }else{ - - // bk_printf("VC_state22 %d.....\n",VC_state); - - // return ; - // } + count_chumo_index[type][mode]=0; + + // aic_free(AI_TTS_str); + // AI_TTS_str = NULL; + VC_state = VC_State_TTSStart; - // if(VC_Play_flag!=1){ + // play_audio_file("C:/chumo.mp3"); - // // aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH); - // play_audio_file("C:/chumo.mp3"); + return ; - // bk_printf("chumo.mp3.....\n"); - +} - // }else{ - // // aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH); - // bk_printf("duihuazhong.....\n"); +void uart_recv_UI_handel_once_dir() +{ - // return ; - - // } + //uart_data_t *data; - send_express_str_chumo(type,mode); + /** + * 1.Com number is aic_uart array index, aic_uart at aicos\projects\xxx\config\serial\serial_config.h; + * 2.use aic_uart[index] parameter to set serial. + */ + // serial_handle = uart_open(LOOPBACK_COM); + // aic_assert(serial_handle); + // bk_printf("uart_recv_UI_handel start"); - return ; + if(!ui_uart_handle) + return ; - // if(chumo_exp_open==1){ + // bk_printf("uart_recv_UI_handel start01"); + /* change baudrate */ + // uart_set_baud_rate(serial_handle, LOOPBACK_BANDRATE); - // random_mada++; + // while(1) { + /* read data. */ + // buf = aic_malloc(20); + size_t read; + char buf_ui[20]; - // if(random_mada==1){ - // send_express_str("happy"); - // } - // if(random_mada==2){ - // send_express_str("cue"); - // } - // if(random_mada==3){ - // send_express_str("surprised"); - // } - // if(random_mada==4){ - // send_express_str("angry"); - // } - // if(random_mada==5){ - // send_express_str("crying"); - // } - // if(random_mada==6){ - // send_express_str("smirk"); - // } - // if(random_mada==7){ - // send_express_str("dissatisfied"); - // } + read = uart_read_dir(ui_uart_handle, buf_ui, 20); - // if(random_mada==7) - // random_mada=0; + bk_printf("uart_recv_UI_handel start02 %d",read); - // } + if(read==0) + return ; - // aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH); + + bk_printf("uart_recv %02X %02X %02X %02X %02X %02X %02X ...\n",buf_ui[0],buf_ui[1],buf_ui[2],buf_ui[3],buf_ui[4],buf_ui[5],buf_ui[6]); -} - #define exptest + if(buf_ui[4]==0x80&&buf_ui[5]==0x01){ -void bk_task_suspend(void *p) -{ - // bk_printf("idle cpu sleep"); + // random_chumo(0,0); + // AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); - aic_gpio_input(GPIO_AGPIO_1, GPIO_INPUT_NOINT, NULL, NULL); + } -} + // if (read == 0 || read > 20) { + // drv_logw("uart_recv: err size = (%d)!\n", read); + // aic_dma_free(buf); + // continue; + // } -void bk_task_resume(void *p) -{ + // drv_logi("uart_recv: read size = (%d)!\n", read); - // bk_printf("cpu work"); + // data = aic_malloc(sizeof(*data)); + // data->addr = buf; + // data->size = read; + // aos_send_to_queue(uart_queue_id, (void*)&data, AOS_WAIT_FOREVER); + // } -} + // bk_printf("uart_recv_UI_handel close "); -extern imsl_nn_printf_func imsl_nn_printf; + /* close uart. */ + // uart_close(ui_uart_handle); -int auth_vad_status=0; -int auth_kws_status=0; +} static void uart_recv_UI_handel(void *thread_input) { @@ -5567,6 +5972,30 @@ static void uart_recv_UI_handel(void *thread_input) bk_printf("uart_recv %02X %02X %02X %02X %02X %02X %02X ...\n",buf_ui[0],buf_ui[1],buf_ui[2],buf_ui[3],buf_ui[4],buf_ui[5],buf_ui[6]); + + if(buf_ui[4]==0x80&&buf_ui[5]==0x01){ + + // random_chumo(0,0); + // AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); + + chumotype=0; + + AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); + + } + + + if(buf_ui[4]==0x80&&buf_ui[5]==0x02){ + + // random_chumo(0,0); + // AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); + + chumotype=1; + + AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); + + } + if (read == 0 || read > 20) { // drv_logw("uart_recv: err size = (%d)!\n", read); // aic_dma_free(buf); @@ -5588,27 +6017,26 @@ static void uart_recv_UI_handel(void *thread_input) // uart_close(ui_uart_handle); } +const char *bk_make_timer = "V1_2_25073M_90"; + +extern int dmp_log_level; static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 { - char count = 20; + char count = 150; boolean pdp_activated ; AI_AMP_MSG_queue_t AI_AMP_msg = {0}; init_VC_cfg(); - - - imsl_nn_printf = bk_printf; - + app_audio_set_master_volume(15*vc_cfg.VC_TTS_volume/100); - aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH );//lcd open ldo - - // GPIO_AGPIO_1 + // aos_sleep(20000); play_audio_file_event("C:/R_POWER_1.mp3"); + // aos_sleep(20000); - // play_audio_file("C:/R_POWER_1.mp3"); + // play_audio_file("C:/R_POWER_1.mp3"); // aic_get_version(FXH01_Version,sizeof(FXH01_Version)); // snprintf(FXH01_Version,sizeof(FXH01_Version),"FX01H_%s",bk_make_timer); @@ -5619,48 +6047,10 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 // http://dmp-ota.tos-cn-guangzhou.volces.com/ota/2025_07_24/1948312805093978114gx306l_h3.diff - bk_printf("FXH01_Version:%s",FXH01_Version); - bk_printf("FXH01_Version:%s",FXH01_Version); - bk_printf("FXH01_Version:%s",FXH01_Version); - - //slp_register_sleep_hook(bk_task_suspend, AIC_NULL, - // bk_task_resume, AIC_NULL); - - - // snd_soc_component_write(codec, ES8311_ADC_REG17, es8311->adc_volume); - // snd_soc_component_write(codec, ES8311_DAC_REG32, es8311->dac_volume); - - // aos_sleep(1000); - - while(count --) - { - pdp_activated = aic_is_default_pdp_activated(); - 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); - } - - - if(pdp_activated){ - - bk_printf("AI_AMP_2_5_init 入网成功..... \n"); - - }else{ - - bk_printf("AI_AMP_2_5_init 第一次入网失败..... \n"); - aic_send_cfun_cmd(0); - aos_sleep(500); - - aic_send_cfun_cmd(1); + bk_printf("FXH01_Version:%s",FXH01_Version); + bk_printf("FXH01_Version:%s",FXH01_Version); + bk_printf("FXH01_Version:%s",FXH01_Version); - aos_sleep(500); - - count=20; while(count --) { @@ -5669,12 +6059,10 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 break; bk_printf("AI_AMP_2_5_init 等待注网..... \n"); - // audio_play_tone(count%15,500);//播放提示音,测试用 - // audio_play_tone(3,500);//播放提示音,测试用 - - aos_sleep(1000); - } + //audio_play_tone(count%15,500);//播放提示音,测试用 + //audio_play_tone(3,500);//播放提示音,测试用 + aos_sleep(200); } bk_websocket_demo(); @@ -5700,8 +6088,6 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 // aos_sleep(5000); - - ota_update_version(); // if(strlen(ota_url)>3){ @@ -5709,7 +6095,9 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 dev_authentication_init(); - count =50; + aic_gpio_output(GPIO_15,GPIO_OUTPUT_LVL_HIGH);//lcd open ldo + + count =500; while(count --) { if(strlen(get_token()) > 5) @@ -5717,9 +6105,18 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 break; } bk_printf("AI_AMP_2_5_init 等待鉴权..... \n"); - aos_sleep(200); + aos_sleep(20); } + // if(AI_VC_1S_uart_timer == NULL) + // { + // AI_VC_1S_uart_timer = aos_create_timer("AI_VC_1S_uart_timer", + // uart_1S_timeout, &sys_10S_timer, + // 1000, + // AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); + // aic_assert(AI_VC_1S_uart_timer); + // } + if(sys_10S_timer == NULL) { sys_10S_timer = aos_create_timer("AI_VC_10S_timer", @@ -5729,109 +6126,75 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 aic_assert(sys_10S_timer); } - // AI_VC_60S_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, - 50000, + 30000, AOS_AUTO_ACTIVATE, AOS_BE_PERIOD); aic_assert(AI_VC_60S_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; - // dmp_log_level = 10086; - - // auth_info.server_addr=2; + dmp_log_level = 10086; - strcpy(auth_info.dmp_token, get_token()); + strcpy(auth_info.dmp_token, get_token()); if(strlen(auth_info.dmp_token)>0){ - for(int i=0;i<40;i++){ - + 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; + bk_printf("auth_vad fail token: %s..... \n",get_token()); - } + }else{ - aos_sleep(100); + bk_printf("auth_vad success token: %s..... \n",get_token()); - } + int ret_kws=auth_kws(&auth_info); - for(int i=0;i<40;i++){ + bk_printf("auth_kws success token: %d..... \n",ret_kws); - int ret=auth_kws(&auth_info); + if(ret_kws==0){ + for(int j=0;j<3;j++){ - if(ret==0){ + ret_kws=auth_kws(&auth_info); + bk_printf("auth_kws success token: %d..... \n",ret_kws); - bk_printf("auth_kws fail token: %s..... \n",get_token()); + if(ret!=0) + break; - } - else - { + } - bk_printf("auth_kws success token: %s..... \n",get_token()); + } + - auth_kws_status=1; - - break; - - } - - aos_sleep(100); + auth_vad_status=1; + break; } - bk_printf("auth_vad_status: %d auth_kws_status %d..... \n",auth_vad_status,auth_kws_status); - - if(auth_vad_status==0||auth_kws_status==0) - { - - // play_audio_file_event("C:/xinhaoruo1.mp3"); - - // aos_sleep(500); - } + }else{ + // play_audio_file_event("C:/xinhaoruo1.mp3"); } - else - { + if(auth_vad_status==0){ - // play_audio_file_event("C:/xinhaoruo1.mp3"); - } + // play_audio_file_event("C:/xinhaoruo1.mp3"); - + aos_sleep(2000); + + } #ifdef exptest - aos_sleep(500); + aos_sleep(50); ui_uart_handle = uart_open(2); @@ -5843,9 +6206,9 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 // uart_write(ui_uart_handle,"123", strlen("123")); - u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x01,0x01,0x03}; + // u8 cmd_ui[7]={0xAA,0x55,0xFF,0x02,0x02,0x01,0x04}; - uart_write(ui_uart_handle,cmd_ui, 7); + // uart_write(ui_uart_handle,cmd_ui, 7); }else{ @@ -5853,6 +6216,7 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 } + if(UI_UART_read_id == 0) { UI_UART_read_id = aos_create_thread( @@ -5867,31 +6231,27 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 bk_printf("uart 读线程 UI_UART_read_id:%d",UI_UART_read_id); } - -#endif -#if BK_SOCKET_IOT - bk_iot_init();//iot初始化 #endif + + aic_network_enable_pdp_nic_capdump();//打开网络调试log // while(1) // { - // aos_sleep(1000); + // } - #if SUPPORT_MOT - mot_init(); -#endif + + if(vc_cfg.dev_type == DEV_JSWM)//金三完美使用dmp配置开场白 { - AI_VC_start(1); + + AI_VC_start(1); + } else{ VC_TTS_Play(vc_cfg.power_on_tone);//开启vc播放tts } - - - while (1) { if(AI_AMP_task_queue) { @@ -5919,40 +6279,17 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 //发送打断 //AI_VC_send_cancel();//发送打断,发送完打断还有发开始音频 //app_logi("interruption!!!!!!!!!!!!!!!!!!!!!"); - - is_dialog_status=0; - VC_Play_End(); VC_state = VC_State_None; websocket_deconnect_VC(); websocket_connect_VC();//重连 AI_VC_send_satrt(0);//发送开始 - - AI_AMP_MSG_put(0,0,VC_PLAY_START);//打开声音 + VC_Play_Start(); break; case VC_CLOSE: AI_VC_Close(); - break; - case CLOSE_IOT: - - if(AI_client_IOT)//先断开再关闭读线程 - { - delete_websocket_res(AI_client_IOT);//删除该对象ws资源管控 - close_websocket_client(AI_client_IOT); - AI_client_IOT = NULL; - AI_VC_read_IOT_id=0; - - bk_printf("CLOSE_IOT"); - // AI_client_IOT - } - - break; - case RANDOM_MT: - - random_statu_mot(); - break; case VC_START: AI_VC_start(0); @@ -5987,6 +6324,16 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 VC_TTS_PLAY_EVENT = VC_TTS_PLAY_POWER_OFF; } break; + case VC_TTS_PLAY_CMD://播放完关机 + + play_data_len = 10*1024;//让音频播放起来 + aimaimiao_hande_cmd("卡卡"); + + // mp3_play_file("C:/am_003.mp3"); + + // play_file_stream_data_event("C:/vadend.pcm"); + + break; case VC_TTS_PLAY_RESET://播放完重启: if(AI_AMP_msg.data!=NULL) { @@ -5994,8 +6341,7 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 VC_TTS_PLAY_EVENT = VC_TTS_PLAY_RESET; } break; - - + case VC_CONNECT_ERROR://连接错误 AI_VC_Close(); if(AI_AMP_msg.data) @@ -6005,14 +6351,78 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 break; case AI_KEY_PRESS: - // is_dialog_status=0; + AI_key_press(); + + break; + //SILENCE_MESSAGE + case SILENCE_MESSAGE: + + bk_printf("SILENCE_MESSAGE"); + + aimaimiao_hande_cmd("摇尾巴"); + + break; + case IOT_MESSAGE_MOT: + + // AI_key_press(); + if(AI_AMP_msg.data!=NULL){ + + bk_printf("iot receive is %s",AI_AMP_msg.data); + aos_sleep(1000); + deal_iot_receive(AI_AMP_msg.data); + } + - AI_key_press(); break; case AI_KEY_RELEASE: AI_key_release(); - break; + break; + + case AI_KEY3_RELEASE: + bk_printf("AI_KEY3_RELEASE"); + // #if SUPPORT_MOT + // mot_nod(5); + // #endif + // if(AI_VC_stata==1&&is_dialog_status==0) + + // if(AI_VC_stata==0){ + + // send_express_aimiaomiao("卡卡"); + + bk_printf("AI_KEY3_RELEASE001 AI_VC_stata %d chumotype%d",AI_VC_stata,chumotype); + + if(chumotype==0) + random_chumo(2,0); + else + random_chumo(0,0); + + // bk_printf("AI_KEY3_RELEASE001"); + // aimaimiao_hande_cmd("卡卡"); + + // }else{ + + // if(is_dialog_status==1){ + // aimaimiao_hande_cmd("卡卡"); + // bk_printf("AI_KEY3_RELEASE002"); + + // } + // else{ + + // random_chumo(0,0); + + // bk_printf("AI_KEY3_RELEASE003"); + // } + + // } + // else{ + // aimaimiao_hande_cmd("卡卡"); + //} + + + + break; + case AI_KEY_LONG_PRESS: AI_key_long_press(); break; @@ -6020,7 +6430,13 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 AI_key_long_release(); break; case PWK_KEY_RELEASE: - pwk_key_release(); + + // random_chumo(0,0); + + bk_printf("pwk_key_release"); + + // pwk_key_release(); + break; case PLAY_AUDIO_FILE: play_audio_file_event(AI_AMP_msg.data); @@ -6031,75 +6447,24 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 case AI_TIMER_EVENT: sys_10S_timeout_even(); break; - case AI_TIMER_EVENT_IOT: + case UART_TIMER_EVENT: + uart_1S_timeout_even(); + break; + + case AI_TIMER_EVENT_IOT: sys_60S_timeout_even(); break; case AI_PRINTF: if(AI_AMP_msg.data) uart_write(ai_uart_handle,AI_AMP_msg.data, AI_AMP_msg.len); + break; + + case AI_ALARM_UPDATE: + //if(AI_AMP_msg.data) + //uart_write(ai_uart_handle,AI_AMP_msg.data, AI_AMP_msg.len); + get_alarm_info13(); break; - case AI_KEY2_RELEASE: - // bk_printf("AI_KEY2_RELEASE"); - - // mot_nod_status(); - if(AI_VC_stata==1) - random_chumo(2,0); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - break; - case AI_KEY3_RELEASE: - bk_printf("AI_KEY3_RELEASE"); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - if(AI_VC_stata==1) - random_chumo(0,0); - break; - case AI_KEY4_RELEASE: - bk_printf("AI_KEY4_RELEASE"); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - if(AI_VC_stata==1) - random_chumo(1,0); - break; - case AI_KEY2_LONG_PRESS: - bk_printf("AI_KEY2_LONG_PRESS"); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - if(AI_VC_stata==1) - random_chumo(2,1); - - break; - case AI_KEY3_LONG_PRESS: - bk_printf("AI_KEY3_LONG_PRESS"); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - if(AI_VC_stata==1) - random_chumo(0,1); - break; - case AI_KEY4_LONG_PRESS: - bk_printf("AI_KEY4_LONG_PRESS"); - // #if SUPPORT_MOT - // mot_nod(5); - // #endif - if(AI_VC_stata==1) - random_chumo(1,1); - break; - case MOT_GPIO_HANDLE_CB: - - mot_gpio_handle_cb(AI_AMP_msg.data); - break; - - case AI_MOT_BACK_MID: - - back_middle_statu_mot(); - break; - default: break; } @@ -6107,7 +6472,7 @@ static void AI_AMP_2_5_task(void *thread_input)//做逻辑线程 aic_free(AI_AMP_msg.data); }else { - aos_sleep(10); + aos_sleep(10); } @@ -6135,7 +6500,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, @@ -6145,8 +6510,7 @@ 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 1d08871..6373a30 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,16 +1,12 @@ #include "aic_osa.h" #define SUPPORT_OPUS 1 -#define SUPPORT_MOT 1 //马达驱动 -#define BK_SOCKET_IOT 0 - #define BK_LOG_NONE 0//不打印 #define BK_LOG_UART 1//串口打印 #define BK_LOG_USB 2//USB打印 -// -#define BK_LOG_CFG BK_LOG_USB + #define BK_LOG_CFG BK_LOG_USB typedef enum { @@ -51,11 +47,18 @@ typedef struct int speech_rate;//语速配置 unsigned int vad_silence;//vad断句时间 ms char VC_TTS_volume; + + char alarm_name[5][50]; + char alarm_content[5][200]; + + u8 alarm_count; //unsigned unsigned int end_flag; }AI_AMP_2_5_cfg; + + //#define FXH01_Version "BK_FX01H_V202505300931" extern char FXH01_Version[]; extern AI_AMP_2_5_cfg vc_cfg; @@ -73,31 +76,24 @@ typedef enum AI_AMP_MSG VC_TTS_PLAY_CLOSE,//播放完关闭对话 VC_TTS_PLAY_RESET,//播放完重启 VC_TTS_PLAY_POWER_OFF,//播放完关机 + VC_TTS_PLAY_CMD,//播放完关机 VC_CLOSE, VC_CONNECT_ERROR, AI_KEY_PRESS,//AI键按下 AI_KEY_RELEASE,//AI键释放 + AI_KEY3_RELEASE, AI_KEY_LONG_PRESS,//AI键长按 AI_KEY_LONG_RELEASE,//AI键长按释放 - AI_KEY2_PRESS,//KEY2 - AI_KEY3_PRESS,//KEY2 - AI_KEY4_PRESS,//KEY2 - AI_KEY2_RELEASE, - AI_KEY3_RELEASE, - AI_KEY4_RELEASE, - AI_KEY2_LONG_PRESS, - AI_KEY3_LONG_PRESS, - AI_KEY4_LONG_PRESS, PWK_KEY_RELEASE,//侧键 PLAY_AUDIO_FILE,//播放本地音频文件 PLAY_FILE_STREAM_DATA,//播放本地文件,用音频流的方式 AI_TIMER_EVENT,//定时器事件 - MOT_GPIO_HANDLE_CB, + UART_TIMER_EVENT, AI_PRINTF,//打印 AI_TIMER_EVENT_IOT,//定时器事件 - AI_MOT_BACK_MID, - CLOSE_IOT, - RANDOM_MT, + AI_ALARM_UPDATE, + IOT_MESSAGE_MOT, + SILENCE_MESSAGE, } AI_AMP_MSG_Type; typedef struct { @@ -139,7 +135,6 @@ 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); @@ -153,4 +148,10 @@ int save_VC_cfg(void); int play_audio_file(char *file_name); void AI_VC_put_play_data(char *data,u32 len);//音频数据入队播放 -int bk_get_iccid(char *iccid); \ No newline at end of file +void IOT_update_statu(int type,char *type_str,char *content); + +void IOT_heart(); + +void IOT_V3_update_statu(int type,char *type_str,char *content); + +void IOT_V3_heart(); \ 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 27346ec..feb6784 100644 --- a/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt +++ b/2.code/aicos/app/AI_AMP_2_5/CMakeLists.txt @@ -11,8 +11,8 @@ 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 - ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/mot_dev.c + ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/aic_mp3decoder_api.c + ${AIC_SOURCE_ROOT}/app/AI_AMP_2_5/aic_mp3decoder_demo.c ) diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_key.c b/2.code/aicos/app/AI_AMP_2_5/bk_key.c index 9552a8a..0bfe880 100644 --- a/2.code/aicos/app/AI_AMP_2_5/bk_key.c +++ b/2.code/aicos/app/AI_AMP_2_5/bk_key.c @@ -10,13 +10,15 @@ #include "alog_api.h" #include "pwr_key.h" #include "sysoff.h" -#include "gpo_inc.h" -#include -#define TP_POWER_EN GPO_0 + + #define LOG_MODULE AMOD_KPC + + + static int32_t bk_kpc_callback(void *p_param, uint32_t size) { kpc_key_array_t *key_arr = (kpc_key_array_t *)p_param; @@ -26,81 +28,36 @@ static int32_t bk_kpc_callback(void *p_param, uint32_t size) } for (int i = 0; i < KPC_KEY_PRESSED_MAX_KEY_NUM; i++) { - // bk_printf("bk_kpc_callback key[%d].key_value=%d",i,key_arr->key[i].key_value); + bk_printf("bk_kpc_callback key[%d].key_value=%d",i,key_arr->key[i].key_value); switch (key_arr->key[i].key_event) { case KPC_KEY_EVENT_PRESS://按下 - - // bk_printf("[%x]testkey123",key_arr->key[i].key_value); - - // bk_printf("[%x]testkey123",key_arr->key[i].); - if (key_arr->key[i].key_value == 0) { //AI键 + bk_printf("[%d]AI_key_press",key_arr->key[i].key_value); - // bk_printf("00000000000"); + if (key_arr->key[i].key_value == 0) {//AI键 AI_AMP_MSG_put(0, 0, AI_KEY_PRESS); - // bk_printf("[%d]AI_key_press",key_arr->key[i].key_value); - }else if(key_arr->key[i].key_value == 1) {//AI键 - // bk_printf("111111111"); + AI_AMP_MSG_put(0, 0, AI_KEY_PRESS); - }else if(key_arr->key[i].key_value == 2) {//按键2键 - // bk_printf("222222"); - AI_AMP_MSG_put(0, 0, AI_KEY2_PRESS); - }else if(key_arr->key[i].key_value == 4) {//按键2键 - // bk_printf("222222"); - AI_AMP_MSG_put(0, 0, AI_KEY2_PRESS); - }else if(key_arr->key[i].key_value == 5) {//按键2键 - // bk_printf("222222"); - AI_AMP_MSG_put(0, 0, AI_KEY3_PRESS); - }else if(key_arr->key[i].key_value == 6) {//按键2键 - // bk_printf("222222"); - AI_AMP_MSG_put(0, 0, AI_KEY4_PRESS); } break; case KPC_KEY_EVENT_RELEASE://释放 - - // bk_printf("KPC_KEY_EVENT_RELEASE"); - if (key_arr->key[i].key_value == 0) {//AI键 AI_AMP_MSG_put(0, 0, AI_KEY_RELEASE); }else if(key_arr->key[i].key_value == 1) {//AI键 AI_AMP_MSG_put(0, 0, AI_KEY_RELEASE); - }else if(key_arr->key[i].key_value == 4) {// - - AI_AMP_MSG_put(0, 0, AI_KEY2_RELEASE); - - }else if(key_arr->key[i].key_value == 5) {// + }else if(key_arr->key[i].key_value == 10) {// - AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); - - }else if(key_arr->key[i].key_value == 6) {// - - AI_AMP_MSG_put(0, 0, AI_KEY4_RELEASE); + // AI_AMP_MSG_put(0, 0, AI_KEY3_RELEASE); } - - + bk_printf("[%d]KPC_KEY_EVENT_RELEASE",key_arr->key[i].key_value); break; - case KPC_KEY_EVENT_LONGKEY://长按 - - bk_printf("KPC_KEY_EVENT_LONGKEY"); if (key_arr->key[i].key_value == 0) {//AI键 AI_AMP_MSG_put(0, 0, AI_KEY_LONG_PRESS); - }else if(key_arr->key[i].key_value == 1) {//AI键 + }else if(key_arr->key[i].key_value == 1) { //AI键 AI_AMP_MSG_put(0, 0, AI_KEY_LONG_PRESS); - }else if(key_arr->key[i].key_value == 5) {//AI键 - - AI_AMP_MSG_put(0, 0, AI_KEY3_LONG_PRESS); - - } - else if(key_arr->key[i].key_value == 6) {//AI键 - - AI_AMP_MSG_put(0, 0, AI_KEY4_LONG_PRESS); - - }else if(key_arr->key[i].key_value == 4) {//AI键 - - AI_AMP_MSG_put(0, 0, AI_KEY2_LONG_PRESS); } break; @@ -111,10 +68,10 @@ static int32_t bk_kpc_callback(void *p_param, uint32_t size) AI_AMP_MSG_put(0, 0, AI_KEY_LONG_RELEASE); } else if(key_arr->key[i].key_value == 2) {//PWK键 - //AI_AMP_MSG_put(0, 0, PWK_KEY_RELEASE); + AI_AMP_MSG_put(0, 0, PWK_KEY_RELEASE); } - // bk_printf("[%d]AI_key_long_press",key_arr->key[i].key_value); + bk_printf("[%d]KPC_KEY_EVENT_LONGKEY_RELEASE",key_arr->key[i].key_value); break; default: @@ -122,7 +79,7 @@ static int32_t bk_kpc_callback(void *p_param, uint32_t size) break; } } - // bk_printf("\n"); + bk_printf("\n"); return 0; @@ -138,12 +95,12 @@ static int32_t bk_kpc_pwr_key_handle(void *para, uint32_t size) switch (pwr_status) { case AIC_PWR_KEYDOWN: - bk_msg_printf("AIC_PWR_KEYDOWN"); + bk_msg_printf("AIC_PWR_KEYDOWN001"); AI_AMP_MSG_put(0, 0, PWK_KEY_RELEASE); break; case AIC_PWR_KEYUP: - bk_msg_printf("AIC_PWR_KEYUP"); + bk_msg_printf("AIC_PWR_KEYUP001"); //play_audio_file("C:/zhengzaiguanji.mp3"); break; @@ -160,8 +117,6 @@ void bk_to_ups_sync(void *ptr) { // gx_ups_phy_from_rtc(); AI_AMP_MSG_put(0, 0, VC_CLOSE); - //aos_sleep(1000); - AI_AMP_MSG_put(0,0,AI_MOT_BACK_MID); // gx_ups_phy_cnt_clr(); play_audio_file("C:/zhengzaiguanji.mp3"); bk_msg_printf("bk_to_ups_sync"); @@ -171,10 +126,7 @@ void bk_to_ups_sync(void *ptr) void bk_key_init(void) { // bk_printf("开始初始化按键模块\n"); 不注释掉就卡住没日志 - aic_gpo_output(TP_POWER_EN,GPO_OUTPUT_HIGH );//tp电源 - - // aic_gpo_output(GPO_13,GPO_OUTPUT_LOW);//tp电源 - + int32_t handle = aic_srv_kpc_register("app", bk_kpc_callback); if (handle < 0) { alog_error("Failed to register KPC callback\n"); // 使用 alog_error 输出错误信息 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 26f663c..326c798 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 @@ -278,9 +278,9 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) /**********1阶段判断头********** */ if(ogg_hend_len==0)//没有剩余的头数据 { - // bk_printf("ogg_hend_len123:%d",ogg_hend_len); + bk_printf("ogg_hend_len123:%d",ogg_hend_len); - // bk_printf("bk_rb_data_len:%d",bk_rb_data_len(OPUS_decoder_rb_data)); + bk_printf("bk_rb_data_len:%d",bk_rb_data_len(OPUS_decoder_rb_data)); bk_rb_read(OPUS_decoder_rb_data,ogg_hend,27); ogg_hend_len+=27;//读页头 @@ -295,7 +295,7 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) { if(bk_rb_data_len(OPUS_decoder_rb_data)< seg_num)//判断可读数据够不够 { - // bk_printf("算页大小数据不够,退出当前解包:seg_num %d",seg_num); + bk_printf("算页大小数据不够,退出当前解包:seg_num %d",seg_num); break; } @@ -327,11 +327,11 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) } break; } - // bk_printf("整页数据大小:%d",seg_size); + bk_printf("整页数据大小:%d",seg_size); /************3阶段读完整包****************** */ if(bk_rb_data_len(OPUS_decoder_rb_data)< seg_size)//判断可读数据够不够 { - // bk_printf("读整页数据大小不够,退出当前解包:seg_size %d,ogg_hend_len:%d",seg_size,ogg_hend_len); + bk_printf("读整页数据大小不够,退出当前解包:seg_size %d,ogg_hend_len:%d",seg_size,ogg_hend_len); break; } @@ -370,7 +370,7 @@ 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++; @@ -400,7 +400,7 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) // 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 ); + 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; @@ -429,6 +429,10 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) #endif return 0; } + + +// char pcm_buf_back[ = aic_malloc(1920)]; + int my_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) { int data_count = 0; @@ -462,7 +466,7 @@ int my_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) dec_inparam.outbuffer_size = 1920; ret = aic_opus_decode(&dec_inparam); - // bk_printf("opus解码 输入大小:%d,输出大小:%d", in_ogg_data_len,dec_inparam.outputfrms_size); + // bk_printf("opus解码 输入大小:%d,输出大小:%d", in_ogg_data_len,dec_inparam.outputfrms_size); opus_encode_len++; @@ -482,7 +486,7 @@ int my_opus_decode_data(char *in_ogg_data,int in_ogg_data_len) AI_VC_put_play_data(pcm_buf,dec_inparam.outputfrms_size);//发送数据到音频队列 - // audio_data(pcm_buf,dec_inparam.outputfrms_size,24); + audio_data(pcm_buf,dec_inparam.outputfrms_size,24); // aos_sleep(1); diff --git a/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.h b/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.h index 4220e30..8fc76dd 100644 --- a/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.h +++ b/2.code/aicos/app/AI_AMP_2_5/bk_opus_coder.h @@ -8,4 +8,5 @@ int ogg_opus_decode_data(char *in_ogg_data,int in_ogg_data_len); int my_opus_decode_data(char *in_ogg_data,int in_ogg_data_len); + #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 2dbcd22..42d3dcf 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 @@ -333,6 +333,8 @@ int send_handshake(WebSocketClient *client,const char *headers) { char *request =aic_zalloc(BUFFER_SIZE); if(headers!= NULL) { + + bk_printf("headers12"); snprintf(request, BUFFER_SIZE, "GET /%s?%s HTTP/1.1\r\n" "Host: %s:%d\r\n" @@ -346,6 +348,8 @@ int send_handshake(WebSocketClient *client,const char *headers) { client->path,client->query ,client->host,client->port, accept_key,client->host,client->port,headers); }else { + + bk_printf("headers13"); snprintf(request, BUFFER_SIZE, "GET /%s?%s HTTP/1.1\r\n" "Host: %s:%d\r\n" @@ -429,10 +433,122 @@ 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; + // } + + bk_printf("receive_iot_message................................."); + + 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) { @@ -668,204 +784,247 @@ int receive_websocket_message(WebSocketClient *client, unsigned char *buf,int l return payload_len;//parse_frame(buf, bytes_received, buf,code); } -// 初始化 WebSocket 客户端 -WebSocketClient *init_websocket_client(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_client 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 ) - //if( getaddrinfo( "www.baidu.com", "80", &hints, &addr_list ) != 0 ) +int receive_websocket_message_V3(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); + if(!client ) + return -1; + unsigned char frame[20]={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) { - bk_printf("init_websocket_client getaddrinfo error"); - goto client_ERROR; - } + bytes_received = recv(client->sockfd, frame, 1, 0); - 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("receive_websocket_message:frame:0x%x,bytes_received:%d",frame[0],bytes_received); + + if (bytes_received == 0) { + bk_printf("Connection closed by server.1\n\n\n\n"); + return 0; + } else if(bytes_received<0){ + bk_printf("receive message failed1\n\n\n\n"); + return -1; } + if(frame[0]!= 0x80 && frame[0]!= 0x81 && frame[0]!= 0x82 && frame[0]!= 0x88 && frame[0]!= 0x89 && frame[0]!= 0x8A) + { + error_count++; + bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x error_count =%d ERROR!!!!!!!!!",frame[0],frame[1],last_byte,error_count); - if( connect( client->sockfd , cur->ai_addr, (int)cur->ai_addrlen ) == 0 ) + if(last_byte!= 0x80 && last_byte!= 0x81 && last_byte!= 0x82 && last_byte!= 0x88 && last_byte!= 0x89 && last_byte!= 0x8A) + { + bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x ERROR222222222!!!!!!!!!",frame[0],frame[1],last_byte); + continue; + } + + }else { break; } + } + // bk_printf("websocket2 timer_ms=%d\n",timer_ms- aos_get_tickcount()); + // timer_ms = aos_get_tickcount(); + //bk_sleep(5); + bytes_received = recv(client->sockfd, frame+1, 1, 0); + if (bytes_received == 0) { + bk_printf("Connection closed by server.2\n\n\n\n"); + return 0; + } else if(bytes_received<0){ + bk_printf("receive message failed 2 \n\n\n\n"); + return -1; + } + //unsigned char fin = (frame[0] & 0x80) >> 7; + *code = frame[0] & 0x0F; + unsigned char mask = (frame[1] & 0x80) >> 7; + unsigned int payload_len = frame[1] & 0x7F; + - close( client->sockfd ); - + size_t offset = 2; + if (payload_len == 126) { + unsigned short extended_payload_len; + bytes_received = 0; + while(1) + { + recv_len= recv(client->sockfd, frame+offset+bytes_received, 2-bytes_received, 0); + if (recv_len == 0) { + bk_printf("Connection closed by server 3.\n\n\n\n"); + return 0; + } else if(recv_len<0){ + bk_printf("receive message failed 3 \n\n\n\n"); + return -1; + } + bytes_received +=recv_len; + if(bytes_received>=2) + break; + + } + // bytes_received_t = bytes_received; + memcpy(&extended_payload_len, frame + 2, 2); + payload_len = ntohs(extended_payload_len); + //bk_printf("receive_websocket_message:payload_len=0x%X",extended_payload_len); + offset += 2; + } else if (payload_len == 127) { + u64 extended_payload_len; + bytes_received = 0; + while(1) + { + recv_len= recv(client->sockfd, frame+offset+bytes_received, 8-bytes_received, 0); + if (recv_len == 0) { + bk_printf("Connection closed by server 4.1\n\n\n\n"); + return 0; + } else if(recv_len<0){ + bk_printf("receive message failed 4\n\n\n\n"); + return -1; + } + bytes_received +=recv_len; + if(bytes_received>=8) + break; + + } + memcpy(&extended_payload_len, frame + 2, 8); + payload_len = ntohll(extended_payload_len); + offset += 8; } - freeaddrinfo(addr_list); // 释放资源 - if(client->sockfd<0) + if(payload_len>len) { - bk_printf("init_websocket_client connect error"); - goto client_ERROR; + bk_printf("receive_websocket_message:len =%d ERROR",payload_len); + recv(client->sockfd, buf, len, 0); + return -1; } - bk_printf("init_websocket_client 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; -// } - - - 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) { //发送头 - close(client->sockfd); - goto client_ERROR; - } - - buffer=aic_zalloc(BUFFER_SIZE); - ssize_t bytes_received = recv(client->sockfd, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received > 0) { - buffer[bytes_received] = '\0'; - bk_printf("Received handshake response:\n%s\n", buffer); - } - if(strstr(buffer,"HTTP/1.1 101") == NULL) - { + // bk_printf("receive_websocket_message :"); + // for(int i = 0;isockfd, frame+offset, 4, 0); + memcpy(masking_key, frame + offset, 4); + offset += 4; + } + + + bytes_received = 0; + // payload_len-=1;//少读一个字节,防止粘包 + //bk_sleep(1); + while(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; + } else if(recv_len<0){ + bk_printf("receive message failed 5\n\n\n\n"); + return -1; + } -// 关闭 WebSocket 客户端 -void close_websocket_client(WebSocketClient *client) { - if(client) + bytes_received += recv_len; + + //bk_printf("receive_websocket_message:bytes_received=%d recv_len =%d",bytes_received,recv_len); + if(bytes_received >= payload_len) + { + //bk_printf("receive_websocket_message:payload_len=%d,bytes_received=%d",payload_len,bytes_received); + break; + } + + } + //bk_sleep(1); + // bk_printf("websocket 3 timer_ms=%d\n",timer_ms- aos_get_tickcount()); + // timer_ms= aos_get_tickcount(); + // bk_sleep(1);//等最后一字节返回,防止粘包 + // recv(client->sockfd, buf+payload_len, 1, 0);//读最后一字节 + // payload_len+=1; + if(mask) { - soc_close(client->sockfd); - aic_free(client); + for (size_t i = 0; i < payload_len; ++i) { + buf[i] = buf[i] ^ masking_key[i % 4]; + } } -} -int http_get_client(const char *url, const char *headers,const char *Content_Type,char* response,int response_len); -int bk_websocket_demo(void) { - // WebSocketClient *client; - // client = init_websocket_client("ws://118.145.158.207:22086/vc/coze?is_auth=0",NULL); - // if (client== NULL) { - // return -1; + // if (*code == WS_TEXT) { + // bk_printf("Received text message: %.*s\n", (int)payload_len, buf); + // } else if (*code == WS_BINARY) { + // bk_printf("Received binary message of length %zu\n", payload_len); // } - // close_websocket_client(client); - // aos_sleep(2); - // char *response =aic_zalloc(BUFFER_SIZE); - // //char *url = "http://dmp.raisound.com/sv/vad/auth/token?nonceStr=abc&sign=3470327e8eee163f00d5f54cc936d32595189b16d1e89c2b50155b80227c85d0×tamp=1750406765243&token=9542143a532adc10ec6ee530cd1824d53b"; - // char *url = "http://dmp.raisound.com/bz-mp-api/sv/vad/auth/token?nonceStr=abc&sign=3470327e8eee163f00d5f54cc936d32595189b16d1e89c2b50155b80227c85d0×tamp=1750406765243&token=9542143a532adc10ec6ee530cd1824d53b"; - // http_get_client(url,"bz-dt:9542143a532adc10ec6ee530cd1824d53b","application/x-www-form-urlencoded",response,BUFFER_SIZE); - // bk_printf("http GET data:%s",response); - // aic_free(response); - return 0; -} - -/************************http*********************************** */ + // if(error_count>0) + // { + // bk_printf("WS ERROR!!!!!!last_byte:0x%x bytes_received_t_1:0x%x\r\n\n\n\n\n\n\n",last_byte,bytes_received_t_1); + // bk_print_hex(frame_t,frame_t_len); + // bk_print_hex(websocket_tmp_b,websocket_tmp_b_len); + // bk_printf("WS ERROR!!!!!!\r\n\n\n\n\n\n\n"); + // bk_print_hex(frame,offset); + // bk_print_hex(buf,payload_len); + + // } + // bytes_received_t_1 = bytes_received_t; + if (*code == WS_TEXT) + { -// WebSocket 客户端结构体 -typedef struct { - int sockfd; - char host[128]; - char path[128]; - char query[512]; - int port; -} httpClient; -int send_http_get_handshake(httpClient *client,const char *headers,const char *Content_Type) { - char key[WEBSOCKET_KEY_LEN]; - bk_printf("send_handshake1"); - generate_websocket_key(key); - char accept_key[64]; - int ret = 0; - generate_accept_key(key, accept_key); - bk_printf("send_handshake2"); - char *request =aic_zalloc(BUFFER_SIZE); - if(headers!= NULL) + }else if(*code == WS_BINARY) { - snprintf(request, BUFFER_SIZE, - "GET /%s?%s HTTP/1.1\r\n" - "Host: %s:%d\r\n" - "Content-Type: %s\r\n" - "Connection: close\r\n" - "%s\r\n" - "\r\n", - client->path,client->query ,client->host,client->port,Content_Type,headers); - }else + + }else if(*code == WS_CLOSE) { - snprintf(request, BUFFER_SIZE, - "GET /%s?%s HTTP/1.1\r\n" - "Host: %s:%d\r\n" - "Content-Type: %s\r\n" - "Connection: close\r\n" - "\r\n", - client->path,client->query , client->host,client->port,Content_Type); + payload_len = 0; + }else if(*code == WS_PONG) + { + //payload_len = 1; + }else if(*code == WS_PING) + { + send_websocket_message(client,buf,payload_len,WS_PONG); + //payload_len = 1; + }else if(*code == WS_CONT) + { + //payload_len = 1; } + last_byte = buf[payload_len-1]; + //timer_ms = aos_get_tickcount(); + //bk_printf("receive_websocket_message 4 timer_ms=%d len = %d\n",timer_ms,payload_len); + error_count=0; + // websocket_tmp_b_len = payload_len; + // if(payload_len>sizeof(websocket_tmp_b)) + // { + // websocket_tmp_b_len=sizeof(websocket_tmp_b); + // } + // memcpy(websocket_tmp_b,buf,websocket_tmp_b_len); - bk_printf("send_handshake request:\r\n%s",request); - if (send(client->sockfd, request, strlen(request), 0) < 0) { - bk_printf("send handshake failed :sockfd:%d",client->sockfd); - - ret = -1; - } - bk_printf("send_handshake3"); - aic_free(request); - return ret; + + // frame_t_len = offset; + // if(frame_t_len>sizeof(frame_t)) + // { + // frame_t_len=sizeof(frame_t); + // } + // memcpy(frame_t,frame,frame_t_len); + + + return payload_len;//parse_frame(buf, bytes_received, buf,code); } -//http get 客户端 -int http_get_client(const char *url, const char *headers,const char *Content_Type,char* response,int response_len) { - //bk_printf("init_websocket_client 1\n"); - httpClient *client =NULL; +// 初始化 WebSocket 客户端 +WebSocketClient *init_websocket_client(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); @@ -880,10 +1039,10 @@ int http_get_client(const char *url, const char *headers,const char *Content_Typ bk_printf("Query: %s", parsed_url.query); } - client = aic_zalloc(sizeof(httpClient)) ; + client = aic_zalloc(sizeof(WebSocketClient)) ; if(!client) { - bk_printf("http_get_client alloc error"); + bk_printf("init_websocket_client alloc error"); goto client_ERROR; } struct addrinfo hints, *addr_list, *cur; @@ -893,7 +1052,7 @@ int http_get_client(const char *url, const char *headers,const char *Content_Typ if( getaddrinfo( parsed_url.host, parsed_url.port_str, &hints, &addr_list ) != 0 ) //if( getaddrinfo( "www.baidu.com", "80", &hints, &addr_list ) != 0 ) { - bk_printf("getaddrinfo error"); + bk_printf("init_websocket_client getaddrinfo error"); goto client_ERROR; } @@ -913,48 +1072,66 @@ int http_get_client(const char *url, const char *headers,const char *Content_Typ } close( client->sockfd ); + + + } + freeaddrinfo(addr_list); // 释放资源 + if(client->sockfd<0) + { + bk_printf("init_websocket_client connect error"); goto client_ERROR; - bk_printf("http_get_client connect error"); } - freeaddrinfo(addr_list); // 释放资源 - bk_printf("http_get_client connect ok,sockfd:%d",client->sockfd); + + bk_printf("init_websocket_client 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; +// } 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_http_get_handshake(client,headers,Content_Type) < 0) { //发送头 + if (send_handshake(client,headers) < 0) { //发送头 close(client->sockfd); goto client_ERROR; } - - ssize_t bytes_received = recv(client->sockfd, response, response_len-1, 0); + buffer=aic_zalloc(BUFFER_SIZE); + ssize_t bytes_received = recv(client->sockfd, buffer, BUFFER_SIZE - 1, 0); if (bytes_received > 0) { - response[bytes_received] = '\0'; - bk_printf("Received handshake response:\n%s\n", response); - }else - { - bk_printf("recv error bytes_received %d", recv); + buffer[bytes_received] = '\0'; + bk_printf("Received handshake response:\n%s\n", buffer); } - // if(strstr(buffer,"HTTP/1.1 101") == NULL) - // { + if(strstr(buffer,"HTTP/1.1 101") == NULL) + { - // goto client_ERROR; - // } - close(client->sockfd); + goto client_ERROR; + } + aic_free(buffer); free_url(parsed_url); - if(client) - aic_free(client); - return bytes_received; + return client; client_ERROR: - + if(buffer) + aic_free(buffer); if(client) aic_free(client); free_url(parsed_url); - return -1; + return NULL; } WebSocketClient *init_websocket_IOT_V3(const char *url, const char *headers) @@ -1076,238 +1253,288 @@ client_ERROR: return NULL; } -int receive_websocket_message_V3(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); - if(!client ) - return -1; - unsigned char frame[20]={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; +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); + // } - - while(1) + + + client = aic_zalloc(sizeof(WebSocketClient)) ; + if(!client) { - bytes_received = recv(client->sockfd, frame, 1, 0); + bk_printf("init_websocket_IOT alloc error"); + goto client_ERROR; + } - // bk_printf("receive_websocket_message:frame:0x%x,bytes_received:%d",frame[0],bytes_received); + 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 ) - if (bytes_received == 0) { - bk_printf("Connection closed by server.1\n\n\n\n"); - return 0; - } else if(bytes_received<0){ - bk_printf("receive message failed1\n\n\n\n"); - return -1; - } - if(frame[0]!= 0x80 && frame[0]!= 0x81 && frame[0]!= 0x82 && frame[0]!= 0x88 && frame[0]!= 0x89 && frame[0]!= 0x8A) + //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 ) { - error_count++; - bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x error_count =%d ERROR!!!!!!!!!",frame[0],frame[1],last_byte,error_count); + continue; + } - if(last_byte!= 0x80 && last_byte!= 0x81 && last_byte!= 0x82 && last_byte!= 0x88 && last_byte!= 0x89 && last_byte!= 0x8A) - { - bk_printf("receive_websocket_message:hend =0x%X,0x%X ,last_byte =0x%x ERROR222222222!!!!!!!!!",frame[0],frame[1],last_byte); - continue; - } - - }else + 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("websocket2 timer_ms=%d\n",timer_ms- aos_get_tickcount()); - // timer_ms = aos_get_tickcount(); - //bk_sleep(5); - bytes_received = recv(client->sockfd, frame+1, 1, 0); - if (bytes_received == 0) { - bk_printf("Connection closed by server.2\n\n\n\n"); - return 0; - } else if(bytes_received<0){ - bk_printf("receive message failed 2 \n\n\n\n"); - return -1; - } - //unsigned char fin = (frame[0] & 0x80) >> 7; - *code = frame[0] & 0x0F; - unsigned char mask = (frame[1] & 0x80) >> 7; - unsigned int payload_len = frame[1] & 0x7F; + bk_printf("init_websocket_client 4\n"); - size_t offset = 2; - if (payload_len == 126) { - unsigned short extended_payload_len; - bytes_received = 0; - while(1) - { - recv_len= recv(client->sockfd, frame+offset+bytes_received, 2-bytes_received, 0); - if (recv_len == 0) { - bk_printf("Connection closed by server 3.\n\n\n\n"); - return 0; - } else if(recv_len<0){ - bk_printf("receive message failed 3 \n\n\n\n"); - return -1; - } - bytes_received +=recv_len; - if(bytes_received>=2) - break; - - } - - // bytes_received_t = bytes_received; - memcpy(&extended_payload_len, frame + 2, 2); - payload_len = ntohs(extended_payload_len); - //bk_printf("receive_websocket_message:payload_len=0x%X",extended_payload_len); - offset += 2; - } else if (payload_len == 127) { - u64 extended_payload_len; - bytes_received = 0; - while(1) - { - recv_len= recv(client->sockfd, frame+offset+bytes_received, 8-bytes_received, 0); - if (recv_len == 0) { - bk_printf("Connection closed by server 4.1\n\n\n\n"); - return 0; - } else if(recv_len<0){ - bk_printf("receive message failed 4\n\n\n\n"); - return -1; - } - bytes_received +=recv_len; - if(bytes_received>=8) - break; - - } - memcpy(&extended_payload_len, frame + 2, 8); - payload_len = ntohll(extended_payload_len); - offset += 8; + freeaddrinfo(addr_list); // 释放资源 + if(client->sockfd<0) + { + bk_printf("init_websocket_IOT connect error"); + goto client_ERROR; } - if(payload_len>len) + + 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) { - bk_printf("receive_websocket_message:len =%d ERROR",payload_len); - recv(client->sockfd, buf, len, 0); - return -1; + soc_close(client->sockfd); + aic_free(client); } - // bk_printf("receive_websocket_message :"); - // for(int i = 0;isockfd, frame+offset, 4, 0); - memcpy(masking_key, frame + offset, 4); - offset += 4; - } + // close_websocket_client(client); + // aos_sleep(2); + // char *response =aic_zalloc(BUFFER_SIZE); + // //char *url = "http://dmp.raisound.com/sv/vad/auth/token?nonceStr=abc&sign=3470327e8eee163f00d5f54cc936d32595189b16d1e89c2b50155b80227c85d0×tamp=1750406765243&token=9542143a532adc10ec6ee530cd1824d53b"; + // char *url = "http://dmp.raisound.com/bz-mp-api/sv/vad/auth/token?nonceStr=abc&sign=3470327e8eee163f00d5f54cc936d32595189b16d1e89c2b50155b80227c85d0×tamp=1750406765243&token=9542143a532adc10ec6ee530cd1824d53b"; + // http_get_client(url,"bz-dt:9542143a532adc10ec6ee530cd1824d53b","application/x-www-form-urlencoded",response,BUFFER_SIZE); + // bk_printf("http GET data:%s",response); + // aic_free(response); + return 0; +} - - bytes_received = 0; - // payload_len-=1;//少读一个字节,防止粘包 - //bk_sleep(1); - while(1)//读主体内容 +/************************http*********************************** */ + +// WebSocket 客户端结构体 +typedef struct { + int sockfd; + char host[128]; + char path[128]; + char query[512]; + int port; +} httpClient; +int send_http_get_handshake(httpClient *client,const char *headers,const char *Content_Type) { + char key[WEBSOCKET_KEY_LEN]; + bk_printf("send_handshake1"); + generate_websocket_key(key); + char accept_key[64]; + int ret = 0; + generate_accept_key(key, accept_key); + bk_printf("send_handshake2"); + char *request =aic_zalloc(BUFFER_SIZE); + if(headers!= NULL) { - // 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); + snprintf(request, BUFFER_SIZE, + "GET /%s?%s HTTP/1.1\r\n" + "Host: %s:%d\r\n" + "Content-Type: %s\r\n" + "Connection: close\r\n" + "%s\r\n" + "\r\n", + client->path,client->query ,client->host,client->port,Content_Type,headers); + }else + { + snprintf(request, BUFFER_SIZE, + "GET /%s?%s HTTP/1.1\r\n" + "Host: %s:%d\r\n" + "Content-Type: %s\r\n" + "Connection: close\r\n" + "\r\n", + client->path,client->query , client->host,client->port,Content_Type); + } + + bk_printf("send_handshake request:\r\n%s",request); + if (send(client->sockfd, request, strlen(request), 0) < 0) { + bk_printf("send handshake failed :sockfd:%d",client->sockfd); + + ret = -1; + } + bk_printf("send_handshake3"); + aic_free(request); + return ret; +} +//http get 客户端 +int http_get_client(const char *url, const char *headers,const char *Content_Type,char* response,int response_len) { + //bk_printf("init_websocket_client 1\n"); - // bk_printf("recv_len %d %d",payload_len,recv_len); + httpClient *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); - if (recv_len == 0) { - bk_printf("Connection closed by server.5\n\n\n\n\n"); - return 0; - } else if(recv_len<0){ - bk_printf("receive message failed 5\n\n\n\n"); - return -1; + } + client = aic_zalloc(sizeof(httpClient)) ; + if(!client) + { + bk_printf("http_get_client 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 ) + //if( getaddrinfo( "www.baidu.com", "80", &hints, &addr_list ) != 0 ) + { + bk_printf("getaddrinfo error"); + goto client_ERROR; + } + + 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; } - bytes_received += recv_len; - - //bk_printf("receive_websocket_message:bytes_received=%d recv_len =%d",bytes_received,recv_len); - if(bytes_received >= payload_len) + if( connect( client->sockfd , cur->ai_addr, (int)cur->ai_addrlen ) == 0 ) { - //bk_printf("receive_websocket_message:payload_len=%d,bytes_received=%d",payload_len,bytes_received); + break; } - + + close( client->sockfd ); + goto client_ERROR; + bk_printf("http_get_client connect error"); } - //bk_sleep(1); - // bk_printf("websocket 3 timer_ms=%d\n",timer_ms- aos_get_tickcount()); - // timer_ms= aos_get_tickcount(); - // bk_sleep(1);//等最后一字节返回,防止粘包 - // recv(client->sockfd, buf+payload_len, 1, 0);//读最后一字节 - // payload_len+=1; - if(mask) - { - for (size_t i = 0; i < payload_len; ++i) { - buf[i] = buf[i] ^ masking_key[i % 4]; - } + freeaddrinfo(addr_list); // 释放资源 + bk_printf("http_get_client connect ok,sockfd:%d",client->sockfd); + + + + 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_http_get_handshake(client,headers,Content_Type) < 0) { //发送头 + close(client->sockfd); + goto client_ERROR; } - - // if (*code == WS_TEXT) { - // bk_printf("Received text message: %.*s\n", (int)payload_len, buf); - // } else if (*code == WS_BINARY) { - // bk_printf("Received binary message of length %zu\n", payload_len); - // } - // if(error_count>0) - // { - // bk_printf("WS ERROR!!!!!!last_byte:0x%x bytes_received_t_1:0x%x\r\n\n\n\n\n\n\n",last_byte,bytes_received_t_1); - // bk_print_hex(frame_t,frame_t_len); - // bk_print_hex(websocket_tmp_b,websocket_tmp_b_len); - // bk_printf("WS ERROR!!!!!!\r\n\n\n\n\n\n\n"); - // bk_print_hex(frame,offset); - // bk_print_hex(buf,payload_len); - - // } - // bytes_received_t_1 = bytes_received_t; - if (*code == WS_TEXT) - { - }else if(*code == WS_BINARY) - { - }else if(*code == WS_CLOSE) - { - payload_len = 0; - }else if(*code == WS_PONG) - { - //payload_len = 1; - }else if(*code == WS_PING) - { - send_websocket_message(client,buf,payload_len,WS_PONG); - //payload_len = 1; - }else if(*code == WS_CONT) + ssize_t bytes_received = recv(client->sockfd, response, response_len-1, 0); + if (bytes_received > 0) { + response[bytes_received] = '\0'; + bk_printf("Received handshake response:\n%s\n", response); + }else { - //payload_len = 1; + bk_printf("recv error bytes_received %d", recv); } - last_byte = buf[payload_len-1]; - //timer_ms = aos_get_tickcount(); - //bk_printf("receive_websocket_message 4 timer_ms=%d len = %d\n",timer_ms,payload_len); - error_count=0; - // websocket_tmp_b_len = payload_len; - // if(payload_len>sizeof(websocket_tmp_b)) - // { - // websocket_tmp_b_len=sizeof(websocket_tmp_b); - // } - // memcpy(websocket_tmp_b,buf,websocket_tmp_b_len); - - - // frame_t_len = offset; - // if(frame_t_len>sizeof(frame_t)) + // if(strstr(buffer,"HTTP/1.1 101") == NULL) // { - // frame_t_len=sizeof(frame_t); + + // goto client_ERROR; // } - // memcpy(frame_t,frame,frame_t_len); - + close(client->sockfd); + free_url(parsed_url); + if(client) + aic_free(client); + return bytes_received; +client_ERROR: - return payload_len;//parse_frame(buf, bytes_received, buf,code); + if(client) + aic_free(client); + free_url(parsed_url); + return -1; } 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 35e673e..f2d1b69 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,9 +24,9 @@ typedef struct { // 初始化 WebSocket 客户端 WebSocketClient *init_websocket_client(const char *url, const char *headers); -WebSocketClient *init_websocket_IOT_V3(const char *url, const char *headers); +WebSocketClient *init_websocket_IOT(const char *url, const char *headers); -int receive_websocket_message_V3(WebSocketClient *client, unsigned char *buf,int len,WebSocketMessageType *code); +WebSocketClient *init_websocket_IOT_V3(const char *url, const char *headers); // 关闭 WebSocket 客户端 void close_websocket_client(WebSocketClient *client) ; @@ -34,11 +34,16 @@ void close_websocket_client(WebSocketClient *client) ; // 接收 WebSocket 消息 int receive_websocket_message(WebSocketClient *client, unsigned char *buf,int len,WebSocketMessageType *code); +int receive_websocket_message_V3(WebSocketClient *client, unsigned char *buf,int len,WebSocketMessageType *code); + +//receive_websocket_message_V3 + 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 fe8a2da..bd35e28 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 @@ -24,11 +24,11 @@ #include "aic_http_client.h" #include "aic_fota.h" - - // #define DMPTEST - #define bk_malloc aic_malloc #define bk_free aic_free + + // #define DMPTEST + int get_dev_pass_MAX_len(void)//获取设备密码最大长度,注意要防止内存越界 { return sizeof(vc_cfg.dev_pass); @@ -329,12 +329,18 @@ char * get_token(void) //激活->登录的密钥 -const char *dev_pid = "cqxJZi5CNgzjS3mX21Zw";//PID -const char *dev_key = "uKLrChwrxA1LklgBc3VwawYjUkbT3BgX";//密钥 + const char *dev_pid = "cqxJZi5CNgzjS3mX21Zw";//PID + const char *dev_key = "uKLrChwrxA1LklgBc3VwawYjUkbT3BgX";//密钥 +//电信 +// const char *dev_pid = "DieBeXahZHLrjSnORbgN";//PID +// const char *dev_key = "FuXG4ptpGbzvKbdB4vpUypisbENgy2nB";//密钥 + #define HTTP_DEMO_CLIENT_NAME "HTTP Demo Client" #define HTTP_DEMO_URL "http://47.101.171.33/doc/download/10.txt" -#define HTTPS_DEMO_URL "https://47.101.171.33/doc/download/CK8K.txt" +#define HTTPS_DEMO_URL "https://ting.raisound.com/GKwRIUEM7KLlABfEQAQ1sw59.mp3" + +//https://ting.raisound.com/GKwRIUEM7KLlABfEQAQ1sw59.mp3 #define HTTP_DEMO_WAIT_TIME (10 * 1000) #define HTTP_DEMO_SLEEP_TIME (60 *1000) @@ -404,18 +410,21 @@ void aic_http_demotest() bk_printf("aic_http_demotest1213123 finish..."); - aic_http_client_delete(http_demo_handle); + aic_http_client_delete(http_demo_handle); return; } +int32_t play_audio_done_duplex(const char *buf, uint32_t buf_len, uint32_t sample_rate, uint32_t is_file); + +void resetClose_time(); void aic_https_demotest() { u32 status = 0; LONG lbytes = 0; u8 *buff; - u32 bytes = 1500; + u32 bytes = 1024*2; u32 total_length = 0; u32 length_to_be_copied = 0; u32 chunked_mode = 0; @@ -435,7 +444,7 @@ void aic_https_demotest() client_ptr->https_client->auth_type = 0; /*wait pdp active,only for demo test.*/ - aos_sleep(HTTP_DEMO_SLEEP_TIME); + // aos_sleep(HTTP_DEMO_SLEEP_TIME); status = aic_https_client_get_start(https_demo_handle, HTTPS_DEMO_URL, NULL, NULL, 443, HTTP_DEMO_WAIT_TIME); @@ -444,7 +453,9 @@ void aic_https_demotest() return; } - buff = aos_zalloc(bytes+2000); + buff = aos_zalloc(bytes); + + bk_printf("aic_https_demotest12132..."); do { lbytes = aic_https_client_response_data_get(https_demo_handle, buff , bytes, @@ -453,7 +464,14 @@ void aic_https_demotest() break; } else { - bk_printf("https get data %d ",lbytes); + bk_printf("https get data %d ",lbytes); + + play_audio_done_duplex(buff, bytes, 16000, 0); + + //audio_data(buff,lbytes,37); + resetClose_time(); + + aos_sleep(20); chunked_mode = aic_http_client_chunked_mode_check(https_demo_handle); length_to_be_copied = aic_https_client_total_receive_bytes_get(https_demo_handle); if (!chunked_mode && length_to_be_copied) { @@ -465,7 +483,7 @@ void aic_https_demotest() // aicx_error(0x0f0971, "s", "https get data is %s", buff); - bk_printf("download123 finish..."); + bk_printf("download123 finish %d...",total_length); aic_https_client_delete(https_demo_handle); @@ -473,6 +491,80 @@ void aic_https_demotest() } +void aic_https_demotest_status() +{ + u32 status = 0; + LONG lbytes = 0; + u8 *buff; + u32 bytes = 1024*2; + u32 total_length = 0; + u32 length_to_be_copied = 0; + u32 chunked_mode = 0; + AIC_HTTPS_CLIENT_INSTANCE *client_ptr; + + bk_printf("aic_https_demotest1213..."); + + https_demo_handle = aic_https_client_create(HTTP_DEMO_CLIENT_NAME); + if (https_demo_handle == 0) { + return; + } + + bk_printf("aic_https_demotest_status..."); + + /*for https test,set auth_type to 0, do not need certifica1*/ + client_ptr = (AIC_HTTPS_CLIENT_INSTANCE *)https_demo_handle; + client_ptr->https_client->auth_type = 0; + + /*wait pdp active,only for demo test.*/ + // aos_sleep(HTTP_DEMO_SLEEP_TIME); + + status = aic_https_client_get_start(https_demo_handle, "https://app-api.raisound.com/dev/device/info/simple/867686073051904", NULL, + NULL, 443, HTTP_DEMO_WAIT_TIME); + if (status != NX_SUCCESS) { + aic_https_client_delete(https_demo_handle); + return; + } + + buff = aos_zalloc(bytes); + + bk_printf("aic_https_demotest12132..."); + + do { + lbytes = aic_https_client_response_data_get(https_demo_handle, buff , bytes, + HTTP_DEMO_WAIT_TIME); + if (lbytes <= 0) { + break; + } else { + + bk_printf("https get data %d ",lbytes); + + // bk_printf("read_bytes :%d\n",read_bytes); + + // play_audio_done_duplex(buff, bytes, 16000, 0); + + bk_printf("IOT_update_bargein :%s",buff); + //audio_data(buff,lbytes,37); + // resetClose_time(); + + // aos_sleep(20); + chunked_mode = aic_http_client_chunked_mode_check(https_demo_handle); + length_to_be_copied = aic_https_client_total_receive_bytes_get(https_demo_handle); + if (!chunked_mode && length_to_be_copied) { + total_length += lbytes; + } + } + + } while (total_length < length_to_be_copied || chunked_mode); + + // aicx_error(0x0f0971, "s", "https get data is %s", buff); + + bk_printf("download123 finish %d...",total_length); + + aic_https_client_delete(https_demo_handle); + + return; +} + void aut_active(void)//设备激活 { int ret; @@ -518,7 +610,11 @@ void aut_active(void)//设备激活 bk_printf("dev_id:%s timestamp:%s ",deviceId,timestamp_str); + //118.145.159.222:30400 + + // http://dmp.raisound.com/bz-api/dev/auth/active + // https://dmp-dev.raisound.com/dmp-api/dev/auth/active #ifdef DMPTEST char *sign_url = "http://dmp-dev.raisound.com/dmp-api/dev/auth/active"; @@ -539,7 +635,7 @@ void aut_active(void)//设备激活 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; + http_client->login_info.server_port = 80;//80 aic_http_client_connect(aut_active_http_handle, 5000); // 创建JSON对象 @@ -674,7 +770,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}; @@ -797,7 +893,216 @@ aut_active_return: aic_http_client_delete(aut_active_http_handle); } - char ota_url[200] ={0}; + +void get_alarm_info13(void)//设备激活 +{ + int ret; + bk_printf("get_alarm_info13"); + + + AIC_HTTPS_CLIENT_INSTANCE *http_client; + char deviceId[30] = {0}; + //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}; + + 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); + + bk_get_iccid(dev_iccid); + + + // const char *signMethod ="MD5"; + + // alog_info("http demo start"); + //登陆数据 + + // bk_printf("dev_id:%s timestamp:%s ",deviceId,timestamp_str); + // char *sign_url = "http://dmp.raisound.com/bz-api/dev/iccid"; + + sprintf(sign_url,"http://wsiot-nocdn.raisound.com/ws-bus/alarm/system/list/%s?devToken=%s",deviceId,get_token()); + + bk_printf(sign_url); + + + // http://dmp.raisound.com/bz-api/dev/iccid + 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; ; + } + + // 添加字段 + cJSON_AddStringToObject(root, "devToken", get_token()); + // cJSON_AddStringToObject(root, "iccid", dev_iccid); + //cJSON_AddStringToObject(root, "sign", md5_buf); + //cJSON_AddStringToObject(root, "timeStamp", timestamp_str); + // cJSON_AddStringToObject(root, "signMethod", signMethod); + + // 生成JSON字符串 + json_string = cJSON_Print(root); + if (json_string == NULL) { + bk_printf("Failed to print JSON\n"); + //cJSON_Delete(root); + goto aut_active_return; + } + + // 输出JSON + // bk_printf("JSON output: %s\n", json_string); + + //sprintf(form_data_string,"deviceId=%s&iccid=%s",deviceId,dev_iccid); + + //bk_printf("form_data_string: %s\n", form_data_string); + + + aic_https_client_request_initialize(aut_active_http_handle,AIC_HTTPS_METHOD_GET, 0, + 0, NULL,NULL, 10000); + + // aic_https_client_request_header_add(aut_active_http_handle,"Content-Type",strlen("Content-Type"),"application/json",strlen("application/json"),10000);//添加头 + // bk_printf("aic_https_client_request_header_add1"); + + // aic_https_client_request_header_add(aut_active_http_handle,"bz-dt",strlen("bz-dt"),aut_token,strlen(aut_token),10000);//添加头 + // bk_printf("aic_https_client_request_header_add3"); + + + response = nx_web_http_client_request_send(http_client->https_client,10 * 1000); + + + http_client->https_client->nx_web_http_client_state = NX_WEB_HTTP_CLIENT_STATE_POST; + +// ret = aic_https_client_put_data(aut_active_http_handle, json_string, strlen(json_string), +// 1000); +// if (ret == 0) { +// bk_printf("数据发送成功\n"); + +// } else { +// // 处理错误 +// bk_printf("数据发送失败,错误码: %d\n", ret); +// goto aut_active_return; +// } + long read_bytes=0; + + long read_bytes_tmp = aic_https_client_response_data_get(aut_active_http_handle,aut_buf,5*1024,10000); + + while(read_bytes_tmp>0){ + + bk_printf("read_bytes :%d\n",read_bytes_tmp); + + read_bytes+=read_bytes_tmp; + + read_bytes_tmp = aic_https_client_response_data_get(aut_active_http_handle,aut_buf+read_bytes,5*1024,10000); + + } + + + if(read_bytes<=0) + goto aut_active_return; + + bk_printf("get_alarm_info13 :%s",aut_buf); + + cJSON* root123 = cJSON_Parse(aut_buf); + // parse_object(root123,aut_buf); + + // cJSON_Array *test=cJSON_GetObjectItem(root123,"data"); + + cJSON* tmp123=cJSON_GetObjectItem(root123,"msg"); + + bk_printf("get_alarm_info13 count :%s",cJSON_GetStringValue(tmp123)); + + cJSON* cjarray1=cJSON_GetObjectItemCaseSensitive(root123,"data"); + + int count_alarm=cJSON_GetArraySize(cjarray1); + + bk_printf("get_alarm_info13 count :%d",cJSON_GetArraySize(cjarray1)); + + vc_cfg.alarm_count=0; + + for(int i=0;i=0&&temp_bag<10){ - vc_cfg.VC_mode=temp_bag; + // if(temp_bag==1) + // vc_cfg.VC_mode=0; + // else + vc_cfg.VC_mode=temp_bag; } // vc_cfg.VC_mode= @@ -1274,12 +1592,13 @@ 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}; @@ -1371,15 +1690,15 @@ void ota_update_version(void)//OTA上报 goto aut_active_return; } - long read_bytes = aic_https_client_response_data_get(aut_active_http_handle,aut_buf,5*1024,10000); + //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); + // bk_printf("read_bytes :%d\n",read_bytes); - if(read_bytes<=0) - goto aut_active_return; + //if(read_bytes<=0) + // goto aut_active_return; - bk_printf("http put data update_iccid :%s",aut_buf); + bk_printf("ota_update_version_end"); aut_active_return: @@ -1451,9 +1770,12 @@ void aut_login(void)//登录 aes_cbc_pkcs5_encrypt(dev_pass, strlen(dev_pass), dev_key, enc_iv,encrypt_data,&encrypt_data_len);//加密 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\""; + // 118.145.159.222:30400 + + // http://dmp.raisound.com/bz-api/dev/auth/sign_in + + // https://dmp-dev.raisound.com/dmp-api/dev/auth/active #ifdef DMPTEST char *sign_url = "http://dmp-dev.raisound.com/dmp-api/dev/auth/sign_in"; // 登录地址 #else @@ -1461,6 +1783,7 @@ void aut_login(void)//登录 char *sign_url = "http://dmp.raisound.com/bz-api/dev/auth/sign_in"; // 登录地址 #endif + //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"); @@ -1659,4 +1982,6 @@ void dev_authentication_init(void) AOS_AUTO_START); } + + // AIC_BASIC_APP_P2_INIT_EXPORT(apifota_callback_init); \ No newline at end of file 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 af64ac4..024ca15 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,5 +24,4 @@ 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/app/AI_AMP_2_5/imsl_kws.h b/2.code/aicos/app/AI_AMP_2_5/imsl_kws.h index daa83b8..bbc185f 100644 --- a/2.code/aicos/app/AI_AMP_2_5/imsl_kws.h +++ b/2.code/aicos/app/AI_AMP_2_5/imsl_kws.h @@ -1,8 +1,8 @@ /* * Build Information - * Git Commit: 66c47c389123d4d655eb5b1d63b5918355f15ce7 + * Git Commit: e32fd299177c43fff4c09199e567ca68615da6d1 * Branch: master - * Build Date: 2025-12-17 16:12:17 + * Build Date: 2026-02-10 13:24:05 */ #ifndef _IMSL_KWS_DLL diff --git a/2.code/aicos/app/AI_AMP_2_5/imsl_vad.h b/2.code/aicos/app/AI_AMP_2_5/imsl_vad.h index 407f713..cc3167f 100644 --- a/2.code/aicos/app/AI_AMP_2_5/imsl_vad.h +++ b/2.code/aicos/app/AI_AMP_2_5/imsl_vad.h @@ -1,8 +1,8 @@ /* * Build Information - * Git Commit: 66c47c389123d4d655eb5b1d63b5918355f15ce7 + * Git Commit: e32fd299177c43fff4c09199e567ca68615da6d1 * Branch: master - * Build Date: 2025-12-17 16:12:18 + * Build Date: 2026-02-10 13:24:06 */ #ifndef _IMSL_VAD_DLL diff --git a/2.code/aicos/app/AI_AMP_2_5/libimslkws.a b/2.code/aicos/app/AI_AMP_2_5/libimslkws.a index fde1017..a76d866 100644 Binary files a/2.code/aicos/app/AI_AMP_2_5/libimslkws.a and b/2.code/aicos/app/AI_AMP_2_5/libimslkws.a differ diff --git a/2.code/aicos/app/AI_AMP_2_5/libimslnn.a b/2.code/aicos/app/AI_AMP_2_5/libimslnn.a index 9ce432c..9bd977a 100644 Binary files a/2.code/aicos/app/AI_AMP_2_5/libimslnn.a and b/2.code/aicos/app/AI_AMP_2_5/libimslnn.a differ diff --git a/2.code/aicos/app/AI_AMP_2_5/libimslvad.a b/2.code/aicos/app/AI_AMP_2_5/libimslvad.a index 2a72056..10e5f2a 100644 Binary files a/2.code/aicos/app/AI_AMP_2_5/libimslvad.a and b/2.code/aicos/app/AI_AMP_2_5/libimslvad.a differ