xref: /btstack/src/classic/bnep.h (revision 045ba8fec0b10b936623b66fd334dbbee083a9fb)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
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 BLUEKITCHEN GMBH 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 MATTHIAS
24  * RINGWALD 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
34  * [email protected]
35  *
36  */
37 
38 /*
39  * bnep.h
40  * Author: Ole Reinhardt <[email protected]>
41  *
42  */
43 
44 #ifndef __BNEP_H
45 #define __BNEP_H
46 
47 #include "utils.h"
48 
49 #include <stdint.h>
50 
51 #if defined __cplusplus
52 extern "C" {
53 #endif
54 
55 #ifndef ETHER_ADDR_LEN
56 #define ETHER_ADDR_LEN sizeof(bd_addr_t)
57 #endif
58 
59 #ifndef ETHERTYPE_VLAN
60 #define	ETHERTYPE_VLAN		                            0x8100 /* IEEE 802.1Q VLAN tag */
61 #endif
62 
63 #define	BNEP_MTU_MIN		                            1691
64 
65 #define MAX_BNEP_NETFILTER                              8
66 #define MAX_BNEP_MULTICAST_FILTER                       8
67 #define MAX_BNEP_NETFILTER_OUT                          421
68 #define MAX_BNEP_MULTICAST_FULTER_OUT                   140
69 
70 #define BNEP_EXT_FLAG                                   0x80
71 #define BNEP_TYPE_MASK                                  0x7F
72 #define	BNEP_TYPE(header)                               ((header) & BNEP_TYPE_MASK)
73 #define BNEP_HEADER_HAS_EXT(x)	                        (((x) & BNEP_EXT_FLAG) == BNEP_EXT_FLAG)
74 
75 /* BNEP packet types */
76 #define	BNEP_PKT_TYPE_GENERAL_ETHERNET                  0x00
77 #define	BNEP_PKT_TYPE_CONTROL                           0x01
78 #define	BNEP_PKT_TYPE_COMPRESSED_ETHERNET               0x02
79 #define	BNEP_PKT_TYPE_COMPRESSED_ETHERNET_SOURCE_ONLY   0x03
80 #define	BNEP_PKT_TYPE_COMPRESSED_ETHERNET_DEST_ONLY     0x04
81 
82 /* BNEP control types */
83 #define	BNEP_CONTROL_TYPE_COMMAND_NOT_UNDERSTOOD        0x00
84 #define	BNEP_CONTROL_TYPE_SETUP_CONNECTION_REQUEST      0x01
85 #define	BNEP_CONTROL_TYPE_SETUP_CONNECTION_RESPONSE     0x02
86 #define	BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET           0x03
87 #define	BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE      0x04
88 #define	BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET         0x05
89 #define	BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE    0x06
90 
91 /* BNEP extension header types */
92 #define	BNEP_EXT_HEADER_TYPE_EXTENSION_CONTROL          0x00
93 
94 /* BNEP setup response codes */
95 #define	BNEP_RESP_SETUP_SUCCESS                         0x0000
96 #define	BNEP_RESP_SETUP_INVALID_DEST_UUID               0x0001
97 #define	BNEP_RESP_SETUP_INVALID_SOURCE_UUID             0x0002
98 #define	BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE       0x0003
99 #define	BNEP_RESP_SETUP_CONNECTION_NOT_ALLOWED          0x0004
100 
101 /* BNEP filter response codes */
102 #define	BNEP_RESP_FILTER_SUCCESS                        0x0000
103 #define	BNEP_RESP_FILTER_UNSUPPORTED_REQUEST            0x0001
104 #define	BNEP_RESP_FILTER_ERR_INVALID_RANGE              0x0002
105 #define	BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS           0x0003
106 #define	BNEP_RESP_FILTER_ERR_SECURITY                   0x0004
107 
108 typedef enum {
109 	BNEP_CHANNEL_STATE_CLOSED = 1,
110     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST,
111     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE,
112 	BNEP_CHANNEL_STATE_CONNECTED,
113 } BNEP_CHANNEL_STATE;
114 
115 typedef enum {
116     BNEP_CHANNEL_STATE_VAR_NONE                            = 0,
117     BNEP_CHANNEL_STATE_VAR_SND_NOT_UNDERSTOOD              = 1 << 0,
118     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST          = 1 << 1,
119     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE         = 1 << 2,
120     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET         = 1 << 3,
121     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_RESPONSE    = 1 << 4,
122     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_SET       = 1 << 5,
123     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_RESPONSE  = 1 << 6,
124 } BNEP_CHANNEL_STATE_VAR;
125 
126 typedef enum {
127     BNEP_CH_EVT_READY_TO_SEND,
128 } BNEP_CHANNEL_EVENT;
129 
130 typedef struct bnep_channel_event {
131     BNEP_CHANNEL_EVENT type;
132 } bnep_channel_event_t;
133 
134 /* network protocol type filter */
135 typedef struct {
136 	uint16_t	        range_start;
137 	uint16_t	        range_end;
138 } bnep_net_filter_t;
139 
140 /* multicast address filter */
141 typedef struct {
142 	uint8_t		        addr_start[ETHER_ADDR_LEN];
143 	uint8_t		        addr_end[ETHER_ADDR_LEN];
144 } bnep_multi_filter_t;
145 
146 
147 // info regarding multiplexer
148 // note: spec mandates single multplexer per device combination
149 typedef struct {
150     // linked list - assert: first field
151     linked_item_t      item;
152 
153     BNEP_CHANNEL_STATE state;	          // Channel state
154 
155     BNEP_CHANNEL_STATE_VAR state_var;     // State flag variable. Needed for asynchronous packet sending
156 
157     uint16_t           max_frame_size;    // incomming max. frame size
158     void              *connection;        // client connection
159     bd_addr_t          local_addr;        // locale drvice address
160 	bd_addr_t          remote_addr;       // remote device address
161     uint16_t           l2cap_cid;         // l2cap channel id
162     hci_con_handle_t   con_handle;        // hci connection handle
163 
164     uint16_t           uuid_source;       // Source UUID
165     uint16_t           uuid_dest;         // Destination UUID
166 
167     uint8_t            last_control_type; // type of last control package
168     uint16_t           response_code;     // response code of last action (temp. storage for state machine)
169 
170     bnep_net_filter_t  net_filter[MAX_BNEP_NETFILTER];              // network protocol filter, define fixed size for now
171     uint16_t           net_filter_count;
172 
173     bnep_net_filter_t *net_filter_out;                              // outgoint network protocol filter, must be statically allocated in the application
174     uint16_t           net_filter_out_count;
175 
176     bnep_multi_filter_t  multicast_filter[MAX_BNEP_MULTICAST_FILTER]; // multicast address filter, define fixed size for now
177     uint16_t             multicast_filter_count;
178 
179     bnep_multi_filter_t *multicast_filter_out;                        // outgoing multicast address filter, must be statically allocated in the application
180     uint16_t             multicast_filter_out_count;
181 
182 
183     timer_source_t     timer;             // Timeout timer
184     int                timer_active;      // Is a timer running?
185     int                retry_count;       // number of retries for CONTROL SETUP MSG
186     // l2cap packet handler
187     btstack_packet_handler_t packet_handler;
188 } bnep_channel_t;
189 
190 /* Internal BNEP service descriptor */
191 typedef struct {
192     linked_item_t    item;           // linked list - assert: first field
193     uint16_t         service_uuid;   // Service class: PANU, NAP, GN
194     uint16_t         max_frame_size; // incomming max. frame size
195 
196     // internal connection
197     btstack_packet_handler_t packet_handler;
198 } bnep_service_t;
199 
200 
201 void bnep_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
202 
203 /* API_START */
204 
205 /**
206  * @brief Set up BNEP.
207  */
208 void bnep_init(void);
209 
210 /**
211  * @brief Check if a data packet can be send out.
212  */
213 int bnep_can_send_packet_now(uint16_t bnep_cid);
214 
215 /**
216  * @brief Send a data packet.
217  */
218 int bnep_send(uint16_t bnep_cid, uint8_t *packet, uint16_t len);
219 
220 /**
221  * @brief Set the network protocol filter.
222  */
223 int bnep_set_net_type_filter(uint16_t bnep_cid, bnep_net_filter_t *filter, uint16_t len);
224 
225 /**
226  * @brief Set the multicast address filter.
227  */
228 int bnep_set_multicast_filter(uint16_t bnep_cid, bnep_multi_filter_t *filter, uint16_t len);
229 
230 /**
231  * @brief Set security level required for incoming connections, need to be called before registering services.
232  */
233 void bnep_set_required_security_level(gap_security_level_t security_level);
234 
235 /**
236  * @brief Register packet handler.
237  */
238 void bnep_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
239 
240 /**
241  * @brief Creates BNEP connection (channel) to a given server on a remote device with baseband address. A new baseband connection will be initiated if necessary.
242  */
243 int bnep_connect(bd_addr_t addr, uint16_t l2cap_psm, uint16_t uuid_src, uint16_t uuid_dest);
244 
245 /**
246  * @brief Disconnects BNEP channel with given identifier.
247  */
248 void bnep_disconnect(bd_addr_t addr);
249 
250 /**
251  * @brief Registers BNEP service, set a maximum frame size and assigns a packet handler. On embedded systems, use NULL for connection parameter.
252  */
253 uint8_t bnep_register_service(uint16_t service_uuid, uint16_t max_frame_size);
254 
255 /**
256  * @brief Unregister BNEP service.
257  */
258 void bnep_unregister_service(uint16_t service_uuid);
259 /* API_END */
260 
261 #if defined __cplusplus
262 }
263 #endif
264 
265 #endif // __BNEP_H
266