Skip to content

Commit f7d1931

Browse files
authored
Merge pull request #1749 from chenyong111/chenyong
Add AT multiple client support
2 parents 830975f + 87c35d4 commit f7d1931

File tree

5 files changed

+353
-205
lines changed

5 files changed

+353
-205
lines changed

components/net/at/Kconfig

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,31 @@ if RT_USING_AT
5151

5252
if AT_USING_CLIENT
5353

54-
config AT_CLIENT_DEVICE
55-
string "Client device name"
56-
default "uart2"
57-
58-
config AT_CLIENT_RECV_BUFF_LEN
59-
int "The maximum length of client data accepted"
60-
default 512
61-
54+
config AT_CLIENT_NUM_MAX
55+
int "The maximum number of supported clients"
56+
default 1
57+
range 1 65535
58+
6259
config AT_USING_SOCKET
63-
bool "Provide similar BSD Socket API by AT"
60+
bool "Enable BSD Socket API support by AT commnads"
6461
select RT_USING_LIBC
62+
select RT_USING_SAL
6563
default n
64+
6665
endif
6766

68-
config AT_USING_CLI
69-
bool "Enable command-line interface for AT commands"
70-
default y
71-
depends on FINSH_USING_MSH
72-
73-
config AT_PRINT_RAW_CMD
74-
bool "Enable print RAW format AT command communication data"
75-
default n
67+
if AT_USING_SERVER || AT_USING_CLIENT
68+
69+
config AT_USING_CLI
70+
bool "Enable CLI(Command-Line Interface) for AT commands"
71+
default y
72+
depends on FINSH_USING_MSH
73+
74+
config AT_PRINT_RAW_CMD
75+
bool "Enable print RAW format AT command communication data"
76+
default n
77+
78+
endif
7679

7780
endif
7881

components/net/at/include/at.h

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Change Logs:
2121
* Date Author Notes
2222
* 2018-03-30 chenyong first version
23+
* 2018-08-17 chenyong multiple client support
2324
*/
2425

2526
#ifndef __AT_H__
@@ -30,7 +31,8 @@
3031
#ifdef __cplusplus
3132
extern "C" {
3233
#endif
33-
#define AT_SW_VERSION "0.3.0"
34+
#define AT_SW_VERSION "1.0.0"
35+
#define AT_SW_VERSION_NUM 0x10000
3436

3537
#define DBG_ENABLE
3638
#define DBG_SECTION_NAME "AT"
@@ -67,16 +69,13 @@ extern "C" {
6769
#define AT_SERVER_RECV_BUFF_LEN 256
6870
#endif
6971

70-
#ifndef AT_CLIENT_RECV_BUFF_LEN
71-
#define AT_CLIENT_RECV_BUFF_LEN 512
72-
#endif
73-
7472
#ifndef AT_SERVER_DEVICE
7573
#define AT_SERVER_DEVICE "uart2"
7674
#endif
7775

78-
#ifndef AT_CLIENT_DEVICE
79-
#define AT_CLIENT_DEVICE "uart2"
76+
/* the maximum number of supported AT clients */
77+
#ifndef AT_CLIENT_NUM_MAX
78+
#define AT_CLIENT_NUM_MAX 1
8079
#endif
8180

8281
#define AT_CMD_EXPORT(_name_, _args_expr_, _test_, _query_, _setup_, _exec_) \
@@ -137,7 +136,6 @@ struct at_server
137136
rt_thread_t parser;
138137
void (*parser_entry)(struct at_server *server);
139138
};
140-
141139
typedef struct at_server *at_server_t;
142140
#endif /* AT_USING_SERVER */
143141

@@ -183,8 +181,10 @@ struct at_client
183181
rt_device_t device;
184182

185183
at_status_t status;
184+
char end_sign;
186185

187-
char recv_buffer[AT_CLIENT_RECV_BUFF_LEN];
186+
char *recv_buffer;
187+
rt_size_t recv_bufsz;
188188
rt_size_t cur_recv_len;
189189
rt_sem_t rx_notice;
190190
rt_mutex_t lock;
@@ -198,7 +198,6 @@ struct at_client
198198

199199
rt_thread_t parser;
200200
};
201-
202201
typedef struct at_client *at_client_t;
203202
#endif /* AT_USING_CLIENT */
204203

@@ -216,20 +215,33 @@ int at_req_parse_args(const char *req_args, const char *req_expr, ...);
216215
#endif /* AT_USING_SERVER */
217216

218217
#ifdef AT_USING_CLIENT
219-
/* AT client initialize and start */
220-
int at_client_init(void);
218+
219+
/* AT client initialize and start*/
220+
int at_client_init(const char *dev_name, rt_size_t recv_bufsz);
221+
222+
/* ========================== multiple AT client function ============================ */
223+
224+
/* get AT client object */
225+
at_client_t at_client_get(const char *dev_name);
226+
at_client_t at_client_get_first(void);
221227

222228
/* AT client wait for connection to external devices. */
223-
int at_client_wait_connect(rt_uint32_t timeout);
229+
int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout);
224230

225-
/* AT client send commands to AT server and waiter response */
226-
int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...);
231+
/* AT client send or receive data */
232+
rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size);
233+
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size);
227234

228-
/* AT Client send or receive data */
229-
rt_size_t at_client_send(const char *buf, rt_size_t size);
230-
rt_size_t at_client_recv(char *buf, rt_size_t size);
235+
/* set AT client a line end sign */
236+
void at_obj_set_end_sign(at_client_t client, char ch);
231237

232-
/* AT response structure create and delete */
238+
/* Set URC(Unsolicited Result Code) table */
239+
void at_obj_set_urc_table(at_client_t client, const struct at_urc * table, rt_size_t size);
240+
241+
/* AT client send commands to AT server and waiter response */
242+
int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...);
243+
244+
/* AT response object create and delete */
233245
at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout);
234246
void at_delete_resp(at_response_t resp);
235247
at_response_t at_resp_set_info(at_response_t resp, rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout);
@@ -240,8 +252,20 @@ const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword);
240252
int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...);
241253
int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...);
242254

243-
/* Set URC(Unsolicited Result Code) table */
244-
void at_set_urc_table(const struct at_urc * table, rt_size_t size);
255+
/* ========================== single AT client function ============================ */
256+
257+
/**
258+
* NOTE: These functions can be used directly when there is only one AT client.
259+
* If there are multiple AT Client in the program, these functions can operate on the first initialized AT client.
260+
*/
261+
262+
#define at_exec_cmd(resp, ...) at_obj_exec_cmd(at_client_get_first(), resp, __VA_ARGS__)
263+
#define at_client_wait_connect(timeout) at_client_obj_wait_connect(at_client_get_first(), timeout)
264+
#define at_client_send(buf, size) at_client_obj_send(at_client_get_first(), buf, size)
265+
#define at_client_recv(buf, size) at_client_obj_recv(at_client_get_first(), buf, size)
266+
#define at_set_end_sign(ch) at_obj_set_end_sign(at_client_get_first(), ch)
267+
#define at_set_urc_table(urc_table, table_sz) at_obj_set_urc_table(at_client_get_first(), urc_table, table_sz)
268+
245269
#endif /* AT_USING_CLIENT */
246270

247271
/* ========================== User port function ============================ */

components/net/at/src/at_cli.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void at_cli_init(void)
7676
rt_base_t int_lvl;
7777
rt_device_t console;
7878

79-
rt_sem_init(&console_rx_notice, "at_cli_notice", 0, RT_IPC_FLAG_FIFO);
79+
rt_sem_init(&console_rx_notice, "cli_c", 0, RT_IPC_FLAG_FIFO);
8080

8181
/* create RX FIFO */
8282
console_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
@@ -209,14 +209,12 @@ static rt_err_t client_getchar_rx_ind(rt_device_t dev, rt_size_t size)
209209

210210
return RT_EOK;
211211
}
212-
static void client_cli_parser(void)
212+
static void client_cli_parser(at_client_t client)
213213
{
214214
#define ESC_KEY 0x1B
215215
#define BACKSPACE_KEY 0x08
216216
#define DELECT_KEY 0x7F
217217

218-
extern at_client_t rt_at_get_client(void);
219-
at_client_t client = rt_at_get_client();
220218
char ch;
221219
char cur_line[FINSH_CMD_SIZE] = { 0 };
222220
rt_size_t cur_line_len = 0;
@@ -234,10 +232,10 @@ static void client_cli_parser(void)
234232
rt_hw_interrupt_enable(int_lvl);
235233
}
236234

237-
rt_sem_init(&client_rx_notice, "at_cli_client_notice", 0, RT_IPC_FLAG_FIFO);
235+
rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
238236
client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
239237

240-
at_client = rt_thread_create("at_cli_client", at_client_entry, RT_NULL, 512, 8, 8);
238+
at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
241239
if (client_rx_fifo && at_client)
242240
{
243241
rt_kprintf("======== Welcome to using RT-Thread AT command client cli ========\n");
@@ -261,7 +259,7 @@ static void client_cli_parser(void)
261259
if (cur_line_len)
262260
{
263261
rt_kprintf("\n");
264-
at_exec_cmd(RT_NULL, "%.*s", cur_line_len, cur_line);
262+
at_obj_exec_cmd(client, RT_NULL, "%.*s", cur_line_len, cur_line);
265263
}
266264
cur_line_len = 0;
267265
}
@@ -297,9 +295,10 @@ static void client_cli_parser(void)
297295

298296
static void at(int argc, char **argv)
299297
{
300-
if (argc < 2)
298+
299+
if (argc != 2 && argc != 3)
301300
{
302-
rt_kprintf("Please input 'at <server|client>' \n");
301+
rt_kprintf("Please input '<server|client [dev_name]>' \n");
303302
return;
304303
}
305304

@@ -311,23 +310,40 @@ static void at(int argc, char **argv)
311310
server_cli_parser();
312311
#else
313312
rt_kprintf("Not support AT server, please check your configure!\n");
314-
#endif
313+
#endif /* AT_USING_SERVER */
315314
}
316315
else if (!strcmp(argv[1], "client"))
317316
{
318317
#ifdef AT_USING_CLIENT
319-
client_cli_parser();
318+
at_client_t client = RT_NULL;
319+
320+
if (argc == 2)
321+
{
322+
client_cli_parser(at_client_get_first());
323+
}
324+
else if (argc == 3)
325+
{
326+
client = at_client_get(argv[2]);
327+
if (client == RT_NULL)
328+
{
329+
rt_kprintf("input AT client device name(%s) error.\n", argv[2]);
330+
}
331+
else
332+
{
333+
client_cli_parser(client);
334+
}
335+
}
320336
#else
321337
rt_kprintf("Not support AT client, please check your configure!\n");
322-
#endif
338+
#endif /* AT_USING_CLIENT */
323339
}
324340
else
325341
{
326-
rt_kprintf("Please input 'at <server|client>' \n");
342+
rt_kprintf("Please input '<server|client [dev_name]>' \n");
327343
}
328344

329345
at_cli_deinit();
330346
}
331-
MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client>);
347+
MSH_CMD_EXPORT(at, RT-Thread AT component cli: at <server|client [dev_name]>);
332348

333349
#endif /* AT_USING_CLI */

0 commit comments

Comments
 (0)