at_utils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2025 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. * 2023-06-09 CX optimize at_vprintfln interface
  10. */
  11. #include <at.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. /**
  15. * dump hex format data to console device
  16. *
  17. * @param name name for hex object, it will show on log header
  18. * @param buf hex buffer
  19. * @param size buffer size
  20. */
  21. void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size)
  22. {
  23. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  24. #define WIDTH_SIZE 32
  25. rt_size_t i, j;
  26. for (i = 0; i < size; i += WIDTH_SIZE)
  27. {
  28. rt_kprintf("[D/AT] %s: %04X-%04X: ", name, i, i + WIDTH_SIZE);
  29. for (j = 0; j < WIDTH_SIZE; j++)
  30. {
  31. if (i + j < size)
  32. {
  33. rt_kprintf("%02X ", (unsigned char)buf[i + j]);
  34. }
  35. else
  36. {
  37. rt_kprintf(" ");
  38. }
  39. if ((j + 1) % 8 == 0)
  40. {
  41. rt_kprintf(" ");
  42. }
  43. }
  44. rt_kprintf(" ");
  45. for (j = 0; j < WIDTH_SIZE; j++)
  46. {
  47. if (i + j < size)
  48. {
  49. rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
  50. }
  51. }
  52. rt_kprintf("\n");
  53. }
  54. }
  55. rt_weak rt_size_t at_utils_send(rt_device_t dev,
  56. rt_off_t pos,
  57. const void *buffer,
  58. rt_size_t size)
  59. {
  60. return rt_device_write(dev, pos, buffer, size);
  61. }
  62. rt_size_t at_vprintf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args)
  63. {
  64. rt_size_t len = vsnprintf(send_buf, buf_size, format, args);
  65. if (len == 0)
  66. {
  67. return 0;
  68. }
  69. #ifdef AT_PRINT_RAW_CMD
  70. at_print_raw_cmd("sendline", send_buf, len);
  71. #endif
  72. return at_utils_send(device, 0, send_buf, len);
  73. }
  74. rt_size_t at_vprintfln(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args)
  75. {
  76. rt_size_t len = vsnprintf(send_buf, buf_size - 2, format, args);
  77. if (len == 0)
  78. {
  79. return 0;
  80. }
  81. send_buf[len++] = '\r';
  82. send_buf[len++] = '\n';
  83. #ifdef AT_PRINT_RAW_CMD
  84. at_print_raw_cmd("sendline", send_buf, len);
  85. #endif
  86. return at_utils_send(device, 0, send_buf, len);
  87. }
  88. rt_size_t at_vprintfcr(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args)
  89. {
  90. rt_size_t len = vsnprintf(send_buf, buf_size - 1, format, args);
  91. if (len == 0)
  92. {
  93. return 0;
  94. }
  95. send_buf[len++] = '\r';
  96. #ifdef AT_PRINT_RAW_CMD
  97. at_print_raw_cmd("sendline", send_buf, len);
  98. #endif
  99. return at_utils_send(device, 0, send_buf, len);
  100. }
  101. rt_size_t at_vprintflf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args)
  102. {
  103. rt_size_t len = vsnprintf(send_buf, buf_size - 1, format, args);
  104. if (len == 0)
  105. {
  106. return 0;
  107. }
  108. send_buf[len++] = '\n';
  109. #ifdef AT_PRINT_RAW_CMD
  110. at_print_raw_cmd("sendline", send_buf, len);
  111. #endif
  112. return at_utils_send(device, 0, send_buf, len);
  113. }