![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed base bard check program for BlueTooth USB dongle module (3 switches, 6 leds, I2C LCD, A/D)
Fork of BTstack by
BTstack/l2cap.h@3:7b7d1273e2d5, 2016-10-17 (annotated)
- Committer:
- tamaki
- Date:
- Mon Oct 17 00:25:18 2016 +0000
- Revision:
- 3:7b7d1273e2d5
- Parent:
- 0:1ed23ab1345f
mbed base bard check program
; for BlueTooth USB dongle module
; (3 switches, 6 leds, I2C LCD, A/D)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | /* |
va009039 | 0:1ed23ab1345f | 2 | * Copyright (C) 2009-2012 by Matthias Ringwald |
va009039 | 0:1ed23ab1345f | 3 | * |
va009039 | 0:1ed23ab1345f | 4 | * Redistribution and use in source and binary forms, with or without |
va009039 | 0:1ed23ab1345f | 5 | * modification, are permitted provided that the following conditions |
va009039 | 0:1ed23ab1345f | 6 | * are met: |
va009039 | 0:1ed23ab1345f | 7 | * |
va009039 | 0:1ed23ab1345f | 8 | * 1. Redistributions of source code must retain the above copyright |
va009039 | 0:1ed23ab1345f | 9 | * notice, this list of conditions and the following disclaimer. |
va009039 | 0:1ed23ab1345f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
va009039 | 0:1ed23ab1345f | 11 | * notice, this list of conditions and the following disclaimer in the |
va009039 | 0:1ed23ab1345f | 12 | * documentation and/or other materials provided with the distribution. |
va009039 | 0:1ed23ab1345f | 13 | * 3. Neither the name of the copyright holders nor the names of |
va009039 | 0:1ed23ab1345f | 14 | * contributors may be used to endorse or promote products derived |
va009039 | 0:1ed23ab1345f | 15 | * from this software without specific prior written permission. |
va009039 | 0:1ed23ab1345f | 16 | * 4. Any redistribution, use, or modification is done solely for |
va009039 | 0:1ed23ab1345f | 17 | * personal benefit and not for any commercial purpose or for |
va009039 | 0:1ed23ab1345f | 18 | * monetary gain. |
va009039 | 0:1ed23ab1345f | 19 | * |
va009039 | 0:1ed23ab1345f | 20 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
va009039 | 0:1ed23ab1345f | 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
va009039 | 0:1ed23ab1345f | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
va009039 | 0:1ed23ab1345f | 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
va009039 | 0:1ed23ab1345f | 24 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
va009039 | 0:1ed23ab1345f | 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
va009039 | 0:1ed23ab1345f | 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
va009039 | 0:1ed23ab1345f | 27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
va009039 | 0:1ed23ab1345f | 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
va009039 | 0:1ed23ab1345f | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
va009039 | 0:1ed23ab1345f | 30 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
va009039 | 0:1ed23ab1345f | 31 | * SUCH DAMAGE. |
va009039 | 0:1ed23ab1345f | 32 | * |
va009039 | 0:1ed23ab1345f | 33 | * Please inquire about commercial licensing options at btstack@ringwald.ch |
va009039 | 0:1ed23ab1345f | 34 | * |
va009039 | 0:1ed23ab1345f | 35 | */ |
va009039 | 0:1ed23ab1345f | 36 | |
va009039 | 0:1ed23ab1345f | 37 | /* |
va009039 | 0:1ed23ab1345f | 38 | * l2cap.h |
va009039 | 0:1ed23ab1345f | 39 | * |
va009039 | 0:1ed23ab1345f | 40 | * Logical Link Control and Adaption Protocl (L2CAP) |
va009039 | 0:1ed23ab1345f | 41 | * |
va009039 | 0:1ed23ab1345f | 42 | * Created by Matthias Ringwald on 5/16/09. |
va009039 | 0:1ed23ab1345f | 43 | */ |
va009039 | 0:1ed23ab1345f | 44 | |
va009039 | 0:1ed23ab1345f | 45 | #pragma once |
va009039 | 0:1ed23ab1345f | 46 | |
va009039 | 0:1ed23ab1345f | 47 | #include "hci.h" |
va009039 | 0:1ed23ab1345f | 48 | #include "l2cap_signaling.h" |
va009039 | 0:1ed23ab1345f | 49 | #include <btstack/utils.h> |
va009039 | 0:1ed23ab1345f | 50 | #include <btstack/btstack.h> |
va009039 | 0:1ed23ab1345f | 51 | |
va009039 | 0:1ed23ab1345f | 52 | #if defined __cplusplus |
va009039 | 0:1ed23ab1345f | 53 | extern "C" { |
va009039 | 0:1ed23ab1345f | 54 | #endif |
va009039 | 0:1ed23ab1345f | 55 | |
va009039 | 0:1ed23ab1345f | 56 | #define L2CAP_SIG_ID_INVALID 0 |
va009039 | 0:1ed23ab1345f | 57 | |
va009039 | 0:1ed23ab1345f | 58 | #define L2CAP_HEADER_SIZE 4 |
va009039 | 0:1ed23ab1345f | 59 | |
va009039 | 0:1ed23ab1345f | 60 | // size of HCI ACL + L2CAP Header for regular data packets (8) |
va009039 | 0:1ed23ab1345f | 61 | #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE) |
va009039 | 0:1ed23ab1345f | 62 | |
va009039 | 0:1ed23ab1345f | 63 | // minimum signaling MTU |
va009039 | 0:1ed23ab1345f | 64 | #define L2CAP_MINIMAL_MTU 48 |
va009039 | 0:1ed23ab1345f | 65 | #define L2CAP_DEFAULT_MTU 672 |
va009039 | 0:1ed23ab1345f | 66 | |
va009039 | 0:1ed23ab1345f | 67 | // check L2CAP MTU |
va009039 | 0:1ed23ab1345f | 68 | #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE |
va009039 | 0:1ed23ab1345f | 69 | #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes" |
va009039 | 0:1ed23ab1345f | 70 | #endif |
va009039 | 0:1ed23ab1345f | 71 | |
va009039 | 0:1ed23ab1345f | 72 | // L2CAP Fixed Channel IDs |
va009039 | 0:1ed23ab1345f | 73 | #define L2CAP_CID_SIGNALING 0x0001 |
va009039 | 0:1ed23ab1345f | 74 | #define L2CAP_CID_CONNECTIONLESS_CHANNEL 0x0002 |
va009039 | 0:1ed23ab1345f | 75 | #define L2CAP_CID_ATTRIBUTE_PROTOCOL 0x0004 |
va009039 | 0:1ed23ab1345f | 76 | #define L2CAP_CID_SIGNALING_LE 0x0005 |
va009039 | 0:1ed23ab1345f | 77 | #define L2CAP_CID_SECURITY_MANAGER_PROTOCOL 0x0006 |
va009039 | 0:1ed23ab1345f | 78 | |
va009039 | 0:1ed23ab1345f | 79 | void l2cap_init(void); |
va009039 | 0:1ed23ab1345f | 80 | void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); |
va009039 | 0:1ed23ab1345f | 81 | void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu); |
va009039 | 0:1ed23ab1345f | 82 | void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); |
va009039 | 0:1ed23ab1345f | 83 | uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); |
va009039 | 0:1ed23ab1345f | 84 | uint16_t l2cap_max_mtu(void); |
va009039 | 0:1ed23ab1345f | 85 | |
va009039 | 0:1ed23ab1345f | 86 | void l2cap_block_new_credits(uint8_t blocked); |
va009039 | 0:1ed23ab1345f | 87 | int l2cap_can_send_packet_now(uint16_t local_cid); // non-blocking UART write |
va009039 | 0:1ed23ab1345f | 88 | |
va009039 | 0:1ed23ab1345f | 89 | // get outgoing buffer and prepare data |
va009039 | 0:1ed23ab1345f | 90 | uint8_t *l2cap_get_outgoing_buffer(void); |
va009039 | 0:1ed23ab1345f | 91 | |
va009039 | 0:1ed23ab1345f | 92 | int l2cap_send_prepared(uint16_t local_cid, uint16_t len); |
va009039 | 0:1ed23ab1345f | 93 | int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); |
va009039 | 0:1ed23ab1345f | 94 | |
va009039 | 0:1ed23ab1345f | 95 | int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); |
va009039 | 0:1ed23ab1345f | 96 | int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); |
va009039 | 0:1ed23ab1345f | 97 | |
va009039 | 0:1ed23ab1345f | 98 | void l2cap_close_connection(void *connection); |
va009039 | 0:1ed23ab1345f | 99 | |
va009039 | 0:1ed23ab1345f | 100 | void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu); |
va009039 | 0:1ed23ab1345f | 101 | void l2cap_unregister_service_internal(void *connection, uint16_t psm); |
va009039 | 0:1ed23ab1345f | 102 | |
va009039 | 0:1ed23ab1345f | 103 | void l2cap_accept_connection_internal(uint16_t local_cid); |
va009039 | 0:1ed23ab1345f | 104 | void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); |
va009039 | 0:1ed23ab1345f | 105 | |
va009039 | 0:1ed23ab1345f | 106 | // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol |
va009039 | 0:1ed23ab1345f | 107 | void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); |
va009039 | 0:1ed23ab1345f | 108 | |
va009039 | 0:1ed23ab1345f | 109 | |
va009039 | 0:1ed23ab1345f | 110 | // private structs |
va009039 | 0:1ed23ab1345f | 111 | typedef enum { |
va009039 | 0:1ed23ab1345f | 112 | L2CAP_STATE_CLOSED = 1, // no baseband |
va009039 | 0:1ed23ab1345f | 113 | L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, |
va009039 | 0:1ed23ab1345f | 114 | L2CAP_STATE_WAIT_CONNECTION_COMPLETE, |
va009039 | 0:1ed23ab1345f | 115 | L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT, |
va009039 | 0:1ed23ab1345f | 116 | L2CAP_STATE_WAIT_CONNECT_RSP, // from peer |
va009039 | 0:1ed23ab1345f | 117 | L2CAP_STATE_CONFIG, |
va009039 | 0:1ed23ab1345f | 118 | L2CAP_STATE_OPEN, |
va009039 | 0:1ed23ab1345f | 119 | L2CAP_STATE_WAIT_DISCONNECT, // from application |
va009039 | 0:1ed23ab1345f | 120 | L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST, |
va009039 | 0:1ed23ab1345f | 121 | L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE, |
va009039 | 0:1ed23ab1345f | 122 | L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT, |
va009039 | 0:1ed23ab1345f | 123 | L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST, |
va009039 | 0:1ed23ab1345f | 124 | L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE, |
va009039 | 0:1ed23ab1345f | 125 | } L2CAP_STATE; |
va009039 | 0:1ed23ab1345f | 126 | |
va009039 | 0:1ed23ab1345f | 127 | typedef enum { |
va009039 | 0:1ed23ab1345f | 128 | L2CAP_CHANNEL_STATE_VAR_NONE = 0, |
va009039 | 0:1ed23ab1345f | 129 | L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ = 1 << 0, |
va009039 | 0:1ed23ab1345f | 130 | L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP = 1 << 1, |
va009039 | 0:1ed23ab1345f | 131 | L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ = 1 << 2, |
va009039 | 0:1ed23ab1345f | 132 | L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP = 1 << 3, |
va009039 | 0:1ed23ab1345f | 133 | L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ = 1 << 4, |
va009039 | 0:1ed23ab1345f | 134 | L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP = 1 << 5, |
va009039 | 0:1ed23ab1345f | 135 | } L2CAP_CHANNEL_STATE_VAR; |
va009039 | 0:1ed23ab1345f | 136 | |
va009039 | 0:1ed23ab1345f | 137 | // info regarding an actual coneection |
va009039 | 0:1ed23ab1345f | 138 | typedef struct { |
va009039 | 0:1ed23ab1345f | 139 | // linked list - assert: first field |
va009039 | 0:1ed23ab1345f | 140 | linked_item_t item; |
va009039 | 0:1ed23ab1345f | 141 | |
va009039 | 0:1ed23ab1345f | 142 | L2CAP_STATE state; |
va009039 | 0:1ed23ab1345f | 143 | L2CAP_CHANNEL_STATE_VAR state_var; |
va009039 | 0:1ed23ab1345f | 144 | |
va009039 | 0:1ed23ab1345f | 145 | bd_addr_t address; |
va009039 | 0:1ed23ab1345f | 146 | hci_con_handle_t handle; |
va009039 | 0:1ed23ab1345f | 147 | |
va009039 | 0:1ed23ab1345f | 148 | uint8_t remote_sig_id; // used by other side, needed for delayed response |
va009039 | 0:1ed23ab1345f | 149 | uint8_t local_sig_id; // own signaling identifier |
va009039 | 0:1ed23ab1345f | 150 | |
va009039 | 0:1ed23ab1345f | 151 | uint16_t local_cid; |
va009039 | 0:1ed23ab1345f | 152 | uint16_t remote_cid; |
va009039 | 0:1ed23ab1345f | 153 | |
va009039 | 0:1ed23ab1345f | 154 | uint16_t local_mtu; |
va009039 | 0:1ed23ab1345f | 155 | uint16_t remote_mtu; |
va009039 | 0:1ed23ab1345f | 156 | |
va009039 | 0:1ed23ab1345f | 157 | uint16_t psm; |
va009039 | 0:1ed23ab1345f | 158 | |
va009039 | 0:1ed23ab1345f | 159 | uint8_t packets_granted; // number of L2CAP/ACL packets client is allowed to send |
va009039 | 0:1ed23ab1345f | 160 | |
va009039 | 0:1ed23ab1345f | 161 | uint8_t reason; // used in decline internal |
va009039 | 0:1ed23ab1345f | 162 | |
va009039 | 0:1ed23ab1345f | 163 | // client connection |
va009039 | 0:1ed23ab1345f | 164 | void * connection; |
va009039 | 0:1ed23ab1345f | 165 | |
va009039 | 0:1ed23ab1345f | 166 | // internal connection |
va009039 | 0:1ed23ab1345f | 167 | btstack_packet_handler_t packet_handler; |
va009039 | 0:1ed23ab1345f | 168 | |
va009039 | 0:1ed23ab1345f | 169 | } l2cap_channel_t; |
va009039 | 0:1ed23ab1345f | 170 | |
va009039 | 0:1ed23ab1345f | 171 | // info regarding potential connections |
va009039 | 0:1ed23ab1345f | 172 | typedef struct { |
va009039 | 0:1ed23ab1345f | 173 | // linked list - assert: first field |
va009039 | 0:1ed23ab1345f | 174 | linked_item_t item; |
va009039 | 0:1ed23ab1345f | 175 | |
va009039 | 0:1ed23ab1345f | 176 | // service id |
va009039 | 0:1ed23ab1345f | 177 | uint16_t psm; |
va009039 | 0:1ed23ab1345f | 178 | |
va009039 | 0:1ed23ab1345f | 179 | // incoming MTU |
va009039 | 0:1ed23ab1345f | 180 | uint16_t mtu; |
va009039 | 0:1ed23ab1345f | 181 | |
va009039 | 0:1ed23ab1345f | 182 | // client connection |
va009039 | 0:1ed23ab1345f | 183 | void *connection; |
va009039 | 0:1ed23ab1345f | 184 | |
va009039 | 0:1ed23ab1345f | 185 | // internal connection |
va009039 | 0:1ed23ab1345f | 186 | btstack_packet_handler_t packet_handler; |
va009039 | 0:1ed23ab1345f | 187 | |
va009039 | 0:1ed23ab1345f | 188 | } l2cap_service_t; |
va009039 | 0:1ed23ab1345f | 189 | |
va009039 | 0:1ed23ab1345f | 190 | |
va009039 | 0:1ed23ab1345f | 191 | typedef struct l2cap_signaling_response { |
va009039 | 0:1ed23ab1345f | 192 | hci_con_handle_t handle; |
va009039 | 0:1ed23ab1345f | 193 | uint8_t sig_id; |
va009039 | 0:1ed23ab1345f | 194 | uint8_t code; |
va009039 | 0:1ed23ab1345f | 195 | uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request |
va009039 | 0:1ed23ab1345f | 196 | } l2cap_signaling_response_t; |
va009039 | 0:1ed23ab1345f | 197 | |
va009039 | 0:1ed23ab1345f | 198 | |
va009039 | 0:1ed23ab1345f | 199 | #if defined __cplusplus |
va009039 | 0:1ed23ab1345f | 200 | } |
va009039 | 0:1ed23ab1345f | 201 | #endif |