#ifndef _UART_CAN_H
#define _UART_CAN_H
#include <stdint.h>
#include "uart_utils.h"
#if 0
typedef struct{
        uint8_t head;   //包首标识固定为 AA
        uint8_t frame_type;     //扩展帧标识:00 为标准帧,01 为扩展帧
        uint8_t frame_rtr;      //远程帧标识:00 为数据帧,01 为远程帧
        uint8_t datalength; //有效数据长度:范围 01~08,远程帧为请求数据长度
        uint8_t extcanid[2];    //2byte ID,扩展帧高位 29bit 有效
        uint8_t cabinetcanid;    //1byte cabinetCanid
        uint8_t boardcanid;    //1byte boardCanid
        uint8_t boardindex;//主板偏移
        uint8_t data[7];//data, max: 7bytes, 加boardindex 8byte 数据,有效数据不足 8byte 后面补 00
        uint8_t tail;   //包尾标识, 默认:7A
}__attribute__((packed)) uart_can_package_t;

int uart_can_data_receive(uart_utils_t *uart,int timeout,uint8_t *cabinet_can_id,uint8_t *board_can_id,uint8_t *index,char data[7]);
int uart_can_data_send(uart_utils_t *uart,uint8_t cabinet_can_id,uint8_t board_can_id,uint8_t index,uint8_t data[]);
#endif

typedef struct
{
        uint16_t pre;
        uint8_t wakeupTime;
        uint8_t func;
        uint16_t len1;
        uint8_t len2;
        uint16_t reserve;
        uint16_t lableNum;
}__attribute__((packed)) jt_head_package_t;
typedef union
{
    struct{
        uint8_t color:4;
        uint8_t sound:2;
        uint8_t flash:2;
    }s;
    uint8_t ch;
}__attribute__((packed)) jt_ledctrl_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t ledCtr;
        uint16_t flashInterval;
        uint16_t lightDuration;
        uint32_t tag1;
        uint16_t crc;
}__attribute__((packed)) jt_light_led1_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t ledCtr;
        uint16_t flashInterval;
        uint16_t lightDuration;
        uint32_t tag1;
        uint32_t tag2;
        uint16_t crc;
}__attribute__((packed)) jt_light_led2_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t ledCtr;
        uint16_t flashInterval;
        uint16_t lightDuration;
        uint32_t tag1;
        uint32_t tag2;
        uint32_t tag3;
        uint16_t crc;
}__attribute__((packed)) jt_light_led3_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t ledCtr;
        uint16_t flashInterval;
        uint16_t lightDuration;
        uint32_t tag1;
        uint32_t tag2;
        uint32_t tag3;
        uint32_t tag4;
        uint16_t crc;
}__attribute__((packed)) jt_light_led4_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t ledCtr;
        uint16_t flashInterval;
        uint16_t lightDuration;
        uint32_t tag1;
        uint32_t tag2;
        uint32_t tag3;
        uint32_t tag4;
        uint32_t tag5;
        uint16_t crc;
}__attribute__((packed)) jt_light_led5_package_t;
typedef struct
{
        uint16_t pre1;//$$ 0x2424
        uint16_t pre2;//$$ 0x2424
        uint16_t pre3;//$$ 0x2424 CC 0x4343 DD 0x4444
        uint16_t ack;//## 0x2323 complete TT 0x5454 timeout RR 0x5252 AP-reset EE 0x4545 AP-crc-error FF 0x4646 AP broadcast end
}__attribute__((packed)) jt_receive_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint32_t tag1;
        uint8_t tag1_lable1;
        uint16_t tag1_lable2;
        uint32_t tag1_lable3;
        uint16_t crc;
}__attribute__((packed)) jt_lable_led1_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint32_t tag1;
        uint8_t tag1_lable1;
        uint16_t tag1_lable2;
        uint32_t tag1_lable3;
        uint32_t tag2;
        uint8_t tag2_lable1;
        uint16_t tag2_lable2;
        uint32_t tag2_lable3;
        uint16_t crc;
}__attribute__((packed)) jt_lable_led2_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint32_t tag1;
        uint8_t tag1_lable1;
        uint16_t tag1_lable2;
        uint32_t tag1_lable3;
        uint32_t tag2;
        uint8_t tag2_lable1;
        uint16_t tag2_lable2;
        uint32_t tag2_lable3;
        uint32_t tag3;
        uint8_t tag3_lable1;
        uint16_t tag3_lable2;
        uint32_t tag3_lable3;
        uint16_t crc;
}__attribute__((packed)) jt_lable_led3_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint32_t tag1;
        uint8_t tag1_lable1;
        uint16_t tag1_lable2;
        uint32_t tag1_lable3;
        uint32_t tag2;
        uint8_t tag2_lable1;
        uint16_t tag2_lable2;
        uint32_t tag2_lable3;
        uint32_t tag3;
        uint8_t tag3_lable1;
        uint16_t tag3_lable2;
        uint32_t tag3_lable3;
        uint32_t tag4;
        uint8_t tag4_lable1;
        uint16_t tag4_lable2;
        uint32_t tag4_lable3;
        uint16_t crc;
}__attribute__((packed)) jt_lable_led4_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint32_t tag1;
        uint8_t tag1_lable1;
        uint16_t tag1_lable2;
        uint32_t tag1_lable3;
        uint32_t tag2;
        uint8_t tag2_lable1;
        uint16_t tag2_lable2;
        uint32_t tag2_lable3;
        uint32_t tag3;
        uint8_t tag3_lable1;
        uint16_t tag3_lable2;
        uint32_t tag3_lable3;
        uint32_t tag4;
        uint8_t tag4_lable1;
        uint16_t tag4_lable2;
        uint32_t tag4_lable3;
        uint32_t tag5;
        uint8_t tag5_lable1;
        uint16_t tag5_lable2;
        uint32_t tag5_lable3;
        uint16_t crc;
}__attribute__((packed)) jt_lable_led5_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t lable1;
        uint16_t lable2;
        uint32_t lable3;
        uint8_t lable1_rule;
        uint8_t lable2_rule;
        uint8_t lable3_rule;
        uint8_t ledctrl;
        uint16_t flash_i;
        uint16_t light_d;
        uint16_t crc;
}__attribute__((packed)) jt_light_rule_package_t;
typedef struct
{
        uint16_t pre1;
        uint16_t pre2;
        uint16_t pre3;
        uint16_t pre4;
        uint16_t tagHead;
        uint32_t tag;
        uint8_t signal;
        uint8_t len;
        uint8_t featureCode;
        uint8_t count;
        uint16_t battery;
        uint16_t version;
        uint8_t ledCtrl;
        uint8_t lable1;
        uint16_t lable2;
        uint32_t lable3;
        uint16_t crc;
}__attribute__((packed)) jt_data_back_package_t;
typedef struct
{
        uint16_t len1;
        uint8_t len2;
        uint8_t lable3_1;
        uint8_t lable3_2;
        uint8_t lable3_3;
        uint8_t lable3_4;
        uint8_t lable3_5;
        uint8_t lable3_6;
        uint8_t lable3_7;
        uint8_t lable3_8;
        uint16_t crc;
}__attribute__((packed)) jt_search_package_t;

typedef struct
{
        uint32_t head;
        uint8_t len1;
        uint8_t len2;
        uint8_t len3;
        uint16_t data_crc;
        uint8_t hdinfo;
        uint16_t sfinfo;
        uint16_t custom_code;
        uint8_t mode;
        uint8_t startmac1;
        uint8_t startmac2;
        uint8_t startmac3;
        uint8_t endmac1;
        uint8_t endmac2;
        uint8_t endmac3;
}__attribute__((packed)) jt_ledota_package_t;

typedef struct
{
        uint32_t head;
        uint8_t len1;
        uint8_t len2;
        uint8_t len3;
        uint16_t packagenum;
        uint16_t reserve;
}__attribute__((packed)) jt_apota_package_t;

typedef struct
{
        uint8_t start;
        uint8_t len1;
        uint8_t len2;
        uint8_t len3;
        uint16_t packageindex;
        uint8_t data[2048];
        uint16_t data_crc;
}__attribute__((packed)) jt_apota_data_package_t;

typedef struct
{
        uint16_t v1;
        uint16_t v2;
}__attribute__((packed)) jt_receive_version_package_t;

uint16_t CRC16_XMODEM(uint8_t *di, uint32_t len);
uint16_t CRC16_APOTA( unsigned char * pdat, unsigned int len);

int uart_data_send_head_lighton(uart_utils_t *uart,uint8_t wakeup_time,uint16_t tag_num);
int uart_data_send_head_lableup(uart_utils_t *uart,uint8_t wakeup_time,uint16_t tag_num);
int uart_data_send_head_lightonrule(uart_utils_t *uart,uint8_t wakeup_time);
int uart_data_send_head_search(uart_utils_t *uart,uint8_t wakeup_time);

int uart_data_send_lighton(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,uint16_t light_d,
                           uint32_t tag_1,uint32_t tag_2,uint32_t tag_3,uint32_t tag_4,uint32_t tag_5,
                           uint32_t tag_6,uint32_t tag_7,uint32_t tag_8,uint32_t tag_9,uint32_t tag_10,
                           uint32_t tag_11,uint32_t tag_12,uint32_t tag_13,uint32_t tag_14,uint32_t tag_15,
                           uint32_t tag_16,uint32_t tag_17,uint32_t tag_18,uint32_t tag_19,uint32_t tag_20,
                           uint32_t tag_21,uint32_t tag_22,uint32_t tag_23,uint32_t tag_24,uint32_t tag_25,
                           uint32_t tag_26,uint32_t tag_27,uint32_t tag_28,uint32_t tag_29,uint32_t tag_30,
                           uint32_t tag_num);
int uart_data_send_lighton_test(uart_utils_t *uart,uint8_t ledctl,uint16_t flash_i,uint16_t light_d,
                           uint32_t tag_1,uint32_t tag_2,uint32_t tag_3,uint32_t tag_4,uint32_t tag_5,uint32_t tag_num);
int uart_data_send_lable(uart_utils_t *uart, uint32_t tags[], uint8_t lable1[], uint16_t lable2[], uint32_t lable3[], uint32_t tag_num);
int uart_data_send_light_rule(uart_utils_t *uart,uint8_t lable_1,uint16_t lable_2,uint32_t lable_3,
                              uint8_t lable_1_rule, uint8_t lable_2_rule, uint8_t lable_3_rule,
                              uint8_t ledctrl, uint16_t flash_i, uint16_t light_d);
int uart_data_send_search(uart_utils_t *uart,uint8_t lable_1,uint8_t lable_2,uint8_t lable_3,uint8_t lable_4,uint8_t lable_5,
                         uint8_t lable_6,uint8_t lable_7,uint8_t lable_8);

int uart_data_receive_data_back(uart_utils_t *uart,uint16_t *parmAck,uint32_t *tagCode,uint8_t *tagSignal,
                               uint8_t *totalLen,uint8_t *tagFeature,uint8_t *count,uint16_t *batteryV,uint16_t *version,uint8_t *ledCtrl,
                               uint8_t *lable1,uint16_t *lable2,uint32_t *lable3);
int uart_data_receive_ack(uart_utils_t *uart,uint16_t *parm_ack);
int uart_data_send_head_ledota(uart_utils_t *uart,uint32_t datalen,uint16_t datacrc,uint16_t version);
int uart_data_send_ledota(uart_utils_t *uart,char *data,int size);
int uart_data_send_head_apota(uart_utils_t *uart,uint32_t datalen,uint16_t packagenum);
int uart_data_send_apota(uart_utils_t *uart,uint8_t dataori[],int datalen,uint16_t pnum,uint16_t datacrc);
int uart_data_receive_version(uart_utils_t *uart);

int uart_data_send_head_lightband(uart_utils_t *uart, uint8_t wakeup_time, uint16_t tag_num);
int uart_data_send_lightband(uart_utils_t *uart, uint8_t ledctl, uint16_t flash_i, uint16_t light_d, uint32_t tags[], uint8_t tag_leds[], uint16_t tag_num);

#define MAX_LABEL_NUM 30
typedef struct {
    uint16_t len1;
    uint8_t len2;
    struct {
        uint32_t tag;
        uint8_t lable1;
        uint16_t lable2;
        uint32_t lable3;
    } items[MAX_LABEL_NUM];
    uint16_t crc;
} __attribute__((packed)) jt_lable_ledx_package_t;

int uart_data_send_head_lableup(uart_utils_t *uart, uint8_t wakeup_time, uint16_t tag_num);
int uart_data_send_lable(uart_utils_t *uart, uint32_t tags[], uint8_t lable1[], uint16_t lable2[], uint32_t lable3[], uint32_t tag_num);
#endif
