Files
kunlun/plc/inc/plc_cert_test.h
2024-09-28 14:24:04 +08:00

451 lines
15 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 PLC_CERT_TEST_H
#define PLC_CERT_TEST_H
/* os shim includes */
#include "os_types.h"
#include "iot_config.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 */
/* defining some characteristic parameters of the sg cert test command. */
#define CERT_TEST_PORT_NOR 0x11
#define CERT_TEST_PORT_EXT 0x12
#define CERT_TEST_ID_V 0x0006
#define CERT_TEST_HEADER_LEN 4
#define CERT_TEST_NID (0)
/* not received cert test command, used for g_mt_ctxt->g_mt_mode_sel status */
#define CERT_TEST_CMD_NOT_RX 0
/* cert test command enter app layer transparent transfer tx mode, in this
* mode, will send MSDU to uart.
*/
#define CERT_TEST_CMD_ENTER_APP_T_T 0x01
/* cert test command enter app layer transparent transfer rx mode, in this
* mode, will packing data received from the UART, sent to the PLC network
* as a MSDU.
*/
#define CERT_TEST_CMD_ENTER_APP_T_R 0x02
/* cert test command enter physical layer hplc transparent transfer mode */
#define CERT_TEST_CMD_ENTER_PHY_T 0x03
/* cert test command for enter physical layer hplc loopback mode */
#define CERT_TEST_CMD_ENTER_PHY_LP 0x04
/* cert test command for enter MAC layer transparent transfer mode */
#define CERT_TEST_CMD_ENTER_MAC_T 0x05
/* cert test command for set frequency band */
#define CERT_TEST_CMD_SET_FB 0x06
/* cert test command for set tonemask */
#define CERT_TEST_CMD_SET_TM 0x07
/* rf configuration, including option and channel */
#define CERT_TEST_CMD_SET_RF 0x08
/* cert test command enter physical layer rf loopback mode */
#define CERT_TEST_CMD_ENTER_PHY_RF_LP 0x09
/* cert test command enter physical layer rf transparent transfer mode */
#define CERT_TEST_CMD_ENTER_PHY_RF_T 0x0A
/* physical layer rf and hplc loopback mode */
#define CERT_TEST_CMD_ENTER_PHY_RF_AND_HPLC_LP 0x0B
/* cert test command enter physical layer hplc to rf loopback mode */
#define CERT_TEST_CMD_ENTER_PHY_HPLC2RF_LP 0x0C
/* cert test command enter security test mode */
#define CERT_TEST_CMD_ENTER_SEC 0x0D
/* Define security mode test items */
/* SHA256 algorithm test */
#define CERT_TEST_SEC_MODE_SHA256 1
/* SM3 algorithm test */
#define CERT_TEST_SEC_MODE_SM3 2
/* elliptic encryption algorithm signature test */
#define CERT_TEST_SEC_MODE_ECC_SIG 3
/* elliptic encryption algorithm signature verification test */
#define CERT_TEST_SEC_MODE_ECC_SIG_VERIFY 4
/* SM2 signature test item */
#define CERT_TEST_SEC_MODE_SM2_SIG 5
/* SM2 signature verification test item */
#define CERT_TEST_SEC_MODE_SM2_SIG_VERIFY 6
/* AES-CBC encryption test */
#define CERT_TEST_SEC_MODE_AES_CBC_ENCRYPT 7
/* AES-CBC decyption test */
#define CERT_TEST_SEC_MODE_AES_CBC_DECRYPT 8
/* AES-GCM encryption test */
#define CERT_TEST_SEC_MODE_AES_GCM_ENCRYPT 9
/* AES-GCM decryption test */
#define CERT_TEST_SEC_MODE_AES_GCM_DECRYPT 10
/* SM4 encryption test */
#define CERT_TEST_SEC_MODE_SM4_CBC_ENCRYPT 11
/* SM4 decryption test */
#define CERT_TEST_SEC_MODE_SM4_CBC_DECRYPT 12
/* tonemask ID defined in cert tests */
#define CERT_TEST_TONEMASK_ID_0 0
#define CERT_TEST_TONEMASK_ID_1 1
#define CERT_TEST_TONEMASK_ID_2 2
#define CERT_TEST_TONEMASK_ID_3 3
/* every band has a tone mask */
#define CERT_TEST_TONEMASK_ID_MAX MAX_SG_BAND_ID
#define CERT_TEST_TONEMASK_ID_INVAL 0xFF
/* freq band ID defined in cert tests */
#define CERT_TEST_BAND_ID_0 0
#define CERT_TEST_BAND_ID_1 1
#define CERT_TEST_BAND_ID_2 2
#define CERT_TEST_BAND_ID_3 3
#define CERT_TEST_BAND_ID_MAX MAX_SG_BAND_ID
#define CERT_TEST_BAND_ID_INVAL 0xFF
/* cert test mode duration unit. the unit is 1ms. */
#define CERT_TEST_MODE_DUR_UNIT (60 * 1000)
/* listen for the certification test command till timer expired. this value
* defined the timer interval. once timer expired, local device won't handle
* certification test command anymore. the unit is 1ms.
* note that currently spec said this value should be 30 seconds, while for
* cert test case 3.3.1.8, test bed may send out cert test command a little
* bit layer. so extend this value a little bit.
*/
#define CERT_TEST_CMD_LISTEN_DUR (50 * 1000)
typedef struct _cert_test {
uint8_t port;
uint16_t id;
uint8_t control;
/* protocol version */
uint32_t ver :6,
/* header length */
header_len :6,
/* command type for cert_test, see CERT_TEST_CMD_XXXX */
cmd_type :4,
/* ignore */
resved :4,
/* command parameter, its meaning depends on the type of command */
cmd_param :12;
/* payload label */
uint8_t data[0];
} cert_test_t;
/* cert test command extended information structure*/
typedef struct _cert_test_ext {
/* security mode */
uint8_t sec_mode :4,
/* rf physical header MCS */
phr_mcs :4;
/* rf psdu MCS */
uint8_t psdu_mcs :4,
/* rf pb size */
pb_size :4;
} cert_test_ext_t;
typedef struct _cert_test_sec_key {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_key_t;
typedef struct _cert_test_sec_mactag {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_mactag_t;
typedef struct _cert_test_sec_hash {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_hash_t;
typedef struct _cert_test_sec_result {
/* data length, fixed is 1 */
uint8_t len;
/* result value, 0 - fail, 1 - success */
uint8_t value;
} cert_test_sec_result_t;
typedef struct _cert_test_sec_iv {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_iv_t;
typedef struct _cert_test_sec_public_key_x {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_public_key_x_t;
typedef struct _cert_test_sec_signature_r {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_signature_r_t;
typedef struct _cert_test_sec_signature_s {
/* data length */
uint8_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_signature_s_t;
typedef struct _cert_test_sec_random {
/* data length */
uint16_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_random_t;
typedef struct _cert_test_sec_plaintext {
/* data length */
uint16_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_plaintext_t;
typedef struct _cert_test_sec_ciphertext {
/* data length */
uint16_t len;
/* data buffer */
uint8_t data[0];
} cert_test_sec_ciphertext_t;
/**
* @brief cert_test_is_vaild_sec_mode_test_item() - check whether it is a valid
security mode test item.
* @param item: item code.
* @retval: 1 - valid, 0 - invalid.
*/
static inline uint8_t cert_test_is_vaild_sec_mode_test_item(uint8_t item)
{
if (item >= CERT_TEST_SEC_MODE_SHA256 &&
item <= CERT_TEST_SEC_MODE_SM4_CBC_DECRYPT) {
return 1;
}
return 0;
}
/**
* @brief cert_test_is_mode_enter_cmd() - check if need to enter certification
* test mode.
* @param ct: pointer to certification test message.
*/
#define cert_test_is_mode_enter_cmd(ct) \
((ct)->port == CERT_TEST_PORT_NOR \
&& (ct)->id == CERT_TEST_ID_V \
&& ((ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_T \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_MAC_T \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_FB \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_TM \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_RF \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_T \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_AND_HPLC_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_HPLC2RF_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_SEC))
/* listen for the certification test command till timer expired. this value
* defined the timer interval. once timer expired, local device won't handle
* certification test command anymore. the unit is 1ms.
*/
#define SPG_CERT_TEST_CMD_LISTEN_DUR (30 * 1000)
/* cert test mode duration, when this time expires, it will return from test
* mode to normal mode. uint is 1ms.
*/
#define SPG_CERT_TEST_MODE_DUR (10 * 60 * 1000)
/* define message port number for spg */
#define SPG_CERT_TEST_PORT_NOR 0x11 /* normal */
#define SPG_CERT_TEST_PORT_EXT 0x12 /* extend */
/* defining some characteristic parameters of the spg cert test command. */
#define SPG_CERT_TEST_ID_V 0x0101
#define SPG_CERT_TEST_FRAME_TYPE 2
#define SPG_CERT_TEST_WORK_ID_V 0xf0
/* cert test command ID for enter loopback mode */
#define SPG_CERT_TEST_ID_ENTER_LP 0
/* cert test command ID for enter transparent forwarding mode */
#define SPG_CERT_TEST_ID_ENTER_FWD 1
/* cert test command for set frequency band */
#define SPG_CERT_TEST_ID_SET_FB 2
/* cert test command for dual mode */
#define SPG_CERT_TEST_ID_DUAL_MODE 3
/* tonemask ID defined in cert tests for spg */
#define SPG_CERT_TEST_TONEMASK_ID_0 0
#define SPG_CERT_TEST_TONEMASK_ID_1 1
#define SPG_CERT_TEST_TONEMASK_ID_2 2
#define SPG_CERT_TEST_TONEMASK_ID_MAX SPG_CERT_TEST_TONEMASK_ID_2
#define SPG_CERT_TEST_TONEMASK_ID_INVAL 0xFF
/* freq band ID defined in cert tests for spg */
#define SPG_CERT_TEST_BAND_ID_0 0
#define SPG_CERT_TEST_BAND_ID_1 1
#define SPG_CERT_TEST_BAND_ID_2 2
#define SPG_CERT_TEST_BAND_ID_MAX SPG_CERT_TEST_BAND_ID_2
#define SPG_CERT_TEST_BAND_ID_INVAL 0xFF
/* cert test frame header structure for spg */
typedef struct _spg_cert_test_hdr {
/* message port, see SPG_CERT_TEST_PORT_XXX */
uint8_t port;
/* message id, it must be SPG_CERT_TEST_ID_V. */
uint16_t id;
/* reserved according to spec */
uint8_t rsvd1;
/* frame type, it must be SPG_CERT_TEST_FRAME_TYPE. */
uint16_t f_type :4,
/* reserved according to spec */
rsvd2 :8,
/* flag to mark if have business extension domain, not check */
ext :1,
/* flag to mark if need respond to this message, not check */
resp :1,
/* start flag, not check */
start :1,
/* direction identifier bit, not check */
dir :1;
/* business ID, it must be SPG_CERT_TEST_WORK_ID_V */
uint8_t work_id;
/* version num, the current standard is fixed at 1. */
uint8_t ver;
/* sequence number */
uint16_t sn;
/* payload len */
uint16_t len;
/* payload label */
uint8_t data[0];
} spg_cert_test_hdr_t;
/* cert test frame command structure for spg */
typedef struct _spg_cert_test_cmd {
/* test id, see SPG_CERT_TEST_ID_XXX */
uint8_t test_id;
/* reserved according to spec */
uint8_t rsvd;
/* payload len */
uint16_t len;
/* payload label */
uint8_t data[0];
} spg_cert_test_cmd_t;
/* test ID 0 and 1 data field layout */
typedef struct _spg_cert_test_cmd_01 {
/* band id */
uint8_t band_id :4,
/* reserved according to spec */
rsvd :4;
} spg_cert_test_cmd_01_t;
/* define dual mode cert test data structure for spg */
typedef struct _spg_cert_test_dm {
/* protocol version */
uint32_t ver :6,
/* header length */
header_len :6,
/* command type for spg cert test, which is the same as the state grid,
* see CERT_TEST_CMD_XXXX
*/
cmd_type :4,
/* reserved for future */
rsvd :4,
/* command parameter, its meaning depends on the type of command */
cmd_param :12;
/* payload label */
uint8_t data[0];
} spg_cert_test_dm_t;
/* dual mode cert test command extended information structure*/
typedef struct _spg_cert_test_dm_ext {
/* security mode */
uint8_t sec_mode :4,
/* rf physical header MCS */
phr_mcs :4;
/* rf psdu MCS */
uint8_t psdu_mcs :4,
/* rf pb size */
pb_size :4;
} spg_cert_test_dm_ext_t;
#pragma pack(pop) /* restore the pack status */
/**
* @brief mac_cert_test_band_cfg_ex() - converting cert test ID to cert mode
* code for spg.
* @param test_id: - test id, see SPG_CERT_TEST_ID_XXX
* @retval: - cert test mode code, see CERT_TEST_CMD_XXX.
*/
static inline uint8_t spg_cert_test_id_to_mode(uint8_t test_id)
{
uint8_t mode = 0;
if (test_id == SPG_CERT_TEST_ID_ENTER_LP) {
mode = CERT_TEST_CMD_ENTER_PHY_LP;
} else if (test_id == SPG_CERT_TEST_ID_ENTER_FWD) {
mode = CERT_TEST_CMD_ENTER_MAC_T;
}
return mode;
}
/**
* @brief cert_test_is_mode_enter_cmd_spg() - check if need to enter
* certification test mode.
* @param ct: point to the command ID of certification test.
*/
#define cert_test_is_mode_enter_cmd_spg(ct) \
((ct)->test_id == SPG_CERT_TEST_ID_ENTER_FWD \
|| (ct)->test_id == SPG_CERT_TEST_ID_ENTER_LP)
/**
* @brief cert_test_is_mode_enter_cmd_ext() - check if need to enter
* certification test mode.
* @param ct: pointer to certification test message.
*/
#define cert_test_is_mode_enter_cmd_ext_spg(ct) \
((ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_T \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_MAC_T \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_FB \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_TM \
|| (ct)->cmd_type == CERT_TEST_CMD_SET_RF \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_T \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_RF_AND_HPLC_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_PHY_HPLC2RF_LP \
|| (ct)->cmd_type == CERT_TEST_CMD_ENTER_SEC)
#ifdef __cplusplus
}
#endif
#endif /* PLC_CERT_TEST_H */