at_utils.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2021, 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_WEAK rt_size_t at_utils_send(rt_device_t dev,
  62. rt_off_t pos,
  63. const void *buffer,
  64. rt_size_t size)
  65. {
  66. return rt_device_write(dev, pos, buffer, size);
  67. }
  68. rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args)
  69. {
  70. last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args);
  71. if(last_cmd_len > sizeof(send_buf))
  72. last_cmd_len = sizeof(send_buf);
  73. #ifdef AT_PRINT_RAW_CMD
  74. at_print_raw_cmd("sendline", send_buf, last_cmd_len);
  75. #endif
  76. return at_utils_send(device, 0, send_buf, last_cmd_len);
  77. }
  78. rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args)
  79. {
  80. rt_size_t len;
  81. last_cmd_len = vsnprintf(send_buf, sizeof(send_buf) - 2, format, args);
  82. if(last_cmd_len > sizeof(send_buf) - 2)
  83. last_cmd_len = sizeof(send_buf) - 2;
  84. rt_memcpy(send_buf + last_cmd_len, "\r\n", 2);
  85. len = last_cmd_len + 2;
  86. #ifdef AT_PRINT_RAW_CMD
  87. at_print_raw_cmd("sendline", send_buf, len);
  88. #endif
  89. return at_utils_send(device, 0, send_buf, len);
  90. }