2016-03-16 21:46:46 +08:00
|
|
|
/** @file
|
|
|
|
* @brief HRS Service sample
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-03-16 21:46:46 +08:00
|
|
|
*/
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-03-16 21:46:46 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <zephyr.h>
|
2019-07-04 22:44:29 +08:00
|
|
|
#include <init.h>
|
2016-03-16 21:46:46 +08:00
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/conn.h>
|
|
|
|
#include <bluetooth/uuid.h>
|
|
|
|
#include <bluetooth/gatt.h>
|
|
|
|
|
2020-09-04 16:20:51 +08:00
|
|
|
#define LOG_LEVEL CONFIG_BT_HRS_LOG_LEVEL
|
2019-07-04 22:44:29 +08:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(hrs);
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static uint8_t hrs_blsc;
|
2016-03-16 21:46:46 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static void hrmc_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
2016-03-16 21:46:46 +08:00
|
|
|
{
|
2019-07-04 22:44:29 +08:00
|
|
|
ARG_UNUSED(attr);
|
|
|
|
|
|
|
|
bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
|
|
|
|
|
|
|
|
LOG_INF("HRS notifications %s", notif_enabled ? "enabled" : "disabled");
|
2016-03-16 21:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t read_blsc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2020-05-28 00:26:57 +08:00
|
|
|
void *buf, uint16_t len, uint16_t offset)
|
2016-03-16 21:46:46 +08:00
|
|
|
{
|
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &hrs_blsc,
|
|
|
|
sizeof(hrs_blsc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Heart Rate Service Declaration */
|
2019-04-16 17:41:06 +08:00
|
|
|
BT_GATT_SERVICE_DEFINE(hrs_svc,
|
2016-03-16 21:46:46 +08:00
|
|
|
BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS),
|
2018-05-12 02:53:02 +08:00
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_NONE, NULL, NULL, NULL),
|
2019-09-04 19:15:07 +08:00
|
|
|
BT_GATT_CCC(hrmc_ccc_cfg_changed,
|
|
|
|
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
2018-05-12 02:53:02 +08:00
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_BODY_SENSOR, BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ, read_blsc, NULL, NULL),
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_CONTROL_POINT, BT_GATT_CHRC_WRITE,
|
|
|
|
BT_GATT_PERM_NONE, NULL, NULL, NULL),
|
2019-04-16 17:41:06 +08:00
|
|
|
);
|
2017-06-12 05:26:19 +08:00
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int hrs_init(const struct device *dev)
|
2016-03-16 21:46:46 +08:00
|
|
|
{
|
2019-07-04 22:44:29 +08:00
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
hrs_blsc = 0x01;
|
|
|
|
|
|
|
|
return 0;
|
2016-03-16 21:46:46 +08:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:20:51 +08:00
|
|
|
int bt_hrs_notify(uint16_t heartrate)
|
2016-03-16 21:46:46 +08:00
|
|
|
{
|
2019-07-04 22:44:29 +08:00
|
|
|
int rc;
|
2020-05-28 00:26:57 +08:00
|
|
|
static uint8_t hrm[2];
|
2016-03-16 21:46:46 +08:00
|
|
|
|
|
|
|
hrm[0] = 0x06; /* uint8, sensor contact */
|
|
|
|
hrm[1] = heartrate;
|
|
|
|
|
2019-07-04 22:44:29 +08:00
|
|
|
rc = bt_gatt_notify(NULL, &hrs_svc.attrs[1], &hrm, sizeof(hrm));
|
|
|
|
|
|
|
|
return rc == -ENOTCONN ? 0 : rc;
|
2016-03-16 21:46:46 +08:00
|
|
|
}
|
2019-07-04 22:44:29 +08:00
|
|
|
|
|
|
|
SYS_INIT(hrs_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|