#ifndef __CAN_DRIVER_H__ // ֹظͷļ
#define __CAN_DRIVER_H__

#include "daemon.h"
#include "bsp_can.h" //  BSP弶ְ֧ص CAN ͷļ

#define MX_FDCAN_COMM_COUNT 4 // ע⸺,һϲҪع

#define FDCAN_COMM_MAX_BUFFSIZE 200 // /ֽ,Ӵֵ
#define FDCAN_COMM_HEADER 's'       // ֡ͷ
#define FDCAN_COMM_TAIL 'e'         // ֡β
#define FDCAN_COMM_OFFSET_BYTES 4   // 's'+ datalen + crc8 + 'e'
#pragma pack(1)
/* CAN comm ṹ, ӵCAN commappӦðһCAN commָ */
typedef struct
{
  FDCANInstance *can_ins;
  /* Ͳ */
  uint8_t send_data_len;                                                  // ݳ
  uint8_t send_buf_len;                                                   // ͻ,Ϊݳ+֡ͷݳ֡βԼУ(4)
  uint8_t raw_sendbuf[FDCAN_COMM_MAX_BUFFSIZE + FDCAN_COMM_OFFSET_BYTES]; // 4bytes֡ͷ֡βdatalenУ
  /* ղ */
  uint8_t recv_data_len;                                                  // ݳ
  uint8_t recv_buf_len;                                                   // ջ,Ϊݳ+֡ͷݳ֡βԼУ(4)
  uint8_t raw_recvbuf[FDCAN_COMM_MAX_BUFFSIZE + FDCAN_COMM_OFFSET_BYTES]; // 4bytes֡ͷ֡βdatalenУ
  uint8_t unpacked_recv_data[FDCAN_COMM_MAX_BUFFSIZE];                    // ,FDCANCommGet()castɶӦָͨȡ
  /* պ͸±־λ*/
  uint8_t recv_state;   // ״̬,
  uint8_t cur_recv_len; // ǰѾյݳ(֡ͷ֡βdatalenУ)
  uint8_t update_flag;  // ݸ±־λ,յʱ,Ὣ˱־λ1,FDCANCommGet()Ὣ˱־λ0

  DaemonInstance *comm_daemon;
} FDCANCommInstance;
#pragma pack()

/* CAN comm ʼṹ */
typedef struct
{
  FDCAN_Init_Config_s can_config; // CANʼṹ
  uint8_t send_data_len;          // ݳ
  uint8_t recv_data_len;          // ݳ
  uint16_t daemon_count;          // ػ̼,ڳʼػ
} FDCANComm_Init_Config_s;

/**
 * @brief ʼFDCANComm
 *
 * @param config FDCANCommʼṹ
 * @return FDCANCommInstance*
 */
FDCANCommInstance *FDCANCommInit(FDCANComm_Init_Config_s *comm_config);

/**
 * @brief ͨFDCANComm
 *
 * @param instance FDCANCommʵ
 * @param data ע˵ַЧݳҪͳʼʱdatalenͬ
 */
void FDCANCommSend(FDCANCommInstance *instance, uint8_t *data);

/**
 * @brief ȡCANCommյ,ҪԼʹǿתصvoidָתָ
 *
 * @return void* صָ
 * @attention עϣֱͨתָ,unionstruct,ҪǷʹpack(n)
 *            CANCommյݿԿpack(1)֮,ŵ.
 *            ʹpack(n)ܻᵼݴ,޷ʹǿתͨmemcpyֱӷ,תҪֶ.
 *            ǿҽͨCANCommʹpack(1)
 */
void *FDCANCommGet(FDCANCommInstance *instance);

/**
 * @brief CANCommǷ
 *
 * @param instance
 * @return uint8_t
 */
uint8_t FDCANCommIsOnline(FDCANCommInstance *instance);

#endif // __CAN_DRIVER_H__
