• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.h>
24 #include "ble_l2cap_coc_priv.h"
25 #include "host/ble_l2cap.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 
69     /* Unions just to avoid confusion on MPS/MTU.
70      * In CoC context, L2CAP MTU is MPS
71      */
72     union {
73         uint16_t my_mtu;
74         uint16_t my_coc_mps;
75     };
76 
77     union {
78         uint16_t peer_mtu;
79         uint16_t peer_coc_mps;
80     };
81 
82     ble_l2cap_chan_flags flags;
83 
84     struct os_mbuf *rx_buf;
85     uint16_t rx_len;        /* Length of current reassembled rx packet. */
86 
87     ble_l2cap_rx_fn *rx_fn;
88 
89 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
90     uint16_t psm;
91     struct ble_l2cap_coc_endpoint coc_rx;
92     struct ble_l2cap_coc_endpoint coc_tx;
93     uint16_t initial_credits;
94     ble_l2cap_event_fn *cb;
95     void *cb_arg;
96 #endif
97 };
98 
99 struct ble_l2cap_hdr {
100     uint16_t len;
101     uint16_t cid;
102 };
103 
104 typedef int ble_l2cap_tx_fn(struct ble_hs_conn *conn,
105                             struct ble_l2cap_chan *chan);
106 
107 #define BLE_L2CAP_CHAN_F_TXED_MTU       0x01    /* We have sent our MTU. */
108 
109 SLIST_HEAD(ble_l2cap_chan_list, ble_l2cap_chan);
110 
111 int ble_l2cap_parse_hdr(struct os_mbuf *om, int off,
112                         struct ble_l2cap_hdr *l2cap_hdr);
113 struct os_mbuf *ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid,
114                                       uint16_t len);
115 
116 struct ble_l2cap_chan *ble_l2cap_chan_alloc(uint16_t conn_handle);
117 void ble_l2cap_chan_free(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan);
118 
119 bool ble_l2cap_is_mtu_req_sent(const struct ble_l2cap_chan *chan);
120 
121 int ble_l2cap_rx(struct ble_hs_conn *conn,
122                  struct hci_data_hdr *hci_hdr,
123                  struct os_mbuf *om,
124                  ble_l2cap_rx_fn **out_rx_cb,
125                  int *out_reject_cid);
126 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
127                  struct os_mbuf *txom);
128 
129 void ble_l2cap_remove_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan);
130 
131 int ble_l2cap_init(void);
132 
133 /* Below experimental API is available when BLE_VERSION >= 52 */
134 int ble_l2cap_enhanced_connect(uint16_t conn_handle,
135                                uint16_t psm, uint16_t mtu,
136                                uint8_t num, struct os_mbuf *sdu_rx[],
137                                ble_l2cap_event_fn *cb, void *cb_arg);
138 int ble_l2cap_reconfig(struct ble_l2cap_chan *chans[], uint8_t num, uint16_t new_mtu);
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif
145