261 lines
		
	
	
		
			9.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			261 lines
		
	
	
		
			9.7 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_MWC_H
 | |
| #define PROTO_MWC_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 */
 | |
| 
 | |
| /* PHR frame normal sequence number definition */
 | |
| #define PROTO_MWC_PHR_PROTO_VER                             (0x01)
 | |
| 
 | |
| /* PSDU frame control field definition */
 | |
| #define PROTO_MWC_PSDU_CTRL_FIELD                           (0xCD47)
 | |
| 
 | |
| /* PSDU frame PanID definition */
 | |
| #define PROTO_MWC_PSDU_PANID_DEFAULT                        (0xFFFF)
 | |
| 
 | |
| /* PSDU frame config work mode definition */
 | |
| #define PROTO_MWC_PSDU_WORK_MODE_INVALID                    (0)
 | |
| #define PROTO_MWC_PSDU_WORK_MODE_VALID                      (1)
 | |
| 
 | |
| /* PSDU frame config work time definition */
 | |
| #define PROTO_MWC_PSDU_WORK_TIME_DEFAULT                    (0xFFFFFF)
 | |
| 
 | |
| /* default channel */
 | |
| #define PROTO_MWC_PSDU_CHANNEL_DEFAULT                      (0)
 | |
| /* min work channel */
 | |
| #define PROTO_MWC_PSDU_CHANNEL_MIN                          (1)
 | |
| /* max work channel */
 | |
| #define PROTO_MWC_PSDU_CHANNEL_MAX                          (32)
 | |
| 
 | |
| /* MWC mac address length */
 | |
| #define PROTO_MWC_PSDU_ADDR_LEN                             (7)
 | |
| 
 | |
| /* DAU mac address length */
 | |
| #define PROTO_MWC_ADDR_LEN                                  (6)
 | |
| 
 | |
| /* PSDU frame cmd identifier char definition */
 | |
| #define PROTO_MWC_PSDU_CMD_CLCT_RAW                         (0x40)
 | |
| #define PROTO_MWC_PSDU_CMD_LP_METER_ACK                     (0x41)
 | |
| #define PROTO_MWC_PSDU_CMD_FORWARD_TIME                     (0x42)
 | |
| #define PROTO_MWC_PSDU_CMD_FORWARD_DETECT                   (0x43)
 | |
| #define PROTO_MWC_PSDU_CMD_CONFIG_LP_METER                  (0x44)
 | |
| #define PROTO_MWC_PSDU_CMD_CONFIG_ACK                       (0x45)
 | |
| 
 | |
| /* raw read meter frame len except 188 proto data len */
 | |
| #define PROTO_MWC_RAW_MR_FRAME_LEN  (sizeof(proto_mwc_phy_phr_t) + \
 | |
|                                      sizeof(proto_mwc_psdu_header_t) + \
 | |
|                                      sizeof(proto_mwc_psdu_clct_raw_t) + \
 | |
|                                      sizeof(proto_mwc_phy_phr_tail_t))
 | |
| 
 | |
| /* config low power meter frame len include phr psdu and crc */
 | |
| #define PROTO_MWC_CFG_LP_METER_LEN  (sizeof(proto_mwc_phy_phr_t) + \
 | |
|                                      sizeof(proto_mwc_psdu_header_t) + \
 | |
|                                      sizeof(proto_mwc_psdu_cfg_lp_meter_t) + \
 | |
|                                      sizeof(proto_mwc_phy_phr_tail_t))
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_CONFIG_ACK */
 | |
| typedef struct _proto_mwc_psdu_cfg_ack {
 | |
|     /* lp meter network channel id, see PROTO_MWC_PSDU_CHANNEL_DEFAULT */
 | |
|     uint8_t                 channel_id;
 | |
|     /* low power meter software version */
 | |
|     uint8_t                 sw_version;
 | |
|     /* reserved */
 | |
|     uint32_t                reserved;
 | |
| } proto_mwc_psdu_cfg_ack_t;
 | |
| 
 | |
| /* config low power meter real time format */
 | |
| typedef struct _proto_mwc_real_time {
 | |
|     /* second, BCD format */
 | |
|     uint8_t                 sec;
 | |
|     /* min, BCD format */
 | |
|     uint8_t                 min;
 | |
|     /* hour, BCD format */
 | |
|     uint8_t                 hour;
 | |
|     /* week, BCD format */
 | |
|     uint8_t                 week;
 | |
|     /* day, BCD format */
 | |
|     uint8_t                 day;
 | |
|     /* month, BCD format */
 | |
|     uint8_t                 month;
 | |
|     /* year, BCD format */
 | |
|     uint8_t                 year;
 | |
| } proto_mwc_real_time_t;
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_CONFIG_LP_METER */
 | |
| typedef struct _proto_mwc_psdu_cfg_lp_meter {
 | |
|     /* lp meter network channel group, see PROTO_MWC_PSDU_CHANNEL_DEFAULT */
 | |
|     uint8_t                 channel_group;
 | |
|     /* the DAU addr is lp meter father node addr, little-endian */
 | |
|     uint8_t                 dau_mac[PROTO_MWC_ADDR_LEN];
 | |
|     /* PanID */
 | |
|     uint16_t                panid;
 | |
|     /* slot time */
 | |
|     uint16_t                slot_time;
 | |
|     /* real time */
 | |
|     proto_mwc_real_time_t   real_time;
 | |
|     /* work mode, 0 - work time invalid, 1 - work time valid */
 | |
|     uint32_t                work_mode       :8,
 | |
|     /* set working time, 3 byte to 24 bit.
 | |
|      * each bit corresponding to one hour work mode, bit0 is 0 clock.
 | |
|      * 0 - sleep mode, 1 - work mode.
 | |
|      */
 | |
|                             work_time       :24;
 | |
| } proto_mwc_psdu_cfg_lp_meter_t;
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_FORWARD_DETECT */
 | |
| typedef struct _proto_mwc_psdu_forward_detect {
 | |
|     /* bcast sequence number */
 | |
|     uint8_t                 bcast_sn;
 | |
|     /* DAU forward bcast scale */
 | |
|     uint16_t                fwd_bcast_scale;
 | |
|     /* first received the DAU current slot time */
 | |
|     uint16_t                cur_slot;
 | |
|     /* low power meter forward network scale */
 | |
|     uint16_t                fwd_nw_scale;
 | |
|     /* low power meter current slot time */
 | |
|     uint16_t                lp_meter_cur_slot_time;
 | |
| } proto_mwc_psdu_forward_detect_t;
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_FORWARD_TIME */
 | |
| typedef struct _proto_mwc_psdu_forward_time {
 | |
|     /* bcast sequence number */
 | |
|     uint8_t                 bcast_sn;
 | |
|     /* DAU forward bcast scale */
 | |
|     uint16_t                fwd_bcast_scale;
 | |
|     /* current slot time */
 | |
|     uint16_t                cur_slot;
 | |
|     /* low power meter forward network scale */
 | |
|     uint16_t                fwd_nw_scale;
 | |
| } proto_mwc_psdu_forward_time_t;
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_LP_METER_ACK */
 | |
| typedef struct _proto_mwc_psdu_lp_meter_ack {
 | |
|     /* data len */
 | |
|     uint8_t                 data_len;
 | |
|     /* 188 protocol data */
 | |
|     uint8_t                 data[0];
 | |
| } proto_mwc_psdu_lp_meter_ack_t;
 | |
| 
 | |
| /* the cmd type is PROTO_MWC_PSDU_CMD_CLCT_RAW */
 | |
| typedef struct _proto_mwc_psdu_clct_raw {
 | |
|     /* baud rate */
 | |
|     uint8_t                 baud_rate;
 | |
|     /* timeout, unit is 1ms */
 | |
|     uint8_t                 timeout;
 | |
|     /* data len */
 | |
|     uint8_t                 data_len;
 | |
|     /* 188 protocol data */
 | |
|     uint8_t                 data[0];
 | |
| } proto_mwc_psdu_clct_raw_t;
 | |
| 
 | |
| typedef struct _proto_mwc_psdu_data {
 | |
|     /* psdu cmd identifier, see PROTO_MWC_PSDU_CMD_XXX */
 | |
|     uint8_t                 cmd_type;
 | |
|     /* command data */
 | |
|     uint8_t                 data[0];
 | |
| } proto_mwc_psdu_data_t;
 | |
| 
 | |
| typedef struct _proto_mwc_psdu_header {
 | |
|     /* control field, see PROTO_MWC_PSDU_CTRL_FIELD */
 | |
|     uint16_t                control;
 | |
|     /* frame sn */
 | |
|     uint8_t                 sn;
 | |
|     /* PanID, see PROTO_MWC_PSDU_PANID_DEFAULT */
 | |
|     uint16_t                panid;
 | |
|     /* destination mac address, little-endian */
 | |
|     uint8_t                 dest_addr[PROTO_MWC_PSDU_ADDR_LEN];
 | |
|     /* source mac address, little-endian */
 | |
|     uint8_t                 source_addr[PROTO_MWC_PSDU_ADDR_LEN];
 | |
|     /* payload */
 | |
|     proto_mwc_psdu_data_t   payload;
 | |
| } proto_mwc_psdu_header_t;
 | |
| 
 | |
| /* PHR in mwc physical layer transport protocol */
 | |
| typedef struct _proto_mwc_phy_phr {
 | |
|     /* data_len includes PSDU len and PHR len except data_len */
 | |
|     uint8_t                 data_len;
 | |
|     /* channel index, see PROTO_MWC_PSDU_CHANNEL_XXX */
 | |
|     uint8_t                 channel_idx;
 | |
|     /* protocol version id, BCD format, see PROTO_MWC_PHR_PROTO_VER */
 | |
|     uint8_t                 proto_ver;
 | |
|     /* xor check sum three bytes in front */
 | |
|     uint8_t                 phr_cs;
 | |
|     /* PSDU data */
 | |
|     uint8_t                 data[0];
 | |
| } proto_mwc_phy_phr_t;
 | |
| 
 | |
| /* PHR tail is behind PSDU */
 | |
| typedef struct _proto_mwc_phy_phr_tail {
 | |
|     /* CRC-ITU(CRC16-CCITT) check */
 | |
|     uint16_t  crc;
 | |
| } proto_mwc_phy_phr_tail_t;
 | |
| 
 | |
| #pragma pack(pop)   /* restore the pack status */
 | |
| 
 | |
| /**
 | |
|  * @brief:  handle raw read low power meter cmd.
 | |
|  * @param   mr_data:   mwc proto data head.
 | |
|  * @param   data:      raw data of the 188 proto data.
 | |
|  * @param   data_len:  raw data len of the 188 proto data.
 | |
|  * @param   plc_nid:   hplc net snid.
 | |
|  * @param   lp_meter_mac: lp meter mac, little-Endian,
 | |
|  *                     length is PROTO_MWC_PSDU_ADDR_LEN.
 | |
|  * @param   dau_mac:   iic access address, little-Endian,
 | |
|  *                     length is PROTO_MWC_ADDR_LEN.
 | |
|  * @return  ERR_OK - success, ERR_FAIL - data_len invalid.
 | |
|  */
 | |
| uint32_t proto_mwc_handle_raw_mr_lp_meter(uint8_t *mr_data, uint8_t *data,
 | |
|     uint16_t data_len, uint32_t plc_nid, uint8_t *lp_meter_mac,
 | |
|     uint8_t *dau_mac);
 | |
| 
 | |
| /**
 | |
|  * @brief:  handle config low power meter cmd.
 | |
|  * @param   cfg_data:  mwc proto data head.
 | |
|  * @param   plc_nid:   hplc net snid.
 | |
|  * @param   panid:     unique network id.
 | |
|  * @param   slot_id:   low power meter module slot time id.
 | |
|  * @param   lp_meter_mac: lp meter mac, little-Endian,
 | |
|  *                     length is PROTO_MWC_PSDU_ADDR_LEN.
 | |
|  * @param   dau_mac:   iic access address, little-Endian,
 | |
|  *                     length is PROTO_MWC_ADDR_LEN.
 | |
|  */
 | |
| void proto_mwc_handle_cfg_lp_meter(uint8_t *cfg_data, uint32_t plc_nid,
 | |
|     uint16_t panid, uint16_t slot_id, uint8_t *lp_meter_mac, uint8_t *dau_mac);
 | |
| 
 | |
| /*
 | |
|  * @brief:  get psdu_data pointer and length check.
 | |
|  * @param   len: data len.
 | |
|  * @return: psdu data pointer, NULL - get psdu data fail.
 | |
|  */
 | |
| proto_mwc_psdu_data_t *proto_mwc_get_psdu_data(uint8_t *cfg_ack_data,
 | |
|     uint32_t len);
 | |
| 
 | |
| #ifdef __cplusplus
 | |
| }
 | |
| #endif
 | |
| 
 | |
| #endif /* PROTO_MWC_H */
 |