at_utils.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-04-14 chenyong first version
  9. */
  10. #include <at.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. static char send_buf[AT_CMD_MAX_LEN];
  14. static rt_size_t last_cmd_len = 0;
  15. /**
  16. * dump hex format data to console device
  17. *
  18. * @param name name for hex object, it will show on log header
  19. * @param buf hex buffer
  20. * @param size buffer size
  21. */
  22. void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size)
  23. {
  24. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  25. #define WIDTH_SIZE 32
  26. rt_size_t i, j;
  27. for (i = 0; i < size; i += WIDTH_SIZE)
  28. {
  29. rt_kprintf("[D/AT] %s: %04X-%04X: ", name, i, i + WIDTH_SIZE);
  30. for (j = 0; j < WIDTH_SIZE; j++)
  31. {
  32. if (i + j < size)
  33. {
  34. rt_kprintf("%02X ", buf[i + j]);
  35. }
  36. else
  37. {
  38. rt_kprintf(" ");
  39. }
  40. if ((j + 1) % 8 == 0)
  41. {
  42. rt_kprintf(" ");
  43. }
  44. }
  45. rt_kprintf(" ");
  46. for (j = 0; j < WIDTH_SIZE; j++)
  47. {
  48. if (i + j < size)
  49. {
  50. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  51. }
  52. }
  53. rt_kprintf("\n");
  54. }
  55. }
  56. const char *at_get_last_cmd(rt_size_t *cmd_size)
  57. {
  58. *cmd_size = last_cmd_len;
  59. return send_buf;
  60. }
  61. rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args)
  62. {
  63. last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args);
  64. #ifdef AT_PRINT_RAW_CMD
  65. at_print_raw_cmd("send", send_buf, last_cmd_len);
  66. #endif
  67. return rt_device_write(device, 0, send_buf, last_cmd_len);
  68. }
  69. rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args)
  70. {
  71. rt_size_t len;
  72. len = at_vprintf(device, format, args);
  73. rt_device_write(device, 0, "\r\n", 2);
  74. return len + 2;
  75. }