2016-03-16 23:44:47 +08:00
|
|
|
/** @file
|
|
|
|
* @brief CTS Service sample
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-03-16 23:44:47 +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 23:44:47 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <misc/printk.h>
|
|
|
|
#include <misc/byteorder.h>
|
|
|
|
#include <zephyr.h>
|
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/conn.h>
|
|
|
|
#include <bluetooth/uuid.h>
|
|
|
|
#include <bluetooth/gatt.h>
|
|
|
|
|
2017-07-04 21:46:13 +08:00
|
|
|
static struct bt_gatt_ccc_cfg ct_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
2017-04-21 22:47:21 +08:00
|
|
|
static u8_t ct[10];
|
|
|
|
static u8_t ct_update;
|
2016-03-16 23:44:47 +08:00
|
|
|
|
2017-04-21 22:47:21 +08:00
|
|
|
static void ct_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t value)
|
2016-03-16 23:44:47 +08:00
|
|
|
{
|
|
|
|
/* TODO: Handle value */
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t read_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2017-04-21 22:47:21 +08:00
|
|
|
void *buf, u16_t len, u16_t offset)
|
2016-03-16 23:44:47 +08:00
|
|
|
{
|
|
|
|
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,
|
2017-04-21 22:47:21 +08:00
|
|
|
const void *buf, u16_t len, u16_t offset,
|
|
|
|
u8_t flags)
|
2016-03-16 23:44:47 +08:00
|
|
|
{
|
2017-04-21 22:47:21 +08:00
|
|
|
u8_t *value = attr->user_data;
|
2016-03-16 23:44:47 +08:00
|
|
|
|
|
|
|
if (offset + len > sizeof(ct)) {
|
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(value + offset, buf, len);
|
|
|
|
ct_update = 1;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Current Time Service Declaration */
|
|
|
|
static struct bt_gatt_attr attrs[] = {
|
|
|
|
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ |
|
2018-05-12 02:53:02 +08:00
|
|
|
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE,
|
|
|
|
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
|
|
|
|
read_ct, write_ct, ct),
|
2016-03-16 23:44:47 +08:00
|
|
|
BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed),
|
|
|
|
};
|
|
|
|
|
2017-06-12 05:26:19 +08:00
|
|
|
static struct bt_gatt_service cts_svc = BT_GATT_SERVICE(attrs);
|
|
|
|
|
2017-04-21 22:47:21 +08:00
|
|
|
static void generate_current_time(u8_t *buf)
|
2016-03-16 23:44:47 +08:00
|
|
|
{
|
2017-04-21 22:47:21 +08:00
|
|
|
u16_t year;
|
2016-03-16 23:44:47 +08:00
|
|
|
|
|
|
|
/* '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] = 5; /* months starting from 1 */
|
|
|
|
buf[3] = 30; /* day */
|
|
|
|
buf[4] = 12; /* hours */
|
|
|
|
buf[5] = 45; /* minutes */
|
|
|
|
buf[6] = 30; /* seconds */
|
|
|
|
|
|
|
|
/* 'Day of Week' part of 'Day Date Time' */
|
|
|
|
buf[7] = 1; /* day of week starting from 1 */
|
|
|
|
|
|
|
|
/* 'Fractions 256 part of 'Exact Time 256' */
|
|
|
|
buf[8] = 0;
|
|
|
|
|
|
|
|
/* Adjust reason */
|
|
|
|
buf[9] = 0; /* No update, change, etc */
|
|
|
|
}
|
|
|
|
|
|
|
|
void cts_init(void)
|
|
|
|
{
|
|
|
|
/* Simulate current time for Current Time Service */
|
|
|
|
generate_current_time(ct);
|
|
|
|
|
2017-06-12 05:26:19 +08:00
|
|
|
bt_gatt_service_register(&cts_svc);
|
2016-03-16 23:44:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cts_notify(void)
|
|
|
|
{ /* Current Time Service updates only when time is changed */
|
|
|
|
if (!ct_update) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ct_update = 0;
|
|
|
|
bt_gatt_notify(NULL, &attrs[3], &ct, sizeof(ct));
|
|
|
|
}
|