mcu_hi3321_watch/tjd/task/task_ble.c
2025-05-26 20:15:20 +08:00

337 lines
9.1 KiB
C

/*----------------------------------------------------------------------------
* Copyright (c) Fenda Technologies Co., Ltd. 2021. All rights reserved.
*
* Description: task_ble.c
*
* Author: saimen
*
* Create: 2022-08-08
*--------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "sys_config.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "portmacro.h"
#include "portable.h"
#include "semphr.h"
#include "am_mcu_apollo.h"
#include "am_util_debug.h"
#include "dm_api.h"
#include "app_api.h"
#include "amotas_api.h"
#include "amota_api.h"
#include "task_radio.h"
#include "sys_typedef.h"
#include "task_ble.h"
#include "ble_data_transmission.h"
#include "ble_management_server.h"
#include "ble_ota_execute.h"
//#include "user_music_server.h"
//#include "user_debug.h"
//#include "rtc_api.h"
//#include "sys_restart.h"
//#include "ui_task.h"
//#include "message_notify.h"
//#include "file_trans.h"
#include "ble_weather_server.h"
//#include "weather_info.h"
//#include "ble_ota_execute.h"
//#include "user_workout.h"
//#include "external_memory_manage.h"
//#include "sql_setting.h"
//#include "ble_daily_health_data_server.h"
//#include "ble_sleep_server.h"
//#include "ble_stress_server.h"
//#include "ble_blood_oxygen_server.h"
#include "ble_music_server.h"
#include "ble_incoming_server.h"
//#include "device_setup_server.h"
#include "sys_restart.h"
#include "sql_message.h"
#define ENABLE_STATIC_PRINT false
extern uint32_t am_util_stdio_printf(const char *pcFmt, ...);
#define static_print_remind(...) am_util_stdio_printf(__VA_ARGS__)
#if ENABLE_STATIC_PRINT
#define static_print_info(...) am_util_stdio_printf(__VA_ARGS__)
#else
#define static_print_info(...)
#endif
#define BLE_TASK_MSG_TIMEOUT 1000
/* Define the queue parameters. */
#define BLE_TASK_QUEUE_LENGTH 128 //256
#define BLE_TASK_QUEUE_ITEM_SIZE sizeof( TaskMessage_t )
static TaskHandle_t xBleTask = NULL;
static QueueHandle_t xMQ_ble = NULL;
extern void task_radio_suspend(void);
extern void user_analysis_rev_data(void);
extern void user_app_ota_timeout_creat_time(void);
extern void OtaCreatTimeoutTimer(void);
extern void BleDiscAncsCreatTimer(void);
void BleSleepMode(void)
{
HciDrvRadioShutdown();
task_radio_suspend();
vTaskSuspend(xBleTask);
}
void BLE_Camera_Control(uint32_t CameraCmd)
{
uint16_t button;
/* Send play command */
switch(CameraCmd)
{
case CAMERA_OPEN:
/*
button = REMOTE_CAMERA_AUTO_FOCUS;
hidAppRemoteReportEvent(button);
button = 0;
hidAppRemoteReportEvent(button);
*/
break;
case CAMERA_PHOTO:
button = REMOTE_VOLUME_DOWN;
hidAppRemoteReportEvent(button);
button = 0;
hidAppRemoteReportEvent(button);
break;
default:
return;
}
}
void task_ble_buffer_notify(uint32_t msgID, uint32_t data, void* buf, uint32_t buf_len)
{
BaseType_t HigherPriorityTaskWoken = pdFALSE;
TaskMessage_t msg = {0};
if ( !(xMQ_ble && xBleTask) )
{
return;
}
msg.uMessageID = msgID;
msg.uData = data;
msg.buf_len = buf_len;
#ifdef BLE_USE_PSRAM
msg.buffer = (uint8_t *)PSRAM_MALLOC(buf_len);
if(msg.buffer == NULL)
{
return;
}
memcpy(msg.buffer, buf, buf_len);
#else
msg.buffer = (uint8_t *)buf;
#endif
if ( __get_IPSR() != 0 )
{
xQueueSendToBackFromISR(xMQ_ble, &msg, &HigherPriorityTaskWoken);
portYIELD_FROM_ISR(HigherPriorityTaskWoken);
}
else
{
xQueueSendToBack(xMQ_ble, &msg, pdMS_TO_TICKS(1000));
}
}
void task_ble_notify(uint32_t msgID, uint32_t data)
{
BaseType_t HigherPriorityTaskWoken = pdFALSE;
TaskMessage_t msg = {0};
if ( !(xMQ_ble && xBleTask) )
{
return;
}
msg.uMessageID = msgID;
msg.uData = data;
if ( __get_IPSR() != 0 )
{
xQueueSendToBackFromISR(xMQ_ble, &msg, &HigherPriorityTaskWoken);
portYIELD_FROM_ISR(HigherPriorityTaskWoken);
}
else
{
xQueueSendToBack(xMQ_ble, &msg, pdMS_TO_TICKS(1000));
}
}
void task_ble_entry(void *pvParameters)
{
static uint32_t ble_task_count = 0;
TaskMessage_t Msg = {0};
static_print_remind("task_ble_entry \r\n");
// OtaCreatTimeoutTimer();
// BleDiscAncsCreatTimer();
while(1)
{
ble_task_count++;
if( xQueueReceive( xMQ_ble, &Msg, portMAX_DELAY ) != pdPASS )
{
continue;
}
ble_task_count++;
//user_poll_send_data();
switch(Msg.uMessageID)
{
case BLE_DATA_RCV_COMPLETE_MSG:
static_print_info("*********ble_notic_receive_complete_msg******************\n");
BLE_ParseReceivedFrame(Msg.buffer, Msg.buf_len);
break;
case BLE_PAIRING_OPER_MSG:
//vTaskDelay(pdMS_TO_TICKS(1000));
UserPairOperate(Msg.uData);
break;
case BLE_DISCONNECTED_MSG:
ble_printf("\r\n>>> BLE_DISCONNECTED_MSG BLE_DISCONNECTED_MSG <<< \r\n");
OtaStop();
FileTransDisconn();
break;
case BLE_POWER_UPLOAD_MSG:
BLE_DeviceElectricQuantityUpload();
break;
case BLE_MESSAGE_LIST_STORAGE_MSG:
// SaveMessageList();
sql_message_save_message_list();
break;
case BLE_SAVE_TRANS_FILE_INFO:
file_trans_break(true);
break;
case BLE_SAVE_WEATHER_MSG:
// SaveWeatherInfo();
if (Msg.uData & 0xff) //Msg.uData == (ForecastID << 8 | ErrCnt)
{
// log_api_record_error(rtc_api_get_utc_timestamp(), EVT_APP_ERROR, INFO1_WEATHER_ERR, (Msg.uData >> 8) & 0xff, Msg.uData & 0xff, NULL);
}
break;
case BLE_SAVE_SETTINGS_MSG:
// SaveSysSettings();
break;
case BLE_HEALTH_DATA_REPORT_MSG:
// ble_health_data_report(Msg.uData);
break;
case BLE_SLEEP_DATA_REPORT_MSG:
// ble_sleep_measure_report();
break;
case BLE_STRESS_MEASURE_REPORT_MSG:
ble_stress_measure_report(Msg.buffer);
break;
case BLE_SPO2_MEASURE_REPORT_MSG:
// ble_spo2_measure_report(Msg.buffer);
break;
case BLE_WORKOUT_EXECUTE_STATUS_MSG:
if(Msg.buffer != NULL)
{
// DeviceNotifyAPPWorkoutExecuteStatus(Msg.buffer);
}
break;
case BLE_WORKOUT_REALTIME_DATA_MSG:
if(Msg.buffer != NULL)
{
// DeviceNotifyAPPRealtimeWorkoutInfo(Msg.buffer);
}
break;
case BLE_WORKOUT_REMIND_MSG:
if(Msg.buffer != NULL)
{
// DeviceNotifyAPPWorkoutRemind(Msg.buffer);
}
break;
case BLE_INCOMING_HANDLE_MSG:
Ble_HandleIncomingUpload(Msg.uData);
break;
case BLE_WEATHER_UPDATE_MSG:
static_print_info("ble rcv BLE_MSG_WEATHER_UPDATE\r\n");
BLE_DeviceNotifyAppUpdateweather();
break;
case BLE_QUERY_MUSIC_INFO_MSG:
BLE_DeviceRequestMusicPlayStatusUpload();
break;
case BLE_MUSIC_CONTRL_CMD_UPLOAG_MSG:
BLE_DeviceMusicContrlCmdUpload(Msg.uData);
break;
case BLE_DISTURB_CHANGE: //4.2.36
BLE_reportDisturbStatus();
break;
case BLE_RESET_AFTER_OTA_MSG:
static_print_info("!!!!!ble_reset_after_ota_msg!!!!!\n");
vTaskDelay(pdMS_TO_TICKS(2000));
system_restart();
break;
case BLE_REMOTE_CAMERA_MSG:
static_print_info("!!!!!ble_remote_camera_msg!!!!!\n");
BLE_Camera_Control(Msg.uData);
break;
default:
break;
}
#ifdef BLE_USE_PSRAM
if(Msg.buffer != NULL) {
PSRAM_FREE(Msg.buffer);
}
#endif
}
}
void change_ble_task_priority(void)
{
//在OTA升级的时候把优先级改成和radio优先级相同
// vTaskPrioritySet(xBleTask, 4);
vTaskPrioritySet(xBleTask, (configMAX_PRIORITIES - 2));
}
void recovery_ble_task_priority(void)
{
vTaskPrioritySet(xBleTask, 2);
}
void task_ble_init(uint16_t stack_depth, uint16_t priority)
{
xMQ_ble = xQueueCreate( 32, sizeof( TaskMessage_t ) );
xTaskCreate(task_ble_entry, "task_ble_entry", stack_depth, 0, priority, &xBleTask);
}