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