/**
* @file example_user_ble_remote.c
* @author www.tuya.com
* @version 0.1
* @date 2022-05-20
*
* @copyright Copyright (c) tuya.inc 2022
*
*/

#include "tuya_cloud_types.h"

#if defined(ENABLE_BT_REMOTE_CTRL) && (ENABLE_BT_REMOTE_CTRL==1)
#include "tal_log.h"
#include "tuya_bt.h"
#include "app_ble.h"

/***********************************************************
*************************micro define***********************
***********************************************************/
#define ADV_DATA_TYPE_SERVICE_UUID 0x07
#define ADV_DATA_TYPE_FLAGS 0x01
#define SERVICE_UUID_AD_LENGTH 0x11  // 17 bytes

// 厂商产品ID (柏曼ID：0201)
#define VENDOR_PRODUCT_ID 0x0201

// 按键ID定义 - 根据协议表格更新
#define KEY_ID_POWER          0xa1  // 总开/关 K3
#define KEY_ID_BRIGHT_UP      0xa2  // 亮度增加 K4
#define KEY_ID_BRIGHT_DOWN    0xa3  // 亮度减少 K8
#define KEY_ID_GROUP1_SWITCH  0xa4  // 分组1开/关 K9
#define KEY_ID_GROUP2_SWITCH  0xa5  // 分组2开/关 K10
#define KEY_ID_COLOR_UP       0xa6  // 色温增加 K12
#define KEY_ID_MAIN_SWITCH    0xa7  // 主光/下光开/关 K13
#define KEY_ID_MAIN_BRIGHT_UP 0xa8  // 主光/下光亮度+ K14
#define KEY_ID_MAIN_BRIGHT_DOWN 0xa9 // 主光/下光亮度- K15
#define KEY_ID_COLOR_DOWN     0xaa  // 色温减少 K16
#define KEY_ID_AMBIENT_SWITCH 0xab  // 氛围光/上光开/关 K18
#define KEY_ID_AMBIENT_BRIGHT_UP 0xac // 氛围光/上光亮度+ K19
#define KEY_ID_AMBIENT_BRIGHT_DOWN 0xad // 氛围光/上光亮度- K20
#define KEY_ID_NIGHT_LIGHT    0xae  // 夜灯 K21
#define KEY_ID_MODE           0xaf  // 模式 K25
#define KEY_RESET             0xb0  //配网复位
#define KEY_ALIGNMENT         0xb1  //对码
#define KEY_CLEAR             0xb2  //清码

// 分组ID定义
#define GROUP_ID_MAIN         0x00  // 总分组
#define GROUP_ID_GROUP1       0x01  // 分组1
#define GROUP_ID_GROUP2       0x02  // 分组2
#define GROUP_ID_AMBIENT      0x02  // 氛围光分组

// 操作类型定义
#define OP_TYPE_SHORT_PRESS   0x00  // 短按松手
#define OP_TYPE_LONG_PRESS    0x01  // 长按
#define OP_TYPE_LONG_RELEASE  0x02  // 长按松手
#define OP_TYPE_COMBO         0x03  // 组合键

// 错误码定义
#define PARSE_SUCCESS     0
#define PARSE_ERROR       1
#define CHECKSUM_ERROR    2

/***********************************************************
***********************typedef define***********************
***********************************************************/

// 遥控器数据结构
typedef struct {
    UINT16_T vendor_product_id;  // 厂商产品ID
    UINT32_T remote_id;          // 遥控器ID
    UINT8_T  command_id;         // 指令ID
    UINT8_T  group_id;           // 分组ID
    UINT8_T  random_id;          // 随机ID
    UINT8_T  key_id;             // 按键ID
    UINT8_T  reserved[5];        // 预留字段
    UINT8_T  checksum;           // 校验位
} USER_REMOTE_DATA_T;

// 解析结果结构
typedef struct {
    USER_REMOTE_DATA_T remote_data;
    UINT8_T mac_addr[6];         // 设备MAC地址
    INT8_T  rssi;                // 信号强度
    UINT8_T parse_result;        // 解析结果
    UINT8_T operation_type;      // 操作类型
    UINT8_T additional_param;    // 附加参数
} REMOTE_PARSE_RESULT_T;

/***********************************************************
***********************variable define**********************
***********************************************************/

// 遥控器ID白名单（可配置接受的遥控器）
STATIC UINT32_T sg_remote_whitelist[] = {
    0x12345678,  // 示例遥控器ID1
    0x87654321,  // 示例遥控器ID2
    0x0112D6A1,
};
STATIC UINT8_T sg_whitelist_count = 2;

// 当前模式状态
STATIC UINT8_T sg_current_mode = 0x11; // 默认白光模式
/***********************************************************
***********************function define**********************
***********************************************************/

/**
* @brief 计算校验和
*/
STATIC UINT8_T __calculate_checksum(UINT8_T *data, UINT8_T len)
{
    UINT8_T sum = 0;
    while (len--) {
        sum += *data++;
    }
    return sum;
}

/**
* @brief 检查遥控器ID是否在白名单中
*/
STATIC BOOL_T __check_remote_whitelist(UINT32_T remote_id)
{
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_remote_whitelist[i] == remote_id) {
            return TRUE;
        }
    }
    return FALSE;
}

/**
* @brief 解析遥控器数据
*/
/**
* @brief 解析遥控器数据
*/
STATIC UINT8_T __parse_remote_data(UINT8_T *service_data, USER_REMOTE_DATA_T *remote_data, UINT8_T *op_type, UINT8_T *add_param)
{
    // if (service_data == NULL || remote_data == NULL) {
    //     return PARSE_ERROR;
    // }
    
    TAL_PR_NOTICE("Raw data:");
    for (int i = 0; i < 16; i++) {
        TAL_PR_NOTICE("[%d]: 0x%02X", i, service_data[i]);
    }

    // 解析16字节的服务数据
    remote_data->vendor_product_id = (service_data[0] << 8) | service_data[1];
    remote_data->remote_id = (service_data[2] << 24) | (service_data[3] << 16) | 
                            (service_data[4] << 8) | service_data[5];
    remote_data->command_id = service_data[6];
    remote_data->group_id = service_data[7];
    remote_data->random_id = service_data[8];
    remote_data->key_id = service_data[9];
    memcpy(remote_data->reserved, &service_data[10], 5);
    remote_data->checksum = service_data[15];

    TAL_PR_NOTICE("key_id = %02x\r\n",remote_data->key_id);
    
    // 验证厂商产品ID
    if (remote_data->vendor_product_id != VENDOR_PRODUCT_ID) {
        TAL_PR_NOTICE("Vendor product ID mismatch: 0x%04X", remote_data->vendor_product_id);
        return PARSE_ERROR;
    }
    
    // 验证校验和
    UINT8_T calculated_checksum = __calculate_checksum(service_data, 15);
    if (calculated_checksum != remote_data->checksum) {
        TAL_PR_NOTICE("Checksum error: calculated=0x%02X, received=0x%02X", 
                     calculated_checksum, remote_data->checksum);
        return CHECKSUM_ERROR;
    }
    
    // 检查遥控器ID白名单
    if (!__check_remote_whitelist(remote_data->remote_id)) {
        TAL_PR_NOTICE("Remote ID not in whitelist: 0x%08X", remote_data->remote_id);
        // user_ble_remote_add_whitelist(remote_data->remote_id);
        // return PARSE_ERROR;
    }

    // 根据协议表格解析操作类型和附加参数
    *op_type = OP_TYPE_SHORT_PRESS; // 默认短按
    
    // 根据命令ID和分组ID判断操作类型
    TAL_PR_NOTICE("remote_id = %08x,group_id = %02x\r\n",remote_data->remote_id,remote_data->group_id);
    switch (remote_data->group_id){
        case 0x00:
            if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x00)){
                remote_data->key_id = KEY_ID_POWER;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        case 0x01:
            if((remote_data->key_id == 0xb0)&&(remote_data->reserved[4] >= 0x05)){
                remote_data->key_id = KEY_ALIGNMENT;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            if(remote_data->key_id == 0x1c)
            {
                remote_data->key_id = KEY_ID_GROUP1_SWITCH;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        break;
        case 0x02:
            if((remote_data->key_id == 0xb0)&&(remote_data->reserved[4] >= 0x05)){
                remote_data->key_id = KEY_CLEAR;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            else{
                remote_data->key_id = KEY_ID_GROUP2_SWITCH;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        break;
        default:
        TAL_PR_NOTICE("group_id = %02x\r\n",remote_data->group_id);
        break;
    }
    TAL_PR_NOTICE("key_id = %02x\r\n",remote_data->key_id);
    switch (remote_data->key_id) {
        case 0x97: // 开关类命令
            if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x01)){
                remote_data->key_id = KEY_ID_MAIN_SWITCH;
            } 
            else if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x02)){
                remote_data->key_id = KEY_ID_AMBIENT_SWITCH;
            }
            break;
            
        case 0x25: // 亮度增加类命令
            if(remote_data->reserved[0] == 0x00){
                remote_data->key_id = KEY_ID_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            else if(remote_data->reserved[0] == 0x01){
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            else if(remote_data->reserved[0] == 0x02){
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x26: // 亮度减少类命令
            if (remote_data->reserved[0] == 0x00) {
                // 亮度减少 K8
                remote_data->key_id = KEY_ID_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            } else if (remote_data->reserved[0] == 0x01) {
                // 主光/下光亮度- K15
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            } else if (remote_data->reserved[0] == 0x02) {
                // 氛围光/上光亮度- K20
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x27: // 色温增加 K12
            if (remote_data->reserved[0] == 0x00) {
                remote_data->key_id = KEY_ID_COLOR_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x28: // 色温减少 K16
            if (remote_data->reserved[0] == 0x00) {
                remote_data->key_id = KEY_ID_COLOR_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
        case 0x32:
            remote_data->key_id = KEY_ID_NIGHT_LIGHT;
        break;
            
        case 0x3E: // 长按亮度增加
            if (remote_data->reserved[0] == 0x00) {
                // 亮度增加长按 K4
                remote_data->key_id = KEY_ID_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->reserved[0] == 0x01) {
                // 主光/下光亮度+长按 K14
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->reserved[0] == 0x02) {
                // 氛围光/上光亮度+长按 K19
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        case 0x3F: // 长按亮度减少
            if (remote_data->group_id == 0x00) {
                // 亮度减少长按 K8
                remote_data->key_id = KEY_ID_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->group_id == 0x01) {
                // 主光/下光亮度-长按 K15
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->group_id == 0x02) {
                // 氛围光/上光亮度-长按 K20
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        case 0x40: // 色温增加长按 K12
            remote_data->key_id = KEY_ID_COLOR_UP;
            *op_type = OP_TYPE_LONG_PRESS;
            break;
            
        case 0x41: // 色温减少长按 K16
            remote_data->key_id = KEY_ID_COLOR_DOWN;
            *op_type = OP_TYPE_LONG_PRESS;
            break;
            
        case 0x29: // 模式 K25
            remote_data->key_id = KEY_ID_MODE;
            *op_type = OP_TYPE_SHORT_PRESS;
            *add_param = remote_data->reserved[0]; // 模式参数
            break;
            
        case 0x00: // 长按松手
            // 长按松手命令，通过reserved字段判断
            if (remote_data->reserved[0] == 0x1F && 
                remote_data->reserved[1] == 0xFF && 
                remote_data->reserved[2] == 0xFF && 
                remote_data->reserved[3] == 0xFF) {
                *op_type = OP_TYPE_LONG_RELEASE;
            }
            break;
            
        case 0xBB: // 组合键 - 色温增加+色温减少
            if(remote_data->reserved[4] >= 0x03){
                remote_data->key_id = KEY_RESET;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        default:
            TAL_PR_NOTICE("Unknown command ID: 0x%02X", remote_data->key_id);
            break;
    }
    
    return PARSE_SUCCESS;
}

/**
* @brief 处理按键事件
*/
/**
* @brief 处理按键事件
*/
STATIC VOID __handle_key_event(REMOTE_PARSE_RESULT_T *result)
{
    if (result == NULL || result->parse_result != PARSE_SUCCESS) {
        return;
    }
    
    USER_REMOTE_DATA_T *remote_data = &result->remote_data;
    
    // TAL_PR_NOTICE("=== Key Event Received ===");
    // TAL_PR_NOTICE("Remote ID: 0x%08X", remote_data->remote_id);
    // TAL_PR_NOTICE("Command ID: 0x%02X", remote_data->command_id);
    // TAL_PR_NOTICE("Key ID: 0x%02X", remote_data->key_id);
    // TAL_PR_NOTICE("Group ID: 0x%02X", remote_data->group_id);
    // TAL_PR_NOTICE("Operation Type: %d", result->operation_type);
    // TAL_PR_NOTICE("RSSI: %d dBm", result->rssi);
    
    // 根据协议表格处理按键
    switch (remote_data->key_id) {
        case KEY_ID_POWER: // K3 总开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Total Power Toggle");
                // 执行总开/关操作
            }
            break;
            
        case KEY_ID_BRIGHT_UP: // K4 亮度增加
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Brightness +10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Brightness Continuous +");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Brightness Change Stop");
            }
            break;
            
        case KEY_ID_BRIGHT_DOWN: // K8 亮度减少
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Brightness -10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Brightness Continuous -");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Brightness Change Stop");
            }
            break;
            
        case KEY_ID_GROUP1_SWITCH: // K9 分组1开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Group 1 Switch");
            }
            break;
            
        case KEY_ID_GROUP2_SWITCH: // K10 分组2开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Group 2 Switch");
            }
            break;
            
        case KEY_ID_COLOR_UP: // K12 色温增加
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature +10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature Continuous +");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Color Temperature Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_SWITCH: // K13 主光/下光开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Switch");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Adjust");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_BRIGHT_UP: // K14 主光/下光亮度+
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness +10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Continuous +");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_BRIGHT_DOWN: // K15 主光/下光亮度-
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness -10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Continuous -");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_COLOR_DOWN: // K16 色温减少
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature -10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature Continuous -");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Color Temperature Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_SWITCH: // K18 氛围光/上光开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Switch");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Adjust");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_BRIGHT_UP: // K19 氛围光/上光亮度+
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness +10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Continuous +");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_BRIGHT_DOWN: // K20 氛围光/上光亮度-
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness -10%%");
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Continuous -");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_NIGHT_LIGHT: // K21 夜灯
            TAL_PR_NOTICE("Action: Night Light Mode");
            break;
            
        case KEY_ID_MODE: // K25 模式
            TAL_PR_NOTICE("Action: Mode Switch");
            // 模式循环: 白光(0x11) -> 黄光(0x12) -> 中性光(0x13) -> 白光(0x11)
            switch (sg_current_mode) {
                case 0x11: // 白光
                    sg_current_mode = 0x12; // 切换到黄光
                    TAL_PR_NOTICE("Switch to Yellow Light Mode");
                    break;
                case 0x12: // 黄光
                    sg_current_mode = 0x13; // 切换到中性光
                    TAL_PR_NOTICE("Switch to Neutral Light Mode");
                    break;
                case 0x13: // 中性光
                    sg_current_mode = 0x11; // 切换回白光
                    TAL_PR_NOTICE("Switch to White Light Mode");
                    break;
                default:
                    sg_current_mode = 0x11; // 默认白光
                    break;
            }
            break;
            
        case KEY_RESET: // 配网复位
            if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Network Reset - Long Press");
                // 执行配网复位操作
            }
            break;
            
        case KEY_ALIGNMENT: // 对码
            if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Alignment/Pairing - Long Press");
                user_ble_remote_add_whitelist(remote_data->remote_id);
                // 执行对码操作
            }
            break;
            
        case KEY_CLEAR: // 清码
            if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Clear Codes - Long Press");
                // 执行清码操作
            }
            break;
            
        default:
            TAL_PR_NOTICE("Action: Unknown key (0x%02X)", remote_data->key_id);
            break;
    }
    
    TAL_PR_NOTICE("=== Key Event Processed ===");
}

/**
* @brief 查找服务UUID数据
*/
STATIC UINT8_T *__find_service_uuid_data(UINT8_T *adv_data, UINT8_T adv_len)
{
    UINT8_T *p = adv_data;
    UINT8_T pos = 0;
    
    while (pos < adv_len) {
        UINT8_T field_len = p[0];
        if (field_len == 0) break;
        
        if (pos + field_len + 1 > adv_len) break;
        
        UINT8_T field_type = p[1];
        
        if (field_type == ADV_DATA_TYPE_SERVICE_UUID && field_len == SERVICE_UUID_AD_LENGTH) {
            // 找到服务UUID数据，返回数据部分（跳过length和type）
            // TAL_PR_NOTICE("success\r\n");
            return &p[2];
        }
        
        p += (field_len + 1);
        pos += (field_len + 1);
    }
    
    return NULL;
}

/**
* @brief 解析完整的BLE广播包
*/
STATIC VOID __parse_ble_adv_packet(TAL_BLE_ADV_REPORT_T *scan_info, REMOTE_PARSE_RESULT_T *result)
{
    if (scan_info == NULL || result == NULL) {
        return;
    }
    
    // 初始化结果结构
    memset(result, 0, sizeof(REMOTE_PARSE_RESULT_T));
    result->rssi = scan_info->rssi;
    memcpy(result->mac_addr, scan_info->peer_addr.addr, 6);
    
    // 查找服务UUID数据
    UINT8_T *service_data = __find_service_uuid_data(scan_info->p_data, scan_info->data_len);
    if (service_data == NULL) {
        TAL_PR_NOTICE("No service UUID data found in ADV packet");
        result->parse_result = PARSE_ERROR;
        return;
    }
    
    // 解析遥控器数据
    result->parse_result = __parse_remote_data(service_data, &result->remote_data,&result->operation_type, &result->additional_param);
}

STATIC VOID __ty_remote_receive_callback(UCHAR_T *data, UCHAR_T len, UCHAR_T type, UCHAR_T* mac)
{
    // 涂鸦标准遥控器数据处理
    // 如果需要支持涂鸦标准遥控器，可以在这里添加处理逻辑
    
    TAL_PR_NOTICE("Tuya standard remote received, MAC: %02X:%02X:%02X:%02X:%02X:%02X", 
                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    
    return;
}

STATIC VOID __user_remote_receive_callback(TAL_BLE_ADV_REPORT_T *scan_info)
{
    TAL_PR_NOTICE("---------------- BLE Remote Data Received ----------------");
    // TAL_PR_NOTICE("Adv Type: %d, RSSI: %d, Data Len: %d", 
    //              scan_info->adv_type, scan_info->rssi, scan_info->data_len);
    
    // 解析BLE广播包
    REMOTE_PARSE_RESULT_T parse_result;
    __parse_ble_adv_packet(scan_info, &parse_result);
    
    if (parse_result.parse_result == PARSE_SUCCESS) {
        TAL_PR_NOTICE("Remote data parsed successfully");
        
        // 处理按键事件
        __handle_key_event(&parse_result);
        
        TAL_PR_NOTICE("---------------- BLE Parse Success ----------------");
    }
}

/***********************************************************
***********************external API*************************
***********************************************************/

/**
* @brief 添加遥控器到白名单
*/
OPERATE_RET user_ble_remote_add_whitelist(UINT32_T remote_id)
{
    // 检查是否已存在
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_remote_whitelist[i] == remote_id) {
            TAL_PR_NOTICE("Remote ID already in whitelist: 0x%08X", remote_id);
            return OPRT_OK;
        }
    }
    
    // 添加到白名单
    sg_remote_whitelist[sg_whitelist_count] = remote_id;
    sg_whitelist_count++;
    
    TAL_PR_NOTICE("Remote ID added to whitelist: 0x%08X", remote_id);
    return OPRT_OK;
}

/**
* @brief 移除遥控器从白名单
*/
OPERATE_RET user_ble_remote_remove_whitelist(UINT32_T remote_id)
{
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_remote_whitelist[i] == remote_id) {
            // 移动后续元素
            for (UINT8_T j = i; j < sg_whitelist_count - 1; j++) {
                sg_remote_whitelist[j] = sg_remote_whitelist[j + 1];
            }
            sg_whitelist_count--;
            TAL_PR_NOTICE("Remote ID removed from whitelist: 0x%08X", remote_id);
            return OPRT_OK;
        }
    }
    
    TAL_PR_NOTICE("Remote ID not found in whitelist: 0x%08X", remote_id);
    return OPRT_OK;
}

/**
* @brief 清空白名单
*/
OPERATE_RET user_ble_remote_clear_whitelist(VOID)
{
    sg_whitelist_count = 0;
    TAL_PR_NOTICE("Remote whitelist cleared");
    return OPRT_OK;
}

/**
* @brief tuya bluetooth remote initialization
*/
VOID user_ble_remote(INT_T argc, CHAR_T *argv[])
{
    OPERATE_RET rt = OPRT_OK;

    /* enable ble scan function */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_cb(__ty_remote_receive_callback), __EXIT);

    /* register user remote */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_raw_scan_adv_cb(__user_remote_receive_callback), __EXIT);

    TAL_PR_NOTICE("User BLE Remote Parser initialized successfully");
    TAL_PR_NOTICE("Vendor Product ID: 0x%04X", VENDOR_PRODUCT_ID);
    TAL_PR_NOTICE("Whitelist count: %d", sg_whitelist_count);

__EXIT:
    if (OPRT_OK != rt) {
        TAL_PR_NOTICE("User BLE Remote Parser initialize failed: %d", rt);
    }
    return;
}
#endif