zephyr/samples/bluetooth/peripheral/src/cts.c
Gerard Marull-Paretas 79e6b0e0f6 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-09-05 16:31:47 +02:00

110 lines
2.6 KiB
C

/** @file
* @brief CTS Service sample
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/types.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
static uint8_t ct[10];
static uint8_t ct_update;
static void ct_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
/* TODO: Handle value */
}
static ssize_t read_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
const char *value = attr->user_data;
return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
sizeof(ct));
}
static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags)
{
uint8_t *value = attr->user_data;
if (offset + len > sizeof(ct)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
memcpy(value + offset, buf, len);
ct_update = 1U;
return len;
}
/* Current Time Service Declaration */
BT_GATT_SERVICE_DEFINE(cts_cvs,
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ |
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_ct, write_ct, ct),
BT_GATT_CCC(ct_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);
static void generate_current_time(uint8_t *buf)
{
uint16_t year;
/* 'Exact Time 256' contains 'Day Date Time' which contains
* 'Date Time' - characteristic contains fields for:
* year, month, day, hours, minutes and seconds.
*/
year = sys_cpu_to_le16(2015);
memcpy(buf, &year, 2); /* year */
buf[2] = 5U; /* months starting from 1 */
buf[3] = 30U; /* day */
buf[4] = 12U; /* hours */
buf[5] = 45U; /* minutes */
buf[6] = 30U; /* seconds */
/* 'Day of Week' part of 'Day Date Time' */
buf[7] = 1U; /* day of week starting from 1 */
/* 'Fractions 256 part of 'Exact Time 256' */
buf[8] = 0U;
/* Adjust reason */
buf[9] = 0U; /* No update, change, etc */
}
void cts_init(void)
{
/* Simulate current time for Current Time Service */
generate_current_time(ct);
}
void cts_notify(void)
{ /* Current Time Service updates only when time is changed */
if (!ct_update) {
return;
}
ct_update = 0U;
bt_gatt_notify(NULL, &cts_cvs.attrs[1], &ct, sizeof(ct));
}