#include "include.h"

void sco_audio_set_param(u8 type, u16 value);
u8 fot_app_connect_auth(uint8_t *packet, uint16_t size);

AT(.rodata.bsp.spp)
static const u16 bt_key_msg_tbl[] = {
    KU_PLAY,
    KU_PREV,
    KU_NEXT,
    KU_VOL_DOWN,
    KU_VOL_UP,
    KU_MODE,
};

void bt_app_cmd_process(u8 *ptr, u16 size, u8 type)
{
//    printf("SPP RX:");
//    print_r(ptr, size);

    switch (ptr[0]) {
        case 0x01:
            //system control
            if (size != 4) {
                return;     //2byte data + 2byte crc16
            }
            if ((ptr[1] > 0) && (ptr[1] < sizeof(bt_key_msg_tbl))) {
                msg_enqueue(bt_key_msg_tbl[ptr[1] - 1]);
            }
            break;

        case 0x02:
            //rgb leds control
            if (size != 7) {
                return;     //5byte data + 2byte crc16
            }
#if PWM_RGB_EN
            if (ptr[1] > 0) {
                pwm_rgb_write(ptr[2], ptr[3], ptr[4]);
            } else {
                pwm_rgb_close();
            }
#endif // PWM_RGB_EN
            break;

        case 0x03:
            //ota control
            if (ptr[1] == 'T') {
                RTCCNT = GET_LE32(ptr + 2);
                //printf("settime: %d\n", (u32)RTCCNT);
            }
            break;
    }
}

void spp_rx_callback(uint8_t *packet, uint16_t size)
{
#if FOT_EN
    if(fot_app_connect_auth(packet, size)){
        fot_recv_proc(packet,size);
        return;
    }
#endif
#if BT_SPP_EN
    bt_app_cmd_process(packet, size, 0);
#endif // BT_SPP_EN
}

void spp_proc_test_rx_callback(uint8_t *packet, uint16_t size)
{
#if FOT_EN
    if(fot_app_connect_auth(packet, size)){
        fot_recv_proc(packet,size);
        return;
    }
#endif
#if BT_SPP_EN
    bt_app_cmd_process(packet, size, 0);
#endif // BT_SPP_EN
}

void spp_dueros_ota_rx_callback(uint8_t *packet, uint16_t size)
{
#if FOT_EN
    if(fot_app_connect_auth(packet, size)){
        fot_recv_proc(packet,size);
        return;
    }
#endif
#if BT_SPP_EN
    bt_app_cmd_process(packet, size, 0);
#endif // BT_SPP_EN
}

void spp_connect_callback(void)
{
    printf("--->spp_connect_callback\n");

#if FOT_EN
    fot_spp_connect_callback();
#endif
}

void spp_disconnect_callback(void)
{
    printf("--->spp_disconnect_callback\n");

#if FOT_EN
    fot_spp_disconnect_callback();
#endif
}
