#include "cat1_ctl.h"
#include "voice_call_volte_sample_wrapper.h"
+#include "audio_capture_wrapper.h"
+#include <ui_service.h>
+#include <bleService.h>
+#include "cat1_ws.h"
+
#ifdef __cplusplus
extern "C" {
#include <stdint.h>
extern osStatus_t osDelay(uint32_t ticks);
-#define cat1_msleep(ms) osDelay(ms) /* osDelay(1) : 1ms */
+#define cat1_msleep(ms) LOS_Msleep(ms) /* osDelay(1) : 1ms */
+static osTimerId_t g_timer_time_sync = NULL;
void bk_sleep(uint32_t time)
{
void uapi_start_tts_single(void);
-uint8_t server_state=0;
+static uint8_t server_state=0;
+static uint8_t disconnect_server=0;
// free
// 定义一个结构体来存储解析后的URL部分
// 发送 WebSocket 消息
int send_websocket_message(WebSocketClient *client, const unsigned char *message, size_t message_len, WebSocketMessageType type) {
- unsigned char *frame=( unsigned char *)aic_zalloc(message_len + 50);
+
+ uint8_t* frame=( uint8_t *)aic_zalloc(message_len + 50);
int offset = 0;
int ret = -1;
if(!frame)
// 第二个字节:设置掩码位和负载长度
if (message_len < 126) {
+
frame[offset++] = 0x80 | message_len;
- } else if (message_len < 65536) {
+
+ }
+ else if (message_len < 65536)
+ {
frame[offset++] = 0x80 | 126;
uint16_t len = htons((uint16_t)message_len);
memcpy(frame + offset, &len, 2);
offset += message_len;
// 发送帧
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"offset %d\r\n",offset);
+
if (send(client->sockfd, frame, offset, 0) < 0) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send message failed\r\n");
ret = -1;
}
ret = 0;
send_websocket_message_ERROR:
- free(frame);
bk_sleep(message_len/1000+1);
+ free(frame);
+ // bk_sleep(message_len/1000+1);
return ret;
}
if (bytes_received <= 0)
{
if (bytes_received == 0) {
- printf("Connection closed by server.\n");
+ printf("Connection lwip_closed by server.\n");
} else {
printf("receive message failed");
}
bytes_received = recv(client->sockfd, frame+1, 1, 0);
if (bytes_received <= 0) {
if (bytes_received == 0) {
- printf("Connection closed by server.\n");
+ printf("Connection lwip_closed by server.\n");
} else {
printf("receive message failed");
}
if(payload_len>len)
{
printf("receive_websocket_message:len =%d ERROR",payload_len);
+
+ // if(*code == WS_BINARY)
+ // {
+
+ // }
+ // fopen
recv(client->sockfd, buf, len, 0);
return -1;
}
client->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (client->sockfd < 0) {
- printf("socket creation failed");
+ printf("socket creation failed %d \r\n",client->sockfd);
goto client_ERROR;
}
if (connect(client->sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("init_websocket_client connection failed\r\n");
- close(client->sockfd);
+ lwip_close(client->sockfd);
goto client_ERROR;
}
printf("socket creation succ\r\n");
client->port = parsed_url.port;
if (send_handshake(client,headers) < 0) { //发送头
printf("socket send_handshake client_ERROR");
- close(client->sockfd);
+ lwip_close(client->sockfd);
goto client_ERROR;
}
}
// 关闭 WebSocket 客户端
-void close_websocket_client(WebSocketClient *client) {
+void lwip_close_websocket_client(WebSocketClient *client) {
if(client)
{
- printf("close_websocket_client ");
- // close(client->sockfd);
+ printf("lwip_close_websocket_client ");
+ lwip_close(client->sockfd);
free(client);
}
if(server_state==0b00000111)
{
// uapi_start_asr();
+ // uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
}
// send_ai_txt(1,"你是谁");
set_ai_chat_service_init_flag(1);
WebSocketMessageType type;
- while (1)
+ while (disconnect_server==0)
{
memset_s(recv_buf,1024,0,1024);
int len= receive_websocket_message(chat_client,(unsigned char *)recv_buf,1024,&type);
printf("bk_websocket_demo %s",recv_buf);
}
-
- close_websocket_client(chat_client);
+ set_ai_chat_service_init_flag(0);
+ lwip_close_websocket_client(chat_client);
chat_client=NULL;
is_bk_websocket_chat=0;
return ;
#include "common_def.h"
#include "los_printf.h"
-#define CAT1_SERVICE_TASK_SIZE (3072)
+#define CAT1_SERVICE_TASK_SIZE (2048)
#define CAT1_SERVICE_TASK_PRIO 5
taskcfg.create_queue_handle = NULL;
taskcfg.attr.name="chat_server_init";
taskcfg.attr.priority = TASK_PRIORITY_APP;
- taskcfg.attr.stack_size = CAT1_SERVICE_TASK_SIZE;
+ taskcfg.attr.stack_size = 0x1000;
taskcfg.func = bk_websocket_demo;
taskcfg.use_mem = USE_EXTEND_MEM;
int32_t uapi_tts_paly_init(void);
int32_t uapi_tts_paly_stop(void);
+
+
//查询是否可以对话
void uapi_start_asr(void)
{
+// bool state = true;
+// SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+
char *jsonString = "{\"header\":{\"event\":\"StartTranscription\",\"trace_id\": \
- \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
+ \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
else
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n");
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n");
+ // lwip_close_websocket_client(asr_client);
+ // asr_client=NULL;
+ // disconnect_server=1;
+ }
// uapi_tts_paly_stop();
}
+void uapi_start_asr_task(void)
+{
+
+ char *jsonString = "{\"header\":{\"event\":\"StartTranscription\",\"trace_id\": \
+ \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\"payload\":{\"format\":\"MP3\",\"sample_rate\":16000}}";
+
+ if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+ else
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n");
+ // lwip_close_websocket_client(asr_client);
+ // asr_client=NULL;
+ // disconnect_server=1;
+ }
+}
+
+
+
//停止对话
void uapi_stop_asr(void)
{
if (send_websocket_message(asr_client,( const unsigned char *)jsonString,strlen(jsonString),WS_TEXT)==0)
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+ else
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message erro\n");
+ // lwip_close_websocket_client(asr_client);
+ // asr_client=NULL;
+
+ // disconnect_server=1;
+ }
}
void send_ai_bin(char *buf,uint32_t len)
{
- // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_ai_bin 0\n");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_ai_bin %d\n",len);
if (send_websocket_message(asr_client,( const unsigned char *)buf,len,WS_BINARY)==0)
{
- // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
- bk_sleep(10);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
+ bk_sleep(300);
}
else
{
void record_test(void)
{
char buf[1024]={0};
+ bk_sleep(1000);
- FILE *fp=fopen("/user/aiRecord.sbc","rb+");
+ FILE *fp=fopen("/user/aiRecord.bin","rb+");
int read_size;
-
+ int read_size_num=0;
uint32_t pack=1024;
if(fp==NULL)
if(read_size!=0)
{
send_ai_bin(buf,read_size);
- bk_sleep(10);
+ bk_sleep(100);
+ read_size_num+=read_size;
+
}
} while (read_size==1024);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"record_test %d\n",read_size_num);
fclose(fp);
}
-#include "audio_capture_wrapper.h"
-#include <ui_service.h>
-#include <bleService.h>
// {"status":20000000,"msg":"success","task_id":"80228c9f62821ffe3f867e1a5042d887","event":"TranscriptionStarted","result":null}
// {"status":20000000,
// "event":"SentenceEnd","result":{"result":"你好你好你好你好。",
// "is_end":true,"sentence_id":"fcb889fe74a0920efe14939de2fd2b1b",
// "start_time":760,"end_time":2280}}
+void asr_record_test(void)
+{
+ record_test();
+ bk_sleep(500);
+ uapi_stop_asr();
+}
+
+
char result_str_asr[256]={0};
+uint8_t asr_is_start=0;
+
bool aer_result_analysis(char *buf)
{
cJSON* root = cJSON_Parse((const char *)buf);
memcpy_s(result_str_asr,256,result_str,strlen(result_str));
SendMsgToUIService(UI_AI_JUMP_TO_RESULT_VIEW, 0, nullptr, 0);//先跳转到结果界面
ble_set_ai_Question_str(result_str_asr);
+ stop_AudioCaptureProcess_AI();
+ // uapi_stop_asr();
send_ai_txt(0,result_str_asr);
// uapi_stop_asr();
//查询结果
// streamingRecordAndSend_AI(0);
// startAudioCaptureTask();
- bool state = true;
- SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+ if(asr_is_start==0)
+ {
+ asr_is_start=1;
+
+ bool state = true;
+ SendMsgToUIService(UI_AI_JUMP_TO_LISTENING_VIEW, 0, &state, sizeof(bool));
+
+ // do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_AI_SEND_TEST, nullptr, 0);
+
+ }
+ else
+ {
+ // uapi_stop_asr();
+ }
+
+ }
+ if(strcmp(event_str,"TranscriptionCompleted")==0) //可以对话了,切换到聆听界面
+ {
+ //查询结果
+ // streamingRecordAndSend_AI(0);
+ // startAudioCaptureTask();
+ {
+ asr_is_start=0;
+ }
+
}
if(strcmp(event_str,"Failed")==0)
{
stop_AudioCaptureProcess_AI();
+ asr_is_start=0;
+ // uapi_stop_asr();
+ return false;
}
cJSON_Delete(root);
}
}
if (asr_client== NULL){
- return;
+
+ is_bk_websocket_asr=0;
+ return;
}
uapi_cat1_keep_wakeup(1);
if(server_state==0b00000111)
{
// uapi_start_asr();
+ // uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
}
asr_recv_buf=(char *)malloc(0x400);
-
+ set_ai_asr_service_init_flag(1);
// uapi_start_asr();
// SendMsgToUIService(UI_4G_AI_INIT_STAUS, 0,&state, sizeof(bool));
- set_ai_asr_service_init_flag(1);
- while (1)
+
+ while (disconnect_server==0)
{
memset_s(asr_recv_buf,1024,0,1024);
int len= receive_websocket_message(asr_client,(unsigned char *)asr_recv_buf,1024,&type);
// if(type==WS_CONT ||
if( type==WS_CLOSE || len==-1)
{
- printf("WS_CLOSE bk_websocket_asr\r\n");
+ printf("WS_lwip_close bk_websocket_asr\r\n");
break;
}
printf(" bk_websocket_asr %d\r\n",type);
printf("bk_websocket_demo %s %d",asr_recv_buf,type);
- aer_result_analysis(asr_recv_buf);
+ if(false== aer_result_analysis(asr_recv_buf))
+ {
+ break;
+ }
//
}
-
+ set_ai_asr_service_init_flag(0);
free(asr_recv_buf);
- close_websocket_client(asr_client);
+ lwip_close_websocket_client(asr_client);
asr_client=NULL;
// return;
}
void uapi_start_tts_single_txt(char *buf)
{
- char *jsonString = (char *)malloc(512);
+ char *jsonString = (char *)malloc(strlen(buf)+300);// [512] ={0};
- sprintf(jsonString, " { \"header\": { \
- \"event\": \"LongTextStream\",\
- \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\" \
- }, \
- \"payload\": { \
- \"voice\":\"xiaoyun\", \
- \"format\":\"mp3\", \
- \"sample_rate\":16000, \
- \"volume\":50, \
- \"speech_rate\":0, \
- \"text\":\"%s\" \
+ sprintf(jsonString, " { \"header\": { \
+ \"event\": \"LongTextStream\", \
+ \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\"\
+ },\
+ \"payload\": {\
+ \"voice\":\"xiaoyun\",\
+ \"format\":\"pcm\",\
+ \"sample_rate\":16000,\
+ \"volume\":50,\
+ \"speech_rate\":0,\
+ \"text\":\"%s\"\
} } ",buf);
printf("uapi_start_tts : %s\r\n",jsonString);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
free(jsonString);
- jsonString = NULL;
-}
+ }
// char *tts_txt_str=
}
+// send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf,len,200);
+
+void tts_cache_file(uint8_t *data, uint32_t len)
+{
+ FILE *fp= fopen("/user/tts.bin","ab+");
+ if(fp)
+ {
+ fwrite(data,1,len,fp);
+ fclose(fp);
+ }
+}
+
+
+void tts_paly()
+{
+ char buf[1024]={0};
+
+ FILE *fp=fopen("/user/tts.bin","rb+");
+ int read_size;
+
+ uint32_t pack=1024;
+
+ if(fp==NULL)
+ {
+ return;
+ }
+ do
+ {
+ memset_s(buf,1024,0,1024);
+ read_size=fread(buf,1,1024,fp);
+
+ if(read_size!=0)
+ {
+ AIStreamWrite((uint8_t *)buf,read_size);
+ bk_sleep(10);
+ }
+
+ } while (read_size==1024);
+ fclose(fp);
+ fp= fopen("/user/tts.bin","wb+");
+ if(fp)
+ {
+ fclose(fp);
+ }
+}
uint8_t is_bk_websocket_tts=0;
+uint32_t bk_websocket_tts_tick=0;
void bk_websocket_tts(void *data) {
if(server_state==0b00000111)
{
// uapi_start_asr();
+ // uapi_start_tts_single_txt("2025英超夏季转会窗分阶段开启第一阶段2025年6月1日至2025年6月11日凌晨2:00北京时间第二阶段常规夏窗2025年6月16日至2025年9月2日凌晨2:00北京时间本贴将持续更新2025年夏窗以曼联转会相关为主的新闻和消息。");
}
+// uapi_start_tts_single();
char* tts_recv_buf=(char *)malloc(1024*4);
- while (1)
+ set_ai_tts_server_init_flag(1);
+
+ while (disconnect_server==0)
{
// if(type==WS_CONT ||
if( type==WS_CLOSE || len==-1)//|| type==WS_CONT )
{
- printf("WS_CLOSE bk_websocket_tts\r\n");
+ printf("WS_lwip_close bk_websocket_tts\r\n");
break;
}
- printf("bk_websocket_tts bk_websocket_tts %d\r\n",type);
+ // printf("bk_websocket_tts bk_websocket_tts %d\r\n",type);
if(type==WS_BINARY)
{
+ if(bk_websocket_tts_tick==0)
+ {
+ bk_websocket_tts_tick = (uint32_t)osKernelGetTickCount();
+ }
+ // tts_cache_file((uint8_t *)tts_recv_buf,len);——
// AIStreamWrite((uint8_t *)tts_recv_buf,len);
send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf,len,200);
+
+ //
+ bk_sleep(1);
+ printf("bk_websocket_demo %d\n",len);
+ }
+ else
+ {
+ printf("bk_websocket_demo %s %d %d\n",tts_recv_buf,bk_websocket_tts_tick,(uint32_t)osKernelGetTickCount());
+ bk_websocket_tts_tick=0;
+ // tts_paly();
}
// aer_result_analysis(asr_recv_buf);
- printf("bk_websocket_demo %s %d",tts_recv_buf,type);
+
}
- close_websocket_client(tts_client);
+ set_ai_tts_server_init_flag(0);
+
+ lwip_close_websocket_client(tts_client);
tts_client=NULL;
// return;
}
app_task_definition_t taskcfg={0};
taskcfg.create_queue_handle = NULL;
taskcfg.attr.name="tts_server_init";
- taskcfg.attr.priority = TASK_PRIORITY_APP;
+ taskcfg.attr.priority = (osPriority_t)35;
taskcfg.attr.stack_size = 0x1000;
taskcfg.func = bk_websocket_tts;
taskcfg.use_mem = USE_EXTEND_MEM;
}
+static void time_sync_func(void)
+{
+
+}
+
+
bool init_ai_task(void)
{
if(is_init_ai)
{
+ disconnect_server=0;
+
+ g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL);
+ if (g_timer_time_sync == NULL)
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"g_timer_time_sync create failed\n");
+
+
uapi_tts_paly_init();
asr_server_init();
chat_server_init();
}
return true;
+}
-
+
+
+bool dinit_ai_task(void)
+{
+ if(is_init_ai)
+ {
+ disconnect_server=0;
+
+ if(g_timer_time_sync==NULL)
+ {
+ g_timer_time_sync = osTimerNew((osTimerFunc_t)time_sync_func, osTimerPeriodic, NULL, NULL);
+ }
+ if (g_timer_time_sync == NULL)
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"g_timer_time_sync create failed\n");
+
+
+ uapi_tts_paly_init();
+ asr_server_init();
+ chat_server_init();
+ tts_server_init();
+ // is_init_ai=0;
+ }
+
+ return true;
}