1 | /*
|
---|
2 | * Copyright (C) 2014 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | *
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. Neither the name of the copyright holders nor the names of
|
---|
14 | * contributors may be used to endorse or promote products derived
|
---|
15 | * from this software without specific prior written permission.
|
---|
16 | * 4. Any redistribution, use, or modification is done solely for
|
---|
17 | * personal benefit and not for any commercial purpose or for
|
---|
18 | * monetary gain.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
24 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
---|
27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
---|
28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
---|
30 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * Please inquire about commercial licensing options at btstack@ringwald.ch
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * BNEP.h
|
---|
39 | */
|
---|
40 |
|
---|
41 | #ifndef __BNEP_H
|
---|
42 | #define __BNEP_H
|
---|
43 |
|
---|
44 | #include <btstack/btstack.h>
|
---|
45 | #include <btstack/utils.h>
|
---|
46 |
|
---|
47 | #include <stdint.h>
|
---|
48 |
|
---|
49 | #if defined __cplusplus
|
---|
50 | extern "C" {
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #ifndef ETHER_ADDR_LEN
|
---|
54 | #define ETHER_ADDR_LEN sizeof(bd_addr_t)
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #ifndef ETHERTYPE_VLAN
|
---|
58 | #define ETHERTYPE_VLAN 0x8100 /* IEEE 802.1Q VLAN tag */
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #define BNEP_MTU_MIN 1691
|
---|
62 |
|
---|
63 | #define MAX_BNEP_NETFILTER 8
|
---|
64 | #define MAX_BNEP_MULTICAST_FILTER 8
|
---|
65 | #define MAX_BNEP_NETFILTER_OUT 421
|
---|
66 | #define MAX_BNEP_MULTICAST_FULTER_OUT 140
|
---|
67 |
|
---|
68 | #define BNEP_EXT_FLAG 0x80
|
---|
69 | #define BNEP_TYPE_MASK 0x7F
|
---|
70 | #define BNEP_TYPE(header) ((header) & BNEP_TYPE_MASK)
|
---|
71 | #define BNEP_HEADER_HAS_EXT(x) (((x) & BNEP_EXT_FLAG) == BNEP_EXT_FLAG)
|
---|
72 |
|
---|
73 | /* BNEP UUIDs */
|
---|
74 | #define BLUETOOTH_SERVICE_CLASS_PANU 0x1115
|
---|
75 | #define BLUETOOTH_SERVICE_CLASS_NAP 0x1116
|
---|
76 | #define BLUETOOTH_SERVICE_CLASS_GN 0x1117
|
---|
77 |
|
---|
78 | /* BNEP packet types */
|
---|
79 | #define BNEP_PKT_TYPE_GENERAL_ETHERNET 0x00
|
---|
80 | #define BNEP_PKT_TYPE_CONTROL 0x01
|
---|
81 | #define BNEP_PKT_TYPE_COMPRESSED_ETHERNET 0x02
|
---|
82 | #define BNEP_PKT_TYPE_COMPRESSED_ETHERNET_SOURCE_ONLY 0x03
|
---|
83 | #define BNEP_PKT_TYPE_COMPRESSED_ETHERNET_DEST_ONLY 0x04
|
---|
84 |
|
---|
85 | /* BNEP control types */
|
---|
86 | #define BNEP_CONTROL_TYPE_COMMAND_NOT_UNDERSTOOD 0x00
|
---|
87 | #define BNEP_CONTROL_TYPE_SETUP_CONNECTION_REQUEST 0x01
|
---|
88 | #define BNEP_CONTROL_TYPE_SETUP_CONNECTION_RESPONSE 0x02
|
---|
89 | #define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET 0x03
|
---|
90 | #define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE 0x04
|
---|
91 | #define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET 0x05
|
---|
92 | #define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE 0x06
|
---|
93 |
|
---|
94 | /* BNEP extension header types */
|
---|
95 | #define BNEP_EXT_HEADER_TYPE_EXTENSION_CONTROL 0x00
|
---|
96 |
|
---|
97 | /* BNEP setup response codes */
|
---|
98 | #define BNEP_RESP_SETUP_SUCCESS 0x0000
|
---|
99 | #define BNEP_RESP_SETUP_INVALID_DEST_UUID 0x0001
|
---|
100 | #define BNEP_RESP_SETUP_INVALID_SOURCE_UUID 0x0002
|
---|
101 | #define BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE 0x0003
|
---|
102 | #define BNEP_RESP_SETUP_CONNECTION_NOT_ALLOWED 0x0004
|
---|
103 |
|
---|
104 | /* BNEP filter response codes */
|
---|
105 | #define BNEP_RESP_FILTER_SUCCESS 0x0000
|
---|
106 | #define BNEP_RESP_FILTER_UNSUPPORTED_REQUEST 0x0001
|
---|
107 | #define BNEP_RESP_FILTER_ERR_INVALID_RANGE 0x0002
|
---|
108 | #define BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS 0x0003
|
---|
109 | #define BNEP_RESP_FILTER_ERR_SECURITY 0x0004
|
---|
110 |
|
---|
111 | typedef enum {
|
---|
112 | BNEP_CHANNEL_STATE_CLOSED = 1,
|
---|
113 | BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST,
|
---|
114 | BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE,
|
---|
115 | BNEP_CHANNEL_STATE_CONNECTED,
|
---|
116 | } BNEP_CHANNEL_STATE;
|
---|
117 |
|
---|
118 | typedef enum {
|
---|
119 | BNEP_CHANNEL_STATE_VAR_NONE = 0,
|
---|
120 | BNEP_CHANNEL_STATE_VAR_SND_NOT_UNDERSTOOD = 1 << 0,
|
---|
121 | BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST = 1 << 1,
|
---|
122 | BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE = 1 << 2,
|
---|
123 | BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET = 1 << 3,
|
---|
124 | BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_RESPONSE = 1 << 4,
|
---|
125 | BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_SET = 1 << 5,
|
---|
126 | BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_RESPONSE = 1 << 6,
|
---|
127 | } BNEP_CHANNEL_STATE_VAR;
|
---|
128 |
|
---|
129 | typedef enum {
|
---|
130 | BNEP_CH_EVT_READY_TO_SEND,
|
---|
131 | } BNEP_CHANNEL_EVENT;
|
---|
132 |
|
---|
133 | typedef struct bnep_channel_event {
|
---|
134 | BNEP_CHANNEL_EVENT type;
|
---|
135 | } bnep_channel_event_t;
|
---|
136 |
|
---|
137 | /* network protocol type filter */
|
---|
138 | typedef struct {
|
---|
139 | uint16_t range_start;
|
---|
140 | uint16_t range_end;
|
---|
141 | } bnep_net_filter_t;
|
---|
142 |
|
---|
143 | /* multicast address filter */
|
---|
144 | typedef struct {
|
---|
145 | uint8_t addr_start[ETHER_ADDR_LEN];
|
---|
146 | uint8_t addr_end[ETHER_ADDR_LEN];
|
---|
147 | } bnep_multi_filter_t;
|
---|
148 |
|
---|
149 |
|
---|
150 | // info regarding multiplexer
|
---|
151 | // note: spec mandates single multplexer per device combination
|
---|
152 | typedef struct {
|
---|
153 | // linked list - assert: first field
|
---|
154 | linked_item_t item;
|
---|
155 |
|
---|
156 | BNEP_CHANNEL_STATE state; // Channel state
|
---|
157 |
|
---|
158 | BNEP_CHANNEL_STATE_VAR state_var; // State flag variable. Needed for asynchronous packet sending
|
---|
159 |
|
---|
160 | uint16_t max_frame_size; // incomming max. frame size
|
---|
161 | void *connection; // client connection
|
---|
162 | bd_addr_t local_addr; // locale drvice address
|
---|
163 | bd_addr_t remote_addr; // remote device address
|
---|
164 | uint16_t l2cap_cid; // l2cap channel id
|
---|
165 | hci_con_handle_t con_handle; // hci connection handle
|
---|
166 |
|
---|
167 | uint16_t uuid_source; // Source UUID
|
---|
168 | uint16_t uuid_dest; // Destination UUID
|
---|
169 |
|
---|
170 | uint8_t last_control_type; // type of last control package
|
---|
171 | uint16_t response_code; // response code of last action (temp. storage for state machine)
|
---|
172 |
|
---|
173 | bnep_net_filter_t net_filter[MAX_BNEP_NETFILTER]; // network protocol filter, define fixed size for now
|
---|
174 | uint16_t net_filter_count;
|
---|
175 |
|
---|
176 | bnep_net_filter_t *net_filter_out; // outgoint network protocol filter, must be statically allocated in the application
|
---|
177 | uint16_t net_filter_out_count;
|
---|
178 |
|
---|
179 | bnep_multi_filter_t multicast_filter[MAX_BNEP_MULTICAST_FILTER]; // multicast address filter, define fixed size for now
|
---|
180 | uint16_t multicast_filter_count;
|
---|
181 |
|
---|
182 | bnep_multi_filter_t *multicast_filter_out; // outgoing multicast address filter, must be statically allocated in the application
|
---|
183 | uint16_t multicast_filter_out_count;
|
---|
184 |
|
---|
185 |
|
---|
186 | timer_source_t timer; // Timeout timer
|
---|
187 | int timer_active; // Is a timer running?
|
---|
188 | int retry_count; // number of retries for CONTROL SETUP MSG
|
---|
189 | // l2cap packet handler
|
---|
190 | btstack_packet_handler_t packet_handler;
|
---|
191 | } bnep_channel_t;
|
---|
192 |
|
---|
193 | /* Internal BNEP service descriptor */
|
---|
194 | typedef struct {
|
---|
195 | linked_item_t item; // linked list - assert: first field
|
---|
196 | void *connection; // client connection
|
---|
197 | uint16_t service_uuid; // Service class: PANU, NAP, GN
|
---|
198 | uint16_t max_frame_size; // incomming max. frame size
|
---|
199 |
|
---|
200 | // internal connection
|
---|
201 | btstack_packet_handler_t packet_handler;
|
---|
202 | } bnep_service_t;
|
---|
203 |
|
---|
204 |
|
---|
205 | void bnep_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
---|
206 |
|
---|
207 | /** Embedded API **/
|
---|
208 |
|
---|
209 | /* Set up BNEP. */
|
---|
210 | void bnep_init(void);
|
---|
211 |
|
---|
212 | /* Check if a data packet can be send out */
|
---|
213 | int bnep_can_send_packet_now(uint16_t bnep_cid);
|
---|
214 |
|
---|
215 | /* Send a data packet */
|
---|
216 | int bnep_send(uint16_t bnep_cid, uint8_t *packet, uint16_t len);
|
---|
217 |
|
---|
218 | /* Set the network protocol filter */
|
---|
219 | int bnep_set_net_type_filter(uint16_t bnep_cid, bnep_net_filter_t *filter, uint16_t len);
|
---|
220 |
|
---|
221 | /* Set the multicast address filter */
|
---|
222 | int bnep_set_multicast_filter(uint16_t bnep_cid, bnep_multi_filter_t *filter, uint16_t len);
|
---|
223 |
|
---|
224 | /* Set security level required for incoming connections, need to be called before registering services */
|
---|
225 | void bnep_set_required_security_level(gap_security_level_t security_level);
|
---|
226 |
|
---|
227 | /* Register packet handler. */
|
---|
228 | void bnep_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type,
|
---|
229 | uint16_t channel, uint8_t *packet, uint16_t size));
|
---|
230 |
|
---|
231 | // Creates BNEP connection (channel) to a given server on a remote device with baseband address. A new baseband connection will be initiated if necessary.
|
---|
232 | int bnep_connect(void * connection, bd_addr_t *addr, uint16_t l2cap_psm, uint16_t uuid_dest);
|
---|
233 |
|
---|
234 | // Disconencts BNEP channel with given identifier.
|
---|
235 | void bnep_disconnect(bd_addr_t *addr);
|
---|
236 |
|
---|
237 | // Registers BNEP service, set a maximum frame size and assigns a packet handler. On embedded systems, use NULL for connection parameter.
|
---|
238 | int bnep_register_service(void * connection, uint16_t service_uuid, uint16_t max_frame_size);
|
---|
239 |
|
---|
240 | // Unregister BNEP service.
|
---|
241 | void bnep_unregister_service(uint16_t service_uuid);
|
---|
242 |
|
---|
243 | #if defined __cplusplus
|
---|
244 | }
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | #endif // __BNEP_H
|
---|