-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfcomm.c
More file actions
1922 lines (1623 loc) · 75.9 KB
/
Copy pathrfcomm.c
File metadata and controls
1922 lines (1623 loc) · 75.9 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
/*
* Copyright (C) 2009-2012 by Matthias Ringwald
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at btstack@ringwald.ch
*
*/
/*
* rfcomm.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // memcpy
#include <stdint.h>
#include <btstack/btstack.h>
#include <btstack/hci_cmds.h>
#include <btstack/utils.h>
#include <btstack/utils.h>
#include "btstack_memory.h"
#include "hci.h"
#include "hci_dump.h"
#include "debug.h"
#include "rfcomm.h"
// workaround for missing PRIxPTR on mspgcc (16/20-bit MCU)
#ifndef PRIxPTR
#if defined(__MSP430X__) && defined(__MSP430X_LARGE__)
#define PRIxPTR "lx"
#else
#define PRIxPTR "x"
#endif
#endif
// Control field values bit no. 1 2 3 4 PF 6 7 8
#define BT_RFCOMM_SABM 0x3F // 1 1 1 1 1 1 0 0
#define BT_RFCOMM_UA 0x73 // 1 1 0 0 1 1 1 0
#define BT_RFCOMM_DM 0x0F // 1 1 1 1 0 0 0 0
#define BT_RFCOMM_DM_PF 0x1F // 1 1 1 1 1 0 0 0
#define BT_RFCOMM_DISC 0x53 // 1 1 0 0 1 0 1 0
#define BT_RFCOMM_UIH 0xEF // 1 1 1 1 0 1 1 1
#define BT_RFCOMM_UIH_PF 0xFF // 1 1 1 1 0 1 1 1
// Multiplexer message types
#define BT_RFCOMM_CLD_CMD 0xC3
#define BT_RFCOMM_FCON_CMD 0xA3
#define BT_RFCOMM_FCON_RSP 0xA1
#define BT_RFCOMM_FCOFF_CMD 0x63
#define BT_RFCOMM_FCOFF_RSP 0x61
#define BT_RFCOMM_MSC_CMD 0xE3
#define BT_RFCOMM_MSC_RSP 0xE1
#define BT_RFCOMM_NSC_RSP 0x11
#define BT_RFCOMM_PN_CMD 0x83
#define BT_RFCOMM_PN_RSP 0x81
#define BT_RFCOMM_RLS_CMD 0x53
#define BT_RFCOMM_RLS_RSP 0x51
#define BT_RFCOMM_RPN_CMD 0x93
#define BT_RFCOMM_RPN_RSP 0x91
#define BT_RFCOMM_TEST_CMD 0x23
#define BT_RFCOMM_TEST_RSP 0x21
#define RFCOMM_MULIPLEXER_TIMEOUT_MS 60000
// FCS calc
#define BT_RFCOMM_CODE_WORD 0xE0 // pol = x8+x2+x1+1
#define BT_RFCOMM_CRC_CHECK_LEN 3
#define BT_RFCOMM_UIHCRC_CHECK_LEN 2
#include "l2cap.h"
// used for debugging
// #define RFCOMM_LOG_CREDITS
// global rfcomm data
static uint16_t rfcomm_client_cid_generator; // used for client channel IDs
// linked lists for all
static linked_list_t rfcomm_multiplexers = NULL;
static linked_list_t rfcomm_channels = NULL;
static linked_list_t rfcomm_services = NULL;
static void (*app_packet_handler)(void * connection, uint8_t packet_type,
uint16_t channel, uint8_t *packet, uint16_t size);
static void rfcomm_run(void);
static void rfcomm_hand_out_credits(void);
static void rfcomm_channel_state_machine(rfcomm_channel_t *channel, rfcomm_channel_event_t *event);
static void rfcomm_channel_state_machine_2(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, rfcomm_channel_event_t *event);
static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel);
static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event);
// MARK: RFCOMM CLIENT EVENTS
// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
static void rfcomm_emit_connection_request(rfcomm_channel_t *channel) {
log_info("RFCOMM_EVENT_INCOMING_CONNECTION addr %s channel #%u cid 0x%02x",
bd_addr_to_str(channel->multiplexer->remote_addr), channel->dlci>>1, channel->rfcomm_cid);
uint8_t event[11];
event[0] = RFCOMM_EVENT_INCOMING_CONNECTION;
event[1] = sizeof(event) - 2;
bt_flip_addr(&event[2], channel->multiplexer->remote_addr);
event[8] = channel->dlci >> 1;
bt_store_16(event, 9, channel->rfcomm_cid);
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
}
// API Change: BTstack-0.3.50x uses
// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
// next Cydia release will use SVN version of this
// data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
static void rfcomm_emit_channel_opened(rfcomm_channel_t *channel, uint8_t status) {
log_info("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE status 0x%x addr %s handle 0x%x channel #%u cid 0x%02x mtu %u",
status, bd_addr_to_str(channel->multiplexer->remote_addr), channel->multiplexer->con_handle,
channel->dlci>>1, channel->rfcomm_cid, channel->max_frame_size);
uint8_t event[16];
uint8_t pos = 0;
event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
event[pos++] = sizeof(event) - 2;
event[pos++] = status;
bt_flip_addr(&event[pos], channel->multiplexer->remote_addr); pos += 6;
bt_store_16(event, pos, channel->multiplexer->con_handle); pos += 2;
event[pos++] = channel->dlci >> 1;
bt_store_16(event, pos, channel->rfcomm_cid); pos += 2; // channel ID
bt_store_16(event, pos, channel->max_frame_size); pos += 2; // max frame size
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
}
static void rfcomm_emit_channel_open_failed_outgoing_memory(void * connection, bd_addr_t *addr, uint8_t server_channel){
log_info("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE BTSTACK_MEMORY_ALLOC_FAILED addr %s",
bd_addr_to_str(*addr));
uint8_t event[16];
uint8_t pos = 0;
event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
event[pos++] = sizeof(event) - 2;
event[pos++] = BTSTACK_MEMORY_ALLOC_FAILED;
bt_flip_addr(&event[pos], *addr); pos += 6;
bt_store_16(event, pos, 0); pos += 2;
event[pos++] = server_channel;
bt_store_16(event, pos, 0); pos += 2; // channel ID
bt_store_16(event, pos, 0); pos += 2; // max frame size
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
}
// data: event(8), len(8), creidts incoming(8), new credits incoming(8), credits outgoing(8)
static inline void rfcomm_emit_credit_status(rfcomm_channel_t * channel) {
#ifdef RFCOMM_LOG_CREDITS
log_info("RFCOMM_LOG_CREDITS incoming %u new_incoming %u outgoing %u", channel->credits_incoming, channel->new_credits_incoming, channel->credits_outgoing);
uint8_t event[5];
event[0] = 0x88;
event[1] = sizeof(event) - 2;
event[2] = channel->credits_incoming;
event[3] = channel->new_credits_incoming;
event[4] = channel->credits_outgoing;
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
#endif
}
// data: event(8), len(8), rfcomm_cid(16)
static void rfcomm_emit_channel_closed(rfcomm_channel_t * channel) {
log_info("RFCOMM_EVENT_CHANNEL_CLOSED cid 0x%02x", channel->rfcomm_cid);
uint8_t event[4];
event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
event[1] = sizeof(event) - 2;
bt_store_16(event, 2, channel->rfcomm_cid);
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
}
static void rfcomm_emit_credits(rfcomm_channel_t * channel, uint8_t credits) {
log_info("RFCOMM_EVENT_CREDITS cid 0x%02x credits %u", channel->rfcomm_cid, credits);
uint8_t event[5];
event[0] = RFCOMM_EVENT_CREDITS;
event[1] = sizeof(event) - 2;
bt_store_16(event, 2, channel->rfcomm_cid);
event[4] = credits;
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
}
static void rfcomm_emit_service_registered(void *connection, uint8_t status, uint8_t channel){
log_info("RFCOMM_EVENT_SERVICE_REGISTERED status 0x%x channel #%u", status, channel);
uint8_t event[4];
event[0] = RFCOMM_EVENT_SERVICE_REGISTERED;
event[1] = sizeof(event) - 2;
event[2] = status;
event[3] = channel;
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
(*app_packet_handler)(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
}
// MARK: RFCOMM MULTIPLEXER HELPER
static uint16_t rfcomm_max_frame_size_for_l2cap_mtu(uint16_t l2cap_mtu){
// Assume RFCOMM header with credits and single byte length field
uint16_t max_frame_size = l2cap_mtu - 5;
// single byte can denote len up to 127
if (max_frame_size > 127) {
max_frame_size--;
}
log_info("rfcomm_max_frame_size_for_l2cap_mtu: %u -> %u\n", l2cap_mtu, max_frame_size);
return max_frame_size;
}
static void rfcomm_multiplexer_initialize(rfcomm_multiplexer_t *multiplexer){
memset(multiplexer, 0, sizeof(rfcomm_multiplexer_t));
multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED;
multiplexer->l2cap_credits = 0;
multiplexer->send_dm_for_dlci = 0;
multiplexer->max_frame_size = rfcomm_max_frame_size_for_l2cap_mtu(l2cap_max_mtu());
}
static rfcomm_multiplexer_t * rfcomm_multiplexer_create_for_addr(bd_addr_t *addr){
// alloc structure
rfcomm_multiplexer_t * multiplexer = btstack_memory_rfcomm_multiplexer_get();
if (!multiplexer) return NULL;
// fill in
rfcomm_multiplexer_initialize(multiplexer);
BD_ADDR_COPY(&multiplexer->remote_addr, addr);
// add to services list
linked_list_add(&rfcomm_multiplexers, (linked_item_t *) multiplexer);
return multiplexer;
}
static rfcomm_multiplexer_t * rfcomm_multiplexer_for_addr(bd_addr_t *addr){
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_multiplexers; it ; it = it->next){
rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it);
if (BD_ADDR_CMP(addr, multiplexer->remote_addr) == 0) {
return multiplexer;
};
}
return NULL;
}
static rfcomm_multiplexer_t * rfcomm_multiplexer_for_l2cap_cid(uint16_t l2cap_cid) {
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_multiplexers; it ; it = it->next){
rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it);
if (multiplexer->l2cap_cid == l2cap_cid) {
return multiplexer;
};
}
return NULL;
}
static int rfcomm_multiplexer_has_channels(rfcomm_multiplexer_t * multiplexer){
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
if (channel->multiplexer == multiplexer) {
return 1;
}
}
return 0;
}
// MARK: RFCOMM CHANNEL HELPER
static void rfcomm_dump_channels(void){
#ifndef EMBEDDED
linked_item_t * it;
int channels = 0;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = (rfcomm_channel_t *) it;
log_info("Channel #%u: addr %p, state %u\n", channels, channel, channel->state);
channels++;
}
#endif
}
static void rfcomm_channel_initialize(rfcomm_channel_t *channel, rfcomm_multiplexer_t *multiplexer,
rfcomm_service_t *service, uint8_t server_channel){
// don't use 0 as channel id
if (rfcomm_client_cid_generator == 0) ++rfcomm_client_cid_generator;
// setup channel
memset(channel, 0, sizeof(rfcomm_channel_t));
channel->state = RFCOMM_CHANNEL_CLOSED;
channel->state_var = RFCOMM_CHANNEL_STATE_VAR_NONE;
channel->multiplexer = multiplexer;
channel->service = service;
channel->rfcomm_cid = rfcomm_client_cid_generator++;
channel->max_frame_size = multiplexer->max_frame_size;
channel->credits_incoming = 0;
channel->credits_outgoing = 0;
channel->packets_granted = 0;
// incoming flow control not active
channel->new_credits_incoming = 0x30;
channel->incoming_flow_control = 0;
if (service) {
// incoming connection
channel->outgoing = 0;
channel->dlci = (server_channel << 1) | multiplexer->outgoing;
if (channel->max_frame_size > service->max_frame_size) {
channel->max_frame_size = service->max_frame_size;
}
channel->incoming_flow_control = service->incoming_flow_control;
channel->new_credits_incoming = service->incoming_initial_credits;
} else {
// outgoing connection
channel->outgoing = 1;
channel->dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1);
}
}
// service == NULL -> outgoing channel
static rfcomm_channel_t * rfcomm_channel_create(rfcomm_multiplexer_t * multiplexer,
rfcomm_service_t * service, uint8_t server_channel){
log_info("rfcomm_channel_create for service %p, channel %u --- list of channels:\n", service, server_channel);
rfcomm_dump_channels();
// alloc structure
rfcomm_channel_t * channel = btstack_memory_rfcomm_channel_get();
if (!channel) return NULL;
// fill in
rfcomm_channel_initialize(channel, multiplexer, service, server_channel);
// add to services list
linked_list_add(&rfcomm_channels, (linked_item_t *) channel);
return channel;
}
static rfcomm_channel_t * rfcomm_channel_for_rfcomm_cid(uint16_t rfcomm_cid){
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
if (channel->rfcomm_cid == rfcomm_cid) {
return channel;
};
}
return NULL;
}
static rfcomm_channel_t * rfcomm_channel_for_multiplexer_and_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci){
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
if (channel->dlci == dlci && channel->multiplexer == multiplexer) {
return channel;
};
}
return NULL;
}
static rfcomm_service_t * rfcomm_service_for_channel(uint8_t server_channel){
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_services; it ; it = it->next){
rfcomm_service_t * service = ((rfcomm_service_t *) it);
if ( service->server_channel == server_channel){
return service;
};
}
return NULL;
}
// MARK: RFCOMM SEND
/**
* @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1
*/
static int rfcomm_send_packet_for_multiplexer(rfcomm_multiplexer_t *multiplexer, uint8_t address, uint8_t control, uint8_t credits, uint8_t *data, uint16_t len){
if (!l2cap_can_send_packet_now(multiplexer->l2cap_cid)) return BTSTACK_ACL_BUFFERS_FULL;
uint8_t * rfcomm_out_buffer = l2cap_get_outgoing_buffer();
uint16_t pos = 0;
uint8_t crc_fields = 3;
rfcomm_out_buffer[pos++] = address;
rfcomm_out_buffer[pos++] = control;
// length field can be 1 or 2 octets
if (len < 128){
rfcomm_out_buffer[pos++] = (len << 1)| 1; // bits 0-6
} else {
rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6
rfcomm_out_buffer[pos++] = len >> 7; // bits 7-14
crc_fields++;
}
// add credits for UIH frames when PF bit is set
if (control == BT_RFCOMM_UIH_PF){
rfcomm_out_buffer[pos++] = credits;
}
// copy actual data
if (len) {
memcpy(&rfcomm_out_buffer[pos], data, len);
pos += len;
}
// UIH frames only calc FCS over address + control (5.1.1)
if ((control & 0xef) == BT_RFCOMM_UIH){
crc_fields = 2;
}
rfcomm_out_buffer[pos++] = crc8_calc(rfcomm_out_buffer, crc_fields); // calc fcs
int credits_taken = 0;
if (multiplexer->l2cap_credits){
credits_taken++;
multiplexer->l2cap_credits--;
} else {
log_info( "rfcomm_send_packet addr %02x, ctrl %02x size %u without l2cap credits\n", address, control, pos);
}
int err = l2cap_send_prepared(multiplexer->l2cap_cid, pos);
if (err) {
// undo credit counting
multiplexer->l2cap_credits += credits_taken;
}
return err;
}
// C/R Flag in Address
// - terms: initiator = station that creates multiplexer with SABM
// - terms: responder = station that responds to multiplexer setup with UA
// "For SABM, UA, DM and DISC frames C/R bit is set according to Table 1 in GSM 07.10, section 5.2.1.2"
// - command initiator = 1 /response responder = 1
// - command responder = 0 /response initiator = 0
// "For UIH frames, the C/R bit is always set according to section 5.4.3.1 in GSM 07.10.
// This applies independently of what is contained wthin the UIH frames, either data or control messages."
// - c/r = 1 for frames by initiating station, 0 = for frames by responding station
// C/R Flag in Message
// "In the message level, the C/R bit in the command type field is set as stated in section 5.4.6.2 in GSM 07.10."
// - If the C/R bit is set to 1 the message is a command
// - if it is set to 0 the message is a response.
// temp/old messge construction
// new object oriented version
static int rfcomm_send_sabm(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_SABM, 0, NULL, 0);
}
static int rfcomm_send_disc(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DISC, 0, NULL, 0);
}
static int rfcomm_send_ua(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UA, 0, NULL, 0);
}
static int rfcomm_send_dm_pf(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DM_PF, 0, NULL, 0);
}
static int rfcomm_send_uih_msc_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) {
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
uint8_t payload[4];
uint8_t pos = 0;
payload[pos++] = BT_RFCOMM_MSC_CMD;
payload[pos++] = 2 << 1 | 1; // len
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
payload[pos++] = signals;
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
}
static int rfcomm_send_uih_msc_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) {
uint8_t address = (1 << 0) | (multiplexer->outgoing<< 1);
uint8_t payload[4];
uint8_t pos = 0;
payload[pos++] = BT_RFCOMM_MSC_RSP;
payload[pos++] = 2 << 1 | 1; // len
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
payload[pos++] = signals;
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
}
static int rfcomm_send_uih_pn_command(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint16_t max_frame_size){
uint8_t payload[10];
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
uint8_t pos = 0;
payload[pos++] = BT_RFCOMM_PN_CMD;
payload[pos++] = 8 << 1 | 1; // len
payload[pos++] = dlci;
payload[pos++] = 0xf0; // pre-defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM
payload[pos++] = 0; // priority
payload[pos++] = 0; // max 60 seconds ack
payload[pos++] = max_frame_size & 0xff; // max framesize low
payload[pos++] = max_frame_size >> 8; // max framesize high
payload[pos++] = 0x00; // number of retransmissions
payload[pos++] = 0x00; // (unused error recovery window) initial number of credits
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
}
// "The response may not change the DLCI, the priority, the convergence layer, or the timer value." RFCOMM-tutorial.pdf
static int rfcomm_send_uih_pn_response(rfcomm_multiplexer_t *multiplexer, uint8_t dlci,
uint8_t priority, uint16_t max_frame_size){
uint8_t payload[10];
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
uint8_t pos = 0;
payload[pos++] = BT_RFCOMM_PN_RSP;
payload[pos++] = 8 << 1 | 1; // len
payload[pos++] = dlci;
payload[pos++] = 0xe0; // pre defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM
payload[pos++] = priority; // priority
payload[pos++] = 0; // max 60 seconds ack
payload[pos++] = max_frame_size & 0xff; // max framesize low
payload[pos++] = max_frame_size >> 8; // max framesize high
payload[pos++] = 0x00; // number of retransmissions
payload[pos++] = 0x00; // (unused error recovery window) initial number of credits
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
}
static int rfcomm_send_uih_rpn_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, rfcomm_rpn_data_t *rpn_data) {
uint8_t payload[10];
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
uint8_t pos = 0;
payload[pos++] = BT_RFCOMM_RPN_RSP;
payload[pos++] = 8 << 1 | 1; // len
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
payload[pos++] = rpn_data->baud_rate;
payload[pos++] = rpn_data->flags;
payload[pos++] = rpn_data->flow_control;
payload[pos++] = rpn_data->xon;
payload[pos++] = rpn_data->xoff;
payload[pos++] = rpn_data->parameter_mask_0;
payload[pos++] = rpn_data->parameter_mask_1;
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
}
static int rfcomm_send_uih_data(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t *data, uint16_t len){
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2);
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, data, len);
}
static void rfcomm_send_uih_credits(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t credits){
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2);
rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH_PF, credits, NULL, 0);
}
// MARK: RFCOMM MULTIPLEXER
static void rfcomm_multiplexer_stop_timer(rfcomm_multiplexer_t * multiplexer){
if (multiplexer->timer_active) {
run_loop_remove_timer(&multiplexer->timer);
multiplexer->timer_active = 0;
}
}
static void rfcomm_multiplexer_free(rfcomm_multiplexer_t * multiplexer){
linked_list_remove( &rfcomm_multiplexers, (linked_item_t *) multiplexer);
btstack_memory_rfcomm_multiplexer_free(multiplexer);
}
static void rfcomm_multiplexer_finalize(rfcomm_multiplexer_t * multiplexer){
// remove (potential) timer
rfcomm_multiplexer_stop_timer(multiplexer);
// close and remove all channels
linked_item_t *it = (linked_item_t *) &rfcomm_channels;
while (it->next){
rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next;
if (channel->multiplexer == multiplexer) {
// emit appropriate events
if (channel->state == RFCOMM_CHANNEL_OPEN) {
rfcomm_emit_channel_closed(channel);
} else {
rfcomm_emit_channel_opened(channel, RFCOMM_MULTIPLEXER_STOPPED);
}
// remove from list
it->next = it->next->next;
// free channel struct
btstack_memory_rfcomm_channel_free(channel);
} else {
it = it->next;
}
}
// keep reference to l2cap channel
uint16_t l2cap_cid = multiplexer->l2cap_cid;
// remove mutliplexer
rfcomm_multiplexer_free(multiplexer);
// close l2cap multiplexer channel, too
l2cap_disconnect_internal(l2cap_cid, 0x13);
}
static void rfcomm_multiplexer_timer_handler(timer_source_t *timer){
rfcomm_multiplexer_t * multiplexer = (rfcomm_multiplexer_t *) linked_item_get_user( (linked_item_t *) timer);
if (!rfcomm_multiplexer_has_channels(multiplexer)){
log_info( "rfcomm_multiplexer_timer_handler timeout: shutting down multiplexer!\n");
rfcomm_multiplexer_finalize(multiplexer);
}
}
static void rfcomm_multiplexer_prepare_idle_timer(rfcomm_multiplexer_t * multiplexer){
if (multiplexer->timer_active) {
run_loop_remove_timer(&multiplexer->timer);
multiplexer->timer_active = 0;
}
if (!rfcomm_multiplexer_has_channels(multiplexer)){
// start timer for multiplexer timeout check
run_loop_set_timer(&multiplexer->timer, RFCOMM_MULIPLEXER_TIMEOUT_MS);
multiplexer->timer.process = rfcomm_multiplexer_timer_handler;
linked_item_set_user((linked_item_t*) &multiplexer->timer, multiplexer);
run_loop_add_timer(&multiplexer->timer);
multiplexer->timer_active = 1;
}
}
static void rfcomm_multiplexer_opened(rfcomm_multiplexer_t *multiplexer){
log_info("Multiplexer up and running\n");
multiplexer->state = RFCOMM_MULTIPLEXER_OPEN;
rfcomm_channel_event_t event = { CH_EVT_MULTIPLEXER_READY };
// transition of channels that wait for multiplexer
linked_item_t *it;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
if (channel->multiplexer != multiplexer) continue;
rfcomm_channel_state_machine(channel, &event);
}
rfcomm_run();
rfcomm_multiplexer_prepare_idle_timer(multiplexer);
}
/**
* @return handled packet
*/
static int rfcomm_multiplexer_hci_event_handler(uint8_t *packet, uint16_t size){
bd_addr_t event_addr;
uint16_t psm;
uint16_t l2cap_cid;
hci_con_handle_t con_handle;
rfcomm_multiplexer_t *multiplexer = NULL;
uint8_t status;
switch (packet[0]) {
// accept incoming PSM_RFCOMM connection if no multiplexer exists yet
case L2CAP_EVENT_INCOMING_CONNECTION:
// data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16)
bt_flip_addr(event_addr, &packet[2]);
con_handle = READ_BT_16(packet, 8);
psm = READ_BT_16(packet, 10);
l2cap_cid = READ_BT_16(packet, 12);
if (psm != PSM_RFCOMM) break;
multiplexer = rfcomm_multiplexer_for_addr(&event_addr);
if (multiplexer) {
log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_RFCOMM => decline - multiplexer already exists", l2cap_cid);
l2cap_decline_connection_internal(l2cap_cid, 0x04); // no resources available
return 1;
}
// create and inititialize new multiplexer instance (incoming)
multiplexer = rfcomm_multiplexer_create_for_addr(&event_addr);
if (!multiplexer){
log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_RFCOMM => decline - no memory left", l2cap_cid);
l2cap_decline_connection_internal(l2cap_cid, 0x04); // no resources available
return 1;
}
multiplexer->con_handle = con_handle;
multiplexer->l2cap_cid = l2cap_cid;
multiplexer->state = RFCOMM_MULTIPLEXER_W4_SABM_0;
log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_RFCOMM => accept", l2cap_cid);
l2cap_accept_connection_internal(l2cap_cid);
return 1;
// l2cap connection opened -> store l2cap_cid, remote_addr
case L2CAP_EVENT_CHANNEL_OPENED:
if (READ_BT_16(packet, 11) != PSM_RFCOMM) break;
status = packet[2];
log_info("L2CAP_EVENT_CHANNEL_OPENED for PSM_RFCOMM, status %u\n", status);
// get multiplexer for remote addr
con_handle = READ_BT_16(packet, 9);
l2cap_cid = READ_BT_16(packet, 13);
bt_flip_addr(event_addr, &packet[3]);
multiplexer = rfcomm_multiplexer_for_addr(&event_addr);
if (!multiplexer) {
log_error("L2CAP_EVENT_CHANNEL_OPENED but no multiplexer prepared\n");
return 1;
}
// on l2cap open error discard everything
if (status){
// remove (potential) timer
rfcomm_multiplexer_stop_timer(multiplexer);
// emit rfcomm_channel_opened with status and free channel
linked_item_t * it = (linked_item_t *) &rfcomm_channels;
while (it->next) {
rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next;
if (channel->multiplexer == multiplexer){
rfcomm_emit_channel_opened(channel, status);
it->next = it->next->next;
btstack_memory_rfcomm_channel_free(channel);
} else {
it = it->next;
}
}
// free multiplexer
rfcomm_multiplexer_free(multiplexer);
return 1;
}
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_CONNECT) {
log_info("L2CAP_EVENT_CHANNEL_OPENED: outgoing connection\n");
// wrong remote addr
if (BD_ADDR_CMP(event_addr, multiplexer->remote_addr)) break;
multiplexer->l2cap_cid = l2cap_cid;
multiplexer->con_handle = con_handle;
// send SABM #0
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_SABM_0;
} else { // multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0
// set max frame size based on l2cap MTU
multiplexer->max_frame_size = rfcomm_max_frame_size_for_l2cap_mtu(READ_BT_16(packet, 17));
}
return 1;
// l2cap disconnect -> state = RFCOMM_MULTIPLEXER_CLOSED;
case L2CAP_EVENT_CREDITS:
// data: event(8), len(8), local_cid(16), credits(8)
l2cap_cid = READ_BT_16(packet, 2);
multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid);
if (!multiplexer) break;
multiplexer->l2cap_credits += packet[4];
// log_info("L2CAP_EVENT_CREDITS: %u (now %u)\n", packet[4], multiplexer->l2cap_credits);
// new credits, continue with signaling
rfcomm_run();
if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) break;
rfcomm_hand_out_credits();
return 1;
case DAEMON_EVENT_HCI_PACKET_SENT:
// testing DMA done code
rfcomm_run();
break;
case L2CAP_EVENT_CHANNEL_CLOSED:
// data: event (8), len(8), channel (16)
l2cap_cid = READ_BT_16(packet, 2);
multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid);
if (!multiplexer) break;
switch (multiplexer->state) {
case RFCOMM_MULTIPLEXER_W4_SABM_0:
case RFCOMM_MULTIPLEXER_W4_UA_0:
case RFCOMM_MULTIPLEXER_OPEN:
rfcomm_multiplexer_finalize(multiplexer);
return 1;
default:
break;
}
break;
default:
break;
}
return 0;
}
static int rfcomm_multiplexer_l2cap_packet_handler(uint16_t channel, uint8_t *packet, uint16_t size){
// get or create a multiplexer for a certain device
rfcomm_multiplexer_t *multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel);
if (!multiplexer) return 0;
// but only care for multiplexer control channel
uint8_t frame_dlci = packet[0] >> 2;
if (frame_dlci) return 0;
const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3
const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames
const uint8_t payload_offset = 3 + length_offset + credit_offset;
switch (packet[1]){
case BT_RFCOMM_SABM:
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0){
log_info("Received SABM #0\n");
multiplexer->outgoing = 0;
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_UA_0;
return 1;
}
break;
case BT_RFCOMM_UA:
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_UA_0) {
// UA #0 -> send UA #0, state = RFCOMM_MULTIPLEXER_OPEN
log_info("Received UA #0 \n");
rfcomm_multiplexer_opened(multiplexer);
return 1;
}
break;
case BT_RFCOMM_DISC:
// DISC #0 -> send UA #0, close multiplexer
log_info("Received DISC #0, (ougoing = %u)\n", multiplexer->outgoing);
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC;
return 1;
case BT_RFCOMM_DM:
// DM #0 - we shouldn't get this, just give up
log_info("Received DM #0\n");
log_info("-> Closing down multiplexer\n");
rfcomm_multiplexer_finalize(multiplexer);
return 1;
case BT_RFCOMM_UIH:
if (packet[payload_offset] == BT_RFCOMM_CLD_CMD){
// Multiplexer close down (CLD) -> close mutliplexer
log_info("Received Multiplexer close down command\n");
log_info("-> Closing down multiplexer\n");
rfcomm_multiplexer_finalize(multiplexer);
return 1;
}
break;
default:
break;
}
return 0;
}
static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event){
// process stored DM responses
if (multiplexer->send_dm_for_dlci){
rfcomm_send_dm_pf(multiplexer, multiplexer->send_dm_for_dlci);
multiplexer->send_dm_for_dlci = 0;
}
switch (multiplexer->state) {
case RFCOMM_MULTIPLEXER_SEND_SABM_0:
switch (event) {
case MULT_EV_READY_TO_SEND:
log_info("Sending SABM #0 - (multi 0x%p)\n", multiplexer);
multiplexer->state = RFCOMM_MULTIPLEXER_W4_UA_0;
rfcomm_send_sabm(multiplexer, 0);
break;
default:
break;
}
break;
case RFCOMM_MULTIPLEXER_SEND_UA_0:
switch (event) {
case MULT_EV_READY_TO_SEND:
log_info("Sending UA #0\n");
multiplexer->state = RFCOMM_MULTIPLEXER_OPEN;
rfcomm_send_ua(multiplexer, 0);
rfcomm_multiplexer_opened(multiplexer);
break;
default:
break;
}
break;
case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC:
switch (event) {
case MULT_EV_READY_TO_SEND:
log_info("Sending UA #0\n");
log_info("Closing down multiplexer\n");
multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED;
rfcomm_send_ua(multiplexer, 0);
rfcomm_multiplexer_finalize(multiplexer);
// try to detect authentication errors: drop link key if multiplexer closed before first channel got opened
if (!multiplexer->at_least_one_connection){
log_info("TODO: no connections established - delete link key prophylactically\n");
// hci_send_cmd(&hci_delete_stored_link_key, multiplexer->remote_addr);
}
default:
break;
}
break;
default:
break;
}
}
// MARK: RFCOMM CHANNEL
static void rfcomm_hand_out_credits(void){
linked_item_t * it;
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
rfcomm_channel_t * channel = (rfcomm_channel_t *) it;
if (channel->state != RFCOMM_CHANNEL_OPEN) {
// log_info("RFCOMM_EVENT_CREDITS: multiplexer not open\n");
continue;
}
if (channel->packets_granted) {
// log_info("RFCOMM_EVENT_CREDITS: already packets granted\n");
continue;
}
if (!channel->credits_outgoing) {
// log_info("RFCOMM_EVENT_CREDITS: no outgoing credits\n");
continue;
}
if (!channel->multiplexer->l2cap_credits){
// log_info("RFCOMM_EVENT_CREDITS: no l2cap credits\n");
continue;
}
// channel open, multiplexer has l2cap credits and we didn't hand out credit before -> go!
// log_info("RFCOMM_EVENT_CREDITS: 1\n");
channel->packets_granted += 1;
rfcomm_emit_credits(channel, 1);
}
}
static void rfcomm_channel_send_credits(rfcomm_channel_t *channel, uint8_t credits){
rfcomm_send_uih_credits(channel->multiplexer, channel->dlci, credits);
channel->credits_incoming += credits;
rfcomm_emit_credit_status(channel);
}
static void rfcomm_channel_opened(rfcomm_channel_t *rfChannel){
log_info("rfcomm_channel_opened!\n");
rfChannel->state = RFCOMM_CHANNEL_OPEN;
rfcomm_emit_channel_opened(rfChannel, 0);
rfcomm_hand_out_credits();
// remove (potential) timer
rfcomm_multiplexer_t *multiplexer = rfChannel->multiplexer;
if (multiplexer->timer_active) {
run_loop_remove_timer(&multiplexer->timer);
multiplexer->timer_active = 0;
}
// hack for problem detecting authentication failure
multiplexer->at_least_one_connection = 1;
// start next connection request if pending
rfcomm_run();