xref: /btstack/src/classic/bnep.h (revision fe3c1d9398e4313afad30ccb7708859e93c2c1f4)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title BNEP
403deb3ec6SMatthias Ringwald  *
413deb3ec6SMatthias Ringwald  */
423deb3ec6SMatthias Ringwald 
43fe5a6c4eSMilanka Ringwald /**
44fe5a6c4eSMilanka Ringwald  * @author: Ole Reinhardt <[email protected]>
45fe5a6c4eSMilanka Ringwald  */
46fe5a6c4eSMilanka Ringwald 
4780e33422SMatthias Ringwald #ifndef BNEP_H
4880e33422SMatthias Ringwald #define BNEP_H
493deb3ec6SMatthias Ringwald 
50eb886013SMatthias Ringwald #include "btstack_util.h"
510e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
520e2df43fSMatthias Ringwald #include "gap.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald #include <stdint.h>
553deb3ec6SMatthias Ringwald 
563deb3ec6SMatthias Ringwald #if defined __cplusplus
573deb3ec6SMatthias Ringwald extern "C" {
583deb3ec6SMatthias Ringwald #endif
593deb3ec6SMatthias Ringwald 
603deb3ec6SMatthias Ringwald #define MAX_BNEP_NETFILTER                              8
613deb3ec6SMatthias Ringwald #define MAX_BNEP_MULTICAST_FILTER                       8
623deb3ec6SMatthias Ringwald #define MAX_BNEP_NETFILTER_OUT                          421
6366b1fcb3SMatthias Ringwald #define MAX_BNEP_MULTICAST_FILTER_OUT                   140
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald typedef enum {
663deb3ec6SMatthias Ringwald 	BNEP_CHANNEL_STATE_CLOSED = 1,
673deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST,
683deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE,
693deb3ec6SMatthias Ringwald 	BNEP_CHANNEL_STATE_CONNECTED,
703deb3ec6SMatthias Ringwald } BNEP_CHANNEL_STATE;
713deb3ec6SMatthias Ringwald 
723deb3ec6SMatthias Ringwald typedef enum {
733deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_NONE                            = 0,
743deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_NOT_UNDERSTOOD              = 1 << 0,
753deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST          = 1 << 1,
763deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE         = 1 << 2,
773deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET         = 1 << 3,
783deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_RESPONSE    = 1 << 4,
793deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_SET       = 1 << 5,
803deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR_SND_FILTER_MULTI_ADDR_RESPONSE  = 1 << 6,
813deb3ec6SMatthias Ringwald } BNEP_CHANNEL_STATE_VAR;
823deb3ec6SMatthias Ringwald 
833deb3ec6SMatthias Ringwald typedef enum {
843deb3ec6SMatthias Ringwald     BNEP_CH_EVT_READY_TO_SEND,
853deb3ec6SMatthias Ringwald } BNEP_CHANNEL_EVENT;
863deb3ec6SMatthias Ringwald 
873deb3ec6SMatthias Ringwald typedef struct bnep_channel_event {
883deb3ec6SMatthias Ringwald     BNEP_CHANNEL_EVENT type;
893deb3ec6SMatthias Ringwald } bnep_channel_event_t;
903deb3ec6SMatthias Ringwald 
913deb3ec6SMatthias Ringwald /* network protocol type filter */
923deb3ec6SMatthias Ringwald typedef struct {
933deb3ec6SMatthias Ringwald 	uint16_t	        range_start;
943deb3ec6SMatthias Ringwald 	uint16_t	        range_end;
953deb3ec6SMatthias Ringwald } bnep_net_filter_t;
963deb3ec6SMatthias Ringwald 
973deb3ec6SMatthias Ringwald /* multicast address filter */
983deb3ec6SMatthias Ringwald typedef struct {
993deb3ec6SMatthias Ringwald 	uint8_t		        addr_start[ETHER_ADDR_LEN];
1003deb3ec6SMatthias Ringwald 	uint8_t		        addr_end[ETHER_ADDR_LEN];
1013deb3ec6SMatthias Ringwald } bnep_multi_filter_t;
1023deb3ec6SMatthias Ringwald 
1033deb3ec6SMatthias Ringwald 
1043deb3ec6SMatthias Ringwald // info regarding multiplexer
1053deb3ec6SMatthias Ringwald // note: spec mandates single multplexer per device combination
1063deb3ec6SMatthias Ringwald typedef struct {
1073deb3ec6SMatthias Ringwald     // linked list - assert: first field
108665d90f2SMatthias Ringwald     btstack_linked_item_t      item;
1093deb3ec6SMatthias Ringwald 
1103deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE state;	          // Channel state
1113deb3ec6SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald     BNEP_CHANNEL_STATE_VAR state_var;     // State flag variable. Needed for asynchronous packet sending
1133deb3ec6SMatthias Ringwald 
1143deb3ec6SMatthias Ringwald     uint16_t           max_frame_size;    // incomming max. frame size
1153deb3ec6SMatthias Ringwald     void              *connection;        // client connection
1163deb3ec6SMatthias Ringwald     bd_addr_t          local_addr;        // locale drvice address
1173deb3ec6SMatthias Ringwald 	bd_addr_t          remote_addr;       // remote device address
1183deb3ec6SMatthias Ringwald     uint16_t           l2cap_cid;         // l2cap channel id
1193deb3ec6SMatthias Ringwald     hci_con_handle_t   con_handle;        // hci connection handle
1203deb3ec6SMatthias Ringwald 
1213deb3ec6SMatthias Ringwald     uint16_t           uuid_source;       // Source UUID
1223deb3ec6SMatthias Ringwald     uint16_t           uuid_dest;         // Destination UUID
1233deb3ec6SMatthias Ringwald 
1243deb3ec6SMatthias Ringwald     uint8_t            last_control_type; // type of last control package
1253deb3ec6SMatthias Ringwald     uint16_t           response_code;     // response code of last action (temp. storage for state machine)
1263deb3ec6SMatthias Ringwald 
1273deb3ec6SMatthias Ringwald     bnep_net_filter_t  net_filter[MAX_BNEP_NETFILTER];              // network protocol filter, define fixed size for now
1283deb3ec6SMatthias Ringwald     uint16_t           net_filter_count;
1293deb3ec6SMatthias Ringwald 
1303deb3ec6SMatthias Ringwald     bnep_net_filter_t *net_filter_out;                              // outgoint network protocol filter, must be statically allocated in the application
1313deb3ec6SMatthias Ringwald     uint16_t           net_filter_out_count;
1323deb3ec6SMatthias Ringwald 
1333deb3ec6SMatthias Ringwald     bnep_multi_filter_t  multicast_filter[MAX_BNEP_MULTICAST_FILTER]; // multicast address filter, define fixed size for now
1343deb3ec6SMatthias Ringwald     uint16_t             multicast_filter_count;
1353deb3ec6SMatthias Ringwald 
1363deb3ec6SMatthias Ringwald     bnep_multi_filter_t *multicast_filter_out;                        // outgoing multicast address filter, must be statically allocated in the application
1373deb3ec6SMatthias Ringwald     uint16_t             multicast_filter_out_count;
1383deb3ec6SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald 
140ec820d77SMatthias Ringwald     btstack_timer_source_t     timer;             // Timeout timer
1413deb3ec6SMatthias Ringwald     int                timer_active;      // Is a timer running?
1423deb3ec6SMatthias Ringwald     int                retry_count;       // number of retries for CONTROL SETUP MSG
1433deb3ec6SMatthias Ringwald     // l2cap packet handler
1443deb3ec6SMatthias Ringwald     btstack_packet_handler_t packet_handler;
1451ed1a3bdSMatthias Ringwald 
1461ed1a3bdSMatthias Ringwald     uint8_t   waiting_for_can_send_now;
1471ed1a3bdSMatthias Ringwald 
1483deb3ec6SMatthias Ringwald } bnep_channel_t;
1493deb3ec6SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald /* Internal BNEP service descriptor */
1513deb3ec6SMatthias Ringwald typedef struct {
152665d90f2SMatthias Ringwald     btstack_linked_item_t    item;           // linked list - assert: first field
1533deb3ec6SMatthias Ringwald     uint16_t         service_uuid;   // Service class: PANU, NAP, GN
1543deb3ec6SMatthias Ringwald     uint16_t         max_frame_size; // incomming max. frame size
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald     // internal connection
1573deb3ec6SMatthias Ringwald     btstack_packet_handler_t packet_handler;
1583deb3ec6SMatthias Ringwald } bnep_service_t;
1593deb3ec6SMatthias Ringwald 
1603deb3ec6SMatthias Ringwald 
1613deb3ec6SMatthias Ringwald void bnep_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1623deb3ec6SMatthias Ringwald 
163*fe3c1d93SMatthias Ringwald 
164*fe3c1d93SMatthias Ringwald /* BNEP Setup Connection Response codes */
165*fe3c1d93SMatthias Ringwald #define BNEP_SETUP_CONNECTION_RESPONSE_SUCCESS                      0x0000
166*fe3c1d93SMatthias Ringwald #define BNEP_SETUP_CONNECTION_RESPONSE_INVALID_DEST_UUID            0x0001
167*fe3c1d93SMatthias Ringwald #define BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SOURCE_UUID          0x0002
168*fe3c1d93SMatthias Ringwald #define BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SERVICE_UUID_SIZE    0x0003
169*fe3c1d93SMatthias Ringwald #define BNEP_SETUP_CONNECTION_RESPONSE_CONNECTION_NOT_ALLOWED       0x0004
170*fe3c1d93SMatthias Ringwald 
1713deb3ec6SMatthias Ringwald /* API_START */
1723deb3ec6SMatthias Ringwald 
1733deb3ec6SMatthias Ringwald /**
1743deb3ec6SMatthias Ringwald  * @brief Set up BNEP.
1753deb3ec6SMatthias Ringwald  */
1763deb3ec6SMatthias Ringwald void bnep_init(void);
1773deb3ec6SMatthias Ringwald 
1783deb3ec6SMatthias Ringwald /**
1793deb3ec6SMatthias Ringwald  * @brief Check if a data packet can be send out.
1803deb3ec6SMatthias Ringwald  */
1813deb3ec6SMatthias Ringwald int bnep_can_send_packet_now(uint16_t bnep_cid);
1823deb3ec6SMatthias Ringwald 
1833deb3ec6SMatthias Ringwald /**
1841ed1a3bdSMatthias Ringwald  * @brief Request emission of BNEP_CAN_SEND_NOW as soon as possible
1851ed1a3bdSMatthias Ringwald  * @note BNEP_CAN_SEND_NOW might be emitted during call to this function
1861ed1a3bdSMatthias Ringwald  *       so packet handler should be ready to handle it
1871ed1a3bdSMatthias Ringwald  * @param bnep_cid
1881ed1a3bdSMatthias Ringwald  */
1891ed1a3bdSMatthias Ringwald void bnep_request_can_send_now_event(uint16_t bnep_cid);
1901ed1a3bdSMatthias Ringwald 
1911ed1a3bdSMatthias Ringwald /**
1923deb3ec6SMatthias Ringwald  * @brief Send a data packet.
1933deb3ec6SMatthias Ringwald  */
1943deb3ec6SMatthias Ringwald int bnep_send(uint16_t bnep_cid, uint8_t *packet, uint16_t len);
1953deb3ec6SMatthias Ringwald 
1963deb3ec6SMatthias Ringwald /**
1973deb3ec6SMatthias Ringwald  * @brief Set the network protocol filter.
1983deb3ec6SMatthias Ringwald  */
1993deb3ec6SMatthias Ringwald int bnep_set_net_type_filter(uint16_t bnep_cid, bnep_net_filter_t *filter, uint16_t len);
2003deb3ec6SMatthias Ringwald 
2013deb3ec6SMatthias Ringwald /**
2023deb3ec6SMatthias Ringwald  * @brief Set the multicast address filter.
2033deb3ec6SMatthias Ringwald  */
2043deb3ec6SMatthias Ringwald int bnep_set_multicast_filter(uint16_t bnep_cid, bnep_multi_filter_t *filter, uint16_t len);
2053deb3ec6SMatthias Ringwald 
2063deb3ec6SMatthias Ringwald /**
2073deb3ec6SMatthias Ringwald  * @brief Set security level required for incoming connections, need to be called before registering services.
20878315a58SMatthias Ringwald  * @deprecated use gap_set_security_level instead
2093deb3ec6SMatthias Ringwald  */
2103deb3ec6SMatthias Ringwald void bnep_set_required_security_level(gap_security_level_t security_level);
2113deb3ec6SMatthias Ringwald 
2123deb3ec6SMatthias Ringwald /**
2133deb3ec6SMatthias Ringwald  * @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.
2143deb3ec6SMatthias Ringwald  */
2150c249750SMatthias Ringwald 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);
2163deb3ec6SMatthias Ringwald 
2173deb3ec6SMatthias Ringwald /**
2183deb3ec6SMatthias Ringwald  * @brief Disconnects BNEP channel with given identifier.
2193deb3ec6SMatthias Ringwald  */
2203deb3ec6SMatthias Ringwald void bnep_disconnect(bd_addr_t addr);
2213deb3ec6SMatthias Ringwald 
2223deb3ec6SMatthias Ringwald /**
2233deb3ec6SMatthias Ringwald  * @brief Registers BNEP service, set a maximum frame size and assigns a packet handler. On embedded systems, use NULL for connection parameter.
2243deb3ec6SMatthias Ringwald  */
2250c249750SMatthias Ringwald uint8_t bnep_register_service(btstack_packet_handler_t packet_handler, uint16_t service_uuid, uint16_t max_frame_size);
2263deb3ec6SMatthias Ringwald 
2273deb3ec6SMatthias Ringwald /**
2283deb3ec6SMatthias Ringwald  * @brief Unregister BNEP service.
2293deb3ec6SMatthias Ringwald  */
2303deb3ec6SMatthias Ringwald void bnep_unregister_service(uint16_t service_uuid);
23151e0db05SMatthias Ringwald 
23251e0db05SMatthias Ringwald /**
23351e0db05SMatthias Ringwald  * @brief De-Init BNEP
23451e0db05SMatthias Ringwald  */
23551e0db05SMatthias Ringwald void bnep_deinit(void);
23651e0db05SMatthias Ringwald 
2373deb3ec6SMatthias Ringwald /* API_END */
2383deb3ec6SMatthias Ringwald 
2393deb3ec6SMatthias Ringwald #if defined __cplusplus
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald #endif
2423deb3ec6SMatthias Ringwald 
24380e33422SMatthias Ringwald #endif // BNEP_H
244