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_LE_CREDIT_CONNECT_REQ 0x14 55 #define BLE_L2CAP_SIG_OP_LE_CREDIT_CONNECT_RSP 0x15 56 #define BLE_L2CAP_SIG_OP_FLOW_CTRL_CREDIT 0x16 57 #define BLE_L2CAP_SIG_OP_CREDIT_CONNECT_REQ 0x17 58 #define BLE_L2CAP_SIG_OP_CREDIT_CONNECT_RSP 0x18 59 #define BLE_L2CAP_SIG_OP_CREDIT_RECONFIG_REQ 0x19 60 #define BLE_L2CAP_SIG_OP_CREDIT_RECONFIG_RSP 0x1A 61 #define BLE_L2CAP_SIG_OP_MAX 0x1B 62 63 #define BLE_L2CAP_SIG_ERR_CMD_NOT_UNDERSTOOD 0x0000 64 #define BLE_L2CAP_SIG_ERR_MTU_EXCEEDED 0x0001 65 #define BLE_L2CAP_SIG_ERR_INVALID_CID 0x0002 66 67 #define BLE_L2CAP_COC_ERR_CONNECTION_SUCCESS 0x0000 68 #define BLE_L2CAP_COC_ERR_UNKNOWN_LE_PSM 0x0002 69 #define BLE_L2CAP_COC_ERR_NO_RESOURCES 0x0004 70 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_AUTHEN 0x0005 71 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_AUTHOR 0x0006 72 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_KEY_SZ 0x0007 73 #define BLE_L2CAP_COC_ERR_INSUFFICIENT_ENC 0x0008 74 #define BLE_L2CAP_COC_ERR_INVALID_SOURCE_CID 0x0009 75 #define BLE_L2CAP_COC_ERR_SOURCE_CID_ALREADY_USED 0x000A 76 #define BLE_L2CAP_COC_ERR_UNACCEPTABLE_PARAMETERS 0x000B 77 #define BLE_L2CAP_COC_ERR_INVALID_PARAMETERS 0x000C 78 79 #define BLE_L2CAP_ERR_RECONFIG_SUCCEED 0x0000 80 #define BLE_L2CAP_ERR_RECONFIG_REDUCTION_MTU_NOT_ALLOWED 0x0001 81 #define BLE_L2CAP_ERR_RECONFIG_REDUCTION_MPS_NOT_ALLOWED 0x0002 82 #define BLE_L2CAP_ERR_RECONFIG_INVALID_DCID 0x0003 83 #define BLE_L2CAP_ERR_RECONFIG_UNACCAPTED_PARAM 0x0004 84 85 #define BLE_L2CAP_EVENT_COC_CONNECTED 0 86 #define BLE_L2CAP_EVENT_COC_DISCONNECTED 1 87 #define BLE_L2CAP_EVENT_COC_ACCEPT 2 88 #define BLE_L2CAP_EVENT_COC_DATA_RECEIVED 3 89 #define BLE_L2CAP_EVENT_COC_TX_UNSTALLED 4 90 #define BLE_L2CAP_EVENT_COC_RECONFIG_COMPLETED 5 91 #define BLE_L2CAP_EVENT_COC_PEER_RECONFIGURED 6 92 93 typedef void ble_l2cap_sig_update_fn(uint16_t conn_handle, int status, 94 void *arg); 95 96 struct ble_l2cap_sig_update_params { 97 uint16_t itvl_min; 98 uint16_t itvl_max; 99 uint16_t slave_latency; 100 uint16_t timeout_multiplier; 101 }; 102 103 int ble_l2cap_sig_update(uint16_t conn_handle, 104 struct ble_l2cap_sig_update_params *params, 105 ble_l2cap_sig_update_fn *cb, void *cb_arg); 106 107 struct ble_l2cap_chan; 108 109 /** 110 * Represents a L2CAP-related event. 111 * When such an event occurs, the host notifies the application by passing an 112 * instance of this structure to an application-specified callback. 113 */ 114 struct ble_l2cap_event { 115 /** 116 * Indicates the type of L2CAP event that occurred. This is one of the 117 * BLE_L2CAP_EVENT codes. 118 */ 119 uint8_t type; 120 121 /** 122 * A discriminated union containing additional details concerning the L2CAP 123 * event. The 'type' field indicates which member of the union is valid. 124 */ 125 union { 126 /** 127 * Represents a connection attempt. Valid for the following event 128 * types: 129 * o BLE_L2CAP_EVENT_COC_CONNECTED */ 130 struct { 131 /** 132 * The status of the connection attempt; 133 * o 0: the connection was successfully established. 134 * o BLE host error code: the connection attempt failed for 135 * the specified reason. 136 */ 137 int status; 138 139 /** Connection handle of the relevant connection */ 140 uint16_t conn_handle; 141 142 /** The L2CAP channel of the relevant L2CAP connection. */ 143 struct ble_l2cap_chan *chan; 144 } connect; 145 146 /** 147 * Represents a terminated connection. Valid for the following event 148 * types: 149 * o BLE_L2CAP_EVENT_COC_DISCONNECTED 150 */ 151 struct { 152 /** Connection handle of the relevant connection */ 153 uint16_t conn_handle; 154 155 /** Information about the L2CAP connection prior to termination. */ 156 struct ble_l2cap_chan *chan; 157 } disconnect; 158 159 /** 160 * Represents connection accept. Valid for the following event 161 * types: 162 * o BLE_L2CAP_EVENT_COC_ACCEPT 163 */ 164 struct { 165 /** Connection handle of the relevant connection */ 166 uint16_t conn_handle; 167 168 /** MTU supported by peer device on the channel */ 169 uint16_t peer_sdu_size; 170 171 /** The L2CAP channel of the relevant L2CAP connection. */ 172 struct ble_l2cap_chan *chan; 173 } accept; 174 175 /** 176 * Represents received data. Valid for the following event 177 * types: 178 * o BLE_L2CAP_EVENT_COC_DATA_RECEIVED 179 */ 180 struct { 181 /** Connection handle of the relevant connection */ 182 uint16_t conn_handle; 183 184 /** The L2CAP channel of the relevant L2CAP connection. */ 185 struct ble_l2cap_chan *chan; 186 187 /** The mbuf with received SDU. */ 188 struct os_mbuf *sdu_rx; 189 } receive; 190 191 /** 192 * Represents tx_unstalled data. Valid for the following event 193 * types: 194 * o BLE_L2CAP_EVENT_COC_TX_UNSTALLED 195 */ 196 struct { 197 /** Connection handle of the relevant connection */ 198 uint16_t conn_handle; 199 200 /** The L2CAP channel of the relevant L2CAP connection. */ 201 struct ble_l2cap_chan *chan; 202 203 /** 204 * The status of the send attempt which was stalled due to 205 * lack of credits; This can be non zero only if there 206 * is an issue with memory allocation for following SDU fragments. 207 * In such a case last SDU has been partially sent to peer device 208 * and it is up to application to decide how to handle it. 209 */ 210 int status; 211 } tx_unstalled; 212 213 /** 214 * Represents reconfiguration done. Valid for the following event 215 * types: 216 * o BLE_L2CAP_EVENT_COC_RECONFIG_COMPLETED 217 * o BLE_L2CAP_EVENT_COC_PEER_RECONFIGURED 218 */ 219 struct { 220 /** 221 * The status of the reconfiguration attempt; 222 * o 0: the reconfiguration was successfully done. 223 * o BLE host error code: the reconfiguration attempt failed for 224 * the specified reason. 225 */ 226 int status; 227 228 /** Connection handle of the relevant connection */ 229 uint16_t conn_handle; 230 231 /** The L2CAP channel of the relevant L2CAP connection. */ 232 struct ble_l2cap_chan *chan; 233 } reconfigured; 234 }; 235 }; 236 237 struct ble_l2cap_chan_info { 238 uint16_t scid; 239 uint16_t dcid; 240 uint16_t our_l2cap_mtu; 241 uint16_t peer_l2cap_mtu; 242 uint16_t psm; 243 uint16_t our_coc_mtu; 244 uint16_t peer_coc_mtu; 245 }; 246 247 typedef int ble_l2cap_event_fn(struct ble_l2cap_event *event, void *arg); 248 249 uint16_t ble_l2cap_get_conn_handle(struct ble_l2cap_chan *chan); 250 int ble_l2cap_create_server(uint16_t psm, uint16_t mtu, 251 ble_l2cap_event_fn *cb, void *cb_arg); 252 253 int ble_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu, 254 struct os_mbuf *sdu_rx, 255 ble_l2cap_event_fn *cb, void *cb_arg); 256 int ble_l2cap_disconnect(struct ble_l2cap_chan *chan); 257 int ble_l2cap_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_tx); 258 int ble_l2cap_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx); 259 int ble_l2cap_get_chan_info(struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info); 260 261 #ifdef __cplusplus 262 } 263 #endif 264 265 #endif 266