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