1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <assert.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include "host/ble_hs.h"
11 #include "host/ble_uuid.h"
12 #include "gattc.h"
13 
14 /**
15  * Utility function to log an array of bytes.
16  */
17 void
print_bytes(const uint8_t * bytes,int len)18 print_bytes(const uint8_t *bytes, int len)
19 {
20     int i;
21 
22     for (i = 0; i < len; i++) {
23         MODLOG_DFLT(DEBUG, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
24     }
25 }
26 
27 void
print_mbuf(const struct os_mbuf * om)28 print_mbuf(const struct os_mbuf *om)
29 {
30     int colon, i;
31 
32     colon = 0;
33     while (om != NULL) {
34         if (colon) {
35             MODLOG_DFLT(INFO, ":");
36         } else {
37             colon = 1;
38         }
39         for (i = 0; i < om->om_len; i++) {
40             MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", om->om_data[i]);
41         }
42         om = SLIST_NEXT(om, om_next);
43     }
44 }
45 
46 char *
addr_str(const void * addr)47 addr_str(const void *addr)
48 {
49     static char buf[6 * 2 + 5 + 1];
50     const uint8_t *u8p;
51 
52     u8p = addr;
53     sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
54             u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
55 
56     return buf;
57 }
58 
59 void
print_uuid(const ble_uuid_t * uuid)60 print_uuid(const ble_uuid_t *uuid)
61 {
62     char buf[BLE_UUID_STR_LEN];
63 
64     MODLOG_DFLT(DEBUG, "%s", ble_uuid_to_str(uuid, buf));
65 }
66 
67 /**
68  * Logs information about a connection to the console.
69  */
70 void
print_conn_desc(const struct ble_gap_conn_desc * desc)71 print_conn_desc(const struct ble_gap_conn_desc *desc)
72 {
73     MODLOG_DFLT(DEBUG, "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(DEBUG, "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(DEBUG, "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(DEBUG, "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(DEBUG, "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 
91 
92 void
print_adv_fields(const struct ble_hs_adv_fields * fields)93 print_adv_fields(const struct ble_hs_adv_fields *fields)
94 {
95     char s[BLE_HS_ADV_MAX_SZ];
96     const uint8_t *u8p;
97     int i;
98 
99     if (fields->flags != 0) {
100         MODLOG_DFLT(DEBUG, "    flags=0x%02x\n", fields->flags);
101     }
102 
103     if (fields->uuids16 != NULL) {
104         MODLOG_DFLT(DEBUG, "    uuids16(%scomplete)=",
105                     fields->uuids16_is_complete ? "" : "in");
106         for (i = 0; i < fields->num_uuids16; i++) {
107             print_uuid(&fields->uuids16[i].u);
108             MODLOG_DFLT(DEBUG, " ");
109         }
110         MODLOG_DFLT(DEBUG, "\n");
111     }
112 
113     if (fields->uuids32 != NULL) {
114         MODLOG_DFLT(DEBUG, "    uuids32(%scomplete)=",
115                     fields->uuids32_is_complete ? "" : "in");
116         for (i = 0; i < fields->num_uuids32; i++) {
117             print_uuid(&fields->uuids32[i].u);
118             MODLOG_DFLT(DEBUG, " ");
119         }
120         MODLOG_DFLT(DEBUG, "\n");
121     }
122 
123     if (fields->uuids128 != NULL) {
124         MODLOG_DFLT(DEBUG, "    uuids128(%scomplete)=",
125                     fields->uuids128_is_complete ? "" : "in");
126         for (i = 0; i < fields->num_uuids128; i++) {
127             print_uuid(&fields->uuids128[i].u);
128             MODLOG_DFLT(DEBUG, " ");
129         }
130         MODLOG_DFLT(DEBUG, "\n");
131     }
132 
133     if (fields->name != NULL) {
134         assert(fields->name_len < sizeof s - 1);
135         memcpy(s, fields->name, fields->name_len);
136         s[fields->name_len] = '\0';
137         MODLOG_DFLT(DEBUG, "    name(%scomplete)=%s\n",
138                     fields->name_is_complete ? "" : "in", s);
139     }
140 
141     if (fields->tx_pwr_lvl_is_present) {
142         MODLOG_DFLT(DEBUG, "    tx_pwr_lvl=%d\n", fields->tx_pwr_lvl);
143     }
144 
145     if (fields->slave_itvl_range != NULL) {
146         MODLOG_DFLT(DEBUG, "    slave_itvl_range=");
147         print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
148         MODLOG_DFLT(DEBUG, "\n");
149     }
150 
151     if (fields->svc_data_uuid16 != NULL) {
152         MODLOG_DFLT(DEBUG, "    svc_data_uuid16=");
153         print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len);
154         MODLOG_DFLT(DEBUG, "\n");
155     }
156 
157     if (fields->public_tgt_addr != NULL) {
158         MODLOG_DFLT(DEBUG, "    public_tgt_addr=");
159         u8p = fields->public_tgt_addr;
160         for (i = 0; i < fields->num_public_tgt_addrs; i++) {
161             MODLOG_DFLT(DEBUG, "public_tgt_addr=%s ", addr_str(u8p));
162             u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
163         }
164         MODLOG_DFLT(DEBUG, "\n");
165     }
166 
167     if (fields->appearance_is_present) {
168         MODLOG_DFLT(DEBUG, "    appearance=0x%04x\n", fields->appearance);
169     }
170 
171     if (fields->adv_itvl_is_present) {
172         MODLOG_DFLT(DEBUG, "    adv_itvl=0x%04x\n", fields->adv_itvl);
173     }
174 
175     if (fields->svc_data_uuid32 != NULL) {
176         MODLOG_DFLT(DEBUG, "    svc_data_uuid32=");
177         print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len);
178         MODLOG_DFLT(DEBUG, "\n");
179     }
180 
181     if (fields->svc_data_uuid128 != NULL) {
182         MODLOG_DFLT(DEBUG, "    svc_data_uuid128=");
183         print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len);
184         MODLOG_DFLT(DEBUG, "\n");
185     }
186 
187     if (fields->uri != NULL) {
188         MODLOG_DFLT(DEBUG, "    uri=");
189         print_bytes(fields->uri, fields->uri_len);
190         MODLOG_DFLT(DEBUG, "\n");
191     }
192 
193     if (fields->mfg_data != NULL) {
194         MODLOG_DFLT(DEBUG, "    mfg_data=");
195         print_bytes(fields->mfg_data, fields->mfg_data_len);
196         MODLOG_DFLT(DEBUG, "\n");
197     }
198 }
199