364 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			364 lines
		
	
	
		
			13 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 _AT_COMMAND_H_
 | ||
|  | #define _AT_COMMAND_H_
 | ||
|  | 
 | ||
|  | #include "iot_flashinfo.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #define INCLUDE_AT_COMMAND_DEBUG_PRINT 1
 | ||
|  | /* TODO , REMOVE DEBUG PRINTF WHEN MODULE OK. */ | ||
|  | #if INCLUDE_AT_COMMAND_DEBUG_PRINT
 | ||
|  | 
 | ||
|  | 
 | ||
|  | #if (HW_PLATFORM == HW_PLATFORM_SIMU)
 | ||
|  | #define DINFOR(fmt, ...) do{\
 | ||
|  |         iot_cus_printf("\r\n[grapp][at]info line:%04d@%s:" fmt "\r\n",\ | ||
|  |         __LINE__, __FILE__, ##__VA_ARGS__);\ | ||
|  |         }while(0) | ||
|  | 
 | ||
|  | #define DERROR(fmt, ...) do{\
 | ||
|  |         iot_cus_printf("\r\n[grapp][at]error line:%04d@%s:" fmt "\r\n", \ | ||
|  |         __LINE__, __FILE__, ##__VA_ARGS__);\ | ||
|  |         }while(0) | ||
|  | #else
 | ||
|  | #define DINFOR(fmt, arg...) do{\
 | ||
|  |         iot_cus_printf("\r\n[grapp][at]infor line:%04d@%s:" fmt "\r\n",\ | ||
|  |         __LINE__, __FILE__, ##arg);\ | ||
|  |         }while(0) | ||
|  | 
 | ||
|  | #define DERROR(fmt, arg...) do{\
 | ||
|  |         iot_cus_printf("\r\n[grapp][at]error line:%04d@%s:" fmt "\r\n", \ | ||
|  |         __LINE__, __FILE__, ##arg);\ | ||
|  |         }while(0) | ||
|  | #endif
 | ||
|  | 
 | ||
|  | 
 | ||
|  | #else
 | ||
|  | 
 | ||
|  | #if (HW_PLATFORM == HW_PLATFORM_SIMU)
 | ||
|  | #define DINFOR(fmt, ...)
 | ||
|  | #define DERROR(fmt, ...)
 | ||
|  | 
 | ||
|  | #else
 | ||
|  | #define DINFOR(fmt, arg...)
 | ||
|  | #define DERROR(fmt, arg...)
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | 
 | ||
|  | /* The list of AT commands. This MUST contain all IDs of AT command. */ | ||
|  | enum at_command_id_list_e | ||
|  | { | ||
|  |     /* Command line : AT+LIST */ | ||
|  |     AT_CID_HELP         = 0, | ||
|  |     /* Command line : AT+MEMORY */ | ||
|  |     AT_CID_MEMORY       = 1, | ||
|  |     /* Command line : AT+TASK */ | ||
|  |     AT_CID_TASK         = 2, | ||
|  |     /* Command line : AT+TIME */ | ||
|  |     AT_CID_TIME         = 3, | ||
|  |     /* Command line : AT+SYSINFO */ | ||
|  |     AT_CID_SYSTEMINFO   = 4, | ||
|  |     /* Command line : AT+UART */ | ||
|  |     AT_CID_UART         = 5, | ||
|  |     /* Command line : AT+MAC */ | ||
|  |     AT_CID_MAC          = 6, | ||
|  |     /* Command line : AT+TOPO */ | ||
|  |     AT_CID_TOPO         = 7, | ||
|  |     /* Command line : AT+PING */ | ||
|  |     AT_CID_PING         = 8, | ||
|  |     /* Command line : AT+SEND */ | ||
|  |     AT_CID_SEND         = 9, | ||
|  |     /* Command line : AT+REBOOT */ | ||
|  |     AT_CID_REBOOT       =10, | ||
|  |     /* Command line : AT+WHO */ | ||
|  |     AT_CID_WHO          =11, | ||
|  |     /* Command line : AT+RFPOWER */ | ||
|  |     AT_CID_RFPOWER      =12, | ||
|  |     /* Command line : AT+WHITELIST */ | ||
|  |     AT_CID_WHITELIST    =13, | ||
|  |     /* Command line : AT+NETWORK */ | ||
|  |     AT_CID_NETWORK      =14, | ||
|  |     /* Command line : AT+FLASH */ | ||
|  |     AT_CID_FLASH        =15, | ||
|  |     /* Command line : AT+PMODE */ | ||
|  |     AT_CID_PMODE        =16, | ||
|  |     /* Command line : AT+SPI */ | ||
|  |     AT_CID_SPI          =17, | ||
|  |     /* Command line : AT+IIC */ | ||
|  |     AT_CID_IIC          =18, | ||
|  |     /* Command line : AT+GPIOCFG */ | ||
|  |     AT_CID_GPIOCFG      =19, | ||
|  |     /* Command line : AT+GPIOVAL */ | ||
|  |     AT_CID_GPIOVAL      =20, | ||
|  |     /* Command line : AT+SWMODE */ | ||
|  |     AT_CID_SWMODE       =21, | ||
|  |     /* Command line : AT+NETGROUP */ | ||
|  |     AT_CID_NETGROUP     =22, | ||
|  |     /* Command line : AT+HOSTPORT */ | ||
|  |     AT_CID_HOSTPORT     =23, | ||
|  |     /* ... */ | ||
|  | 
 | ||
|  |     AT_CID_MAX | ||
|  | }; | ||
|  | 
 | ||
|  | /* Max length of name in AT command. */ | ||
|  | #define AT_NAME_STR_LEN_MAX 64
 | ||
|  | 
 | ||
|  | /* Max length of aguments in AT command. */ | ||
|  | #define AT_ARG_STR_LEN_MAX  192
 | ||
|  | 
 | ||
|  | /* Length of buffer for putting pices of AT command together. */ | ||
|  | #define AT_HOOK_BUFFER_LEN  (AT_NAME_STR_LEN_MAX + AT_ARG_STR_LEN_MAX)
 | ||
|  | 
 | ||
|  | /** const, STA DEVs max supported in network */ | ||
|  | #define AT_STA_DEV_MAX  STA_DEV_MAX
 | ||
|  | 
 | ||
|  | #define AT_TMR_CHECK_END_PMODE (1000 * 3) // //unit ms
 | ||
|  | 
 | ||
|  | 
 | ||
|  | /* Handle of an AT command to call downlayer. 'argc' not include command
 | ||
|  |  name or opcode string.*/ | ||
|  | typedef void (*at_fn_handle_send_t)(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | /* Handle of an AT command to response the resault. */ | ||
|  | typedef void (*at_fn_handle_resp_t)(iot_pkt_t *resp); | ||
|  | 
 | ||
|  | /* Response to the host. AT command uses it, uplayer registered.*/ | ||
|  | typedef uint32_t (*at_fn_response_from_at_t)(iot_pkt_t *resp); | ||
|  | 
 | ||
|  | /* Response to the AT command. Downlayer uses it, AT command registered.*/ | ||
|  | typedef void (*at_fn_response_to_at_t)(iot_pkt_t *resp); | ||
|  | 
 | ||
|  | /* Send command to the downlayer. AT command uses it, Downlayer registered. */ | ||
|  | typedef uint8_t (*at_fn_send_from_at_t)(iot_pkt_t *arg); | ||
|  | 
 | ||
|  | /* Send command string to AT command. uplayer uses it, AT command registered. */ | ||
|  | typedef void (*at_fn_send_to_at_t)(uint8_t *str, uint32_t dlen); | ||
|  | 
 | ||
|  | typedef struct _at_command_line_t | ||
|  | { | ||
|  |     /* AT command name. */ | ||
|  |     char                **name; | ||
|  |     /* strlen(cmd_name), just for searching command. */ | ||
|  |     uint8_t            head_len; | ||
|  |     /*
 | ||
|  |     Timeout when after handle runout and waiting for response.n Seconds. | ||
|  |     Zero means not timeout count on this command. | ||
|  |     */ | ||
|  |     uint8_t            timeout; | ||
|  |     /* The priority of this command. */ | ||
|  |     uint16_t            prio; | ||
|  |     /*
 | ||
|  |     Handle to send this command. AT use it. | ||
|  |     */ | ||
|  |     at_fn_handle_send_t      fn_send; | ||
|  |     /*
 | ||
|  |     Handle for response the resault form downlayer to uplayer. | ||
|  |     */ | ||
|  |     at_fn_handle_resp_t     fn_resp; | ||
|  | }at_cmd_t; | ||
|  | 
 | ||
|  | /* Register callback handles for AT command module using. */ | ||
|  | typedef struct _at_command_callback_t | ||
|  | { | ||
|  |     /*
 | ||
|  |     Provied by greeapp top uart , called by AT command moudle, send AT response | ||
|  |     char* to host uart. | ||
|  |     */ | ||
|  |     at_fn_response_from_at_t    fn_resp; | ||
|  |     /*
 | ||
|  |     Provided by plctxrx moudle. used by AT command moudle. send control command | ||
|  |     to plctxrx moudle. | ||
|  |     */ | ||
|  |     at_fn_send_from_at_t        fn_send; | ||
|  | }at_cb_t; | ||
|  | 
 | ||
|  | typedef struct _at_command_buffer_t | ||
|  | { | ||
|  |     /*
 | ||
|  |     Flag to tell if this buffer has a completed AT command. | ||
|  |     TRUE : completed, FALSE : NOT. | ||
|  |     */ | ||
|  |     uint16_t    cmd_flushed; | ||
|  |     /* The point to store command in menber "c". */ | ||
|  |     uint16_t    index; | ||
|  |     /* Buffer to store AT command. */ | ||
|  |     uint8_t     c[AT_HOOK_BUFFER_LEN]; | ||
|  | }at_buf_t; | ||
|  | 
 | ||
|  | /*
 | ||
|  | This is the status of current AT command that executing. No need to protect | ||
|  | those items for there is one place to write / read. | ||
|  | */ | ||
|  | typedef struct _at_command_status_t | ||
|  | { | ||
|  |     at_cmd_t *cur_cmd;  /* Executing AT command */ | ||
|  |     uint16_t resp_index;  /* Current response frame index of this AT command. */ | ||
|  |     uint16_t resp_total;  /* Total response frames count of this AT command. */ | ||
|  |     uint16_t handle_runout; /* A flag to show if this AT cmd handle finished. */ | ||
|  |     uint16_t resp_finished; /* A flag to show if this response finished. */ | ||
|  | }at_status_t; | ||
|  | 
 | ||
|  | typedef struct _at_command_device_t { | ||
|  |     /* Role of this device as CCO or STA. */ | ||
|  |     uint8_t     dev_role; | ||
|  |     /* Mac address of this device. */ | ||
|  |     uint8_t     mac[IOT_MAC_ADDR_LEN]; | ||
|  |     /* Reserved for byte completion. */ | ||
|  |     uint8_t     resv; | ||
|  | } at_dev_t; | ||
|  | 
 | ||
|  | /*
 | ||
|  | General struct for online device list saved in AT layer local | ||
|  | and device list saved in flash. | ||
|  | */ | ||
|  | typedef struct _at_command_device_list_t { | ||
|  |     /* The valid device number. */ | ||
|  |     uint8_t     valid_dev_cnt; | ||
|  |     /* Reserved for byte completion. */ | ||
|  |     uint8_t     resv[3]; | ||
|  |     /* The device table. */ | ||
|  |     at_dev_t    dev[AT_STA_DEV_MAX]; | ||
|  | } at_dev_list_t; | ||
|  | 
 | ||
|  | typedef struct _at_cust_flash_info_header_t | ||
|  | { | ||
|  |     /* Data length from first item to the end. */ | ||
|  |     uint16_t    dlen; | ||
|  |     /* Reserved for byte completion. */ | ||
|  |     uint8_t     resv[2]; | ||
|  |     /* CRC32 for data what 'dlen' stands for. */ | ||
|  |     uint32_t    dcrc; | ||
|  | }at_cust_flash_head_t; | ||
|  | 
 | ||
|  | /* The whitelist config enable/disable state. 0, disable, 1, enable.*/ | ||
|  | typedef enum _at_whitelist_config_state_e | ||
|  | { | ||
|  |     AT_WL_CFG_STATE_DISABLE, | ||
|  |     AT_WL_CFG_STATE_ENABLE, | ||
|  | }at_wl_cfg_state_e; | ||
|  | 
 | ||
|  | /* Top level structure of AT command. */ | ||
|  | typedef struct _at_command_context_t | ||
|  | { | ||
|  |     /*
 | ||
|  |     Magic key to tell if AT command module initialized. | ||
|  |     mkey = 0x569a4323 : initialized, else NOT. | ||
|  |     */ | ||
|  |     uint32_t                mkey; | ||
|  |     /* Local device type. */ | ||
|  |     uint8_t                 dev_type; | ||
|  |     /* Local mac address. */ | ||
|  |     uint8_t                 mac[IOT_MAC_ADDR_LEN]; | ||
|  |     /* The whitelist config enable/disable state. */ | ||
|  |     at_wl_cfg_state_e       wl_cfg_state; | ||
|  |     /* Task handle. */ | ||
|  |     iot_task_h              task; | ||
|  |     /* Semaphore. */ | ||
|  |     os_mutex_h              sem; | ||
|  |     /* Timer count down for response timout. */ | ||
|  |     timer_id_t              tmr; | ||
|  |     /* end transparent mode check timer */ | ||
|  |     timer_id_t              end_tmode_tmr; | ||
|  |     /* The status of current AT command */ | ||
|  |     at_status_t             status; | ||
|  |     /* Callback handles for AT command module using */ | ||
|  |     at_cb_t                 fn_callback; | ||
|  |     /* Buffer to store pices of AT command in more than one uart frames. */ | ||
|  |     at_buf_t                at_buffer; | ||
|  |     /* Online device list saved in local. */ | ||
|  |     at_dev_list_t           online_dev_list; | ||
|  |     /* Table of all AT commands that registerd statically. */ | ||
|  |     at_cmd_t                at_table[AT_CID_MAX]; | ||
|  |     /* transparent info */ | ||
|  |     at_transparent_t        transp; | ||
|  | }at_context_t; | ||
|  | 
 | ||
|  | /* Initialize the AT module. */ | ||
|  | iot_task_h iot_at_task_init(void); | ||
|  | 
 | ||
|  | /* API for de-initializing module. */ | ||
|  | void iot_at_task_deinit(void); | ||
|  | 
 | ||
|  | at_context_t *iot_at_context_get(void); | ||
|  | 
 | ||
|  | /* at provided function, registed to top uart, receive command string  */ | ||
|  | uint8_t iot_at_command_proto_send_to_at(uint8_t *buf, uint16_t blen); | ||
|  | 
 | ||
|  | /* top uart provided send function, registed to at. */ | ||
|  | uint8_t iot_at_command_fn_register_response_to_uart(at_fn_response_from_at_t fn); | ||
|  | 
 | ||
|  | /* plctxrx provided function. registed to at. */ | ||
|  | uint8_t iot_at_command_fn_register_send_to_plc(at_fn_send_from_at_t fn); | ||
|  | 
 | ||
|  | /* at provided function, registed to plctxrx. receive command response */ | ||
|  | uint8_t iot_at_command_plc_send_response_to_at(iot_pkt_t *p_resp_pkt); | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | /* ------------------The command handles-----------------   */ | ||
|  | void at_handle_command_help(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_mac(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_mac(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_time(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_time(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_systeminfo(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_systeminfo(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_uart(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_uart(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_topo(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_topo(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_ping(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_send(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | uint32_t at_handle_transparent_data(uint8_t *buf, uint16_t blen); | ||
|  | void at_handle_command_pmode(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_send_receive(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_memory(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_task(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_reboot(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_reboot(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_who(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_who(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_rfpower(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_rfpower(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_whitelist(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_whitelist(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_connect(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_connect(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_disconnect(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_disconnect(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_network(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_response_network(iot_pkt_t *p_resp_pkt); | ||
|  | void at_handle_command_flash(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_spi(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_iic(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_gpiocfg(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_gpioval(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_cmd_sw_uartmode(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_netgroup(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | void at_handle_command_host_port(uint32_t opcode, uint32_t argc, uint8_t *argv[]); | ||
|  | 
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif
 |