xref: /btstack/src/classic/bnep.h (revision 16ece13520cb8efcf94cb63eab58a3eda87f40a2)
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 #define MAX_BNEP_NETFILTER                              8
56 #define MAX_BNEP_MULTICAST_FILTER                       8
57 #define MAX_BNEP_NETFILTER_OUT                          421
58 #define MAX_BNEP_MULTICAST_FILTER_OUT                   140
59 
60 typedef enum {
61 	BNEP_CHANNEL_STATE_CLOSED = 1,
62     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST,
63     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE,
64 	BNEP_CHANNEL_STATE_CONNECTED,
65 } BNEP_CHANNEL_STATE;
66 
67 typedef enum {
68     BNEP_CHANNEL_STATE_VAR_NONE                            = 0,
69     BNEP_CHANNEL_STATE_VAR_SND_NOT_UNDERSTOOD              = 1 << 0,
70     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST          = 1 << 1,
71     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE         = 1 << 2,
72     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET         = 1 << 3,
73     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_RESPONSE    = 1 << 4,
74     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_SET       = 1 << 5,
75     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_RESPONSE  = 1 << 6,
76 } BNEP_CHANNEL_STATE_VAR;
77 
78 typedef enum {
79     BNEP_CH_EVT_READY_TO_SEND,
80 } BNEP_CHANNEL_EVENT;
81 
82 typedef struct bnep_channel_event {
83     BNEP_CHANNEL_EVENT type;
84 } bnep_channel_event_t;
85 
86 /* network protocol type filter */
87 typedef struct {
88 	uint16_t	        range_start;
89 	uint16_t	        range_end;
90 } bnep_net_filter_t;
91 
92 /* multicast address filter */
93 typedef struct {
94 	uint8_t		        addr_start[ETHER_ADDR_LEN];
95 	uint8_t		        addr_end[ETHER_ADDR_LEN];
96 } bnep_multi_filter_t;
97 
98 
99 // info regarding multiplexer
100 // note: spec mandates single multplexer per device combination
101 typedef struct {
102     // linked list - assert: first field
103     btstack_linked_item_t      item;
104 
105     BNEP_CHANNEL_STATE state;	          // Channel state
106 
107     BNEP_CHANNEL_STATE_VAR state_var;     // State flag variable. Needed for asynchronous packet sending
108 
109     uint16_t           max_frame_size;    // incomming max. frame size
110     void              *connection;        // client connection
111     bd_addr_t          local_addr;        // locale drvice address
112 	bd_addr_t          remote_addr;       // remote device address
113     uint16_t           l2cap_cid;         // l2cap channel id
114     hci_con_handle_t   con_handle;        // hci connection handle
115 
116     uint16_t           uuid_source;       // Source UUID
117     uint16_t           uuid_dest;         // Destination UUID
118 
119     uint8_t            last_control_type; // type of last control package
120     uint16_t           response_code;     // response code of last action (temp. storage for state machine)
121 
122     bnep_net_filter_t  net_filter[MAX_BNEP_NETFILTER];              // network protocol filter, define fixed size for now
123     uint16_t           net_filter_count;
124 
125     bnep_net_filter_t *net_filter_out;                              // outgoint network protocol filter, must be statically allocated in the application
126     uint16_t           net_filter_out_count;
127 
128     bnep_multi_filter_t  multicast_filter[MAX_BNEP_MULTICAST_FILTER]; // multicast address filter, define fixed size for now
129     uint16_t             multicast_filter_count;
130 
131     bnep_multi_filter_t *multicast_filter_out;                        // outgoing multicast address filter, must be statically allocated in the application
132     uint16_t             multicast_filter_out_count;
133 
134 
135     timer_source_t     timer;             // Timeout timer
136     int                timer_active;      // Is a timer running?
137     int                retry_count;       // number of retries for CONTROL SETUP MSG
138     // l2cap packet handler
139     btstack_packet_handler_t packet_handler;
140 } bnep_channel_t;
141 
142 /* Internal BNEP service descriptor */
143 typedef struct {
144     btstack_linked_item_t    item;           // linked list - assert: first field
145     uint16_t         service_uuid;   // Service class: PANU, NAP, GN
146     uint16_t         max_frame_size; // incomming max. frame size
147 
148     // internal connection
149     btstack_packet_handler_t packet_handler;
150 } bnep_service_t;
151 
152 
153 void bnep_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
154 
155 /* API_START */
156 
157 /**
158  * @brief Set up BNEP.
159  */
160 void bnep_init(void);
161 
162 /**
163  * @brief Check if a data packet can be send out.
164  */
165 int bnep_can_send_packet_now(uint16_t bnep_cid);
166 
167 /**
168  * @brief Send a data packet.
169  */
170 int bnep_send(uint16_t bnep_cid, uint8_t *packet, uint16_t len);
171 
172 /**
173  * @brief Set the network protocol filter.
174  */
175 int bnep_set_net_type_filter(uint16_t bnep_cid, bnep_net_filter_t *filter, uint16_t len);
176 
177 /**
178  * @brief Set the multicast address filter.
179  */
180 int bnep_set_multicast_filter(uint16_t bnep_cid, bnep_multi_filter_t *filter, uint16_t len);
181 
182 /**
183  * @brief Set security level required for incoming connections, need to be called before registering services.
184  */
185 void bnep_set_required_security_level(gap_security_level_t security_level);
186 
187 /**
188  * @brief Register packet handler.
189  */
190 void bnep_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
191 
192 /**
193  * @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.
194  */
195 int bnep_connect(bd_addr_t addr, uint16_t l2cap_psm, uint16_t uuid_src, uint16_t uuid_dest);
196 
197 /**
198  * @brief Disconnects BNEP channel with given identifier.
199  */
200 void bnep_disconnect(bd_addr_t addr);
201 
202 /**
203  * @brief Registers BNEP service, set a maximum frame size and assigns a packet handler. On embedded systems, use NULL for connection parameter.
204  */
205 uint8_t bnep_register_service(uint16_t service_uuid, uint16_t max_frame_size);
206 
207 /**
208  * @brief Unregister BNEP service.
209  */
210 void bnep_unregister_service(uint16_t service_uuid);
211 /* API_END */
212 
213 #if defined __cplusplus
214 }
215 #endif
216 
217 #endif // __BNEP_H
218