1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <errno.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <zephyr/autoconf.h>
14 #include <zephyr/bluetooth/att.h>
15 #include <zephyr/bluetooth/audio/gmap.h>
16 #include <zephyr/bluetooth/conn.h>
17 #include <zephyr/bluetooth/gatt.h>
18 #include <zephyr/bluetooth/uuid.h>
19 #include <zephyr/logging/log.h>
20 #include <zephyr/net/buf.h>
21 #include <zephyr/sys/__assert.h>
22 #include <zephyr/sys/check.h>
23
24 #include "audio_internal.h"
25
26 LOG_MODULE_REGISTER(bt_gmap_client, CONFIG_BT_GMAP_LOG_LEVEL);
27
28 static const struct bt_uuid *gmas_uuid = BT_UUID_GMAS;
29 static const struct bt_uuid *gmap_role_uuid = BT_UUID_GMAP_ROLE;
30 static const struct bt_uuid *gmap_ugg_feat_uuid = BT_UUID_GMAP_UGG_FEAT;
31 static const struct bt_uuid *gmap_ugt_feat_uuid = BT_UUID_GMAP_UGT_FEAT;
32 static const struct bt_uuid *gmap_bgs_feat_uuid = BT_UUID_GMAP_BGS_FEAT;
33 static const struct bt_uuid *gmap_bgr_feat_uuid = BT_UUID_GMAP_BGR_FEAT;
34
35 static const struct bt_gmap_cb *gmap_cb;
36
37 static struct bt_gmap_client {
38 /** Profile connection reference */
39 struct bt_conn *conn;
40
41 /* Remote role and features */
42 enum bt_gmap_role role;
43 struct bt_gmap_feat feat;
44
45 uint16_t svc_start_handle;
46 uint16_t svc_end_handle;
47
48 bool busy;
49
50 /* GATT procedure parameters */
51 union {
52 struct bt_gatt_read_params read;
53 struct bt_gatt_discover_params discover;
54 } params;
55 } gmap_insts[CONFIG_BT_MAX_CONN];
56
gmap_reset(struct bt_gmap_client * gmap_cli)57 static void gmap_reset(struct bt_gmap_client *gmap_cli)
58 {
59 if (gmap_cli->conn != NULL) {
60 bt_conn_unref(gmap_cli->conn);
61 }
62
63 memset(gmap_cli, 0, sizeof(*gmap_cli));
64 }
65
client_by_conn(struct bt_conn * conn)66 static struct bt_gmap_client *client_by_conn(struct bt_conn *conn)
67 {
68 struct bt_gmap_client *gmap_cli = &gmap_insts[bt_conn_index(conn)];
69
70 if (gmap_cli->conn == conn) {
71 return gmap_cli;
72 }
73
74 return NULL;
75 }
76
disconnected(struct bt_conn * conn,uint8_t reason)77 static void disconnected(struct bt_conn *conn, uint8_t reason)
78 {
79 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
80
81 if (gmap_cli != NULL) {
82 bt_conn_unref(gmap_cli->conn);
83 gmap_cli->conn = NULL;
84 }
85 }
86
87 BT_CONN_CB_DEFINE(conn_callbacks) = {
88 .disconnected = disconnected,
89 };
90
discover_complete(struct bt_gmap_client * gmap_cli)91 static void discover_complete(struct bt_gmap_client *gmap_cli)
92 {
93 LOG_DBG("conn %p", (void *)gmap_cli->conn);
94
95 gmap_cli->busy = false;
96
97 if (gmap_cb->discover != NULL) {
98 gmap_cb->discover(gmap_cli->conn, 0, gmap_cli->role, gmap_cli->feat);
99 }
100 }
101
discover_failed(struct bt_gmap_client * gmap_cli,int err)102 static void discover_failed(struct bt_gmap_client *gmap_cli, int err)
103 {
104 struct bt_conn *conn = gmap_cli->conn;
105
106 gmap_reset(gmap_cli);
107
108 LOG_DBG("conn %p err %d", (void *)conn, err);
109
110 gmap_cb->discover(conn, err, 0, (struct bt_gmap_feat){0});
111 }
112
bgr_feat_read_cb(struct bt_conn * conn,uint8_t att_err,struct bt_gatt_read_params * params,const void * data,uint16_t len)113 static uint8_t bgr_feat_read_cb(struct bt_conn *conn, uint8_t att_err,
114 struct bt_gatt_read_params *params, const void *data, uint16_t len)
115 {
116 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
117 struct net_buf_simple buf;
118 int err = att_err;
119
120 __ASSERT(gmap_cli, "no instance for conn %p", (void *)conn);
121
122 LOG_DBG("conn %p att_err 0x%02x params %p data %p len %u", (void *)conn, att_err, params,
123 data, len);
124
125 if (data == NULL || att_err != BT_ATT_ERR_SUCCESS || len != sizeof(uint8_t)) {
126 if (att_err == BT_ATT_ERR_SUCCESS) {
127 att_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
128 }
129
130 discover_failed(gmap_cli, err);
131
132 return BT_GATT_ITER_STOP;
133 }
134
135 net_buf_simple_init_with_data(&buf, (void *)data, len);
136
137 gmap_cli->feat.bgr_feat = net_buf_simple_pull_u8(&buf);
138 LOG_DBG("bgr_feat 0x%02x", gmap_cli->feat.bgr_feat);
139
140 discover_complete(gmap_cli);
141
142 return BT_GATT_ITER_STOP;
143 }
144
gmap_read_bgr_feat(struct bt_gmap_client * gmap_cli,uint16_t handle)145 static int gmap_read_bgr_feat(struct bt_gmap_client *gmap_cli, uint16_t handle)
146 {
147 LOG_DBG("conn %p handle 0x%04x", (void *)gmap_cli->conn, handle);
148
149 memset(&gmap_cli->params.read, 0, sizeof(gmap_cli->params.read));
150
151 gmap_cli->params.read.func = bgr_feat_read_cb;
152 gmap_cli->params.read.handle_count = 1u;
153 gmap_cli->params.read.single.handle = handle;
154 gmap_cli->params.read.single.offset = 0u;
155
156 return bt_gatt_read(gmap_cli->conn, &gmap_cli->params.read);
157 }
158
bgr_feat_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)159 static uint8_t bgr_feat_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
160 struct bt_gatt_discover_params *params)
161 {
162 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
163 const struct bt_gatt_chrc *chrc;
164 int err;
165
166 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
167
168 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
169
170 if (attr == NULL) {
171 discover_failed(gmap_cli, -ENOENT);
172
173 return BT_GATT_ITER_STOP;
174 }
175
176 chrc = attr->user_data;
177
178 /* Read features */
179 err = gmap_read_bgr_feat(gmap_cli, chrc->value_handle);
180 if (err != 0) {
181 discover_failed(gmap_cli, err);
182 }
183
184 return BT_GATT_ITER_STOP;
185 }
186
gmap_discover_bgr_feat(struct bt_gmap_client * gmap_cli)187 static int gmap_discover_bgr_feat(struct bt_gmap_client *gmap_cli)
188 {
189 LOG_DBG("conn %p", (void *)gmap_cli->conn);
190
191 memset(&gmap_cli->params.discover, 0, sizeof(gmap_cli->params.discover));
192
193 gmap_cli->params.discover.func = bgr_feat_discover_func;
194 gmap_cli->params.discover.uuid = gmap_bgr_feat_uuid;
195 gmap_cli->params.discover.type = BT_GATT_DISCOVER_CHARACTERISTIC;
196 gmap_cli->params.discover.start_handle = gmap_cli->svc_start_handle;
197 gmap_cli->params.discover.end_handle = gmap_cli->svc_end_handle;
198
199 return bt_gatt_discover(gmap_cli->conn, &gmap_cli->params.discover);
200 }
201
bgs_feat_read_cb(struct bt_conn * conn,uint8_t att_err,struct bt_gatt_read_params * params,const void * data,uint16_t len)202 static uint8_t bgs_feat_read_cb(struct bt_conn *conn, uint8_t att_err,
203 struct bt_gatt_read_params *params, const void *data, uint16_t len)
204 {
205 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
206 struct net_buf_simple buf;
207 int err = att_err;
208
209 __ASSERT(gmap_cli, "no instance for conn %p", (void *)conn);
210
211 LOG_DBG("conn %p att_err 0x%02x params %p data %p len %u", (void *)conn, att_err, params,
212 data, len);
213
214 if (data == NULL || att_err != BT_ATT_ERR_SUCCESS || len != sizeof(uint8_t)) {
215 if (att_err == BT_ATT_ERR_SUCCESS) {
216 att_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
217 }
218
219 discover_failed(gmap_cli, err);
220
221 return BT_GATT_ITER_STOP;
222 }
223
224 net_buf_simple_init_with_data(&buf, (void *)data, len);
225
226 gmap_cli->feat.bgs_feat = net_buf_simple_pull_u8(&buf);
227 LOG_DBG("bgs_feat 0x%02x", gmap_cli->feat.bgs_feat);
228
229 if ((gmap_cli->role & BT_GMAP_ROLE_BGR) != 0) {
230 err = gmap_discover_bgr_feat(gmap_cli);
231 } else {
232 discover_complete(gmap_cli);
233
234 return BT_GATT_ITER_STOP;
235 }
236
237 if (err) {
238 discover_failed(gmap_cli, err);
239 }
240
241 return BT_GATT_ITER_STOP;
242 }
243
gmap_read_bgs_feat(struct bt_gmap_client * gmap_cli,uint16_t handle)244 static int gmap_read_bgs_feat(struct bt_gmap_client *gmap_cli, uint16_t handle)
245 {
246 LOG_DBG("conn %p handle 0x%04x", (void *)gmap_cli->conn, handle);
247
248 memset(&gmap_cli->params.read, 0, sizeof(gmap_cli->params.read));
249
250 gmap_cli->params.read.func = bgs_feat_read_cb;
251 gmap_cli->params.read.handle_count = 1u;
252 gmap_cli->params.read.single.handle = handle;
253 gmap_cli->params.read.single.offset = 0u;
254
255 return bt_gatt_read(gmap_cli->conn, &gmap_cli->params.read);
256 }
257
bgs_feat_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)258 static uint8_t bgs_feat_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
259 struct bt_gatt_discover_params *params)
260 {
261 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
262 const struct bt_gatt_chrc *chrc;
263 int err;
264
265 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
266
267 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
268
269 if (attr == NULL) {
270 discover_failed(gmap_cli, -ENOENT);
271
272 return BT_GATT_ITER_STOP;
273 }
274
275 chrc = attr->user_data;
276
277 /* Read features */
278 err = gmap_read_bgs_feat(gmap_cli, chrc->value_handle);
279 if (err != 0) {
280 discover_failed(gmap_cli, err);
281 }
282
283 return BT_GATT_ITER_STOP;
284 }
285
gmap_discover_bgs_feat(struct bt_gmap_client * gmap_cli)286 static int gmap_discover_bgs_feat(struct bt_gmap_client *gmap_cli)
287 {
288 LOG_DBG("conn %p", (void *)gmap_cli->conn);
289
290 memset(&gmap_cli->params.discover, 0, sizeof(gmap_cli->params.discover));
291
292 gmap_cli->params.discover.func = bgs_feat_discover_func;
293 gmap_cli->params.discover.uuid = gmap_bgs_feat_uuid;
294 gmap_cli->params.discover.type = BT_GATT_DISCOVER_CHARACTERISTIC;
295 gmap_cli->params.discover.start_handle = gmap_cli->svc_start_handle;
296 gmap_cli->params.discover.end_handle = gmap_cli->svc_end_handle;
297
298 return bt_gatt_discover(gmap_cli->conn, &gmap_cli->params.discover);
299 }
300
ugt_feat_read_cb(struct bt_conn * conn,uint8_t att_err,struct bt_gatt_read_params * params,const void * data,uint16_t len)301 static uint8_t ugt_feat_read_cb(struct bt_conn *conn, uint8_t att_err,
302 struct bt_gatt_read_params *params, const void *data, uint16_t len)
303 {
304 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
305 struct net_buf_simple buf;
306 int err = att_err;
307
308 __ASSERT(gmap_cli, "no instance for conn %p", (void *)conn);
309
310 LOG_DBG("conn %p att_err 0x%02x params %p data %p len %u", (void *)conn, att_err, params,
311 data, len);
312
313 if (data == NULL || att_err != BT_ATT_ERR_SUCCESS || len != sizeof(uint8_t)) {
314 if (att_err == BT_ATT_ERR_SUCCESS) {
315 att_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
316 }
317
318 discover_failed(gmap_cli, err);
319
320 return BT_GATT_ITER_STOP;
321 }
322
323 net_buf_simple_init_with_data(&buf, (void *)data, len);
324
325 gmap_cli->feat.ugt_feat = net_buf_simple_pull_u8(&buf);
326 LOG_DBG("ugt_feat 0x%02x", gmap_cli->feat.ugt_feat);
327
328 if ((gmap_cli->role & BT_GMAP_ROLE_BGS) != 0) {
329 err = gmap_discover_bgs_feat(gmap_cli);
330 } else if ((gmap_cli->role & BT_GMAP_ROLE_BGR) != 0) {
331 err = gmap_discover_bgr_feat(gmap_cli);
332 } else {
333 discover_complete(gmap_cli);
334
335 return BT_GATT_ITER_STOP;
336 }
337
338 if (err) {
339 discover_failed(gmap_cli, err);
340 }
341
342 return BT_GATT_ITER_STOP;
343 }
344
gmap_read_ugt_feat(struct bt_gmap_client * gmap_cli,uint16_t handle)345 static int gmap_read_ugt_feat(struct bt_gmap_client *gmap_cli, uint16_t handle)
346 {
347 LOG_DBG("conn %p handle 0x%04x", (void *)gmap_cli->conn, handle);
348
349 memset(&gmap_cli->params.read, 0, sizeof(gmap_cli->params.read));
350
351 gmap_cli->params.read.func = ugt_feat_read_cb;
352 gmap_cli->params.read.handle_count = 1u;
353 gmap_cli->params.read.single.handle = handle;
354 gmap_cli->params.read.single.offset = 0u;
355
356 return bt_gatt_read(gmap_cli->conn, &gmap_cli->params.read);
357 }
358
ugt_feat_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)359 static uint8_t ugt_feat_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
360 struct bt_gatt_discover_params *params)
361 {
362 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
363 const struct bt_gatt_chrc *chrc;
364 int err;
365
366 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
367
368 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
369
370 if (attr == NULL) {
371 discover_failed(gmap_cli, -ENOENT);
372
373 return BT_GATT_ITER_STOP;
374 }
375
376 chrc = attr->user_data;
377
378 /* Read features */
379 err = gmap_read_ugt_feat(gmap_cli, chrc->value_handle);
380 if (err != 0) {
381 discover_failed(gmap_cli, err);
382 }
383
384 return BT_GATT_ITER_STOP;
385 }
386
gmap_discover_ugt_feat(struct bt_gmap_client * gmap_cli)387 static int gmap_discover_ugt_feat(struct bt_gmap_client *gmap_cli)
388 {
389 LOG_DBG("conn %p", (void *)gmap_cli->conn);
390
391 memset(&gmap_cli->params.discover, 0, sizeof(gmap_cli->params.discover));
392
393 gmap_cli->params.discover.func = ugt_feat_discover_func;
394 gmap_cli->params.discover.uuid = gmap_ugt_feat_uuid;
395 gmap_cli->params.discover.type = BT_GATT_DISCOVER_CHARACTERISTIC;
396 gmap_cli->params.discover.start_handle = gmap_cli->svc_start_handle;
397 gmap_cli->params.discover.end_handle = gmap_cli->svc_end_handle;
398
399 return bt_gatt_discover(gmap_cli->conn, &gmap_cli->params.discover);
400 }
401
ugg_feat_read_cb(struct bt_conn * conn,uint8_t att_err,struct bt_gatt_read_params * params,const void * data,uint16_t len)402 static uint8_t ugg_feat_read_cb(struct bt_conn *conn, uint8_t att_err,
403 struct bt_gatt_read_params *params, const void *data, uint16_t len)
404 {
405 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
406 struct net_buf_simple buf;
407 int err = att_err;
408
409 __ASSERT(gmap_cli, "no instance for conn %p", (void *)conn);
410
411 LOG_DBG("conn %p att_err 0x%02x params %p data %p len %u", (void *)conn, att_err, params,
412 data, len);
413
414 if (data == NULL || att_err != BT_ATT_ERR_SUCCESS || len != sizeof(uint8_t)) {
415 if (att_err == BT_ATT_ERR_SUCCESS) {
416 att_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
417 }
418
419 discover_failed(gmap_cli, err);
420
421 return BT_GATT_ITER_STOP;
422 }
423
424 net_buf_simple_init_with_data(&buf, (void *)data, len);
425
426 gmap_cli->feat.ugg_feat = net_buf_simple_pull_u8(&buf);
427 LOG_DBG("ugg_feat 0x%02x", gmap_cli->feat.ugg_feat);
428
429 if ((gmap_cli->role & BT_GMAP_ROLE_UGT) != 0) {
430 err = gmap_discover_ugt_feat(gmap_cli);
431 } else if ((gmap_cli->role & BT_GMAP_ROLE_BGS) != 0) {
432 err = gmap_discover_bgs_feat(gmap_cli);
433 } else if ((gmap_cli->role & BT_GMAP_ROLE_BGR) != 0) {
434 err = gmap_discover_bgr_feat(gmap_cli);
435 } else {
436 discover_complete(gmap_cli);
437
438 return BT_GATT_ITER_STOP;
439 }
440
441 if (err) {
442 discover_failed(gmap_cli, err);
443 }
444
445 return BT_GATT_ITER_STOP;
446 }
447
gmap_read_ugg_feat(struct bt_gmap_client * gmap_cli,uint16_t handle)448 static int gmap_read_ugg_feat(struct bt_gmap_client *gmap_cli, uint16_t handle)
449 {
450 LOG_DBG("conn %p handle 0x%04x", (void *)gmap_cli->conn, handle);
451
452 memset(&gmap_cli->params.read, 0, sizeof(gmap_cli->params.read));
453
454 gmap_cli->params.read.func = ugg_feat_read_cb;
455 gmap_cli->params.read.handle_count = 1u;
456 gmap_cli->params.read.single.handle = handle;
457 gmap_cli->params.read.single.offset = 0u;
458
459 return bt_gatt_read(gmap_cli->conn, &gmap_cli->params.read);
460 }
461
ugg_feat_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)462 static uint8_t ugg_feat_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
463 struct bt_gatt_discover_params *params)
464 {
465 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
466 const struct bt_gatt_chrc *chrc;
467 int err;
468
469 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
470
471 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
472
473 if (attr == NULL) {
474 discover_failed(gmap_cli, -ENOENT);
475
476 return BT_GATT_ITER_STOP;
477 }
478
479 chrc = attr->user_data;
480
481 /* Read features */
482 err = gmap_read_ugg_feat(gmap_cli, chrc->value_handle);
483 if (err != 0) {
484 discover_failed(gmap_cli, err);
485 }
486
487 return BT_GATT_ITER_STOP;
488 }
489
gmap_discover_ugg_feat(struct bt_gmap_client * gmap_cli)490 static int gmap_discover_ugg_feat(struct bt_gmap_client *gmap_cli)
491 {
492 LOG_DBG("conn %p", (void *)gmap_cli->conn);
493
494 memset(&gmap_cli->params.discover, 0, sizeof(gmap_cli->params.discover));
495
496 gmap_cli->params.discover.func = ugg_feat_discover_func;
497 gmap_cli->params.discover.uuid = gmap_ugg_feat_uuid;
498 gmap_cli->params.discover.type = BT_GATT_DISCOVER_CHARACTERISTIC;
499 gmap_cli->params.discover.start_handle = gmap_cli->svc_start_handle;
500 gmap_cli->params.discover.end_handle = gmap_cli->svc_end_handle;
501
502 return bt_gatt_discover(gmap_cli->conn, &gmap_cli->params.discover);
503 }
504
role_read_cb(struct bt_conn * conn,uint8_t att_err,struct bt_gatt_read_params * params,const void * data,uint16_t len)505 static uint8_t role_read_cb(struct bt_conn *conn, uint8_t att_err,
506 struct bt_gatt_read_params *params, const void *data, uint16_t len)
507 {
508 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
509 struct net_buf_simple buf;
510 int err = att_err;
511
512 __ASSERT(gmap_cli, "no instance for conn %p", (void *)conn);
513
514 LOG_DBG("conn %p att_err 0x%02x params %p data %p len %u", (void *)conn, att_err, params,
515 data, len);
516
517 if (data == NULL || att_err != BT_ATT_ERR_SUCCESS || len != sizeof(uint8_t)) {
518 if (att_err == BT_ATT_ERR_SUCCESS) {
519 att_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
520 }
521
522 discover_failed(gmap_cli, err);
523
524 return BT_GATT_ITER_STOP;
525 }
526
527 net_buf_simple_init_with_data(&buf, (void *)data, len);
528
529 gmap_cli->role = net_buf_simple_pull_u8(&buf);
530 LOG_DBG("role 0x%02x", gmap_cli->role);
531
532 if ((gmap_cli->role & BT_GMAP_ROLE_UGG) != 0) {
533 err = gmap_discover_ugg_feat(gmap_cli);
534 } else if ((gmap_cli->role & BT_GMAP_ROLE_UGT) != 0) {
535 err = gmap_discover_ugt_feat(gmap_cli);
536 } else if ((gmap_cli->role & BT_GMAP_ROLE_BGS) != 0) {
537 err = gmap_discover_bgs_feat(gmap_cli);
538 } else if ((gmap_cli->role & BT_GMAP_ROLE_BGR) != 0) {
539 err = gmap_discover_bgr_feat(gmap_cli);
540 } else {
541 LOG_DBG("Remote device does not support any known roles");
542 err = -ECANCELED;
543 }
544
545 if (err) {
546 discover_failed(gmap_cli, err);
547 }
548
549 return BT_GATT_ITER_STOP;
550 }
551
gmap_read_role(struct bt_gmap_client * gmap_cli,uint16_t handle)552 static int gmap_read_role(struct bt_gmap_client *gmap_cli, uint16_t handle)
553 {
554 LOG_DBG("conn %p handle 0x%04x", (void *)gmap_cli->conn, handle);
555
556 memset(&gmap_cli->params.read, 0, sizeof(gmap_cli->params.read));
557
558 gmap_cli->params.read.func = role_read_cb;
559 gmap_cli->params.read.handle_count = 1u;
560 gmap_cli->params.read.single.handle = handle;
561 gmap_cli->params.read.single.offset = 0u;
562
563 return bt_gatt_read(gmap_cli->conn, &gmap_cli->params.read);
564 }
565
role_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)566 static uint8_t role_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
567 struct bt_gatt_discover_params *params)
568 {
569 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
570 const struct bt_gatt_chrc *chrc;
571 int err;
572
573 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
574
575 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
576
577 if (attr == NULL) {
578 discover_failed(gmap_cli, -ENOENT);
579
580 return BT_GATT_ITER_STOP;
581 }
582
583 chrc = attr->user_data;
584
585 /* Read features */
586 err = gmap_read_role(gmap_cli, chrc->value_handle);
587 if (err != 0) {
588 discover_failed(gmap_cli, err);
589 }
590
591 return BT_GATT_ITER_STOP;
592 }
593
gmap_discover_role(struct bt_gmap_client * gmap_cli)594 static int gmap_discover_role(struct bt_gmap_client *gmap_cli)
595 {
596 LOG_DBG("conn %p", (void *)gmap_cli->conn);
597
598 memset(&gmap_cli->params.discover, 0, sizeof(gmap_cli->params.discover));
599
600 gmap_cli->params.discover.func = role_discover_func;
601 gmap_cli->params.discover.uuid = gmap_role_uuid;
602 gmap_cli->params.discover.type = BT_GATT_DISCOVER_CHARACTERISTIC;
603 gmap_cli->params.discover.start_handle = gmap_cli->svc_start_handle;
604 gmap_cli->params.discover.end_handle = gmap_cli->svc_end_handle;
605
606 return bt_gatt_discover(gmap_cli->conn, &gmap_cli->params.discover);
607 }
608
gmas_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)609 static uint8_t gmas_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
610 struct bt_gatt_discover_params *params)
611 {
612 struct bt_gmap_client *gmap_cli = client_by_conn(conn);
613 const struct bt_gatt_service_val *svc;
614 int err;
615
616 __ASSERT(gmap_cli != NULL, "no instance for conn %p", (void *)conn);
617
618 LOG_DBG("conn %p attr %p params %p", (void *)conn, attr, params);
619
620 if (attr == NULL) {
621 discover_failed(gmap_cli, -ENOENT);
622
623 return BT_GATT_ITER_STOP;
624 }
625
626 svc = (struct bt_gatt_service_val *)attr->user_data;
627 gmap_cli->svc_start_handle = attr->handle;
628 gmap_cli->svc_end_handle = svc->end_handle;
629
630 err = gmap_discover_role(gmap_cli);
631 if (err != 0) {
632 discover_failed(gmap_cli, err);
633 }
634
635 return BT_GATT_ITER_STOP;
636 }
637
bt_gmap_discover(struct bt_conn * conn)638 int bt_gmap_discover(struct bt_conn *conn)
639 {
640 struct bt_gmap_client *gmap_cli;
641 int err;
642
643 CHECKIF(conn == NULL) {
644 LOG_DBG("NULL conn");
645
646 return -EINVAL;
647 }
648
649 gmap_cli = &gmap_insts[bt_conn_index(conn)];
650
651 if (gmap_cli->busy) {
652 LOG_DBG("Busy");
653
654 return -EBUSY;
655 }
656
657 gmap_reset(gmap_cli);
658
659 gmap_cli->params.discover.func = gmas_discover_func;
660 gmap_cli->params.discover.uuid = gmas_uuid;
661 gmap_cli->params.discover.type = BT_GATT_DISCOVER_PRIMARY;
662 gmap_cli->params.discover.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
663 gmap_cli->params.discover.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
664
665 err = bt_gatt_discover(conn, &gmap_cli->params.discover);
666 if (err != 0) {
667 LOG_DBG("Failed to initiate discovery: %d", err);
668
669 return -ENOEXEC;
670 }
671
672 gmap_cli->conn = bt_conn_ref(conn);
673
674 return 0;
675 }
676
bt_gmap_cb_register(const struct bt_gmap_cb * cb)677 int bt_gmap_cb_register(const struct bt_gmap_cb *cb)
678 {
679 CHECKIF(cb == NULL) {
680 LOG_DBG("cb is NULL");
681
682 return -EINVAL;
683 }
684
685 if (gmap_cb != NULL) {
686 LOG_DBG("GMAP callbacks already registered");
687
688 return -EALREADY;
689 }
690
691 gmap_cb = cb;
692
693 return 0;
694 }
695