This sample is a peripheral that shows the notifications of an iOS device
with the ANCS client (pbl/bluetooth/clients/ancs.h) and what it plays with
the AMS client (pbl/bluetooth/clients/ams.h), on NimBLE alone.
It advertises a solicitation for the Apple Notification Center Service, so that iOS lists it among the accessories to pair with. Once a device connects, the sample asks it to secure the link, since the services are only there for a paired accessory, and then starts the clients: each looks for its service and subscribes to it when the device has it.
Each client is in a file of its own, and the sample has an option for each:
CONFIG_SAMPLE_ANCS and CONFIG_SAMPLE_AMS, both on by default. With only
the second, the sample solicits the Apple Media Service instead.
The ANCS client subscribes to the Data Source and the Notification Source, in that order so that no response is missed. Each notification the device adds, modifies or removes is printed as the client reports it. For the first two the sample then asks the client for the title and the message, and prints them once the client has the whole response. The client runs one request at a time, so the attributes of a notification that comes while another is being asked about are not got.
The AMS client subscribes to the Remote Command, which tells the commands the media player takes, and to the Entity Update, with which it then registers for the playback info and the volume of the player, and for the artist, the album, the title and the duration of the track. Their values are printed as the device tells them: once registered, and whenever they change. A value that did not fit comes cut, and the sample then reads the whole of it.
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 clients get them. The clients call back on the NimBLE host
thread.
The bond is kept by NimBLE’s config store in a settings file: the board overlays give each board a filesystem partition.
pbl samples samples/subsys/bluetooth/ios_clients
every board
#include "sample.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <pbl/bluetooth/ams.h>
#include <pbl/bluetooth/ancs.h>
#include <pbl/bluetooth/clients/gatt.h>
#include <pbl/fs/fs.h>
#include <pbl/fs/pfs.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 struct pbl_fs_mount s_mount = {.mnt_point = "/", .ops = &pbl_fs_pfs_ops};
static PBL_SEM_DEFINE(s_synced, 0, 1);
static uint8_t s_own_addr_type;
/* iOS lists the accessories that ask for ANCS; the payload has room for one service. */
#if defined(CONFIG_SAMPLE_ANCS)
#define SOLICITED_UUID PBL_BT_ANCS_UUID_SERVICE
#else
#define SOLICITED_UUID PBL_BT_AMS_UUID_SERVICE
#endif
static int prv_gap_event(struct ble_gap_event *event, void *arg);
static void prv_advertise(void) {
/*
* Flags, and a solicitation for a service of the device. The payload is built by hand, as
* NimBLE's advertising fields have no solicitation: each field is its length (type and
* data), its type and its data.
*/
static const uint8_t data[] = {
1 + 1, BLE_HS_ADV_TYPE_FLAGS, BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP,
1 + 16, BLE_HS_ADV_TYPE_SOL_UUIDS128, SOLICITED_UUID,
};
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_data(data, sizeof(data));
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, ¶ms, prv_gap_event,
nullptr);
}
if (rc != 0) {
printf("ios_clients: advertising failed (%d)\n", rc);
return;
}
printf("ios_clients: 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("ios_clients: connected\n");
/* The services are only there for a paired accessory, over an encrypted link. */
(void)ble_gap_security_initiate(event->connect.conn_handle);
break;
case BLE_GAP_EVENT_DISCONNECT:
printf("ios_clients: disconnected, reason 0x%03x\n", event->disconnect.reason);
#if defined(CONFIG_SAMPLE_ANCS)
sample_ancs_stop();
#endif
#if defined(CONFIG_SAMPLE_AMS)
sample_ams_stop();
#endif
prv_advertise();
break;
case BLE_GAP_EVENT_ENC_CHANGE:
if (event->enc_change.status != 0) {
printf("ios_clients: encryption failed (%d)\n", event->enc_change.status);
break;
}
#if defined(CONFIG_SAMPLE_ANCS)
sample_ancs_start(event->enc_change.conn_handle);
#endif
#if defined(CONFIG_SAMPLE_AMS)
sample_ams_start(event->enc_change.conn_handle);
#endif
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;
/* The store keeps the bond in a settings file. */
rc = pbl_fs_mount(&s_mount);
if (rc != 0) {
printf("ios_clients: mount failed (%d)\n", rc);
return rc;
}
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("ios_clients: 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("ios_clients: no address (%d)\n", rc);
return rc;
}
prv_advertise();
return 0;
}
#include "sample.h"
#include <stdint.h>
#include <stdio.h>
#include <pbl/bluetooth/clients/ancs.h>
#include <pbl/util/size.h>
#define TITLE_MAX 32
#define MESSAGE_MAX 96
static const struct pbl_bt_ancs_attr_req s_attrs[] = {
{.id = PBL_BT_ANCS_NOTIF_ATTR_TITLE, .max_len = TITLE_MAX},
{.id = PBL_BT_ANCS_NOTIF_ATTR_MESSAGE, .max_len = MESSAGE_MAX},
};
/* Room for the response: its header, and each attribute with its own. */
PBL_BT_ANCS_CLIENT_DEFINE(s_client,
5 + 2 * sizeof(struct pbl_bt_ancs_attr) + TITLE_MAX + MESSAGE_MAX);
static void prv_discovered(struct pbl_bt_ancs_client *client, int status) {
if (status != 0) {
printf("ancs: not on the peer (%d)\n", status);
return;
}
printf("ancs: found\n");
/* The Data Source first, so that no response is missed. */
(void)pbl_bt_ancs_client_subscribe(client, PBL_BT_ANCS_SOURCE_DATA, true);
(void)pbl_bt_ancs_client_subscribe(client, PBL_BT_ANCS_SOURCE_NOTIFICATION, true);
}
static void prv_subscribed(struct pbl_bt_ancs_client *client, enum pbl_bt_ancs_source source,
bool enable, int status) {
if (status != 0) {
printf("ancs: subscribing failed (%d)\n", status);
} else if (source == PBL_BT_ANCS_SOURCE_NOTIFICATION) {
printf("ancs: subscribed\n");
}
}
static void prv_notification(struct pbl_bt_ancs_client *client,
const struct pbl_bt_ancs_notif *notif) {
static const char *const events[] = {"added", "modified", "removed"};
if (notif->event >= PBL_ARRAY_SIZE(events)) {
return;
}
printf("ancs: notification %u %s, category %u%s\n", (unsigned int)notif->uid,
events[notif->event], notif->category,
(notif->flags & PBL_BT_ANCS_FLAG_PRE_EXISTING) ? ", from before" : "");
/* One request at a time: the attributes of a notification that comes meanwhile are not got. */
if (notif->event != PBL_BT_ANCS_EVENT_REMOVED) {
(void)pbl_bt_ancs_client_get_notif_attrs(client, notif->uid, s_attrs, PBL_ARRAY_SIZE(s_attrs));
}
}
static void prv_notif_attrs(struct pbl_bt_ancs_client *client, int status, uint32_t uid,
const struct pbl_bt_ancs_attr *const *attrs, uint8_t num_attrs) {
static const char *const names[] = {"title", "message"};
if (status != 0) {
/* E.g. the notification is gone since. */
printf("ancs: request refused (%d)\n", status);
return;
}
for (uint8_t i = 0; i < num_attrs; i++) {
printf("ancs: %s: %.*s\n", names[i], (int)attrs[i]->len, (const char *)attrs[i]->value);
}
}
static const struct pbl_bt_ancs_client_cb s_cb = {
.discovered = prv_discovered,
.subscribed = prv_subscribed,
.notification = prv_notification,
.notif_attrs = prv_notif_attrs,
};
void sample_ancs_start(uint16_t conn_handle) {
const int rc = pbl_bt_ancs_client_start(&s_client, conn_handle, &s_cb);
if (rc != 0) {
printf("ancs: starting the client failed (%d)\n", rc);
}
}
void sample_ancs_stop(void) {
pbl_bt_ancs_client_stop(&s_client);
}
#include "sample.h"
#include <stdint.h>
#include <stdio.h>
#include <pbl/bluetooth/clients/ams.h>
#include <pbl/util/size.h>
PBL_BT_AMS_CLIENT_DEFINE(s_client);
static const uint8_t s_player_attrs[] = {
PBL_BT_AMS_PLAYER_ATTR_PLAYBACK_INFO,
PBL_BT_AMS_PLAYER_ATTR_VOLUME,
};
static const uint8_t s_track_attrs[] = {
PBL_BT_AMS_TRACK_ATTR_ARTIST,
PBL_BT_AMS_TRACK_ATTR_ALBUM,
PBL_BT_AMS_TRACK_ATTR_TITLE,
PBL_BT_AMS_TRACK_ATTR_DURATION,
};
static const char *prv_attr_name(uint8_t entity, uint8_t attr) {
static const char *const player[] = {"player", "playback", "volume"};
static const char *const track[] = {"artist", "album", "title", "duration"};
if (entity == PBL_BT_AMS_ENTITY_PLAYER && attr < PBL_ARRAY_SIZE(player)) {
return player[attr];
} else if (entity == PBL_BT_AMS_ENTITY_TRACK && attr < PBL_ARRAY_SIZE(track)) {
return track[attr];
}
return "?";
}
static void prv_discovered(struct pbl_bt_ams_client *client, int status) {
if (status != 0) {
printf("ams: not on the peer (%d)\n", status);
return;
}
printf("ams: found\n");
(void)pbl_bt_ams_client_subscribe(client, PBL_BT_AMS_SOURCE_REMOTE_COMMAND, true);
(void)pbl_bt_ams_client_subscribe(client, PBL_BT_AMS_SOURCE_ENTITY_UPDATE, true);
}
static void prv_subscribed(struct pbl_bt_ams_client *client, enum pbl_bt_ams_source source,
bool enable, int status) {
if (status != 0) {
printf("ams: subscribing failed (%d)\n", status);
return;
}
/* The Entity Update only takes registrations once subscribed to. */
if (source == PBL_BT_AMS_SOURCE_ENTITY_UPDATE) {
printf("ams: subscribed\n");
(void)pbl_bt_ams_client_register(client, PBL_BT_AMS_ENTITY_PLAYER, s_player_attrs,
PBL_ARRAY_SIZE(s_player_attrs));
(void)pbl_bt_ams_client_register(client, PBL_BT_AMS_ENTITY_TRACK, s_track_attrs,
PBL_ARRAY_SIZE(s_track_attrs));
}
}
static void prv_commands(struct pbl_bt_ams_client *client, const uint8_t *commands,
uint8_t num_commands) {
printf("ams: the player takes commands");
for (uint8_t i = 0; i < num_commands; i++) {
printf(" %u", commands[i]);
}
printf("\n");
}
static void prv_update(struct pbl_bt_ams_client *client, const struct pbl_bt_ams_update *update) {
/* One read at a time: a value cut while another is being read stays cut. */
if ((update->flags & PBL_BT_AMS_FLAG_TRUNCATED) &&
pbl_bt_ams_client_read_attr(client, update->entity, update->attr) == 0) {
return;
}
printf("ams: %s: %.*s\n", prv_attr_name(update->entity, update->attr), (int)update->len,
(const char *)update->value);
}
static void prv_registered(struct pbl_bt_ams_client *client, int status) {
if (status != 0) {
printf("ams: registering failed (%d)\n", status);
}
}
static void prv_attr_read(struct pbl_bt_ams_client *client, int status,
enum pbl_bt_ams_entity entity, uint8_t attr, const uint8_t *value,
uint16_t len) {
if (status != 0) {
printf("ams: reading failed (%d)\n", status);
return;
}
printf("ams: %s: %.*s\n", prv_attr_name(entity, attr), (int)len, (const char *)value);
}
static const struct pbl_bt_ams_client_cb s_cb = {
.discovered = prv_discovered,
.subscribed = prv_subscribed,
.commands = prv_commands,
.update = prv_update,
.registered = prv_registered,
.attr_read = prv_attr_read,
};
void sample_ams_start(uint16_t conn_handle) {
const int rc = pbl_bt_ams_client_start(&s_client, conn_handle, &s_cb);
if (rc != 0) {
printf("ams: starting the client failed (%d)\n", rc);
}
}
void sample_ams_stop(void) {
pbl_bt_ams_client_stop(&s_client);
}
mainmenu "iOS clients sample"
source "Kconfig.pebbleos"
config SAMPLE_ANCS
bool "Notifications, with the ANCS client"
default y
select BT_ANCS_CLIENT
config SAMPLE_AMS
bool "Media, with the AMS client"
default y
select BT_AMS_CLIENT
CONFIG_CONSOLE=y
CONFIG_CONSOLE_STDOUT=y
CONFIG_FLASH=y
CONFIG_FS=y
CONFIG_SETTINGS=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
ios_clients: advertising as PebbleOS
Generated from PebbleOS 4bbc630fa. This page is maintained in the pebbleos repository: docs/samples/subsys/bluetooth/ios_clients.md.