Files
kunlun/app/iot_sniffer/inc/iot_proto_ge.h
2024-09-28 14:24:04 +08:00

257 lines
7.9 KiB
C
Executable File

/****************************************************************************
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_PROTO_GE_H
#define IOT_PROTO_GE_H
/* common includes */
#include "iot_utils_api.h"
#ifdef __cplusplus
extern "C" {
#endif
/* preamble code of gree frame */
#define GE_FRM_PREAMBLE_CODE (0xAAAA)
/* tail code of gree frame */
#define GE_FRM_TAIL_CODE (0xFF)
#define GE_FRM_MIN_LEN (10)
#define GE_FRM_PRE_LEN (2)
#define GE_FRM_SEQ_LEN (1)
#define GE_FRM_CHECKSUM_LEN (2)
#define GE_FRM_TAIL_LEN (1)
/* the position of data length field in a intact frame
* offset start with 0
*/
#define GE_FRM_DATA_CNT_FIELD_POS (2)
#define GE_FRM_NORMAL_SEQ_POS (7)
/* the position of check sum field in a intact frame
* offset start with data cnt
*/
#define GE_FRM_CHECK_SUM_FIELD_POS (7)
#define GE_FRM_PLD_MIN_LEN (0x0)
#define GE_FRM_PLD_MAX_LEN (0x80)
/** check gree frame command */
#define GE_CHECK_FRM_CMD(control) (control >= GE_FRM_PLD_MIN_LEN && \
control <= GE_FRM_PLD_MAX_LEN)
/* the real data length */
#define ACQUIRE_DATA_LEN(data_len) (data_len - GE_FRM_PLD_MIN_LEN)
/************************************extended fn define***********************/
/* fill gree extended fn frame */
#define EXT_FN_FRM_PREPARE(cmd, ext_fn, sub_fn) do { \
cmd->hdr.hdr.preamble = GE_FRM_PREAMBLE_CODE; \
cmd->hdr.hdr.data_len = sizeof(*cmd) - \
GE_FRM_MIN_LEN + GE_FRM_PLD_MIN_LEN; \
cmd->hdr.hdr.fn = ext_fn; \
cmd->hdr.subfn = sub_fn; \
cmd->tail.tail = GE_FRM_TAIL_CODE; \
}while(0)
enum proto_cmd_cnf_reason_e {
PROTO_REASON_OK,
PROTO_REASON_STA_LEAVE_ERR,
PROTO_REASON_WL_LOST_FRM,
PROTO_REASON_WL_SEQ_MISSMATCH,
PROTO_REASON_WL_CLR_NOTSUPPORT,
PROTO_REASON_PAIR_TBL_LOST_FRM,
PROTO_REASON_PAIR_TBL_SEQ_MISSMATCH,
PROTO_REASON_PAIR_TBL_CLR_NOTSUPPORT,
PROTO_REASON_NID_DUP,
PROTO_REASON_TIME_OUT,
PROTO_REASON_MAC_MISSMATCH,
PROTO_REASON_PWD_MISSMATCH, //11
PROTO_REASON_JOINED,
PROTO_REASON_NO_AUTHORITY,
PROTO_REASON_LEN_MISSMATCH,
PROTO_REASON_NID_MISSMATCH,
PROTO_REASON_CCO_GROUPNETING,
PROTO_REASON_CCO_GROUPNETING_IDLE,
PROTO_REASON_ERR_DEVTYPE,
PROTO_REASON_MAC_ADDR_ZERO,
PROTO_REASON_NO_MAC_SETTED, //20
PROTO_REASON_DUP_SETTING,
PROTO_REASON_ROLE_MISMATCH,
PROTO_REASON_CMD_DIR_ERR,
PROTO_REASON_NOT_READY,
PROTO_REASON_ALLOC_MEM_ERR,
PROTO_REASON_NO_NB_NW_FOUND,
PROTO_REASON_MONITOR_EXCESS,
PROTO_REASON_PARAME_ERR,
/* out of range */
PROTO_REASON_BAUD_IDX_OOR,
PROTO_REASON_SWITCH_UART_MODE_ERR,
PROTO_REASON_SET_HOST_PORT_ERR,
PROTO_REASON_NOT_SUPPORT,
PROTO_REASON_NO_IP4_SETTED,
PROTO_REASON_IP4_IS_INVALID,
PROTO_REASON_MAX,
};
/** fn code enum */
typedef enum _proto_fn_code_e
{
/* have to stay at the front */
PROTO_FNCODE_MIN = 0x00,
/** general query cmd fn */
PROTO_GE_PLC_QUERY_CMD = 0xFC,
/** general response cmd fn */
PROTO_GE_PLC_RESP_CMD = 0xFD,
/** general set cmd fn */
PROTO_GE_PLC_SET_CMD = 0xFE,
PROTO_FNCODE_MAX = 0xFF,
}proto_fn_code_e;
/* fn = PROTO_GE_PLC_SET_CMD */
enum proto_set_subfn_e {
/* cmd confirm cmd from outair to monitor */
PROTO_CMD_CFM_CMD = 0x14,
/* set work band */
PROTO_WORK_BAND_SET_CMD = 0x1C,
};
/* fn = PROTO_GE_PLC_RESP_CMD */
enum proto_query_subfn_e {
/* MCU query work band */
PROTO_WORK_BAND_QUERY_CMD = 0x16,
};
/* fn = PROTO_GE_PLC_RESP_CMD */
enum proto_query_response_subfn_e {
/* work band response to MCU */
PROTO_WORK_BAND_RESP_CMD = 0x16,
};
/* data or cmd tansmit direction */
typedef enum _transmit_direction_e {
/* cmd - (mcu->)protocol->plctxrx */
CMD_LOCAL_DOWN_LINK,
/* cmd - plctxrx->protocol(->mcu) */
CMD_LOCAL_UP_LINK,
} transmit_direction_e;
typedef uint8_t transmit_direction_e_t;
#pragma pack(push) // save the pack status
#pragma pack(1) // 1 byte align
/* msdu header at begging of msdu payload. */
typedef struct _msdu_pkt_hdr {
/* 0 means, this is msdu.
* non-zero means, remote peer msdu received confirmation seq.
*/
uint16_t recvconf_seq;
/* msdu sent seq */
uint16_t msdusend_seq;
/* the total cnt of sta that will receive the pkt */
uint8_t dest_cnt;
/* indicating if confirmation is necessary for receivers*/
uint8_t need_ack : 1,
aes_encrypt : 1,
aes_padding : 4,
resv : 2;
/* identify msdu pkt comes from proto or at moudle */
uint8_t src_module;
/* below mac are for cco forward msdu from sta to sta */
/* src mac */
uint8_t src_mac[IOT_MAC_ADDR_LEN];
/* dst mac */
uint8_t dst_mac[IOT_MAC_ADDR_LEN];
/* org_mac */
uint8_t org_mac[IOT_MAC_ADDR_LEN];
} msdu_pkt_hdr_t;
/* gree protocol frame header */
typedef struct _ge_frm_hdr_t {
/* preable code 0xAAAA */
uint16_t preamble;
/* data length range:[0x80,0x88] */
uint8_t data_len;
/* function code */
uint8_t fn;
} ge_frm_hdr_t;
/* gree protocol frame header */
typedef struct _ge_extend_fn_hdr_t {
ge_frm_hdr_t hdr;
/* sub function code */
uint8_t subfn;
} ge_extend_fn_hdr_t;
typedef struct _ge_frm_tail_t {
/* crc16 for gree frame */
uint16_t check_sum;
/* tail(0xFF)code of gree frame */
uint8_t tail;
} ge_frm_tail_t;
/* subfn = PROTO_CMD_CFM_CMD */
typedef struct _ge_frame_cmd_cfm_set_subfn20_t {
ge_extend_fn_hdr_t hdr;
uint8_t cmd;
uint8_t resv1;
uint8_t seq;
uint8_t result;
uint8_t reason;
ge_frm_tail_t tail;
} ge_frame_cmd_cfm_set_subfn20_t;
/* subfn = PROTO_WORK_BAND_RESP_CMD */
typedef struct _ge_frame_work_band_resp_subfn22_t {
ge_extend_fn_hdr_t hdr;
uint8_t resv0;
uint8_t resv1;
uint8_t seq;
/* work band */
uint8_t band;
ge_frm_tail_t tail;
} ge_frame_work_band_resp_subfn22_t;
#pragma pack(pop)
/**
* @brief ge_frm_checksum_calc - calculate frame check sum
* @param data: data to check
* @param data: data len
* @retval: verification data for gree frame
*/
uint16_t ge_frm_checksum_calc(uint8_t *data, uint16_t len);
void iot_proto_ge_resp_cfm_frame(uint32_t resp, uint32_t dir, uint32_t cmd,
uint8_t seq);
void iot_proto_ge_set_work_band_handler(uint8_t *data, uint8_t len,
transmit_direction_e_t dir);
void iot_proto_ge_query_work_band_handler(uint8_t *data, uint8_t len,
transmit_direction_e_t dir);
void iot_proto_ge_parse_handle(uint8_t *buffer, uint32_t buffer_len);
void iot_sniffer_fix_send_ge_data(uint8_t *data, uint16_t len);
#ifdef __cplusplus
}
#endif
#endif /* IOT_PROTO_GE_H */