pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Kernel
      • Subsystems
      • Drivers
      • Third-party
      • Rust
      • Benchmarks
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Pebble protocol: PPoGATT

This sample is a peripheral that opens a Pebble protocol session with a phone app over PPoGATT (pbl/pp/transports/gatt.h) and answers its pings, on the protocol subsystem and NimBLE alone.

Once a phone connects, the sample asks it to secure the link, and then starts the transport: it looks for the PPoGATT services of the phone, reads what each says about its app, subscribes to its data and opens a session with it. The sample prints the sessions as they open and close, and what the phone app tells about itself.

Main thread

The subsystem asks the application for the thread it runs on, with pbl_pp_main_call() and pbl_pp_main_is_current() (pbl/pp/main.h). The sample gives it a work queue of its own. It cannot be the system work queue: the handlers of the endpoints run there, and may wait for the transport to make room while they send.

What NimBLE reports on its host thread, the link being secured or gone, is passed on to that thread, where the transport is started and stopped.

Endpoint

The sample defines the ping endpoint: its handler prints the cookie of each ping, and sends it back in a pong. The subsystem brings the endpoints every session needs, to ask the phone app for its version and to refuse the messages nothing handles.

Transport callbacks

PPoGATT asks the application for the serial number it sends to the phone, and to apply the connection response time a session asks for. The sample has a made-up serial number, and leaves the connection as the phone set it.

Bluetooth

The sample handles the GAP events of its connection itself, and so hands the notifications it receives to the listeners with pbl_bt_gattc_notify_rx(), which is how the transport gets the data of the phone.

The bond is kept by NimBLE’s config store in RAM, its default without settings: it is lost when the watch resets, and the phone, which keeps its own, then has to forget the watch and pair again.

Run:

pbl samples samples/subsys/pp/gatt

Boards:

every board

Sources

src/main.c
#include "sample.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <pbl/bluetooth/clients/gatt.h>
#include <pbl/kernel/sem.h>
#include <pbl/kernel/types.h>
#include <pbl/util/size.h>

#include <host/ble_hs.h>
#include <host/util/util.h>
#include <nimble/nimble_port.h>
#include <nimble/nimble_port_pebble.h>
#include <services/gap/ble_svc_gap.h>
#include <services/gatt/ble_svc_gatt.h>

#define NAME "PebbleOS"

static PBL_SEM_DEFINE(s_synced, 0, 1);
static uint8_t s_own_addr_type;

static int prv_gap_event(struct ble_gap_event *event, void *arg);

static void prv_advertise(void) {
  const struct ble_hs_adv_fields fields = {
    .flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP,
  };
  const struct ble_hs_adv_fields rsp_fields = {
    .name = (const uint8_t *)NAME,
    .name_len = strlen(NAME),
    .name_is_complete = 1,
  };
  const struct ble_gap_adv_params params = {
    .conn_mode = BLE_GAP_CONN_MODE_UND,
    .disc_mode = BLE_GAP_DISC_MODE_GEN,
  };
  int rc;

  rc = ble_gap_adv_set_fields(&fields);
  if (rc == 0) {
    rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
  }
  if (rc == 0) {
    rc = ble_gap_adv_start(s_own_addr_type, nullptr, BLE_HS_FOREVER, &params, prv_gap_event,
                           nullptr);
  }
  if (rc != 0) {
    printf("pp_gatt: advertising failed (%d)\n", rc);
    return;
  }

  printf("pp_gatt: advertising as %s\n", NAME);
}

/* Runs on the host thread. */
static int prv_gap_event(struct ble_gap_event *event, void *arg) {
  struct ble_gap_conn_desc desc;

  switch (event->type) {
    case BLE_GAP_EVENT_CONNECT:
      if (event->connect.status != 0) {
        prv_advertise();
        break;
      }
      printf("pp_gatt: connected\n");
      /* The session is only opened over an encrypted link. */
      (void)ble_gap_security_initiate(event->connect.conn_handle);
      break;
    case BLE_GAP_EVENT_DISCONNECT:
      printf("pp_gatt: disconnected, reason 0x%03x\n", event->disconnect.reason);
      sample_pp_stop();
      prv_advertise();
      break;
    case BLE_GAP_EVENT_ENC_CHANGE:
      if (event->enc_change.status != 0) {
        printf("pp_gatt: encryption failed (%d)\n", event->enc_change.status);
        break;
      }
      sample_pp_start(event->enc_change.conn_handle);
      break;
    case BLE_GAP_EVENT_NOTIFY_RX:
      (void)pbl_bt_gattc_notify_rx(event->notify_rx.conn_handle, event->notify_rx.attr_handle,
                                   event->notify_rx.om);
      break;
    case BLE_GAP_EVENT_REPEAT_PAIRING:
      /* A bonded peer that lost its keys: forget ours, and pair again. */
      if (ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc) == 0) {
        (void)ble_store_util_delete_peer(&desc.peer_id_addr);
      }
      return BLE_GAP_REPEAT_PAIRING_RETRY;
    default:
      break;
  }

  return 0;
}

static void prv_on_sync(void) {
  pbl_sem_give(&s_synced);
}

int main(void) {
  int rc;

  sample_pp_init();

  nimble_port_init();
  ble_hs_cfg.sync_cb = prv_on_sync;
  ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  ble_store_config_init();

  ble_svc_gap_init();
  ble_svc_gatt_init();

  rc = ble_svc_gap_device_name_set(NAME);
  if (rc != 0) {
    printf("pp_gatt: setting the name failed (%d)\n", rc);
    return rc;
  }

  nimble_port_pebble_init();

  pbl_sem_take(&s_synced, PBL_FOREVER);

  rc = ble_hs_util_ensure_addr(0);
  if (rc == 0) {
    rc = ble_hs_id_infer_auto(0, &s_own_addr_type);
  }
  if (rc != 0) {
    printf("pp_gatt: no address (%d)\n", rc);
    return rc;
  }

  prv_advertise();

  return 0;
}
src/pp.c
#include "sample.h"

#include <stdio.h>

#include <pbl/kernel/assert.h>
#include <pbl/kernel/compiler.h>
#include <pbl/kernel/thread.h>
#include <pbl/kernel/work.h>
#include <pbl/pp/endpoint.h>
#include <pbl/pp/main.h>
#include <pbl/pp/protocol.h>
#include <pbl/pp/send.h>
#include <pbl/pp/session.h>
#include <pbl/pp/transports/gatt.h>
#include <pbl/util/byteorder.h>

#define MAIN_STACK_SIZE 4096
#define MAIN_CALLS      32

enum : uint8_t {
  CMD_PING = 0,
  CMD_PONG = 1,
};

struct PBL_PACKED ping_msg {
  uint8_t cmd;
  uint32_t cookie;
};

/* The main thread of the protocol: a work queue of the sample. */
static PBL_WORKQ_DEFINE(s_main_q);
static PBL_WORK_POOL_DEFINE(s_main_pool, MAIN_CALLS);
static struct pbl_thread s_main_thread;
PBL_THREAD_STACK_DEFINE(s_main_stack, MAIN_STACK_SIZE);

void pbl_pp_main_call(void (*fn)(void *data), void *data) {
  /* A call from the main thread itself does not wait for a slot, and none may be lost. */
  const int rc = pbl_work_call(&s_main_q, &s_main_pool, fn, data, PBL_FOREVER);

  PBL_ASSERTN(rc == 0);
}

bool pbl_pp_main_is_current(void) {
  return pbl_workq_is_current(&s_main_q);
}

const char *pbl_pp_gatt_cb_get_serial_number(void) {
  return "SAMPLE000001";
}

static void prv_granted_cb(void *data) {
  pbl_bt_responsiveness_granted_cb_t granted = data;

  granted();
}

/* The sample leaves the connection as the phone set it, and grants every request. */
void pbl_pp_gatt_cb_set_responsiveness(uint16_t conn_handle, enum pbl_bt_consumer consumer,
                                       enum pbl_bt_response_time_state state,
                                       uint16_t max_period_secs,
                                       pbl_bt_responsiveness_granted_cb_t granted) {
  if (granted != nullptr) {
    pbl_pp_main_call(prv_granted_cb, granted);
  }
}

static void prv_session_cb(struct pbl_pp_session *session, enum pbl_pp_session_event event,
                           void *user_data) {
  const char *kind = pbl_pp_session_is_system(session) ? "system" : "app";

  switch (event) {
    case PBL_PP_SESSION_OPENED:
      printf("pp_gatt: %s session opened\n", kind);
      break;
    case PBL_PP_SESSION_CLOSED:
      printf("pp_gatt: %s session closed\n", kind);
      break;
    case PBL_PP_SESSION_CAPABILITIES:
      printf("pp_gatt: capabilities 0x%08x\n",
             (unsigned int)pbl_pp_session_get_capabilities(session));
      break;
    case PBL_PP_SESSION_REMOTE_INFO:
      printf("pp_gatt: phone app runs on OS %u\n", pbl_pp_session_get_remote_os(session));
      break;
  }
}

PBL_PP_SESSION_LISTENER_DEFINE(sample, prv_session_cb, nullptr);

/* Runs on the system work queue, where sending may wait for room. */
static void prv_ping_handler(struct pbl_pp_session *session, const uint8_t *data, size_t length) {
  const struct ping_msg *ping = (const struct ping_msg *)data;
  struct ping_msg pong;
  int rc;

  if (length < sizeof(*ping) || ping->cmd != CMD_PING) {
    return;
  }

  printf("pp_gatt: ping %u\n", (unsigned int)pbl_be32_to_cpu(ping->cookie));

  pong = (struct ping_msg){.cmd = CMD_PONG, .cookie = ping->cookie};
  rc = pbl_pp_send(session, PBL_PP_ENDPOINT_PING, (const uint8_t *)&pong, sizeof(pong),
                   PBL_PP_DEFAULT_TIMEOUT_MS);
  if (rc != 0) {
    printf("pp_gatt: pong not sent (%d)\n", rc);
  }
}

PBL_PP_ENDPOINT_DEFINE(PBL_PP_ENDPOINT_PING, PBL_PP_ACCESS_ANY, prv_ping_handler);

static void prv_start_cb(void *data) {
  pbl_pp_gatt_destroy();
  pbl_pp_gatt_create();
  pbl_pp_gatt_discover((uint16_t)(uintptr_t)data);
}

static void prv_stop_cb(void *data) {
  pbl_pp_gatt_destroy();
}

void sample_pp_init(void) {
  const struct pbl_thread_attr attr = {
    .name = "pp",
    .entry = pbl_workq_thread,
    .arg = &s_main_q,
    .prio = CONFIG_KERNEL_MAIN_THREAD_PRIO,
    .privileged = true,
    .stack = s_main_stack,
    .stack_size = sizeof(s_main_stack),
  };

  pbl_thread_create(&s_main_thread, &attr);
}

void sample_pp_start(uint16_t conn_handle) {
  pbl_pp_main_call(prv_start_cb, (void *)(uintptr_t)conn_handle);
}

void sample_pp_stop(void) {
  pbl_pp_main_call(prv_stop_cb, nullptr);
}
prj.conf
CONFIG_CONSOLE=y
CONFIG_CONSOLE_STDOUT=y
CONFIG_NIMBLE=y
CONFIG_NIMBLE_STORE_CONFIG=y
CONFIG_NIMBLE_BLE_SM_SC=y
CONFIG_NIMBLE_BLE_SM_BONDING=y
CONFIG_NIMBLE_BLE_SM_OUR_KEY_DIST=3
CONFIG_NIMBLE_BLE_SM_THEIR_KEY_DIST=3
CONFIG_PP=y
CONFIG_PP_TRANSPORT_GATT=y

Output

pp_gatt: advertising as PebbleOS

Generated from PebbleOS d62d3c7d4. This page is maintained in the pebbleos repository: docs/samples/subsys/pp/gatt.md.

Overview

  • Main thread
  • Endpoint
  • Transport callbacks
  • Bluetooth
  • Sources
  • Output