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