-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathl2cap.c
More file actions
1562 lines (1416 loc) · 52.5 KB
/
l2cap.c
File metadata and controls
1562 lines (1416 loc) · 52.5 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ogcsys.h>
#include <gccore.h>
#include "hci.h"
#include "l2cap.h"
#include "btmemb.h"
#include "btpbuf.h"
/* Next Identifier to be sent */
u8_t sigid_nxt;
/* The L2CAP PCB lists. */
struct l2cap_pcb_listen *l2cap_listen_pcbs = NULL; /* List of all L2CAP PCBs in CLOSED state
but awaiting an incoming conn req */
struct l2cap_pcb *l2cap_active_pcbs; /* List of all L2CAP PCBs that are in a
state in which they accept or send
data */
struct l2cap_pcb *l2cap_tmp_pcb = NULL;
/* Temp signal */
struct l2cap_sig *l2cap_tmp_sig = NULL;
/* Global variable involved in input processing of l2cap data segements */
struct l2cap_seg *l2cap_insegs = NULL;
struct l2cap_seg *l2cap_tmp_inseg = NULL;
/* Global Baseband disconnect callback. */
static void (*l2cap_disconnect_bb_cb)(struct bd_addr *bdaddr,u8_t reason) = NULL;
/* Forward declarations */
static u16_t l2cap_cid_alloc(void);
MEMB(l2cap_pcbs,sizeof(struct l2cap_pcb),MEMB_NUM_L2CAP_PCB);
MEMB(l2cap_listenpcbs,sizeof(struct l2cap_pcb_listen),MEMB_NUM_L2CAP_PCB_LISTEN);
MEMB(l2cap_sigs,sizeof(struct l2cap_sig),MEMB_NUM_L2CAP_SIG);
MEMB(l2cap_segs,sizeof(struct l2cap_seg),MEMB_NUM_L2CAP_SEG);
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_init():
*
* Initializes the L2CAP layer.
*/
/*-----------------------------------------------------------------------------------*/
void l2cap_init(void)
{
btmemb_init(&l2cap_pcbs);
btmemb_init(&l2cap_listenpcbs);
btmemb_init(&l2cap_sigs);
btmemb_init(&l2cap_segs);
/* Clear globals */
l2cap_listen_pcbs = NULL;
l2cap_active_pcbs = NULL;
l2cap_tmp_pcb = NULL;
l2cap_tmp_sig = NULL;
l2cap_insegs = NULL;
l2cap_tmp_inseg = NULL;
l2cap_disconnect_bb_cb = NULL;
/* Initialize the signal identifier (0x00 shall never be used) */
sigid_nxt = 0x00;
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_tmr():
*
* Called every 1s and implements the retransmission timer that
* removes a channel if it has been waiting for a request enough
* time. It also includes a configuration timer.
*/
/*-----------------------------------------------------------------------------------*/
void l2cap_tmr(void)
{
struct l2cap_sig *sig;
struct l2cap_pcb *pcb;
err_t ret;
(void) ret;
/* Step through all of the active pcbs */
for(pcb = l2cap_active_pcbs; pcb != NULL; pcb = pcb->next) {
/* Step through any unresponded signals */
for(sig = pcb->unrsp_sigs; sig != NULL; sig = sig->next) {
/* Check if channel is not reliable */
if(pcb->cfg.outflushto < 0xFFFF) {
/* Check if rtx is active. Otherwise ertx is active */
if(sig->rtx > 0) {
/* Adjust rtx timer */
--sig->rtx;
/* Check if rtx has expired */
if(sig->rtx == 0) {
if(sig->nrtx == 0) {
/* Move pcb to closed state */
pcb->state = L2CAP_CLOSED;
/* Indicate disconnect to upper layer */
LOG("l2cap_tmr: Max number of retransmissions (rtx) has expired\n");
L2CA_ACTION_DISCONN_IND(pcb,ERR_OK,ret);
} else {
--sig->nrtx;
/* Indicate timeout to upper layer */
L2CA_ACTION_TO_IND(pcb,ERR_OK,ret);
/* Retransmitt signal w timeout doubled */
sig->rtx += sig->rtx;
ret = l2cap_rexmit_signal(pcb, sig);
}
} /* if */
} else {
/* Adjust ertx timer */
--sig->ertx;
/* Check if ertx has expired */
if(sig->ertx == 0) {
if(sig->nrtx == 0) {
/* Move pcb to closed state */
pcb->state = L2CAP_CLOSED;
/* Indicate disconnect to upper layer */
LOG("l2cap_tmr: Max number of retransmissions (ertx) has expired\n");
L2CA_ACTION_DISCONN_IND(pcb,ERR_OK,ret);
} else {
--sig->nrtx;
/* Indicate timeout to upper layer */
L2CA_ACTION_TO_IND(pcb,ERR_OK,ret);
/* Disable ertx, activate rtx and retransmitt signal */
sig->ertx = 0;
sig->rtx = L2CAP_RTX;
ret = l2cap_rexmit_signal(pcb, sig);
}
} /* if */
} /* else */
} /* if */
} /* for */
/* Check configuration timer */
if(pcb->state == L2CAP_CONFIG) {
/* Check if configuration timer is active */
if(pcb->cfg.cfgto > 0) {
--pcb->cfg.cfgto;
//LOG("l2cap_tmr: Configuration timer = %d\n", pcb->cfg.cfgto);
/* Check if config timer has expired */
if(pcb->cfg.cfgto == 0) {
/* Connection attempt failed. Disconnect */
l2ca_disconnect_req(pcb, NULL);
/* Notify the application that the connection attempt failed */
if(pcb->cfg.l2capcfg & L2CAP_CFG_IR) {
L2CA_ACTION_CONN_CFM(pcb, L2CAP_CONN_CFG_TO, 0x0000, ret);
} else {
L2CA_ACTION_CONN_IND(pcb, ERR_OK, ret);
}
pcb->cfg.cfgto = L2CAP_CFG_TO; /* Reset timer */
}
}
}
} /* for */
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_write():
*
* Output L2CAP data to the lower layers. Segments the packet in to PDUs.
*/
/*-----------------------------------------------------------------------------------*/
err_t l2cap_write(struct bd_addr *bdaddr, struct pbuf *p, u16_t len)
{
u8_t pb = L2CAP_ACL_START;
u16_t maxsize;
u16_t outsize;
err_t ret = ERR_OK;
struct pbuf *q;
u16_t i = 0;
/*u16_t i;
struct pbuf *q;
for(q = p; q != NULL; q = q->next) {
for(i = 0; i < q->len; ++i) {
LWIP_DEBUGF(L2CAP_DEBUG, ("l2cap_write: 0x%x\n", ((u8_t *)q->payload)[i]));
}
LWIP_DEBUGF(L2CAP_DEBUG, ("l2cap_write: *\n"));
}
*/
maxsize = lp_pdu_maxsize();
q = p;
while(len && ret == ERR_OK) {
//LOG("l2cap_write: len %d maxsize %d p->len %d\n", len, maxsize, p->len);
if(len > maxsize) {
ret = lp_acl_write(bdaddr, q, maxsize, pb);
len -= maxsize;
outsize = maxsize;
//LOG("l2cap_write: Outsize before %d\n", outsize);
while(q->len < outsize) {
outsize -= q->len;
q = q->next;
}
//LOG("l2cap_write: Outsize after %d\n", outsize);
if(outsize) {
btpbuf_header(q, -outsize);
i += outsize;
}
pb = L2CAP_ACL_CONT;
LOG("l2cap_write: FRAG\n");
} else {
ret = lp_acl_write(bdaddr, q, len, pb);
len = 0;
}
}
btpbuf_header(q, i);
LOG("l2cap_write: DONE\n");
return ret;
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_process_sig():
*
* Parses the received message handles it.
*/
/*-----------------------------------------------------------------------------------*/
void l2cap_process_sig(struct pbuf *q, struct l2cap_hdr *l2caphdr, struct bd_addr *bdaddr)
{
struct l2cap_sig_hdr *sighdr;
struct l2cap_sig *sig = NULL;
struct l2cap_pcb *pcb = NULL;
struct l2cap_pcb_listen *lpcb;
struct l2cap_cfgopt_hdr *opthdr;
u16_t result, status, flags, psm, dcid;
u16_t len;
u16_t siglen;
struct pbuf *p, *r = NULL, *s = NULL, *data;
err_t ret;
u8_t i;
u16_t rspstate = L2CAP_CFG_SUCCESS;
(void)ret;
if(q->len != q->tot_len) {
LOG("l2cap_process_sig: Fragmented packet received. Reassemble into one buffer\n");
if((p = btpbuf_alloc(PBUF_RAW, q->tot_len, PBUF_RAM)) != NULL) {
i = 0;
for(r = q; r != NULL; r = r->next) {
memcpy(((u8_t *)p->payload) + i, r->payload, r->len);
i += r->len;
}
} else {
ERROR("l2cap_process_sig: Could not allocate buffer for fragmented packet\n");
return;
}
} else {
p = q;
}
len = l2caphdr->len;
while(len > 0) {
/* Set up signal header */
sighdr = p->payload;
btpbuf_header(p, -L2CAP_SIGHDR_LEN);
/* Check if this is a response/reject signal, and if so, find the matching request */
if(sighdr->code % 2) { /* if odd this is a resp/rej signal */
LOG("l2cap_process_sig: Response/reject signal received id = %d code = %d\n", sighdr->id, sighdr->code);
for(pcb = l2cap_active_pcbs; pcb != NULL; pcb = pcb->next) {
for(sig = pcb->unrsp_sigs; sig != NULL; sig = sig->next) {
if(sig->sigid == sighdr->id) {
break; /* found */
}
}
if(sig != NULL) {
break;
}
}
} else {
LOG("l2cap_process_sig: Request signal received id = %d code = %d\n", sighdr->id, sighdr->code);
}
/* Reject packet if length exceeds MTU */
if(l2caphdr->len > L2CAP_MTU) {
/* Alloc size of reason in cmd rej + MTU */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CMD_REJ_SIZE+2, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_MTU_EXCEEDED);
((u16_t *)data->payload)[1] = htole16(L2CAP_MTU);
l2cap_signal(NULL, L2CAP_CMD_REJ, sighdr->id, bdaddr, data);
}
break;
}
switch(sighdr->code) {
case L2CAP_CMD_REJ:
if(pcb == NULL) {
/* A rejection without a matching PCB is silently discarded */
break;
}
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
LOG("l2cap_process_sig: Our command was rejected so we disconnect\n");
l2ca_disconnect_req(pcb, NULL);
break;
case L2CAP_CONN_REQ:
psm = le16toh(((u16_t *)p->payload)[0]);
/* Search for a listening pcb */
for(lpcb = l2cap_listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
if(bd_addr_cmp(&(lpcb->bdaddr),bdaddr) && lpcb->psm == psm) {
/* Found a listening pcb with the correct PSM & BD Address */
break;
}
}
//printf("l2cap_process_sig(L2CAP_CONN_REQ): psm = %04x, lpcb = %p\n",psm,lpcb);
/* If no matching pcb was found, send a connection rsp neg (PSM) */
if(lpcb == NULL) {
/* Alloc size of data in conn rsp signal */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CONN_RSP_SIZE, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_CONN_REF_PSM);
((u16_t *)data->payload)[1] = 0; /* No further info available */
ret = l2cap_signal(NULL, L2CAP_CONN_RSP, sighdr->id, bdaddr, data);
}
} else {
/* Initiate a new active pcb */
pcb = l2cap_new();
if(pcb == NULL) {
LOG("l2cap_process_sig: could not allocate PCB\n");
/* Send a connection rsp neg (no resources available) and alloc size of data in conn rsp
signal */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CONN_RSP_SIZE, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_CONN_REF_RES);
((u16_t *)data->payload)[1] = 0; /* No further info available */
ret = l2cap_signal(NULL, L2CAP_CONN_RSP, sighdr->id, bdaddr, data);
}
}
bd_addr_set(&(pcb->remote_bdaddr),bdaddr);
pcb->scid = l2cap_cid_alloc();
pcb->dcid = le16toh(((u16_t *)p->payload)[1]);
pcb->psm = psm;
pcb->callback_arg = lpcb->callback_arg;
pcb->l2ca_connect_ind = lpcb->l2ca_connect_ind;
pcb->state = L2CAP_CONFIG;
L2CAP_REG(&l2cap_active_pcbs, pcb);
/* Free the listening pcb, don't need it anymore */
l2cap_close((struct l2cap_pcb *)lpcb);
LOG("l2cap_process_sig: A connection request was received. Send a response\n");
data = btpbuf_alloc(PBUF_RAW, L2CAP_CONN_RSP_SIZE, PBUF_RAM);
if(data == NULL) {
ERROR("l2cap_connect_rsp: Could not allocate memory for pbuf\n");
break;
}
((u16_t *)data->payload)[0] = htole16(pcb->scid);
((u16_t *)data->payload)[1] = htole16(pcb->dcid);
((u16_t *)data->payload)[2] = htole16(L2CAP_CONN_SUCCESS);
((u16_t *)data->payload)[3] = 0x0000; /* No further information available */
/* Send the response */
ret = l2cap_signal(pcb, L2CAP_CONN_RSP, sighdr->id, &(pcb->remote_bdaddr), data);
}
break;
case L2CAP_CONN_RSP:
if(pcb == NULL) {
/* A response without a matching request is silently discarded */
break;
}
LOG("l2cap_process_sig: conn rsp, active pcb->state == W4_L2CAP_CONNECT_RSP\n");
result = le16toh(((u16_t *)p->payload)[2]);
status = le16toh(((u16_t *)p->payload)[3]);
switch(result) {
case L2CAP_CONN_SUCCESS:
LOG("l2cap_process_sig: Conn_rsp_sucess, status %d\n", status);
LOG("l2cap_process_sig: conn rsp success, pcb->scid == %04x\n", ((u16_t *)p->payload)[1]);
/* Set destination connection id */
pcb->dcid = le16toh(((u16_t *)p->payload)[0]);
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
/* Configure connection */
pcb->state = L2CAP_CONFIG;
/* If initiator send a configuration request */
if(pcb->cfg.l2capcfg & L2CAP_CFG_IR) {
l2ca_config_req(pcb);
pcb->cfg.l2capcfg |= L2CAP_CFG_OUT_REQ;
}
break;
case L2CAP_CONN_PND:
LOG("l2cap_process_sig: Conn_rsp_pnd, status %d\n", status);
/* Disable rtx and enable ertx */
sig->rtx = 0;
sig->ertx = L2CAP_ERTX;
break;
default:
LOG("l2cap_process_sig: Conn_rsp_neg, result %d\n", result);
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
L2CA_ACTION_CONN_CFM(pcb,result,status,ret);
break;
}
break;
case L2CAP_CFG_REQ:
siglen = le16toh(sighdr->len);
dcid = le16toh(((u16_t *)p->payload)[0]);
flags = le16toh(((u16_t *)p->payload)[1]);
siglen -= 4;
btpbuf_header(p, -4);
LOG("l2cap_process_sig: Congfiguration request, flags = %d\n", flags);
/* Find PCB with matching cid */
for(pcb = l2cap_active_pcbs; pcb != NULL; pcb = pcb->next) {
//LOG("l2cap_process_sig: dcid = 0x%x, pcb->scid = 0x%x, pcb->dcid = 0x%x\n\n", dcid, pcb->scid, pcb->dcid);
if(pcb->scid == dcid) {
/* Matching cid found */
break;
}
}
/* If no matching cid was found, send a cmd reject (Invalid cid) */
if(pcb == NULL) {
LOG("l2cap_process_sig: Cfg req: no matching cid was found\n");
/* Alloc size of reason in cmd rej + data (dcid + scid) */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CMD_REJ_SIZE+4, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_INVALID_CID);
((u16_t *)data->payload)[1] = htole16(dcid); /* Requested local cid */
((u16_t *)data->payload)[2] = htole16(L2CAP_NULL_CID); /* Remote cid not known */
ret = l2cap_signal(NULL, L2CAP_CMD_REJ, sighdr->id, bdaddr, data);
}
} else { /* Handle config request */
LOG("l2cap_process_sig: Handle configuration request\n");
pcb->ursp_id = sighdr->id; /* Set id of request to respond to */
/* Parse options and add to pcb */
while(siglen > 0) {
LOG("l2cap_process_sig: Siglen = %d\n", siglen);
opthdr = p->payload;
/* Check if type of action bit indicates a non-hint. Hints are ignored */
LOG("l2cap_process_sig: Type of action bit = %d\n", L2CAP_OPTH_TOA(opthdr));
if(L2CAP_OPTH_TOA(opthdr) == 0) {
LOG("l2cap_process_sig: Type = %d\n", L2CAP_OPTH_TYPE(opthdr));
LOG("l2cap_process_sig: Length = %d\n", opthdr->len);
switch(L2CAP_OPTH_TYPE(opthdr)) {
case L2CAP_CFG_MTU:
LOG("l2cap_process_sig: Out MTU = %d\n", le16toh(((u16_t *)p->payload)[1]));
pcb->cfg.outmtu = le16toh(((u16_t *)p->payload)[1]);
break;
case L2CAP_FLUSHTO:
LOG("l2cap_process_sig: In flush timeout = %d\n", ((u16_t *)p->payload)[1]);
pcb->cfg.influshto = le16toh(((u16_t *)p->payload)[1]);
break;
case L2CAP_QOS:
/* If service type is Best Effort or No Traffic the remainder fields will be ignored */
if(((u8_t *)p->payload)[3] == L2CAP_QOS_GUARANTEED) {
/*LOG("l2cap_process_sig: This implementation does not support the guaranteed QOS service type");
if(rspstate == L2CAP_CFG_SUCCESS) {
rspstate = L2CAP_CFG_UNACCEPT;
if(pcb->cfg.opt != NULL) {
btpbuf_free(pcb->cfg.opt);
pcb->cfg.opt = NULL;
}
}*/
s = btpbuf_alloc(PBUF_RAW, L2CAP_CFGOPTHDR_LEN + opthdr->len, PBUF_RAM);
memcpy((u8_t *)s->payload, (u8_t *)p->payload, L2CAP_CFGOPTHDR_LEN + opthdr->len);
if(pcb->cfg.opt == NULL) {
pcb->cfg.opt = s;
} else {
btpbuf_chain(pcb->cfg.opt, s);
btpbuf_free(s);
}
}
break;
default:
if(rspstate != L2CAP_CFG_REJ) {
/* Unknown option. Add to unknown option type buffer */
if(rspstate != L2CAP_CFG_UNKNOWN) {
rspstate = L2CAP_CFG_UNKNOWN;
if(pcb->cfg.opt != NULL) {
btpbuf_free(pcb->cfg.opt);
pcb->cfg.opt = NULL;
}
}
s = btpbuf_alloc(PBUF_RAW, L2CAP_CFGOPTHDR_LEN + opthdr->len, PBUF_RAM);
memcpy((u8_t *)s->payload, (u8_t *)p->payload, L2CAP_CFGOPTHDR_LEN + opthdr->len);
if(pcb->cfg.opt == NULL) {
pcb->cfg.opt = s;
} else {
btpbuf_chain(pcb->cfg.opt, s);
btpbuf_free(s);
}
}
break;
} /* switch */
} /* if(L2CAP_OPTH_TOA(opthdr) == 0) */
btpbuf_header(p, -(L2CAP_CFGOPTHDR_LEN + opthdr->len));
siglen -= L2CAP_CFGOPTHDR_LEN + opthdr->len;
} /* while */
/* If continuation flag is set we don't send the final response just yet */
if((flags & 0x0001) == 1) {
/* Send success result with no options until the full request has been received */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CFG_RSP_SIZE, PBUF_RAM)) == NULL) {
ERROR("l2cap_process_sig: Could not allocate memory for pbuf\n");
break;
}
((u16_t *)data->payload)[0] = htole16(pcb->dcid);
((u16_t *)data->payload)[1] = 0;
((u16_t *)data->payload)[2] = htole16(L2CAP_CFG_SUCCESS);
ret = l2cap_signal(pcb, L2CAP_CFG_RSP, pcb->ursp_id, &(pcb->remote_bdaddr), data);
break;
}
/* Send a configure request for outgoing link if it hasnt been configured */
if(!(pcb->cfg.l2capcfg & L2CAP_CFG_IR) && !(pcb->cfg.l2capcfg & L2CAP_CFG_OUT_REQ)) {
l2ca_config_req(pcb);
pcb->cfg.l2capcfg |= L2CAP_CFG_OUT_REQ;
}
/* Send response to configuration request */
LOG("l2cap_process_sig: Send response to configuration request\n");
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CFG_RSP_SIZE, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(pcb->dcid);
((u16_t *)data->payload)[1] = 0; /* Flags (No continuation) */
((u16_t *)data->payload)[2] = htole16(rspstate); /* Result */
if(pcb->cfg.opt != NULL) {
LOG("l2cap_process_sig: pcb->cfg.opt->len = %d\n", pcb->cfg.opt->len);
btpbuf_chain(data, pcb->cfg.opt); /* Add option type buffer to data buffer */
btpbuf_free(pcb->cfg.opt);
pcb->cfg.opt = NULL;
}
ret = l2cap_signal(pcb, L2CAP_CFG_RSP, pcb->ursp_id, &(pcb->remote_bdaddr), data);
}
if(rspstate == L2CAP_CFG_SUCCESS) {
pcb->cfg.l2capcfg |= L2CAP_CFG_OUT_SUCCESS;
/* L2CAP connection established if a successful configuration response has been sent */
if(pcb->cfg.l2capcfg & L2CAP_CFG_IN_SUCCESS) {
/* IPCP connection established, notify upper layer that connection is open */
pcb->state = L2CAP_OPEN;
if(pcb->cfg.l2capcfg & L2CAP_CFG_IR) {
L2CA_ACTION_CONN_CFM(pcb, L2CAP_CONN_SUCCESS, 0x0000, ret);
} else {
L2CA_ACTION_CONN_IND(pcb, ERR_OK, ret);
}
}
}
} /* else */
break;
case L2CAP_CFG_RSP:
if(pcb == NULL) {
/* A response without a matching request is silently discarded */
LOG("l2cap_process_sig: discarded response without matching request\n");
break;
}
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
siglen = le16toh(sighdr->len);
//scid = le16toh(((u16_t *)p->payload)[0]);
flags = le16toh(((u16_t *)p->payload)[1]);
result = le16toh(((u16_t *)p->payload)[2]);
siglen -= 6;
btpbuf_header(p, -6);
LOG("l2cap_process_sig: Outgoing configuration result == %d continuation flag == %d\n", result, flags);
/* Handle config request */
switch(result) {
case L2CAP_CFG_SUCCESS:
LOG("l2cap_process_sig: Successfull outgoing configuration\n");
pcb->cfg.l2capcfg |= L2CAP_CFG_IN_SUCCESS; /* Local side of the connection
has been configured for outgoing data */
pcb->cfg.cfgto = L2CAP_CFG_TO; /* Reset configuration timeout */
if(pcb->cfg.outflushto != L2CAP_CFG_DEFAULT_OUTFLUSHTO) {
lp_write_flush_timeout(&pcb->remote_bdaddr, pcb->cfg.outflushto);
}
/* L2CAP connection established if a successful configuration response has been sent */
if(pcb->cfg.l2capcfg & L2CAP_CFG_OUT_SUCCESS) {
pcb->state = L2CAP_OPEN;
if(pcb->cfg.l2capcfg & L2CAP_CFG_IR) {
L2CA_ACTION_CONN_CFM(pcb, L2CAP_CONN_SUCCESS, 0x0000, ret);
} else {
L2CA_ACTION_CONN_IND(pcb, ERR_OK, ret);
}
}
break;
case L2CAP_CFG_UNACCEPT:
/* Parse and add options to pcb */
while(siglen > 0) {
opthdr = p->payload;
/* Check if type of action bit indicates a non-hint. Hints are ignored */
if(L2CAP_OPTH_TOA(opthdr) == 0) {
switch(L2CAP_OPTH_TYPE(opthdr)) {
case L2CAP_CFG_MTU:
if(L2CAP_MTU > le16toh(((u16_t *)p->payload)[1])) {
pcb->cfg.outmtu = le16toh(((u16_t *)p->payload)[1]);
} else {
ERROR("l2cap_process_sig: Configuration of MTU failed\n");
l2ca_disconnect_req(pcb, NULL);
return;
}
break;
case L2CAP_FLUSHTO:
pcb->cfg.influshto = le16toh(((u16_t *)p->payload)[1]);
break;
case L2CAP_QOS:
/* If service type Best Effort is not accepted we will close the connection */
if(((u8_t *)p->payload)[3] != L2CAP_QOS_BEST_EFFORT) {
ERROR("l2cap_process_sig: Unsupported service type\n");
l2ca_disconnect_req(pcb, NULL);
return;
}
break;
default:
/* Should not happen, skip option */
break;
} /* switch */
} /* if(L2CAP_OPTH_TOA(opthdr) == 0) */
btpbuf_header(p, -(L2CAP_CFGOPTHDR_LEN + opthdr->len));
siglen -= L2CAP_CFGOPTHDR_LEN + opthdr->len;
} /* while */
/* Send out a new configuration request if the continuation flag isn't set */
if((flags & 0x0001) == 0) {
l2ca_config_req(pcb);
}
break;
case L2CAP_CFG_REJ:
/* Fallthrough */
case L2CAP_CFG_UNKNOWN:
/* Fallthrough */
default:
if((flags & 0x0001) == 0) {
LOG("l2cap_process_sig: Configuration failed\n");
l2ca_disconnect_req(pcb, NULL);
return;
}
break;
} /* switch(result) */
/* If continuation flag is set we must send a NULL configuration request */
if((flags & 0x0001) == 1) {
LOG("l2cap_process_sig: Continuation flag is set. Send empty (default) config request signal\n");
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CFG_REQ_SIZE, PBUF_RAM)) == NULL) {
ERROR("l2cap_process_sig: Could not allocate memory for pbuf\n");
return;
}
/* Assemble config request packet */
((u16_t *)data->payload)[0] = htole16(pcb->scid);
((u16_t *)data->payload)[2] = 0;
l2cap_signal(pcb, L2CAP_CFG_REQ, 0, &(pcb->remote_bdaddr), data);
}
break;
case L2CAP_DISCONN_REQ:
siglen = le16toh(sighdr->len);
dcid = le16toh(((u16_t *)p->payload)[0]);
siglen = siglen - 2;
flags = le16toh(((u16_t *)p->payload)[1]);
siglen = siglen - 2;
btpbuf_header(p, -4);
/* Find PCB with matching cid */
for(pcb = l2cap_active_pcbs; pcb != NULL; pcb = pcb->next) {
if(pcb->scid == dcid) {
/* Matching cid found */
break;
}
}
/* If no matching cid was found, send a cmd reject (Invalid cid) */
if(pcb == NULL) {
/* Alloc size of reason in cmd rej + data (dcid + scid) */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CMD_REJ_SIZE+4, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_INVALID_CID);
((u16_t *)data->payload)[1] = htole16(dcid); /* Requested local cid */
((u16_t *)data->payload)[2] = htole16(L2CAP_NULL_CID); /* Remote cid not known */
ret = l2cap_signal(NULL, L2CAP_CMD_REJ, sighdr->id, bdaddr, data);
}
} else { /* Handle disconnection request */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_DISCONN_RSP_SIZE, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(pcb->scid);
((u16_t *)data->payload)[1] = htole16(pcb->dcid);
ret = l2cap_signal(pcb, L2CAP_DISCONN_RSP, sighdr->id, &(pcb->remote_bdaddr), data);
/* Give upper layer indication */
pcb->state = L2CAP_CLOSED;
LOG("l2cap_process_sig: Disconnection request\n");
L2CA_ACTION_DISCONN_IND(pcb,ERR_OK,ret);
}
}
break;
case L2CAP_DISCONN_RSP:
if(pcb == NULL) {
/* A response without a matching request is silently discarded */
break;
}
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
L2CA_ACTION_DISCONN_CFM(pcb,ret); /* NOTE: Application should
now close the connection */
break;
case L2CAP_ECHO_REQ:
pcb->ursp_id = sighdr->id;
ret = l2cap_signal(pcb, L2CAP_ECHO_RSP, sighdr->id, &(pcb->remote_bdaddr), NULL);
break;
case L2CAP_ECHO_RSP:
if(pcb == NULL) {
/* A response without a matching request is silently discarded */
break;
}
/* Remove signal from unresponded list and deallocate it */
L2CAP_SIG_RMV(&(pcb->unrsp_sigs), sig);
btpbuf_free(sig->p);
btmemb_free(&l2cap_sigs, sig);
/* Remove temporary pcb from active list */
L2CAP_RMV(&l2cap_active_pcbs, pcb);
L2CA_ACTION_PING_CFM(pcb,L2CAP_ECHO_RCVD,ret);
break;
default:
/* Alloc size of reason in cmd rej */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CMD_REJ_SIZE, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_CMD_NOT_UNDERSTOOD);
ret = l2cap_signal(NULL, L2CAP_CMD_REJ, sighdr->id, bdaddr, data);
}
break;
} /* switch */
len = len - (le16toh(sighdr->len) + L2CAP_SIGHDR_LEN);
btpbuf_header(p, -(le16toh(sighdr->len)));
} /* while */
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_input():
*
* Called by the lower layer. Reassembles the packet, parses the header and forward
* it to the upper layer or the signal handler.
*/
/*-----------------------------------------------------------------------------------*/
void l2cap_input(struct pbuf *p, struct bd_addr *bdaddr)
{
struct l2cap_seg *inseg;
struct hci_acl_hdr *aclhdr;
struct pbuf *data;
err_t ret;
(void)ret;
btpbuf_header(p, HCI_ACL_HDR_LEN);
aclhdr = p->payload;
btpbuf_header(p, -HCI_ACL_HDR_LEN);
btpbuf_realloc(p, aclhdr->len);
for(inseg = l2cap_insegs; inseg != NULL; inseg = inseg->next) {
if(bd_addr_cmp(bdaddr, &(inseg->bdaddr))) {
break;
}
}
aclhdr->connhdl_pb_bc = le16toh(aclhdr->connhdl_pb_bc);
aclhdr->len = le16toh(aclhdr->len);
/* Reassembly procedures */
/* Check if continuing fragment or start of L2CAP packet */
if(((aclhdr->connhdl_pb_bc >> 12) & 0x03)== L2CAP_ACL_CONT) { /* Continuing fragment */
if(inseg == NULL) {
/* Discard packet */
LOG("l2cap_input: Continuing fragment. Discard packet\n");
btpbuf_free(p);
return;
} else if(inseg->p->tot_len + p->tot_len > inseg->len) { /* Check if length of
segment exceeds
l2cap header length */
/* Discard packet */
LOG("l2cap_input: Continuing fragment. Length exceeds L2CAP hdr length. Discard packet\n");
btpbuf_free(inseg->p);
L2CAP_SEG_RMV(&(l2cap_insegs), inseg);
btmemb_free(&l2cap_segs, inseg);
btpbuf_free(p);
return;
}
/* Add pbuf to segement */
btpbuf_chain(inseg->p, p);
btpbuf_free(p);
} else if(((aclhdr->connhdl_pb_bc >> 12) & 0x03) == L2CAP_ACL_START) { /* Start of L2CAP packet */
//LOG("l2cap_input: Start of L2CAP packet p->len = %d, p->tot_len = %d\n", p->len, p->tot_len);
if(inseg != NULL) { /* Check if there are segments missing in a previous packet */
/* Discard previous packet */
LOG("l2cap_input: Start of L2CAP packet. Discard previous packet\n");
btpbuf_free(inseg->p);
} else {
inseg = btmemb_alloc(&l2cap_segs);
bd_addr_set(&(inseg->bdaddr), bdaddr);
L2CAP_SEG_REG(&(l2cap_insegs), inseg);
}
inseg->p = p;
inseg->l2caphdr = p->payload;
inseg->l2caphdr->cid = le16toh(inseg->l2caphdr->cid);
inseg->l2caphdr->len = le16toh(inseg->l2caphdr->len);
inseg->len = inseg->l2caphdr->len + L2CAP_HDR_LEN;
for(inseg->pcb = l2cap_active_pcbs; inseg->pcb != NULL; inseg->pcb = inseg->pcb->next) {
if(inseg->pcb->scid == inseg->l2caphdr->cid) {
break; /* found */
}
}
} else {
/* Discard packet */
LOG("l2cap_input: Discard packet\n");
btpbuf_free(inseg->p);
L2CAP_SEG_RMV(&(l2cap_insegs), inseg);
btmemb_free(&l2cap_segs, inseg);
btpbuf_free(p);
return;
}
if(inseg->p->tot_len < inseg->len) {
LOG("l2cap_input: Get continuing segments\n");
return; /* Get continuing segments */
}
/* Handle packet */
switch(inseg->l2caphdr->cid) {
case L2CAP_NULL_CID:
/* Illegal */
LOG("l2cap_input: Illegal null cid\n");
btpbuf_free(inseg->p);
break;
case L2CAP_SIG_CID:
btpbuf_header(inseg->p, -L2CAP_HDR_LEN);
l2cap_process_sig(inseg->p, inseg->l2caphdr, bdaddr);
btpbuf_free(inseg->p);
break;
case L2CAP_CONNLESS_CID:
/* Not needed by PAN, LAN access or DUN profiles */
btpbuf_free(inseg->p);
break;
default:
if(inseg->l2caphdr->cid < 0x0040 || inseg->pcb == NULL) {
/* Reserved for specific L2CAP functions or channel does not exist */
/* Alloc size of reason in cmd rej */
if((data = btpbuf_alloc(PBUF_RAW, L2CAP_CMD_REJ_SIZE+4, PBUF_RAM)) != NULL) {
((u16_t *)data->payload)[0] = htole16(L2CAP_INVALID_CID);
((u16_t *)data->payload)[1] = htole16(inseg->l2caphdr->cid);
((u16_t *)data->payload)[2] = htole16(L2CAP_NULL_CID);
ret = l2cap_signal(NULL, L2CAP_CMD_REJ, l2cap_next_sigid(), bdaddr, data);
}
btpbuf_free(inseg->p);
break;
}
btpbuf_header(inseg->p, -L2CAP_HDR_LEN);
/* Forward packet to higher layer */
LOG("l2cap_input: Forward packet to higher layer\n");
/*
LOG("l2cap_input: Remote BD address: 0x%x:0x%x:0x%x:0x%x:0x%x:0x%x\n",
inseg->pcb->remote_bdaddr.addr[5],
inseg->pcb->remote_bdaddr.addr[4],
inseg->pcb->remote_bdaddr.addr[3],
inseg->pcb->remote_bdaddr.addr[2],
inseg->pcb->remote_bdaddr.addr[1],
inseg->pcb->remote_bdaddr.addr[0]));
*/
L2CA_ACTION_RECV(inseg->pcb,inseg->p,ERR_OK,ret);
break;
}
/* Remove input segment */
L2CAP_SEG_RMV(&(l2cap_insegs), inseg);
btmemb_free(&l2cap_segs, inseg);
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_cid_alloc():
*
* Allocates a channel identifier (CID). They are local names representing a logical
* channel endpoint on the device.
*/
/*-----------------------------------------------------------------------------------*/
static u16_t l2cap_cid_alloc(void)
{
u16_t cid;
struct l2cap_pcb *pcb;
for (cid = L2CAP_MIN_CID; cid < L2CAP_MAX_CID; ++cid) {
for(pcb = l2cap_active_pcbs; pcb != NULL; pcb = pcb->next) {
if(pcb->scid == cid) {
break;
}
}
if(pcb == NULL) {
return cid;
}
}
return 0;
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_new():
*
* Creates a new L2CAP protocol control block but doesn't place it on
* any of the L2CAP PCB lists.
*/
/*-----------------------------------------------------------------------------------*/
struct l2cap_pcb* l2cap_new(void)
{
struct l2cap_pcb *pcb;
pcb = btmemb_alloc(&l2cap_pcbs);
if(pcb != NULL) {
memset(pcb, 0, sizeof(struct l2cap_pcb));
pcb->state = L2CAP_CLOSED;
/* Initialize configuration parameter options with default values */
/* Maximum Transmission Unit */
pcb->cfg.inmtu = L2CAP_MTU; /* The MTU that this implementation support */
pcb->cfg.outmtu = 672; /* Default MTU. Two Baseband DH5 packets minus the Baseband ACL headers and
L2CAP header. This can be set here since we will never send any signals
larger than the L2CAP sig MTU (48 bytes) before L2CAP has been configured
*/
/* Flush Timeout */
pcb->cfg.influshto = 0xFFFF;
pcb->cfg.outflushto = 0xFFFF;
pcb->cfg.cfgto = L2CAP_CFG_TO; /* Maximum time before terminating a negotiation.
Cfg shall not last more than 120s */
pcb->cfg.opt = NULL;
return pcb;
}
ERROR("l2cap_new: Could not allocate memory for pcb\n");
return NULL;
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_close():
*
* Closes the L2CAP protocol control block.
*/
/*-----------------------------------------------------------------------------------*/
err_t l2cap_close(struct l2cap_pcb *pcb)
{
struct l2cap_sig *tmpsig;
if(pcb->state == L2CAP_LISTEN) {
L2CAP_RMV((struct l2cap_pcb**)((void*)&(l2cap_listen_pcbs)), pcb);
btmemb_free(&l2cap_listenpcbs, pcb);
} else {
L2CAP_RMV(&(l2cap_active_pcbs), pcb);
/* Free any unresponded signals */
while(pcb->unrsp_sigs != NULL) {
tmpsig = pcb->unrsp_sigs;
pcb->unrsp_sigs = pcb->unrsp_sigs->next;
btmemb_free(&l2cap_sigs, tmpsig);
}
btmemb_free(&l2cap_pcbs, pcb);
}
pcb = NULL;
return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
/*
* l2cap_reset_all():
*
* Closes all active and listening L2CAP protocol control blocks.
*/