/*
 * @Author: zhanghongyang zhanghy@bmser.com
 * @Date: 2023-02-14 09:23:30
 * @LastEditors: qianziyang qianzy@bmser.com
 * @LastEditTime: 2023-09-15 17:08:54
 * @FilePath: /lede-sdk-imx6_gcc-5.4.0_musl-1.1.16_eabi.Linux-x86_64/package/xieneng_can/src/bmser_can_init.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#ifndef BMSER_CAN_INIT_H_
#define BMSER_CAN_INIT_H_


#include <string.h>
#include <syslog.h>
#include "cJSON.h"
#include "can_inf.h"
#include "toolkit.h"
#include "bmser_utils.h"
#include "bmser_log.h"
#include "bmser_mqtt.h"
#include "bmser_dev.h"
#include "bmser_error_code.h"


//新增can_bus配置文件以及相关的配置数据结构
#define CAN_BUS_MAX_REG_READ_CNT    128
#define CAN_BUS_NAME_MAX_LEN        32
#define CAN_BUS_REG_DESC_MAX_LEN    32
#define CAN_BUS_MAX_CAN_PORT        4
#define CAN_BUS_INTERFACE_NAME      "can"
#define BMSER_CAN_BUS_CFG_FILE      "/app/config/bmser/canbus_cfg.json"
#define BMSER_PRO_CFG_FILE          "/app/config/bmser/project_para.json"

typedef enum 
{
    BMSER_BAU_REG = 0,
    BMSER_BCU_REG,
    BMSER_BMU_REG,
    BMSER_BDU_REG,
    BMSER_ENV_REG,
    BMSER_REG_TYPE
} BMSER_REG_TYPE_E;

typedef struct
{
    bool enable;                                                                     //REG配置状态
    BMSER_REG_TYPE_E type;                                                           //REG类型,总控/主控/从控
    char desc[CAN_BUS_REG_DESC_MAX_LEN + 1];                                         //REG描述信息
    uint16_t start;                                                                  //REG起始地址
    uint16_t stop;                                                                   //REG结束地址
    uint16_t period_ms;                                                              //REG读周期                         
} BMSER_CAN_REG_READ_S;

typedef struct
{
    uint8_t cnt;
    BMSER_CAN_REG_READ_S reg_read[CAN_BUS_MAX_REG_READ_CNT + 1]; 
} BMSER_CAN_REG_READ_TABLE_S;

typedef struct 
{
    uint32_t bitrate;                                                                 //CAN波特率
    char name[CAN_BUS_NAME_MAX_LEN + 1];                                              //CAN接口名称
    bool disable;                                                                     //CAN使能状态
    bool filt_en;                                                                     //CAN是否开启滤波
    bool main_can;                                                                    //是否为主CAN
    uint8_t  rack_id_start;                                                           //CAN接入的起始簇id
    uint8_t  rack_id_end;                                                             //CAN接入的结束簇id
    uint8_t  bdu_id_start;                                                            //CAN接入的起始bdu id
    uint8_t  bdu_id_end;                                                              //CAN接入的结束bdu id
    BMSER_CAN_REG_READ_TABLE_S reg_read_table[BMSER_REG_TYPE];                        //CAN读REG信息表
} BMSER_CAN_BUS_CFG_S;

typedef struct
{
    uint8_t j1939_req_enable;                                                         //j1939主动请求开关(1: 使能;0: 关闭)
    uint8_t j1939_send_detail_only;                                                   //j1939仅请求详细信息(1: 使能;0: 关闭)
    uint8_t j1939_send_detail_compress;                                               //j1939详细信息多帧形式请求(1: 多帧;0: 单帧)
    uint8_t j1939_listen_enable;                                                      //j1939接收处理开关(1: 使能;0: 关闭)
} BMSER_CAN_GENERAL_CFG_S;

typedef struct
{
    uint8_t rack_num;                                                                 //配置的簇数量
    uint8_t num;                                                                      //CAN总线个数
    BMSER_CAN_GENERAL_CFG_S general_cfg;                                              //通用配置信息
    BMSER_CAN_BUS_CFG_S can_cfg[CAN_BUS_MAX_CAN_PORT + 1];                            //CAN配置信息
} BMSER_CAN_BUS_CFG_TABLE_S;

/**
 * @description: BMSER CAN 初始化
 * @return {*} 0:success, others:fail.
 */
int bmser_can_init();

/**
 * @description: 获取j1939主动请求开关使能状态
 * @param 
 * @return {*} 0:disable, 1:enable.
 */
bool bmser_general_cfg_j1939_req_enable();

/**
 * @description: 获取j1939接收处理开关使能状态
 * @param 
 * @return {*} 0:disable, 1:enable.
 */
bool bmser_general_cfg_j1939_listen_enable();

/**
 * @description: 判断是否有CAN配置文件 
 * @return {*} true:有, false:无
 */
bool bmser_can_cfg_file_enable();

/**
 * @description: 获取设备的CAN OBJ ID
 * @param {uint8_t} dev_id 设备id
 * @return {*} 设备所在的CAN网口的OBJ ID: success, -1: fail
 */
int bmser_can_get_can_obj_id(uint8_t dev_id);

/**
 * @description: 处理设置项目参数(修改簇数等)
 * @param {char} *payload 消息内容
 * @param {int} payload_len 消息长度
 * @return {*}
 */
int bmser_can_set_mqtt_project_para(const char *payload, int payload_len);

/**
 * @description: 获取CAN CFG TABLES句柄 
 * @return {*} BMSER_CAN_BUS_CFG_TABLE_S句柄
 */
BMSER_CAN_BUS_CFG_TABLE_S* bmser_can_get_cfg_tables_mgr();

/**
 * @description: 获取CFG配置文件配置的CAN个数 
 * @return {*} CAN个数
 */
int bmser_can_get_cfg_can_num();

#endif /* BMSER_CAN_INIT_H_ */
