228 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			228 lines
		
	
	
		
			5.8 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 IOT_APP_DL645_MID           IOT_APP_DEMO_MID
 | |
| 
 | |
| /* DLT645 data */
 | |
| #define ID_PLC_DLT645_DATA           (0x60)
 | |
| 
 | |
| #define TIMER_TYPE_ONCE              false  /* Single-period timer */
 | |
| #define TIMER_TYPE_PERIOD            true   /* Periodic timer */
 | |
| 
 | |
| #define STA_OFFLINE_EVENT            (0)
 | |
| #define STA_ONLINE_EVENT             (1)
 | |
| 
 | |
| #define MAX_RSP_DATA_LEN             (512)
 | |
| 
 | |
| typedef enum {
 | |
|     /* app timer for reboot local device. */
 | |
|     APP_TIMER_ID_REBOOT = 0,
 | |
|     /* app timer for set mac address. */
 | |
|     APP_TIMER_ID_SET_MAC,
 | |
|     /* app timer for signal led show. */
 | |
|     APP_TIMER_ID_SIGNAL_LED,
 | |
|     /* app timer for check if network done. */
 | |
|     APP_TIMER_ID_NET_DONE_CHECK,
 | |
|     APP_TIMER_ID_MAX
 | |
| } app_timer_id_e;
 | |
| 
 | |
| typedef enum {
 | |
|     APP_TASK_MAIN,
 | |
|     APP_TASK_CUS,
 | |
|     APP_TASK_MAX
 | |
| } app_task_e;
 | |
| 
 | |
| /* save the pack status */
 | |
| #pragma pack(push)
 | |
| /* 1 byte align */
 | |
| #pragma pack(1)
 | |
| 
 | |
| typedef struct {
 | |
|     /* PLC packet ID */
 | |
|     uint16_t id;
 | |
|     /* destination address */
 | |
|     uint8_t mac[IOT_MAC_ADDR_LEN];
 | |
| } app_custom_data;
 | |
| 
 | |
| /* append data before send plc data */
 | |
| typedef struct append_tx_info {
 | |
|     /* send plc no connectness */
 | |
|     uint16_t  force_connless : 1,
 | |
|     /* reserve */
 | |
|               resv           : 15;
 | |
| } append_tx_info_t;
 | |
| 
 | |
| /* restore the pack status */
 | |
| #pragma pack(pop)
 | |
| 
 | |
| /**
 | |
|  * @brief app_set_freq_band() - set carrier communication frequency band.
 | |
|  *
 | |
|  * @param [in] band_id: band id.
 | |
|  *
 | |
|  * @return None.
 | |
|  */
 | |
| void app_set_freq_band(uint8_t band_id);
 | |
| 
 | |
| /**
 | |
|  * @brief app_set_scan_band_bitmap() - set scan band bitmap.
 | |
|  *
 | |
|  * @param [in] band_bitmap: band bitmap.
 | |
|  *
 | |
|  * @return None.
 | |
|  */
 | |
| void app_set_scan_band_bitmap(uint8_t *band_bitmap);
 | |
| 
 | |
| /**
 | |
|  * @brief app_get_cco_mac_addr() - get the MAC of CCO in this topo.
 | |
|  *
 | |
|  * @param None.
 | |
|  *
 | |
|  * @return mac address.
 | |
|  */
 | |
| uint8_t *app_get_cco_mac_addr(void);
 | |
| 
 | |
| /**
 | |
|  * @brief app_get_cco_mac_addr() - get the MAC of local device.
 | |
|  *
 | |
|  * @param None.
 | |
|  *
 | |
|  * @return mac address.
 | |
|  */
 | |
| uint8_t *app_get_mac_addr(void);
 | |
| 
 | |
| /**
 | |
|  * @brief app_set_mac_addr() - set the MAC of local device.
 | |
|  *
 | |
|  * @param [in] mac: mac address.
 | |
|  *
 | |
|  * @return None.
 | |
|  */
 | |
| void app_set_mac_addr(uint8_t *mac);
 | |
| 
 | |
| /**
 | |
|  * @brief app_get_log_enable() - get log print enable or disable.
 | |
|  *
 | |
|  * @param None.
 | |
|  *
 | |
|  * @return true: enable;
 | |
|  * @return false: disable;
 | |
|  */
 | |
| uint8_t app_get_log_enable(void);
 | |
| 
 | |
| /**
 | |
|  * @brief app_set_log_enable() - set log print enable or disable.
 | |
|  *
 | |
|  * @param [in] flag: true, eanble; false, disable;
 | |
|  *
 | |
|  * @return None.
 | |
|  */
 | |
| void app_set_log_enable(uint8_t flag);
 | |
| 
 | |
| /**
 | |
|  * @brief app_timer_start() - start app timer.
 | |
|  *
 | |
|  * @param [in] id: app timer id, see app_timer_id_e.
 | |
|  * @param [in] period: timer period in milliseconds.
 | |
|  * @param [in] type: timer type, TIMER_TYPE_ONCE or TIMER_TYPE_PERIOD.
 | |
|  *
 | |
|  * @return 0: failed;
 | |
|  * @return others: timer id;
 | |
|  */
 | |
| timer_id_t app_timer_start(app_timer_id_e id, uint32_t period, uint8_t type);
 | |
| 
 | |
| /**
 | |
|  * @brief app_get_random_num32() - get a random number in range.
 | |
|  *
 | |
|  * @param [in] min: range minimum.
 | |
|  * @param [in] max: range maximum.
 | |
|  *
 | |
|  * @return random number;
 | |
|  */
 | |
| uint32_t app_get_random_num32(uint32_t min, uint32_t max);
 | |
| 
 | |
| /**
 | |
|  * @brief app_plc_tx() - send data to destination address throw plc, add append
 | |
|  *          info before data.
 | |
|  *
 | |
|  * @param [in] data: data to send.
 | |
|  * @param [in] data_length: data length to send.
 | |
|  * @param [in] dst_addr: destination address.
 | |
|  * @param [in] id: APP id.
 | |
|  * @param [in] tx_info: append info.
 | |
|  *
 | |
|  * @return ERR_OK: send succeeded;
 | |
|  * @return ERR_FAIL: send failed;
 | |
|  */
 | |
| uint32_t app_plc_tx(uint8_t *data, uint16_t data_length, uint8_t *dst_addr,
 | |
|     uint16_t id, append_tx_info_t *tx_info);
 | |
| 
 | |
| #ifdef __cplusplus
 | |
|  }
 | |
| #endif
 | |
| 
 | |
| #endif  /* _APP_COMMON_H_ */
 |