164 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			164 lines
		
	
	
		
			5.6 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_188_H
 | ||
|  | #define PROTO_188_H
 | ||
|  | 
 | ||
|  | #include "os_types_api.h"
 | ||
|  | #include "iot_utils_api.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | /* pack for the structures in the whole file */ | ||
|  | #pragma pack(push)  /* save the pack status */
 | ||
|  | #pragma pack(1)     /* 1 byte align */
 | ||
|  | 
 | ||
|  | /* frame start char definition */ | ||
|  | #define PROTO_188_START_CHAR                                0x68
 | ||
|  | 
 | ||
|  | /* frame end char definition */ | ||
|  | #define PROTO_188_END_CHAR                                  0x16
 | ||
|  | 
 | ||
|  | /* device type */ | ||
|  | #define PROTO_188_DEV_TYPE_COLD_WATER                       0x10
 | ||
|  | #define PROTO_188_DEV_TYPE_HOT_WATER                        0x11
 | ||
|  | #define PROTO_188_DEV_TYPE_DRINK_WATER                      0x12
 | ||
|  | #define PROTO_188_DEV_TYPE_RECLAIMED_WATER                  0x13
 | ||
|  | #define PROTO_188_DEV_TYPE_HEAT_CAPACITY                    0x20
 | ||
|  | #define PROTO_188_DEV_TYPE_COOLING_CAPACITY                 0x21
 | ||
|  | #define PROTO_188_DEV_TYPE_GAS                              0x30
 | ||
|  | #define PROTO_188_DEV_TYPE_OTHERS                           0x40
 | ||
|  | 
 | ||
|  | /* control function code definition for CJ/T 188-2004 */ | ||
|  | #define PROTO_188_2004_FN_RESERVED                          0x00
 | ||
|  | #define PROTO_188_2004_FN_READ_DATA                         0x01
 | ||
|  | #define PROTO_188_2004_FN_READ_ADDR                         0x03
 | ||
|  | #define PROTO_188_2004_FN_WRITE_DATA                        0x04
 | ||
|  | #define PROTO_188_2004_FN_READ_KEY_VERSION                  0x05
 | ||
|  | #define PROTO_188_2004_FN_WRITE_ADDR                        0x15
 | ||
|  | #define PROTO_188_2004_FN_WRITE_SYNCHRONIZATION             0x16
 | ||
|  | 
 | ||
|  | /* communication state */ | ||
|  | #define PROTO_188_COMM_STATE_NORMAL                         (1)
 | ||
|  | 
 | ||
|  | /* Message direction: master->slave */ | ||
|  | #define PROTO_188_DIR_MASTER                                (0)
 | ||
|  | /* Message direction: slave->master */ | ||
|  | #define PROTO_188_DIR_SLAVE                                 (1)
 | ||
|  | 
 | ||
|  | /* CJ/T 188-2004 mac address length */ | ||
|  | #define PROTO_188_ADDR_LEN                                  (7)
 | ||
|  | 
 | ||
|  | /* meter time length */ | ||
|  | #define PROTO_188_METER_TIME_LEN                            (6)
 | ||
|  | 
 | ||
|  | /* meter accumulative value length */ | ||
|  | #define PROTO_188_METER_ACC_VAL_LEN                         (4)
 | ||
|  | 
 | ||
|  | /* water meter report cmd type */ | ||
|  | #define PROTO_188_WATER_CMD                                 (0xC6)
 | ||
|  | 
 | ||
|  | typedef struct _proto_188_ctrl { | ||
|  |     /* function code. see PROTO_188_2004_FN_XXX */ | ||
|  |     uint8_t     fn                  :6, | ||
|  |     /* communication state, see PROTO_188_COMM_STATE_XXX */ | ||
|  |                 comm_flag           :1, | ||
|  |     /* direction. see PROTO_188_DIR_XXX */ | ||
|  |                 dir                 :1; | ||
|  | } proto_188_ctrl_t; | ||
|  | 
 | ||
|  | typedef struct _proto_188_header { | ||
|  |     /* start char, see PROTO_188_START_CHAR */ | ||
|  |     uint8_t                 start_char; | ||
|  |     /* device type, see PROTO_188_DEV_TYPE_XXX */ | ||
|  |     uint8_t                 dev_type; | ||
|  |     /* target mac address, little endian */ | ||
|  |     uint8_t                 addr[PROTO_188_ADDR_LEN]; | ||
|  |     /* control code */ | ||
|  |     proto_188_ctrl_t        control; | ||
|  |     /* data length */ | ||
|  |     uint8_t                 len; | ||
|  |     /* cmd type */ | ||
|  |     uint8_t                 cmd; | ||
|  |     /* data */ | ||
|  |     uint8_t                 data[0]; | ||
|  | } proto_188_header_t; | ||
|  | 
 | ||
|  | typedef struct _proto_188_tailer { | ||
|  |     /* reserved */ | ||
|  |     uint8_t    rsvd; | ||
|  |     /* check by accumulation sum */ | ||
|  |     uint8_t    cs; | ||
|  |     /* end char. see PROTO_188_END_CHAR */ | ||
|  |     uint8_t    end_char; | ||
|  | } proto_188_tailer_t; | ||
|  | 
 | ||
|  | typedef struct _proto_188_lp_meter_report_data { | ||
|  |     /* meter time */ | ||
|  |     uint8_t     meter_time[PROTO_188_METER_TIME_LEN]; | ||
|  |     /* meter type */ | ||
|  |     uint8_t     meter_type; | ||
|  |     /* accumulative value */ | ||
|  |     uint8_t     acc_value[PROTO_188_METER_ACC_VAL_LEN]; | ||
|  |     /* statistical time */ | ||
|  |     uint8_t     stat_time; | ||
|  |     /* last month accumulative value */ | ||
|  |     uint8_t     last_mon_acc_value[PROTO_188_METER_ACC_VAL_LEN]; | ||
|  |     /* meter undervoltage */ | ||
|  |     uint8_t     uvp:1, | ||
|  |     /* reserved */ | ||
|  |                 rsvd:7; | ||
|  |     /* release date */ | ||
|  |     uint8_t     release_date; | ||
|  | } proto_188_lp_meter_report_data_t; | ||
|  | 
 | ||
|  | #pragma pack(pop)   /* restore the pack status */
 | ||
|  | 
 | ||
|  | /* proto_188_calc_cs() - calculate checksum for 188  protocol
 | ||
|  |  * @app_id:     pointer to the 188 protocol header | ||
|  |  * | ||
|  |  * return | ||
|  |  *  calculated checksum value | ||
|  |  */ | ||
|  | uint8_t proto_188_calc_cs(proto_188_header_t *hdr); | ||
|  | 
 | ||
|  | /* proto_188_format_check() - 188 protocol format check
 | ||
|  |  * @data:   pointer to protocol data buffer | ||
|  |  * @len:    data length | ||
|  |  * @dir:    message direction--see PROTO_188_DIR_XXX | ||
|  |  * return | ||
|  |  *    NULL      -- Incorrect protocol format. | ||
|  |  *    otherwise -- pointer to the 188 protocol header | ||
|  |  */ | ||
|  | proto_188_header_t *proto_188_format_check(uint8_t *data, | ||
|  |     uint32_t len, uint32_t dir); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief:           check if the mac is 188 broadcast mac | ||
|  |  * @param dst:       the mac address to be checked | ||
|  |  * @return           1 is 188 broadcast mac | ||
|  |  * @return           0 NOT a 188 broadcast mac | ||
|  |  */ | ||
|  | uint8_t proto_188_is_bcast(uint8_t* dst); | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* PROTO_188_H */
 | ||
|  | 
 |