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