bnep.h 7.9 KB

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