290 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			290 lines
		
	
	
		
			9.9 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 _IOT_SG_CTRL_H_
 | ||
|  | #define _IOT_SG_CTRL_H_
 | ||
|  | 
 | ||
|  | #include "iot_task_api.h"
 | ||
|  | #include "os_timer_api.h"
 | ||
|  | #include "iot_uart_api.h"
 | ||
|  | #include "iot_addr_hash_table_api.h"
 | ||
|  | #include "iot_plc_api.h"
 | ||
|  | #include "iot_cli_sg_api.h"
 | ||
|  | #include "iot_sg_ctrl_api.h"
 | ||
|  | #include "iot_sg_ctrl_fr.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | /* define priorities for message to be handle */ | ||
|  | #define IOT_SG_CTRL_TASK_QUEUE_HP           0
 | ||
|  | #define IOT_SG_CTRL_TASK_QUEUE_MAX_PRIO     1
 | ||
|  | 
 | ||
|  | #define IOT_SG_CTRL_TASK_POOL_SIZE          128
 | ||
|  | /* UART received buffer size */ | ||
|  | #define IOT_SG_CTRL_UART_RECV_BUF_SIZE      2048
 | ||
|  | 
 | ||
|  | /* message type */ | ||
|  | #define IOT_SG_CTRL_MSG_TYPE_PLC            0
 | ||
|  | #define IOT_SG_CTRL_MSG_TYPE_TIME           1
 | ||
|  | #define IOT_SG_CTRL_MSG_TYPE_UART           2
 | ||
|  | #define IOT_SG_CTRL_MSG_TYPE_CLI            3
 | ||
|  | 
 | ||
|  | /* message id for timer */ | ||
|  | #define IOT_SG_CTRL_MSG_ID_TIMER_CHECK      1 /* check cache data in buffer */
 | ||
|  | #define IOT_SG_CTRL_MSG_ID_PLC_RECV         2
 | ||
|  | #define IOT_SG_CTRL_MSG_ID_UART_RECV        3
 | ||
|  | #define IOT_SG_CTRL_MSG_ID_CLI              4
 | ||
|  | 
 | ||
|  | /* check cache data in sendq interval, unit is ms */ | ||
|  | #define IOT_SG_CTRL_PERIODIC_TIMER_INTERVAL (250)
 | ||
|  | 
 | ||
|  | /* Macro to define msdu pkt pool number */ | ||
|  | #define IOT_SG_CTRL_POOL_NUM                8
 | ||
|  | 
 | ||
|  | /* Macro to define ul sn buffer size */ | ||
|  | #define IOT_SG_CTRL_UL_SIZE                 16
 | ||
|  | 
 | ||
|  | /* max life-span cnt, which decide how long will the buff keep the frame info */ | ||
|  | #define IOT_SG_CTRL_MAX_LIFE_SPAN_CNT       24
 | ||
|  | 
 | ||
|  | /* rerty invterval , unit is IOT_SG_CTRL_APP_MAX_RETRY_INTV_CNT */ | ||
|  | #define IOT_SG_CTRL_APP_RETRY_INTV_CNT      8
 | ||
|  | 
 | ||
|  | /* scan node check periodic max count
 | ||
|  |  * periodis value is IOT_SG_CTRL_PERIODIC_TIMER_INTERVAL ms | ||
|  |  */ | ||
|  | #define IOT_SG_CTRL_MAX_CHECK_PERIODIC     (0x0f)
 | ||
|  | 
 | ||
|  | /* scan node entry max age- unit :
 | ||
|  |  * (IOT_SG_CTRL_PERIODIC_TIMER_INTERVAL * IOT_SG_CTRL_MAX_CHECK_PERIODIC) ms | ||
|  |  */ | ||
|  | #define IOT_SG_CTRL_MAX_AGE                (60)
 | ||
|  | 
 | ||
|  | /* define scan node hash table size */ | ||
|  | #define IOT_SG_CTRL_SCAN_HASH_TABLE_SIZE   7
 | ||
|  | 
 | ||
|  | /* scan node max count */ | ||
|  | #define IOT_SG_CTRL_SCAN_MAX_CNT           (255)
 | ||
|  | 
 | ||
|  | /* define type of gw app ctrl proto */ | ||
|  | #define IOT_SG_APP_CTRL_PROTO_GW           0
 | ||
|  | /* define type of nw app ctrl proto */ | ||
|  | #define IOT_SG_APP_CTRL_PROTO_NW           1
 | ||
|  | /* define the max inner num */ | ||
|  | #define GPIO_PIN_NUMBER_INNER_MAX          48
 | ||
|  | /* phy num of plc led */ | ||
|  | #define IOT_EXT_PLC_LED_GPIO               (1)
 | ||
|  | /* phy num of warn led */ | ||
|  | #define IOT_EXT_WARN_LED_GPIO              (2)
 | ||
|  | /* the logic num of inner gpio is 0 ~ 48, the num of external gpio is 49 ~ 64
 | ||
|  |  * logic num of plc led | ||
|  |  */ | ||
|  | #define IOT_EXT_PLC_LED_GPIO_LOGIC         (GPIO_PIN_NUMBER_INNER_MAX + 2)
 | ||
|  | /* logic num of warn led */ | ||
|  | #define IOT_EXT_WARN_LED_GPIO_LOGIC        (GPIO_PIN_NUMBER_INNER_MAX + 3)
 | ||
|  | /* led on */ | ||
|  | #define LED_ON                             (0)
 | ||
|  | /* led off */ | ||
|  | #define LED_OFF                            (1)
 | ||
|  | 
 | ||
|  | enum gpio_type_e { | ||
|  |     /* plc led blink num */ | ||
|  |     EXT_LED_PLC = 0, | ||
|  |     /* crash led blink num */ | ||
|  |     EXT_LED_WARRING, | ||
|  |     EXT_LED_MAX, | ||
|  | }; | ||
|  | 
 | ||
|  | typedef struct _ckb_led_blink_ctrl_t { | ||
|  |     /* is the led need blink */ | ||
|  |     uint8_t blink_flag; | ||
|  |     /* the last state of the blinking led */ | ||
|  |     uint8_t last_state; | ||
|  | } net_led_ctrl_t; | ||
|  | 
 | ||
|  | typedef struct _iot_ctrl_task_msg { | ||
|  |     /* standard iot_task message */ | ||
|  |     iot_task_msg_t  msg; | ||
|  |     /* pointer to message data */ | ||
|  |     void            *data; | ||
|  | } iot_ctrl_task_msg_t; | ||
|  | 
 | ||
|  | /* frame infor extracted from the frame */ | ||
|  | typedef struct _frame_info_t { | ||
|  |     /* command type, see PROTO_CTRL_CMD_TYPE_XXX */ | ||
|  |     uint8_t         cmd_type; | ||
|  |     /* pkt been sent counter, 0 means the buffer is not used */ | ||
|  |     uint8_t         send_cnt; | ||
|  |     /* life-span counter */ | ||
|  |     uint16_t        span_cnt; | ||
|  |     /* app sequence number */ | ||
|  |     uint16_t        app_sn; | ||
|  |     /* sent pkt */ | ||
|  |     iot_pkt_t       *send_pkt; | ||
|  | } frame_info_t; | ||
|  | 
 | ||
|  | /* frame infor pool */ | ||
|  | typedef struct _info_pool_t { | ||
|  |     /* how many buffers are not used */ | ||
|  |     uint8_t         remain_cnt; | ||
|  |     /* infor extracted from the frame */ | ||
|  |     frame_info_t    frm_info[IOT_SG_CTRL_POOL_NUM]; | ||
|  | } info_pool_t; | ||
|  | 
 | ||
|  | /* scan node info */ | ||
|  | typedef struct _scan_node_info { | ||
|  |     iot_addr_hash_entry_t entry; | ||
|  |     /* index of entry list */ | ||
|  |     uint8_t         index; | ||
|  |     /* age of the entry. It's updated periodically.
 | ||
|  |      * The entry shall be removed when its age reach certain value. */ | ||
|  |     uint8_t         age; | ||
|  |     /* node information */ | ||
|  |     ctrl_node_info_t node_info; | ||
|  | } scan_node_info_t; | ||
|  | 
 | ||
|  | /* scan node info */ | ||
|  | typedef struct _scan_node_handle { | ||
|  |     /* scan running  */ | ||
|  |     uint8_t             scan_runing; | ||
|  |     /* refresh node periodic 250ms * 16 = 4s */ | ||
|  |     uint8_t             scan_period; | ||
|  |     /* hash table for scan node entry. */ | ||
|  |     iot_addr_hash_table_h table; | ||
|  |     /* scan total node count */ | ||
|  |     uint8_t             total_scan_cnt; | ||
|  |     /* pointer array point to hash table entry for iteration. */ | ||
|  |     scan_node_info_t    **scan_node_list; | ||
|  | } scan_node_handle_t; | ||
|  | 
 | ||
|  | typedef struct _iot_sg_ctrl_test_task_data { | ||
|  |     /* app sending sequence number for msdu */ | ||
|  |     uint16_t            app_sn; | ||
|  |     /* handle of the sg_ctrl iot_task */ | ||
|  |     iot_task_h          task_handle; | ||
|  |     /* a flag indicating if app registered successfully */ | ||
|  |     uint8_t             app_registered; | ||
|  |     /* indication for device is ready for data transmition or not */ | ||
|  |     uint8_t             dev_ready; | ||
|  |     /* mac address of local device */ | ||
|  |     uint8_t             local_mac_addr[IOT_MAC_ADDR_LEN]; | ||
|  |     /* mac address of destination device*/ | ||
|  |     uint8_t             dst_mac_addr[IOT_MAC_ADDR_LEN]; | ||
|  |     /* handle of this app */ | ||
|  |     iot_plc_app_h       app_handle; | ||
|  |     /* timer to call api periodically */ | ||
|  |     timer_id_t          ctrl_timer; | ||
|  |     /* com for upper host */ | ||
|  |     iot_uart_h          uart_h; | ||
|  |     /* cli interface for sg ctrl */ | ||
|  |     iot_cli_sg_interface_t  cli_sg_interface; | ||
|  |     /* connect role ,see IOT_PLC_DEV_ROLE_XXX */ | ||
|  |     uint8_t             connect_role; | ||
|  |     /* frame infor pool */ | ||
|  |     info_pool_t         info_pool; | ||
|  |     /* ul sn buffer */ | ||
|  |     uint16_t            ul_sn[IOT_SG_CTRL_UL_SIZE]; | ||
|  |     /* ul sn buffer index */ | ||
|  |     uint8_t             ul_sn_index; | ||
|  |     /* translate packet proto type, see - IOT_CTRL_PROTO_XXX */ | ||
|  |     uint8_t             proto_type; | ||
|  |     /* app ctrl proto type fro PLC translate, see - IOT_SG_APP_CTRL_PROTO_XXX */ | ||
|  |     uint8_t             app_proto; | ||
|  |     /* ckq frequence band, see - PLC_LIB_FREQ_BAND_XXX */ | ||
|  |     uint8_t             freq_band; | ||
|  |     /* app set ckq frequence band, see - PLC_LIB_FREQ_BAND_XXX */ | ||
|  |     uint8_t             config_freq_band; | ||
|  |     /* connected sequence */ | ||
|  |     uint8_t             connect_seq; | ||
|  |     /* ckq drv array */ | ||
|  |     iot_ctrl_drv_t      *ctrl_drv; | ||
|  |     /* scan node handle */ | ||
|  |     scan_node_handle_t  scan_handle; | ||
|  | }iot_sg_ctrl_global_t; | ||
|  | 
 | ||
|  | extern iot_sg_ctrl_global_t *sg_ctrl_global; | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_get_ckq_data_sn() - get ckq data sequence number and | ||
|  |  *                              check master or slave. | ||
|  |  * @param data:                 ckq source data from host. | ||
|  |  * @param app_sn:               sequence number ptr. | ||
|  |  * @retval:                     1 - is report data(master). | ||
|  |  *                              0 - is response data(slave). | ||
|  |  */ | ||
|  | uint8_t iot_sg_ctrl_get_ckq_data_sn(uint8_t *proto_header, uint8_t *app_sn); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_ul_sn_exist() - check sequence number is exist in ul | ||
|  |  *                              buffer, record sequence number. | ||
|  |  * @param app_sn:               sequence number. | ||
|  |  * @retval:                     1 - exist. | ||
|  |  *                              0 - no exist and record sn. | ||
|  |  */ | ||
|  | uint8_t iot_sg_ctrl_ul_sn_exist(uint16_t app_sn); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_pool_sn_exist() - check sequence number is exist in dl | ||
|  |  *                              pool buffer. | ||
|  |  * @param pool:                 pool buffer. | ||
|  |  * @param app_sn:               sequence number. | ||
|  |  * @param clr_flag:             1 - clean sn frm data. 0 - no clean sn frm data. | ||
|  |  * @retval:                     1 - exist. | ||
|  |  *                              0 - no exist. | ||
|  |  */ | ||
|  | uint8_t iot_sg_ctrl_pool_sn_exist(info_pool_t *pool, uint16_t app_sn, | ||
|  |     uint8_t clr_flag); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_rpt_sta_data() - report sta meter data to host. | ||
|  |  * @param data:                 report data ptr. | ||
|  |  * @param data_len:             report data length. | ||
|  |  * @param msg_id:               msg id see - IOT_CTRL_EVENT_XXX | ||
|  |  */ | ||
|  | void iot_sg_ctrl_rpt_sta_data(uint8_t *data, uint16_t data_len, uint8_t msg_id); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_rpt_cco_data() - report cco data to host. | ||
|  |  * @param rev_pkt:              report pkt, is consumed. | ||
|  |  */ | ||
|  | void iot_sg_ctrl_rpt_cco_data(iot_pkt_t *rev_pkt); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_id_info_rpt() - report id information | ||
|  |  * @param id:       id information | ||
|  |  * @param len:      id data length | ||
|  |  * @param id_type:  id type, see PROTO_CTRL_ID_INFO_TYPE_XXX. | ||
|  |  * @param dev_type: device type, see IOT_PLC_DEV_TYPE_XXX. | ||
|  |  */ | ||
|  | void iot_sg_ctrl_id_info_rpt(uint8_t *id, uint8_t len, uint8_t id_type, | ||
|  |     uint8_t dev_type); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief iot_sg_ctrl_data_rpt_drv() - report data to driver | ||
|  |  * @param sn:            sequence number for a packet. | ||
|  |  * @param evt_type:      report event type, see IOT_CTRL_EVENT_XXX. | ||
|  |  * @param data:          report data. | ||
|  |  * @param data_len:      report data length. | ||
|  |  */ | ||
|  | void iot_sg_ctrl_data_rpt_drv(uint8_t sn, uint8_t evt_type, uint8_t *data, | ||
|  |     uint16_t data_len); | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif //_IOT_SG_CTRL_H_
 |