367 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			367 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #ifndef PROTO_ACM_H
 | ||
|  | #define PROTO_ACM_H
 | ||
|  | 
 | ||
|  | /* os shim includes */ | ||
|  | #include "os_types_api.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #define PROTO_ACM_SOF_BYTE             (0x68)
 | ||
|  | #define PROTO_ACM_EOF_BYTE             (0x16)
 | ||
|  | 
 | ||
|  | #define PROTO_ACM_SOF_BYTE_LEN         (1)
 | ||
|  | #define PROTO_ACM_EOF_BYTE_LEN         (1)
 | ||
|  | #define PROTO_ACM_FCS_LEN              (2)
 | ||
|  | 
 | ||
|  | #define PROTO_ACM_PRM_MASTER           (1)
 | ||
|  | #define PROTO_ACM_PRM_SLAVE            (0)
 | ||
|  | 
 | ||
|  | /* transmission direction */ | ||
|  | #define PROTO_ACM_DIR_DOWN_LINK        (0)
 | ||
|  | #define PROTO_ACM_DIR_UP_LINK          (1)
 | ||
|  | 
 | ||
|  | /* control number */ | ||
|  | #define PROTO_ACM_CMD_ACK              (0)
 | ||
|  | #define PROTO_ACM_CMD_NACK             (1)
 | ||
|  | #define PROTO_ACM_CMD_DATA             (2)
 | ||
|  | 
 | ||
|  | /* apdu type */ | ||
|  | #define PROTO_ACM_APDU_TYPE_LINKREQUEST         (1)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_GETREQUEST          (2)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_SETREQUEST          (3)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_REPORTNOTIFICATION  (4)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_LINKRESPONSE        (129)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_GETRESPONSE         (130)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_SETRESPONSE         (131)
 | ||
|  | #define PROTO_ACM_APDU_TYPE_REPORTRESPONSE      (132)
 | ||
|  | 
 | ||
|  | /* acm proto version */ | ||
|  | #define PROTO_ACM_PROTO_VER                     (0x1)
 | ||
|  | 
 | ||
|  | /* concurrent transportation window count */ | ||
|  | #define PROTO_ACM_CON_TRAN_WINDOW_CNT           (1)
 | ||
|  | 
 | ||
|  | /* virtual channel work mode count */ | ||
|  | #define PROTO_ACM_VIRTUAL_CHANNEL_CNT           (2)
 | ||
|  | 
 | ||
|  | /* virtual channel interface type */ | ||
|  | #define PROTO_ACM_VIRTUAL_CHANNEL_TYPE_ACM      (1)
 | ||
|  | #define PROTO_ACM_VIRTUAL_CHANNEL_TYPE_ECM      (2)
 | ||
|  | #define PROTO_ACM_VIRTUAL_CHANNEL_TYPE_HID      (3)
 | ||
|  | 
 | ||
|  | /* virtual channel function configuration */ | ||
|  | #define PROTO_ACM_FUN_CONF_MAG_CHANNEL          (0)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_REM             (1)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_PLC_RF          (2)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_RS485           (3)
 | ||
|  | #define PROTO_ACM_FUN_CONF_TELE_PULSE           (4)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_CAN             (5)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_M_BUS           (6)
 | ||
|  | #define PROTO_ACM_FUN_CONF_CONTROL              (7)
 | ||
|  | #define PROTO_ACM_FUN_CONF_SIMU_COLLECT         (8)
 | ||
|  | #define PROTO_ACM_FUN_CONF_COMM_RS232           (9)
 | ||
|  | 
 | ||
|  | #define PROTO_ACM_INVALID_ACM_SN                (0xFFFF)
 | ||
|  | 
 | ||
|  | /* acm proto module device type length */ | ||
|  | #define PROTO_ACM_MODULE_DEV_TYPE_LEN           (4)
 | ||
|  | /* acm proto module device type */ | ||
|  | #define PROTO_ACM_MODULE_DEV_TYPE               "0001"
 | ||
|  | 
 | ||
|  | /* DTB function info for only read */ | ||
|  | #define PROTO_ACM_DTB_MODULE_INFO               (0x00)
 | ||
|  | #define PROTO_ACM_DTB_LINK_INFO                 (0x01)
 | ||
|  | #define PROTO_ACM_DTB_TRANS_STATUS              (0x02)
 | ||
|  | #define PROTO_ACM_DTB_PROTO_VER                 (0x03)
 | ||
|  | #define PROTO_ACM_DTB_POWER_STATUS              (0x04)
 | ||
|  | 
 | ||
|  | /* DTB function info for read write */ | ||
|  | #define PROTO_ACM_DTB_CLOCK                     (0x00)
 | ||
|  | 
 | ||
|  | /* DTB function info for only write */ | ||
|  | #define PROTO_ACM_DTB_HW_INIT                   (0x00)
 | ||
|  | #define PROTO_ACM_DTB_PARAM_INIT                (0x01)
 | ||
|  | #define PROTO_ACM_DTB_DATA_INIT                 (0x02)
 | ||
|  | #define PROTO_ACM_DTB_FILE_TRANS                (0x03)
 | ||
|  | 
 | ||
|  | /* DTA2 type */ | ||
|  | #define PROTO_ACM_DTA2_ONLY_READ                (0x00)
 | ||
|  | #define PROTO_ACM_DTA2_READ_WRITE               (0x01)
 | ||
|  | #define PROTO_ACM_DTA2_ONLY_WRITE               (0x02)
 | ||
|  | 
 | ||
|  | /* DTA1 type */ | ||
|  | #define PROTO_ACM_DTA1_COMM_INFO                (0x00)
 | ||
|  | 
 | ||
|  | /* data access result */ | ||
|  | /* success */ | ||
|  | #define PROTO_ACM_DAR_OK                        (0)
 | ||
|  | /* reject to operate */ | ||
|  | #define PROTO_ACM_DAR_REJECT                    (1)
 | ||
|  | /* info type undefine */ | ||
|  | #define PROTO_ACM_DAR_UNDEFINE                  (2)
 | ||
|  | /* others */ | ||
|  | #define PROTO_ACM_DAR_OTHERS                    (255)
 | ||
|  | 
 | ||
|  | /* get response result */ | ||
|  | #define PROTO_ACM_GET_RESP_DAR                  (0)
 | ||
|  | #define PROTO_ACM_GET_RESP_DATA                 (1)
 | ||
|  | 
 | ||
|  | /* data struct type */ | ||
|  | #define PROTO_ACM_DATA_TYPE_ARRAY               (1)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_STRUCT              (2)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_UIN32               (6)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_STR                 (10)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_UIN8                (17)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_UIN16               (18)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_ENUM                (22)
 | ||
|  | #define PROTO_ACM_DATA_TYPE_DATE                (26)
 | ||
|  | 
 | ||
|  | /* get response module info struct cnt */ | ||
|  | #define PROTO_ACM_GETRESPONSE_STRUCT_CNT_MOD    (7)
 | ||
|  | /* get response link info struct cnt */ | ||
|  | #define PROTO_ACM_GETRESPONSE_STRUCT_CNT_LINK   (7)
 | ||
|  | 
 | ||
|  | /* virtual channel work mode struct cnt */ | ||
|  | #define PROTO_ACM_VIRTUAL_CHANNEL_STRU_CNT      (2)
 | ||
|  | 
 | ||
|  | #pragma pack(push)      /* save the pack status */
 | ||
|  | #pragma pack(1)         /* 1 byte align */
 | ||
|  | 
 | ||
|  | typedef struct _proto_acm_data_type { | ||
|  |     /* data struct type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           data_type; | ||
|  | } proto_acm_data_type_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_struct_type { | ||
|  |     /* struct type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           type; | ||
|  |     /* count */ | ||
|  |     uint8_t           cnt; | ||
|  | } proto_acm_struct_type_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_data_len { | ||
|  |     /* data struct type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           data_type; | ||
|  |     /* data length */ | ||
|  |     uint8_t           len; | ||
|  | } proto_acm_data_len_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_ctrl { | ||
|  |     /* control number, see PROTO_ACM_CMD_XXX */ | ||
|  |     uint8_t cmd_type : 6, | ||
|  |     /* start flag, see PROTO_ACM_PRM_XXX */ | ||
|  |             prm      : 1, | ||
|  |     /* transmission direction, see PROTO_ACM_DIR_XXX */ | ||
|  |             dir      : 1; | ||
|  |     uint8_t seq; | ||
|  | } proto_acm_ctrl_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_hdr { | ||
|  |     /* start byte */ | ||
|  |     uint8_t           sof_byte; | ||
|  |     /* frame length */ | ||
|  |     uint16_t          len  : 12, | ||
|  |                       rsvd : 4; | ||
|  |     /* control bytes */ | ||
|  |     proto_acm_ctrl_t  ctrl_byte; | ||
|  |     /* apdu data */ | ||
|  |     uint8_t           apdu[0]; | ||
|  | } proto_acm_hdr_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_tail { | ||
|  |     /* fcs */ | ||
|  |     uint16_t          fcs; | ||
|  |     /* eof byte */ | ||
|  |     uint8_t           eof_byte; | ||
|  | } proto_acm_tail_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_apdu_head { | ||
|  |     /* apdu type, see PROTO_ACM_APDU_TYPE_XXX */ | ||
|  |     uint8_t           apdu_type; | ||
|  |     /* apdu data */ | ||
|  |     uint8_t           data[0]; | ||
|  | } proto_acm_apdu_head_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_proto_ver { | ||
|  |     /* proto version number */ | ||
|  |     uint16_t          proto_ver; | ||
|  | } proto_acm_proto_ver_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dev_type { | ||
|  |     /* device type length */ | ||
|  |     uint8_t           len; | ||
|  |     /* device type */ | ||
|  |     uint8_t           data[0]; | ||
|  | } proto_acm_dev_type_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dev_id { | ||
|  |     /* device id length */ | ||
|  |     uint8_t           len; | ||
|  |     /* device id */ | ||
|  |     uint8_t           data[0]; | ||
|  | } proto_acm_dev_id_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dev_param { | ||
|  |     /* max send buffer length */ | ||
|  |     uint16_t          max_send_buf_len; | ||
|  |     /* max recv buffer length */ | ||
|  |     uint16_t          max_recv_buf_len; | ||
|  |     /* concurrent transportation window */ | ||
|  |     uint8_t           con_tran_window; | ||
|  | } proto_acm_dev_param_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dev_param_type { | ||
|  |     /* send buf len data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           send_data_type; | ||
|  |     /* max send buffer length */ | ||
|  |     uint16_t          max_send_buf_len; | ||
|  |     /* recv buf len data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           recv_data_type; | ||
|  |     /* max recv buffer length */ | ||
|  |     uint16_t          max_recv_buf_len; | ||
|  |     /* transportation window data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           window_data_type; | ||
|  |     /* concurrent transportation window */ | ||
|  |     uint8_t           con_tran_window; | ||
|  | } proto_acm_dev_param_type_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_virtual_channel { | ||
|  |     /* virtual channel interface type */ | ||
|  |     uint8_t           interface_type; | ||
|  |     /* virtual channel interface function */ | ||
|  |     uint8_t           function; | ||
|  | } proto_acm_virtual_channel_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_virtual_channel_type { | ||
|  |     /* virtual channel struct type */ | ||
|  |     proto_acm_struct_type_t         stru_type; | ||
|  |     /*  interface type data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t                         int_data_type; | ||
|  |     /* virtual channel interface type */ | ||
|  |     uint8_t                         interface_type; | ||
|  |     /*  function type data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t                         func_data_type; | ||
|  |     /* virtual channel interface function */ | ||
|  |     uint8_t                         function; | ||
|  | } proto_acm_virtual_channel_type_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_virtual_channel_work_mode { | ||
|  |     /* mode count */ | ||
|  |     uint8_t           cnt; | ||
|  |     /* virtual channel paramter */ | ||
|  |     proto_acm_virtual_channel_t channel[0]; | ||
|  | } proto_acm_virtual_channel_work_mode_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_virtual_channel_work_mode_type { | ||
|  |     /* count data type, see PROTO_ACM_DATA_TYPE_XXX */ | ||
|  |     uint8_t           cnt_data_type; | ||
|  |     /* strcut type count */ | ||
|  |     uint8_t           cnt; | ||
|  |     /* virtual channel paramter */ | ||
|  |     proto_acm_virtual_channel_type_t channel[0]; | ||
|  | } proto_acm_virtual_channel_work_mode_type_t; | ||
|  | 
 | ||
|  | typedef struct _acm_reject { | ||
|  |     /* error code */ | ||
|  |     uint16_t   err_code; | ||
|  | } acm_reject_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dt { | ||
|  |     /* DTA2 type, see PROTO_ACM_DTA2_XXX */ | ||
|  |     uint16_t          dta2: 2, | ||
|  |     /* DTA1 type, see PROTO_ACM_DTA1_XXX */ | ||
|  |                       dta1: 6, | ||
|  |     /* DTB function info, see PROTO_ACM_DTB_XXX */ | ||
|  |                       dtb : 8; | ||
|  | } proto_acm_dt_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_dar { | ||
|  |     /* data access result, see PROTO_ACM_DAR_XXX */ | ||
|  |     uint8_t           result; | ||
|  | } proto_acm_dar_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_get_resp { | ||
|  |     /* dt */ | ||
|  |     proto_acm_dt_t    dt; | ||
|  |     /* get response result, see PROTO_ACM_GET_RESP_XXX */ | ||
|  |     uint8_t           ret_type; | ||
|  | } proto_acm_get_resp_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_date { | ||
|  |     /* year, 0xFFFF is invalid */ | ||
|  |     uint16_t          year; | ||
|  |     /* month, 0xFF is invalid */ | ||
|  |     uint8_t           mon; | ||
|  |     /* day of month, 0xFF is invalid */ | ||
|  |     uint8_t           day; | ||
|  |     /* day of week, 0xFF is invalid */ | ||
|  |     uint8_t           week_day; | ||
|  | } proto_acm_date_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_build_info { | ||
|  |     /* version data type */ | ||
|  |     uint8_t           ver_type; | ||
|  |     /* version */ | ||
|  |     uint32_t          ver; | ||
|  |     /* date data type */ | ||
|  |     uint8_t           date_type; | ||
|  |     /* date */ | ||
|  |     proto_acm_date_t  date; | ||
|  | } proto_acm_build_info_t; | ||
|  | 
 | ||
|  | typedef struct _proto_acm_vendor_id { | ||
|  |     /* vendor id length */ | ||
|  |     uint8_t           len; | ||
|  |     /* vendor id */ | ||
|  |     uint8_t           data[0]; | ||
|  | } proto_acm_vendor_id_t; | ||
|  | 
 | ||
|  | #pragma pack(pop)   /* restore the pack status */
 | ||
|  | 
 | ||
|  | void proto_acm_check_frame_handler(uint8_t *buffer, uint32_t buffer_len, | ||
|  |     bool_t *is_frame); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief:         get length to reserve before app data in acm pkt. | ||
|  |  *                 it's the sum of header length and extra headroom. | ||
|  |  * @return:        length of acm UART packet header. | ||
|  |  */ | ||
|  | static inline uint32_t proto_acm_get_rsvd_len() | ||
|  | { | ||
|  |     return sizeof(proto_acm_hdr_t); | ||
|  | } | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief:         get length of iot_pkt to alloc for sending the acm pkt | ||
|  |  *                 to uart. | ||
|  |  * @data_len:      app data len in acm packet | ||
|  |  * return:         length of the acm UART packet | ||
|  |  */ | ||
|  | static inline uint32_t proto_acm_get_pkt_len(uint32_t data_len) | ||
|  | { | ||
|  |     return proto_acm_get_rsvd_len() + data_len + PROTO_ACM_FCS_LEN + | ||
|  |         PROTO_ACM_EOF_BYTE_LEN; | ||
|  | } | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif  /* PROTO_ACM_H */
 |