-
Notifications
You must be signed in to change notification settings - Fork 105
/
bluetooth_common.c
1397 lines (1167 loc) · 36.2 KB
/
bluetooth_common.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
/****************************************************************************
* modules/bluetooth/bluetooth_common.c
*
* Copyright 2018, 2023 Sony Semiconductor Solutions Corporation
*
* 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 Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <string.h>
#include <bluetooth/bt_common.h>
#include <bluetooth/hal/bt_if.h>
#include "bluetooth_hal_init.h"
#include "bluetooth_le_gatt.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_BLUETOOTH_NAME
#define CONFIG_BLUETOOTH_NAME "SONY-BT-CLASSIC"
#endif
#ifndef CONFIG_BLUETOOTH_LE_NAME
#define CONFIG_BLUETOOTH_LE_NAME "SONY-BLE-CLASSIC"
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static struct bt_common_state_s g_bt_common_state =
{
.bt_name = CONFIG_BLUETOOTH_NAME,
.ble_name = CONFIG_BLUETOOTH_LE_NAME,
.bt_addr = {{0x20, 0x70, 0x3A, 0x10, 0x00, 0x01}},
.ble_addr = {{0x20, 0x70, 0x3A, 0x10, 0x00, 0x01}}
};
static struct bt_acl_state_s g_bt_acl_state =
{
.bt_acl_connection = BT_DISCONNECTED,
.bt_common_state = &g_bt_common_state
};
static struct ble_state_s g_ble_state =
{
.ble_connection = BT_DISCONNECTED,
.bt_common_state = &g_bt_common_state
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int bt_event_cmd_status(struct bt_event_cmd_stat_t *cmd_stat_evt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->command_status)
{
bt_common_ops->command_status(cmd_stat_evt->cmd_status);
}
else
{
_err("%s [BT][Common] Command status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_pairing_complete(struct bt_event_pair_cmplt_t *pair_cmplt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->pairing_complete)
{
bt_common_ops->pairing_complete(pair_cmplt->addr, pair_cmplt->status);
}
else
{
_err("%s [BT][Common] Command status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_inquiry_result(struct bt_event_inquiry_rslt_t *inq_result_evt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->inquiry_result)
{
bt_common_ops->inquiry_result(inq_result_evt->addr, inq_result_evt->name);
}
else
{
_err("%s [BT][Common] Command status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_inquiry_complete(void)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->inquiry_complete)
{
bt_common_ops->inquiry_complete();
}
else
{
_err("%s [BT][Common] Command status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_conn_stat_change(struct bt_event_conn_stat_t *conn_stat_evt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (conn_stat_evt->connected)
{
g_bt_acl_state.bt_target_addr = conn_stat_evt->addr;
g_bt_acl_state.bt_acl_connection = BT_CONNECTED;
}
else
{
g_bt_acl_state.bt_acl_connection = BT_DISCONNECTED;
}
if (bt_common_ops && bt_common_ops->connect_status_changed)
{
bt_common_ops->connect_status_changed(&g_bt_acl_state, conn_stat_evt->connected, conn_stat_evt->status);
}
else
{
_err("%s [BT][Common] Connect status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_conn_dev_name(struct bt_event_dev_name_t *dev_name_evt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->connected_device_name)
{
memcpy(g_bt_acl_state.bt_target_name, dev_name_evt->name, BT_NAME_LEN);
bt_common_ops->connected_device_name(dev_name_evt->name);
}
else
{
_err("%s [BT][Common] Connected device name callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int bt_event_bond_info(struct bt_event_bond_info_t *bond_info_evt)
{
int ret = BT_SUCCESS;
struct bt_common_ops_s *bt_common_ops = g_bt_common_state.bt_common_ops;
if (bt_common_ops && bt_common_ops->bond_info)
{
g_bt_acl_state.bt_target_addr = bond_info_evt->addr;
bt_common_ops->bond_info(bond_info_evt->addr);
}
else
{
_err("%s [BT][Common] Bonding information callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_connect_stat_change(struct ble_event_conn_stat_t *conn_stat_evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ble_common_ops = g_bt_common_state.ble_common_ops;
if (conn_stat_evt->connected)
{
g_ble_state.bt_target_addr = conn_stat_evt->addr;
g_ble_state.ble_connect_handle = conn_stat_evt->handle;
g_ble_state.ble_connection = BT_CONNECTED;
}
else
{
g_ble_state.ble_connection = BT_DISCONNECTED;
}
if (ble_common_ops && ble_common_ops->connect_status_changed)
{
ble_common_ops->connect_status_changed(&g_ble_state, conn_stat_evt->connected);
}
else
{
_err("%s [BLE][Common] Connect status callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_connect_dev_name(struct ble_event_dev_name_t *dev_name_evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ble_common_ops = g_bt_common_state.ble_common_ops;
if (ble_common_ops && ble_common_ops->connected_device_name_resp)
{
memcpy(g_ble_state.bt_target_name, dev_name_evt->name, BT_NAME_LEN);
ble_common_ops->connected_device_name_resp(dev_name_evt->name);
}
else
{
_err("%s [BLE][Common] Connected device name callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_advertise_report(struct ble_event_adv_rept_t *adv_rept_evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ble_common_ops = g_bt_common_state.ble_common_ops;
if (ble_common_ops && ble_common_ops->scan_result)
{
ble_common_ops->scan_result(adv_rept_evt->addr, adv_rept_evt->data, adv_rept_evt->length);
}
else
{
_err("%s [BLE][Common] Advertise report callback failed(CB not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_mtusize(struct ble_event_mtusize_t *evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ops = g_bt_common_state.ble_common_ops;
if (ops && ops->mtusize)
{
ops->mtusize(evt->handle, evt->mtusize);
}
else
{
_err("%s [BLE][Common] callback not registered.\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_save_bond(struct ble_event_bondinfo_t *evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ops = g_bt_common_state.ble_common_ops;
if (ops && ops->save_bondinfo)
{
ops->save_bondinfo(evt->num, evt->bond);
}
else
{
_err("%s [BLE][Common] callback not registered.\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_load_bond(struct ble_event_bondinfo_t *evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ops = g_bt_common_state.ble_common_ops;
if (ops && ops->load_bondinfo)
{
ret = ops->load_bondinfo(evt->num, evt->bond);
}
else
{
_err("%s [BLE][Common] callback not registered.\n", __func__);
return BT_FAIL;
}
return ret;
}
static int ble_event_encryption_result(struct ble_event_encryption_result_t *evt)
{
int ret = BT_SUCCESS;
struct ble_common_ops_s *ops = g_bt_common_state.ble_common_ops;
if (ops && ops->encryption_result)
{
ops->encryption_result(evt->conn_handle, evt->result);
}
else
{
_err("%s [BLE][Common] callback not registered.\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bt_init
*
* Description:
* Initialize BT module(HAL, File system, Pin config, UART config, etc).
*
****************************************************************************/
int bt_init(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops;
/* Initialize HAL module */
ret = bt_hal_init();
if (ret != BT_SUCCESS)
{
_err("%s [BT][Common] Initialization failed(HAL initialize failed).\n", __func__);
return ret;
}
/* Pick HAL callbacks */
bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->init)
{
ret = bt_hal_common_ops->init();
}
return ret;
}
/****************************************************************************
* Name: bt_finalize
*
* Description:
* Finalize BT module.
*
****************************************************************************/
int bt_finalize(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->finalize)
{
ret = bt_hal_common_ops->finalize();
}
ble_gatt_finalize();
return ret;
}
/****************************************************************************
* Name: bt_set_address
*
* Description:
* Set bluetooth device address.
* If not set address, use default address .
*
****************************************************************************/
int bt_set_address(BT_ADDR *addr)
{
int ret = BT_SUCCESS;
if (!addr)
{
_err("%s [BT][Common] Set local BT address failed(addr not set).\n", __func__);
return BT_FAIL;
}
memcpy(&g_bt_common_state.bt_addr, addr, sizeof(BT_ADDR));
return ret;
}
/****************************************************************************
* Name: bt_get_address
*
* Description:
* Get current bluetooth device address.
*
****************************************************************************/
int bt_get_address(BT_ADDR *addr)
{
int ret = BT_SUCCESS;
BT_ADDR target_addr;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (!addr)
{
_err("%s [BT][Common] addr not allocated.\n", __func__);
return BT_FAIL;
}
if (bt_hal_common_ops && bt_hal_common_ops->getDevAddr)
{
ret = bt_hal_common_ops->getDevAddr(&target_addr);
memcpy(addr, &target_addr, sizeof(BT_ADDR));
}
else
{
_err("%s [BT][Common] Get local address failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_set_name
*
* Description:
* Set bluetooth device name.
*
****************************************************************************/
int bt_set_name(char *name)
{
int ret = BT_SUCCESS;
if (!name)
{
_err("%s [BT][Common] Set BT name failed(name not set).\n", __func__);
return BT_FAIL;
}
memcpy(g_bt_common_state.bt_name, name, sizeof(char) * BT_NAME_LEN);
return ret;
}
/****************************************************************************
* Name: bt_get_name
*
* Description:
* Get current bluetooth device name.
*
****************************************************************************/
int bt_get_name(char *name)
{
int ret = BT_SUCCESS;
char target_name[BT_NAME_LEN];
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (!name)
{
_err("%s [BT][Common] name not allocated.\n", __func__);
return BT_FAIL;
}
if (bt_hal_common_ops && bt_hal_common_ops->getDevName)
{
ret = bt_hal_common_ops->getDevName(target_name);
memcpy(name, target_name, sizeof(char) * BT_NAME_LEN);
}
else
{
_err("%s [BT][Common] Get local name failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_enable
*
* Description:
* Enable bluetooth module(Power ON, Reset, Firmware load, set device name, etc).
*
****************************************************************************/
int bt_enable(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->enable)
{
ret = bt_hal_common_ops->enable(true);
}
else
{
_err("%s [BT][Common] Enabling failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
if (bt_hal_common_ops && bt_hal_common_ops->setDevAddr)
{
ret = bt_hal_common_ops->setDevAddr(&g_bt_common_state.bt_addr);
}
if (bt_hal_common_ops && bt_hal_common_ops->setDevName)
{
ret = bt_hal_common_ops->setDevName(g_bt_common_state.bt_name);
}
return ret;
}
/****************************************************************************
* Name: bt_disable
*
* Description:
* Disable bluetooth module(Power OFF).
*
****************************************************************************/
int bt_disable(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->enable)
{
ret = bt_hal_common_ops->enable(false);
}
else
{
_err("%s [BT][Common] Disabling failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_pairing_enable
*
* Description:
* Start pairing mode with pairing .
*
****************************************************************************/
int bt_pairing_enable(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->paringEnable)
{
ret = bt_hal_common_ops->paringEnable(true);
}
else
{
_err("%s [BT][Common] Paring enable failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_paring_disable
*
* Description:
* Cancel pairing mode.
*
****************************************************************************/
int bt_paring_disable(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->paringEnable)
{
ret = bt_hal_common_ops->paringEnable(false);
}
else
{
_err("%s [BT][Common] Paring disable failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_get_bond_list
*
* Description:
* Get bonding device BT_ADDR list.
*
****************************************************************************/
int bt_get_bond_list(BT_ADDR *addr, int *num)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->getBondList)
{
ret = bt_hal_common_ops->getBondList(addr, num);
}
else
{
_err("%s [BT][Common] Get bonding list failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_unbond
*
* Description:
* Unbond device by BT_ADDR.
*
****************************************************************************/
int bt_unbond(BT_ADDR *addr)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->unBond)
{
ret = bt_hal_common_ops->unBond(addr);
}
else
{
_err("%s [BT][Common] Get bonding list failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_set_visible
*
* Description:
* Set bluetooth device to visible for connect from peer device.
*
****************************************************************************/
int bt_set_visibility(BT_VISIBILITY visibility)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->setVisibility)
{
ret = bt_hal_common_ops->setVisibility(visibility);
}
else
{
_err("%s [BT][Common] Set visibility failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_start_inquiry
*
* Description:
* Start inquiry to device by name.
*
****************************************************************************/
int bt_start_inquiry(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->inquiryStart)
{
ret = bt_hal_common_ops->inquiryStart();
}
else
{
_err("%s [BT][Common] Inquiry start(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_cancel_inquiry
*
* Description:
* Cancel to current inquiry.
*
****************************************************************************/
int bt_cancel_inquiry(void)
{
int ret = BT_SUCCESS;
struct bt_hal_common_ops_s *bt_hal_common_ops = g_bt_common_state.bt_hal_common_ops;
if (bt_hal_common_ops && bt_hal_common_ops->inquiryCancel)
{
ret = bt_hal_common_ops->inquiryCancel();
}
else
{
_err("%s [BT][Common] Inquiry cancel failed(HAL not registered).\n", __func__);
return BT_FAIL;
}
return ret;
}
/****************************************************************************
* Name: bt_register_common_cb
*
* Description:
* Register A2DP generic BT operation callback for Application.
*
****************************************************************************/
int bt_register_common_cb(struct bt_common_ops_s *bt_common_ops)
{
if (!bt_common_ops)
{
_err("%s [BT][Common] Set application callback failed.\n", __func__);
return BT_FAIL;
}
g_bt_common_state.bt_common_ops = bt_common_ops;
return BT_SUCCESS;
}
/****************************************************************************
* Name: bt_common_register_hal
*
* Description:
* Register common HAl interface.
*
****************************************************************************/
int bt_common_register_hal(struct bt_hal_common_ops_s *bt_hal_common_ops)
{
if (!bt_hal_common_ops)
{
_err("%s [BT][Common] Set HAL callback failed.\n", __func__);
return BT_FAIL;
}
g_bt_common_state.bt_hal_common_ops = bt_hal_common_ops;
return BT_SUCCESS;
}
/****************************************************************************
* Name: bt_common_event_handler
*
* Description:
* Handler of generic event.
* Receive generic event from HAL and dispatch event for application.
*
****************************************************************************/
int bt_common_event_handler(struct bt_event_t *bt_event)
{
switch (bt_event->event_id)
{
case BT_COMMON_EVENT_CMD_STATUS:
return bt_event_cmd_status((struct bt_event_cmd_stat_t *) bt_event);
case BT_COMMON_EVENT_PAIRING_COMPLETE:
return bt_event_pairing_complete((struct bt_event_pair_cmplt_t *) bt_event);
case BT_COMMON_EVENT_INQUIRY_RESULT:
return bt_event_inquiry_result((struct bt_event_inquiry_rslt_t *) bt_event);
case BT_COMMON_EVENT_INQUIRY_COMPLETE:
return bt_event_inquiry_complete();
case BT_COMMON_EVENT_CONN_STAT_CHANGE:
return bt_event_conn_stat_change((struct bt_event_conn_stat_t *) bt_event);
case BT_COMMON_EVENT_CONN_DEV_NAME:
return bt_event_conn_dev_name((struct bt_event_dev_name_t *) bt_event);
case BT_COMMON_EVENT_BOND_INFO:
return bt_event_bond_info((struct bt_event_bond_info_t *) bt_event);
default:
break;
}
return BT_SUCCESS;
}
/****************************************************************************
* Name: ble_set_address
*
* Description:
* Set Bluetooth LE module address
* This is Spresense side address and should be call before bt_enable.
*
****************************************************************************/
int ble_set_address(BT_ADDR *addr)
{
int ret = BT_SUCCESS;
if (!addr)
{
_err("%s [BLE][Common] Set local BT address failed(addr not set).\n", __func__);
return BT_FAIL;
}
memcpy(&g_bt_common_state.ble_addr, addr, sizeof(BT_ADDR));
return ret;
}
/****************************************************************************
* Name: ble_get_address
*
* Description:
* Get Bluetooth LE module address
*
****************************************************************************/
int ble_get_address(BT_ADDR *addr)
{
int ret = BT_SUCCESS;
if (!addr)
{
_err("%s [BLE][Common] addr not allocated.\n", __func__);
return BT_FAIL;
}
memcpy(addr, &g_bt_common_state.ble_addr, sizeof(BT_ADDR));
return ret;
}
/****************************************************************************
* Name: ble_set_name
*
* Description:
* Set Bluetooth LE module name
* This name visible for other devices and should be call before bt_enable.
*
****************************************************************************/
int ble_set_name(char *name)
{
int ret = BT_SUCCESS;
if (!name)
{
_err("%s [BLE][Common] Set BT name failed(name not set).\n", __func__);
return BT_FAIL;
}
memcpy(g_bt_common_state.ble_name, name, sizeof(char) * BT_NAME_LEN);
return ret;
}
/****************************************************************************
* Name: ble_get_name
*
* Description:
* Get Bluetooth LE module name
*
****************************************************************************/
int ble_get_name(char *name)
{
int ret = BT_SUCCESS;
if (!name)
{
_err("%s [BLE][Common] name not allocated.\n", __func__);
return BT_FAIL;
}
memcpy(name, g_bt_common_state.ble_name, sizeof(char) * BT_NAME_LEN);
return ret;
}
/****************************************************************************
* Name: ble_enable
*
* Description:
* Bluetooth LE enable
* Set initialize parameter to HAL(Name/Addr/PPCP/Appearance)
*
****************************************************************************/
int ble_enable(void)
{
int ret = BT_SUCCESS;
struct ble_hal_common_ops_s *ble_hal_common_ops = g_bt_common_state.ble_hal_common_ops;
BLE_CONN_PARAMS ppcp = {0xFFFF, 0xFFFF, 0x00, 0xFFFF};
if (ble_hal_common_ops && ble_hal_common_ops->setDevName &&
ble_hal_common_ops->setDevAddr &&
ble_hal_common_ops->setAppearance &&
ble_hal_common_ops->setPPCP)
{
ret = ble_hal_common_ops->setDevName(g_bt_common_state.ble_name);
if (ret != BT_SUCCESS)
{
_err("%s [BLE][Common] BLE set name failed.\n", __func__);
return ret;
}
ret = ble_hal_common_ops->setDevAddr(&g_bt_common_state.ble_addr);
if (ret != BT_SUCCESS)
{
_err("%s [BLE][Common] BLE set address failed.\n", __func__);
return ret;
}
ret = ble_hal_common_ops->setAppearance(BLE_APPEARANCE_GENERIC_PHONE);
if (ret != BT_SUCCESS)
{
_err("%s [BLE][Common] BLE set appearance failed.\n", __func__);
return ret;
}
ret = ble_hal_common_ops->setPPCP(ppcp);
if (ret != BT_SUCCESS)
{
_err("%s [BLE][Common] BLE set PPCP failed.\n", __func__);
return ret;