misc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(DEBUG, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
  34. }
  35. }
  36. void
  37. print_mbuf(const struct os_mbuf *om)
  38. {
  39. int colon, i;
  40. colon = 0;
  41. while (om != NULL) {
  42. if (colon) {
  43. MODLOG_DFLT(INFO, ":");
  44. } else {
  45. colon = 1;
  46. }
  47. for (i = 0; i < om->om_len; i++) {
  48. MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", om->om_data[i]);
  49. }
  50. om = SLIST_NEXT(om, om_next);
  51. }
  52. }
  53. char *
  54. addr_str(const void *addr)
  55. {
  56. static char buf[6 * 2 + 5 + 1];
  57. const uint8_t *u8p;
  58. u8p = addr;
  59. sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
  60. u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  61. return buf;
  62. }
  63. void
  64. print_uuid(const ble_uuid_t *uuid)
  65. {
  66. char buf[BLE_UUID_STR_LEN];
  67. MODLOG_DFLT(DEBUG, "%s", ble_uuid_to_str(uuid, buf));
  68. }
  69. /**
  70. * Logs information about a connection to the console.
  71. */
  72. void
  73. print_conn_desc(const struct ble_gap_conn_desc *desc)
  74. {
  75. MODLOG_DFLT(DEBUG, "handle=%d our_ota_addr_type=%d our_ota_addr=%s ",
  76. desc->conn_handle, desc->our_ota_addr.type,
  77. addr_str(desc->our_ota_addr.val));
  78. MODLOG_DFLT(DEBUG, "our_id_addr_type=%d our_id_addr=%s ",
  79. desc->our_id_addr.type, addr_str(desc->our_id_addr.val));
  80. MODLOG_DFLT(DEBUG, "peer_ota_addr_type=%d peer_ota_addr=%s ",
  81. desc->peer_ota_addr.type, addr_str(desc->peer_ota_addr.val));
  82. MODLOG_DFLT(DEBUG, "peer_id_addr_type=%d peer_id_addr=%s ",
  83. desc->peer_id_addr.type, addr_str(desc->peer_id_addr.val));
  84. MODLOG_DFLT(DEBUG, "conn_itvl=%d conn_latency=%d supervision_timeout=%d "
  85. "encrypted=%d authenticated=%d bonded=%d",
  86. desc->conn_itvl, desc->conn_latency,
  87. desc->supervision_timeout,
  88. desc->sec_state.encrypted,
  89. desc->sec_state.authenticated,
  90. desc->sec_state.bonded);
  91. }
  92. #if CONFIG_EXAMPLE_EXTENDED_ADV
  93. void
  94. print_addr(const void *addr, const char *name)
  95. {
  96. const uint8_t *u8p;
  97. u8p = addr;
  98. MODLOG_DFLT(DEBUG, "%s = %02x:%02x:%02x:%02x:%02x:%02x",
  99. name, u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  100. }
  101. void
  102. ext_print_adv_report(const void *param)
  103. {
  104. const struct ble_gap_ext_disc_desc *disc = (struct ble_gap_ext_disc_desc *)param;
  105. MODLOG_DFLT(DEBUG, "props=%d data_status=%d legacy_event_type=%d", disc->props, disc->data_status, disc->legacy_event_type);
  106. print_addr(disc->addr.val, "address");
  107. MODLOG_DFLT(DEBUG, "rssi=%d tx_power=%d", disc->rssi, disc->tx_power);
  108. MODLOG_DFLT(DEBUG, "sid=%d prim_phy=%d sec_phy=%d", disc->sid, disc->prim_phy, disc->sec_phy);
  109. MODLOG_DFLT(DEBUG, "periodic_adv_itvl=%d length_data=%d", disc->periodic_adv_itvl, disc->length_data);
  110. print_addr(disc->direct_addr.val, "direct address");
  111. }
  112. #endif
  113. void
  114. print_adv_fields(const struct ble_hs_adv_fields *fields)
  115. {
  116. char s[BLE_HS_ADV_MAX_SZ];
  117. const uint8_t *u8p;
  118. int i;
  119. if (fields->flags != 0) {
  120. MODLOG_DFLT(DEBUG, " flags=0x%02x\n", fields->flags);
  121. }
  122. if (fields->uuids16 != NULL) {
  123. MODLOG_DFLT(DEBUG, " uuids16(%scomplete)=",
  124. fields->uuids16_is_complete ? "" : "in");
  125. for (i = 0; i < fields->num_uuids16; i++) {
  126. print_uuid(&fields->uuids16[i].u);
  127. MODLOG_DFLT(DEBUG, " ");
  128. }
  129. MODLOG_DFLT(DEBUG, "\n");
  130. }
  131. if (fields->uuids32 != NULL) {
  132. MODLOG_DFLT(DEBUG, " uuids32(%scomplete)=",
  133. fields->uuids32_is_complete ? "" : "in");
  134. for (i = 0; i < fields->num_uuids32; i++) {
  135. print_uuid(&fields->uuids32[i].u);
  136. MODLOG_DFLT(DEBUG, " ");
  137. }
  138. MODLOG_DFLT(DEBUG, "\n");
  139. }
  140. if (fields->uuids128 != NULL) {
  141. MODLOG_DFLT(DEBUG, " uuids128(%scomplete)=",
  142. fields->uuids128_is_complete ? "" : "in");
  143. for (i = 0; i < fields->num_uuids128; i++) {
  144. print_uuid(&fields->uuids128[i].u);
  145. MODLOG_DFLT(DEBUG, " ");
  146. }
  147. MODLOG_DFLT(DEBUG, "\n");
  148. }
  149. if (fields->name != NULL) {
  150. assert(fields->name_len < sizeof s - 1);
  151. memcpy(s, fields->name, fields->name_len);
  152. s[fields->name_len] = '\0';
  153. MODLOG_DFLT(DEBUG, " name(%scomplete)=%s\n",
  154. fields->name_is_complete ? "" : "in", s);
  155. }
  156. if (fields->tx_pwr_lvl_is_present) {
  157. MODLOG_DFLT(DEBUG, " tx_pwr_lvl=%d\n", fields->tx_pwr_lvl);
  158. }
  159. if (fields->slave_itvl_range != NULL) {
  160. MODLOG_DFLT(DEBUG, " slave_itvl_range=");
  161. print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
  162. MODLOG_DFLT(DEBUG, "\n");
  163. }
  164. if (fields->svc_data_uuid16 != NULL) {
  165. MODLOG_DFLT(DEBUG, " svc_data_uuid16=");
  166. print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len);
  167. MODLOG_DFLT(DEBUG, "\n");
  168. }
  169. if (fields->public_tgt_addr != NULL) {
  170. MODLOG_DFLT(DEBUG, " public_tgt_addr=");
  171. u8p = fields->public_tgt_addr;
  172. for (i = 0; i < fields->num_public_tgt_addrs; i++) {
  173. MODLOG_DFLT(DEBUG, "public_tgt_addr=%s ", addr_str(u8p));
  174. u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
  175. }
  176. MODLOG_DFLT(DEBUG, "\n");
  177. }
  178. if (fields->appearance_is_present) {
  179. MODLOG_DFLT(DEBUG, " appearance=0x%04x\n", fields->appearance);
  180. }
  181. if (fields->adv_itvl_is_present) {
  182. MODLOG_DFLT(DEBUG, " adv_itvl=0x%04x\n", fields->adv_itvl);
  183. }
  184. if (fields->svc_data_uuid32 != NULL) {
  185. MODLOG_DFLT(DEBUG, " svc_data_uuid32=");
  186. print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len);
  187. MODLOG_DFLT(DEBUG, "\n");
  188. }
  189. if (fields->svc_data_uuid128 != NULL) {
  190. MODLOG_DFLT(DEBUG, " svc_data_uuid128=");
  191. print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len);
  192. MODLOG_DFLT(DEBUG, "\n");
  193. }
  194. if (fields->uri != NULL) {
  195. MODLOG_DFLT(DEBUG, " uri=");
  196. print_bytes(fields->uri, fields->uri_len);
  197. MODLOG_DFLT(DEBUG, "\n");
  198. }
  199. if (fields->mfg_data != NULL) {
  200. MODLOG_DFLT(DEBUG, " mfg_data=");
  201. print_bytes(fields->mfg_data, fields->mfg_data_len);
  202. MODLOG_DFLT(DEBUG, "\n");
  203. }
  204. }