-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_peripheral_tp_server.c
303 lines (253 loc) · 8.29 KB
/
ble_peripheral_tp_server.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/****************************************************************************
FILE NAME
ble_peripheral_tp_server.c
DESCRIPTION
test profile demo
NOTES
*/
/****************************************************************************/
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <FreeRTOS.h>
#include <task.h>
#include "bluetooth.h"
#include "conn.h"
#include "gatt.h"
#include "hci_core.h"
#include "uuid.h"
#include "ble_peripheral_tp_server.h"
#include "log.h"
static void ble_tp_connected(struct bt_conn *conn, u8_t err);
static void ble_tp_disconnected(struct bt_conn *conn, u8_t reason);
static struct bt_conn *ble_tp_conn;
static struct bt_gatt_exchange_params exchg_mtu;
static TaskHandle_t ble_tp_task_h;
static struct k_sem notify_poll_sem;
static int tx_mtu_size = 20;
static u8_t created_tp_task = 0;
static u8_t isRegister = 0;
static struct bt_conn_cb ble_tp_conn_callbacks = {
.connected = ble_tp_connected,
.disconnected = ble_tp_disconnected,
};
/*************************************************************************
NAME
ble_tp_tx_mtu_size
*/
static void ble_tp_tx_mtu_size(struct bt_conn *conn, u8_t err,
struct bt_gatt_exchange_params *params)
{
if (!err) {
tx_mtu_size = bt_gatt_get_mtu(ble_tp_conn);
BT_WARN("ble tp echange mtu size success, mtu size: %d", tx_mtu_size);
} else {
BT_WARN("ble tp echange mtu size failure, err: %d", err);
}
}
/*************************************************************************
NAME
ble_tp_connected
*/
static void ble_tp_connected(struct bt_conn *conn, u8_t err)
{
int tx_octets = 0x00fb;
int tx_time = 0x0848;
int ret = -1;
if (err) {
return;
}
BT_WARN("Tp connected");
ble_tp_conn = conn;
//set data length after connected.
ret = bt_le_set_data_len(ble_tp_conn, tx_octets, tx_time);
if (!ret) {
BT_WARN("ble tp set data length success");
} else {
BT_WARN("ble tp set data length failure, err: %d", ret);
}
//exchange mtu size after connected.
exchg_mtu.func = ble_tp_tx_mtu_size;
ret = bt_gatt_exchange_mtu(ble_tp_conn, &exchg_mtu);
if (!ret) {
BT_WARN("ble tp exchange mtu size pending");
} else {
BT_WARN("ble tp exchange mtu size failure, err: %d", ret);
}
}
/*************************************************************************
NAME
ble_tp_disconnected
*/
static void ble_tp_disconnected(struct bt_conn *conn, u8_t reason)
{
BT_WARN("Tp disconnected");
if (created_tp_task) {
BT_WARN("Delete throughput tx task");
vTaskDelete(ble_tp_task_h);
created_tp_task = 0;
}
ble_tp_conn = NULL;
}
/*************************************************************************
NAME
ble_tp_recv_rd
*/
static int ble_tp_recv_rd(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
int size = 9;
char data[9] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
memcpy(buf, data, size);
return size;
}
/*************************************************************************
NAME
ble_tp_recv_wr(receive data from client)
*/
static int ble_tp_recv_wr(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, u16_t len, u16_t offset, u8_t flags)
{
BT_WARN("recv data len=%d, offset=%d, flag=%d", len, offset, flags);
BT_WARN("recv data:%s", bt_hex(buf, len));
if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
//Don't use prepare write data, execute write will upload data again.
BT_WARN("recv prepare write request");
return 0;
}
if (flags & BT_GATT_WRITE_FLAG_CMD) {
//Use write command data.
BT_WARN("recv write command");
} else {
//Use write request / execute write data.
BT_WARN("recv write request / exce write");
}
k_sem_give(¬ify_poll_sem);
return len;
}
/*************************************************************************
NAME
indicate_rsp /bl_tp_send_indicate
*/
static void indicate_rsp(struct bt_conn *conn, const struct bt_gatt_attr *attr, u8_t err)
{
BT_WARN("receive comfirmation, err:%d", err);
}
static int bl_tp_send_indicate(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *data, u16_t len)
{
static struct bt_gatt_indicate_params ind_params;
ind_params.attr = attr;
ind_params.data = data;
ind_params.len = len;
ind_params.func = indicate_rsp;
ind_params.uuid = NULL;
return bt_gatt_indicate(conn, &ind_params);
}
/*************************************************************************
NAME
ble_tp_ind_ccc_changed
*/
static void ble_tp_ind_ccc_changed(const struct bt_gatt_attr *attr, u16_t value)
{
int err = -1;
char data[9] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
if (value == BT_GATT_CCC_INDICATE) {
err = bl_tp_send_indicate(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_IND_ATTR_VAL_INDEX), data, 9);
BT_WARN("ble tp send indatcate: %d", err);
}
}
/*************************************************************************
NAME
ble_tp_notify(send data to client)
*/
static void ble_tp_notify_task(void *pvParameters)
{
int err = -1;
u8_t data[244] = { 0x01 };
k_sem_give(¬ify_poll_sem);
while (1) {
k_sem_take(¬ify_poll_sem, K_FOREVER);
//send data to client
err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), data, (tx_mtu_size - 3));
data[0] = data[0] + 1;
BT_WARN("ble tp send notify : %d", err);
}
}
/*************************************************************************
NAME
ble_tp_not_ccc_changed
*/
static void ble_tp_notify_ccc_changed(const struct bt_gatt_attr *attr, u16_t value)
{
BT_WARN("ccc:value=[%d]", value);
if (value == BT_GATT_CCC_NOTIFY) {
if (xTaskCreate(ble_tp_notify_task, (char *)"bletp", 512, NULL, 15, &ble_tp_task_h) == pdPASS) {
created_tp_task = 1;
BT_WARN("Create throughput tx task success");
} else {
created_tp_task = 0;
BT_WARN("Create throughput tx taskfail");
}
} else {
if (created_tp_task) {
BT_WARN("Delete throughput tx task");
vTaskDelete(ble_tp_task_h);
created_tp_task = 0;
}
}
}
/*************************************************************************
* DEFINE : attrs
*/
static struct bt_gatt_attr attrs[] = {
BT_GATT_PRIMARY_SERVICE(BT_UUID_SVC_BLE_TP),
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_RD,
BT_GATT_CHRC_READ,
BT_GATT_PERM_READ,
ble_tp_recv_rd,
NULL,
NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_WR,
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE | BT_GATT_PERM_PREPARE_WRITE,
NULL,
ble_tp_recv_wr,
NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_IND,
BT_GATT_CHRC_INDICATE,
0,
NULL,
NULL,
NULL),
BT_GATT_CCC(ble_tp_ind_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_NOT,
BT_GATT_CHRC_NOTIFY,
0,
NULL,
NULL,
NULL),
BT_GATT_CCC(ble_tp_notify_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE)
};
/*************************************************************************
NAME
get_attr
*/
struct bt_gatt_attr *get_attr(u8_t index)
{
return &attrs[index];
}
static struct bt_gatt_service ble_tp_server = BT_GATT_SERVICE(attrs);
/*************************************************************************
NAME
ble_tp_init
*/
void ble_tp_init()
{
if (!isRegister) {
isRegister = 1;
bt_conn_cb_register(&ble_tp_conn_callbacks);
bt_gatt_service_register(&ble_tp_server);
k_sem_init(¬ify_poll_sem, 0, 1);
}
}