---
title: "Pebble protocol: PPoGATT"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/pp/gatt/
last_updated: 2026-11-24
sdk_version: 4.44
---
# Pebble protocol: PPoGATT

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/pp/gatt/

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 32enum : 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](https://github.com/coredevices/pebbleos/commit/d62d3c7d42c76d513fd998422f03315fc7baa2fd). This page is maintained in the pebbleos repository: [docs/samples/subsys/pp/gatt.md](https://github.com/coredevices/pebbleos/blob/d62d3c7d42c76d513fd998422f03315fc7baa2fd/docs/samples/subsys/pp/gatt.md).
