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