forked from nrfconnect/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatt.c
More file actions
4082 lines (3285 loc) · 97.4 KB
/
att.c
File metadata and controls
4082 lines (3285 loc) · 97.4 KB
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
/* att.c - Attribute protocol handling */
/*
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/att.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include "common/bt_str.h"
#include "hci_core.h"
#include "conn_internal.h"
#include "l2cap_internal.h"
#include "smp.h"
#include "att_internal.h"
#include "gatt_internal.h"
#define LOG_LEVEL CONFIG_BT_ATT_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_att);
#define ATT_CHAN(_ch) CONTAINER_OF(_ch, struct bt_att_chan, chan.chan)
#define ATT_REQ(_node) CONTAINER_OF(_node, struct bt_att_req, node)
#define ATT_CMD_MASK 0x40
#if defined(CONFIG_BT_EATT)
#define ATT_CHAN_MAX (CONFIG_BT_EATT_MAX + 1)
#else
#define ATT_CHAN_MAX 1
#endif /* CONFIG_BT_EATT */
typedef enum __packed {
ATT_COMMAND,
ATT_REQUEST,
ATT_RESPONSE,
ATT_NOTIFICATION,
ATT_CONFIRMATION,
ATT_INDICATION,
ATT_UNKNOWN,
} att_type_t;
static att_type_t att_op_get_type(uint8_t op);
#if CONFIG_BT_ATT_PREPARE_COUNT > 0
struct bt_attr_data {
uint16_t handle;
uint16_t offset;
};
/* Pool for incoming ATT packets */
NET_BUF_POOL_DEFINE(prep_pool, CONFIG_BT_ATT_PREPARE_COUNT, BT_ATT_BUF_SIZE,
sizeof(struct bt_attr_data), NULL);
#endif /* CONFIG_BT_ATT_PREPARE_COUNT */
K_MEM_SLAB_DEFINE(req_slab, sizeof(struct bt_att_req),
CONFIG_BT_L2CAP_TX_BUF_COUNT, __alignof__(struct bt_att_req));
enum {
ATT_PENDING_RSP,
ATT_PENDING_CFM,
ATT_CONNECTED,
ATT_ENHANCED,
ATT_PENDING_SENT,
ATT_OUT_OF_SYNC_SENT,
/* Total number of flags - must be at the end of the enum */
ATT_NUM_FLAGS,
};
/* ATT channel specific data */
struct bt_att_chan {
/* Connection this channel is associated with */
struct bt_att *att;
struct bt_l2cap_le_chan chan;
ATOMIC_DEFINE(flags, ATT_NUM_FLAGS);
struct bt_att_req *req;
struct k_fifo tx_queue;
struct k_work_delayable timeout_work;
sys_snode_t node;
};
static bool bt_att_is_enhanced(struct bt_att_chan *chan)
{
/* Optimization. */
if (!IS_ENABLED(CONFIG_BT_EATT)) {
return false;
}
return atomic_test_bit(chan->flags, ATT_ENHANCED);
}
static uint16_t bt_att_mtu(struct bt_att_chan *chan)
{
/* Core v5.3 Vol 3 Part F 3.4.2:
*
* The server and client shall set ATT_MTU to the minimum of the
* Client Rx MTU and the Server Rx MTU.
*/
return MIN(chan->chan.rx.mtu, chan->chan.tx.mtu);
}
/* Descriptor of application-specific authorization callbacks that are used
* with the CONFIG_BT_GATT_AUTHORIZATION_CUSTOM Kconfig enabled.
*/
const static struct bt_gatt_authorization_cb *authorization_cb;
/* ATT connection specific data */
struct bt_att {
struct bt_conn *conn;
/* Shared request queue */
sys_slist_t reqs;
struct k_fifo tx_queue;
#if CONFIG_BT_ATT_PREPARE_COUNT > 0
sys_slist_t prep_queue;
#endif
/* Contains bt_att_chan instance(s) */
sys_slist_t chans;
#if defined(CONFIG_BT_EATT)
struct {
struct k_work_delayable connection_work;
uint8_t chans_to_connect;
uint16_t prev_conn_rsp_result;
uint16_t prev_conn_req_result;
uint8_t prev_conn_req_missing_chans;
} eatt;
#endif /* CONFIG_BT_EATT */
};
K_MEM_SLAB_DEFINE(att_slab, sizeof(struct bt_att),
CONFIG_BT_MAX_CONN, __alignof__(struct bt_att));
K_MEM_SLAB_DEFINE(chan_slab, sizeof(struct bt_att_chan),
CONFIG_BT_MAX_CONN * ATT_CHAN_MAX,
__alignof__(struct bt_att_chan));
static struct bt_att_req cancel;
/** The thread ATT response handlers likely run on.
*
* Blocking this thread while waiting for an ATT request to resolve can cause a
* deadlock.
*
* This can happen if the application queues ATT requests in the context of a
* callback from the Bluetooth stack. This is because queuing an ATT request
* will block until a request-resource is available, and the callbacks run on
* the same thread as the ATT response handler that frees request-resources.
*
* The intended use of this value is to detect the above situation.
*/
static k_tid_t att_handle_rsp_thread;
struct bt_att_tx_meta_data {
struct bt_att_chan *att_chan;
uint16_t attr_count;
bt_gatt_complete_func_t func;
void *user_data;
enum bt_att_chan_opt chan_opt;
};
struct bt_att_tx_meta {
struct bt_att_tx_meta_data *data;
};
#define bt_att_tx_meta_data(buf) (((struct bt_att_tx_meta *)net_buf_user_data(buf))->data)
static struct bt_att_tx_meta_data tx_meta_data[CONFIG_BT_CONN_TX_MAX];
K_FIFO_DEFINE(free_att_tx_meta_data);
static struct bt_att_tx_meta_data *tx_meta_data_alloc(k_timeout_t timeout)
{
/* The meta data always get freed in the system workqueue,
* so if we're in the same workqueue but there are no immediate
* contexts available, there's no chance we'll get one by waiting.
*/
if (k_current_get() == &k_sys_work_q.thread) {
return k_fifo_get(&free_att_tx_meta_data, K_NO_WAIT);
}
return k_fifo_get(&free_att_tx_meta_data, timeout);
}
static inline void tx_meta_data_free(struct bt_att_tx_meta_data *data)
{
__ASSERT_NO_MSG(data);
(void)memset(data, 0, sizeof(*data));
k_fifo_put(&free_att_tx_meta_data, data);
}
static int bt_att_chan_send(struct bt_att_chan *chan, struct net_buf *buf);
static bt_conn_tx_cb_t chan_cb(const struct net_buf *buf);
static bt_conn_tx_cb_t att_cb(const struct net_buf *buf);
static void att_chan_mtu_updated(struct bt_att_chan *updated_chan);
static void bt_att_disconnected(struct bt_l2cap_chan *chan);
struct net_buf *bt_att_create_rsp_pdu(struct bt_att_chan *chan,
uint8_t op, size_t len);
void att_sent(struct bt_conn *conn, void *user_data)
{
struct bt_att_tx_meta_data *data = user_data;
struct bt_att_chan *att_chan = data->att_chan;
struct bt_l2cap_chan *chan = &att_chan->chan.chan;
LOG_DBG("conn %p chan %p", conn, chan);
if (chan->ops->sent) {
chan->ops->sent(chan);
}
}
/* In case of success the ownership of the buffer is transferred to the stack
* which takes care of releasing it when it completes transmitting to the
* controller.
*
* In case bt_l2cap_send_cb fails the buffer state and ownership are retained
* so the buffer can be safely pushed back to the queue to be processed later.
*/
static int chan_send(struct bt_att_chan *chan, struct net_buf *buf)
{
struct bt_att_hdr *hdr;
struct net_buf_simple_state state;
int err;
struct bt_att_tx_meta_data *data = bt_att_tx_meta_data(buf);
struct bt_att_chan *prev_chan = data->att_chan;
hdr = (void *)buf->data;
LOG_DBG("code 0x%02x", hdr->code);
if (!atomic_test_bit(chan->flags, ATT_CONNECTED)) {
LOG_ERR("ATT channel not connected");
return -EINVAL;
}
if (IS_ENABLED(CONFIG_BT_EATT) && hdr->code == BT_ATT_OP_MTU_REQ &&
chan->chan.tx.cid != BT_L2CAP_CID_ATT) {
/* The Exchange MTU sub-procedure shall only be supported on
* the LE Fixed Channel Unenhanced ATT bearer
*/
return -ENOTSUP;
}
if (IS_ENABLED(CONFIG_BT_EATT) && bt_att_is_enhanced(chan)) {
/* Check if sent is pending already, if it does it cannot be
* modified so the operation will need to be queued.
*/
if (atomic_test_bit(chan->flags, ATT_PENDING_SENT)) {
return -EAGAIN;
}
if (hdr->code == BT_ATT_OP_SIGNED_WRITE_CMD) {
return -ENOTSUP;
}
/* Check if the channel is ready to send in case of a request */
if (att_op_get_type(hdr->code) == ATT_REQUEST &&
!atomic_test_bit(chan->chan.chan.status,
BT_L2CAP_STATUS_OUT)) {
return -EAGAIN;
}
atomic_set_bit(chan->flags, ATT_PENDING_SENT);
data->att_chan = chan;
/* bt_l2cap_chan_send does actually return the number of bytes
* that could be sent immediately.
*/
err = bt_l2cap_chan_send_cb(&chan->chan.chan, buf, chan_cb(buf), data);
if (err < 0) {
data->att_chan = prev_chan;
atomic_clear_bit(chan->flags, ATT_PENDING_SENT);
return err;
}
return 0;
}
if (hdr->code == BT_ATT_OP_SIGNED_WRITE_CMD) {
err = bt_smp_sign(chan->att->conn, buf);
if (err) {
LOG_ERR("Error signing data");
tx_meta_data_free(bt_att_tx_meta_data(buf));
net_buf_unref(buf);
return err;
}
}
net_buf_simple_save(&buf->b, &state);
data->att_chan = chan;
err = bt_l2cap_send_cb(chan->att->conn, BT_L2CAP_CID_ATT,
buf, att_cb(buf), data);
if (err) {
if (err == -ENOBUFS) {
LOG_ERR("Ran out of TX buffers or contexts.");
}
/* In case of an error has occurred restore the buffer state */
net_buf_simple_restore(&buf->b, &state);
data->att_chan = prev_chan;
}
return err;
}
static bool att_chan_matches_chan_opt(struct bt_att_chan *chan, enum bt_att_chan_opt chan_opt)
{
__ASSERT_NO_MSG(chan_opt <= BT_ATT_CHAN_OPT_ENHANCED_ONLY);
if (chan_opt == BT_ATT_CHAN_OPT_NONE) {
return true;
}
if (bt_att_is_enhanced(chan)) {
return (chan_opt & BT_ATT_CHAN_OPT_ENHANCED_ONLY);
} else {
return (chan_opt & BT_ATT_CHAN_OPT_UNENHANCED_ONLY);
}
}
static struct net_buf *get_first_buf_matching_chan(struct k_fifo *fifo, struct bt_att_chan *chan)
{
if (IS_ENABLED(CONFIG_BT_EATT)) {
struct k_fifo skipped;
struct net_buf *buf;
struct net_buf *ret = NULL;
k_fifo_init(&skipped);
while ((buf = net_buf_get(fifo, K_NO_WAIT))) {
if (!ret &&
att_chan_matches_chan_opt(chan, bt_att_tx_meta_data(buf)->chan_opt)) {
ret = buf;
} else {
net_buf_put(&skipped, buf);
}
}
__ASSERT_NO_MSG(k_fifo_is_empty(fifo));
while ((buf = net_buf_get(&skipped, K_NO_WAIT))) {
net_buf_put(fifo, buf);
}
__ASSERT_NO_MSG(k_fifo_is_empty(&skipped));
return ret;
} else {
return net_buf_get(fifo, K_NO_WAIT);
}
}
static struct bt_att_req *get_first_req_matching_chan(sys_slist_t *reqs, struct bt_att_chan *chan)
{
if (IS_ENABLED(CONFIG_BT_EATT)) {
sys_snode_t *curr, *prev = NULL;
SYS_SLIST_FOR_EACH_NODE(reqs, curr) {
if (att_chan_matches_chan_opt(
chan, bt_att_tx_meta_data(ATT_REQ(curr)->buf)->chan_opt)) {
break;
}
prev = curr;
}
if (curr) {
sys_slist_remove(reqs, prev, curr);
return ATT_REQ(curr);
}
return NULL;
}
sys_snode_t *node = sys_slist_get(reqs);
if (node) {
return ATT_REQ(node);
} else {
return NULL;
}
}
static int process_queue(struct bt_att_chan *chan, struct k_fifo *queue)
{
struct net_buf *buf;
int err;
buf = get_first_buf_matching_chan(queue, chan);
if (buf) {
err = bt_att_chan_send(chan, buf);
if (err) {
/* Push it back if it could not be send */
k_queue_prepend(&queue->_queue, buf);
return err;
}
return 0;
}
return -ENOENT;
}
/* Send requests without taking tx_sem */
static int chan_req_send(struct bt_att_chan *chan, struct bt_att_req *req)
{
struct net_buf *buf;
int err;
if (bt_att_mtu(chan) < net_buf_frags_len(req->buf)) {
return -EMSGSIZE;
}
LOG_DBG("chan %p req %p len %zu", chan, req, net_buf_frags_len(req->buf));
chan->req = req;
/* Release since bt_l2cap_send_cb takes ownership of the buffer */
buf = req->buf;
req->buf = NULL;
/* This lock makes sure the value of `bt_att_mtu(chan)` does not
* change.
*/
k_sched_lock();
err = bt_att_chan_send(chan, buf);
if (err) {
/* We still have the ownership of the buffer */
req->buf = buf;
chan->req = NULL;
} else {
bt_gatt_req_set_mtu(req, bt_att_mtu(chan));
}
k_sched_unlock();
return err;
}
static void bt_att_sent(struct bt_l2cap_chan *ch)
{
struct bt_att_chan *chan = ATT_CHAN(ch);
struct bt_att *att = chan->att;
int err;
LOG_DBG("chan %p", chan);
atomic_clear_bit(chan->flags, ATT_PENDING_SENT);
if (!att) {
LOG_DBG("Ignore sent on detached ATT chan");
return;
}
/* Process pending requests first since they require a response they
* can only be processed one at time while if other queues were
* processed before they may always contain a buffer starving the
* request queue.
*/
if (!chan->req && !sys_slist_is_empty(&att->reqs)) {
sys_snode_t *node = sys_slist_get(&att->reqs);
if (chan_req_send(chan, ATT_REQ(node)) >= 0) {
return;
}
/* Prepend back to the list as it could not be sent */
sys_slist_prepend(&att->reqs, node);
}
/* Process channel queue */
err = process_queue(chan, &chan->tx_queue);
if (!err) {
return;
}
/* Process global queue */
(void)process_queue(chan, &att->tx_queue);
}
static void chan_cfm_sent(struct bt_conn *conn, void *user_data, int err)
{
struct bt_att_tx_meta_data *data = user_data;
struct bt_att_chan *chan = data->att_chan;
LOG_DBG("chan %p", chan);
if (IS_ENABLED(CONFIG_BT_ATT_ENFORCE_FLOW)) {
atomic_clear_bit(chan->flags, ATT_PENDING_CFM);
}
tx_meta_data_free(data);
}
static void chan_rsp_sent(struct bt_conn *conn, void *user_data, int err)
{
struct bt_att_tx_meta_data *data = user_data;
struct bt_att_chan *chan = data->att_chan;
LOG_DBG("chan %p", chan);
if (IS_ENABLED(CONFIG_BT_ATT_ENFORCE_FLOW)) {
atomic_clear_bit(chan->flags, ATT_PENDING_RSP);
}
tx_meta_data_free(data);
}
static void chan_req_sent(struct bt_conn *conn, void *user_data, int err)
{
struct bt_att_tx_meta_data *data = user_data;
struct bt_att_chan *chan = data->att_chan;
LOG_DBG("chan %p chan->req %p", chan, chan->req);
/* Start timeout work */
if (chan->req) {
k_work_reschedule(&chan->timeout_work, BT_ATT_TIMEOUT);
}
tx_meta_data_free(user_data);
}
static void chan_tx_complete(struct bt_conn *conn, void *user_data, int err)
{
struct bt_att_tx_meta_data *data = user_data;
struct bt_att_chan *chan = data->att_chan;
bt_gatt_complete_func_t func = data->func;
uint16_t attr_count = data->attr_count;
void *ud = data->user_data;
LOG_DBG("TX Complete chan %p CID 0x%04X", chan, chan->chan.tx.cid);
tx_meta_data_free(data);
if (!err && func) {
for (uint16_t i = 0; i < attr_count; i++) {
func(conn, ud);
}
}
}
static void chan_unknown(struct bt_conn *conn, void *user_data, int err)
{
tx_meta_data_free(user_data);
}
static bt_conn_tx_cb_t chan_cb(const struct net_buf *buf)
{
const att_type_t op_type = att_op_get_type(buf->data[0]);
switch (op_type) {
case ATT_RESPONSE:
return chan_rsp_sent;
case ATT_CONFIRMATION:
return chan_cfm_sent;
case ATT_REQUEST:
case ATT_INDICATION:
return chan_req_sent;
case ATT_COMMAND:
case ATT_NOTIFICATION:
return chan_tx_complete;
default:
__ASSERT(false, "Unknown op type 0x%02X", op_type);
}
return chan_unknown;
}
static void att_cfm_sent(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
att_sent(conn, user_data);
}
chan_cfm_sent(conn, user_data, err);
}
static void att_rsp_sent(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
att_sent(conn, user_data);
}
chan_rsp_sent(conn, user_data, err);
}
static void att_req_sent(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
att_sent(conn, user_data);
}
chan_req_sent(conn, user_data, err);
}
static void att_tx_complete(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
att_sent(conn, user_data);
}
chan_tx_complete(conn, user_data, err);
}
static void att_unknown(struct bt_conn *conn, void *user_data, int err)
{
if (!err) {
att_sent(conn, user_data);
}
chan_unknown(conn, user_data, err);
}
static bt_conn_tx_cb_t att_cb(const struct net_buf *buf)
{
const att_type_t op_type = att_op_get_type(buf->data[0]);
switch (op_type) {
case ATT_RESPONSE:
return att_rsp_sent;
case ATT_CONFIRMATION:
return att_cfm_sent;
case ATT_REQUEST:
case ATT_INDICATION:
return att_req_sent;
case ATT_COMMAND:
case ATT_NOTIFICATION:
return att_tx_complete;
default:
__ASSERT(false, "Unknown op type 0x%02X", op_type);
}
return att_unknown;
}
static struct net_buf *bt_att_chan_create_pdu(struct bt_att_chan *chan, uint8_t op, size_t len)
{
struct bt_att_hdr *hdr;
struct net_buf *buf;
struct bt_att_tx_meta_data *data;
k_timeout_t timeout;
if (len + sizeof(op) > bt_att_mtu(chan)) {
LOG_WRN("ATT MTU exceeded, max %u, wanted %zu", bt_att_mtu(chan),
len + sizeof(op));
return NULL;
}
switch (att_op_get_type(op)) {
case ATT_RESPONSE:
case ATT_CONFIRMATION:
/* Use a timeout only when responding/confirming */
timeout = BT_ATT_TIMEOUT;
break;
default:
timeout = K_FOREVER;
}
buf = bt_l2cap_create_pdu_timeout(NULL, 0, timeout);
if (!buf) {
LOG_ERR("Unable to allocate buffer for op 0x%02x", op);
return NULL;
}
data = tx_meta_data_alloc(timeout);
if (!data) {
LOG_WRN("Unable to allocate ATT TX meta");
net_buf_unref(buf);
return NULL;
}
if (IS_ENABLED(CONFIG_BT_EATT)) {
net_buf_reserve(buf, BT_L2CAP_SDU_BUF_SIZE(0));
}
data->att_chan = chan;
bt_att_tx_meta_data(buf) = data;
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->code = op;
return buf;
}
static int bt_att_chan_send(struct bt_att_chan *chan, struct net_buf *buf)
{
LOG_DBG("chan %p flags %lu code 0x%02x", chan, atomic_get(chan->flags),
((struct bt_att_hdr *)buf->data)->code);
if (IS_ENABLED(CONFIG_BT_EATT) &&
!att_chan_matches_chan_opt(chan, bt_att_tx_meta_data(buf)->chan_opt)) {
return -EINVAL;
}
return chan_send(chan, buf);
}
static void att_send_process(struct bt_att *att)
{
struct bt_att_chan *chan, *tmp, *prev = NULL;
int err = 0;
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&att->chans, chan, tmp, node) {
if (err == -ENOENT && prev &&
(bt_att_is_enhanced(chan) == bt_att_is_enhanced(prev))) {
/* If there was nothing to send for the previous channel and the current
* channel has the same "enhancedness", there will be nothing to send for
* this channel either.
*/
continue;
}
err = process_queue(chan, &att->tx_queue);
if (!err) {
/* Success */
return;
}
prev = chan;
}
}
static void bt_att_chan_send_rsp(struct bt_att_chan *chan, struct net_buf *buf)
{
int err;
err = chan_send(chan, buf);
if (err) {
/* Responses need to be sent back using the same channel */
net_buf_put(&chan->tx_queue, buf);
}
}
static void send_err_rsp(struct bt_att_chan *chan, uint8_t req, uint16_t handle,
uint8_t err)
{
struct bt_att_error_rsp *rsp;
struct net_buf *buf;
/* Ignore opcode 0x00 */
if (!req) {
return;
}
buf = bt_att_chan_create_pdu(chan, BT_ATT_OP_ERROR_RSP, sizeof(*rsp));
if (!buf) {
return;
}
rsp = net_buf_add(buf, sizeof(*rsp));
rsp->request = req;
rsp->handle = sys_cpu_to_le16(handle);
rsp->error = err;
bt_att_chan_send_rsp(chan, buf);
}
static uint8_t att_mtu_req(struct bt_att_chan *chan, struct net_buf *buf)
{
struct bt_att_exchange_mtu_req *req;
struct bt_att_exchange_mtu_rsp *rsp;
struct net_buf *pdu;
uint16_t mtu_client, mtu_server;
/* Exchange MTU sub-procedure shall only be supported on the
* LE Fixed Channel Unenhanced ATT bearer.
*/
if (bt_att_is_enhanced(chan)) {
return BT_ATT_ERR_NOT_SUPPORTED;
}
req = (void *)buf->data;
mtu_client = sys_le16_to_cpu(req->mtu);
LOG_DBG("Client MTU %u", mtu_client);
/* Check if MTU is valid */
if (mtu_client < BT_ATT_DEFAULT_LE_MTU) {
return BT_ATT_ERR_INVALID_PDU;
}
pdu = bt_att_create_rsp_pdu(chan, BT_ATT_OP_MTU_RSP, sizeof(*rsp));
if (!pdu) {
return BT_ATT_ERR_UNLIKELY;
}
mtu_server = BT_LOCAL_ATT_MTU_UATT;
LOG_DBG("Server MTU %u", mtu_server);
rsp = net_buf_add(pdu, sizeof(*rsp));
rsp->mtu = sys_cpu_to_le16(mtu_server);
bt_att_chan_send_rsp(chan, pdu);
/* The ATT_EXCHANGE_MTU_REQ/RSP is just an alternative way of
* communicating the L2CAP MTU.
*/
chan->chan.rx.mtu = mtu_server;
chan->chan.tx.mtu = mtu_client;
LOG_DBG("Negotiated MTU %u", bt_att_mtu(chan));
#if defined(CONFIG_BT_GATT_CLIENT)
/* Mark the MTU Exchange as complete.
* This will skip sending ATT Exchange MTU from our side.
*
* Core 5.3 | Vol 3, Part F 3.4.2.2:
* If MTU is exchanged in one direction, that is sufficient for both directions.
*/
atomic_set_bit(chan->att->conn->flags, BT_CONN_ATT_MTU_EXCHANGED);
#endif /* CONFIG_BT_GATT_CLIENT */
att_chan_mtu_updated(chan);
return 0;
}
static int bt_att_chan_req_send(struct bt_att_chan *chan,
struct bt_att_req *req)
{
__ASSERT_NO_MSG(chan);
__ASSERT_NO_MSG(req);
__ASSERT_NO_MSG(req->func);
__ASSERT_NO_MSG(!chan->req);
LOG_DBG("req %p", req);
return chan_req_send(chan, req);
}
static void att_req_send_process(struct bt_att *att)
{
struct bt_att_req *req = NULL;
struct bt_att_chan *chan, *tmp, *prev = NULL;
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&att->chans, chan, tmp, node) {
/* If there is an ongoing transaction, do not use the channel */
if (chan->req) {
continue;
}
if (!req && prev && (bt_att_is_enhanced(chan) == bt_att_is_enhanced(prev))) {
/* If there was nothing to send for the previous channel and the current
* channel has the same "enhancedness", there will be nothing to send for
* this channel either.
*/
continue;
}
prev = chan;
/* Pull next request from the list */
req = get_first_req_matching_chan(&att->reqs, chan);
if (!req) {
continue;
}
if (bt_att_chan_req_send(chan, req) >= 0) {
return;
}
/* Prepend back to the list as it could not be sent */
sys_slist_prepend(&att->reqs, &req->node);
}
}
static uint8_t att_handle_rsp(struct bt_att_chan *chan, void *pdu, uint16_t len,
uint8_t err)
{
bt_att_func_t func = NULL;
void *params;
LOG_DBG("chan %p err 0x%02x len %u: %s", chan, err, len, bt_hex(pdu, len));
/* Cancel timeout if ongoing */
k_work_cancel_delayable(&chan->timeout_work);
if (!chan->req) {
LOG_WRN("No pending ATT request");
goto process;
}
/* Check if request has been cancelled */
if (chan->req == &cancel) {
chan->req = NULL;
goto process;
}
/* Reset func so it can be reused by the callback */
func = chan->req->func;
chan->req->func = NULL;
params = chan->req->user_data;
/* free allocated request so its memory can be reused */
bt_att_req_free(chan->req);
chan->req = NULL;
process:
/* Process pending requests */
att_req_send_process(chan->att);
if (func) {
func(chan->att->conn, err, pdu, len, params);
}
return 0;
}
#if defined(CONFIG_BT_GATT_CLIENT)
static uint8_t att_mtu_rsp(struct bt_att_chan *chan, struct net_buf *buf)
{
struct bt_att_exchange_mtu_rsp *rsp;
uint16_t mtu;
rsp = (void *)buf->data;
mtu = sys_le16_to_cpu(rsp->mtu);
LOG_DBG("Server MTU %u", mtu);
/* Check if MTU is valid */
if (mtu < BT_ATT_DEFAULT_LE_MTU) {
return att_handle_rsp(chan, NULL, 0, BT_ATT_ERR_INVALID_PDU);
}
/* The following must equal the value we sent in the req. We assume this
* is a rsp to `gatt_exchange_mtu_encode`.
*/
chan->chan.rx.mtu = BT_LOCAL_ATT_MTU_UATT;
/* The ATT_EXCHANGE_MTU_REQ/RSP is just an alternative way of
* communicating the L2CAP MTU.
*/
chan->chan.tx.mtu = mtu;
LOG_DBG("Negotiated MTU %u", bt_att_mtu(chan));
att_chan_mtu_updated(chan);
return att_handle_rsp(chan, rsp, buf->len, 0);
}
#endif /* CONFIG_BT_GATT_CLIENT */
static bool range_is_valid(uint16_t start, uint16_t end, uint16_t *err)
{
/* Handle 0 is invalid */
if (!start || !end) {
if (err) {
*err = 0U;
}
return false;
}
/* Check if range is valid */
if (start > end) {
if (err) {
*err = start;
}
return false;
}
return true;
}
struct find_info_data {
struct bt_att_chan *chan;
struct net_buf *buf;
struct bt_att_find_info_rsp *rsp;
union {
struct bt_att_info_16 *info16;
struct bt_att_info_128 *info128;
};
};
static uint8_t find_info_cb(const struct bt_gatt_attr *attr, uint16_t handle,
void *user_data)
{
struct find_info_data *data = user_data;
struct bt_att_chan *chan = data->chan;
LOG_DBG("handle 0x%04x", handle);