---
title: "Pebble protocol: USB"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/pp/usb/
last_updated: 2026-12-05
sdk_version: 4.44
---
# Pebble protocol: USB

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

This sample opens Pebble protocol sessions over USB (`pbl/pp/transports/usb.h`) and over the serial console, with a few endpoints a host tool needs: ping, the watch’s version, which libpebble2 asks for when it connects, and app logs, which the sample sends once a second while the host wants them. A last endpoint measures throughput: it takes what the host sends, sends the host as many messages as it asks for, and answers once all that came before arrived.

Connect the board’s USB device port to a computer, then talk to the sample with `pbl-watch` (`tools/libs/pbl-watch`), or over the console with `--console` and the board’s serial port:

```
pbl-watch ping --usb
pbl-watch logs --usb
```

As the protocol’s main thread, the sample gives the subsystem a work queue of its own, as [Pebble protocol: PPoGATT](https://developer.repebble.com/pebbleos/docs/samples/subsys/pp/gatt.md) does.

Run:

`pbl samples samples/subsys/pp/usb`

Boards:

every board

## Sources

src/main.c

```
#include \<stdatomic.h\>#include \<stddef.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/console/console.h\>#include \<pbl/kernel/assert.h\>#include \<pbl/kernel/compiler.h\>#include \<pbl/kernel/thread.h\>#include \<pbl/kernel/time.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/usb.h\>#include \<pbl/usb/usbd.h\>#include \<pbl/util/byteorder.h\>#include \<pbl/util/math.h\>#define MAIN\_STACK\_SIZE 4096#define MAIN\_CALLS 32#define ENDPOINT\_BENCH 0x0b0b#define LOG\_PERIOD\_MS 1000Uenum : uint8\_t {CMD\_PING = 0,CMD\_PONG = 1,};enum : uint8\_t {VERSION\_REQUEST = 0,VERSION\_RESPONSE = 1,};enum : uint8\_t {BENCH\_SINK = 0,BENCH\_SOURCE = 1,BENCH\_SYNC = 2,};struct PBL\_PACKED ping\_msg {uint8\_t cmd;uint32\_t cookie;};struct PBL\_PACKED fw\_version {uint32\_t timestamp;char version\_tag[32];char git\_hash[8];uint8\_t is\_recovery;uint8\_t hw\_platform;uint8\_t metadata\_version;};struct PBL\_PACKED version\_response {uint8\_t cmd;struct fw\_version running;struct fw\_version recovery;uint32\_t bootloader\_timestamp;char board[9];char serial[12];uint8\_t bt\_address[6];uint32\_t resource\_crc;uint32\_t resource\_timestamp;char language[6];uint16\_t language\_version;uint64\_t capabilities;};struct PBL\_PACKED app\_log\_msg {uint8\_t uuid[16];uint32\_t timestamp;uint8\_t level;uint8\_t message\_length;uint16\_t line\_number;char filename[16];char message[32];};struct PBL\_PACKED bench\_source {uint8\_t cmd;uint16\_t count;uint16\_t size;};/\* 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);static void prv\_log\_cb(struct pbl\_work \*work);static PBL\_DELAYED\_WORK\_DEFINE(s\_log\_work, prv\_log\_cb);static struct pbl\_pp\_session \*\_Atomic s\_log\_session;static unsigned int s\_log\_count;static uint8\_t s\_bench\_buf[PBL\_PP\_MAX\_OUTBOUND\_PAYLOAD];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);}static const char \*prv\_transport(const struct pbl\_pp\_session \*session) {return pbl\_pp\_session\_get\_transport(session)-\>name;}static void prv\_session\_cb(struct pbl\_pp\_session \*session, enum pbl\_pp\_session\_event event,void \*user\_data) {switch (event) {case PBL\_PP\_SESSION\_OPENED:printf("pp\_usb: %s session opened\n", prv\_transport(session));break;case PBL\_PP\_SESSION\_CLOSED:printf("pp\_usb: %s session closed\n", prv\_transport(session));break;default: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;if (length \< sizeof(\*ping) || ping-\>cmd != CMD\_PING) {return;}pong = (struct ping\_msg){.cmd = CMD\_PONG, .cookie = ping-\>cookie};(void)pbl\_pp\_send(session, PBL\_PP\_ENDPOINT\_PING, (const uint8\_t \*)&pong, sizeof(pong),PBL\_PP\_DEFAULT\_TIMEOUT\_MS);}PBL\_PP\_ENDPOINT\_DEFINE(PBL\_PP\_ENDPOINT\_PING, prv\_ping\_handler);static void prv\_version\_handler(struct pbl\_pp\_session \*session, const uint8\_t \*data,size\_t length) {struct version\_response rsp = {.cmd = VERSION\_RESPONSE,.running ={.version\_tag = "v0.0.0-pp-usb",.git\_hash = "sample",},.board = "sample",.language = "en\_US",};if (length \< 1U || data[0] != VERSION\_REQUEST) {return;}memcpy(rsp.serial, "SAMPLE000001", sizeof(rsp.serial));rsp.running.timestamp = pbl\_cpu\_to\_be32((uint32\_t)pbl\_realtime());(void)pbl\_pp\_send(session, PBL\_PP\_ENDPOINT\_SYSTEM\_VERSION, (const uint8\_t \*)&rsp, sizeof(rsp),PBL\_PP\_DEFAULT\_TIMEOUT\_MS);}PBL\_PP\_ENDPOINT\_DEFINE(PBL\_PP\_ENDPOINT\_SYSTEM\_VERSION, prv\_version\_handler);static void prv\_log\_cb(struct pbl\_work \*work) {struct pbl\_pp\_session \*session = atomic\_load(&s\_log\_session);struct app\_log\_msg msg = {.level = 100U,.filename = "main.c",};int len;if (session == nullptr) {return;}len = snprintf(msg.message, sizeof(msg.message), "tick %u, uptime %u ms", s\_log\_count++,(unsigned int)pbl\_uptime\_ms());msg.message\_length = (uint8\_t)PBL\_MIN((size\_t)len, sizeof(msg.message));msg.timestamp = pbl\_cpu\_to\_be32((uint32\_t)pbl\_realtime());msg.line\_number = pbl\_cpu\_to\_be16((uint16\_t)\_\_LINE\_\_);if (pbl\_pp\_send(session, PBL\_PP\_ENDPOINT\_APP\_LOG, (const uint8\_t \*)&msg,offsetof(struct app\_log\_msg, message) + msg.message\_length,PBL\_PP\_DEFAULT\_TIMEOUT\_MS) != 0) {atomic\_store(&s\_log\_session, nullptr);return;}(void)pbl\_delayed\_work\_schedule(&pbl\_sys\_workq, &s\_log\_work, PBL\_MSEC(LOG\_PERIOD\_MS));}/\* The host turns the shipping of app logs on or off. \*/static void prv\_app\_log\_handler(struct pbl\_pp\_session \*session, const uint8\_t \*data,size\_t length) {if (length \< 1U) {return;}printf("pp\_usb: app logs %s over %s\n", data[0] != 0U ? "on" : "off", prv\_transport(session));atomic\_store(&s\_log\_session, data[0] != 0U ? session : nullptr);if (data[0] != 0U) {(void)pbl\_delayed\_work\_schedule(&pbl\_sys\_workq, &s\_log\_work, PBL\_MSEC(0));}}PBL\_PP\_ENDPOINT\_DEFINE(PBL\_PP\_ENDPOINT\_APP\_LOG, prv\_app\_log\_handler);/\* Sinks what the host sends, sends it what it asks for, and answers when all before arrived. \*/static void prv\_bench\_handler(struct pbl\_pp\_session \*session, const uint8\_t \*data, size\_t length) {const struct bench\_source \*src = (const struct bench\_source \*)data;uint16\_t count;uint16\_t size;if (length \< 1U) {return;}switch (data[0]) {case BENCH\_SYNC:(void)pbl\_pp\_send(session, ENDPOINT\_BENCH, data, 1U, PBL\_PP\_DEFAULT\_TIMEOUT\_MS);break;case BENCH\_SOURCE:if (length \< sizeof(\*src)) {return;}count = pbl\_be16\_to\_cpu(src-\>count);size = (uint16\_t)PBL\_CLAMP(pbl\_be16\_to\_cpu(src-\>size), 1U, sizeof(s\_bench\_buf));s\_bench\_buf[0] = BENCH\_SOURCE;for (uint16\_t i = 0U; i \< count; i++) {if (pbl\_pp\_send(session, ENDPOINT\_BENCH, s\_bench\_buf, size, PBL\_PP\_DEFAULT\_TIMEOUT\_MS) !=0) {printf("pp\_usb: bench stopped after %u messages\n", i);return;}}break;default:break;}}PBL\_PP\_ENDPOINT\_DEFINE(ENDPOINT\_BENCH, prv\_bench\_handler);int main(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),};int rc;pbl\_thread\_create(&s\_main\_thread, &attr);/\* The console takes frames, for the same endpoints over the serial port. \*/pbl\_console\_enable\_prompt();rc = pbl\_usbd\_enable();if (rc != 0) {printf("pp\_usb: USB device failed (%d)\n", rc);return rc;}printf("pp\_usb: USB device up\n");return 0;}
```

## Output

```
pp_usb: USB device up
```

Generated from PebbleOS [7416f78a1](https://github.com/coredevices/pebbleos/commit/7416f78a1dc6d00d91f42a28c68918ae92bcfbbc). This page is maintained in the pebbleos repository: [docs/samples/subsys/pp/usb.md](https://github.com/coredevices/pebbleos/blob/7416f78a1dc6d00d91f42a28c68918ae92bcfbbc/docs/samples/subsys/pp/usb.md).
