xref: /nrf52832-nimble/packages/NimBLE-latest/apps/btshell/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 "host/ble_uuid.h"
21 #include "host/ble_gap.h"
22 
23 #include "btshell.h"
24 
25 #include "nimble/npl_shell.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         console_printf("%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             console_printf(":");
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 void
print_addr(const void * addr)58 print_addr(const void *addr)
59 {
60     const uint8_t *u8p;
61 
62     u8p = addr;
63     console_printf("%02x:%02x:%02x:%02x:%02x:%02x",
64                    u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
65 }
66 
67 void
print_uuid(const ble_uuid_t * uuid)68 print_uuid(const ble_uuid_t *uuid)
69 {
70     char buf[BLE_UUID_STR_LEN];
71 
72     ble_uuid_to_str(uuid, buf);
73 
74     console_printf("%s", buf);
75 }
76 
77 int
svc_is_empty(const struct btshell_svc * svc)78 svc_is_empty(const struct btshell_svc *svc)
79 {
80     return svc->svc.end_handle <= svc->svc.start_handle;
81 }
82 
83 uint16_t
chr_end_handle(const struct btshell_svc * svc,const struct btshell_chr * chr)84 chr_end_handle(const struct btshell_svc *svc, const struct btshell_chr *chr)
85 {
86     const struct btshell_chr *next_chr;
87 
88     next_chr = SLIST_NEXT(chr, next);
89     if (next_chr != NULL) {
90         return next_chr->chr.def_handle - 1;
91     } else {
92         return svc->svc.end_handle;
93     }
94 }
95 
96 int
chr_is_empty(const struct btshell_svc * svc,const struct btshell_chr * chr)97 chr_is_empty(const struct btshell_svc *svc, const struct btshell_chr *chr)
98 {
99     return chr_end_handle(svc, chr) <= chr->chr.val_handle;
100 }
101 
102 void
print_conn_desc(const struct ble_gap_conn_desc * desc)103 print_conn_desc(const struct ble_gap_conn_desc *desc)
104 {
105     console_printf("handle=%d our_ota_addr_type=%d our_ota_addr=",
106                    desc->conn_handle, desc->our_ota_addr.type);
107     print_addr(desc->our_ota_addr.val);
108     console_printf(" our_id_addr_type=%d our_id_addr=",
109                    desc->our_id_addr.type);
110     print_addr(desc->our_id_addr.val);
111     console_printf(" peer_ota_addr_type=%d peer_ota_addr=",
112                    desc->peer_ota_addr.type);
113     print_addr(desc->peer_ota_addr.val);
114     console_printf(" peer_id_addr_type=%d peer_id_addr=",
115                    desc->peer_id_addr.type);
116     print_addr(desc->peer_id_addr.val);
117     console_printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d "
118                    "encrypted=%d authenticated=%d bonded=%d\n",
119                    desc->conn_itvl, desc->conn_latency,
120                    desc->supervision_timeout,
121                    desc->sec_state.encrypted,
122                    desc->sec_state.authenticated,
123                    desc->sec_state.bonded);
124 }
125 
126 static void
print_dsc(struct btshell_dsc * dsc)127 print_dsc(struct btshell_dsc *dsc)
128 {
129     console_printf("            dsc_handle=%d uuid=", dsc->dsc.handle);
130     print_uuid(&dsc->dsc.uuid.u);
131     console_printf("\n");
132 }
133 
134 static void
print_chr(struct btshell_chr * chr)135 print_chr(struct btshell_chr *chr)
136 {
137     struct btshell_dsc *dsc;
138 
139     console_printf("        def_handle=%d val_handle=%d properties=0x%02x "
140                    "uuid=", chr->chr.def_handle, chr->chr.val_handle,
141                    chr->chr.properties);
142     print_uuid(&chr->chr.uuid.u);
143     console_printf("\n");
144 
145     SLIST_FOREACH(dsc, &chr->dscs, next) {
146         print_dsc(dsc);
147     }
148 }
149 
150 void
print_svc(struct btshell_svc * svc)151 print_svc(struct btshell_svc *svc)
152 {
153     struct btshell_chr *chr;
154 
155     console_printf("    start=%d end=%d uuid=", svc->svc.start_handle,
156                    svc->svc.end_handle);
157     print_uuid(&svc->svc.uuid.u);
158     console_printf("\n");
159 
160     SLIST_FOREACH(chr, &svc->chrs, next) {
161         print_chr(chr);
162     }
163 }
164