#include "include.h"
#include "fota_proc.h"
#include "usb_com.h"
#include "usb_audio.h"
#include "usb_hid.h"
#include "usb_vendor.h"
#include "utils_hid_usage.h"
#include "wireless_client.h"


func_cb_t func_cb AT(.buf.func_cb);

AT(.text.app.proc.func)
void func_process(void)
{
    WDT_CLR();

#if BSP_SARADC_EN
    bsp_saradc_process();
#endif

#if BSP_CHARGE_EN
    bsp_charge_process();
#endif

#if USB_EN
    if (sys_cb.usb_is_active) {
        usb_device_process();
    }
#endif

#if AB_FOT_EN
    fota_process();
#endif

#if BSP_MOUSE_SENSEN_EN
    bsp_mouse_sense_process();
#endif

    prod_test_process();

    touch_process();
}

//func common message process
AT(.text.func.msg)
void func_message(u16 msg)
{
    bool event_valid = true;

    switch (msg) {

#if USB_EN
        case MSG_SYS_PC_INSERT:
            printf("[MSG] usb connected\n");
            sys_cb.usb_is_active = 1;
            #if FUNC_WIRELESS_EN
            if (func_cb.sta == FUNC_WIRELESS) {
                wireless_send_ctrl_cmd(WIRELESS_CTRL_CMD_CODE_USB_CONNECTED, 0, NULL);
            }
            #endif
            break;

        case MSG_SYS_PC_REMOVE: {
            printf("[MSG] usb disconnected\n");
            func_cb.sta = func_cb.last;
            usb_device_exit();
            usb_device_deinit();
            #if FUNC_WIRELESS_EN
            if (func_cb.sta == FUNC_WIRELESS) {
                wireless_send_ctrl_cmd(WIRELESS_CTRL_CMD_CODE_USB_DISCONNECTED, 0, NULL);
            }
            #endif
            break;
        }
#endif // USB_EN

        default:
            event_valid = false;
            break;
    }

    if (event_valid) {
        lowpwr_pwroff_delay_reset();
        lowpwr_sleep_delay_reset();
    }
}

///进入一个功能的总入口
AT(.text.func)
void func_enter(void)
{
    bsp_param_sync();

    if (func_cb.sta == FUNC_NULL) {
        func_cb.sta = (FUNC_BT == param_wl_bt_mode_read())? FUNC_BT: FUNC_WIRELESS;
    } else if (func_cb.sta == FUNC_WIRELESS || func_cb.sta == FUNC_BT) {
        param_wl_bt_mode_write(func_cb.sta);
    }

    lowpwr_sleep_delay_reset();

    lowpwr_pwroff_delay_reset();
}

AT(.text.func)
void func_exit(void)
{
    func_cb.sta = FUNC_IDLE;
}

AT(.text.func)
void func_run(void)
{
    printf("%s\n", __func__);

    while (1) {
        WDT_CLR();
        func_enter();
        switch (func_cb.sta) {
#if FUNC_BT_EN
        case FUNC_BT:
            func_bt();
            break;
#endif

#if FUNC_LE_FCC_EN
        case FUNC_LE_FCC:
            func_le_fcc();
            break;
#endif

#if FUNC_LE_BQB_RF_EN
        case FUNC_LE_BQB_RF:
            func_le_bqb_rf();
            break;
#endif

#if FUNC_IODM_EN
        case FUNC_IODM:
            func_iodm();
            break;
#endif

#if FUNC_USBD_EN
        case FUNC_USBD:
            func_usbd();
            break;
#endif

#if FUNC_IDLE_EN
        case FUNC_IDLE:
            func_idle();
            break;
#endif

#if FUNC_WIRELESS_EN
        case FUNC_WIRELESS:
            func_wireless();
            break;
#endif

        case FUNC_PWROFF:
            func_pwroff();
            break;

        default:
            func_exit();
            break;
        }
    }
}
