Files

256 lines
6.0 KiB
C

/*================================================================
Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
Quectel Wireless Solutions Proprietary and Confidential.
=================================================================*/
#include <stdarg.h>
#include "ql_type.h"
#include "ql_data_call.h"
#include "ql_rtos.h"
#include "ql_application.h"
#include <stdio.h>
#include "sockets.h"
#include "ql_uart.h"
#include "ql_func.h"
#include "netdb.h"
#include "ql_sim.h"
#include "ql_dev.h"
#include "ql_wtd.h"
#include "ql_fs.h"
#include "ql_fota.h"
#include "ql_sim.h"
#include "ql_ping.h"
#include "ql_nw.h"
#include "ql_atcmd.h"
#include "app_inc.h"
#include "ql_uart.h"
#include "ql_application.h"
#include "ql_log.h"
#include "hal_api.h"
#include "nfc.h"
#define APP_ENABLE_SLEEP
static int g_app_lpm_fd = -1;
static int g_app_lpm_flag = 1;//唤醒状态为1 休眠状态为0
static unsigned char app_operation = 0;
extern int fota_start;
int fisrt_net_start = 1;
// 安全修复:添加互斥锁保护全局共享变量
static ql_mutex_t g_app_op_mutex = NULL;
#define APP_OP_LOCK() do{ if(g_app_op_mutex) ql_rtos_mutex_lock(g_app_op_mutex, QL_WAIT_FOREVER); }while(0)
#define APP_OP_UNLOCK() do{ if(g_app_op_mutex) ql_rtos_mutex_unlock(g_app_op_mutex); }while(0)
void app_set_app_operation(unsigned char value)
{
// 安全修复:使用互斥锁保护全局变量
APP_OP_LOCK();
app_operation = value;
APP_OP_UNLOCK();
//app_check_connect_sem_release();
rws_socket_work_sem_release();
app_vad_check_sem_release();
#ifdef APP_ENABLE_SLEEP
APP_OP_LOCK();
if(g_app_lpm_flag==0){
g_app_lpm_flag = 1;
APP_OP_UNLOCK();
ql_es8311_wake();
printf("app_set_app_operation wake up\n");
ql_lpm_wakelock_lock(g_app_lpm_fd);
}else{
APP_OP_UNLOCK();
}
#endif
}
int app_get_app_sleep_flag(void)
{
return g_app_lpm_flag;
}
void quec_usbcdc_callback(QL_UART_PORT_NUMBER_E port, void *para)
{
int read_len = 0;
char r_data[1024] = {0};
read_len = ql_uart_read(port, r_data, sizeof(r_data));
printf("uart[%d] callback read data, data:%s, read_len:%d \n", port, r_data, read_len);
}
void quec_usbcdc_write(unsigned char *data, int len)
{
ql_uart_write(QL_USB_CDC_PORT, data, len);
}
static void quec_usbcdc_init(void)
{
ql_uart_config_t dcb;
ql_uart_open(QL_USB_CDC_PORT, QL_UART_BAUD_115200, QL_FC_NONE);
dcb.baudrate = QL_UART_BAUD_115200;
dcb.data_bit = 8;
dcb.stop_bit = 1;
dcb.parity_bit = 0;
dcb.flow_ctrl = 0;
ql_uart_set_dcbconfig(QL_USB_CDC_PORT, &dcb);
ql_uart_register_cb(QL_USB_CDC_PORT, quec_usbcdc_callback); //use callback to read uart data
}
#define AT_CMD_SLEPP_SUFFIX "\r\n"
void ql_atcmd_sleep(void)
{
int ret = 0,i = 0;
//char test_at[][30] = {"AT+MEDCR=0,34,3\r\n"};
char at_cmd_str[32]={0};
char resp[32]={0};
snprintf(at_cmd_str,sizeof(at_cmd_str)-1,"AT+MEDCR=0,34,5%s",AT_CMD_SLEPP_SUFFIX);
ret = ql_atcmd_send_sync(at_cmd_str,resp,sizeof(resp),NULL,10);
if(ret==QL_AT_RESP_OK)
{
printf("ql_atcmd_sleep ok\r\n");
}
ql_autosleep_enable(1);
}
static void custom_app_task(void * argv)
{
char tempbuf[256]={0};
QL_NW_SIGNAL_STRENGTH_INFO_T rsrp_t;
int csq_value = 0;
int sim_status = 0;
uint8_t flag = 0;
u32 app_start_time_record = 0;
// 安全修复:初始化互斥锁
ql_rtos_mutex_create(&g_app_op_mutex);
hal_led_init();
ql_autosleep_enable(0);
ql_wtd_timeoutperiod_set(4);//feed dog 16s period
ql_wtd_faultwake_enable(1);
ql_wtd_enable(1);
hal_os_get_build_time(tempbuf);
printf("BUILD TIME:%s\r\n",tempbuf);
//app_uartsh_init();
// quec_usbcdc_init();
//ql_rtos_task_sleep_s(15);
//app_gnss_init();
app_audio_init();
rws_socket_work_sem_init();
app_aofei_key_init();
app_ws_talkingq_init();
app_net_mgt_init(); //根据网络状态,后续就调用相关ws和http
app_mqtt_init();
DP1312EA_Init();
ql_adc_init();
#ifdef APP_ENABLE_SLEEP
g_app_lpm_fd = ql_lpm_wakelock_create("app_lpm_lock", sizeof("app_lpm_lock"));
if (g_app_lpm_fd <= 0) {
printf("ql_lpm_wakelock_create, lpm_fd = %d\n", g_app_lpm_fd);
}
ql_lpm_wakelock_lock(g_app_lpm_fd);
ql_atcmd_sleep();
app_start_time_record = hal_os_up_time_ms();
#endif
app_sleep_init();
g_app_lpm_flag = 1;
while(1)
{
#ifdef APP_ENABLE_SLEEP
APP_OP_LOCK();
unsigned char current_app_op = app_operation;
int current_lpm_flag = g_app_lpm_flag;
APP_OP_UNLOCK();
if(current_app_op==1){//每次有新操作 重新计时进入休眠
APP_OP_LOCK();
app_operation = 0;
APP_OP_UNLOCK();
app_start_time_record = hal_os_up_time_ms();//1个tick代表5ms
if(current_lpm_flag==0){
g_app_lpm_flag = 1;
//if(get_xiaoice_connected_status()==2){
// hal_led_b_on();//表示处于唤醒状态
//}
ql_es8311_wake();
printf("custom_app_task wake up\n");
ql_lpm_wakelock_lock(g_app_lpm_fd);
}
}
if(hal_os_up_time_ms() - app_start_time_record > 30000){
if(get_gnss_mode()==GNSS_MODE_FAST) goto jump_not_sleep;
if(fota_start==1 || get_gnss_open_status()==1) goto jump_not_sleep;//not sleep
if(xiaoice_get_tts_play_flag()==1) goto jump_not_sleep;//not sleep
if(xiaoice_get_tts_play_flag()==2&&xiaoice_get_play_counter()>0) goto jump_not_sleep;//not sleep
//if(ql_es8311_status_check()==0){
if(g_app_lpm_flag==1){
g_app_lpm_flag = 0;
//app_play_tips_file(10);
//ql_rtos_task_sleep_s(5);
// talkingq_ws_disconnect();
hal_led_b_off();
app_codec_pa_close();
ql_es8311_sleep();
printf("custom_app_task sleep\n");
ql_lpm_wakelock_unlock(g_app_lpm_fd);
}
//}
jump_not_sleep:
app_start_time_record = hal_os_up_time_ms();
}
#endif
battery_check();
// printf("custom_app_task running\n");
ql_rtos_task_sleep_s(5);
ql_wtd_feed();
}
}
application_init(custom_app_task, "custom_app", 10, 0);