xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/include/host/ble_l2cap.h (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 #ifndef H_BLE_L2CAP_
21 #define H_BLE_L2CAP_
22 
23 #include "nimble/nimble_opt.h"
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 struct ble_l2cap_sig_update_req;
29 struct ble_hs_conn;
30 
31 #define BLE_L2CAP_CID_ATT           4
32 #define BLE_L2CAP_CID_SIG           5
33 #define BLE_L2CAP_CID_SM            6
34 
35 #define BLE_L2CAP_SIG_OP_REJECT                 0x01
36 #define BLE_L2CAP_SIG_OP_CONNECT_REQ            0x02
37 #define BLE_L2CAP_SIG_OP_CONNECT_RSP            0x03
38 #define BLE_L2CAP_SIG_OP_CONFIG_REQ             0x04
39 #define BLE_L2CAP_SIG_OP_CONFIG_RSP             0x05
40 #define BLE_L2CAP_SIG_OP_DISCONN_REQ            0x06
41 #define BLE_L2CAP_SIG_OP_DISCONN_RSP            0x07
42 #define BLE_L2CAP_SIG_OP_ECHO_REQ               0x08
43 #define BLE_L2CAP_SIG_OP_ECHO_RSP               0x09
44 #define BLE_L2CAP_SIG_OP_INFO_REQ               0x0a
45 #define BLE_L2CAP_SIG_OP_INFO_RSP               0x0b
46 #define BLE_L2CAP_SIG_OP_CREATE_CHAN_REQ        0x0c
47 #define BLE_L2CAP_SIG_OP_CREATE_CHAN_RSP        0x0d
48 #define BLE_L2CAP_SIG_OP_MOVE_CHAN_REQ          0x0e
49 #define BLE_L2CAP_SIG_OP_MOVE_CHAN_RSP          0x0f
50 #define BLE_L2CAP_SIG_OP_MOVE_CHAN_CONF_REQ     0x10
51 #define BLE_L2CAP_SIG_OP_MOVE_CHAN_CONF_RSP     0x11
52 #define BLE_L2CAP_SIG_OP_UPDATE_REQ             0x12
53 #define BLE_L2CAP_SIG_OP_UPDATE_RSP             0x13
54 #define BLE_L2CAP_SIG_OP_CREDIT_CONNECT_REQ     0x14
55 #define BLE_L2CAP_SIG_OP_CREDIT_CONNECT_RSP     0x15
56 #define BLE_L2CAP_SIG_OP_FLOW_CTRL_CREDIT       0x16
57 #define BLE_L2CAP_SIG_OP_MAX                    0x17
58 
59 #define BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD    0x0000
60 #define BLE_L2CAP_SIG_ERR_MTU_EXCEEDED          0x0001
61 #define BLE_L2CAP_SIG_ERR_INVALID_CID           0x0002
62 
63 #define BLE_L2CAP_COC_ERR_CONNECTION_SUCCESS        0x0000
64 #define BLE_L2CAP_COC_ERR_UNKNOWN_LE_PSM            0x0002
65 #define BLE_L2CAP_COC_ERR_NO_RESOURCES              0x0004
66 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_AUTHEN       0x0005
67 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_AUTHOR       0x0006
68 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_KEY_SZ       0x0007
69 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_ENC          0x0008
70 #define BLE_L2CAP_COC_ERR_INVALID_SOURCE_CID        0x0009
71 #define BLE_L2CAP_COC_ERR_SOURCE_CID_ALREADY_USED   0x000A
72 #define BLE_L2CAP_COC_ERR_UNACCEPTABLE_PARAMETERS   0x000B
73 
74 #define BLE_L2CAP_EVENT_COC_CONNECTED                 0
75 #define BLE_L2CAP_EVENT_COC_DISCONNECTED              1
76 #define BLE_L2CAP_EVENT_COC_ACCEPT                    2
77 #define BLE_L2CAP_EVENT_COC_DATA_RECEIVED             3
78 
79 typedef void ble_l2cap_sig_update_fn(uint16_t conn_handle, int status,
80                                      void *arg);
81 
82 struct ble_l2cap_sig_update_params {
83     uint16_t itvl_min;
84     uint16_t itvl_max;
85     uint16_t slave_latency;
86     uint16_t timeout_multiplier;
87 };
88 
89 int ble_l2cap_sig_update(uint16_t conn_handle,
90                          struct ble_l2cap_sig_update_params *params,
91                          ble_l2cap_sig_update_fn *cb, void *cb_arg);
92 
93 struct ble_l2cap_chan;
94 
95 /**
96  * Represents a L2CAP-related event.
97  * When such an event occurs, the host notifies the application by passing an
98  * instance of this structure to an application-specified callback.
99  */
100 struct ble_l2cap_event {
101     /**
102      * Indicates the type of L2CAP event that occurred.  This is one of the
103      * BLE_L2CAP_EVENT codes.
104      */
105     uint8_t type;
106 
107     /**
108      * A discriminated union containing additional details concerning the L2CAP
109      * event.  The 'type' field indicates which member of the union is valid.
110      */
111     union {
112         /**
113          * Represents a connection attempt. Valid for the following event
114          * types:
115          *     o BLE_L2CAP_EVENT_COC_CONNECTED */
116         struct {
117             /**
118              * The status of the connection attempt;
119              *     o 0: the connection was successfully established.
120              *     o BLE host error code: the connection attempt failed for
121              *       the specified reason.
122              */
123             int status;
124 
125             /** Connection handle of the relevant connection */
126             uint16_t conn_handle;
127 
128             /** The L2CAP channel of the relevant L2CAP connection. */
129             struct ble_l2cap_chan *chan;
130         } connect;
131 
132         /**
133          * Represents a terminated connection. Valid for the following event
134          * types:
135          *     o BLE_L2CAP_EVENT_COC_DISCONNECTED
136          */
137         struct {
138             /** Connection handle of the relevant connection */
139             uint16_t conn_handle;
140 
141             /** Information about the L2CAP connection prior to termination. */
142             struct ble_l2cap_chan *chan;
143         } disconnect;
144 
145         /**
146          * Represents connection accept. Valid for the following event
147          * types:
148          *     o BLE_L2CAP_EVENT_COC_ACCEPT
149          */
150         struct {
151             /** Connection handle of the relevant connection */
152             uint16_t conn_handle;
153 
154             /** MTU supported by peer device on the channel */
155             uint16_t peer_sdu_size;
156 
157             /** The L2CAP channel of the relevant L2CAP connection. */
158             struct ble_l2cap_chan *chan;
159         } accept;
160 
161         /**
162          * Represents received data. Valid for the following event
163          * types:
164          *     o BLE_L2CAP_EVENT_COC_DATA_RECEIVED
165          */
166         struct {
167             /** Connection handle of the relevant connection */
168             uint16_t conn_handle;
169 
170             /** The L2CAP channel of the relevant L2CAP connection. */
171             struct ble_l2cap_chan *chan;
172 
173             /** The mbuf with received SDU. */
174             struct os_mbuf *sdu_rx;
175         } receive;
176     };
177 };
178 
179 typedef int ble_l2cap_event_fn(struct ble_l2cap_event *event, void *arg);
180 
181 uint16_t ble_l2cap_get_conn_handle(struct ble_l2cap_chan *chan);
182 int ble_l2cap_create_server(uint16_t psm, uint16_t mtu,
183                             ble_l2cap_event_fn *cb, void *cb_arg);
184 
185 int ble_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
186                       struct os_mbuf *sdu_rx,
187                       ble_l2cap_event_fn *cb, void *cb_arg);
188 int ble_l2cap_disconnect(struct ble_l2cap_chan *chan);
189 int ble_l2cap_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_tx);
190 void ble_l2cap_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx);
191 int ble_l2cap_get_scid(struct ble_l2cap_chan *chan);
192 int ble_l2cap_get_dcid(struct ble_l2cap_chan *chan);
193 int ble_l2cap_get_our_mtu(struct ble_l2cap_chan *chan);
194 int ble_l2cap_get_peer_mtu(struct ble_l2cap_chan *chan);
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif
201