forked from lwalkera/lwBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_spp.c
1049 lines (921 loc) · 31.5 KB
/
bt_spp.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
/*
* Copyright (c) 2009 PASCO scientific
* All rights reserved.
*
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
*
* This file is part of the lwBT Bluetooth stack.
*
* Author: Laine Walker-Avina <[email protected]>
* Based on code from: Conny Ohult <[email protected]>
*
*/
/**
* @file bt_spp.c
*
* This is a control application that initialises a host controller and
* connects to a network as a DT through a DUN or LAP enabled device.
* When a network connection has been established, it initialises its own LAP
* server.
*/
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/inet.h"
#include "lwbt/phybusif.h"
#include "lwbt/lwbt_memp.h"
#include "lwbt/hci.h"
#include "lwbt/l2cap.h"
#include "lwbt/sdp.h"
#include "lwbt/rfcomm.h"
//#include "stdlib.h"
#define BT_SPP_DEBUG LWIP_DBG_ON /* Controls debug messages */
err_t command_complete(void *arg, struct hci_pcb *pcb, u8_t ogf, u8_t ocf, u8_t result);
err_t pin_req(void *arg, struct bd_addr *bdaddr);
err_t l2cap_connected(void *arg, struct l2cap_pcb *l2cappcb, u16_t result, u16_t status);
err_t bt_spp_init();
struct bt_state {
struct bd_addr bdaddr;
struct pbuf *p;
u8_t btctrl;
u8_t cn;
} bt_spp_state;
static const u8_t spp_service_record[] =
{
SDP_DES_SIZE8, 0x8,
SDP_UINT16, 0x0, 0x0, /* Service record handle attribute */
SDP_UINT32, 0x00, 0x00, 0x00, 0x00, /*dummy vals, filled in on xmit*/
SDP_DES_SIZE8, 0x16,
SDP_UINT16, 0x0, 0x1, /* Service class ID list attribute */
SDP_DES_SIZE8, 17,
SDP_UUID128, 0x00, 0x00, 0x00, 0x00,
0xde, 0xca,
0xfa, 0xde,
0xde, 0xca,
0xde, 0xaf, 0xde, 0xca, 0xca, 0xff,
SDP_DES_SIZE8, 0x11,
SDP_UINT16, 0x0, 0x4, /* Protocol descriptor list attribute */
SDP_DES_SIZE8, 0xc,
SDP_DES_SIZE8, 0x3,
SDP_UUID16, 0x1, 0x0, /*L2CAP*/
SDP_DES_SIZE8, 0x5,
SDP_UUID16, 0x0, 0x3, /*RFCOMM*/
SDP_UINT8, 0x1, /*RFCOMM channel*/
SDP_DES_SIZE8, 0x8,
SDP_UINT16, 0x0, 0x5, /*Browse group list */
SDP_DES_SIZE8, 0x3,
SDP_UUID16, 0x10, 0x02, /*PublicBrowseGroup*/
};
/*
* bt_spp_start():
*
* Called by the main application to initialize and connect to a network
*
*/
void bt_spp_start(void)
{
hci_reset_all();
l2cap_reset_all();
sdp_reset_all();
rfcomm_reset_all();
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_start\n"));
hci_cmd_complete(command_complete);
hci_pin_req(pin_req);
bt_spp_state.btctrl = 0;
bt_spp_state.p = NULL;
hci_reset();
if(bt_spp_init() != ERR_OK) /* Initialize the SPP role */
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_start: couldn't init role\n"));
return;
}
}
/*
* bt_spp_tmr():
*
* Called by the main application to initialize and connect to a network
*
*/
void bt_spp_tmr(void)
{
#if 0
u8_t update_cmd[12];
update_cmd[0] = 1;
update_cmd[1] = 0;
update_cmd[2] = bt_spp_state.bdaddr.addr[5];
update_cmd[3] = bt_spp_state.bdaddr.addr[4];
update_cmd[4] = bt_spp_state.bdaddr.addr[3];
update_cmd[5] = bt_spp_state.bdaddr.addr[2];
update_cmd[6] = bt_spp_state.bdaddr.addr[1];
update_cmd[7] = bt_spp_state.bdaddr.addr[0];
update_cmd[8] = 0x00;
update_cmd[9] = 0x00;
update_cmd[10] = 0x00;
update_cmd[11] = 0x00;
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_tmr: Update cmd bd address: 0x%x:0x%x:0x%x:0x%x:0x%x:0x%x\n", update_cmd[2], update_cmd[3], update_cmd[4], update_cmd[5], update_cmd[6], update_cmd[7]));
if(bt_spp_state.tcppcb != NULL) {
tcp_write(bt_spp_state.tcppcb, &update_cmd, 12, 1);
}
#endif
}
/*
* rfcomm_disconnected():
*
* Called by RFCOMM when the remote RFCOMM protocol or upper layer was disconnected.
* Disconnects the PPP protocol.
*
*/
err_t rfcomm_disconnected(void *arg, struct rfcomm_pcb *pcb, err_t err)
{
err_t ret = ERR_OK;
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_disconnected: CN = %d\n", rfcomm_cn(pcb)));
if(rfcomm_cn(pcb) != 0) {
; //ppp_lp_disconnected(pcb);
}
rfcomm_close(pcb);
return ret;
}
/*
* l2cap_disconnected_ind():
*
* Called by L2CAP to indicate that remote L2CAP protocol disconnected.
* Disconnects the RFCOMM protocol and the ACL link before it initializes a search for
* other devices.
*
*/
err_t l2cap_disconnected_ind(void *arg, struct l2cap_pcb *pcb, err_t err)
{
err_t ret = ERR_OK;
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_disconnected_ind: L2CAP disconnected\n"));
if(pcb->psm == SDP_PSM) {
sdp_lp_disconnected(pcb);
l2cap_close(pcb);
} else if(pcb->psm == RFCOMM_PSM) {
ret = rfcomm_lp_disconnected(pcb);
/* We can do this since we know that we are the only channel on the ACL link.
* If ACL link already is down we get an ERR_CONN returned */
hci_disconnect(&(pcb->remote_bdaddr), HCI_OTHER_END_TERMINATED_CONN_USER_ENDED);
l2cap_close(pcb);
bt_spp_start();
}
return ret;
}
/*
* bluetoothif_init():
*
* Called by lwIP to initialize the lwBT network interface.
*
*/
#if 0
err_t bluetoothif_init(struct netif *netif)
{
netif->name[0] = 'b';
netif->name[1] = '0' + bt_spp_netifn++;
netif->output = ppp_netif_output;
netif->state = NULL;
return ERR_OK;
}
#endif
/*
* tcp_connected():
*
* Called by TCP when a connection has been established.
* Connects to a remote gateway and give the TCP connection to the HTTP server
* application
*
*/
#if 0
err_t tcp_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
u8_t update_cmd[12];
LWIP_DEBUGF(BT_SPP_DEBUG, ("tcp_connected\n"));
update_cmd[0] = 1;
update_cmd[1] = 0;
update_cmd[2] = bt_spp_state.bdaddr.addr[5];
update_cmd[3] = bt_spp_state.bdaddr.addr[4];
update_cmd[4] = bt_spp_state.bdaddr.addr[3];
update_cmd[5] = bt_spp_state.bdaddr.addr[2];
update_cmd[6] = bt_spp_state.bdaddr.addr[1];
update_cmd[7] = bt_spp_state.bdaddr.addr[0];
LWIP_DEBUGF(BT_SPP_DEBUG, ("tcp_connected: bd address: 0x%x:0x%x:0x%x:0x%x:0x%x:0x%x\n", bt_spp_state.bdaddr.addr[0], bt_spp_state.bdaddr.addr[1], bt_spp_state.bdaddr.addr[2], bt_spp_state.bdaddr.addr[3], bt_spp_state.bdaddr.addr[4], bt_spp_state.bdaddr.addr[5]));
LWIP_DEBUGF(BT_SPP_DEBUG, ("tcp_connected: Update cmd bd address: 0x%x:0x%x:0x%x:0x%x:0x%x:0x%x\n", update_cmd[2], update_cmd[3], update_cmd[4], update_cmd[5], update_cmd[6], update_cmd[7]));
update_cmd[8] = 0x00;
update_cmd[9] = 0x00;
update_cmd[10] = 0x00;
update_cmd[11] = 0x00;
tcp_write(pcb, &update_cmd, 12, 1);
LWIP_DEBUGF(BT_SPP_DEBUG, ("tcp_connected: Update command sent\n"));
bt_spp_state.tcppcb = pcb;
return http_accept((void *)&(bt_spp_state.bdaddr), pcb, ERR_OK);
}
#endif
#if 0
err_t ppp_accept(void *arg, struct ppp_pcb *pcb, err_t err)
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("ppp_accept\n"));
if(err != ERR_OK) {
netif_remove(pcb->bluetoothif);
pcb->bluetoothif = NULL;
}
return ERR_OK;
}
#endif
#if 0
err_t modem_emu(void *arg, struct rfcomm_pcb *pcb, struct pbuf *p, err_t err)
{
u8_t *data = p->payload;
struct pbuf *q = NULL;
u16_t proto;
u8_t code;
u8_t i;
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: p->len == %d p->tot_len == %d\n", p->len, p->tot_len));
for (i = 0; i < p->len; ++i) {
if(data[i] == PPP_END) {
if ((p->len - i) >= 7) {
if (data[i + 2] == PPP_ESC) {
proto = ntohs(*((u16_t *)(((u8_t *)p->payload) + i + 4)));
}
else {
proto = ntohs(*((u16_t *)(((u8_t *)p->payload) + i + 3)));
}
if(proto == PPP_LCP) {
code = data[i + 7] ^ 0x20;
if(code == LCP_CFG_REQ) {
rfcomm_recv(pcb, ppp_input);
ppp_input(arg, pcb, p, ERR_OK);
}
}
}
return ERR_OK;
}
}
if(arg != NULL) {
q = pbuf_alloc(PBUF_RAW, p->len + ((struct pbuf *)arg)->len, PBUF_RAM);
memcpy((u8_t *)q->payload, (u8_t *)((struct pbuf *)arg)->payload, ((struct pbuf *)arg)->len);
memcpy(((u8_t *)q->payload) + ((struct pbuf *)arg)->len, (u8_t *)p->payload, p->len);
pbuf_free((struct pbuf *)arg);
(struct pbuf *)arg = NULL;
pbuf_free(p);
data = q->payload;
p = q;
}
for (i = 0; i < p->len; ++i) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: %c %d\n", data[i], data[i]));
}
if(p->len == 0) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: p->len == 0\n"));
q = pbuf_alloc(PBUF_RAW, sizeof("OK\r\n"), PBUF_RAM);
((u8_t *)q->payload) = "OK\r\n";
pbuf_free(p);
} else if(!strncmp(data, "ATD", 3)) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: !strncasecmp(data, \"ATD\", 3)\n"));
q = pbuf_alloc(PBUF_RAW, sizeof("CONNECT\r\n"), PBUF_RAM);
((u8_t *)q->payload) = "CONNECT\r\n";
pbuf_free(p);
} else if(!strncmp(data, "CLIENT", 6)) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: !strncasecmp(data, \"CLIENT\", 6)\n"));
q = pbuf_alloc(PBUF_RAW, sizeof("CLIENTSERVER\r\n"), PBUF_RAM);
((u8_t *)q->payload) = "CLIENTSERVER\r\n";
pbuf_free(p);
} else if(data[p->len - 1] != 0x0D) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: data[p->len - 1] != 0x0D\n"));
rfcomm_arg(pcb, p);
return ERR_OK;
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("modem_emu: Unknown. Sending OK\n"));
q = pbuf_alloc(PBUF_RAW, sizeof("OK\r\n"), PBUF_RAM);
((u8_t *)q->payload) = "OK\r\n";
pbuf_free(p);
}
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, PBUF_POOL_SIZE - rfcomm_remote_credits(pcb), q);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), q);
}
pbuf_free(q);
return ERR_OK;
}
#endif
err_t spp_recv(void *arg, struct rfcomm_pcb *pcb, struct pbuf *p, err_t err)
{
u8_t *data = p->payload;
struct pbuf *q = NULL;
LWIP_DEBUGF(BT_SPP_DEBUG, ("spp_recv: p->len == %d p->tot_len == %d\n", p->len, p->tot_len));
if(arg != NULL) {
q = pbuf_alloc(PBUF_RAW, p->len + ((struct pbuf *)arg)->len, PBUF_RAM);
LWIP_ERROR("couldn't alloc pbuf", q == NULL, return ERR_MEM);
memcpy((u8_t *)q->payload, (u8_t *)((struct pbuf *)arg)->payload, ((struct pbuf *)arg)->len);
memcpy(((u8_t *)q->payload) + ((struct pbuf *)arg)->len, (u8_t *)p->payload, p->len);
pbuf_free((struct pbuf *)arg);
//(struct pbuf *)arg = NULL;
pbuf_free(p);
data = q->payload;
p = q;
}
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, PBUF_POOL_SIZE - rfcomm_remote_credits(pcb), q);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), q);
}
pbuf_free(q);
return ERR_OK;
}
err_t rfcomm_accept(void *arg, struct rfcomm_pcb *pcb, err_t err)
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_accept: CN = %d\n", rfcomm_cn(pcb)));
rfcomm_disc(pcb, rfcomm_disconnected);
if(pcb->cn != 0) {
//set recv callback
rfcomm_recv(pcb, spp_recv);
}
return ERR_OK;
}
static err_t bt_disconnect_ind(void *arg, struct l2cap_pcb *pcb, err_t err)
{
err_t ret;
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_disconnect_ind\n"));
if(pcb->psm == SDP_PSM) {
sdp_lp_disconnected(pcb);
} else if(pcb->psm == RFCOMM_PSM) {
ret = rfcomm_lp_disconnected(pcb);
}
l2cap_close(pcb);
return ERR_OK;
}
err_t bt_connect_ind(void *arg, struct l2cap_pcb *pcb, err_t err)
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_connect_ind\n"));
/* Tell L2CAP that we wish to be informed of a disconnection request */
l2cap_disconnect_ind(pcb, bt_disconnect_ind);
/* Tell L2CAP that we wish to be informed of incoming data */
if(pcb->psm == SDP_PSM) {
l2cap_recv(pcb, sdp_recv);
} else if (pcb->psm == RFCOMM_PSM) {
l2cap_recv(pcb, rfcomm_input);
}
return ERR_OK;
}
err_t bt_spp_init(void)
{
struct l2cap_pcb *l2cappcb;
struct rfcomm_pcb *rfcommpcb;
struct sdp_record *record;
if((l2cappcb = l2cap_new()) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_init: Could not alloc L2CAP PCB for SDP_PSM\n"));
return ERR_MEM;
}
l2cap_connect_ind(l2cappcb, SDP_PSM, bt_connect_ind);
if((l2cappcb = l2cap_new()) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_init: Could not alloc L2CAP PCB for RFCOMM_PSM\n"));
return ERR_MEM;
}
l2cap_connect_ind(l2cappcb, RFCOMM_PSM, bt_connect_ind);
LWIP_DEBUGF(RFCOMM_DEBUG, ("bt_spp_init: Allocate RFCOMM PCB for CN 0\n"));
if((rfcommpcb = rfcomm_new(NULL)) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_init: Could not alloc RFCOMM PCB for channel 0\n"));
return ERR_MEM;
}
rfcomm_listen(rfcommpcb, 0, rfcomm_accept);
LWIP_DEBUGF(RFCOMM_DEBUG, ("bt_spp_init: Allocate RFCOMM PCB for CN 1\n"));
if((rfcommpcb = rfcomm_new(NULL)) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("lap_init: Could not alloc RFCOMM PCB for channel 1\n"));
return ERR_MEM;
}
rfcomm_listen(rfcommpcb, 1, rfcomm_accept);
if((record = sdp_record_new((u8_t *)spp_service_record, sizeof(spp_service_record))) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("bt_spp_init: Could not alloc SDP record\n"));
return ERR_MEM;
} else {
sdp_register_service(record);
}
LWIP_DEBUGF(BT_SPP_DEBUG, ("SPP initialized\n"));
return ERR_OK;
}
/*
* ppp_connected():
*
* Called by PPP when a LCP and IPCP connection has been established.
* Connects to a given TCP host.
*
*/
#if 0
err_t ppp_connected(void *arg, struct ppp_pcb *pcb, err_t err)
{
struct tcp_pcb *tcppcb;
struct ip_addr ipaddr;
err_t ret;
u8_t flag = 0x03;
LWIP_DEBUGF(BT_SPP_DEBUG, ("ppp_connected: err = %d\n", err));
/* return ppp_echo(pcb, NULL); */
if(err != ERR_OK) {
netif_remove(pcb->bluetoothif);
pcb->bluetoothif = NULL;
//TODO: RESTART??
return ERR_OK;
}
//pcb->bluetoothif->netmask.addr = htonl(~(ntohl(pcb->bluetoothif->gw.addr) ^ ntohl(pcb->bluetoothif->ip_addr.addr)));
nat_init(pcb->bluetoothif, ip_input); /* Set function to be called by NAT to pass a packet up to the TCP/IP
stack */
ret = lap_init(); /* Initialize the LAP role */
//if(bt_spp_state.profile == DUN_PROFILE) {
/* Make LAP discoverable */
hci_cmd_complete(command_complete);
hci_set_event_filter(0x02, 0x00, &flag); /* Auto accept all connections with role switch enabled */
//}
//tcppcb = tcp_new();
//IP4_ADDR(&ipaddr, 130,240,45,234);
//tcp_connect(tcppcb, &ipaddr, 8989, tcp_connected);
return ret;
}
#endif
/*
* at_input():
*
* Called by RFCOMM during the DUN profile GPRS connection attempt.
* When a GPRS connection is established, PPP is connected.
*
*/
u8_t at_state;
err_t at_input(void *arg, struct rfcomm_pcb *pcb, struct pbuf *p, err_t err)
{
#if 0
struct ppp_pcb *ppppcb;
struct ip_addr ipaddr, netmask, gw;
struct netif *netif;;
// u16_t i;
struct pbuf *q;
//for(q = p; q != NULL; q = q->next) {
// for(i = 0; i < q->len; ++i) {
// LWIP_DEBUGF(BT_SPP_DEBUG, ("at_input: 0x%x\n",((u8_t *)p->payload)[i]));
// }
// LWIP_DEBUGF(BT_SPP_DEBUG, ("*\n"));
//}
LWIP_DEBUGF(BT_SPP_DEBUG, ("at_input: %s\n", ((u8_t *)p->payload)));
LWIP_DEBUGF(BT_SPP_DEBUG, ("state == %d\n", at_state));
if(at_state == 0 && ((u8_t *)p->payload)[2] == 'O') {
//q = pbuf_alloc(PBUF_RAW, sizeof("AT&F\r")-1, PBUF_RAM);
//((u8_t *)q->payload) = "AT&F\r";
q = pbuf_alloc(PBUF_RAW, sizeof("ATE1\r"), PBUF_RAM);
((u8_t *)q->payload) = "ATE1\r";
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, 2, q);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), q);
}
pbuf_free(q);
at_state = 1;
} else if(at_state == 1 && ((u8_t *)p->payload)[2] == 'O') {
q = pbuf_alloc(PBUF_RAW, sizeof("AT+cgdcont=1,\"IP\",\"online.telia.se\"\r"), PBUF_RAM);
((u8_t *)q->payload) = "AT+cgdcont=1,\"IP\",\"online.telia.se\"\r";
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, 2, q);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), q);
}
pbuf_free(q);
at_state = 4;
} else if(at_state == 4 && ((u8_t *)p->payload)[2] == 'O') {
q = pbuf_alloc(PBUF_RAW, sizeof("ATD*99***1#\r"), PBUF_RAM);
((u8_t *)q->payload) = "ATD*99***1#\r";
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, 2, q);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), q);
}
pbuf_free(q);
at_state = 5;
} else if(at_state == 5 && ((u8_t *)p->payload)[2] == 'C') {
at_state = 6;
/* Establish a PPP connection */
if((ppppcb = ppp_new(pcb)) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_msc_rsp: Could not alloc PPP pcb\n"));
return ERR_MEM;
}
/* Add PPP network interface to lwIP and initialize NAT */
gw.addr = 0;
ipaddr.addr = 0;
IP4_ADDR(&netmask, 255,255,255,0);
netif = netif_add(&ipaddr, &netmask, &gw, NULL, bluetoothif_init, nat_input);
netif_set_default(netif);
ppp_netif(ppppcb, netif);
rfcomm_recv(pcb, ppp_input);
ppp_disconnected(ppppcb, ppp_is_disconnected);
return ppp_connect(ppppcb, ppp_connected);
}
pbuf_free(p);
#endif
return ERR_OK;
}
/*
* pin_req():
*
* Called by HCI when a request for a PIN code has been received. A PIN code
* is required to create a new link key.
* Replys to the request with the given PIN code
*/
err_t pin_req(void *arg, struct bd_addr *bdaddr)
{
u8_t pin[] = "1234";
LWIP_DEBUGF(BT_SPP_DEBUG, ("pin_req\n"));
return hci_pin_code_request_reply(bdaddr, 4, pin);
}
/*
* link_key_not():
*
* Called by HCI when a new link key has been created for the connection
* Writes the key to the Bluetooth host controller, where it can be stored for future
* connection attempts.
*
*/
err_t link_key_not(void *arg, struct bd_addr *bdaddr, u8_t *key)
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("link_key_not\n"));
return hci_write_stored_link_key(bdaddr, key); /* Write link key to be stored in the
Bluetooth host controller */
}
/*
* l2cap_disconnected_cfm():
*
* Called by L2CAP to confirm that the L2CAP disconnection request was successful
*
*/
err_t l2cap_disconnected_cfm(void *arg, struct l2cap_pcb *pcb)
{
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_disconnected_cfm\n"));
l2cap_close(pcb);
return ERR_OK;
}
/*
* get_rfcomm_cn():
*
* Parse the RFCOMM channel number from an SDP attribute list
*
*/
u8_t get_rfcomm_cn(u16_t attribl_bc, struct pbuf *attribute_list)
{
u8_t i;
for(i = 0; i < attribl_bc; i++) {
if(((u8_t *)attribute_list->payload)[i] == (SDP_DE_TYPE_UUID | SDP_DE_SIZE_16)) {
if(ntohs(*((u16_t *)(((u8_t *)attribute_list->payload)+i+1))) == 0x0003) {
return *(((u8_t *)attribute_list->payload)+i+4);
}
}
}
return 0;
}
/*
* rfcomm_connected():
*
* Called by RFCOMM when a connection attempt response was received.
* Creates a RFCOMM connection for the channel retreived from SDP.
* Initializes a search for other devices if the connection attempt failed.
*/
err_t rfcomm_connected(void *arg, struct rfcomm_pcb *pcb, err_t err)
{
//struct pbuf *p;
//struct ip_addr ipaddr, netmask, gw;
//struct netif *netif;
//struct ppp_pcb *ppppcb;
if(err == ERR_OK) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_connected. CN = %d\n", rfcomm_cn(pcb)));
rfcomm_disc(pcb, rfcomm_disconnected);
#if 0
if(bt_spp_state.profile == DUN_PROFILE) {
/* Establish a GPRS connection */
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_msc_rsp: Establish a GPRS connection\n"));
rfcomm_recv(pcb, at_input);
//p = pbuf_alloc(PBUF_RAW, sizeof("ATZ\r")-1, PBUF_RAM);
//((u8_t *)p->payload) = "ATZ\r";
p = pbuf_alloc(PBUF_RAW, sizeof("AT\r"), PBUF_RAM);
((u8_t *)p->payload) = "AT\r";
at_state = 0;
if(rfcomm_cl(pcb)) {
rfcomm_uih_credits(pcb, 6, p);
} else {
rfcomm_uih(pcb, rfcomm_cn(pcb), p);
}
pbuf_free(p);
} else {
/* Establish a PPP connection */
//if((ppppcb = ppp_new(pcb)) == NULL) {
// LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_msc_rsp: Could not alloc PPP pcb\n"));
// return ERR_MEM;
//}
/* Add PPP network interface to lwIP and initialize NAT */
//gw.addr = 0;
//ipaddr.addr = 0;
//IP4_ADDR(&netmask, 255,255,255,0);
//netif = netif_add(&ipaddr, &netmask, &gw, NULL, bluetoothif_init, nat_input);
//netif_set_default(netif);
//ppp_netif(ppppcb, netif);
rfcomm_recv(pcb, ppp_input);
//ppp_disconnected(ppppcb, ppp_is_disconnected);
return ERR_OK; //ppp_connect(ppppcb, ppp_connected);
}
#endif
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("rfcomm_connected. Connection attempt failed CN = %d\n", rfcomm_cn(pcb)));
l2cap_close(pcb->l2cappcb);
rfcomm_close(pcb);
bt_spp_start();
}
return ERR_OK;
}
/*
* sdp_attributes_recv():
*
* Can be used as a callback by SDP when a response to a service attribute request or
* a service search attribute request was received.
* Disconnects the L2CAP SDP channel and connects to the RFCOMM one.
* If no RFCOMM channel was found it initializes a search for other devices.
*/
void sdp_attributes_recv(void *arg, struct sdp_pcb *sdppcb, u16_t attribl_bc, struct pbuf *p)
{
struct l2cap_pcb *l2cappcb;
l2ca_disconnect_req(sdppcb->l2cappcb, l2cap_disconnected_cfm);
/* Get the RFCOMM channel identifier from the protocol descriptor list */
if((bt_spp_state.cn = get_rfcomm_cn(attribl_bc, p)) != 0) {
if((l2cappcb = l2cap_new()) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("sdp_attributes_recv: Could not alloc L2CAP pcb\n"));
return;
}
LWIP_DEBUGF(BT_SPP_DEBUG, ("sdp_attributes_recv: RFCOMM channel: %d\n", bt_spp_state.cn));
//if(bt_spp_state.profile == DUN_PROFILE) {
// l2ca_connect_req(l2cappcb, &(sdppcb->l2cappcb->remote_bdaddr), RFCOMM_PSM, 0, l2cap_connected);
//} else {
l2ca_connect_req(l2cappcb, &(sdppcb->l2cappcb->remote_bdaddr), RFCOMM_PSM, HCI_ALLOW_ROLE_SWITCH, l2cap_connected);
//}
} else {
bt_spp_start();
}
sdp_free(sdppcb);
}
/*
* l2cap_connected():
*
* Called by L2CAP when a connection response was received.
* Sends a L2CAP configuration request.
* Initializes a search for other devices if the connection attempt failed.
*/
err_t l2cap_connected(void *arg, struct l2cap_pcb *l2cappcb, u16_t result, u16_t status)
{
struct sdp_pcb *sdppcb;
struct rfcomm_pcb *rfcommpcb;
u8_t ssp[] = {0x35, 0x03, 0x19, 0x11, 0x02}; /* Service search pattern with LAP UUID is default */
err_t ret;
u8_t attrids[] = {0x35, 0x03, 0x09, 0x00, 0x04}; /* Attribute IDs to search for in data element
sequence form */
if(result == L2CAP_CONN_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: L2CAP connected pcb->state = %d\n", l2cappcb->state));
/* Tell L2CAP that we wish to be informed of a disconnection request */
l2cap_disconnect_ind(l2cappcb, l2cap_disconnected_ind);
switch(l2cap_psm(l2cappcb)) {
case SDP_PSM:
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: SDP L2CAP configured. Result = %d\n", result));
if((sdppcb = sdp_new(l2cappcb)) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: Failed to create a SDP PCB\n"));
return ERR_MEM;
}
l2cap_recv(l2cappcb, sdp_recv);
ret = sdp_service_search_attrib_req(sdppcb, 0xFFFF, ssp, sizeof(ssp),
attrids, sizeof(attrids), sdp_attributes_recv);
return ret;
case RFCOMM_PSM:
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: RFCOMM L2CAP configured. Result = %d CN = %d\n", result, bt_spp_state.cn));
l2cap_recv(l2cappcb, rfcomm_input);
if((rfcommpcb = rfcomm_new(l2cappcb)) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: Failed to create a RFCOMM PCB\n"));
return ERR_MEM;
}
hci_link_key_not(link_key_not); /* Set function to be called if a new link key is created */
return rfcomm_connect(rfcommpcb, bt_spp_state.cn, rfcomm_connected); /* Connect with DLCI 0 */
default:
return ERR_VAL;
}
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("l2cap_connected: L2CAP not connected. Redo inquiry\n"));
l2cap_close(l2cappcb);
bt_spp_start();
}
return ERR_OK;
}
/*
* inquiry_complete():
*
* Called by HCI when a inquiry complete event was received.
* Connects to the first device in the list.
* Initializes a search for other devices if the inquiry failed.
*/
err_t inquiry_complete(void *arg, struct hci_pcb *pcb, struct hci_inq_res *ires, u16_t result)
{
struct l2cap_pcb *l2cappcb;
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful Inquiry\n"));
if(ires != NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Initiate L2CAP connection\n"));
LWIP_DEBUGF(BT_SPP_DEBUG, ("ires->psrm %d\n ires->psm %d\n ires->co %d\n", ires->psrm, ires->psm, ires->co));
LWIP_DEBUGF(BT_SPP_DEBUG, ("ires->bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
ires->bdaddr.addr[5], ires->bdaddr.addr[4], ires->bdaddr.addr[3],
ires->bdaddr.addr[2], ires->bdaddr.addr[1], ires->bdaddr.addr[0]));
/*if((ires->cod[1] & 0x1F) == 0x03) {
bt_spp_state.profile = LAP_PROFILE;
} else {
bt_spp_state.profile = DUN_PROFILE;
}*/
if((l2cappcb = l2cap_new()) == NULL) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("inquiry_complete: Could not alloc L2CAP pcb\n"));
return ERR_MEM;
}
//if(bt_spp_state.profile == DUN_PROFILE) {
// l2ca_connect_req(l2cappcb, &(ires->bdaddr), SDP_PSM, 0, l2cap_connected);
//} else {
l2ca_connect_req(l2cappcb, &(ires->bdaddr), SDP_PSM, HCI_ALLOW_ROLE_SWITCH, l2cap_connected);
//}
} else {
hci_inquiry(0x009E8B33, 0x04, 0x01, inquiry_complete);
}
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful Inquiry.\n"));
hci_inquiry(0x009E8B33, 0x04, 0x01, inquiry_complete);
}
return ERR_OK;
}
/*
* acl_wpl_complete():
*
* Called by HCI when a successful write link policy settings complete event was
* received.
*/
err_t acl_wpl_complete(void *arg, struct bd_addr *bdaddr)
{
hci_sniff_mode(bdaddr, 200, 100, 10, 10);
return ERR_OK;
}
/*
* acl_conn_complete():
*
* Called by HCI when a connection complete event was received.
*/
err_t acl_conn_complete(void *arg, struct bd_addr *bdaddr)
{
//hci_wlp_complete(acl_wpl_complete);
//hci_write_link_policy_settings(bdaddr, 0x000F);
return ERR_OK;
}
/*
* read_bdaddr_complete():
*
* Called by HCI when a read local bluetooth device address complete event was received.
*/
err_t read_bdaddr_complete(void *arg, struct bd_addr *bdaddr)
{
memcpy(&(bt_spp_state.bdaddr), bdaddr, 6);
LWIP_DEBUGF(BT_SPP_DEBUG, ("read_bdaddr_complete: %02x:%02x:%02x:%02x:%02x:%02x\n",
bdaddr->addr[5], bdaddr->addr[4], bdaddr->addr[3],
bdaddr->addr[2], bdaddr->addr[1], bdaddr->addr[0]));
return ERR_OK;
}
/*
* command_complete():
*
* Called by HCI when an issued command has completed during the initialization of the
* host controller. Waits for a connection from remote device once connected.
*
* Event Sequence:
* HCI Reset -> Read Buf Size -> Read BDAddr -> Set Ev Filter -+
* +-----------------------------------------------------------+
* |_/-> Write CoD -> Cng Local Name -> Write Pg Timeout -> Inq -> Complete
* \-> Scan Enable -> Complete
*/
err_t command_complete(void *arg, struct hci_pcb *pcb, u8_t ogf, u8_t ocf, u8_t result)
{
u8_t cod_spp[] = {0x08,0x04,0x24};
u8_t devname[] = "iAirlink----";
u8_t n1, n2, n3;
u8_t flag = HCI_SET_EV_FILTER_AUTOACC_ROLESW;
switch(ogf) {
case HCI_INFO_PARAM:
switch(ocf) {
case HCI_READ_BUFFER_SIZE:
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful HCI_READ_BUFFER_SIZE.\n"));
hci_read_bd_addr(read_bdaddr_complete);
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful HCI_READ_BUFFER_SIZE.\n"));
return ERR_CONN;
}
break;
case HCI_READ_BD_ADDR:
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful HCI_READ_BD_ADDR.\n"));
/* Make discoverable */
hci_set_event_filter(HCI_SET_EV_FILTER_CONNECTION,
HCI_SET_EV_FILTER_ALLDEV, &flag);
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful HCI_READ_BD_ADDR.\n"));
return ERR_CONN;
}
break;
default:
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unknown HCI_INFO_PARAM command complete event\n"));
break;
}
break;
case HCI_HC_BB_OGF:
switch(ocf) {
case HCI_RESET:
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful HCI_RESET.\n"));
hci_read_buffer_size();
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful HCI_RESET.\n"));
return ERR_CONN;
}
break;
case HCI_WRITE_SCAN_ENABLE:
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful HCI_WRITE_SCAN_ENABLE.\n"));
//hci_cmd_complete(NULL); /* Initialization done, don't come back */
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful HCI_WRITE_SCAN_ENABLE.\n"));
return ERR_CONN;
}
break;
case HCI_SET_EVENT_FILTER:
if(result == HCI_SUCCESS) {
LWIP_DEBUGF(BT_SPP_DEBUG, ("successful HCI_SET_EVENT_FILTER.\n"));
hci_write_cod(cod_spp);
hci_write_scan_enable(HCI_SCAN_EN_INQUIRY | HCI_SCAN_EN_PAGE);
} else {
LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful HCI_SET_EVENT_FILTER.\n"));
return ERR_CONN;
}
break;