280 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			280 lines
		
	
	
		
			8.7 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_EDGE_COMPUTE_H
							 | 
						||
| 
								 | 
							
								#define IOT_EDGE_COMPUTE_H
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* the max cache number */
							 | 
						||
| 
								 | 
							
								#define EDGECOM_MAX_CMD                     (10)
							 | 
						||
| 
								 | 
							
								/* the max lenth of tx data */
							 | 
						||
| 
								 | 
							
								#define EDGECOM_CACHE_TXDATA_MAX_LEN        (32)
							 | 
						||
| 
								 | 
							
								/* the max lenth of rx data */
							 | 
						||
| 
								 | 
							
								#define EDGECOM_CACHE_RXDATA_MAX_LEN        (255)
							 | 
						||
| 
								 | 
							
								/* edge uart respond timeover each send(ms) */
							 | 
						||
| 
								 | 
							
								#define UART_RX_TIMEOUT                     (500)
							 | 
						||
| 
								 | 
							
								/* edge uart respond max overtime(ms) */
							 | 
						||
| 
								 | 
							
								#define UART_MAX_TIMEOUT                    (1000)
							 | 
						||
| 
								 | 
							
								/* edge starting and stopping state wait before go into next state(ms) */
							 | 
						||
| 
								 | 
							
								#define EDGE_STARTING_STOPPING_TIMEOUT      (500)
							 | 
						||
| 
								 | 
							
								/* timer between two cache cmd(ms) */
							 | 
						||
| 
								 | 
							
								#define UART_NEXT_CACHE_TIMEOUT             (100)
							 | 
						||
| 
								 | 
							
								/* timer for another edge start(ms) */
							 | 
						||
| 
								 | 
							
								#define UART_MONITOR_START_TIMEOUT          (1000)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* edge command timeout time */
							 | 
						||
| 
								 | 
							
								#define UART_RX_MAX_TIMEOUT_MIN             (500)    // utils:ms
							 | 
						||
| 
								 | 
							
								#define UART_RX_MAX_TIMEOUT_MAX             (5000)   // utils:ms
							 | 
						||
| 
								 | 
							
								/* edge command interve time */
							 | 
						||
| 
								 | 
							
								#define EDGE_CMD_INTERVE_MIN                (20)     // utils:ms
							 | 
						||
| 
								 | 
							
								#define EDGE_CMD_INTERVE_MAX                (3000)   // utils:ms
							 | 
						||
| 
								 | 
							
								/* monitor start time */
							 | 
						||
| 
								 | 
							
								#define UART_MONITOR_START_MIN              (20)     // utils:ms
							 | 
						||
| 
								 | 
							
								#define UART_MONITOR_START_MAX              (20000)  // utils:ms
							 | 
						||
| 
								 | 
							
								/* edge starting stopping time */
							 | 
						||
| 
								 | 
							
								#define EDGE_STARTING_STOPPING_MIN          (20)     // utils:ms
							 | 
						||
| 
								 | 
							
								#define EDGE_STARTING_STOPPING_MAX          (3000)   // utils:ms
							 | 
						||
| 
								 | 
							
								/* edge each command interve time */
							 | 
						||
| 
								 | 
							
								#define UART_RX_TIMEOUT_MIN                 (500)    // utils:ms
							 | 
						||
| 
								 | 
							
								#define UART_RX_TIMEOUT_MAX                 (5000)   // utils:ms
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* edge baudrate index max count */
							 | 
						||
| 
								 | 
							
								#define EDGE_BAUD_IDX_CNT                   (5)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define INPUT_PROTO_CHECK(msg_id)                \
							 | 
						||
| 
								 | 
							
								    ((msg_id == EDGE_INPUT_DEL_CACHE_ID)       ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_ADD_CACHE_ID)        ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_QUERY_CACHE_INFO_ID) ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_SET_DELAY_TIME_ID)   ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_QUERY_DELAY_TIME_ID) ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_QUERY_CACHE_CNT_ID)  ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_SET_DELAY_TIME_INNER_ID) ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_QUERY_DELAY_TIME_INNER_ID)||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_SET_BAUD_ID)              ||\
							 | 
						||
| 
								 | 
							
								    (msg_id == EDGE_INPUT_QUERY_BAUD_ID))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								enum
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* edge uart receive msg */
							 | 
						||
| 
								 | 
							
								    EDGE_UART_RX_MSG = 0x10,
							 | 
						||
| 
								 | 
							
								    /* msg input to edge */
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_MSG,
							 | 
						||
| 
								 | 
							
								    /* edge timer msg */
							 | 
						||
| 
								 | 
							
								    EDGE_TIMER_MSG,
							 | 
						||
| 
								 | 
							
								    /* edge inner msg */
							 | 
						||
| 
								 | 
							
								    EDGE_INTERNAL_MSG,
							 | 
						||
| 
								 | 
							
								    /* msg edge output to proto */
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_MSG,
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* input edge id */
							 | 
						||
| 
								 | 
							
								enum
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_REQ_START_ID = 0x1,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_REQ_STOPID_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_ADD_CACHE_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_DEL_CACHE_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_QUERY_CACHE_INFO_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_QUERY_CACHE_CNT_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_SET_DELAY_TIME_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_QUERY_DELAY_TIME_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_SET_DELAY_TIME_INNER_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_QUERY_DELAY_TIME_INNER_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_SET_BAUD_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_INPUT_QUERY_BAUD_ID,
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* output edge id */
							 | 
						||
| 
								 | 
							
								enum
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_STOPPED_ID = 0x1,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_DL645_PKT_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_CACHE_INFO_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_CACHE_CNT_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_DELAY_TIME_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_DELAY_TIME_INNER_ID,
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_BAUD_ID,
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    EDGE_RESP_TIME_PARAM = 0,
							 | 
						||
| 
								 | 
							
								    EDGE_RESP_EDGE_CMD_CNT,
							 | 
						||
| 
								 | 
							
								    EDGE_RESP_EDGE_CMD_INFO,
							 | 
						||
| 
								 | 
							
								    EDGE_RESP_TIME_PARAM_INNER,
							 | 
						||
| 
								 | 
							
								    EDGE_RESP_CMD_BAUD,
							 | 
						||
| 
								 | 
							
								} edge_resp_type_e;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef enum {
							 | 
						||
| 
								 | 
							
								    EDGE_QUERY_EDGE_CMD_645 = 0,
							 | 
						||
| 
								 | 
							
								    EDGE_QUERY_EDGE_CMD_MODBUS,
							 | 
						||
| 
								 | 
							
								} edge_query_type_e;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma pack(push)  // save the pack status
							 | 
						||
| 
								 | 
							
								#pragma pack(1)     // 1 byte align
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* used by EDGE_INPUT_SET_DELAY_TIME_ID and EDGE_OUTPUT_RESP_DELAY_TIME_ID */
							 | 
						||
| 
								 | 
							
								typedef struct _edge_delay_time_info_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint16_t  cmd_inverval;
							 | 
						||
| 
								 | 
							
								    uint16_t  cmd_tol_tm;
							 | 
						||
| 
								 | 
							
								} edge_delay_time_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _baud_info_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* baudrate */
							 | 
						||
| 
								 | 
							
								    uint8_t baud_idx;
							 | 
						||
| 
								 | 
							
								    /* parity */
							 | 
						||
| 
								 | 
							
								    uint8_t parity;
							 | 
						||
| 
								 | 
							
								} baud_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _edge_baud_cmd_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* edge cmd index*/
							 | 
						||
| 
								 | 
							
								    uint8_t     index;
							 | 
						||
| 
								 | 
							
								    /* baudrate info */
							 | 
						||
| 
								 | 
							
								    baud_info_t baud_info;
							 | 
						||
| 
								 | 
							
								} edge_baud_cmd_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* used by EDGE_INPUT_SET_DELAY_TIME_INNER_ID and
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_DELAY_TIME_INNER_ID */
							 | 
						||
| 
								 | 
							
								typedef struct _edge_delay_time_info_inner_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* time for starting and stopping state delay */
							 | 
						||
| 
								 | 
							
								    uint16_t  cmd_starting_stopping_tm;
							 | 
						||
| 
								 | 
							
								    /* time for each one receive time out */
							 | 
						||
| 
								 | 
							
								    uint16_t  cmd_each_tm;
							 | 
						||
| 
								 | 
							
								} edge_delay_time_info_inner_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* used by EDGE_INPUT_ADD_CACHE_ID, EDGE_INPUT_DEL_CACHE_ID and
							 | 
						||
| 
								 | 
							
								    EDGE_OUTPUT_RESP_CACHE_INFO_ID*/
							 | 
						||
| 
								 | 
							
								typedef struct _edge_cache_cmd_info_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* the type of cache command */
							 | 
						||
| 
								 | 
							
								    uint8_t cmd_type;
							 | 
						||
| 
								 | 
							
								    /* the index of cache command */
							 | 
						||
| 
								 | 
							
								    uint8_t idx;
							 | 
						||
| 
								 | 
							
								    /* the data of the cache command */
							 | 
						||
| 
								 | 
							
								    uint8_t data[0];
							 | 
						||
| 
								 | 
							
								} edge_cache_cmd_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* used by  EDGE_OUTPUT_RESP_CACHE_CNT_ID */
							 | 
						||
| 
								 | 
							
								typedef struct _edge_cmd_cnt_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* the count of cache command */
							 | 
						||
| 
								 | 
							
								    uint8_t cnt;
							 | 
						||
| 
								 | 
							
								} edge_cmd_cnt_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* command info */
							 | 
						||
| 
								 | 
							
								typedef struct _edgecom_cmd_t {
							 | 
						||
| 
								 | 
							
								    /* send cmd length, need less than EDGECOM_CACHE_TXDATA_MAX_LEN */
							 | 
						||
| 
								 | 
							
								    uint8_t  tx_len;
							 | 
						||
| 
								 | 
							
								    /* send buffer */
							 | 
						||
| 
								 | 
							
								    uint8_t  tx_buf[EDGECOM_CACHE_TXDATA_MAX_LEN];
							 | 
						||
| 
								 | 
							
								    /* read cmd len need less than EDGECOM_CACHE_TXDATA_MAX_LEN */
							 | 
						||
| 
								 | 
							
								    uint8_t  rx_len;
							 | 
						||
| 
								 | 
							
								    /* receive buffer */
							 | 
						||
| 
								 | 
							
								    uint8_t  rx_buf[EDGECOM_CACHE_RXDATA_MAX_LEN];
							 | 
						||
| 
								 | 
							
								} edgecom_cmd_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* this struct will be saved to flash */
							 | 
						||
| 
								 | 
							
								typedef struct edgecom_flash_info_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint32_t      magic_start;
							 | 
						||
| 
								 | 
							
								    /* one edge command total time */
							 | 
						||
| 
								 | 
							
								    uint32_t      cmd_tol_tm;
							 | 
						||
| 
								 | 
							
								    /* edge command interval time */
							 | 
						||
| 
								 | 
							
								    uint32_t      cmd_inverval;
							 | 
						||
| 
								 | 
							
								    /* for each uart rx wait time */
							 | 
						||
| 
								 | 
							
								    uint32_t      cmd_each_tm;
							 | 
						||
| 
								 | 
							
								    /* delay time for comming in or out edge_stated state */
							 | 
						||
| 
								 | 
							
								    uint32_t      cmd_starting_stopping_tm;
							 | 
						||
| 
								 | 
							
								    /* record whitch place of cache has cmd */
							 | 
						||
| 
								 | 
							
								    uint32_t      edge_cmd_sign;
							 | 
						||
| 
								 | 
							
								    edgecom_cmd_t cmd_list[EDGECOM_MAX_CMD];
							 | 
						||
| 
								 | 
							
								    /* edge commmand baud info */
							 | 
						||
| 
								 | 
							
								    baud_info_t   baud_info[EDGECOM_MAX_CMD];
							 | 
						||
| 
								 | 
							
								    uint32_t      magic_end;
							 | 
						||
| 
								 | 
							
								} edgecom_flash_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _debug_statistic
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* the count receive data during stopping state */
							 | 
						||
| 
								 | 
							
								    uint32_t stopping_rxuart_cnt;
							 | 
						||
| 
								 | 
							
								} debug_statistic;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _edge_obj_t
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /* edge_state_idle etc */
							 | 
						||
| 
								 | 
							
								    uint8_t        edge_state;
							 | 
						||
| 
								 | 
							
								    /* uart hdl, need to be check if it is NULL before send msg */
							 | 
						||
| 
								 | 
							
								    void*          uart_hdl;
							 | 
						||
| 
								 | 
							
								    /* which cmd is polling */
							 | 
						||
| 
								 | 
							
								    uint8_t        cmd_bit_idx;
							 | 
						||
| 
								 | 
							
								    /* every loop, which cmd is not poll yet */
							 | 
						||
| 
								 | 
							
								    uint32_t       loop_cache_sign;
							 | 
						||
| 
								 | 
							
								    /* the total time edge uart wait for respond each cmd */
							 | 
						||
| 
								 | 
							
								    uint32_t       uart_rx_total_time;
							 | 
						||
| 
								 | 
							
								    /* to count debug statistic during polling */
							 | 
						||
| 
								 | 
							
								    debug_statistic debug_statistic_cnt;
							 | 
						||
| 
								 | 
							
								    /* starting and stopping timer */
							 | 
						||
| 
								 | 
							
								    timer_id_t     edge_monitor_start_stop_tmr;
							 | 
						||
| 
								 | 
							
								    /* timer for interval between two cache cmd */
							 | 
						||
| 
								 | 
							
								    timer_id_t     next_cache_tmr;
							 | 
						||
| 
								 | 
							
								    /* timer for uart respond */
							 | 
						||
| 
								 | 
							
								    timer_id_t     uart_rx_tmr;
							 | 
						||
| 
								 | 
							
								    /* cache info, will save to flash */
							 | 
						||
| 
								 | 
							
								    edgecom_flash_info_t  cache_info;
							 | 
						||
| 
								 | 
							
								} edge_obj_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma pack(pop)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void iot_edge_init(void);
							 | 
						||
| 
								 | 
							
								uint8_t iot_edge_tmr_init(void);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_edge_state_machine() - to handle the edge state machine of edge
							 | 
						||
| 
								 | 
							
								 * @param msg:        message type
							 | 
						||
| 
								 | 
							
								 * @param msg_id:     message id
							 | 
						||
| 
								 | 
							
								 * @param arg:        point to the data
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void iot_edge_state_machine(uint8_t msg, uint8_t msg_id, void *arg);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief iot_os_stop_timer_handle() - to stop the timer and clear msg of the
							 | 
						||
| 
								 | 
							
								                                       timer
							 | 
						||
| 
								 | 
							
								 * @param tmr:        the timer id
							 | 
						||
| 
								 | 
							
								 * @param msg:        the msg
							 | 
						||
| 
								 | 
							
								 * @param msg_id:     the message id
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void iot_os_stop_timer_handle(timer_id_t tmr, uint16_t msg, uint8_t msg_id);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief proto_post_data_to_edge_msg_handle() - proto post data message to edge
							 | 
						||
| 
								 | 
							
								 * @param msg_id:     the message id
							 | 
						||
| 
								 | 
							
								 * @param data:       the msg data
							 | 
						||
| 
								 | 
							
								 * @param data_len:   the msg data len
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void proto_post_data_to_edge_msg_handle(uint8_t msg_id, void *data,
							 | 
						||
| 
								 | 
							
								    uint16_t data_len);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* end IOT_EDGE_COMPUTE_H */
							 |