BTstack for Nucleo F401RE/FRDM-KL46Z example program

Dependencies:   F401RE-USBHost mbed

The usage is the same as KL46Z-BTstack_example.
使い方はKL46Z-BTstack_exampleと同じです。
/media/uploads/va009039/f401re-btstack.jpg

Committer:
va009039
Date:
Mon Jun 09 09:03:25 2014 +0000
Revision:
0:a05a07cd6fdf
first commit

Who changed what in which revision?

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