lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
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
/******************************************************************************
 *
 *  Copyright (C) 2020-2021 SeekWave Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 ******************************************************************************/
 
/******************************************************************************
 *
 *  Filename:      bt_vendor_skw.c
 *
 *  Description:   SeekWave vendor specific library implementation
 *
 ******************************************************************************/
 
#undef NDEBUG
#define LOG_TAG "libbt_vendor"
 
 
#include <utils/Log.h>
#include "bt_vendor_skw.h"
#include "scom_vendor.h"
#include "skw_btsnoop.h"
#include "skw_log.h"
#include "skw_gen_addr.h"
#include "skw_common.h"
#include <cutils/properties.h>
#include "skw_ext.h"
 
#define SKWBT_CONFIG_FILE       "/vendor/etc/bluetooth/skwbt.conf"
 
bt_vendor_callbacks_t *bt_vendor_cbacks = NULL;
uint8_t vnd_local_bd_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t port_cnts = 0;
 
char skwbt_transtype = 0;
static char skwbt_device_node[BT_COM_PORT_SIZE][DEVICE_NODE_MAX_LEN + 1];
 
int  btboot_fp = -1;
int  btpw_fp = -1;
char btsnoop_log_en = FALSE;
char btcp_log_en = FALSE;
char skwlog_slice = FALSE;
char skwdriverlog_en = TRUE;
char skwbtuartonly = TRUE;
char skwbtNoSleep = FALSE; //No Sleep
 
extern char skw_btsnoop_path[];
extern char btsnoop_save_log;
extern Wakeup_ADV_Info_St   wakeup_ADV_Info;
 
static char *trim(char *str)
{
    while (isspace(*str))
    {
        ++str;
    }
 
    if (!*str)
    {
        return str;
    }
 
    char *end_str = str + strlen(str) - 1;
    while (end_str > str && isspace(*end_str))
    {
        --end_str;
    }
 
    end_str[1] = '\0';
    return str;
}
 
 
static void load_skwbt_conf()
{
    memset(skwbt_device_node, 0, sizeof(skwbt_device_node));
    FILE *fp = fopen(SKWBT_CONFIG_FILE, "rt");
    if (!fp)
    {
        //ALOGE("%s unable to open file '%s': %s", __func__, SKWBT_CONFIG_FILE, strerror(errno));
        strcpy(skwbt_device_node[0], "/dev/ttyS0");
        return ;
    }
 
    char *split;
    char line[1024];
    int port_index = BT_COM_PORT_CMDEVT;
 
    while (fgets(line, sizeof(line), fp))
    {
        char *line_ptr = trim(line);
 
        // Skip blank and comment lines.
        if (*line_ptr == '\0' || *line_ptr == '#' || *line_ptr == '[')
        {
            continue;
        }
 
        split = strchr(line_ptr, '=');
        if (!split)
        {
            ALOGE("%s no key/value separator found", __func__);
            fclose(fp);
            return;
        }
 
        *split = '\0';
        split ++;
        if((!strcmp(trim(line_ptr), "BtDeviceNode")) && (port_index < BT_COM_PORT_SIZE))
        {
            int mode = O_RDWR;
            char *str_ptr = trim(split);
            int len = strlen(str_ptr);
#if 0
            if(strstr(split, "BTDATA"))
            {
                port_index = BT_COM_PORT_ACL;
            }
            else if(strstr(split, "BTAUDIO"))
            {
                port_index = BT_COM_PORT_AUDIO;
            }
            else if(strstr(split, "BTISOC"))
            {
                port_index = BT_COM_PORT_ISO;
            }
#endif
            if(len > DEVICE_NODE_MAX_LEN)
            {
                len = DEVICE_NODE_MAX_LEN;
                str_ptr[DEVICE_NODE_MAX_LEN] = 0x00;
            }
            memcpy(skwbt_device_node[port_index], str_ptr, len);
            scomm_vendor_set_port_name(port_index, skwbt_device_node[port_index], mode);
 
            port_index ++;
        }
    }
 
 
    fclose(fp);
 
    skwbt_transtype = SKWBT_TRANS_TYPE_H4;
    if(skwbt_device_node[0][0] == '?')
    {
        int i = 0;
        while(skwbt_device_node[0][i] != '\0')
        {
            skwbt_device_node[0][i] = skwbt_device_node[0][i + 1];
            i++;
        }
        scomm_vendor_set_port_name(0, skwbt_device_node[0], O_RDWR);
        skwbt_transtype |= SKWBT_TRANS_TYPE_UART;
    }
}
 
char skwbt_boot_open()
{
    btboot_fp = open("/dev/BTBOOT", O_RDWR);
    if (btboot_fp < 0)
    {
        ALOGE("%s: unable to open : %s", __func__, strerror(errno));
        return FALSE;
    }
    return TRUE;
}
 
 
static void load_skwbt_stack_conf()
{
    FILE *fp = fopen(SKWBT_CONFIG_FILE, "rt");
    if (!fp)
    {
        ALOGE("%s unable to open file '%s': %s", __func__, SKWBT_CONFIG_FILE, strerror(errno));
        return;
    }
    char *split;
    int line_num = 0;
    char line[1024];
    while (fgets(line, sizeof(line), fp))
    {
        char *line_ptr = trim(line);
        ++line_num;
 
        // Skip blank and comment lines.
        if (*line_ptr == '\0' || *line_ptr == '#' || *line_ptr == '[')
        {
            continue;
        }
 
        split = strchr(line_ptr, '=');
        if (!split)
        {
            ALOGE("%s no key/value separator found on line %d.", __func__, line_num);
            continue;
        }
 
        *split = '\0';
 
        if(!strcmp(trim(line_ptr), "SkwBtsnoopDump"))
        {
            if(!strcmp(trim(split + 1), "true"))
            {
                btsnoop_log_en = TRUE;
            }
            else
            {
                btsnoop_log_en = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "BtSnoopFileName"))
        {
            sprintf(skw_btsnoop_path, "%s", trim(split + 1));
        }
        else if(!strcmp(trim(line_ptr), "BtSnoopSaveLog"))
        {
            if(!strcmp(trim(split + 1), "true"))
            {
                btsnoop_save_log = TRUE;
            }
            else
            {
                btsnoop_save_log = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "SkwBtcplog"))
        {
            if(!strcmp(trim(split + 1), "true"))
            {
                btcp_log_en = TRUE;
            }
            else
            {
                btcp_log_en = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "SkwLogSlice"))
        {
            if(!strcmp(trim(split + 1), "true"))
            {
                skwlog_slice = TRUE;
            }
            else
            {
                skwlog_slice = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "SkwBtDrvlog"))
        {
            if(!strcmp(trim(split + 1), "false"))
            {
                skwdriverlog_en = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "SkwBtUartOnly"))
        {
            if(!strcmp(trim(split + 1), "false"))//SkwBtUartOnly is false
            {
                skwbtuartonly = FALSE;
            }
        }
        else if(!strcmp(trim(line_ptr), "SkwBtNoSleep"))
        {
            if(strcmp(trim(split + 1), "true") == 0)//SkwBtNoSleep is true
            {
                skwbtNoSleep = TRUE;
            }
        }
        else if(!strcmp(trim(line_ptr), "WakeupADVData"))
        {
            char *data_str = trim(split + 1);
            if(data_str)
            {
                scomm_vendor_parse_wakeup_adv_conf(data_str);
            }
        }
 
 
    }
 
    fclose(fp);
}
 
 
 
 
/*****************************************************************************
**
**   BLUETOOTH VENDOR INTERFACE LIBRARY FUNCTIONS
**
*****************************************************************************/
 
 
 
/** initialise **/
static int init(const bt_vendor_callbacks_t *p_cb, unsigned char *local_bdaddr)
{
    ALOGI("SeekWave BlueTooth Version: %s", SKW_LIBBT_VERSION);
    if (p_cb == NULL)
    {
        ALOGE("init failed with no user callbacks!");
        return -1;
    }
 
    scomm_vendor_init();
 
    load_skwbt_conf();
    load_skwbt_stack_conf();
    skwlog_init();
 
 
    bt_vendor_cbacks = (bt_vendor_callbacks_t *) p_cb;//save callback
    memcpy(vnd_local_bd_addr, local_bdaddr, 6);
 
    if(btsnoop_log_en)
    {
        //enable btsnoop log
        skw_btsnoop_init();
    }
    skw_addr_gen_init();
 
    return 0;
}
 
/** operations **/
static int op(bt_vendor_opcode_t opcode, void *param)
{
    int retval = 0;
 
    ALOGD("op for %d", opcode);
 
    switch(opcode)
    {
        case BT_VND_OP_POWER_CTRL:
        {
            if(skwbt_transtype & SKWBT_TRANS_TYPE_UART)
            {
                int *state = (int *) param;
                if (*state == BT_VND_PWR_OFF)
                {
                    usleep(200000);
                    ALOGD("set power off and delay 200ms");
                }
                else if (*state == BT_VND_PWR_ON)
                {
                    usleep(200000);
                    ALOGD("set power on and delay 200ms");
                }
            }
        }
        break;
 
        case BT_VND_OP_FW_CFG:
        {
            scomm_vendor_config_start();
        }
        break;
 
        case BT_VND_OP_SCO_CFG:
        {
            retval = -1;
        }
        break;
 
        case BT_VND_OP_USERIAL_OPEN:
        {
            int fd = -1, idx = 0;
            int (*fd_array)[] = (int (*)[]) param;
 
 
            if(skwbt_transtype & SKWBT_TRANS_TYPE_UART)
            {
                if(skwbtuartonly == FALSE)
                {
                    SKWBT_LOG("open boot");
                    if(!skwbt_boot_open())
                    {
                        retval = 0;
                        break;
                    }
                    btpw_fp = open("/dev/BTDATA", O_RDWR);
                }
                if(scomm_vendor_uart_open(0) != -1)
                {
                    retval = 1;
                    fd = scomm_vendor_socket_open(0);
                }
            }
            else
            {
                int tFd = -1;
                //uint8_t log_enable = 0;
                if(!skwbt_boot_open())
                {
                    retval = 0;
                    break;
                }
#if 0
                if(ioctl(btboot_fp, IOCTL_CMD_LOG_EN_GET, &log_enable) == 0)
                {
                    ALOGD("SKWBT log_enable res:%d, btcp_log_en:%d, btsnoop_log_en:%d", log_enable, btcp_log_en, btsnoop_log_en);
                    if(btcp_log_en && log_enable)//0:default, 1:release version; 2:debug version
                    {
                        btcp_log_en = log_enable == 2;
                    }
                    if(btsnoop_log_en && log_enable)
                    {
                        btsnoop_log_en = log_enable == 2;
                    }
                }
#endif
 
#ifdef IOCTL_CMD_WAKEUP_ADV_EN
                if(wakeup_ADV_Info.grp_nums > 0)
                {
                    int res = ioctl(btboot_fp, IOCTL_CMD_WAKEUP_ADV_EN);
                    ALOGD("%s, wakeup adv en res:%d", __func__, res);
                }
#endif
                for (idx = 0; idx < BT_COM_PORT_SIZE; idx++)
                {
                    if(scomm_vendor_check_port_valid(idx) == FALSE)
                    {
                        continue;
                    }
                    if(scomm_vendor_usbsdio_open(idx) != -1)
                    {
                        tFd = scomm_vendor_socket_open(idx);
 
                        ALOGD("idx:%d, tFd:%d, fd:%d", idx, tFd, fd);
                        if((tFd != -1) && (fd == -1))
                        {
                            fd = tFd;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
 
                if(idx <= BT_COM_PORT_ACL)
                {
                    skwbt_transtype |= SKWBT_TRANS_TYPE_USB;
                }
                else
                {
                    skwbt_transtype |= SKWBT_TRANS_TYPE_SDIO;
                }
            }
            ALOGD("retval:%d, fd:%d, idx:%d", retval, fd, idx);
 
            if (fd != -1)
            {
                retval = 1;
                for (idx = 0; idx < CH_MAX; idx++)
                {
                    (*fd_array)[idx] = fd;
                }
                skw_ext_inotify_thread_init();
                property_set("SKWBT.OPEN.STATE", "1");
            }
            else
            {
                retval = 0;
            }
        }
        break;
 
        case BT_VND_OP_USERIAL_CLOSE:
        {
            if(btboot_fp > 0)
            {
                scomm_vendor_write_bt_state();
                close(btboot_fp);
                btboot_fp = -1;
            }
            scomm_vendor_close();
            skw_ext_inotify_thread_exit();
            property_set("SKWBT.OPEN.STATE", "0");
        }
        break;
 
        case BT_VND_OP_GET_LPM_IDLE_TIMEOUT:
        {
 
        }
        break;
 
        case BT_VND_OP_LPM_SET_MODE:
        {
 
        }
        break;
 
        case BT_VND_OP_LPM_WAKE_SET_STATE:
        {
 
        }
        break;
        case BT_VND_OP_EPILOG:
        {
            if(bt_vendor_cbacks)
            {
                bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
            }
        }
        break;
 
        default:
            break;
    }
 
    return retval;
}
 
 
/** Close **/
static void cleanup( void )
{
    ALOGD("bt_vendor_skw.c cleanup");
 
    if(btsnoop_log_en)
    {
        skw_btsnoop_close();
    }
    skwlog_close();
 
    btsnoop_log_en = FALSE;
    bt_vendor_cbacks = NULL;
    skwbt_transtype = 0;
}
 
 
 
// Entry point
const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE = {sizeof(bt_vendor_interface_t),
                                                              init,
                                                              op,
                                                              cleanup
                                                             };