-
Notifications
You must be signed in to change notification settings - Fork 4
/
remote.c
121 lines (92 loc) · 3.38 KB
/
remote.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "remote.h"
#define LOG_MODULE_NAME remote
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
static K_SEM_DEFINE(bt_init_ok, 0, 1);
static uint8_t button_value = 0;
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME)-1)
static struct bt_remote_service_cb remote_service_callbacks;
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN)
};
static const struct bt_data sd[] = {
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_REMOTE_SERV_VAL),
};
/* Declarations */
static ssize_t read_button_characteristic_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset);
void button_chrc_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value);
BT_GATT_SERVICE_DEFINE(remote_srv,
BT_GATT_PRIMARY_SERVICE(BT_UUID_REMOTE_SERVICE),
BT_GATT_CHARACTERISTIC(BT_UUID_REMOTE_BUTTON_CHRC,
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ,
read_button_characteristic_cb, NULL, NULL),
BT_GATT_CCC(button_chrc_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);
/* Callbacks */
void bt_ready(int err)
{
if (err) {
LOG_ERR("bt_ready returned %d", err);
}
k_sem_give(&bt_init_ok);
}
static ssize_t read_button_characteristic_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
return bt_gatt_attr_read(conn, attr, buf, len, offset, &button_value,
sizeof(button_value));
}
void button_chrc_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
LOG_INF("Notifications %s", notif_enabled? "enabled":"disabled");
if (remote_service_callbacks.notif_changed) {
remote_service_callbacks.notif_changed(notif_enabled?BT_BUTTON_NOTIFICATIONS_ENABLED:BT_BUTTON_NOTIFICATIONS_DISABLED);
}
}
void on_sent(struct bt_conn *conn, void *user_data)
{
ARG_UNUSED(user_data);
LOG_INF("Notification sent on connection %p", (void *)conn);
}
/* Remote controller functions */
int send_button_notification(struct bt_conn *conn, uint8_t value)
{
int err = 0;
struct bt_gatt_notify_params params = {0};
const struct bt_gatt_attr *attr = &remote_srv.attrs[2];
params.attr = attr;
params.data = &value;
params.len = 1;
params.func = on_sent;
err = bt_gatt_notify_cb(conn, ¶ms);
return err;
}
void set_button_value(uint8_t btn_value)
{
button_value = btn_value;
}
int bluetooth_init(struct bt_conn_cb *bt_cb, struct bt_remote_service_cb *remote_cb)
{
int err;
LOG_INF("Initializing Bluetooth");
if (bt_cb == NULL || remote_cb == NULL) {
return NRFX_ERROR_NULL;
}
bt_conn_cb_register(bt_cb);
remote_service_callbacks.notif_changed = remote_cb->notif_changed;
err = bt_enable(bt_ready);
if (err) {
LOG_ERR("bt_enable returned %d", err);
return err;
}
k_sem_take(&bt_init_ok, K_FOREVER);
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err){
LOG_ERR("couldn't start advertising (err = %d", err);
return err;
}
return err;
}