xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/src/ble_l2cap_priv.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_L2CAP_PRIV_
21 #define H_L2CAP_PRIV_
22 
23 #include "ble_l2cap_coc_priv.h"
24 #include "host/ble_l2cap.h"
25 #include <inttypes.h>
26 #include "stats/stats.h"
27 #include "os/queue.h"
28 #include "os/os_mbuf.h"
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 struct ble_hs_conn;
34 struct hci_data_hdr;
35 
36 STATS_SECT_START(ble_l2cap_stats)
37     STATS_SECT_ENTRY(chan_create)
38     STATS_SECT_ENTRY(chan_delete)
39     STATS_SECT_ENTRY(update_init)
40     STATS_SECT_ENTRY(update_rx)
41     STATS_SECT_ENTRY(update_fail)
42     STATS_SECT_ENTRY(proc_timeout)
43     STATS_SECT_ENTRY(sig_tx)
44     STATS_SECT_ENTRY(sig_rx)
45     STATS_SECT_ENTRY(sm_tx)
46     STATS_SECT_ENTRY(sm_rx)
47 STATS_SECT_END
48 extern STATS_SECT_DECL(ble_l2cap_stats) ble_l2cap_stats;
49 
50 extern struct os_mempool ble_l2cap_chan_pool;
51 
52 /* This is nimble specific; packets sent to the black hole CID do not elicit
53  * an "invalid CID" response.
54  */
55 #define BLE_L2CAP_CID_BLACK_HOLE    0xffff
56 
57 #define BLE_L2CAP_HDR_SZ    4
58 
59 typedef uint8_t ble_l2cap_chan_flags;
60 
61 typedef int ble_l2cap_rx_fn(struct ble_l2cap_chan *chan);
62 
63 struct ble_l2cap_chan {
64     SLIST_ENTRY(ble_l2cap_chan) next;
65     uint16_t conn_handle;
66     uint16_t dcid;
67     uint16_t scid;
68     uint16_t my_mtu;
69     uint16_t peer_mtu;      /* 0 if not exchanged. */
70     ble_l2cap_chan_flags flags;
71 
72     struct os_mbuf *rx_buf;
73     uint16_t rx_len;        /* Length of current reassembled rx packet. */
74 
75     ble_l2cap_rx_fn *rx_fn;
76 
77 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
78     uint16_t psm;
79     struct ble_l2cap_coc_endpoint coc_rx;
80     struct ble_l2cap_coc_endpoint coc_tx;
81     uint16_t initial_credits;
82     ble_l2cap_event_fn *cb;
83     void *cb_arg;
84 #endif
85 };
86 
87 struct ble_l2cap_hdr {
88     uint16_t len;
89     uint16_t cid;
90 };
91 
92 typedef int ble_l2cap_tx_fn(struct ble_hs_conn *conn,
93                             struct ble_l2cap_chan *chan);
94 
95 #define BLE_L2CAP_CHAN_F_TXED_MTU       0x01    /* We have sent our MTU. */
96 
97 SLIST_HEAD(ble_l2cap_chan_list, ble_l2cap_chan);
98 
99 int ble_l2cap_parse_hdr(struct os_mbuf *om, int off,
100                         struct ble_l2cap_hdr *l2cap_hdr);
101 struct os_mbuf *ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid,
102                                       uint16_t len);
103 
104 struct ble_l2cap_chan *ble_l2cap_chan_alloc(uint16_t conn_handle);
105 void ble_l2cap_chan_free(struct ble_l2cap_chan *chan);
106 
107 bool ble_l2cap_is_mtu_req_sent(const struct ble_l2cap_chan *chan);
108 
109 int ble_l2cap_rx(struct ble_hs_conn *conn,
110                  struct hci_data_hdr *hci_hdr,
111                  struct os_mbuf *om,
112                  ble_l2cap_rx_fn **out_rx_cb,
113                  int *out_reject_cid);
114 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
115                  struct os_mbuf *txom);
116 
117 void ble_l2cap_remove_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan);
118 
119 int ble_l2cap_init(void);
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif
126