230 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			230 lines
		
	
	
		
			7.4 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 _APP_COMMON_H_
 | |
| #define _APP_COMMON_H_
 | |
| 
 | |
| #include "app_types.h"
 | |
| 
 | |
| #ifdef __cplusplus
 | |
|   extern "C" {
 | |
| #endif
 | |
| 
 | |
| #define APP_ARRAY_COUNT(x) (sizeof(x) / sizeof((x)[0]))
 | |
| #define APP_SWAP16(x) ((((x)&0xff00) >> 8) | (((x)&0x00ff) << 8))
 | |
| #define APP_MAKE_U16(a, b) ((uint16_t)(((uint8_t)(a)) | ((uint16_t)((uint8_t)(b))) << 8))
 | |
| #define APP_MAKE_U32(a, b) ((uint32_t)(((uint16_t)(a)) | ((uint32_t)((uint16_t)(b))) << 16))
 | |
| #define APP_MAKE_IDENTIFIER(a, b, c, d) APP_MAKE_U32(APP_MAKE_U16(a, b), APP_MAKE_U16(c, d))
 | |
| #define APP_VER2BCD(v, r, spc) ((((v)&0xF) << 12) | (((r)&0xF) << 8) | ((spc)&0xFF))
 | |
| 
 | |
| 
 | |
| #define APP_PRINTF(fmt, ...) \
 | |
| do { \
 | |
|     if (app_get_log_enable()) { \
 | |
|         iot_cus_printf(fmt, ##__VA_ARGS__); \
 | |
|         iot_cus_printf("\n"); \
 | |
|     } \
 | |
| } while(0)
 | |
| 
 | |
| #define APP_PRINT_BUF(sz, buf, len) \
 | |
| do { \
 | |
|     if (app_get_log_enable()) { \
 | |
|         int32_t tmp_length = (len); \
 | |
|         int32_t tmp_i = 0; \
 | |
|         iot_cus_printf((sz)); \
 | |
|         iot_cus_printf("[len=%d]", len); \
 | |
|         for (tmp_i = 0; tmp_i < tmp_length; tmp_i++) { \
 | |
|             iot_cus_printf(" %02X", (uint8_t)(buf)[tmp_i]); \
 | |
|         } \
 | |
|         iot_cus_printf("\n"); \
 | |
|     } \
 | |
| } while (0)
 | |
| 
 | |
| #ifndef MAC_FMT
 | |
| #define MAC_FMT "%02X%02X%02X%02X%02X%02X"
 | |
| #endif
 | |
| 
 | |
| #ifndef MAC_ARG
 | |
| #define MAC_ARG(x) ((uint8_t*)(x))[0],((uint8_t*)(x))[1],((uint8_t*)(x))[2],((uint8_t*)(x))[3],((uint8_t*)(x))[4],((uint8_t*)(x))[5]
 | |
| #endif
 | |
| 
 | |
| #define INVALID_MAC_ADDR    "\x00\x00\x00\x00\x00\x00"
 | |
| #define BROADCAST_MAC_ADDR  "\xff\xff\xff\xff\xff\xff"
 | |
| #define LOCAL_BROADCAST_MAC "\x00\xff\xff\xff\xff\xff"
 | |
| 
 | |
| /* PLC data transmit */
 | |
| #define ID_PLC_DATA_TRANSMIT         0x50
 | |
| /* PLC remote cmd */
 | |
| #define ID_PLC_REMOTE_CMD            0x51
 | |
| #if SUPPORT_IEEE_1901
 | |
| /* PLC bypass*/
 | |
| #define ID_PLC_DATA_BYPASS           0x50//0x52
 | |
| #else
 | |
| /* PLC bypass*/
 | |
| #define ID_PLC_DATA_BYPASS           0x52
 | |
| #endif
 | |
| /*AT remote cmd*/
 | |
| #define ID_PLC_REMOTE_AT             0x53
 | |
| /*AT PLC TEST*/
 | |
| #define ID_PLC_DATA_TEST             0x54
 | |
| /*PLC system control data communication. */
 | |
| #define ID_PLC_SYSCTRL_DATA          0x55
 | |
| /*AT hex data*/
 | |
| #define ID_PLC_AT_DATA               0x56
 | |
| /*AT string data*/
 | |
| #define ID_PLC_AT_STRING             0x57
 | |
| /*AT delay time*/
 | |
| #define ID_PLC_AT_DELAYTIME          0x58
 | |
| #define APP_TOPO_NODE_MAX            200
 | |
| #define APP_TIMER_REBOOT_PERIOD      500
 | |
| #define APP_TIMER_AT_SEND_PERIOD     3000    /* ms */
 | |
| #define ONE_MINUTES                  (60 * 1000) /* units:ms */
 | |
| #define APP_TIMER_IDF_CHECK_PERIOD   (5 * ONE_MINUTES) /* units:ms */
 | |
| 
 | |
| #define APP_WHITELIST_SETUP_MAX      40
 | |
| 
 | |
| #define TIMER_TYPE_ONCE              false  /* Single-period timer */
 | |
| #define TIMER_TYPE_PERIOD            true   /* Periodic timer */
 | |
| 
 | |
| #define INFO_FORWARD_DATA            0X12
 | |
| #define INFO_DELAY_TEST              0X70
 | |
| 
 | |
| #define MAX_RSP_DATA_LEN             (512)
 | |
| 
 | |
| /* invalid rssi unit dbm */
 | |
| #define APP_INV_RSSI                 (-128)
 | |
| /* best rssi, use PHY_RSSI_DBUV_TO_DBM calc valid rssi:-87 - 33 dbm */
 | |
| #define BEST_RSSI                    (33)
 | |
| 
 | |
| #define SEND_FORCE_TYPE_DEF          (0)
 | |
| #define SEND_FORCE_TYPE_CONNLESS     (1)
 | |
| #define SEND_FORCE_TYPE_CONNECT      (2)
 | |
| 
 | |
| typedef enum {
 | |
|     CCO_TX_TIME = 2,
 | |
|     STA_RX_TIME = 3,
 | |
|     STA_TX_TIME = 4,
 | |
|     CCO_RX_TIME = 5,
 | |
| } app_delay_timer_id_e;
 | |
| 
 | |
| typedef enum {
 | |
|     APP_TIMER_ID_UART = 0,
 | |
|     APP_TIMER_ID_REBOOT,
 | |
|     APP_TIMER_ID_SET_MAC,
 | |
|     APP_TIMER_ID_AT_SEND_PLC,
 | |
|     APP_TIMER_ID_HILINK_REPORT,
 | |
|     APP_TIMER_ID_WRITE_SCENE_TO_FLASH,
 | |
|     APP_TIMER_ID_HILINK_RE_REPORT,
 | |
|     APP_TIMER_ID_HILINK_STATE,
 | |
|     APP_TIMER_ID_IDF_CHECK,
 | |
|     APP_TIMER_ID_SIGNAL_LED,
 | |
|     APP_TIMER_ID_NET_DONE_CHECK_OR_BCAST,
 | |
|     APP_TIMER_ID_MAX
 | |
| } app_timer_id_e;
 | |
| 
 | |
| typedef enum {
 | |
|     APP_SOURCE_UART,
 | |
|     APP_SOURCE_SPI,
 | |
|     APP_SOURCE_PLC,
 | |
| } app_source_e;
 | |
| 
 | |
| /* User version information */
 | |
| typedef struct {
 | |
|     uint16_t sw_ver;        /* Software version number in BCD format. */
 | |
|     uint8_t boot_ver;       /* Bootloader version. */
 | |
|     uint8_t year;           /* Version date (year), in BIN format. */
 | |
|     uint8_t month;          /* Version date (month), in BIN format. */
 | |
|     uint8_t day;            /* Version date (day), in BIN format. */
 | |
|     uint8_t chip_code[2];   /* Chip code, in ASCII format.2 count  */
 | |
|     uint8_t manu_code[2];   /* Manufacturer code, in ASCII format.2count  */
 | |
|     uint8_t pad[2];         /* pad 2 */
 | |
| } app_user_info_t;
 | |
| 
 | |
| #pragma pack(push)  // save the pack status
 | |
| #pragma pack(1)     // 1 byte align
 | |
| 
 | |
| typedef struct {
 | |
|     uint16_t id;                      /* PLC packet ID */
 | |
|     uint8_t mac[IOT_MAC_ADDR_LEN];    /* destination address */
 | |
| } app_custom_data;
 | |
| 
 | |
| /* append data before send plc data */
 | |
| typedef struct append_tx_info {
 | |
|     /* force send plc type, see SEND_FORCE_TYPE_XXX */
 | |
|     uint16_t  force_type     : 2,
 | |
|     /* reserve */
 | |
|               resv           : 14;
 | |
| } append_tx_info_t;
 | |
| 
 | |
| typedef struct {
 | |
|     uint32_t sn;        /* sequence number */
 | |
|     uint32_t mcu_tx_time;   /* mcu transmit time (us)*/
 | |
|     uint32_t cco_tx_time;   /* cco transmit time (us)*/
 | |
|     uint32_t sta_rx_time;   /* cco receive time (us)*/
 | |
|     uint32_t sta_tx_time;   /* sta transmit time (us)*/
 | |
|     uint32_t cco_rx_time;   /* sta receive time (us)*/
 | |
|     uint16_t datalen;       /* data lenth */
 | |
|     uint8_t resver[0];      /* resver (us)*/
 | |
| } delay_time_test;
 | |
| 
 | |
| typedef struct _signal_test_t
 | |
| {
 | |
|     uint8_t  src[IOT_MAC_ADDR_LEN]; /* query source mac */
 | |
|     uint8_t  dst[IOT_MAC_ADDR_LEN]; /* query dest mac */
 | |
|     int8_t   dl_snr;                /* downlink snr */
 | |
|     int8_t   dl_rssi;               /* downlink rssi,dbm */
 | |
|     int8_t   ul_snr;                /* uplink snr */
 | |
|     int8_t   ul_rssi;               /* uplink rssi,dbm */
 | |
| } signal_test_t;
 | |
| 
 | |
| #pragma pack(pop)
 | |
| 
 | |
| void app_set_freq_band(uint8_t band_id);
 | |
| void app_set_scan_band_bitmap(uint8_t *band_bitmap);
 | |
| void app_get_user_info(app_user_info_t *user_info);
 | |
| uint8_t *app_get_cco_mac_addr(void);
 | |
| uint8_t *app_get_mac_addr(void);
 | |
| void app_set_mac_addr(uint8_t *mac);
 | |
| uint8_t app_get_online_state(void);
 | |
| uint8_t app_get_log_enable(void);
 | |
| void app_set_log_enable(uint8_t flag);
 | |
| timer_id_t app_timer_start(app_timer_id_e id, uint32_t period, uint8_t type);
 | |
| void app_timer_stop(app_timer_id_e id);
 | |
| uint32_t get_random_num32(uint32_t min, uint32_t max);
 | |
| 
 | |
| void app_ntb_to_us(uint8_t *data);
 | |
| void delay_timer_get(uint8_t *data, uint8_t time_type, uint32_t set_time);
 | |
| uint16_t app_plc_tx(uint8_t *data, uint16_t data_length, uint8_t *dst_addr,
 | |
|     uint16_t id, append_tx_info_t *tx_info);
 | |
| 
 | |
| /**
 | |
|  * @brief: get net enable flag.
 | |
|  * @retval: flag of net enable.
 | |
|  */
 | |
| uint8_t app_get_net_enable(void);
 | |
| 
 | |
| /**
 | |
|  * @brief: set net enable flag.
 | |
|  * @param flag: flag of net enable, 1 mean enable, 0 mena disable.
 | |
|  */
 | |
| void app_set_net_enable(uint8_t flag);
 | |
| 
 | |
| #ifdef __cplusplus
 | |
|  }
 | |
| #endif
 | |
| 
 | |
| #endif  /* _APP_COMMON_H_ */
 | |
| 
 |