misc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <assert.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "host/ble_hs.h"
  23. #include "host/ble_uuid.h"
  24. #include "blecent.h"
  25. /**
  26. * Utility function to log an array of bytes.
  27. */
  28. void
  29. print_bytes(const uint8_t *bytes, int len)
  30. {
  31. int i;
  32. for (i = 0; i < len; i++) {
  33. MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
  34. }
  35. }
  36. void
  37. print_mbuf(const struct os_mbuf *om)
  38. {
  39. int colon;
  40. colon = 0;
  41. while (om != NULL) {
  42. if (colon) {
  43. MODLOG_DFLT(INFO, ":");
  44. } else {
  45. colon = 1;
  46. }
  47. print_bytes(om->om_data, om->om_len);
  48. om = SLIST_NEXT(om, om_next);
  49. }
  50. }
  51. char *
  52. addr_str(const void *addr)
  53. {
  54. static char buf[6 * 2 + 5 + 1];
  55. const uint8_t *u8p;
  56. u8p = addr;
  57. sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  58. u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  59. return buf;
  60. }
  61. void
  62. print_uuid(const ble_uuid_t *uuid)
  63. {
  64. char buf[BLE_UUID_STR_LEN];
  65. MODLOG_DFLT(INFO, "%s", ble_uuid_to_str(uuid, buf));
  66. }
  67. /**
  68. * Logs information about a connection to the console.
  69. */
  70. void
  71. print_conn_desc(const struct ble_gap_conn_desc *desc)
  72. {
  73. MODLOG_DFLT(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=%s ",
  74. desc->conn_handle, desc->our_ota_addr.type,
  75. addr_str(desc->our_ota_addr.val));
  76. MODLOG_DFLT(INFO, "our_id_addr_type=%d our_id_addr=%s ",
  77. desc->our_id_addr.type, addr_str(desc->our_id_addr.val));
  78. MODLOG_DFLT(INFO, "peer_ota_addr_type=%d peer_ota_addr=%s ",
  79. desc->peer_ota_addr.type, addr_str(desc->peer_ota_addr.val));
  80. MODLOG_DFLT(INFO, "peer_id_addr_type=%d peer_id_addr=%s ",
  81. desc->peer_id_addr.type, addr_str(desc->peer_id_addr.val));
  82. MODLOG_DFLT(INFO, "conn_itvl=%d conn_latency=%d supervision_timeout=%d "
  83. "encrypted=%d authenticated=%d bonded=%d",
  84. desc->conn_itvl, desc->conn_latency,
  85. desc->supervision_timeout,
  86. desc->sec_state.encrypted,
  87. desc->sec_state.authenticated,
  88. desc->sec_state.bonded);
  89. }
  90. void
  91. print_adv_fields(const struct ble_hs_adv_fields *fields)
  92. {
  93. char s[BLE_HS_ADV_MAX_SZ];
  94. const uint8_t *u8p;
  95. int i;
  96. if (fields->flags != 0) {
  97. MODLOG_DFLT(INFO, " flags=0x%02x\n", fields->flags);
  98. }
  99. if (fields->uuids16 != NULL) {
  100. MODLOG_DFLT(INFO, " uuids16(%scomplete)=",
  101. fields->uuids16_is_complete ? "" : "in");
  102. for (i = 0; i < fields->num_uuids16; i++) {
  103. print_uuid(&fields->uuids16[i].u);
  104. MODLOG_DFLT(INFO, " ");
  105. }
  106. MODLOG_DFLT(INFO, "\n");
  107. }
  108. if (fields->uuids32 != NULL) {
  109. MODLOG_DFLT(INFO, " uuids32(%scomplete)=",
  110. fields->uuids32_is_complete ? "" : "in");
  111. for (i = 0; i < fields->num_uuids32; i++) {
  112. print_uuid(&fields->uuids32[i].u);
  113. MODLOG_DFLT(INFO, " ");
  114. }
  115. MODLOG_DFLT(INFO, "\n");
  116. }
  117. if (fields->uuids128 != NULL) {
  118. MODLOG_DFLT(INFO, " uuids128(%scomplete)=",
  119. fields->uuids128_is_complete ? "" : "in");
  120. for (i = 0; i < fields->num_uuids128; i++) {
  121. print_uuid(&fields->uuids128[i].u);
  122. MODLOG_DFLT(INFO, " ");
  123. }
  124. MODLOG_DFLT(INFO, "\n");
  125. }
  126. if (fields->name != NULL) {
  127. assert(fields->name_len < sizeof s - 1);
  128. memcpy(s, fields->name, fields->name_len);
  129. s[fields->name_len] = '\0';
  130. MODLOG_DFLT(INFO, " name(%scomplete)=%s\n",
  131. fields->name_is_complete ? "" : "in", s);
  132. }
  133. if (fields->tx_pwr_lvl_is_present) {
  134. MODLOG_DFLT(INFO, " tx_pwr_lvl=%d\n", fields->tx_pwr_lvl);
  135. }
  136. if (fields->slave_itvl_range != NULL) {
  137. MODLOG_DFLT(INFO, " slave_itvl_range=");
  138. print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
  139. MODLOG_DFLT(INFO, "\n");
  140. }
  141. if (fields->svc_data_uuid16 != NULL) {
  142. MODLOG_DFLT(INFO, " svc_data_uuid16=");
  143. print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len);
  144. MODLOG_DFLT(INFO, "\n");
  145. }
  146. if (fields->public_tgt_addr != NULL) {
  147. MODLOG_DFLT(INFO, " public_tgt_addr=");
  148. u8p = fields->public_tgt_addr;
  149. for (i = 0; i < fields->num_public_tgt_addrs; i++) {
  150. MODLOG_DFLT(INFO, "public_tgt_addr=%s ", addr_str(u8p));
  151. u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
  152. }
  153. MODLOG_DFLT(INFO, "\n");
  154. }
  155. if (fields->appearance_is_present) {
  156. MODLOG_DFLT(INFO, " appearance=0x%04x\n", fields->appearance);
  157. }
  158. if (fields->adv_itvl_is_present) {
  159. MODLOG_DFLT(INFO, " adv_itvl=0x%04x\n", fields->adv_itvl);
  160. }
  161. if (fields->svc_data_uuid32 != NULL) {
  162. MODLOG_DFLT(INFO, " svc_data_uuid32=");
  163. print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len);
  164. MODLOG_DFLT(INFO, "\n");
  165. }
  166. if (fields->svc_data_uuid128 != NULL) {
  167. MODLOG_DFLT(INFO, " svc_data_uuid128=");
  168. print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len);
  169. MODLOG_DFLT(INFO, "\n");
  170. }
  171. if (fields->uri != NULL) {
  172. MODLOG_DFLT(INFO, " uri=");
  173. print_bytes(fields->uri, fields->uri_len);
  174. MODLOG_DFLT(INFO, "\n");
  175. }
  176. if (fields->mfg_data != NULL) {
  177. MODLOG_DFLT(INFO, " mfg_data=");
  178. print_bytes(fields->mfg_data, fields->mfg_data_len);
  179. MODLOG_DFLT(INFO, "\n");
  180. }
  181. }