-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gatt-srv.c
167 lines (142 loc) · 4.91 KB
/
gatt-srv.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2020 Dyne.org foundation
*
* This file is subject to the terms and conditions of the Affero GNU
* General Public License (AGPL) version 3. See the file LICENSE for
* more details.
*
*/
#include <stdio.h>
#include <stdint.h>
#include "assert.h"
#include "event/timeout.h"
#include "nimble_riot.h"
#include "net/bluetil/ad.h"
#include "host/ble_hs.h"
#include "host/ble_gatt.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#include "dp3t.h"
#include "random.h"
#include "keystore.h"
#define HRS_FLAGS_DEFAULT (0x01) /* 16-bit BPM value */
#define SENSOR_LOCATION (0x02) /* wrist sensor */
#define UPDATE_INTERVAL (250U * US_PER_MS)
static const char *device_name = "DP3T";
static const char *_manufacturer_name = "Dyne.org";
static const char *_model_number = "1";
static event_queue_t _eq;
static event_t _update_evt;
static uint16_t _conn_handle;
static uint16_t _hrs_val_handle;
static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
static void start_advertising(void);
/* GATT service definitions */
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{
/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_GATT_SVC_DEVINFO),
.characteristics = (struct ble_gatt_chr_def[]) { {
.uuid = BLE_UUID16_DECLARE(BLE_GATT_CHAR_MANUFACTURER_NAME),
.access_cb = _devinfo_handler,
.flags = BLE_GATT_CHR_F_READ,
}, {
.uuid = BLE_UUID16_DECLARE(BLE_GATT_CHAR_MODEL_NUMBER_STR),
.access_cb = _devinfo_handler,
.flags = BLE_GATT_CHR_F_READ,
}, {
.uuid = BLE_UUID16_DECLARE(BLE_GATT_CHAR_SERIAL_NUMBER_STR),
.access_cb = _devinfo_handler,
.flags = BLE_GATT_CHR_F_READ,
}, {
.uuid = BLE_UUID16_DECLARE(BLE_GATT_CHAR_FW_REV_STR),
.access_cb = _devinfo_handler,
.flags = BLE_GATT_CHR_F_READ,
}, {
.uuid = BLE_UUID16_DECLARE(BLE_GATT_CHAR_HW_REV_STR),
.access_cb = _devinfo_handler,
.flags = BLE_GATT_CHR_F_READ,
}, {
0, /* no more characteristics in this service */
}, }
},
{
0, /* no more services */
},
};
static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg)
{
(void)conn_handle;
(void)attr_handle;
(void)arg;
const char *str;
switch (ble_uuid_u16(ctxt->chr->uuid)) {
case BLE_GATT_CHAR_MANUFACTURER_NAME:
//printf("[READ] device information service: manufacturer name value");
str = _manufacturer_name;
break;
case BLE_GATT_CHAR_MODEL_NUMBER_STR:
//printf("[READ] device information service: model number value");
str = _model_number;
break;
default:
return BLE_ATT_ERR_UNLIKELY;
}
int res = os_mbuf_append(ctxt->om, str, strlen(str));
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
static int gap_event_cb(struct ble_gap_event *event, void *arg)
{
(void)arg;
/* Used only if we want to export other GATT services */
return 0;
}
static void start_advertising(void)
{
struct ble_gap_adv_params advp;
int res;
memset(&advp, 0, sizeof advp);
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
advp.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
advp.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MAX;
res = ble_gap_adv_start(nimble_riot_own_addr_type, NULL, BLE_HS_FOREVER,
&advp, gap_event_cb, NULL);
assert(res == 0);
(void)res;
}
int gatt_server(void)
{
int res = 0;
uint8_t buf[31];
uint16_t hdr_txpow = BLE_GAP_AD_TX_POWER_LEVEL;
uint16_t hdr_uuid = BLE_GAP_AD_UUID128_COMP;
uint8_t tx_pow = 0x00;
uint8_t addr[6];
//printf("BLE DP3T service started");
/* verify and add our custom services */
res = ble_gatts_count_cfg(gatt_svr_svcs);
assert(res == 0);
res = ble_gatts_add_svcs(gatt_svr_svcs);
assert(res == 0);
/* set the device name */
ble_svc_gap_device_name_set("");
/* reload the GATT server to link our added services */
ble_gatts_start();
sys_random(addr, 6);
ble_hs_id_set_rnd(addr);
/* configure and set the advertising data */
bluetil_ad_t ad;
bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLE_GAP_DISCOVERABLE);
bluetil_ad_add(&ad, hdr_txpow, &tx_pow, 1);
bluetil_ad_add(&ad, hdr_uuid, dp3t_get_ephid(0), EPHID_LEN);
ble_gap_adv_set_data(ad.buf, ad.pos);
/* start to advertise this node */
start_advertising();
/* run an event loop for handling the heart rate update events */
//event_loop(&_eq);
return 0;
}