at_utils.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * File : at_utils.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-04-14 chenyong first version
  23. */
  24. #include <at.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. static char send_buf[AT_CMD_MAX_LEN];
  28. static rt_size_t last_cmd_len = 0;
  29. /**
  30. * dump hex format data to console device
  31. *
  32. * @param name name for hex object, it will show on log header
  33. * @param buf hex buffer
  34. * @param size buffer size
  35. */
  36. void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size)
  37. {
  38. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  39. #define WIDTH_SIZE 32
  40. rt_size_t i, j;
  41. for (i = 0; i < size; i += WIDTH_SIZE)
  42. {
  43. rt_kprintf("[D/AT] %s: %04X-%04X: ", name, i, i + WIDTH_SIZE);
  44. for (j = 0; j < WIDTH_SIZE; j++)
  45. {
  46. if (i + j < size)
  47. {
  48. rt_kprintf("%02X ", buf[i + j]);
  49. }
  50. else
  51. {
  52. rt_kprintf(" ");
  53. }
  54. if ((j + 1) % 8 == 0)
  55. {
  56. rt_kprintf(" ");
  57. }
  58. }
  59. rt_kprintf(" ");
  60. for (j = 0; j < WIDTH_SIZE; j++)
  61. {
  62. if (i + j < size)
  63. {
  64. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  65. }
  66. }
  67. rt_kprintf("\n");
  68. }
  69. }
  70. const char *at_get_last_cmd(rt_size_t *cmd_size)
  71. {
  72. *cmd_size = last_cmd_len;
  73. return send_buf;
  74. }
  75. rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args)
  76. {
  77. last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args);
  78. #ifdef AT_PRINT_RAW_CMD
  79. at_print_raw_cmd("send", send_buf, last_cmd_len);
  80. #endif
  81. return rt_device_write(device, 0, send_buf, last_cmd_len);
  82. }
  83. rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args)
  84. {
  85. rt_size_t len;
  86. len = at_vprintf(device, format, args);
  87. rt_device_write(device, 0, "\r\n", 2);
  88. return len + 2;
  89. }