Dual Mode - SPP and LE Counter

Source Code: spp_and_le_counter.c

The SPP and LE Counter example combines the Bluetooth Classic SPP Counter and the Bluetooth LE Counter into a single application.

In this Section, we only point out the differences to the individual examples and how the stack is configured.

Note: To test, please run the example, and then:

Advertisements

The Flags attribute in the Advertisement Data indicates if a device is dual-mode or le-only.

const uint8_t adv_data[] = {
  // Flags general discoverable
  0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x02,
  // Name
  0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r', 
  // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
  0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
};

Packet Handler

The packet handler of the combined example is just the combination of the individual packet handlers.

Heartbeat Handler

Similar to the packet handler, the heartbeat handler is the combination of the individual ones. After updating the counter, it requests an ATT_EVENT_CAN_SEND_NOW and/or RFCOMM_EVENT_CAN_SEND_NOW

static void heartbeat_handler(struct btstack_timer_source *ts){

  if (rfcomm_channel_id || le_notification_enabled) {
    beat();
  }

  if (rfcomm_channel_id){
    <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/rfcomm.h#L373">rfcomm_request_can_send_now_event</a>(rfcomm_channel_id);
  }

  if (le_notification_enabled) {
    <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/att_server.h#L210">att_server_request_can_send_now_event</a>(att_con_handle);
  }

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L233">btstack_run_loop_set_timer</a>(ts, HEARTBEAT_PERIOD_MS);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L260">btstack_run_loop_add_timer</a>(ts);
}

Main Application Setup

As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.

int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
  UNUSED(argc);
  UNUSED(argv);

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

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/rfcomm.h#L278">rfcomm_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/rfcomm.h#L323">rfcomm_register_service</a>(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);

  // init SDP, create record for SPP and register with SDP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/spp_server.h#L63">spp_create_sdp_record</a>(spp_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), RFCOMM_SERVER_CHANNEL, "SPP Counter");
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( spp_service_buffer) &lt;= sizeof(spp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(spp_service_buffer);

#ifdef ENABLE_GATT_OVER_CLASSIC
  // init SDP, create record for GATT and register with SDP
  memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/gatt_sdp.h#L64">gatt_create_sdp_record</a>(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(gatt_service_buffer);
  printf("SDP service record size: %u\n", <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>(gatt_service_buffer));
#endif

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("SPP and LE Counter 00:00:00:00:00:00");
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L549">gap_ssp_set_io_capability</a>(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);

  // setup SM: Just Works pairing, without bonding
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L125">sm_set_io_capabilities</a>(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L119">sm_set_authentication_requirements</a>(0);

  // setup ATT server
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/att_server.h#L80">att_server_init</a>(profile_data, att_read_callback, att_write_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);

  // register for SM events
  sm_event_callback_registration.callback = &amp;packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L94">sm_add_event_handler</a>(&amp;sm_event_callback_registration);

  // register for ATT events
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/att_server.h#L102">att_server_register_packet_handler</a>(packet_handler);

  // setup advertisements
  uint16_t adv_int_min = 0x0030;
  uint16_t adv_int_max = 0x0030;
  uint8_t adv_type = 0;
  bd_addr_t null_addr;
  memset(null_addr, 0, 6);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L714">gap_advertisements_set_params</a>(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L701">gap_advertisements_set_data</a>(adv_data_len, (uint8_t*) adv_data);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L721">gap_advertisements_enable</a>(1);

  // set one-shot timer
  heartbeat.process = &amp;heartbeat_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L233">btstack_run_loop_set_timer</a>(&amp;heartbeat, HEARTBEAT_PERIOD_MS);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L260">btstack_run_loop_add_timer</a>(&amp;heartbeat);

  // beat once
  beat();

  // turn on!
    <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);

  return 0;
}