at_sample_client.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * File : at_sample_client.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. #define LOG_TAG "at.sample"
  29. #include <at_log.h>
  30. /* AT+CIFSR Query local IP address and MAC */
  31. int at_client_test(int argc, char **argv)
  32. {
  33. at_response_t resp = RT_NULL;
  34. int result = 0;
  35. if (argc != 1)
  36. {
  37. LOG_E("at_client_test - AT client send commands to AT server.");
  38. return -1;
  39. }
  40. resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
  41. if (resp == RT_NULL)
  42. {
  43. LOG_E("No memory for response structure!");
  44. return -2;
  45. }
  46. /* close echo */
  47. at_exec_cmd(resp, "ATE0");
  48. result = at_exec_cmd(resp, "AT+CIFSR");
  49. if (result != RT_EOK)
  50. {
  51. LOG_E("AT client send commands failed or return response error!");
  52. goto __exit;
  53. }
  54. /* Print response line buffer */
  55. {
  56. const char *line_buffer = RT_NULL;
  57. LOG_D("Response buffer");
  58. for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
  59. {
  60. if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
  61. {
  62. LOG_D("line %d buffer : %s", line_num, line_buffer);
  63. }
  64. else
  65. {
  66. LOG_E("Parse line buffer error!");
  67. }
  68. }
  69. }
  70. {
  71. char resp_arg[AT_CMD_MAX_LEN] = { 0 };
  72. const char * resp_expr = "%*[^\"]\"%[^\"]\"";
  73. LOG_D(" Parse arguments");
  74. if (at_resp_parse_line_args(resp, 1, resp_expr, resp_arg) == 1)
  75. {
  76. LOG_D("Station IP : %s", resp_arg);
  77. memset(resp_arg, 0x00, AT_CMD_MAX_LEN);
  78. }
  79. else
  80. {
  81. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 4));
  82. }
  83. if (at_resp_parse_line_args(resp, 2, resp_expr, resp_arg) == 1)
  84. {
  85. LOG_D("Station MAC : %s", resp_arg);
  86. }
  87. else
  88. {
  89. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 5));
  90. goto __exit;
  91. }
  92. }
  93. __exit:
  94. if(resp)
  95. {
  96. at_delete_resp(resp);
  97. }
  98. return result;
  99. }
  100. int at_client_test_init(int argc, char **argv)
  101. {
  102. #define AT_CLIENT_RECV_BUFF_LEN 512
  103. if (argc != 2)
  104. {
  105. rt_kprintf("at_client_init <dev_name> -- AT client initialize.\n");
  106. return -RT_ERROR;
  107. }
  108. at_client_init(argv[1], AT_CLIENT_RECV_BUFF_LEN);
  109. return RT_EOK;
  110. }
  111. #ifdef FINSH_USING_MSH
  112. #include <finsh.h>
  113. MSH_CMD_EXPORT(at_client_test, AT client send cmd and get response);
  114. MSH_CMD_EXPORT_ALIAS(at_client_test_init, at_client_init, initialize AT client);
  115. #endif