113 lines
3.1 KiB
C
113 lines
3.1 KiB
C
/*
|
|
* File : at_client_sample.c
|
|
* This file is part of RT-Thread RTOS
|
|
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
*
|
|
* Change Logs:
|
|
* Date Author Notes
|
|
* 2018-07-06 chenyong first version
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <rtthread.h>
|
|
#include <rt_at.h>
|
|
|
|
/* AT+CIFSR Query local IP address and MAC */
|
|
int at_client_test(int argc, char **argv)
|
|
{
|
|
rt_at_response_t resp = RT_NULL;
|
|
int result = 0;
|
|
|
|
if (argc != 1)
|
|
{
|
|
LOG_E("at_client_test - AT client send commands to AT server.");
|
|
return -1;
|
|
}
|
|
|
|
resp = rt_at_create_resp(RT_AT_CLIENT_RECV_BUFF_LEN, 0, rt_tick_from_millisecond(5000));
|
|
if (resp == RT_NULL)
|
|
{
|
|
LOG_E("No memory for response structure!");
|
|
return -2;
|
|
}
|
|
|
|
result = rt_at_exec_cmd(resp, "AT+CIFSR");
|
|
if (result != RT_EOK)
|
|
{
|
|
LOG_E("AT client send commands failed or return response error!");
|
|
goto __exit;
|
|
}
|
|
|
|
/* Print response line buffer */
|
|
{
|
|
const char *line_buffer = RT_NULL;
|
|
|
|
LOG_D("Response buffer");
|
|
|
|
for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
|
|
{
|
|
if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
|
|
{
|
|
LOG_D("line %d buffer : %s", line_num, line_buffer);
|
|
}
|
|
else
|
|
{
|
|
LOG_E("Parse line buffer error!");
|
|
}
|
|
}
|
|
}
|
|
|
|
{
|
|
char resp_arg[RT_AT_CMD_MAX_LEN] = { 0 };
|
|
const char * resp_expr = "%*[^\"]\"%[^\"]\"";
|
|
|
|
LOG_D(" Parse arguments");
|
|
if (at_resp_parse_line_args(resp, 2, resp_expr, resp_arg) == 1)
|
|
{
|
|
LOG_D("Station IP : %s", resp_arg);
|
|
memset(resp_arg, 0x00, RT_AT_CMD_MAX_LEN);
|
|
}
|
|
else
|
|
{
|
|
LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 4));
|
|
}
|
|
|
|
if (at_resp_parse_line_args(resp, 3, resp_expr, resp_arg) == 1)
|
|
{
|
|
LOG_D("Station MAC : %s", resp_arg);
|
|
}
|
|
else
|
|
{
|
|
LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 5));
|
|
goto __exit;
|
|
}
|
|
}
|
|
__exit:
|
|
if(resp)
|
|
{
|
|
rt_at_delete_resp(resp);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#ifdef FINSH_USING_MSH
|
|
#include <finsh.h>
|
|
MSH_CMD_EXPORT(at_client_test, AT client send cmd and get response);
|
|
#endif
|