lib_dumpbuffer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /****************************************************************************
  2. * libs/libc/misc/lib_dumpbuffer.c
  3. *
  4. * Copyright (C) 2009, 2011, 2014, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <nuttx/compiler.h>
  40. #include <stdint.h>
  41. #include <debug.h>
  42. /****************************************************************************
  43. * Pre-processor definitions
  44. ****************************************************************************/
  45. #define _NITEMS 32 /* 32 bytes displayed per line */
  46. #define _LINESIZE (3 * _NITEMS + 4) /* 2 hex chars, ASCII char, 3 spaces, NUL */
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: lib_nibble
  52. *
  53. * Description:
  54. * Convert a binary nibble to a hexadecimal character.
  55. *
  56. ****************************************************************************/
  57. static char lib_nibble(unsigned char nibble)
  58. {
  59. if (nibble < 10)
  60. {
  61. return '0' + nibble;
  62. }
  63. else
  64. {
  65. return 'a' + nibble - 10;
  66. }
  67. }
  68. /****************************************************************************
  69. * Public Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: lib_dumpbuffer
  73. *
  74. * Description:
  75. * Do a pretty buffer dump.
  76. *
  77. * A fairly large on-stack buffer is used for the case where timestamps are
  78. * applied to each line.
  79. *
  80. ****************************************************************************/
  81. void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
  82. unsigned int buflen)
  83. {
  84. char buf[_LINESIZE];
  85. unsigned int i;
  86. unsigned int j;
  87. unsigned int k;
  88. syslog(LOG_INFO, "%s (%p):\n", msg, buffer);
  89. for (i = 0; i < buflen; i += _NITEMS)
  90. {
  91. FAR char *ptr = buf;
  92. /* Generate hex values: 2 * _NITEMS + 1 bytes */
  93. for (j = 0; j < _NITEMS; j++)
  94. {
  95. k = i + j;
  96. if (j == (_NITEMS / 2))
  97. {
  98. *ptr++ = ' ';
  99. }
  100. if (k < buflen)
  101. {
  102. *ptr++ = lib_nibble((buffer[k] >> 4) & 0xf);
  103. *ptr++ = lib_nibble(buffer[k] & 0xf);
  104. }
  105. else
  106. {
  107. *ptr++ = ' ';
  108. *ptr++ = ' ';
  109. }
  110. }
  111. *ptr++ = ' '; /* Plus 1 byte */
  112. /* Generate printable characters: Plus 1 * _NITEMS + 1 bytes */
  113. for (j = 0; j < _NITEMS; j++)
  114. {
  115. k = i + j;
  116. if (j == (_NITEMS / 2))
  117. {
  118. *ptr++ = ' ';
  119. }
  120. if (k < buflen)
  121. {
  122. if (buffer[k] >= 0x20 && buffer[k] < 0x7f)
  123. {
  124. *ptr++ = buffer[k];
  125. }
  126. else
  127. {
  128. *ptr++ = '.';
  129. }
  130. }
  131. }
  132. *ptr = '\0'; /* Plus 1 byte */
  133. syslog(LOG_INFO, "%04x: %s\n", i, buf);
  134. }
  135. }