at_client_sample.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * File : at_client_sample.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-07-06 chenyong first version
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <rtthread.h>
  27. #include <at.h>
  28. /* AT+CIFSR Query local IP address and MAC */
  29. int at_client_test(int argc, char **argv)
  30. {
  31. at_response_t resp = RT_NULL;
  32. int result = 0;
  33. if (argc != 1)
  34. {
  35. LOG_E("at_client_test - AT client send commands to AT server.");
  36. return -1;
  37. }
  38. resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
  39. if (resp == RT_NULL)
  40. {
  41. LOG_E("No memory for response structure!");
  42. return -2;
  43. }
  44. /* close echo */
  45. at_exec_cmd(resp, "ATE0");
  46. result = at_exec_cmd(resp, "AT+CIFSR");
  47. if (result != RT_EOK)
  48. {
  49. LOG_E("AT client send commands failed or return response error!");
  50. goto __exit;
  51. }
  52. /* Print response line buffer */
  53. {
  54. const char *line_buffer = RT_NULL;
  55. LOG_D("Response buffer");
  56. for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
  57. {
  58. if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
  59. {
  60. LOG_D("line %d buffer : %s", line_num, line_buffer);
  61. }
  62. else
  63. {
  64. LOG_E("Parse line buffer error!");
  65. }
  66. }
  67. }
  68. {
  69. char resp_arg[AT_CMD_MAX_LEN] = { 0 };
  70. const char * resp_expr = "%*[^\"]\"%[^\"]\"";
  71. LOG_D(" Parse arguments");
  72. if (at_resp_parse_line_args(resp, 1, resp_expr, resp_arg) == 1)
  73. {
  74. LOG_D("Station IP : %s", resp_arg);
  75. memset(resp_arg, 0x00, AT_CMD_MAX_LEN);
  76. }
  77. else
  78. {
  79. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 4));
  80. }
  81. if (at_resp_parse_line_args(resp, 2, resp_expr, resp_arg) == 1)
  82. {
  83. LOG_D("Station MAC : %s", resp_arg);
  84. }
  85. else
  86. {
  87. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 5));
  88. goto __exit;
  89. }
  90. }
  91. __exit:
  92. if(resp)
  93. {
  94. at_delete_resp(resp);
  95. }
  96. return result;
  97. }
  98. #ifdef FINSH_USING_MSH
  99. #include <finsh.h>
  100. MSH_CMD_EXPORT(at_client_test, AT client send cmd and get response);
  101. #endif