log.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* log.c - logging helpers */
  2. /*
  3. * Copyright (c) 2017 Nordic Semiconductor ASA
  4. * Copyright (c) 2016 Intel Corporation
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. /* Helper for printk parameters to convert from binary to hex.
  9. * We declare multiple buffers so the helper can be used multiple times
  10. * in a single printk call.
  11. */
  12. #include <stddef.h>
  13. #include <zephyr/types.h>
  14. #include <zephyr.h>
  15. #include <sys/util.h>
  16. #include <acts_bluetooth/bluetooth.h>
  17. #include <acts_bluetooth/uuid.h>
  18. #include <acts_bluetooth/hci.h>
  19. #define LOG_DIRECT_OUTPUT 1
  20. const char *bt_hex_real(const void *buf, size_t len)
  21. {
  22. #if LOG_DIRECT_OUTPUT
  23. static const char *retStr = "bt_hex";
  24. const uint8_t *data = buf;
  25. size_t i;
  26. printk("%s: ", retStr);
  27. for (i=0; i<len; i++) {
  28. printk("%02x", data[i]);
  29. }
  30. printk("\n");
  31. return retStr;
  32. #else
  33. static const char hex[] = "0123456789abcdef";
  34. static char str[129];
  35. const uint8_t *b = buf;
  36. size_t i;
  37. len = MIN(len, (sizeof(str) - 1) / 2);
  38. for (i = 0; i < len; i++) {
  39. str[i * 2] = hex[b[i] >> 4];
  40. str[i * 2 + 1] = hex[b[i] & 0xf];
  41. }
  42. str[i * 2] = '\0';
  43. return str;
  44. #endif
  45. }
  46. const char *bt_addr_str_real(const bt_addr_t *addr)
  47. {
  48. #if LOG_DIRECT_OUTPUT
  49. static const char *retStr = "bt_addr";
  50. int i;
  51. printk("%s: ", retStr);
  52. for (i=0; i<6; i++) {
  53. printk("%02x%s", addr->val[5-i], (5 - i)? ":" : "");
  54. }
  55. printk("\n");
  56. return retStr;
  57. #else
  58. static char str[BT_ADDR_STR_LEN];
  59. bt_addr_to_str(addr, str, sizeof(str));
  60. return str;
  61. #endif
  62. }
  63. const char *bt_addr_le_str_real(const bt_addr_le_t *addr)
  64. {
  65. #if LOG_DIRECT_OUTPUT
  66. static const char *retStr = "bt_addr_le";
  67. int i;
  68. printk("%s: ", retStr);
  69. switch (addr->type) {
  70. case BT_ADDR_LE_PUBLIC:
  71. printk("public ");
  72. break;
  73. case BT_ADDR_LE_RANDOM:
  74. printk("random ");
  75. break;
  76. default:
  77. printk("type(0x%02x) ", addr->type);
  78. break;
  79. }
  80. for (i=0; i<6; i++) {
  81. printk("%02x%s", addr->a.val[5-i], (5 - i)? ":" : "");
  82. }
  83. printk("\n");
  84. return retStr;
  85. #else
  86. static char str[BT_ADDR_LE_STR_LEN];
  87. bt_addr_le_to_str(addr, str, sizeof(str));
  88. return str;
  89. #endif
  90. }
  91. const char *bt_uuid_str_real(const struct bt_uuid *uuid)
  92. {
  93. static char str[BT_UUID_STR_LEN];
  94. bt_uuid_to_str(uuid, str, sizeof(str));
  95. return str;
  96. }