|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/types.h> |
| 8 | +#include <stddef.h> |
| 9 | +#include <zephyr/sys/printk.h> |
| 10 | +#include <zephyr/sys/util.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | +#include <zephyr/bluetooth/bluetooth.h> |
| 13 | +#include <zephyr/bluetooth/hci.h> |
| 14 | +#include "common.h" |
| 15 | + |
| 16 | +LOG_MODULE_REGISTER(bt_thread, LOG_LEVEL_INF); |
| 17 | + |
| 18 | +#define DEVICE_NAME CONFIG_BT_DEVICE_NAME |
| 19 | +#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) |
| 20 | + |
| 21 | +/* |
| 22 | + * Set Advertisement data. Based on the Eddystone specification: |
| 23 | + * https://github.com/google/eddystone/blob/master/protocol-specification.md |
| 24 | + * https://github.com/google/eddystone/tree/master/eddystone-url |
| 25 | + */ |
| 26 | +static const struct bt_data ad[] = { |
| 27 | + BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR), |
| 28 | + BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe), |
| 29 | + BT_DATA_BYTES(BT_DATA_SVC_DATA16, |
| 30 | + 0xaa, 0xfe, /* Eddystone UUID */ |
| 31 | + 0x10, /* Eddystone-URL frame type */ |
| 32 | + 0x00, /* Calibrated Tx power at 0m */ |
| 33 | + 0x00, /* URL Scheme Prefix http://www. */ |
| 34 | + 'z', 'e', 'p', 'h', 'y', 'r', |
| 35 | + 'p', 'r', 'o', 'j', 'e', 'c', 't', |
| 36 | + 0x08) /* .org */ |
| 37 | +}; |
| 38 | + |
| 39 | +/* Set Scan Response data */ |
| 40 | +static const struct bt_data sd[] = { |
| 41 | + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), |
| 42 | +}; |
| 43 | + |
| 44 | +static void bt_ready(int err) |
| 45 | +{ |
| 46 | + char addr_s[BT_ADDR_LE_STR_LEN]; |
| 47 | + bt_addr_le_t addr = {0}; |
| 48 | + size_t count = 1; |
| 49 | + |
| 50 | + if (err) { |
| 51 | + LOG_ERR("Bluetooth init failed (err %d)", err); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + LOG_INF("Bluetooth initialized\n"); |
| 56 | + |
| 57 | + /* Start advertising */ |
| 58 | + err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad), |
| 59 | + sd, ARRAY_SIZE(sd)); |
| 60 | + if (err) { |
| 61 | + LOG_ERR("Advertising failed to start (err %d)", err); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /* For connectable advertising you would use |
| 67 | + * bt_le_oob_get_local(). For non-connectable non-identity |
| 68 | + * advertising an non-resolvable private address is used; |
| 69 | + * there is no API to retrieve that. |
| 70 | + */ |
| 71 | + |
| 72 | + bt_id_get(&addr, &count); |
| 73 | + bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s)); |
| 74 | + |
| 75 | + LOG_INF("Beacon started, advertising as %s", addr_s); |
| 76 | +} |
| 77 | + |
| 78 | +/* BT thread */ |
| 79 | +static void bt_thread(void *arg1, void *arg2, void *arg3) |
| 80 | +{ |
| 81 | + ARG_UNUSED(arg1); |
| 82 | + ARG_UNUSED(arg2); |
| 83 | + ARG_UNUSED(arg3); |
| 84 | + |
| 85 | + LOG_INF("Starting BT beacon"); |
| 86 | + |
| 87 | + /* Initialize the Bluetooth Subsystem */ |
| 88 | + if (bt_enable(bt_ready)) { |
| 89 | + LOG_ERR("Bluetooth init failed"); |
| 90 | + } else { |
| 91 | + LOG_INF("Bluetooth init OK"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +K_THREAD_DEFINE(thread_bt_id, BT_THREAD_STACKSIZE, bt_thread, NULL, NULL, NULL, |
| 96 | + K_PRIO_PREEMPT(BT_THREAD_PRIORITY), 0, 0); |
0 commit comments