303 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			303 lines
		
	
	
		
			9.1 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 _IOT_MODBUS_EXT_H_
 | |
| #define _IOT_MODBUS_EXT_H_
 | |
| 
 | |
| #include "iot_utils_api.h"
 | |
| 
 | |
| #ifdef __cplusplus
 | |
|     extern "C" {
 | |
| #endif
 | |
| 
 | |
| #define big_little_swap16(d) ((((uint16_t)(d) & 0xff00) >> 8) | \
 | |
|                                  (((uint16_t)(d) & 0x00ff) << 8))
 | |
| 
 | |
| #pragma pack(push)  /* save the pack status */
 | |
| #pragma pack(1)     /* 1 byte align         */
 | |
| 
 | |
| /** fn code enum */
 | |
| typedef enum _modbus_fn_code_e {
 | |
|     /* have to stay at the front */
 | |
|     MODBUS_EXT_FNCODE_MIN                     = 0x00,
 | |
|     /** modbus ext query cmd fn */
 | |
|     MODBUS_EXT_QUERY_FN                       = 0x03,
 | |
|     /** modbus ext set single register cmd fn */
 | |
|     MODBUS_EXT_SET_SINGLE_REG_FN              = 0x06,
 | |
|     /** modbus ext set multi register cmd fn */
 | |
|     MODBUS_EXT_SET_MULTI_REG_FN               = 0x10,
 | |
| 
 | |
|     MODBUS_EXT_FNCODE_MAX                     = 0x10,
 | |
| } modbus_fn_code_e;
 | |
| 
 | |
| /* fn = MODBUS_EXT_SET_SINGLE_REG_CMD
 | |
|  * subfn code is mapping to register address.
 | |
|  */
 | |
| enum modbus_set_single_ra_e {
 | |
|     /** set adapter id cmd subfn */
 | |
|     AID_SET_RA                             = 0x1000,
 | |
|     /** set slaver address cmd subfn */
 | |
|     SA_SET_RA                              = 0x1002,
 | |
|     /** set white list cmd subfn */
 | |
|     WL_SET_RA                              = 0x1004,
 | |
|     /** set gpio cmd subfn */
 | |
|     GPIO_SET_BASE_RA                       = 0x6e00,
 | |
| };
 | |
| 
 | |
| /* fn = MODBUS_EXT_SET_MULTI_REG_CMD */
 | |
| enum modbus_set_multi_ra_e {
 | |
|     /** add item in white list */
 | |
|     WL_ADD_ITEM_SET_RA                       = 0x1008,
 | |
|     /** remove item from white list */
 | |
|     WL_RM_ITEM_SET_RA                        = 0x1010,
 | |
| 
 | |
| };
 | |
| 
 | |
| /* fn = MODBUS_EXT_QUERY_CMD */
 | |
| enum modbus_query_ra_e {
 | |
|     /** query adapter id cmd subfn */
 | |
|     AID_QUERY_RA                             = 0x1000,
 | |
|     /** query slaver address cmd subfn */
 | |
|     SA_QUERY_RA                              = 0x1002,
 | |
|     /** query item count from white list cmd subfn */
 | |
|     WL_ITEM_COUNT_QUERY_RA                   = 0x1006,
 | |
|     /** query item from white list cmd subfn */
 | |
|     WL_ITEM_QUERY_BASE_RA                    = 0x6000,
 | |
|     /** query gpio cmd subfn */
 | |
|     GPIO_QUERY_BASE_RA                       = 0x6e00,
 | |
| };
 | |
| 
 | |
| /* modbus cmd tansmit direction */
 | |
| typedef enum _mb_transmit_direction_e {
 | |
|     MODBUS_CMD_FROM_UART,
 | |
|     MODBUS_CMD_FROM_PLC,
 | |
| } mb_transmit_direction_e;
 | |
| 
 | |
| /* modbus ext protocol frame header */
 | |
| typedef struct _mb_extend_frm_hdr_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t          sa;
 | |
|     /* command code. */
 | |
|     uint8_t          fn;
 | |
|     /* register address. */
 | |
|     uint16_t         reg_addr;
 | |
| } mb_extend_fn_hdr_t;
 | |
| 
 | |
| typedef struct _mb_ext_cmd_current_t {
 | |
|     mb_extend_fn_hdr_t       hdr;
 | |
|     /* the data of current cmd */
 | |
|     uint16_t                 data;
 | |
|     mb_transmit_direction_e  dir;
 | |
| } mb_ext_cmd_current_t;
 | |
| 
 | |
| /* modbus ext protocol frame tail */
 | |
| typedef struct _mb_ext_frm_tail_t {
 | |
|     /* crc16 for modbus ext frame */
 | |
|     uint16_t  check_sum;
 | |
| } mb_ext_frm_tail_t;
 | |
| 
 | |
| // set single register sub fn.
 | |
| 
 | |
| typedef struct _mb_ext_frame_aid_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* adapter id. */
 | |
|     uint16_t               adapt_id;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_aid_set_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_sa_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* slaver address. */
 | |
|     uint16_t               slaver_addr;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_sa_set_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* 00:disable wl, 01:enable wl. */
 | |
|     uint16_t               enable_wl;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_set_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_gpio_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* 0:low level, 1:high level. */
 | |
|     uint16_t               gpio_value;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_gpio_set_t;
 | |
| 
 | |
| // set multi register sub fn.
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_add_item_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     uint16_t               reg_count;
 | |
|     uint8_t                data_len;
 | |
|     uint8_t                mac_addr[IOT_MAC_ADDR_LEN];
 | |
|     uint16_t               slaver_addr;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_add_item_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_rm_item_set_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     uint16_t               reg_count;
 | |
|     uint8_t                data_len;
 | |
|     uint8_t                mac_addr[IOT_MAC_ADDR_LEN];
 | |
|     uint16_t               slaver_addr;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_rm_item_t;
 | |
| 
 | |
| // query sub fn.
 | |
| 
 | |
| typedef struct _mb_ext_frame_aid_query_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_aid_query_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_sa_query_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_sa_query_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_item_count_query_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_query_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_item_query_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_item_query_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_gpio_query_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_gpio_query_t;
 | |
| 
 | |
| // resp
 | |
| 
 | |
| typedef struct _mb_ext_frame_set_resp_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* data of set to resp */
 | |
|     uint16_t               data;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_set_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_aid_query_resp_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t                sa;
 | |
|     /* command code. */
 | |
|     uint8_t                fn;
 | |
|     /* bytes num of data */
 | |
|     uint8_t                data_len;
 | |
|     /* adapter id. */
 | |
|     uint16_t               adapt_id;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_aid_query_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_sa_query_resp_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t                sa;
 | |
|     /* command code. */
 | |
|     uint8_t                fn;
 | |
|     /* bytes num of data */
 | |
|     uint8_t                data_len;
 | |
|     uint16_t               slaver_addr;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_sa_query_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_item_count_query_resp_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t                sa;
 | |
|     /* command code. */
 | |
|     uint8_t                fn;
 | |
|     /* bytes num of data */
 | |
|     uint8_t                data_len;
 | |
|     uint16_t               item_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_item_count_query_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_item_query_resp_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t                sa;
 | |
|     /* command code. */
 | |
|     uint8_t                fn;
 | |
|     /* bytes num of data */
 | |
|     uint8_t                data_len;
 | |
|     uint8_t                mac_addr[IOT_MAC_ADDR_LEN];
 | |
|     uint16_t               slaver_addr;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_item_query_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_gpio_query_resp_t {
 | |
|     /* slaver address. */
 | |
|     uint8_t                sa;
 | |
|     /* command code. */
 | |
|     uint8_t                fn;
 | |
|     /* bytes num of data */
 | |
|     uint8_t                data_len;
 | |
|     /* value of the gpio */
 | |
|     uint16_t               gpio_value;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_gpio_query_resp_t;
 | |
| 
 | |
| typedef struct _mb_ext_frame_wl_item_set_resp_t {
 | |
|     mb_extend_fn_hdr_t     hdr;
 | |
|     /* count of register to read */
 | |
|     uint16_t               reg_count;
 | |
|     mb_ext_frm_tail_t   tail;
 | |
| } mb_ext_frame_wl_item_set_resp_t;
 | |
| 
 | |
| /* Gree Fn Code handle. Each Code should define itself function*/
 | |
| typedef bool_t(* MODBUS_FN_HANDLE)(uint8_t *data, uint8_t len, \
 | |
|                                         mb_transmit_direction_e dir);
 | |
| 
 | |
| /* Modbus function code table */
 | |
| typedef struct _mb_fnhdl_tbl_t
 | |
| {
 | |
|     /*gree function code*/
 | |
|     uint32_t cmdid;
 | |
|     /*each code handler*/
 | |
|     MODBUS_FN_HANDLE cmd_handle_fn;
 | |
|     /*each fn handle statistic */
 | |
|     uint32_t cmd_num;
 | |
| } mb_fnhdl_tbl_t;
 | |
| 
 | |
| #pragma pack(pop)
 | |
| 
 | |
| uint8_t iot_mb_ext_cmd_handler(uint8_t * data, uint16_t len,
 | |
|     mb_transmit_direction_e dir);
 | |
| 
 | |
| void iot_mb_ext_resp_cmd_timeout_or_failed();
 | |
| 
 | |
| uint8_t iot_mb_ext_handle_resp_from_ge(iot_pkt_t *p_pkt_frame);
 | |
| 
 | |
| #ifdef __cplusplus
 | |
| }
 | |
| #endif
 | |
| 
 | |
| #endif /* _IOT_MODBUS_EXT_H_ */
 | |
| 
 | |
| 
 |