forked from bluez/bluez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatt-database.c
3928 lines (3099 loc) · 92.2 KB
/
gatt-database.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2015 Google Inc.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/sdp_lib.h"
#include "lib/uuid.h"
#include "btio/btio.h"
#include "gdbus/gdbus.h"
#include "src/shared/util.h"
#include "src/shared/queue.h"
#include "src/shared/io.h"
#include "src/shared/att.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-server.h"
#include "log.h"
#include "error.h"
#include "btd.h"
#include "adapter.h"
#include "device.h"
#include "gatt-database.h"
#include "dbus-common.h"
#include "profile.h"
#include "service.h"
#define GATT_MANAGER_IFACE "org.bluez.GattManager1"
#define GATT_PROFILE_IFACE "org.bluez.GattProfile1"
#define GATT_SERVICE_IFACE "org.bluez.GattService1"
#define GATT_CHRC_IFACE "org.bluez.GattCharacteristic1"
#define GATT_DESC_IFACE "org.bluez.GattDescriptor1"
#define ERROR_FAILED ERROR_INTERFACE ".Failed"
#define UUID_GAP 0x1800
#define UUID_GATT 0x1801
#define UUID_DIS 0x180a
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
struct gatt_record {
struct btd_gatt_database *database;
uint32_t handle;
struct gatt_db_attribute *attr;
};
struct btd_gatt_database {
struct btd_adapter *adapter;
struct gatt_db *db;
unsigned int db_id;
GIOChannel *le_io;
GIOChannel *eatt_io;
GIOChannel *bredr_io;
struct queue *records;
struct queue *device_states;
struct queue *ccc_callbacks;
struct gatt_db_attribute *svc_chngd;
struct gatt_db_attribute *svc_chngd_ccc;
struct gatt_db_attribute *cli_feat;
struct gatt_db_attribute *db_hash;
struct gatt_db_attribute *eatt;
struct queue *apps;
struct queue *profiles;
};
struct gatt_app {
struct btd_gatt_database *database;
char *owner;
char *path;
DBusMessage *reg;
GDBusClient *client;
bool failed;
struct queue *profiles;
struct queue *services;
struct queue *proxies;
};
struct external_service {
struct gatt_app *app;
char *path; /* Path to GattService1 */
GDBusProxy *proxy;
struct gatt_db_attribute *attrib;
uint16_t attr_cnt;
struct queue *chrcs;
struct queue *descs;
struct queue *includes;
};
struct external_profile {
struct gatt_app *app;
GDBusProxy *proxy;
struct queue *profiles; /* btd_profile list */
};
struct external_chrc {
struct external_service *service;
char *path;
GDBusProxy *proxy;
uint8_t props;
uint8_t ext_props;
uint32_t perm;
uint32_t ccc_perm;
uint16_t mtu;
struct io *write_io;
struct io *notify_io;
struct gatt_db_attribute *attrib;
struct gatt_db_attribute *ccc;
struct queue *pending_reads;
struct queue *pending_writes;
unsigned int ntfy_cnt;
bool prep_authorized;
bool req_prep_authorization;
};
struct external_desc {
struct external_service *service;
char *chrc_path;
GDBusProxy *proxy;
uint32_t perm;
struct gatt_db_attribute *attrib;
bool handled;
struct queue *pending_reads;
struct queue *pending_writes;
bool prep_authorized;
bool req_prep_authorization;
};
struct pending_op {
struct bt_att *att;
unsigned int id;
unsigned int disconn_id;
uint16_t offset;
uint8_t link_type;
struct gatt_db_attribute *attrib;
struct queue *owner_queue;
struct iovec data;
bool is_characteristic;
bool prep_authorize;
};
struct notify {
struct btd_gatt_database *database;
uint16_t handle, ccc_handle;
uint8_t *value;
uint16_t len;
bt_gatt_server_conf_func_t conf;
void *user_data;
};
#define CLI_FEAT_SIZE 1
struct device_state {
struct btd_gatt_database *db;
bdaddr_t bdaddr;
uint8_t bdaddr_type;
unsigned int disc_id;
uint8_t cli_feat[CLI_FEAT_SIZE];
bool change_aware;
bool out_of_sync;
struct queue *ccc_states;
struct notify *pending;
};
typedef uint8_t (*btd_gatt_database_ccc_write_t) (struct pending_op *op,
void *user_data);
typedef void (*btd_gatt_database_destroy_t) (void *data);
struct ccc_state {
uint16_t handle;
uint16_t value;
};
struct ccc_cb_data {
uint16_t handle;
btd_gatt_database_ccc_write_t callback;
btd_gatt_database_destroy_t destroy;
void *user_data;
};
struct device_info {
bdaddr_t bdaddr;
uint8_t bdaddr_type;
};
static void ccc_cb_free(void *data)
{
struct ccc_cb_data *ccc_cb = data;
if (ccc_cb->destroy)
ccc_cb->destroy(ccc_cb->user_data);
free(ccc_cb);
}
static bool ccc_cb_match_service(const void *data, const void *match_data)
{
const struct ccc_cb_data *ccc_cb = data;
const struct gatt_db_attribute *attrib = match_data;
uint16_t start, end;
if (!gatt_db_attribute_get_service_handles(attrib, &start, &end))
return false;
return ccc_cb->handle >= start && ccc_cb->handle <= end;
}
static bool ccc_cb_match_handle(const void *data, const void *match_data)
{
const struct ccc_cb_data *ccc_cb = data;
uint16_t handle = PTR_TO_UINT(match_data);
return ccc_cb->handle == handle;
}
static bool dev_state_match(const void *a, const void *b)
{
const struct device_state *dev_state = a;
const struct device_info *dev_info = b;
return bacmp(&dev_state->bdaddr, &dev_info->bdaddr) == 0 &&
dev_state->bdaddr_type == dev_info->bdaddr_type;
}
static struct device_state *
find_device_state(struct btd_gatt_database *database, const bdaddr_t *bdaddr,
uint8_t bdaddr_type)
{
struct device_info dev_info;
memset(&dev_info, 0, sizeof(dev_info));
bacpy(&dev_info.bdaddr, bdaddr);
dev_info.bdaddr_type = bdaddr_type;
return queue_find(database->device_states, dev_state_match, &dev_info);
}
static bool ccc_state_match(const void *a, const void *b)
{
const struct ccc_state *ccc = a;
uint16_t handle = PTR_TO_UINT(b);
return ccc->handle == handle;
}
static struct ccc_state *find_ccc_state(struct device_state *dev_state,
uint16_t handle)
{
return queue_find(dev_state->ccc_states, ccc_state_match,
UINT_TO_PTR(handle));
}
static struct device_state *device_state_create(struct btd_gatt_database *db,
const bdaddr_t *bdaddr,
uint8_t bdaddr_type)
{
struct device_state *dev_state;
dev_state = new0(struct device_state, 1);
dev_state->db = db;
dev_state->ccc_states = queue_new();
bacpy(&dev_state->bdaddr, bdaddr);
dev_state->bdaddr_type = bdaddr_type;
return dev_state;
}
static void device_state_free(void *data)
{
struct device_state *state = data;
queue_destroy(state->ccc_states, free);
if (state->pending) {
free(state->pending->value);
free(state->pending);
}
free(state);
}
static void clear_ccc_state(void *data, void *user_data)
{
struct ccc_state *ccc = data;
struct btd_gatt_database *db = user_data;
struct ccc_cb_data *ccc_cb;
if (!ccc->value)
return;
ccc_cb = queue_find(db->ccc_callbacks, ccc_cb_match_handle,
UINT_TO_PTR(ccc->handle));
if (!ccc_cb)
return;
if (ccc_cb->callback)
ccc_cb->callback(NULL, ccc_cb->user_data);
}
static void att_disconnected(int err, void *user_data)
{
struct device_state *state = user_data;
struct btd_device *device;
DBG("");
state->disc_id = 0;
state->out_of_sync = false;
device = btd_adapter_find_device(state->db->adapter, &state->bdaddr,
state->bdaddr_type);
if (!device)
goto remove;
if (device_is_bonded(device, state->bdaddr_type)) {
struct ccc_state *ccc;
uint16_t handle;
handle = gatt_db_attribute_get_handle(state->db->svc_chngd_ccc);
ccc = find_ccc_state(state, handle);
if (ccc && ccc->value)
device_store_svc_chng_ccc(device, state->bdaddr_type,
ccc->value);
return;
}
remove:
/* Remove device state if device no longer exists or is not paired */
if (queue_remove(state->db->device_states, state)) {
queue_foreach(state->ccc_states, clear_ccc_state, state->db);
device_state_free(state);
}
}
static bool get_dst_info(struct bt_att *att, bdaddr_t *dst, uint8_t *dst_type)
{
GIOChannel *io = NULL;
GError *gerr = NULL;
int fd;
fd = bt_att_get_fd(att);
if (fd < 0)
return false;
io = g_io_channel_unix_new(fd);
if (!io)
return false;
bt_io_get(io, &gerr, BT_IO_OPT_DEST_BDADDR, dst,
BT_IO_OPT_DEST_TYPE, dst_type,
BT_IO_OPT_INVALID);
if (gerr) {
error("gatt: bt_io_get: %s", gerr->message);
g_error_free(gerr);
g_io_channel_unref(io);
return false;
}
g_io_channel_unref(io);
return true;
}
static struct device_state *get_device_state(struct btd_gatt_database *database,
struct bt_att *att)
{
struct device_state *dev_state;
bdaddr_t bdaddr;
uint8_t bdaddr_type;
if (!get_dst_info(att, &bdaddr, &bdaddr_type))
return NULL;
/*
* Find and return a device state. If a matching state doesn't exist,
* then create a new one.
*/
dev_state = find_device_state(database, &bdaddr, bdaddr_type);
if (dev_state)
goto done;
dev_state = device_state_create(database, &bdaddr, bdaddr_type);
queue_push_tail(database->device_states, dev_state);
done:
if (!dev_state->disc_id)
dev_state->disc_id = bt_att_register_disconnect(att,
att_disconnected,
dev_state, NULL);
return dev_state;
}
static struct ccc_state *get_ccc_state(struct btd_gatt_database *database,
struct bt_att *att, uint16_t handle)
{
struct device_state *dev_state;
struct ccc_state *ccc;
dev_state = get_device_state(database, att);
if (!dev_state)
return NULL;
ccc = find_ccc_state(dev_state, handle);
if (ccc)
return ccc;
ccc = new0(struct ccc_state, 1);
ccc->handle = handle;
queue_push_tail(dev_state->ccc_states, ccc);
return ccc;
}
static void cancel_pending_read(void *data)
{
struct pending_op *op = data;
gatt_db_attribute_read_result(op->attrib, op->id,
BT_ATT_ERROR_REQUEST_NOT_SUPPORTED,
NULL, 0);
op->owner_queue = NULL;
}
static void cancel_pending_write(void *data)
{
struct pending_op *op = data;
gatt_db_attribute_write_result(op->attrib, op->id,
BT_ATT_ERROR_REQUEST_NOT_SUPPORTED);
op->owner_queue = NULL;
}
static void chrc_free(void *data)
{
struct external_chrc *chrc = data;
io_destroy(chrc->write_io);
io_destroy(chrc->notify_io);
queue_destroy(chrc->pending_reads, cancel_pending_read);
queue_destroy(chrc->pending_writes, cancel_pending_write);
g_free(chrc->path);
g_dbus_proxy_set_property_watch(chrc->proxy, NULL, NULL);
g_dbus_proxy_unref(chrc->proxy);
free(chrc);
}
static void desc_free(void *data)
{
struct external_desc *desc = data;
queue_destroy(desc->pending_reads, cancel_pending_read);
queue_destroy(desc->pending_writes, cancel_pending_write);
g_dbus_proxy_unref(desc->proxy);
g_free(desc->chrc_path);
free(desc);
}
static void inc_free(void *data)
{
struct external_desc *inc = data;
free(inc);
}
static void service_free(void *data)
{
struct external_service *service = data;
queue_destroy(service->chrcs, chrc_free);
queue_destroy(service->descs, desc_free);
queue_destroy(service->includes, inc_free);
if (service->attrib)
gatt_db_remove_service(service->app->database->db,
service->attrib);
if (service->app->client)
g_dbus_proxy_unref(service->proxy);
g_free(service->path);
free(service);
}
static void profile_remove(void *data)
{
struct btd_profile *p = data;
DBG("Removed \"%s\"", p->name);
adapter_foreach(adapter_remove_profile, p);
btd_profile_unregister(p);
g_free((void *) p->name);
g_free((void *) p->remote_uuid);
free(p);
}
static void profile_release(struct external_profile *profile)
{
DBG("Releasing \"%s\"", profile->app->owner);
g_dbus_proxy_method_call(profile->proxy, "Release", NULL, NULL, NULL,
NULL);
}
static void profile_free(void *data)
{
struct external_profile *profile = data;
queue_destroy(profile->profiles, profile_remove);
profile_release(profile);
g_dbus_proxy_unref(profile->proxy);
free(profile);
}
static void app_free(void *data)
{
struct gatt_app *app = data;
queue_destroy(app->profiles, profile_free);
queue_destroy(app->services, service_free);
queue_destroy(app->proxies, NULL);
if (app->client) {
g_dbus_client_set_disconnect_watch(app->client, NULL, NULL);
g_dbus_client_set_proxy_handlers(app->client, NULL, NULL,
NULL, NULL);
g_dbus_client_set_ready_watch(app->client, NULL, NULL);
g_dbus_client_unref(app->client);
}
if (app->reg)
dbus_message_unref(app->reg);
g_free(app->owner);
g_free(app->path);
free(app);
}
static void gatt_record_free(void *data)
{
struct gatt_record *rec = data;
adapter_service_remove(rec->database->adapter, rec->handle);
free(rec);
}
static void gatt_database_free(void *data)
{
struct btd_gatt_database *database = data;
if (database->le_io) {
g_io_channel_shutdown(database->le_io, FALSE, NULL);
g_io_channel_unref(database->le_io);
}
if (database->eatt_io) {
g_io_channel_shutdown(database->eatt_io, FALSE, NULL);
g_io_channel_unref(database->eatt_io);
}
if (database->bredr_io) {
g_io_channel_shutdown(database->bredr_io, FALSE, NULL);
g_io_channel_unref(database->bredr_io);
}
/* TODO: Persistently store CCC states before freeing them */
gatt_db_unregister(database->db, database->db_id);
queue_destroy(database->records, gatt_record_free);
queue_destroy(database->device_states, device_state_free);
queue_destroy(database->apps, app_free);
queue_destroy(database->profiles, profile_free);
queue_destroy(database->ccc_callbacks, ccc_cb_free);
database->device_states = NULL;
database->ccc_callbacks = NULL;
gatt_db_unref(database->db);
btd_adapter_unref(database->adapter);
free(database);
}
static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
{
struct btd_adapter *adapter;
struct btd_device *device;
uint8_t dst_type;
bdaddr_t src, dst;
if (gerr) {
error("%s", gerr->message);
return;
}
bt_io_get(io, &gerr, BT_IO_OPT_SOURCE_BDADDR, &src,
BT_IO_OPT_DEST_BDADDR, &dst,
BT_IO_OPT_DEST_TYPE, &dst_type,
BT_IO_OPT_INVALID);
if (gerr) {
error("bt_io_get: %s", gerr->message);
g_error_free(gerr);
return;
}
DBG("New incoming %s ATT connection", dst_type == BDADDR_BREDR ?
"BR/EDR" : "LE");
adapter = adapter_find(&src);
if (!adapter)
return;
device = btd_adapter_get_device(adapter, &dst, dst_type);
if (!device)
return;
device_attach_att(device, io);
}
static void gap_device_name_read_cb(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct btd_gatt_database *database = user_data;
uint8_t error = 0;
size_t len = 0;
const uint8_t *value = NULL;
const char *device_name;
DBG("GAP Device Name read request\n");
device_name = btd_adapter_get_name(database->adapter);
len = strlen(device_name);
if (offset > len) {
error = BT_ATT_ERROR_INVALID_OFFSET;
goto done;
}
len -= offset;
value = len ? (const uint8_t *) &device_name[offset] : NULL;
done:
gatt_db_attribute_read_result(attrib, id, error, value, len);
}
static void gap_appearance_read_cb(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct btd_gatt_database *database = user_data;
uint8_t error = 0;
size_t len = 2;
const uint8_t *value = NULL;
uint8_t appearance[2];
uint32_t dev_class;
DBG("GAP Appearance read request\n");
dev_class = btd_adapter_get_class(database->adapter);
appearance[0] = dev_class & 0x00ff;
appearance[1] = (dev_class >> 8) & 0x001f;
len -= offset;
value = len ? &appearance[offset] : NULL;
gatt_db_attribute_read_result(attrib, id, error, value, len);
}
static void gap_car_read_cb(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
uint8_t value = 0x01;
DBG("GAP Central Address Resolution read request\n");
gatt_db_attribute_read_result(attrib, id, 0, &value, sizeof(value));
}
static sdp_record_t *record_new(uuid_t *uuid, uint16_t start, uint16_t end)
{
sdp_list_t *svclass_id, *apseq, *proto[2], *root, *aproto;
uuid_t root_uuid, proto_uuid, l2cap;
sdp_record_t *record;
sdp_data_t *psm, *sh, *eh;
uint16_t lp = BT_ATT_PSM;
if (uuid == NULL)
return NULL;
if (start > end)
return NULL;
record = sdp_record_alloc();
if (record == NULL)
return NULL;
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root = sdp_list_append(NULL, &root_uuid);
sdp_set_browse_groups(record, root);
sdp_list_free(root, NULL);
svclass_id = sdp_list_append(NULL, uuid);
sdp_set_service_classes(record, svclass_id);
sdp_list_free(svclass_id, NULL);
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto[0] = sdp_list_append(NULL, &l2cap);
psm = sdp_data_alloc(SDP_UINT16, &lp);
proto[0] = sdp_list_append(proto[0], psm);
apseq = sdp_list_append(NULL, proto[0]);
sdp_uuid16_create(&proto_uuid, ATT_UUID);
proto[1] = sdp_list_append(NULL, &proto_uuid);
sh = sdp_data_alloc(SDP_UINT16, &start);
proto[1] = sdp_list_append(proto[1], sh);
eh = sdp_data_alloc(SDP_UINT16, &end);
proto[1] = sdp_list_append(proto[1], eh);
apseq = sdp_list_append(apseq, proto[1]);
aproto = sdp_list_append(NULL, apseq);
sdp_set_access_protos(record, aproto);
sdp_data_free(psm);
sdp_data_free(sh);
sdp_data_free(eh);
sdp_list_free(proto[0], NULL);
sdp_list_free(proto[1], NULL);
sdp_list_free(apseq, NULL);
sdp_list_free(aproto, NULL);
return record;
}
static void database_add_record(struct btd_gatt_database *database,
struct gatt_db_attribute *attr)
{
struct gatt_record *rec;
sdp_record_t *record;
uint16_t start, end;
uuid_t svc, gap_uuid;
bt_uuid_t uuid;
const char *name = NULL;
char uuidstr[MAX_LEN_UUID_STR];
gatt_db_attribute_get_service_uuid(attr, &uuid);
switch (uuid.type) {
case BT_UUID16:
name = bt_uuid16_to_str(uuid.value.u16);
sdp_uuid16_create(&svc, uuid.value.u16);
break;
case BT_UUID32:
name = bt_uuid32_to_str(uuid.value.u32);
sdp_uuid32_create(&svc, uuid.value.u32);
break;
case BT_UUID128:
bt_uuid_to_string(&uuid, uuidstr, sizeof(uuidstr));
name = bt_uuidstr_to_str(uuidstr);
sdp_uuid128_create(&svc, (void *) &uuid.value.u128);
break;
case BT_UUID_UNSPEC:
return;
}
gatt_db_attribute_get_service_handles(attr, &start, &end);
record = record_new(&svc, start, end);
if (!record)
return;
if (name != NULL)
sdp_set_info_attr(record, name, "BlueZ", NULL);
sdp_uuid16_create(&gap_uuid, UUID_GAP);
if (sdp_uuid_cmp(&svc, &gap_uuid) == 0) {
sdp_set_url_attr(record, "http://www.bluez.org/",
"http://www.bluez.org/",
"http://www.bluez.org/");
}
if (adapter_service_add(database->adapter, record) < 0) {
sdp_record_free(record);
return;
}
rec = new0(struct gatt_record, 1);
rec->database = database;
rec->handle = record->handle;
rec->attr = attr;
queue_push_tail(database->records, rec);
}
static void populate_gap_service(struct btd_gatt_database *database)
{
bt_uuid_t uuid;
struct gatt_db_attribute *service, *attrib;
/* Add the GAP service */
bt_uuid16_create(&uuid, UUID_GAP);
service = gatt_db_add_service(database->db, &uuid, true, 7);
/*
* Device Name characteristic.
*/
bt_uuid16_create(&uuid, GATT_CHARAC_DEVICE_NAME);
gatt_db_service_add_characteristic(service, &uuid, BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ,
gap_device_name_read_cb,
NULL, database);
/*
* Device Appearance characteristic.
*/
bt_uuid16_create(&uuid, GATT_CHARAC_APPEARANCE);
attrib = gatt_db_service_add_characteristic(service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ,
gap_appearance_read_cb,
NULL, database);
gatt_db_attribute_set_fixed_length(attrib, 2);
/*
* Central Address Resolution characteristic.
*/
bt_uuid16_create(&uuid, GATT_CHARAC_CAR);
attrib = gatt_db_service_add_characteristic(service, &uuid,
BT_ATT_PERM_READ,
BT_GATT_CHRC_PROP_READ,
gap_car_read_cb,
NULL, database);
gatt_db_attribute_set_fixed_length(attrib, 1);
gatt_db_service_set_active(service, true);
database_add_record(database, service);
}
static void gatt_ccc_read_cb(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct btd_gatt_database *database = user_data;
struct ccc_state *ccc;
uint16_t handle;
uint8_t ecode = 0;
const uint8_t *value = NULL;
size_t len = 0;
handle = gatt_db_attribute_get_handle(attrib);
DBG("CCC read called for handle: 0x%04x", handle);
ccc = get_ccc_state(database, att, handle);
if (!ccc) {
ecode = BT_ATT_ERROR_UNLIKELY;
goto done;
}
len = sizeof(ccc->value);
value = (void *) &ccc->value;
done:
gatt_db_attribute_read_result(attrib, id, ecode, value, len);
}
static struct btd_device *att_get_device(struct bt_att *att)
{
GIOChannel *io = NULL;
GError *gerr = NULL;
bdaddr_t src, dst;
uint8_t dst_type;
struct btd_adapter *adapter;
io = g_io_channel_unix_new(bt_att_get_fd(att));
if (!io)
return NULL;
bt_io_get(io, &gerr, BT_IO_OPT_SOURCE_BDADDR, &src,
BT_IO_OPT_DEST_BDADDR, &dst,
BT_IO_OPT_DEST_TYPE, &dst_type,
BT_IO_OPT_INVALID);
if (gerr) {
error("bt_io_get: %s", gerr->message);
g_error_free(gerr);
g_io_channel_unref(io);
return NULL;
}
g_io_channel_unref(io);
adapter = adapter_find(&src);
if (!adapter) {
error("Unable to find adapter object");
return NULL;
}
return btd_adapter_find_device(adapter, &dst, dst_type);
}
static void pending_op_free(void *data)
{
struct pending_op *op = data;
if (op->owner_queue)
queue_remove(op->owner_queue, op);
bt_att_unregister_disconnect(op->att, op->disconn_id);
bt_att_unref(op->att);
free(op);
}
static void pending_disconnect_cb(int err, void *user_data)
{
struct pending_op *op = user_data;
op->owner_queue = NULL;
}
static struct pending_op *pending_ccc_new(struct bt_att *att,
struct gatt_db_attribute *attrib,
uint16_t value,
uint8_t link_type)
{
struct pending_op *op;
struct btd_device *device;
device = att_get_device(att);
if (!device) {
error("Unable to find device object");
return NULL;
}
op = new0(struct pending_op, 1);
op->data.iov_base = UINT_TO_PTR(value);
op->data.iov_len = sizeof(value);
op->att = bt_att_ref(att);
op->attrib = attrib;
op->link_type = link_type;
op->disconn_id = bt_att_register_disconnect(att,
pending_disconnect_cb,
op,
NULL);
return op;
}
static void gatt_ccc_write_cb(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
const uint8_t *value, size_t len,
uint8_t opcode, struct bt_att *att,
void *user_data)
{
struct btd_gatt_database *database = user_data;
struct ccc_state *ccc;
struct ccc_cb_data *ccc_cb;
uint16_t handle, val;
uint8_t ecode = 0;