LE u-blox SPP-like Heartbeat Server

Source Code: ublox_spp_le_counter.c

Main Application Setup

Listing here shows main application code. It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled ATT Database generated from ublox_spp_le_counter.gatt. Additionally, it enables the Battery Service Server with the current battery level. Finally, it configures the advertisements and the heartbeat handler and boots the Bluetooth stack. In this example, the Advertisement contains the Flags attribute and the device name. The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.

static btstack_timer_source_t heartbeat;
static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
static btstack_context_callback_registration_t send_request;
static btstack_packet_callback_registration_t  hci_event_callback_registration;

const uint8_t adv_data[] = {
  // Flags general discoverable, BR/EDR not supported
  2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 
  // Name
  5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, '6', '-','5', '6', 
  // UUID ...
  17, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS, 0x1, 0xd7, 0xe9, 0x1, 0x4f, 0xf3, 0x44, 0xe7, 0x83, 0x8f, 0xe2, 0x26, 0xb9, 0xe1, 0x56, 0x24,
};

const uint8_t adv_data_len = sizeof(adv_data);

Heartbeat Handler

The heartbeat handler updates the value of the single Characteristic provided in this example, and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing here.

static int  counter = 0;
static char counter_string[30];
static int  counter_string_len;

static void beat(void){
  counter++;
  counter_string_len = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L425">btstack_snprintf_assert_complete</a>(counter_string, sizeof(counter_string), "BTstack counter %03u", counter);
}

static void ublox_can_send(void * context){
  UNUSED(context);
  beat();
  printf("SEND: %s\n", counter_string);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/ublox_spp_service_server.h#L82">ublox_spp_service_server_send</a>(con_handle, (uint8_t*) counter_string, counter_string_len);
}

static void heartbeat_handler(struct btstack_timer_source *ts){
  if (con_handle != HCI_CON_HANDLE_INVALID) {
    send_request.callback = &amp;ublox_can_send;
    <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt-service/ublox_spp_service_server.h#L74">ublox_spp_service_server_request_can_send_now</a>(&amp;send_request, 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);
}

Packet Handler

The packet handler is used to:

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

  switch (packet_type) {
    case HCI_EVENT_PACKET:
      switch (hci_event_packet_get_type(packet)) {
        case HCI_EVENT_DISCONNECTION_COMPLETE:
          con_handle = HCI_CON_HANDLE_INVALID;
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}