/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-03-30     chenyong     first version
 * 2018-08-17     chenyong     multiple client support
 */

#ifndef __AT_H__
#define __AT_H__

#define AT_CMD_NAME_LEN				32
#define AT_END_MARK_LEN				4
#define AT_CMD_MAX_LEN				128
#define AT_ARGC_MAX					16

/* the server AT commands new line sign */
#define AT_CMD_END_MARK				"\r\n"

typedef enum
{
	AT_RESULT_OK = 0,                  /* AT result is no error */
	AT_RESULT_FAILE = -1,              /* AT result have a generic error */
	AT_RESULT_NULL = -2,               /* AT result not need return */
	AT_RESULT_CMD_ERR = -3,            /* AT command format error or No way to execute */
	//AT_RESULT_CHECK_FAILE = -4,        /* AT command expression format is error */
	AT_RESULT_PARSE_FAILE = -5,        /* AT command arguments parse is error */
}at_result_t;


typedef struct
{
	char name[AT_CMD_NAME_LEN];
	char *args_desc;
	at_result_t (*test)(void);
	at_result_t (*query)(void);
	at_result_t (*setup)(int argc,char *argv[]);
	at_result_t (*exec)(void);
}at_cmd_t;

#define AT_CMD_EXPORT(_name_, _args_desc_, _test_, _query_, _setup_, _exec_)   \
	static const at_cmd_t __at_cmd_##_test_##_query_##_setup_##_exec_ __attribute__((used)) __attribute__((section (".at_cmd"))) = \
	{                                                                          \
		_name_,                                                                \
		_args_desc_,                                                           \
		_test_,                                                                \
		_query_,                                                               \
		_setup_,                                                               \
		_exec_,                                                                \
	};

typedef struct
{
	at_cmd_t* cmd_table;
	uint32_t cmd_num;
	void (*echo_func)(void* this);
	char recv_buffer[AT_CMD_MAX_LEN];
	char end_mark[AT_END_MARK_LEN];
	char cmd_name[AT_CMD_NAME_LEN];
	uint32_t argc;
	char *argv[AT_ARGC_MAX];
}at_server_t;

#endif /* __AT_H__ */
