-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_vendor_nxp.c
1719 lines (1580 loc) · 52.1 KB
/
bt_vendor_nxp.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 2012 The Android Open Source Project
* Portions copyright (C) 2009-2012 Broadcom Corporation
* Portions copyright 2012-2013, 2015, 2018-2022 NXP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* Filename: bt_vendor_nxp.c
*
* Description: NXP vendor specific library implementation
*
******************************************************************************/
#define LOG_TAG "bt-vnd-nxp"
#include <ctype.h>
#include <cutils/properties.h>
#include <errno.h>
#include <grp.h>
#include <pthread.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifdef FW_LOADER_V2
#include "fw_loader_uart_v2.h"
#else
#include "fw_loader_uart.h"
#endif
#include <linux/gpio.h>
#include "bt_vendor_nxp.h"
#include "bt_vendor_log.h"
/******************************************************************************
**
** Constants and Macro's
**
******************************************************************************/
/*[NK] @NXP - Driver FIX
ioctl command to release the read thread before driver close */
#define MBTCHAR_IOCTL_RELEASE _IO('M', 1)
/*
* Defines for wait for Bluetooth firmware ready Specify durations
* between polls and max wait time
*/
#define POLL_DRIVER_DURATION_US (100000)
#define POLL_DRIVER_MAX_TIME_MS (20000)
#define POLL_CONFIG_UART_MS (10)
#define POLL_INBAND_COMMAND_MS (1)
#define POLL_MAX_TIMOUT_MS (1000)
#define CONF_COMMENT '#'
#define CONF_DELIMITERS " =\n\r\t"
#define CONF_VALUES_DELIMITERS "=\n\r\t"
#define CONF_MAX_LINE_LEN 1024
#define UNUSED(x) (void)(x)
#define BD_ADDR_LEN 6
/******************************************************************************
** Variables
******************************************************************************/
int mchar_fd = 0;
int vhal_trace_level = BT_TRACE_LEVEL_INFO;
struct termios ti;
static uint8_t adapterState;
unsigned char* bdaddr = NULL;
const bt_vendor_callbacks_t* vnd_cb = NULL;
/* for NXP USB/SD interface */
static char mbt_port[MAX_PATH_LEN] = "/dev/mbtchar0";
/* for NXP Uart interface */
static char mchar_port[MAX_PATH_LEN] = "/dev/ttyUSB0";
static int is_uart_port = 0;
static int32_t baudrate_fw_init = 115200;
static int32_t baudrate_bt = 3000000;
int write_bdaddrss = 0;
int8_t ble_1m_power = 0;
int8_t ble_2m_power = 0;
uint8_t set_1m_2m_power = 0;
int8_t bt_max_power_sel = 0;
uint8_t bt_set_max_power = 0;
uint8_t independent_reset_gpio_pin = 0xFF;
bool enable_sco_config = TRUE;
bool enable_pdn_recovery = FALSE;
bool use_controller_addr = TRUE;
uint8_t ir_host_gpio_pin = 14;
static char chrdev_name[32] = "/dev/gpiochip5";
/* 0:disable IR(default); 1:OOB IR(FW Config); 2:Inband IR;*/
uint8_t independent_reset_mode = IR_MODE_NONE;
/* 0:disable OOB IR Trigger; 1:RFKILL Trigger; 2:GPIO Trigger;*/
uint8_t send_oob_ir_trigger = IR_TRIGGER_NONE;
bool enable_heartbeat_config = FALSE;
char pFilename_fw_init_config_bin[MAX_PATH_LEN];
uint8_t write_bd_address[WRITE_BD_ADDRESS_SIZE] = {
0xFE, /* Parameter ID */
0x06, /* bd_addr length */
0x00, /* 6th byte of bd_addr */
0x00, /* 5th */
0x00, /* 4th */
0x00, /* 3rd */
0x00, /* 2nd */
0x00 /* 1st */
};
#ifdef UART_DOWNLOAD_FW
static int enable_download_fw = 0;
static int uart_sleep_after_dl = 100;
static int download_helper = 0;
static bool auto_select_fw_name = TRUE;
static int32_t baudrate_dl_helper = 115200;
static int32_t baudrate_dl_image = 3000000;
static char pFileName_helper[MAX_PATH_LEN] =
"/vendor/firmware/helper_uart_3000000.bin";
static char pFileName_image[MAX_PATH_LEN] =
"/vendor/firmware/uart8997_bt_v4.bin";
static int32_t iSecondBaudrate = 0;
uint8_t enable_poke_controller = 0;
static bool send_boot_sleep_trigger = FALSE;
#endif
char pFilename_cal_data[MAX_PATH_LEN];
static int rfkill_id = -1;
static char* rfkill_state_path = NULL;
int last_baudrate = 0;
wakeup_gpio_config_t wakeup_gpio_config[wakeup_key_num] = {
{.gpio_pin = 13, .high_duration = 2, .low_duration = 2},
{.gpio_pin = 13, .high_duration = 4, .low_duration = 4}};
wakeup_adv_pattern_config_t wakeup_adv_config = {.length = 0};
wakeup_scan_param_config_t wakeup_scan_param_config = {.le_scan_type = 0,
.interval = 128,
.window = 96,
.own_addr_type = 0,
.scan_filter_policy = 0};
wakeup_local_param_config_t wakup_local_param_config = {.heartbeat_timer_value =
8};
/*****************************************************************************
**
** HELPER FUNCTIONS
**
*****************************************************************************/
typedef int(conf_action_t)(char* p_conf_name, char* p_conf_value, int param);
typedef struct {
const char* conf_entry;
conf_action_t* p_action;
int param;
} conf_entry_t;
static int set_enable_sco_config(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
enable_sco_config = (atoi(p_conf_value) == 0) ? FALSE : TRUE;
return 0;
}
static int set_use_controller_addr(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
use_controller_addr = (atoi(p_conf_value) == 0) ? FALSE : TRUE;
return 0;
}
static int set_mchar_port(char* p_conf_name, char* p_conf_value, int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(mchar_port, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
is_uart_port = 1;
return 0;
}
static int set_mbt_port(char* p_conf_name, char* p_conf_value, int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(mbt_port, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
return 0;
}
static int set_baudrate_bt(char* p_conf_name, char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
baudrate_bt = atoi(p_conf_value);
return 0;
}
static int set_baudrate_fw_init(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
baudrate_fw_init = atoi(p_conf_value);
return 0;
}
static int set_ble_1m_power(char* p_conf_name, char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
ble_1m_power = atoi(p_conf_value);
set_1m_2m_power |= BLE_SET_1M_POWER;
return 0;
}
static int set_ble_2m_power(char* p_conf_name, char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
ble_2m_power = atoi(p_conf_value);
set_1m_2m_power |= BLE_SET_2M_POWER;
return 0;
}
static int set_bt_tx_power(char* p_conf_name, char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
bt_max_power_sel = atoi(p_conf_value);
bt_set_max_power = 1;
return 0;
}
static int set_pFilename_fw_init_config_bin(char* p_conf_name,
char* p_conf_value, int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(pFilename_fw_init_config_bin, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
return 0;
}
static int set_independent_reset_mode(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
independent_reset_mode = atoi(p_conf_value);
return 0;
}
static int set_enable_pdn_recovery(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
enable_pdn_recovery = (atoi(p_conf_value) == 0) ? FALSE : TRUE;
return 0;
}
static int set_send_oob_ir_trigger(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
send_oob_ir_trigger = atoi(p_conf_value);
return 0;
}
static int set_independent_reset_gpio_pin(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
independent_reset_gpio_pin = atoi(p_conf_value);
return 0;
}
static int set_oob_ir_host_gpio_pin(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
ir_host_gpio_pin = atoi(p_conf_value);
return 0;
}
static int set_charddev_name(char* p_conf_name, char* p_conf_value, int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_DEVICE_LEN) {
strncpy(chrdev_name, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_DEVICE_LEN);
}
return 0;
}
static int set_bd_address_buf(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
int i = 0;
int j = 7;
int len = 0;
if (p_conf_value == NULL) return 0;
len = strlen(p_conf_value);
if (len != 17) return 0;
for (i = 0; i < len; i++) {
if (((i + 1) % 3) == 0 && p_conf_value[i] != ':') return 0;
if (((i + 1) % 3) != 0 && !isxdigit(p_conf_value[i])) return 0;
char tmp = p_conf_value[i];
if (isupper(p_conf_value[i])) {
p_conf_value[i] = p_conf_value[i] - 'A' + 10;
} else if (islower(p_conf_value[i])) {
p_conf_value[i] = p_conf_value[i] - 'a' + 10;
} else if (isdigit(p_conf_value[i])) {
p_conf_value[i] = p_conf_value[i] - '0';
} else if (p_conf_value[i] == ':')
p_conf_value[i] = tmp;
else
return 0;
}
for (i = 0; i < 17; i++) {
write_bd_address[j--] = (p_conf_value[i] << 4) | p_conf_value[i + 1];
i = i + 2;
}
write_bdaddrss = 1;
return 0;
}
#ifdef UART_DOWNLOAD_FW
static int set_enable_download_fw(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
enable_download_fw = atoi(p_conf_value);
return 0;
}
static int set_pFileName_image(char* p_conf_name, char* p_conf_value,
int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(pFileName_image, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
auto_select_fw_name = FALSE;
return 0;
}
static int set_pFileName_helper(char* p_conf_name, char* p_conf_value,
int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(pFileName_helper, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
download_helper = 1;
return 0;
}
static int set_baudrate_dl_helper(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
baudrate_dl_helper = atoi(p_conf_value);
return 0;
}
static int set_baudrate_dl_image(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
baudrate_dl_image = atoi(p_conf_value);
return 0;
}
static int set_iSecondBaudrate(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
iSecondBaudrate = atoi(p_conf_value);
return 0;
}
static int set_uart_sleep_after_dl(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
uart_sleep_after_dl = atoi(p_conf_value);
return 0;
}
static int set_enable_poke_controller(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
enable_poke_controller = atoi(p_conf_value);
return 0;
}
static int set_send_boot_sleep_trigger(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
send_boot_sleep_trigger = (atoi(p_conf_value) == 0) ? FALSE : TRUE;
return 0;
}
#endif
static int set_Filename_cal_data(char* p_conf_name, char* p_conf_value,
int param) {
int len_p_conf_value = 0;
UNUSED(p_conf_name);
UNUSED(param);
len_p_conf_value = strlen(p_conf_value);
if (len_p_conf_value < MAX_PATH_LEN) {
strncpy(pFilename_cal_data, p_conf_value, len_p_conf_value + 1);
} else {
VND_LOGE("String length too long, unable to process");
VND_LOGW("Source length: %d and destination length: %d", len_p_conf_value,
MAX_PATH_LEN);
}
return 0;
}
static int set_vhal_trace_level(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
vhal_trace_level = atoi(p_conf_value);
return 0;
}
static int set_enable_heartbeat_config(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
enable_heartbeat_config = (atoi(p_conf_value) == 0) ? FALSE : TRUE;
return 0;
}
static int set_powerkey_gpio_pin(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_power_key].gpio_pin =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_powerkey_gpio_high_duration(char* p_conf_name,
char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_power_key].high_duration =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_powerkey_gpio_low_duration(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_power_key].low_duration =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_netflixkey_gpio_pin(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_netflix_key].gpio_pin =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_netflixkey_gpio_high_duration(char* p_conf_name,
char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_netflix_key].high_duration =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_netflixkey_gpio_low_duration(char* p_conf_name,
char* p_conf_value, int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_gpio_config[wakeup_netflix_key].low_duration =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_wakeup_adv_pattern(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
if (wakeup_adv_config.length >= NXP_WAKEUP_ADV_PATTERN_LENGTH) {
VND_LOGE("%s, wrong length:%d", __func__, wakeup_adv_config.length);
return -1;
}
wakeup_adv_config.adv_pattern[wakeup_adv_config.length] =
(unsigned char)atoi(p_conf_value);
wakeup_adv_config.length += 1;
return 0;
}
static int set_wakeup_scan_type(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_scan_param_config.le_scan_type = (unsigned char)atoi(p_conf_value);
return 0;
}
static int set_wakeup_scan_interval(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_scan_param_config.interval = (unsigned short)atoi(p_conf_value);
return 0;
}
static int set_wakeup_scan_window(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_scan_param_config.window = (unsigned short)atoi(p_conf_value);
return 0;
}
static int set_wakeup_scan_own_addr_type(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_scan_param_config.own_addr_type = (unsigned char)atoi(p_conf_value);
return 0;
}
static int set_wakeup_scan_filter_policy(char* p_conf_name, char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakeup_scan_param_config.scan_filter_policy =
(unsigned char)atoi(p_conf_value);
return 0;
}
static int set_wakeup_local_heartbeat_timer_value(char* p_conf_name,
char* p_conf_value,
int param) {
UNUSED(p_conf_name);
UNUSED(param);
wakup_local_param_config.heartbeat_timer_value =
(unsigned char)atoi(p_conf_value);
return 0;
}
/*
* Current supported entries and corresponding action functions
*/
static const conf_entry_t conf_table[] = {
{"mchar_port", set_mchar_port, 0},
{"mbt_port", set_mbt_port, 0},
{"baudrate_bt", set_baudrate_bt, 0},
{"baudrate_fw_init", set_baudrate_fw_init, 0},
{"bd_address", set_bd_address_buf, 0},
{"pFilename_fw_init_config_bin", set_pFilename_fw_init_config_bin, 0},
{"ble_1m_power", set_ble_1m_power, 0},
{"ble_2m_power", set_ble_2m_power, 0},
{"bt_max_power_sel", set_bt_tx_power, 0},
{"independent_reset_gpio_pin", set_independent_reset_gpio_pin, 0},
{"oob_ir_host_gpio_pin", set_oob_ir_host_gpio_pin, 0},
{"chardev_name", set_charddev_name, 0},
{"independent_reset_mode", set_independent_reset_mode, 0},
{"send_oob_ir_trigger", set_send_oob_ir_trigger, 0},
#ifdef UART_DOWNLOAD_FW
{"enable_download_fw", set_enable_download_fw, 0},
{"pFileName_image", set_pFileName_image, 0},
{"pFileName_helper", set_pFileName_helper, 0},
{"baudrate_dl_helper", set_baudrate_dl_helper, 0},
{"baudrate_dl_image", set_baudrate_dl_image, 0},
{"iSecondBaudrate", set_iSecondBaudrate, 0},
{"uart_sleep_after_dl", set_uart_sleep_after_dl, 0},
{"enable_poke_controller", set_enable_poke_controller, 0},
{"send_boot_sleep_trigger", set_send_boot_sleep_trigger, 0},
#endif
{"enable_pdn_recovery", set_enable_pdn_recovery, 0},
{"pFilename_cal_data", set_Filename_cal_data, 0},
{"vhal_trace_level", set_vhal_trace_level, 0},
{"enable_sco_config", set_enable_sco_config, 0},
{"use_controller_addr", set_use_controller_addr, 0},
{"enable_heartbeat_config", set_enable_heartbeat_config, 0},
{"wakeup_power_gpio_pin", set_powerkey_gpio_pin, 0},
{"wakeup_power_gpio_high_duration", set_powerkey_gpio_high_duration, 0},
{"wakeup_power_gpio_low_duration", set_powerkey_gpio_low_duration, 0},
{"wakeup_netflix_gpio_pin", set_netflixkey_gpio_pin, 0},
{"wakeup_netflix_gpio_high_duration", set_netflixkey_gpio_high_duration, 0},
{"wakeup_netflix_gpio_low_duration", set_netflixkey_gpio_low_duration, 0},
{"wakeup_adv_pattern", set_wakeup_adv_pattern, 0},
{"wakeup_scan_type", set_wakeup_scan_type, 0},
{"wakeup_scan_interval", set_wakeup_scan_interval, 0},
{"wakeup_scan_window", set_wakeup_scan_window, 0},
{"wakeup_own_addr_type", set_wakeup_scan_own_addr_type, 0},
{"wakeup_scan_filter_policy", set_wakeup_scan_filter_policy, 0},
{"wakeup_local_heartbeat_timer_value",
set_wakeup_local_heartbeat_timer_value, 0},
{(const char*)NULL, NULL, 0}};
/*******************************************************************************
**
** Function vnd_load_conf
**
** Description Read conf file from mentioned p_path at run time and read
** one by one entry and call the corresponding config function
**
** Returns None
**
*******************************************************************************/
static void vnd_load_conf(const char* p_path) {
FILE* p_file;
char* p_name;
char* p_value;
conf_entry_t* p_entry;
char line[CONF_MAX_LINE_LEN + 1]; /* add 1 for \0 char */
VND_LOGD("Attempt to load conf from %s", p_path);
if ((p_file = fopen(p_path, "r")) != NULL) {
/* read line by line */
while (fgets(line, CONF_MAX_LINE_LEN + 1, p_file) != NULL) {
if (line[0] == CONF_COMMENT) continue;
p_name = strtok(line, CONF_DELIMITERS);
if (NULL == p_name) {
continue;
}
p_value = strtok(NULL, CONF_DELIMITERS);
if (NULL == p_value) {
ALOGW("vnd_load_conf: missing value for name: %s", p_name);
continue;
}
p_entry = (conf_entry_t*)conf_table;
while (p_entry->conf_entry != NULL) {
if (strcmp(p_entry->conf_entry, (const char*)p_name) == 0) {
p_entry->p_action(p_name, p_value, p_entry->param);
break;
}
p_entry++;
}
}
fclose(p_file);
} else {
VND_LOGW("vnd_load_conf file >%s< not found", p_path);
}
}
/******************************************************************************
**
** Function: read_hci_event
**
** Description: Reads the parameter of event received from controller.
**
** Return Value: offset of parameter
**
*
*****************************************************************************/
static int read_hci_event(int fd, unsigned char* buf, int size,
int retry_delay_ms, int max_duration) {
int remain, r;
int count = 0;
int total_duration = 0;
if (size <= 0) return -1;
/* The first byte identifies the packet type. For HCI event packets, it
* should be 0x04, so we read until we get to the 0x04. */
VND_LOGD("start read hci event 0x4");
while (total_duration < max_duration) {
r = read(fd, buf, 1);
if (r <= 0) {
VND_LOGV("read hci event 0x04 failed, retry");
usleep(retry_delay_ms * 1000);
total_duration += retry_delay_ms;
continue;
}
if (buf[0] == 0x04) break;
}
if (total_duration >= max_duration) {
VND_LOGE("read hci event 0x04 failed, return error. total_duration = %d",
total_duration);
return -1;
}
count++;
/* The next two bytes are the event code and parameter total length. */
VND_LOGD("start read hci event code and len");
while (count < 3) {
r = read(fd, buf + count, 3 - count);
if (r <= 0) {
VND_LOGE("read hci event code and len failed");
return -1;
}
count += r;
}
/* Now we read the parameters. */
VND_LOGD("start read hci event para");
if (buf[2] < (size - 3))
remain = buf[2];
else
remain = size - 3;
if ((int)remain > (size - 3)) {
return -1;
}
while ((count - 3) < remain) {
r = read(fd, buf + count, remain - (count - 3));
if (r <= 0) {
VND_LOGE("read hci event para failed");
return -1;
}
count += r;
}
VND_LOGD("over read count = %d", count);
return count;
}
int set_prop_int32(char* name, int value) {
char init_value[PROPERTY_VALUE_MAX];
int ret;
sprintf(init_value, "%d", value);
ret = property_set(name, init_value);
if (ret < 0) {
VND_LOGE("set_prop_int32 failed: %d", ret);
}
return ret;
}
int get_prop_int32(char* name) {
int ret;
ret = property_get_int32(name, -1);
VND_LOGD("get_prop_int32: %d", ret);
if (ret < 0) {
return 0;
}
return ret;
}
/******************************************************************************
**
** Function: uart_speed
**
** Description: Return the baud rate corresponding to the frequency.
**
** Return Value: Baudrate
*
*****************************************************************************/
static int32 uart_speed(int32 s) {
switch (s) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 3000000:
return B3000000;
default:
return B0;
}
}
/******************************************************************************
**
** Function: uart_speed_translate
**
** Description: Return the frequency to corresponding baud rate.
**
** Return Value: Baudrate
*
*****************************************************************************/
static int32 uart_speed_translate(int32 s) {
switch (s) {
case B9600:
return 9600;
case B19200:
return 19200;
case B38400:
return 38400;
case B57600:
return 57600;
case B115200:
return 115200;
case B230400:
return 230400;
case B460800:
return 460800;
case B500000:
return 500000;
case B576000:
return 576000;
case B921600:
return 921600;
case B1000000:
return 1000000;
case B1152000:
return 1152000;
case B1500000:
return 1500000;
case B3000000:
return 3000000;
default:
return 0;
}
}
/******************************************************************************
*
** Function uart_get_speed
**
** Description Get the last baud rate speed.
** Return Value: Value of last baud rate
**
*****************************************************************************/
static int32 uart_get_speed(struct termios* ti) {
int32 speed = 0;
speed = (int32)cfgetospeed(ti);
return (uart_speed_translate(speed));
}
/******************************************************************************
*
** Function uart_set_speed
**
** Description Set the baud rate speed.
** Return Value: 0 On success else -1
**
*****************************************************************************/
static int32 uart_set_speed(int32 fd, struct termios* ti, int32 speed) {
if (cfsetospeed(ti, uart_speed(speed)) < 0) {
VND_LOGE("Set O speed failed!");
return -1;
}
if (cfsetispeed(ti, uart_speed(speed)) < 0) {
VND_LOGE("Set I speed failed!");
return -1;
}
if (tcsetattr(fd, TCSANOW, ti) < 0) {
VND_LOGE("Set Attr speed failed!");
return -1;
}
VND_LOGD("Baudrate set to %d", speed);
return 0;
}
/******************************************************************************
**
** Name: init_uart
**
** Description Initialize UART.
**
** Return Value Valid fd on success
**
*****************************************************************************/
int32 init_uart(int8* dev, int32 dwBaudRate, uint8 ucFlowCtrl) {
int32 fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) {
VND_LOGE("Can't open serial port");
return -1;
}
tcflush(fd, TCIOFLUSH);
if (tcgetattr(fd, &ti) < 0) {
VND_LOGE("Can't get port settings");
close(fd);
return -1;
}
cfmakeraw(&ti);
#ifdef FW_LOADER_V2
ti.c_cflag |= CLOCAL | CREAD;
#else
ti.c_cflag |= CLOCAL;
#endif
/* Set 1 stop bit & no parity (8-bit data already handled by cfmakeraw) */
ti.c_cflag &= ~(CSTOPB | PARENB);
if (ucFlowCtrl) {
ti.c_cflag |= CRTSCTS;
} else {
ti.c_cflag &= ~CRTSCTS;
}
/*FOR READS: set timeout time w/ no minimum characters needed
(since we read only 1 at at time...) */
ti.c_cc[VMIN] = 0;
ti.c_cc[VTIME] = TIMEOUT_SEC * 10;
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
VND_LOGE("Can't set port settings");
close(fd);
return -1;
}
tcflush(fd, TCIOFLUSH);
if (independent_reset_mode == IR_MODE_INBAND_VSC) {
last_baudrate = uart_get_speed(&ti);
VND_LOGD("Last buad rate = %d", last_baudrate);
}
/* Set actual baudrate */
if (uart_set_speed(fd, &ti, dwBaudRate) < 0) {
VND_LOGD("Can't set baud rate");
close(fd);
return -1;
}
return fd;
}