#include "ch32v30x.h"
#include "string.h"
#include "eth_driver.h"
#include "dvp.h"
#include "mini212g2.h"
#include "qdx_port.h"
#include "qdx_preprocess.h"
#include "qdx_tcp_logic.h"

#include "FreeRTOS.h"
#include "task.h"

/* ============================================================
 * TEST MODE: Generate simulated sensor data without real hardware.
 * Set to 1 to enable test pattern generation (no sensor/DVP needed).
 * Set to 0 for normal operation with real Mini212G2 sensor.
 * ============================================================ */
#define TEST_PATTERN_MODE    1

#define MAX_TCP_PAYLOAD_SIZE 10240
uint8_t g_TxNetBuffer_A_Mem[MAX_TCP_PAYLOAD_SIZE];
uint8_t g_TxNetBuffer_B_Mem[MAX_TCP_PAYLOAD_SIZE];

TcpTxBuffer_t g_TxNetBuffer_A = {
    .pBuffer = g_TxNetBuffer_A_Mem,
    .TotalCapacity = MAX_TCP_PAYLOAD_SIZE,
    .HeadOffset = 64,
    .ValidPayloadLen = 0
};
TcpTxBuffer_t g_TxNetBuffer_B = {
    .pBuffer = g_TxNetBuffer_B_Mem,
    .TotalCapacity = MAX_TCP_PAYLOAD_SIZE,
    .HeadOffset = 64,
    .ValidPayloadLen = 0
};



void OnConfigUpdate(const ConfigCommon_t *common, const Config2D_t *cfg2d, const Config1D_t *cfg1d)
{
    Preprocess_Settings_Change(cfg2d, cfg1d, common);
}

/* NG output GPIO: PA8 push-pull, active-high when NG detected */
#define NG_GPIO_PORT   GPIOA
#define NG_GPIO_PIN    GPIO_Pin_8
#define NG_GPIO_CLK    RCC_APB2Periph_GPIOA
#define NG_PULSE_MS    200  /* default NG pulse width */

static volatile uint32_t g_ng_off_time = 0;
extern volatile uint32_t sys_tick_ms;

static void NG_GPIO_Init(void)
{
    GPIO_InitTypeDef gpio = {0};
    RCC_APB2PeriphClockCmd(NG_GPIO_CLK, ENABLE);
    gpio.GPIO_Pin   = NG_GPIO_PIN;
    gpio.GPIO_Mode  = GPIO_Mode_Out_PP;
    gpio.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(NG_GPIO_PORT, &gpio);
    GPIO_ResetBits(NG_GPIO_PORT, NG_GPIO_PIN);
}

/* Flag set by TempFrameRequest callback; consumed by business task */
static volatile uint8_t g_temp_req_pending = 0;
static volatile uint8_t g_temp_req_is2d   = 1;

void OnTempFrameRequest(uint8_t is2dRequest)
{
    g_temp_req_is2d   = is2dRequest;
    g_temp_req_pending = 1;
    DBG_APP("TempFrameReq is2d=%d\r\n", (int)is2dRequest);
}

void OnDetectionResult(uint32_t frameNumber, uint8_t resultStatus)
{
    (void)frameNumber;
    /* resultStatus: 0 = NG, 1 = OK */
    if (resultStatus == 0) {
        ConfigCommon_t tmp_common;
        Config2D_t tmp_cfg2d;
        Config1D_t tmp_cfg1d;
        uint32_t pulse_ms = NG_PULSE_MS; /* fallback default */
        if (TcpLogic_GetLatestConfig(&tmp_common, &tmp_cfg2d, &tmp_cfg1d) == 0
            && tmp_cfg2d.NGioDelay > 0) {
            pulse_ms = tmp_cfg2d.NGioDelay;
        }
        GPIO_SetBits(NG_GPIO_PORT, NG_GPIO_PIN);
        g_ng_off_time = sys_tick_ms + pulse_ms;
    }
}

#define KEEPALIVE_ENABLE    1

/* ============================================================
 * FLASH / SRAM 分配配置 (Option Bytes RAM_CODE_MOD[2:0])
 * 修改后需复位才生效
 * ============================================================ */
typedef enum {
    FLASH_192_SRAM_128 = 0,   /* 00x  默认 */
    FLASH_224_SRAM_96,        /* 01x */
    FLASH_256_SRAM_64,        /* 10x */
    FLASH_128_SRAM_192,       /* 110 */
    FLASH_288_SRAM_32         /* 111 */
} FLASH_SRAM_DEFIN;

static void Config_Flash_SRAM(FLASH_SRAM_DEFIN mode)
{
    uint8_t UserByte = FLASH_GetUserOptionByte() & 0xFF;
    uint8_t newByte  = UserByte & ~0xE0;   /* clear bits [7:5] */

    switch (mode) {
    case FLASH_192_SRAM_128:                          break; /* 000 */
    case FLASH_224_SRAM_96:  newByte |= 0x40;         break; /* 010 */
    case FLASH_256_SRAM_64:  newByte |= 0x80;         break; /* 100 */
    case FLASH_128_SRAM_192: newByte |= 0xC0;         break; /* 110 */
    case FLASH_288_SRAM_32:  newByte |= 0xE0;         break; /* 111 */
    default: return;
    }

    if (newByte == UserByte) return;   /* already configured */

    FLASH_Unlock();
    FLASH_ProgramOptionByteData(0x1FFFF802, newByte);
    FLASH_Lock();
    printf("Flash/SRAM config changed to %d, resetting...\r\n", mode);
    NVIC_SystemReset();
}

u8  MACAddr[6];
u8  IPAddr[4]   = {192, 168, 7, 10};
u8  GWIPAddr[4] = {192, 168, 7,  1};
u8  IPMask[4]   = {255, 255, 255, 0};
u8  DESIP[4]    = {192, 168, 7, 50};
u16 desport     = 5512;
u16 srcport     = 5511;

u8 SocketId;
u8 SocketRecvBuf[WCHNET_MAX_SOCKET_NUM][RECE_BUF_LEN];

void mStopIfError(u8 iError)
{
    if (iError == WCHNET_ERR_SUCCESS) return;
    printf("Error: %02X\r\n", (u16)iError);
}

void TIM2_Init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseStructure.TIM_Period        = WCHNETTIMERPERIOD * 1000 - 1;
    TIM_TimeBaseStructure.TIM_Prescaler     = SystemCoreClock / 1000000 - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    NVIC_EnableIRQ(TIM2_IRQn);
}





/* qdx_port.c notification hooks */
extern void qdx_port_sock_recv_notify(uint8_t sockid);
extern void qdx_port_sock_connect_notify(uint8_t sockid);
extern void qdx_port_sock_disconnect_notify(uint8_t sockid);
extern void qdx_port_init(void);


void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
{
    DBG_APP("SockInt: id=%d stat=0x%02X\r\n", socketid, intstat);
    if (intstat & SINT_STAT_RECV)
    {
        qdx_port_sock_recv_notify(socketid);
    }
    if (intstat & SINT_STAT_CONNECT)
    {
        WCHNET_ModifyRecvBuf(socketid, (u32)SocketRecvBuf[socketid], RECE_BUF_LEN);
        qdx_port_sock_connect_notify(socketid);
        DBG_APP("TCP Connected, socket %d\r\n", socketid);
    }
    if (intstat & SINT_STAT_DISCONNECT)
    {
        qdx_port_sock_disconnect_notify(socketid);
        DBG_APP("TCP Disconnected, socket %d\r\n", socketid);
    }
    if (intstat & SINT_STAT_TIM_OUT)
    {
        qdx_port_sock_disconnect_notify(socketid);
        DBG_APP("TCP Timeout, socket %d\r\n", socketid);
    }
}

void WCHNET_HandleGlobalInt(void)
{
    u8  intstat;
    u16 i;
    u8  socketint;
    intstat = WCHNET_GetGlobalInt();
    DBG_APP("GlobalInt: 0x%02X\r\n", intstat);
    if (intstat & GINT_STAT_UNREACH)    DBG_APP("GINT_STAT_UNREACH\r\n");
    if (intstat & GINT_STAT_IP_CONFLI)  DBG_APP("GINT_STAT_IP_CONFLI\r\n");
    if (intstat & GINT_STAT_PHY_CHANGE) {
        i = WCHNET_GetPHYStatus();
        DBG_APP("PHY_CHANGE: status=0x%04X %s\r\n", i,
                (i & PHY_Linked_Status) ? "LINK_UP" : "LINK_DOWN");
    }
    if (intstat & GINT_STAT_SOCKET) {
        for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
            socketint = WCHNET_GetSocketInt(i);
            if (socketint) WCHNET_HandleSockInt(i, socketint);
        }
    }
}

/* ============================================================
 * RTOS Task: Heartbeat (periodic print for debug)
 * ============================================================ */
static void task_heartbeat_entry(void *pvParameters)
{
    (void)pvParameters;
    uint32_t cnt = 0;
    while (1)
    {
#if TEST_PATTERN_MODE
        printf("[HB] %d  tick=%d  TEST_MODE  frm=%d\r\n",
               (int)cnt++, (int)xTaskGetTickCount(),
               (int)dvp_frame_count);
#else
        printf("[HB] %d  tick=%d  dvp_frm=%d row_irq=%d\r\n",
               (int)cnt++, (int)xTaskGetTickCount(),
               (int)dvp_frame_count, (int)dvp_row_irq_cnt);
#endif
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}

/* ============================================================
 * RTOS Task: WCHNET protocol stack driver (highest priority)
 * ============================================================ */
static void task_wchnet_entry(void *pvParameters)
{
    (void)pvParameters;
    while (1)
    {
        WCHNET_MainTask();
        if (WCHNET_QueryGlobalInt())
            WCHNET_HandleGlobalInt();
        vTaskDelay(pdMS_TO_TICKS(5));
    }
}

/* ============================================================
 * RTOS Task: Business logic (DVP + preprocess + send)
 * ============================================================ */
/* ============================================================
 * Burst capture state machine
 * ============================================================ */
static uint8_t  burst_active       = 0;   /* 1 = currently in burst */
static uint8_t  burst_remaining    = 0;   /* frames left to capture */
static uint32_t burst_next_time_ms = 0;   /* next burst frame time */

#if TEST_PATTERN_MODE
/* ============================================================
 * Test Pattern Generator
 * Generates simulated 256x192 Y16 thermal images at ~10 FPS.
 * Pattern cycles through several types so all pipeline paths
 * can be exercised.
 *
 * Pattern 0: Gradient (25.00~45.00°C) — baseline, no trigger
 * Pattern 1: Hot center spot (90.00°C) — should trigger alarm
 * Pattern 2: Uniform warm (35.00°C) — no trigger
 * Pattern 3: Checkerboard with hot cells — tests ROI search
 * ============================================================ */
#define TEST_FPS_DELAY_MS   100   /* ~10 FPS */
#define TEMP_RAW(deg_c)     ((uint16_t)((deg_c) * 100))  /* e.g. 35.00°C → 3500 */

static void test_fill_gradient(uint16_t *buf, uint16_t w, uint16_t h)
{
    /* Top-to-bottom gradient: 25°C at top → 45°C at bottom */
    for (uint16_t y = 0; y < h; y++) {
        uint16_t temp = TEMP_RAW(25.0) + (uint16_t)((uint32_t)y * TEMP_RAW(20.0) / h);
        for (uint16_t x = 0; x < w; x++)
            buf[y * w + x] = temp;
    }
}

static void test_fill_hotspot(uint16_t *buf, uint16_t w, uint16_t h)
{
    /* Background 30°C, center 32x32 block at 90°C */
    for (uint16_t y = 0; y < h; y++)
        for (uint16_t x = 0; x < w; x++)
            buf[y * w + x] = TEMP_RAW(30.0);
    uint16_t cx = w / 2, cy = h / 2;
    for (uint16_t y = cy - 16; y < cy + 16; y++)
        for (uint16_t x = cx - 16; x < cx + 16; x++)
            buf[y * w + x] = TEMP_RAW(90.0);
}

static void test_fill_uniform(uint16_t *buf, uint16_t w, uint16_t h)
{
    for (uint16_t y = 0; y < h; y++)
        for (uint16_t x = 0; x < w; x++)
            buf[y * w + x] = TEMP_RAW(35.0);
}

static void test_fill_checker(uint16_t *buf, uint16_t w, uint16_t h)
{
    /* 32x32 checkerboard: alternating 30°C and 85°C blocks */
    for (uint16_t y = 0; y < h; y++)
        for (uint16_t x = 0; x < w; x++) {
            uint8_t cell = ((y / 32) + (x / 32)) & 1;
            buf[y * w + x] = cell ? TEMP_RAW(85.0) : TEMP_RAW(30.0);
        }
}

static void task_test_pattern_entry(void *pvParameters)
{
    (void)pvParameters;
    uint32_t frame_num = 0;
    uint8_t pattern_idx = 0;
    /* Cycle: gradient x20, hotspot x5, uniform x10, checker x5 */
    const uint8_t pattern_seq[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                                   1,1,1,1,1,
                                   2,2,2,2,2,2,2,2,2,2,
                                   3,3,3,3,3};
    const uint8_t seq_len = sizeof(pattern_seq);

    while (1)
    {
        uint16_t *buf = (uint16_t *)FrameBuffer;
        switch (pattern_seq[pattern_idx % seq_len]) {
            case 0: test_fill_gradient(buf, SENSOR_WIDTH, SENSOR_HEIGHT); break;
            case 1: test_fill_hotspot(buf, SENSOR_WIDTH, SENSOR_HEIGHT);  break;
            case 2: test_fill_uniform(buf, SENSOR_WIDTH, SENSOR_HEIGHT);  break;
            case 3: test_fill_checker(buf, SENSOR_WIDTH, SENSOR_HEIGHT);  break;
        }
        pattern_idx++;
        if (pattern_idx >= seq_len) pattern_idx = 0;

        frame_num++;
        dvp_frame_count = frame_num;
        Ready_Frame_Count = frame_num;
        Frame_Ready_Flag = 1;

        vTaskDelay(pdMS_TO_TICKS(TEST_FPS_DELAY_MS));
    }
}
#endif /* TEST_PATTERN_MODE */

/* ============================================================
 * 1D mode: build and send a single 1D temperature frame from
 * the current raw image. Scans the center row and packs
 * TempPoint1D_t (2B time_offset + 2B temp, little-endian).
 * ============================================================ */
static void send_1d_frame_from_raw(const RawImageBuffer_t *raw, TcpTxBuffer_t *tx_buf)
{
    uint16_t w = raw->Width;
    uint16_t h = raw->Height;
    uint16_t *src = raw->pData;
    uint16_t row = h / 2;  /* center row */

    tx_buf->ValidPayloadLen = 0;
    uint8_t *dest = tx_buf->pBuffer + tx_buf->HeadOffset;
    uint32_t capacity = tx_buf->TotalCapacity - tx_buf->HeadOffset;
    uint16_t points = w;
    if ((uint32_t)points * 4 > capacity)
        points = (uint16_t)(capacity / 4);

    int16_t min_t = 32767, max_t = -32768;
    int32_t sum_t = 0;

    for (uint16_t i = 0; i < points; i++) {
        uint16_t temp = src[row * w + i];
        uint16_t time_offset = (uint16_t)((uint32_t)i * 600 / (points > 1 ? points - 1 : 1));
        int16_t t = (int16_t)temp;
        if (t < min_t) min_t = t;
        if (t > max_t) max_t = t;
        sum_t += t;
        dest[0] = (uint8_t)(time_offset & 0xFF);
        dest[1] = (uint8_t)((time_offset >> 8) & 0xFF);
        dest[2] = (uint8_t)(temp & 0xFF);
        dest[3] = (uint8_t)((temp >> 8) & 0xFF);
        dest += 4;
    }
    tx_buf->ValidPayloadLen = (uint32_t)points * 4;

    PreprocessResult_t meta;
    memset(&meta, 0, sizeof(meta));
    meta.pValidData  = tx_buf->pBuffer + tx_buf->HeadOffset;
    meta.DataLength  = tx_buf->ValidPayloadLen;
    meta.ValidWidth  = points;
    meta.ValidHeight = 1;
    meta.MinTemp     = min_t;
    meta.MaxTemp     = max_t;
    meta.AvgTemp     = (int16_t)(sum_t / (points > 0 ? points : 1));
    meta.RoiTemp     = meta.AvgTemp;
    meta.FrameNumber = raw->FrameNumber;

    TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 0x01, 0 /* IS_1D */);
}

static void task_business_entry(void *pvParameters)
{
    (void)pvParameters;
    static uint8_t use_buffer_A = 1;

    while (1)
    {
        /* NG pulse off check */
        if (g_ng_off_time && sys_tick_ms >= g_ng_off_time) {
            GPIO_ResetBits(NG_GPIO_PORT, NG_GPIO_PIN);
            g_ng_off_time = 0;
        }

#if !TEST_PATTERN_MODE
        DVP_Task();
#endif

        /* Handle on-demand frame request from server */
        if (g_temp_req_pending)
        {
            g_temp_req_pending = 0;
            RawImageBuffer_t raw_img;
            raw_img.pData = (uint16_t *)FrameBuffer;
            raw_img.Width = SENSOR_WIDTH;
            raw_img.Height = SENSOR_HEIGHT;
            raw_img.FrameNumber = Ready_Frame_Count;

            TcpTxBuffer_t *tx_buf = use_buffer_A ? &g_TxNetBuffer_A : &g_TxNetBuffer_B;
            use_buffer_A = !use_buffer_A;
            tx_buf->ValidPayloadLen = 0;

            if (g_temp_req_is2d) {
                PreprocessResult_t meta;
                if (Preprocess_Execute(&raw_img, tx_buf, &meta) == 0)
                    TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 0x02 /* REQUEST */, 1);
            } else {
                send_1d_frame_from_raw(&raw_img, tx_buf);
            }
        }

        if (Frame_Ready_Flag)
        {
            Frame_Ready_Flag = 0;

            RawImageBuffer_t raw_img;
            raw_img.pData = (uint16_t *)FrameBuffer;
            raw_img.Width = SENSOR_WIDTH;
            raw_img.Height = SENSOR_HEIGHT;
            raw_img.FrameNumber = Ready_Frame_Count;

            if (burst_active)
            {
                /* Burst mode: wait for interval, then capture & send */
                if (sys_tick_ms >= burst_next_time_ms)
                {
                    PreprocessResult_t meta;
                    TcpTxBuffer_t *tx_buf = use_buffer_A ? &g_TxNetBuffer_A : &g_TxNetBuffer_B;
                    use_buffer_A = !use_buffer_A;
                    tx_buf->ValidPayloadLen = 0;

                    if (Preprocess_Execute(&raw_img, tx_buf, &meta) == 0)
                    {
                        TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 0x01, 1);
                    }
                    burst_remaining--;
                    if (burst_remaining == 0) {
                        burst_active = 0;
                        DBG_APP("Burst complete\r\n");
                    } else {
                        /* Read interval from config */
                        ConfigCommon_t tc; Config2D_t t2; Config1D_t t1;
                        uint16_t interval_ms = 0;
                        if (TcpLogic_GetLatestConfig(&tc, &t2, &t1) == 0)
                            interval_ms = t2.TriggerInternalIntervalMs;
                        burst_next_time_ms = sys_tick_ms + interval_ms;
                    }
                }
            }
            else if (Preprocess_CheckInternalTrigger2D(&raw_img) == 1)
            {
                /* Trigger hit! Start burst: this frame is frame #0 */
                PreprocessResult_t meta;
                TcpTxBuffer_t *tx_buf = use_buffer_A ? &g_TxNetBuffer_A : &g_TxNetBuffer_B;
                use_buffer_A = !use_buffer_A;
                tx_buf->ValidPayloadLen = 0;

                if (Preprocess_Execute(&raw_img, tx_buf, &meta) == 0)
                {
                    TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 0x01, 1);
                }

                /* Determine burst count from config */
                ConfigCommon_t tc; Config2D_t t2; Config1D_t t1;
                uint8_t total_burst = 1;
                uint16_t interval_ms = 0;
                if (TcpLogic_GetLatestConfig(&tc, &t2, &t1) == 0) {
                    if (t2.TriggerBurstCount > 1)
                        total_burst = t2.TriggerBurstCount;
                    interval_ms = t2.TriggerInternalIntervalMs;
                }
                if (total_burst > 1) {
                    burst_active = 1;
                    burst_remaining = total_burst - 1; /* frame #0 already sent */
                    burst_next_time_ms = sys_tick_ms + interval_ms;
                    DBG_APP("Burst start: %d frames, interval=%d ms\r\n",
                            (int)total_burst, (int)interval_ms);
                }
            }
        }
        else
        {
            vTaskDelay(pdMS_TO_TICKS(2));
        }
    }
}

int main(void)
{
    u8 i;
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    printf("TCPClient Test\r\nSystemClk:%d\r\n", SystemCoreClock);
    printf("UserByte: %02x\r\n", FLASH_GetUserOptionByte() & 0xFF);
    Config_Flash_SRAM(FLASH_128_SRAM_192);
    printf("net version:%x\n", WCHNET_GetVer());
    if (WCHNET_LIB_VER != WCHNET_GetVer()) printf("version error.\n");

    WCHNET_GetMacAddr(MACAddr);
    printf("mac addr:");
    for (i = 0; i < 6; i++) printf("%x ", MACAddr[i]);
    printf("\n");

#if TEST_PATTERN_MODE
    printf("=== TEST PATTERN MODE === No sensor/DVP hardware needed\r\n");
    /* Skip Mini212G2_Init() and DVP_Init() */
#else
    Mini212G2_Init();   /* Configure sensor for CMOS/DVP Y16 output */
    DVP_Init();
#endif
    TIM2_Init();
    NG_GPIO_Init();

    i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr);
    mStopIfError(i);
    if (i == WCHNET_ERR_SUCCESS) printf("WCHNET_LibInit Success\r\n");

#if KEEPALIVE_ENABLE
    {
        struct _KEEP_CFG cfg = {20000, 15000, 9};
        WCHNET_ConfigKeepLive(&cfg);
    }
#endif

    qdx_port_init();

    Preprocess_Init(SENSOR_WIDTH, SENSOR_HEIGHT);

#if TEST_PATTERN_MODE
    /* Set default preprocess config so trigger fires with test patterns.
     * Without this, all Config2D_t fields are 0 and trigger never fires
     * (roi_w==0 → immediate return 0). */
    {
        Config2D_t test_cfg2d;
        memset(&test_cfg2d, 0, sizeof(test_cfg2d));
        test_cfg2d.TargetWidth  = 64;   /* ROI extraction 64x64 */
        test_cfg2d.TargetHeight = 64;
        test_cfg2d.TriggerRoiX  = 0;   /* Full-frame trigger area */
        test_cfg2d.TriggerRoiY  = 0;
        test_cfg2d.TriggerRoiW  = SENSOR_WIDTH;
        test_cfg2d.TriggerRoiH  = SENSOR_HEIGHT;
        test_cfg2d.TriggerCondition = 1;         /* Max temperature */
        test_cfg2d.TriggerTemperatureThreshold = 8000; /* 80.00°C */
        test_cfg2d.TriggerBurstCount = 3;
        test_cfg2d.TriggerInternalIntervalMs = 200;
        test_cfg2d.NGioDelay = 200;
        Preprocess_Settings_Change(&test_cfg2d, NULL, NULL);
        printf("Test default config loaded: trigger thresh=8000 ROI=full burst=3\r\n");
    }
#endif

    TcpLogic_Init(MACAddr, NULL);
    TcpLogic_RegisterConfigCallback(OnConfigUpdate);
    TcpLogic_RegisterDetectionCallback(OnDetectionResult);
    TcpLogic_RegisterTempFrameRequestCallback(OnTempFrameRequest);
    DBG_APP("TcpLogic_Start...\r\n");
    TcpLogic_Start();

    DBG_APP("Creating RTOS tasks...\r\n");
    xTaskCreate(task_wchnet_entry, "wchnet", 512, NULL, 6, NULL);
    xTaskCreate(task_business_entry, "business", 512, NULL, 5, NULL);
    xTaskCreate(task_heartbeat_entry, "hb", 256, NULL, 3, NULL);
#if TEST_PATTERN_MODE
    xTaskCreate(task_test_pattern_entry, "testpat", 256, NULL, 4, NULL);
#endif
    DBG_APP("Starting scheduler\r\n");
    vTaskStartScheduler();

    /* Should never reach here */
    while (1) {}
}
