xref: /nrf52832-nimble/packages/NimBLE-latest/apps/btshell/src/cmd_l2cap.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 <inttypes.h>
21 #include <errno.h>
22 
23 #include "host/ble_gap.h"
24 #include "host/ble_l2cap.h"
25 #include "btshell.h"
26 #include "cmd.h"
27 #include "cmd_l2cap.h"
28 
29 #include "nimble/npl_shell.h"
30 
31 /*****************************************************************************
32  * $l2cap-update                                                             *
33  *****************************************************************************/
34 
35 int
cmd_l2cap_update(int argc,char ** argv)36 cmd_l2cap_update(int argc, char **argv)
37 {
38     struct ble_l2cap_sig_update_params params;
39     uint16_t conn_handle;
40     int rc;
41 
42     rc = parse_arg_all(argc - 1, argv + 1);
43     if (rc != 0) {
44         return rc;
45     }
46 
47     conn_handle = parse_arg_uint16("conn", &rc);
48     if (rc != 0) {
49         console_printf("invalid 'conn' parameter\n");
50         return rc;
51     }
52 
53     params.itvl_min = parse_arg_uint16_dflt("interval_min",
54                                             BLE_GAP_INITIAL_CONN_ITVL_MIN,
55                                             &rc);
56     if (rc != 0) {
57         console_printf("invalid 'interval_min' parameter\n");
58         return rc;
59     }
60 
61     params.itvl_max = parse_arg_uint16_dflt("interval_max",
62                                             BLE_GAP_INITIAL_CONN_ITVL_MAX,
63                                             &rc);
64     if (rc != 0) {
65         console_printf("invalid 'interval_max' parameter\n");
66         return rc;
67     }
68 
69     params.slave_latency = parse_arg_uint16_dflt("latency", 0, &rc);
70     if (rc != 0) {
71         console_printf("invalid 'latency' parameter\n");
72         return rc;
73     }
74 
75     params.timeout_multiplier = parse_arg_uint16_dflt("timeout", 0x0100, &rc);
76     if (rc != 0) {
77         console_printf("invalid 'timeout' parameter\n");
78         return rc;
79     }
80 
81     rc = btshell_l2cap_update(conn_handle, &params);
82     if (rc != 0) {
83         console_printf("error txing l2cap update; rc=%d\n", rc);
84         return rc;
85     }
86 
87     return 0;
88 }
89 
90 /*****************************************************************************
91  * $l2cap-create-server                                                      *
92  *****************************************************************************/
93 
94 int
cmd_l2cap_create_server(int argc,char ** argv)95 cmd_l2cap_create_server(int argc, char **argv)
96 {
97     uint16_t psm = 0;
98     int error;
99     int accept_response = 0;
100     int rc;
101 
102     rc = parse_arg_all(argc - 1, argv + 1);
103     if (rc != 0) {
104         return rc;
105     }
106 
107     psm = parse_arg_uint16("psm", &rc);
108     if (rc != 0) {
109         console_printf("invalid 'psm' parameter\n");
110         return rc;
111     }
112 
113     error = parse_arg_uint32_dflt("error", 0, &rc);
114     if (rc != 0) {
115         console_printf("invalid 'error' parameter\n");
116         return rc;
117     }
118 
119     switch (error) {
120     case 1:
121         accept_response = BLE_HS_EAUTHEN;
122         break;
123     case 2:
124         accept_response = BLE_HS_EAUTHOR;
125         break;
126     case 3:
127         accept_response = BLE_HS_EENCRYPT_KEY_SZ;
128         break;
129     }
130 
131     rc = btshell_l2cap_create_srv(psm, accept_response);
132     if (rc) {
133         console_printf("Server create error: 0x%02x", rc);
134     }
135 
136     return 0;
137 }
138 
139 /*****************************************************************************
140  * $l2cap-connect                                                            *
141  *****************************************************************************/
142 
143 int
cmd_l2cap_connect(int argc,char ** argv)144 cmd_l2cap_connect(int argc, char **argv)
145 {
146     uint16_t conn = 0;
147     uint16_t psm = 0;
148     int rc;
149 
150     rc = parse_arg_all(argc - 1, argv + 1);
151     if (rc != 0) {
152         return rc;
153     }
154 
155     conn = parse_arg_uint16("conn", &rc);
156     if (rc != 0) {
157         console_printf("invalid 'conn' parameter\n");
158         return rc;
159     }
160 
161     psm = parse_arg_uint16("psm", &rc);
162     if (rc != 0) {
163         console_printf("invalid 'psm' parameter\n");
164         return rc;
165     }
166 
167     return btshell_l2cap_connect(conn, psm);
168 }
169 
170 /*****************************************************************************
171  * $l2cap-disconnect                                                         *
172  *****************************************************************************/
173 
174 int
cmd_l2cap_disconnect(int argc,char ** argv)175 cmd_l2cap_disconnect(int argc, char **argv)
176 {
177     uint16_t conn;
178     uint16_t idx;
179     int rc;
180 
181     rc = parse_arg_all(argc - 1, argv + 1);
182     if (rc != 0) {
183         return rc;
184     }
185 
186     conn = parse_arg_uint16("conn", &rc);
187     if (rc != 0) {
188         console_printf("invalid 'conn' parameter\n");
189         return rc;
190     }
191 
192     idx = parse_arg_uint16("idx", &rc);
193     if (rc != 0) {
194         console_printf("invalid 'idx' parameter\n");
195         return rc;
196     }
197 
198     return btshell_l2cap_disconnect(conn, idx);
199 }
200 
201 /*****************************************************************************
202  * $l2cap-send                                                               *
203  *****************************************************************************/
204 
205 int
cmd_l2cap_send(int argc,char ** argv)206 cmd_l2cap_send(int argc, char **argv)
207 {
208     uint16_t conn;
209     uint16_t idx;
210     uint16_t bytes;
211     int rc;
212 
213     rc = parse_arg_all(argc - 1, argv + 1);
214     if (rc != 0) {
215         return rc;
216     }
217 
218     conn = parse_arg_uint16("conn", &rc);
219     if (rc != 0) {
220        console_printf("invalid 'conn' parameter\n");
221        return rc;
222     }
223 
224     idx = parse_arg_uint16("idx", &rc);
225     if (rc != 0) {
226        console_printf("invalid 'idx' parameter\n");
227        return rc;
228     }
229 
230     bytes = parse_arg_uint16("bytes", &rc);
231     if (rc != 0) {
232        console_printf("invalid 'bytes' parameter\n");
233        return rc;
234     }
235 
236     return btshell_l2cap_send(conn, idx, bytes);
237 }
238 
239 int
cmd_l2cap_show_coc(int argc,char ** argv)240 cmd_l2cap_show_coc(int argc, char **argv)
241 {
242     struct btshell_conn *conn = NULL;
243     struct btshell_l2cap_coc *coc;
244     int i, j;
245 
246     for (i = 0; i < btshell_num_conns; i++) {
247         conn = btshell_conns + i;
248 
249         if (SLIST_EMPTY(&conn->coc_list)) {
250             continue;
251         }
252 
253         console_printf("conn_handle: 0x%04x\n", conn->handle);
254         j = 0;
255         SLIST_FOREACH(coc, &conn->coc_list, next) {
256             console_printf("    idx: %i, chan pointer = %p\n", j++, coc->chan);
257         }
258     }
259 
260     return 0;
261 }
262