BNEP/PANU (Linux only)

Source Code: panu_demo.c

This example implements both a PANU client and a server. In server mode, it sets up a BNEP server and registers a PANU SDP record and waits for incoming connections. In client mode, it connects to a remote device, does an SDP Query to identify the PANU service and initiates a BNEP connection.

Note: currently supported only on Linux and provides a TAP network interface which you can configure yourself.

To enable client mode, uncomment ENABLE_PANU_CLIENT below.

Main application configuration

In the application configuration, L2CAP and BNEP are initialized and a BNEP service, for server mode, is registered, before the Bluetooth stack gets started, as shown in Listing here.

static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
#ifdef ENABLE_PANU_CLIENT
static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
#endif
static void network_send_packet_callback(const uint8_t * packet, uint16_t size);

static void panu_setup(void){

  // Initialize L2CAP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();

#ifdef ENABLE_BLE
  // Initialize LE Security Manager. Needed for cross-transport key derivation
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif

  // init SDP, create record for PANU and register with SDP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  memset(panu_sdp_record, 0, sizeof(panu_sdp_record));
  uint16_t network_packet_types[] = { NETWORK_TYPE_IPv4, NETWORK_TYPE_ARP, 0};  // 0 as end of list

  // Initialise BNEP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/bnep.h#L178">bnep_init</a>();
  // Minimum L2CAP MTU for bnep is 1691 bytes
#ifdef ENABLE_PANU_CLIENT
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/bnep.h#L230">bnep_register_service</a>(packet_handler, BLUETOOTH_SERVICE_CLASS_PANU, 1691);
  // PANU
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/pan.h#L67">pan_create_panu_sdp_record</a>(panu_sdp_record, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), network_packet_types, NULL, NULL, BNEP_SECURITY_NONE);
#else
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/bnep.h#L230">bnep_register_service</a>(packet_handler, BLUETOOTH_SERVICE_CLASS_NAP, 1691);
  // NAP Network Access Type: Other, 1 MB/s
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/pan.h#L102">pan_create_nap_sdp_record</a>(panu_sdp_record, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), network_packet_types, NULL, NULL, BNEP_SECURITY_NONE, PAN_NET_ACCESS_TYPE_OTHER, 1000000, NULL, NULL);
#endif
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( panu_sdp_record) &lt;= sizeof(panu_sdp_record));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(panu_sdp_record);

  // Initialize network interface
  btstack_network_init(&amp;network_send_packet_callback);

  // register for HCI events
  hci_event_callback_registration.callback = &amp;packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&amp;hci_event_callback_registration);
}

SDP parser callback

The SDP parsers retrieves the BNEP PAN UUID as explained in Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.

Packet Handler

The packet handler responds to various HCI Events.

static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
{
...
  switch (packet_type) {
        case HCI_EVENT_PACKET:
      event = hci_event_packet_get_type(packet);
      switch (event) {      
#ifdef ENABLE_PANU_CLIENT
        case BTSTACK_EVENT_STATE:
          if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
            printf("Start SDP BNEP query for remote PAN Network Access Point (NAP).\n");
            sdp_client_query_uuid16(&amp;handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_NAP);
          }
          break;
#endif
...

                case BNEP_EVENT_CHANNEL_OPENED:
          if (bnep_event_channel_opened_get_status(packet)) {
            printf("BNEP channel open failed, status 0x%02x\n", bnep_event_channel_opened_get_status(packet));
          } else {
            bnep_cid  = bnep_event_channel_opened_get_bnep_cid(packet);
            uuid_source = bnep_event_channel_opened_get_source_uuid(packet);
            uuid_dest   = bnep_event_channel_opened_get_destination_uuid(packet);
            mtu     = bnep_event_channel_opened_get_mtu(packet);
            bnep_event_channel_opened_get_remote_address(packet, event_addr);
            printf("BNEP connection open succeeded to %s source UUID 0x%04x dest UUID: 0x%04x, max frame size %u\n", <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L271">bd_addr_to_str</a>(event_addr), uuid_source, uuid_dest, mtu);

            <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L379">gap_local_bd_addr</a>(local_addr);
            <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_network.h#L65">btstack_network_up</a>(local_addr);
            printf("Network Interface %s activated\n", <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_network.h#L91">btstack_network_get_name</a>());
          }
                    break;

        case BNEP_EVENT_CHANNEL_TIMEOUT:
          printf("BNEP channel timeout! Channel will be closed\n");
          break;

        case BNEP_EVENT_CHANNEL_CLOSED:
          printf("BNEP channel closed\n");
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_network.h#L72">btstack_network_down</a>();
          break;

        case BNEP_EVENT_CAN_SEND_NOW:
          if (network_buffer_len &gt; 0) {
            <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/bnep.h#L199">bnep_send</a>(bnep_cid, (uint8_t*) network_buffer, network_buffer_len);
            network_buffer_len = 0;
            <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_network.h#L84">btstack_network_packet_sent</a>();
          }
          break;

        default:
          break;
      }
      break;

    case BNEP_DATA_PACKET:
      // Write out the ethernet frame to the network interface
      <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_network.h#L79">btstack_network_process_packet</a>(packet, size);
      break;

    default:
      break;
  }
}

When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING is received and the example is started in client mode, the remote SDP BNEP query is started.

BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or or when the connection fails. The status field returns the error code.

The TAP network interface is then configured. A data source is set up and registered with the run loop to receive Ethernet packets from the TAP interface.

The event contains both the source and destination UUIDs, as well as the MTU for this connection and the BNEP Channel ID, which is used for sending Ethernet packets over BNEP.

If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received and the BNEP connection will be closed

BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed.

BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the send of a stored network packet. The tap datas source can be enabled again

Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET. It is forwarded to the TAP interface.

Network packet handler

A pointer to the network packet is stored and a BNEP_EVENT_CAN_SEND_NOW requested

static void network_send_packet_callback(const uint8_t * packet, uint16_t size){
  network_buffer = packet;
  network_buffer_len = size;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/bnep.h#L191">bnep_request_can_send_now_event</a>(bnep_cid);
}