
#include "dwt.h"
#include "fly_param.h"
#include "iec104_conf.h"
#include "iec104_queue.h"
#include "iec104_types.h"
#include "main.h"
#include "nor_flash.h"
#include <string.h>

#define TEST_BUF_SIZE 2048

#define TEST_SECTOR_NO 127 /* 测试扇区号，值域【0,128】 */

uint8_t g_test_buf[TEST_BUF_SIZE]; /* 数据测试用缓冲区 */

/*******************************************************************************
 * Function Name  : fly_init
 * Description    : fly_init program
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
static int fly_init(void)
{

    return 0;
}

static void *fly_malloc(unsigned char nbyte)
{

    return malloc(nbyte);
}

static void fly_free(void *pblock)
{

    free(pblock);
}

static iec_32u fly_set_time(PCP56Time2a_T time)
{

    return RET_SUCESS;
}
static iec_32u fly_get_time(PCP56Time2a_T time)
{

    return RET_SUCESS;
}

static iec_8s fly_get_station_info(iec_16u *addr, iec_8u n, iec_8u *MaxNum)
{

    *MaxNum = WTP_SUPPORT_END_NUMBER;
    *addr   = n;
    return RET_SUCESS;
}

static float fly_get_station_temp(iec_16u addr)
{

    return 30;
}

static iec_16u fly_get_link_addr(void)
{

    return 0x0001;
}

static void fly_close_link(void)
{

    // GPRSFlag               = SYSTEM_FLAG_GPRS_CLOSE;
    // IEC104_STATE_FLAG_INIT = IEC104_FLAG_CLOSED;
}

void delay_ms(iec_16u ms)
{

    us_delay(ms * 1000);
}

iec_8u fly_send(int socketfd, char *buf, int len)
{
    int i;
    if (-1 == write(socketfd, buf, len))
    {
        LOG("-%s-,Send error \n", __FUNCTION__);
        return RET_ERROR;
    }

    // for (i = 0; i < len; i++)
    // {
    // NOR_WriteByte(TEST_SECTOR_NO * NOR_SECTOR_SIZE, buf[i]);
    // if (NOR_WriteBuffer(buf, TEST_SECTOR_NO * NOR_SECTOR_SIZE + 1, len) == NOR_SUCCESS)
    // {
    //     printf("send sram buffer ok!\r\n");
    // }
    // }
    LOG("Send Ok!\r\n");

    return RET_SUCESS;
}

iec_8s fly_get_info_num(iec_8u *info_num, iec_8u dev_type)
{
    return 0;
}
iec_8s fly_set_onfig(long value, iec_32u addr)
{
    return 0;
}
iec_8s fly_save_firmware(iec_8u firm_len, iec_8u *buf, iec_32u firmware_type, iec_32u iec104_update_seek_addr)
{
    return 0;
}
iec_8s fly_check_firmware(iec_32u firmware_type, iec_32u total_len)
{
    return 0;
}
iec_8s fly_update_firmware(iec_32u firmware_type)
{
    return 0;
}
iec_8s fly_back_off_firmware(iec_32u firmware_type)
{
    return 0;
}

void fly_lock(void)
{
    INT8U err;
    OSMutexPend(net_mutex, 0, &err);
    // pthread_mutex_lock(&mutex);
    LOG("Net_mutex lock!\r\n");
}
void fly_unlock()
{
    OSMutexPost(net_mutex);
    // pthread_mutex_unlock(&mutex);
    LOG("Net_mutex unlock!\r\n");
}

static iec104_t fly = {
    "fly_iec104",
    fly_init,
    delay_ms,
    fly_close_link,
    fly_malloc,
    fly_free,
    iec104_prio_enqueue,
    iec104_prio_dequeue,
    iec104_prio_queue_find_head,
    iec10x_prio_highest, /* Get the highest Prio Queue*/
    iec104_prio_queue_init,
    iec104_prio_queue_clear,
    fly_send,
    fly_set_time,
    fly_get_time,
    fly_get_station_info,
    fly_get_station_temp,
    fly_get_link_addr,
    fly_get_info_num,
    fly_set_onfig,
    fly_save_firmware,
    fly_check_firmware,
    fly_update_firmware,
    fly_back_off_firmware,

#ifdef IEC104LOCK
    fly_lock,
    fly_unlock
#endif
};

iec_32u iec10x_regist(void)
{
    return iec104_module_register(&fly);
}
