at_sample_client.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-07-06 chenyong first version
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <rtthread.h>
  13. #include <at.h>
  14. #define LOG_TAG "at.sample"
  15. #include <at_log.h>
  16. /* AT+CIFSR Query local IP address and MAC */
  17. int at_client_test(int argc, char **argv)
  18. {
  19. at_response_t resp = RT_NULL;
  20. int result = 0;
  21. if (argc != 1)
  22. {
  23. LOG_E("at_client_test - AT client send commands to AT server.");
  24. return -1;
  25. }
  26. resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
  27. if (resp == RT_NULL)
  28. {
  29. LOG_E("No memory for response structure!");
  30. return -2;
  31. }
  32. /* close echo */
  33. at_exec_cmd(resp, "ATE0");
  34. result = at_exec_cmd(resp, "AT+CIFSR");
  35. if (result != RT_EOK)
  36. {
  37. LOG_E("AT client send commands failed or return response error!");
  38. goto __exit;
  39. }
  40. /* Print response line buffer */
  41. {
  42. const char *line_buffer = RT_NULL;
  43. LOG_D("Response buffer");
  44. for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
  45. {
  46. if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
  47. {
  48. LOG_D("line %d buffer : %s", line_num, line_buffer);
  49. }
  50. else
  51. {
  52. LOG_E("Parse line buffer error!");
  53. }
  54. }
  55. }
  56. {
  57. char resp_arg[AT_CMD_MAX_LEN] = { 0 };
  58. const char * resp_expr = "%*[^\"]\"%[^\"]\"";
  59. LOG_D(" Parse arguments");
  60. if (at_resp_parse_line_args(resp, 1, resp_expr, resp_arg) == 1)
  61. {
  62. LOG_D("Station IP : %s", resp_arg);
  63. rt_memset(resp_arg, 0x00, AT_CMD_MAX_LEN);
  64. }
  65. else
  66. {
  67. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 4));
  68. }
  69. if (at_resp_parse_line_args(resp, 2, resp_expr, resp_arg) == 1)
  70. {
  71. LOG_D("Station MAC : %s", resp_arg);
  72. }
  73. else
  74. {
  75. LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 5));
  76. goto __exit;
  77. }
  78. }
  79. __exit:
  80. if(resp)
  81. {
  82. at_delete_resp(resp);
  83. }
  84. return result;
  85. }
  86. int at_client_test_init(int argc, char **argv)
  87. {
  88. #define AT_CLIENT_RECV_BUFF_LEN 512
  89. if (argc != 2)
  90. {
  91. rt_kprintf("at_client_init <dev_name> -- AT client initialize.\n");
  92. return -RT_ERROR;
  93. }
  94. at_client_init(argv[1], AT_CLIENT_RECV_BUFF_LEN);
  95. return RT_EOK;
  96. }
  97. #ifdef FINSH_USING_MSH
  98. #include <finsh.h>
  99. MSH_CMD_EXPORT(at_client_test, AT client send cmd and get response);
  100. MSH_CMD_EXPORT_ALIAS(at_client_test_init, at_client_init, initialize AT client);
  101. #endif