2021-08-18 16:56:31 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
* Copyright (c) 2021 Lingao Meng
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/sys/byteorder.h>
|
2021-08-18 16:56:31 +08:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/net/buf.h>
|
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
|
|
#include <zephyr/bluetooth/conn.h>
|
|
|
|
#include <zephyr/bluetooth/gatt.h>
|
|
|
|
#include <zephyr/bluetooth/mesh.h>
|
2021-08-18 16:56:31 +08:00
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
#include "mesh.h"
|
|
|
|
#include "adv.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "rpl.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "host/ecc.h"
|
|
|
|
#include "prov.h"
|
2022-02-08 16:58:26 +08:00
|
|
|
#include "pb_gatt.h"
|
2021-08-18 16:56:31 +08:00
|
|
|
#include "beacon.h"
|
|
|
|
#include "foundation.h"
|
|
|
|
#include "access.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
#include "proxy_msg.h"
|
2021-08-19 09:25:25 +08:00
|
|
|
#include "pb_gatt_srv.h"
|
2021-08-18 16:56:31 +08:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#define LOG_LEVEL CONFIG_BT_MESH_PROV_LOG_LEVEL
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(bt_mesh_pb_gatt_srv);
|
|
|
|
|
2021-09-04 09:00:53 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_PB_GATT_USE_DEVICE_NAME)
|
|
|
|
#define ADV_OPT_USE_NAME BT_LE_ADV_OPT_USE_NAME
|
|
|
|
#else
|
|
|
|
#define ADV_OPT_USE_NAME 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ADV_OPT_PROV \
|
|
|
|
(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_SCANNABLE | \
|
|
|
|
BT_LE_ADV_OPT_ONE_TIME | ADV_OPT_USE_IDENTITY | \
|
|
|
|
ADV_OPT_USE_NAME)
|
|
|
|
|
2022-06-22 10:44:17 +02:00
|
|
|
#define FAST_ADV_TIME (60LL * MSEC_PER_SEC)
|
|
|
|
|
|
|
|
static int64_t fast_adv_timestamp;
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
static int gatt_send(struct bt_conn *conn,
|
|
|
|
const void *data, uint16_t len,
|
|
|
|
bt_gatt_complete_func_t end, void *user_data);
|
|
|
|
|
2021-09-02 16:18:48 +08:00
|
|
|
static struct bt_mesh_proxy_role *cli;
|
2021-08-18 16:56:31 +08:00
|
|
|
static bool service_registered;
|
|
|
|
|
2021-09-02 16:18:48 +08:00
|
|
|
static void proxy_msg_recv(struct bt_mesh_proxy_role *role)
|
2021-09-02 10:42:57 +08:00
|
|
|
{
|
|
|
|
switch (role->msg_type) {
|
|
|
|
case BT_MESH_PROXY_PROV:
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Mesh Provisioning PDU");
|
2021-09-02 10:42:57 +08:00
|
|
|
bt_mesh_pb_gatt_recv(role->conn, &role->buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Unhandled Message Type 0x%02x", role->msg_type);
|
2021-09-02 10:42:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 16:56:31 +08:00
|
|
|
static ssize_t gatt_recv(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, const void *buf,
|
|
|
|
uint16_t len, uint16_t offset, uint8_t flags)
|
|
|
|
{
|
|
|
|
const uint8_t *data = buf;
|
|
|
|
|
2021-09-02 16:18:48 +08:00
|
|
|
if (cli->conn != conn) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("No PB-GATT Client found");
|
2021-08-18 16:56:31 +08:00
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 1) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Too small Proxy PDU");
|
2021-08-18 16:56:31 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDU_TYPE(data) != BT_MESH_PROXY_PROV) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Proxy PDU type doesn't match GATT service");
|
2021-08-18 16:56:31 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:32:19 +08:00
|
|
|
return bt_mesh_proxy_msg_recv(conn, buf, len);
|
2021-08-18 16:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gatt_connected(struct bt_conn *conn, uint8_t err)
|
|
|
|
{
|
|
|
|
struct bt_conn_info info;
|
|
|
|
|
|
|
|
bt_conn_get_info(conn, &info);
|
2022-11-16 10:45:43 +01:00
|
|
|
if (info.role != BT_CONN_ROLE_PERIPHERAL || !service_registered ||
|
2022-11-23 14:54:51 +01:00
|
|
|
bt_mesh_is_provisioned() || info.id != BT_ID_DEFAULT || cli) {
|
2021-08-18 16:56:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:18:48 +08:00
|
|
|
cli = bt_mesh_proxy_role_setup(conn, gatt_send, proxy_msg_recv);
|
2021-08-18 16:56:31 +08:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p err 0x%02x", (void *)conn, err);
|
2021-08-18 16:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
|
|
|
|
{
|
|
|
|
struct bt_conn_info info;
|
|
|
|
|
|
|
|
bt_conn_get_info(conn, &info);
|
2022-11-16 10:45:43 +01:00
|
|
|
if (info.role != BT_CONN_ROLE_PERIPHERAL || !service_registered ||
|
2022-11-23 14:54:51 +01:00
|
|
|
info.id != BT_ID_DEFAULT || !cli || cli->conn != conn) {
|
2021-08-18 16:56:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:54:51 +01:00
|
|
|
bt_mesh_proxy_role_cleanup(cli);
|
|
|
|
cli = NULL;
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
bt_mesh_pb_gatt_close(conn);
|
|
|
|
|
2021-09-03 13:59:37 +08:00
|
|
|
if (bt_mesh_is_provisioned()) {
|
2022-02-08 16:44:31 +08:00
|
|
|
(void)bt_mesh_pb_gatt_srv_disable();
|
2021-09-02 16:18:48 +08:00
|
|
|
}
|
2021-08-18 16:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void prov_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("value 0x%04x", value);
|
2021-08-18 16:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t prov_ccc_write(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2021-09-02 16:18:48 +08:00
|
|
|
if (cli->conn != conn) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("No PB-GATT Client found");
|
2021-08-18 16:56:31 +08:00
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("value 0x%04x", value);
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
if (value != BT_GATT_CCC_NOTIFY) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Client wrote 0x%04x instead enabling notify", value);
|
2021-08-18 16:56:31 +08:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:25:09 +08:00
|
|
|
bt_mesh_pb_gatt_start(conn);
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
return sizeof(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mesh Provisioning Service Declaration */
|
|
|
|
static struct _bt_gatt_ccc prov_ccc =
|
|
|
|
BT_GATT_CCC_INITIALIZER(prov_ccc_changed, prov_ccc_write, NULL);
|
|
|
|
|
|
|
|
static struct bt_gatt_attr prov_attrs[] = {
|
|
|
|
BT_GATT_PRIMARY_SERVICE(BT_UUID_MESH_PROV),
|
|
|
|
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROV_DATA_IN,
|
|
|
|
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
|
|
|
BT_GATT_PERM_WRITE, NULL, gatt_recv,
|
|
|
|
NULL),
|
|
|
|
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROV_DATA_OUT,
|
|
|
|
BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE,
|
|
|
|
NULL, NULL, NULL),
|
|
|
|
BT_GATT_CCC_MANAGED(&prov_ccc,
|
|
|
|
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct bt_gatt_service prov_svc = BT_GATT_SERVICE(prov_attrs);
|
|
|
|
|
2022-02-08 16:44:31 +08:00
|
|
|
int bt_mesh_pb_gatt_srv_enable(void)
|
2021-08-18 16:56:31 +08:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
if (bt_mesh_is_provisioned()) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (service_registered) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)bt_gatt_service_register(&prov_svc);
|
|
|
|
service_registered = true;
|
2022-06-22 10:44:17 +02:00
|
|
|
fast_adv_timestamp = k_uptime_get();
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:44:31 +08:00
|
|
|
int bt_mesh_pb_gatt_srv_disable(void)
|
2021-08-18 16:56:31 +08:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
if (!service_registered) {
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_gatt_service_unregister(&prov_svc);
|
|
|
|
service_registered = false;
|
|
|
|
|
2021-11-18 09:23:08 +08:00
|
|
|
bt_mesh_adv_gatt_update();
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t prov_svc_data[20] = {
|
|
|
|
BT_UUID_16_ENCODE(BT_UUID_MESH_PROV_VAL),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct bt_data prov_ad[] = {
|
|
|
|
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
|
|
|
BT_DATA_BYTES(BT_DATA_UUID16_ALL,
|
|
|
|
BT_UUID_16_ENCODE(BT_UUID_MESH_PROV_VAL)),
|
|
|
|
BT_DATA(BT_DATA_SVC_DATA16, prov_svc_data, sizeof(prov_svc_data)),
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t gatt_prov_adv_create(struct bt_data prov_sd[1])
|
|
|
|
{
|
|
|
|
const struct bt_mesh_prov *prov = bt_mesh_prov_get();
|
|
|
|
size_t uri_len;
|
|
|
|
|
|
|
|
memcpy(prov_svc_data + 2, prov->uuid, 16);
|
|
|
|
sys_put_be16(prov->oob_info, prov_svc_data + 18);
|
|
|
|
|
|
|
|
if (!prov->uri) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri_len = strlen(prov->uri);
|
|
|
|
if (uri_len > 29) {
|
|
|
|
/* There's no way to shorten an URI */
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Too long URI to fit advertising packet");
|
2021-08-18 16:56:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
prov_sd[0].type = BT_DATA_URI;
|
|
|
|
prov_sd[0].data_len = uri_len;
|
|
|
|
prov_sd[0].data = (const uint8_t *)prov->uri;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gatt_send(struct bt_conn *conn,
|
|
|
|
const void *data, uint16_t len,
|
|
|
|
bt_gatt_complete_func_t end, void *user_data)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("%u bytes: %s", len, bt_hex(data, len));
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
struct bt_gatt_notify_params params = {
|
|
|
|
.data = data,
|
|
|
|
.len = len,
|
|
|
|
.attr = &prov_attrs[3],
|
|
|
|
.user_data = user_data,
|
|
|
|
.func = end,
|
|
|
|
};
|
|
|
|
|
|
|
|
return bt_gatt_notify_cb(conn, ¶ms);
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:44:31 +08:00
|
|
|
int bt_mesh_pb_gatt_srv_adv_start(void)
|
2021-08-18 16:56:31 +08:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("");
|
2021-08-18 16:56:31 +08:00
|
|
|
|
2022-11-04 16:10:14 +01:00
|
|
|
if (!service_registered || bt_mesh_is_provisioned() ||
|
2022-11-23 13:46:55 +01:00
|
|
|
!bt_mesh_proxy_has_avail_conn()) {
|
2021-08-18 16:56:31 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bt_le_adv_param fast_adv_param = {
|
|
|
|
.options = ADV_OPT_PROV,
|
|
|
|
ADV_FAST_INT,
|
|
|
|
};
|
|
|
|
struct bt_data prov_sd[1];
|
|
|
|
size_t prov_sd_len;
|
2022-06-22 10:44:17 +02:00
|
|
|
int64_t timestamp = fast_adv_timestamp;
|
|
|
|
int64_t elapsed_time = k_uptime_delta(×tamp);
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
prov_sd_len = gatt_prov_adv_create(prov_sd);
|
|
|
|
|
2022-06-22 10:44:17 +02:00
|
|
|
if (elapsed_time > FAST_ADV_TIME) {
|
2021-08-18 16:56:31 +08:00
|
|
|
struct bt_le_adv_param slow_adv_param = {
|
|
|
|
.options = ADV_OPT_PROV,
|
|
|
|
ADV_SLOW_INT,
|
|
|
|
};
|
|
|
|
|
2021-11-18 09:23:08 +08:00
|
|
|
return bt_mesh_adv_gatt_start(&slow_adv_param, SYS_FOREVER_MS, prov_ad,
|
|
|
|
ARRAY_SIZE(prov_ad), prov_sd, prov_sd_len);
|
2021-08-18 16:56:31 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("remaining fast adv time (%lld ms)", (FAST_ADV_TIME - elapsed_time));
|
2021-08-18 16:56:31 +08:00
|
|
|
/* Advertise 60 seconds using fast interval */
|
2022-06-22 10:44:17 +02:00
|
|
|
return bt_mesh_adv_gatt_start(&fast_adv_param, (FAST_ADV_TIME - elapsed_time),
|
|
|
|
prov_ad, ARRAY_SIZE(prov_ad),
|
|
|
|
prov_sd, prov_sd_len);
|
2021-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-03 13:59:37 +08:00
|
|
|
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
2021-08-18 16:56:31 +08:00
|
|
|
.connected = gatt_connected,
|
|
|
|
.disconnected = gatt_disconnected,
|
|
|
|
};
|