LE Nordic SPP-like Streamer Server

Source Code: nordic_spp_le_streamer.c

All newer operating systems provide GATT Client functionality. This example shows how to get a maximal throughput via BLE:

In theory, we should also update the connection parameters, but we already get a connection interval of 30 ms and there's no public way to use a shorter interval with iOS (if we're not implementing an HID device).

Note: To start the streaming, run the example. On remote device use some GATT Explorer, e.g. LightBlue, BLExplr to enable notifications.

Track throughput

We calculate the throughput by setting a start time and measuring the amount of data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s and reset the counter and start time.

static void test_reset(nordic_spp_le_streamer_connection_t * context){
  context-&gt;test_data_start = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L273">btstack_run_loop_get_time_ms</a>();
  context-&gt;test_data_sent = 0;
}

static void test_track_sent(nordic_spp_le_streamer_connection_t * context, int bytes_sent){
  context-&gt;test_data_sent += bytes_sent;
  // evaluate
  uint32_t now = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L273">btstack_run_loop_get_time_ms</a>();
  uint32_t time_passed = now - context-&gt;test_data_start;
  if (time_passed &lt; REPORT_INTERVAL_MS) return;
  // print speed
  int bytes_per_second = context-&gt;test_data_sent * 1000 / time_passed;
  printf("%c: %"PRIu32" bytes sent-&gt; %u.%03u kB/s\n", context-&gt;name, context-&gt;test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000);

  // restart
  context-&gt;test_data_start = now;
  context-&gt;test_data_sent  = 0;
}

HCI Packet Handler

The packet handler prints the welcome message and requests a connection parameter update for LE Connections

static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  UNUSED(channel);
  UNUSED(size);

  uint16_t conn_interval;
  hci_con_handle_t con_handle;

  if (packet_type != HCI_EVENT_PACKET) return;

  switch (hci_event_packet_get_type(packet)) {
    case BTSTACK_EVENT_STATE:
      // BTstack activated, get started
      if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
        printf("To start the streaming, please run nRF Toolbox -&gt; UART to connect.\n");
      } 
      break;
    case HCI_EVENT_META_GAP:
      switch (hci_event_gap_meta_get_subevent_code(packet)) {
        case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
          // print connection parameters (without using float operations)
          con_handle  = gap_subevent_le_connection_complete_get_connection_handle(packet);
          conn_interval = gap_subevent_le_connection_complete_get_conn_interval(packet);
          printf("LE Connection - Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval &amp; 3));
          printf("LE Connection - Connection Latency: %u\n", gap_subevent_le_connection_complete_get_conn_latency(packet));

          // request min con interval 15 ms for iOS 11+
          printf("LE Connection - Request 15 ms connection interval\n");
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1019">gap_request_connection_parameter_update</a>(con_handle, 12, 12, 4, 0x0048);
          break;
        default:
          break;
      }
      break;

    case HCI_EVENT_LE_META:
      switch (hci_event_le_meta_get_subevent_code(packet)) {
        case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
          // print connection parameters (without using float operations)
          con_handle  = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
          conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
          printf("LE Connection - Connection Param update - connection interval %u.%02u ms, latency %u\n", conn_interval * 125 / 100,
            25 * (conn_interval &amp; 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet));
          break;
        default:
          break;
      }
      break;  
    default:
      break;
  }
}

ATT Packet Handler

The packet handler is used to setup and tear down the spp-over-gatt connection and its MTU

static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  UNUSED(channel);
  UNUSED(size);

  if (packet_type != HCI_EVENT_PACKET) return;

  int mtu;
  nordic_spp_le_streamer_connection_t * context;

  switch (hci_event_packet_get_type(packet)) {
    case ATT_EVENT_CONNECTED:
      // setup new 
      context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID);
      if (!context) break;
      context-&gt;counter = 'A';
      context-&gt;test_data_len = ATT_DEFAULT_MTU - 4;   // -1 for nordic 0x01 packet type
      context-&gt;connection_handle = att_event_connected_get_handle(packet);
      break;
    case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
      mtu = att_event_mtu_exchange_complete_get_MTU(packet) - 3;
      context = connection_for_conn_handle(att_event_mtu_exchange_complete_get_handle(packet));
      if (!context) break;
      context-&gt;test_data_len = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L89">btstack_min</a>(mtu - 3, sizeof(context-&gt;test_data));
      printf("%c: ATT MTU = %u =&gt; use test data of len %u\n", context-&gt;name, mtu, context-&gt;test_data_len);
      break;

    default:
      break;
  }
}

Streamer

The streamer function checks if notifications are enabled and if a notification can be sent now. It creates some test data - a single letter that gets increased every time - and tracks the data sent.

static void nordic_can_send(void * some_context){
  UNUSED(some_context);

  // find next active streaming connection
  int old_connection_index = connection_index;
  while (1){
    // active found?
    if ((nordic_spp_le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) &amp;&amp;
      (nordic_spp_le_streamer_connections[connection_index].le_notification_enabled)) break;

    // check next
    next_connection_index();

    // none found
    if (connection_index == old_connection_index) return;
  }

  nordic_spp_le_streamer_connection_t * context = &amp;nordic_spp_le_streamer_connections[connection_index];

  // create test data
  context-&gt;counter++;
  if (context-&gt;counter &gt; 'Z') context-&gt;counter = 'A';
  memset(context-&gt;test_data, context-&gt;counter, context-&gt;test_data_len);

  // send
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/nordic_spp_service_server.h#L82">nordic_spp_service_server_send</a>(context-&gt;connection_handle, (uint8_t*) context-&gt;test_data, context-&gt;test_data_len);

  // track
  test_track_sent(context, context-&gt;test_data_len);

  // request next send event
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/nordic_spp_service_server.h#L74">nordic_spp_service_server_request_can_send_now</a>(&amp;context-&gt;send_request, context-&gt;connection_handle);

  // check next
  next_connection_index();
}