-
Notifications
You must be signed in to change notification settings - Fork 4
/
att.c
1425 lines (1069 loc) · 32.4 KB
/
att.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
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2010 Nokia Corporation
* Copyright (C) 2010 Marcel Holtmann <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "jni/bluetooth.h"
#if 0
#include <bluetooth/bluetooth.h>
#endif
#include "att.h"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
uint8_t is_response(uint8_t opcode) {
switch (opcode) {
case ATT_OP_ERROR:
case ATT_OP_MTU_RESP:
case ATT_OP_FIND_INFO_RESP:
case ATT_OP_FIND_BY_TYPE_RESP:
case ATT_OP_READ_BY_TYPE_RESP:
case ATT_OP_READ_RESP:
case ATT_OP_READ_BLOB_RESP:
case ATT_OP_READ_MULTI_RESP:
case ATT_OP_READ_BY_GROUP_RESP:
case ATT_OP_WRITE_RESP:
case ATT_OP_PREP_WRITE_RESP:
case ATT_OP_EXEC_WRITE_RESP:
case ATT_OP_HANDLE_CNF:
return 1;
}
return 0;
}
uint8_t opcode2expected(uint8_t opcode) {
switch (opcode) {
case ATT_OP_MTU_REQ:
return ATT_OP_MTU_RESP;
case ATT_OP_FIND_INFO_REQ:
return ATT_OP_FIND_INFO_RESP;
case ATT_OP_FIND_BY_TYPE_REQ:
return ATT_OP_FIND_BY_TYPE_RESP;
case ATT_OP_READ_BY_TYPE_REQ:
return ATT_OP_READ_BY_TYPE_RESP;
case ATT_OP_READ_REQ:
return ATT_OP_READ_RESP;
case ATT_OP_READ_BLOB_REQ:
return ATT_OP_READ_BLOB_RESP;
case ATT_OP_READ_MULTI_REQ:
return ATT_OP_READ_MULTI_RESP;
case ATT_OP_READ_BY_GROUP_REQ:
return ATT_OP_READ_BY_GROUP_RESP;
case ATT_OP_WRITE_REQ:
return ATT_OP_WRITE_RESP;
case ATT_OP_PREP_WRITE_REQ:
return ATT_OP_PREP_WRITE_RESP;
case ATT_OP_EXEC_WRITE_REQ:
return ATT_OP_EXEC_WRITE_RESP;
case ATT_OP_HANDLE_IND:
return ATT_OP_HANDLE_CNF;
}
return 0;
}
/* Attribute Protocol Opcodes */
const char *att_op2str(uint8_t op) {
switch (op) {
case ATT_OP_ERROR:
return "Error";
case ATT_OP_MTU_REQ:
return "MTU req";
case ATT_OP_MTU_RESP:
return "MTU resp";
case ATT_OP_FIND_INFO_REQ:
return "Find Information req";
case ATT_OP_FIND_INFO_RESP:
return "Find Information resp";
case ATT_OP_FIND_BY_TYPE_REQ:
return "Find By Type req";
case ATT_OP_FIND_BY_TYPE_RESP:
return "Find By Type resp";
case ATT_OP_READ_BY_TYPE_REQ:
return "Read By Type req";
case ATT_OP_READ_BY_TYPE_RESP:
return "Read By Type resp";
case ATT_OP_READ_REQ:
return "Read req";
case ATT_OP_READ_RESP:
return "Read resp";
case ATT_OP_READ_BLOB_REQ:
return "Read Blob req";
case ATT_OP_READ_BLOB_RESP:
return "Read Blob resp";
case ATT_OP_READ_MULTI_REQ:
return "Read Multi req";
case ATT_OP_READ_MULTI_RESP:
return "Read Multi resp";
case ATT_OP_READ_BY_GROUP_REQ:
return "Read By Group req";
case ATT_OP_READ_BY_GROUP_RESP:
return "Read By Group resp";
case ATT_OP_WRITE_REQ:
return "Write req";
case ATT_OP_WRITE_RESP:
return "Write resp";
case ATT_OP_WRITE_CMD:
return "Write cmd";
case ATT_OP_PREP_WRITE_REQ:
return "Prepare Write req";
case ATT_OP_PREP_WRITE_RESP:
return "Prepare Write resp";
case ATT_OP_EXEC_WRITE_REQ:
return "Exec Write req";
case ATT_OP_EXEC_WRITE_RESP:
return "Exec Write resp";
case ATT_OP_HANDLE_NOTIFY:
return "Handle notify";
case ATT_OP_HANDLE_IND:
return "Handle indicate";
case ATT_OP_HANDLE_CNF:
return "Handle CNF";
case ATT_OP_SIGNED_WRITE_CMD:
return "Signed Write Cmd";
default:
return "Unknown";
}
}
/* Attribute Protocol Error Codes */
const char *att_ecode2str(uint8_t status) {
switch (status) {
case ATT_ECODE_INVALID_HANDLE:
return "Invalid handle";
case ATT_ECODE_READ_NOT_PERM:
return "Attribute can't be read";
case ATT_ECODE_WRITE_NOT_PERM:
return "Attribute can't be written";
case ATT_ECODE_INVALID_PDU:
return "Attribute PDU was invalid";
case ATT_ECODE_AUTHENTICATION:
return "Attribute requires authentication before read/write";
case ATT_ECODE_REQ_NOT_SUPP:
return "Server doesn't support the request received";
case ATT_ECODE_INVALID_OFFSET:
return "Offset past the end of the attribute";
case ATT_ECODE_AUTHORIZATION:
return "Attribute requires authorization before read/write";
case ATT_ECODE_PREP_QUEUE_FULL:
return "Too many prepare writes have been queued";
case ATT_ECODE_ATTR_NOT_FOUND:
return "No attribute found within the given range";
case ATT_ECODE_ATTR_NOT_LONG:
return "Attribute can't be read/written using Read Blob Req";
case ATT_ECODE_INSUFF_ENCR_KEY_SIZE:
return "Encryption Key Size is insufficient";
case ATT_ECODE_INVAL_ATTR_VALUE_LEN:
return "Attribute value length is invalid";
case ATT_ECODE_UNLIKELY:
return "Request attribute has encountered an unlikely error";
case ATT_ECODE_INSUFF_ENC:
return "Encryption required before read/write";
case ATT_ECODE_UNSUPP_GRP_TYPE:
return "Attribute type is not a supported grouping attribute";
case ATT_ECODE_INSUFF_RESOURCES:
return "Insufficient Resources to complete the request";
case ATT_ECODE_IO:
return "Internal application error: I/O";
case ATT_ECODE_TIMEOUT:
return "A timeout occurred";
case ATT_ECODE_ABORTED:
return "The operation was aborted";
default:
return "Unexpected error code";
}
}
void att_data_list_free(struct att_data_list *list) {
if (list == NULL)
return;
if (list->data) {
int i;
for (i = 0; i < list->num; i++)
free(list->data[i]);
}
free(list->data);
free(list);
}
struct att_data_list *att_data_list_alloc(uint16_t num, uint16_t len) {
struct att_data_list *list;
int i;
if (len > UINT8_MAX)
return NULL;
list = malloc(sizeof(struct att_data_list));
list->len = len;
list->num = num;
list->data = malloc(sizeof(uint8_t *) * num);
for (i = 0; i < num; i++)
list->data[i] = malloc(sizeof(uint8_t) * len);
return list;
}
uint16_t enc_read_by_grp_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
uint8_t *pdu, size_t len) {
uint16_t min_len = sizeof(pdu[0]) + sizeof(start) + sizeof(end);
uint16_t length;
if (!uuid)
return 0;
if (uuid->type == BT_UUID16)
length = 2;
else if (uuid->type == BT_UUID128)
length = 16;
else
return 0;
if (len < min_len + length)
return 0;
pdu[0] = ATT_OP_READ_BY_GROUP_REQ;
att_put_u16(start, &pdu[1]);
att_put_u16(end, &pdu[3]);
att_put_uuid(*uuid, &pdu[5]);
return min_len + length;
}
uint16_t dec_read_by_grp_req(const uint8_t *pdu, size_t len, uint16_t *start,
uint16_t *end, bt_uuid_t *uuid) {
const size_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
if (pdu == NULL)
return 0;
if (start == NULL || end == NULL || uuid == NULL)
return 0;
if (pdu[0] != ATT_OP_READ_BY_GROUP_REQ)
return 0;
if (len < min_len + 2)
return 0;
*start = att_get_u16(&pdu[1]);
*end = att_get_u16(&pdu[3]);
if (len == min_len + 2)
*uuid = att_get_uuid16(&pdu[5]);
else
*uuid = att_get_uuid128(&pdu[5]);
return len;
}
uint16_t enc_read_by_grp_resp(struct att_data_list *list, uint8_t *pdu,
size_t len) {
int i;
uint16_t w;
uint8_t *ptr;
if (list == NULL)
return 0;
if (len < list->len + sizeof(uint8_t) * 2)
return 0;
pdu[0] = ATT_OP_READ_BY_GROUP_RESP;
pdu[1] = list->len;
ptr = &pdu[2];
for (i = 0, w = 2; i < list->num && w + list->len <= len; i++) {
memcpy(ptr, list->data[i], list->len);
ptr += list->len;
w += list->len;
}
return w;
}
struct att_data_list *dec_read_by_grp_resp(const uint8_t *pdu, size_t len) {
struct att_data_list *list;
const uint8_t *ptr;
uint16_t elen, num;
int i;
if (pdu[0] != ATT_OP_READ_BY_GROUP_RESP)
return NULL;
elen = pdu[1];
num = (len - 2) / elen;
list = att_data_list_alloc(num, elen);
if (list == NULL)
return NULL;
ptr = &pdu[2];
for (i = 0; i < num; i++) {
memcpy(list->data[i], ptr, list->len);
ptr += list->len;
}
return list;
}
uint16_t enc_find_by_type_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
const uint8_t *value, size_t vlen, uint8_t *pdu, size_t len) {
uint16_t min_len = sizeof(pdu[0]) + sizeof(start) + sizeof(end)
+ sizeof(uint16_t);
if (pdu == NULL)
return 0;
if (!uuid)
return 0;
if (uuid->type != BT_UUID16)
return 0;
if (len < min_len)
return 0;
if (vlen > len - min_len)
vlen = len - min_len;
pdu[0] = ATT_OP_FIND_BY_TYPE_REQ;
att_put_u16(start, &pdu[1]);
att_put_u16(end, &pdu[3]);
att_put_uuid16(*uuid, &pdu[5]);
if (vlen > 0) {
memcpy(&pdu[7], value, vlen);
return min_len + vlen;
}
return min_len;
}
uint16_t dec_find_by_type_req(const uint8_t *pdu, size_t len, uint16_t *start,
uint16_t *end, bt_uuid_t *uuid, uint8_t *value, size_t *vlen) {
size_t valuelen;
uint16_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end)
+ sizeof(uint16_t);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_FIND_BY_TYPE_REQ)
return 0;
/* First requested handle number */
if (start)
*start = att_get_u16(&pdu[1]);
/* Last requested handle number */
if (end)
*end = att_get_u16(&pdu[3]);
/* Always UUID16 */
if (uuid)
*uuid = att_get_uuid16(&pdu[5]);
valuelen = len - min_len;
/* Attribute value to find */
if (valuelen > 0 && value)
memcpy(value, pdu + min_len, valuelen);
if (vlen)
*vlen = valuelen;
return len;
}
uint16_t enc_read_by_type_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
uint8_t *pdu, size_t len) {
uint16_t min_len = sizeof(pdu[0]) + sizeof(start) + sizeof(end);
uint16_t length;
if (!uuid)
return 0;
if (uuid->type == BT_UUID16)
length = 2;
else if (uuid->type == BT_UUID128)
length = 16;
else
return 0;
if (len < min_len + length)
return 0;
pdu[0] = ATT_OP_READ_BY_TYPE_REQ;
att_put_u16(start, &pdu[1]);
att_put_u16(end, &pdu[3]);
att_put_uuid(*uuid, &pdu[5]);
return min_len + length;
}
uint16_t dec_read_by_type_req(const uint8_t *pdu, size_t len, uint16_t *start,
uint16_t *end, bt_uuid_t *uuid) {
const size_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
if (pdu == NULL)
return 0;
if (start == NULL || end == NULL || uuid == NULL)
return 0;
if (len < min_len + 2)
return 0;
if (pdu[0] != ATT_OP_READ_BY_TYPE_REQ)
return 0;
*start = att_get_u16(&pdu[1]);
*end = att_get_u16(&pdu[3]);
if (len == min_len + 2)
*uuid = att_get_uuid16(&pdu[5]);
else
*uuid = att_get_uuid128(&pdu[5]);
return len;
}
uint16_t enc_read_by_type_resp(struct att_data_list *list, uint8_t *pdu,
size_t len) {
uint8_t *ptr;
size_t i, w, l;
if (list == NULL)
return 0;
if (pdu == NULL)
return 0;
l = MIN(len - 2, list->len);
pdu[0] = ATT_OP_READ_BY_TYPE_RESP;
pdu[1] = l;
ptr = &pdu[2];
for (i = 0, w = 2; i < list->num && w + l <= len; i++) {
memcpy(ptr, list->data[i], l);
ptr += l;
w += l;
}
return w;
}
struct att_data_list *dec_read_by_type_resp(const uint8_t *pdu, size_t len) {
struct att_data_list *list;
const uint8_t *ptr;
uint16_t elen, num;
int i;
if (pdu[0] != ATT_OP_READ_BY_TYPE_RESP)
return NULL;
elen = pdu[1];
num = (len - 2) / elen;
list = att_data_list_alloc(num, elen);
if (list == NULL)
return NULL;
ptr = &pdu[2];
for (i = 0; i < num; i++) {
memcpy(list->data[i], ptr, list->len);
ptr += list->len;
}
return list;
}
uint16_t enc_write_cmd(uint16_t handle, const uint8_t *value, size_t vlen,
uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
if (vlen > len - min_len)
vlen = len - min_len;
pdu[0] = ATT_OP_WRITE_CMD;
att_put_u16(handle, &pdu[1]);
if (vlen > 0) {
memcpy(&pdu[3], value, vlen);
return min_len + vlen;
}
return min_len;
}
uint16_t dec_write_cmd(const uint8_t *pdu, size_t len, uint16_t *handle,
uint8_t *value, size_t *vlen) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
if (pdu == NULL)
return 0;
if (value == NULL || vlen == NULL || handle == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_WRITE_CMD)
return 0;
*handle = att_get_u16(&pdu[1]);
memcpy(value, pdu + min_len, len - min_len);
*vlen = len - min_len;
return len;
}
uint16_t enc_write_req(uint16_t handle, const uint8_t *value, size_t vlen,
uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
if (vlen > len - min_len)
vlen = len - min_len;
pdu[0] = ATT_OP_WRITE_REQ;
att_put_u16(handle, &pdu[1]);
if (vlen > 0) {
memcpy(&pdu[3], value, vlen);
return min_len + vlen;
}
return min_len;
}
uint16_t dec_write_req(const uint8_t *pdu, size_t len, uint16_t *handle,
uint8_t *value, size_t *vlen) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
if (pdu == NULL)
return 0;
if (value == NULL || vlen == NULL || handle == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_WRITE_REQ)
return 0;
*handle = att_get_u16(&pdu[1]);
*vlen = len - min_len;
if (*vlen > 0)
memcpy(value, pdu + min_len, *vlen);
return len;
}
uint16_t enc_write_resp(uint8_t *pdu) {
if (pdu == NULL)
return 0;
pdu[0] = ATT_OP_WRITE_RESP;
return sizeof(pdu[0]);
}
uint16_t dec_write_resp(const uint8_t *pdu, size_t len) {
if (pdu == NULL)
return 0;
if (pdu[0] != ATT_OP_WRITE_RESP)
return 0;
return len;
}
uint16_t enc_read_req(uint16_t handle, uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
pdu[0] = ATT_OP_READ_REQ;
att_put_u16(handle, &pdu[1]);
return min_len;
}
uint16_t enc_read_blob_req(uint16_t handle, uint16_t offset, uint8_t *pdu,
size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle) + sizeof(offset);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
pdu[0] = ATT_OP_READ_BLOB_REQ;
att_put_u16(handle, &pdu[1]);
att_put_u16(offset, &pdu[3]);
return min_len;
}
uint16_t dec_read_req(const uint8_t *pdu, size_t len, uint16_t *handle) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
if (pdu == NULL)
return 0;
if (handle == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_READ_REQ)
return 0;
*handle = att_get_u16(&pdu[1]);
return min_len;
}
uint16_t dec_read_blob_req(const uint8_t *pdu, size_t len, uint16_t *handle,
uint16_t *offset) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle) + sizeof(*offset);
if (pdu == NULL)
return 0;
if (handle == NULL)
return 0;
if (offset == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_READ_BLOB_REQ)
return 0;
*handle = att_get_u16(&pdu[1]);
*offset = att_get_u16(&pdu[3]);
return min_len;
}
uint16_t enc_read_resp(uint8_t *value, size_t vlen, uint8_t *pdu, size_t len) {
if (pdu == NULL)
return 0;
/* If the attribute value length is longer than the allowed PDU size,
* send only the octets that fit on the PDU. The remaining octets can
* be requested using the Read Blob Request. */
if (vlen > len - 1)
vlen = len - 1;
pdu[0] = ATT_OP_READ_RESP;
memcpy(pdu + 1, value, vlen);
return vlen + 1;
}
uint16_t enc_read_blob_resp(uint8_t *value, size_t vlen, uint16_t offset,
uint8_t *pdu, size_t len) {
if (pdu == NULL)
return 0;
vlen -= offset;
if (vlen > len - 1)
vlen = len - 1;
pdu[0] = ATT_OP_READ_BLOB_RESP;
memcpy(pdu + 1, &value[offset], vlen);
return vlen + 1;
}
ssize_t dec_read_resp(const uint8_t *pdu, size_t len, uint8_t *value,
size_t vlen) {
if (pdu == NULL)
return -EINVAL;
if (pdu[0] != ATT_OP_READ_RESP)
return -EINVAL;
if (value == NULL)
return len - 1;
if (vlen < (len - 1))
return -ENOBUFS;
memcpy(value, pdu + 1, len - 1);
return len - 1;
}
uint16_t enc_error_resp(uint8_t opcode, uint16_t handle, uint8_t status,
uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(opcode) + sizeof(handle)
+ sizeof(status);
uint16_t u16;
if (len < min_len)
return 0;
u16 = htobs(handle);
pdu[0] = ATT_OP_ERROR;
pdu[1] = opcode;
memcpy(&pdu[2], &u16, sizeof(u16));
pdu[4] = status;
return min_len;
}
uint16_t enc_find_info_req(uint16_t start, uint16_t end, uint8_t *pdu,
size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(start) + sizeof(end);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
pdu[0] = ATT_OP_FIND_INFO_REQ;
att_put_u16(start, &pdu[1]);
att_put_u16(end, &pdu[3]);
return min_len;
}
uint16_t dec_find_info_req(const uint8_t *pdu, size_t len, uint16_t *start,
uint16_t *end) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
if (start == NULL || end == NULL)
return 0;
if (pdu[0] != ATT_OP_FIND_INFO_REQ)
return 0;
*start = att_get_u16(&pdu[1]);
*end = att_get_u16(&pdu[3]);
return min_len;
}
uint16_t enc_find_info_resp(uint8_t format, struct att_data_list *list,
uint8_t *pdu, size_t len) {
uint8_t *ptr;
size_t i, w;
if (pdu == NULL)
return 0;
if (list == NULL)
return 0;
if (len < list->len + sizeof(uint8_t) * 2)
return 0;
pdu[0] = ATT_OP_FIND_INFO_RESP;
pdu[1] = format;
ptr = (void *) &pdu[2];
for (i = 0, w = 2; i < list->num && w + list->len <= len; i++) {
memcpy(ptr, list->data[i], list->len);
ptr += list->len;
w += list->len;
}
return w;
}
struct att_data_list *dec_find_info_resp(const uint8_t *pdu, size_t len,
uint8_t *format) {
struct att_data_list *list;
uint8_t *ptr;
uint16_t elen, num;
int i;
if (pdu == NULL)
return 0;
if (format == NULL)
return 0;
if (pdu[0] != ATT_OP_FIND_INFO_RESP)
return 0;
*format = pdu[1];
elen = sizeof(pdu[0]) + sizeof(*format);
if (*format == 0x01)
elen += 2;
else if (*format == 0x02)
elen += 16;
num = (len - 2) / elen;
ptr = (void *) &pdu[2];
list = att_data_list_alloc(num, elen);
if (list == NULL)
return NULL;
for (i = 0; i < num; i++) {
memcpy(list->data[i], ptr, list->len);
ptr += list->len;
}
return list;
}
uint16_t enc_notification(uint16_t handle, uint8_t *value, size_t vlen,
uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
if (pdu == NULL)
return 0;
if (len < (vlen + min_len))
return 0;
pdu[0] = ATT_OP_HANDLE_NOTIFY;
att_put_u16(handle, &pdu[1]);
memcpy(&pdu[3], value, vlen);
return vlen + min_len;
}
uint16_t enc_indication(uint16_t handle, uint8_t *value, size_t vlen,
uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
if (pdu == NULL)
return 0;
if (len < (vlen + min_len))
return 0;
pdu[0] = ATT_OP_HANDLE_IND;
att_put_u16(handle, &pdu[1]);
memcpy(&pdu[3], value, vlen);
return vlen + min_len;
}
uint16_t dec_indication(const uint8_t *pdu, size_t len, uint16_t *handle,
uint8_t *value, size_t vlen) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
uint16_t dlen;
if (pdu == NULL)
return 0;
if (pdu[0] != ATT_OP_HANDLE_IND)
return 0;
if (len < min_len)
return 0;
dlen = MIN(len - min_len, vlen);
if (handle)
*handle = att_get_u16(&pdu[1]);
memcpy(value, &pdu[3], dlen);
return dlen;
}
uint16_t enc_confirmation(uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
pdu[0] = ATT_OP_HANDLE_CNF;
return min_len;
}
uint16_t enc_mtu_req(uint16_t mtu, uint8_t *pdu, size_t len) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(mtu);
if (pdu == NULL)
return 0;
if (len < min_len)
return 0;
pdu[0] = ATT_OP_MTU_REQ;
att_put_u16(mtu, &pdu[1]);
return min_len;
}
uint16_t dec_mtu_req(const uint8_t *pdu, size_t len, uint16_t *mtu) {
const uint16_t min_len = sizeof(pdu[0]) + sizeof(*mtu);
if (pdu == NULL)
return 0;
if (mtu == NULL)
return 0;
if (len < min_len)
return 0;
if (pdu[0] != ATT_OP_MTU_REQ)