-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathapp_factory_bt.cpp
More file actions
323 lines (296 loc) · 9.63 KB
/
Copy pathapp_factory_bt.cpp
File metadata and controls
323 lines (296 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
#include "app_factory_bt.h"
#include "app_battery.h"
#include "app_factory.h"
#include "app_utils.h"
#include "apps.h"
#include "bluetooth.h"
#include "bt_drv_interface.h"
#include "bt_drv_reg_op.h"
#include "cmsis_os.h"
#include "conmgr_api.h"
#include "hal_bootmode.h"
#include "hal_chipid.h"
#include "hal_sleep.h"
#include "hal_trace.h"
#include "intersyshci.h"
#include "me_api.h"
#include "nvrecord.h"
#include "nvrecord_dev.h"
#include "pmu.h"
#include "tgt_hardware.h"
#define APP_FACT_CPU_WAKE_LOCK HAL_CPU_WAKE_LOCK_USER_3
#ifdef __FACTORY_MODE_SUPPORT__
static uint8_t inquiry_buff[] = {0x01, 0x72, 0x77, 0xb0, 0x18, 0x57, 0x60, 0x01,
0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00};
static btif_cmgr_handler_t *app_factorymode_cmgrHandler;
static void bt_error_check_timer_handler(void const *param);
osTimerDef(bt_error_check_timer, bt_error_check_timer_handler);
static osTimerId bt_error_check_timer_id = NULL;
uint8_t test_mode_type = 0;
static void bt_error_check_timer_handler(void const *param) {
// dump rssi
bt_drv_rssi_dump_handler();
// check BT core status
if (bt_drv_error_check_handler()) {
if (test_mode_type == 1) {
hal_sw_bootmode_set(HAL_SW_BOOTMODE_TEST_MODE |
HAL_SW_BOOTMODE_TEST_SIGNALINGMODE);
} else if (test_mode_type == 2) {
hal_sw_bootmode_set(HAL_SW_BOOTMODE_TEST_MODE |
HAL_SW_BOOTMODE_TEST_NOSIGNALINGMODE);
}
hal_cmu_sys_reboot();
}
}
static void app_factorymode_bt_inquiry_buff_update(void) {
bt_bdaddr_t flsh_dongle_addr;
int ret = -1;
ret = nvrec_dev_get_dongleaddr(&flsh_dongle_addr);
if (0 == ret) {
memcpy((void *)&inquiry_buff[1], (void *)flsh_dongle_addr.address,
BTIF_BD_ADDR_SIZE);
DUMP8("0x%02x ", &inquiry_buff[2], BTIF_BD_ADDR_SIZE);
}
}
static void app_factorymode_CmgrCallback(btif_cmgr_handler_t *cHandler,
cmgr_event_t Event,
bt_status_t Status) {
APP_FACTORY_TRACE(4, "%s cHandler:%p Event:%d status:%d", __func__, cHandler,
Event, Status);
if (Event == BTIF_CMEVENT_DATA_LINK_CON_CNF) {
if (Status == BT_STS_SUCCESS) {
APP_FACTORY_TRACE(0, "connect ok");
app_factorymode_result_set(true);
btif_cmgr_remove_data_link(cHandler);
} else {
APP_FACTORY_TRACE(0, "connect failed");
app_factorymode_result_set(false);
}
}
if (Event == BTIF_CMEVENT_DATA_LINK_DIS) {
if (Status == BT_STS_SUCCESS) {
APP_FACTORY_TRACE(0, "disconnect ok");
} else {
APP_FACTORY_TRACE(0, "disconnect failed");
}
}
}
static void app_factorymode_bt_InquiryResult_add(void) {
U8 len = 15;
bool rssi = false, extended = false;
U8 *parm = (U8 *)inquiry_buff;
/* Found one or more devices. Report to clients */
APP_FACTORY_TRACE(4, "%s len:%d rssi:%d extended:%d", __func__, len, rssi,
extended);
DUMP8("0x%02x ", parm, len);
btif_me_inquiry_result_setup(parm, rssi, extended);
}
void app_factorymode_bt_create_connect(void) {
bt_status_t status;
bt_bdaddr_t *bdAddr = (bt_bdaddr_t *)(inquiry_buff + 1);
status = btif_cmgr_create_data_link(app_factorymode_cmgrHandler, bdAddr);
APP_FACTORY_TRACE(2, "%s:%d", __func__, status);
}
void app_factorymode_bt_init_connect(void) {
app_factorymode_cmgrHandler = btif_cmgr_handler_create();
btif_cmgr_register_handler(app_factorymode_cmgrHandler,
app_factorymode_CmgrCallback);
app_factorymode_bt_inquiry_buff_update();
app_factorymode_bt_InquiryResult_add();
}
extern osTimerId app_bt_accessmode_timer;
#if defined(CHIP_BEST1400) || defined(CHIP_BEST1402)
#define XTAL_FCAP_RANGE (0x1FF)
#else
#define XTAL_FCAP_RANGE (0xFF)
#endif
void app_factorymode_bt_xtalrangetest(APP_KEY_STATUS *status, void *param) {
dev_addr_name devinfo;
uint32_t fcap = 0;
APP_FACTORY_TRACE(1, "%s", __func__);
#ifdef __WATCHER_DOG_RESET__
app_wdt_close();
#endif
hal_cpu_wake_lock(APP_FACT_CPU_WAKE_LOCK);
app_stop_10_second_timer(APP_PAIR_TIMER_ID);
app_stop_10_second_timer(APP_POWEROFF_TIMER_ID);
if (app_bt_accessmode_timer) {
osTimerStop(app_bt_accessmode_timer);
}
if (!bt_error_check_timer_id) {
bt_error_check_timer_id =
osTimerCreate(osTimer(bt_error_check_timer), osTimerPeriodic, NULL);
}
if (bt_error_check_timer_id != NULL) {
osTimerStart(bt_error_check_timer_id, 1000);
}
test_mode_type = 1;
app_status_indication_set(APP_STATUS_INDICATION_TESTMODE);
pmu_sleep_en(0);
BESHCI_Close();
btdrv_hciopen();
btdrv_hci_reset();
#ifndef BT_50_FUNCTION
btdrv_sleep_config(0);
osDelay(2000);
btdrv_ins_patch_test_init();
btdrv_feature_default();
#endif
devinfo.btd_addr = bt_addr;
devinfo.ble_addr = ble_addr;
devinfo.localname = BT_LOCAL_NAME;
nvrec_dev_localname_addr_init(&devinfo);
btdrv_write_localinfo((char *)devinfo.localname,
strlen(devinfo.localname) + 1, devinfo.btd_addr);
btdrv_vco_test_start(78);
while (1) {
btdrv_rf_set_xtal_fcap(fcap % XTAL_FCAP_RANGE, 1);
osDelay(300);
TRACE(2, "xtal tune:%d", fcap % XTAL_FCAP_RANGE);
fcap++;
}
}
void app_factorymode_bt_signalingtest(APP_KEY_STATUS *status, void *param) {
dev_addr_name devinfo;
APP_FACTORY_TRACE(1, "%s", __func__);
#ifdef __WATCHER_DOG_RESET__
app_wdt_close();
#endif
hal_cpu_wake_lock(APP_FACT_CPU_WAKE_LOCK);
app_stop_10_second_timer(APP_PAIR_TIMER_ID);
app_stop_10_second_timer(APP_POWEROFF_TIMER_ID);
if (app_bt_accessmode_timer) {
osTimerStop(app_bt_accessmode_timer);
}
if (!bt_error_check_timer_id) {
bt_error_check_timer_id =
osTimerCreate(osTimer(bt_error_check_timer), osTimerPeriodic, NULL);
}
if (bt_error_check_timer_id != NULL) {
osTimerStart(bt_error_check_timer_id, 1000);
}
test_mode_type = 1;
app_status_indication_set(APP_STATUS_INDICATION_TESTMODE);
pmu_sleep_en(0);
BESHCI_Close();
btdrv_hciopen();
btdrv_ins_patch_test_init();
btdrv_hci_reset();
#ifndef BT_50_FUNCTION
btdrv_sleep_config(0);
osDelay(2000);
btdrv_testmode_start();
btdrv_feature_default();
#endif
devinfo.btd_addr = bt_addr;
devinfo.ble_addr = ble_addr;
devinfo.localname = BT_LOCAL_NAME;
devinfo.ble_name = BT_LOCAL_NAME;
nvrec_dev_localname_addr_init(&devinfo);
#ifdef __IBRT_IBRT_TESTMODE__
uint8_t ibrt_address[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
memcpy(bt_addr, ibrt_address, 6);
memcpy(devinfo.btd_addr, ibrt_address, 6);
#endif
btdrv_write_localinfo((char *)devinfo.localname,
strlen(devinfo.localname) + 1, devinfo.btd_addr);
bt_drv_extra_config_after_init();
btdrv_enable_dut();
#ifdef __IBRT_IBRT_TESTMODE__
btdrv_enable_ibrt_test();
#endif
}
int app_battery_stop(void);
void app_factorymode_bt_nosignalingtest(APP_KEY_STATUS *status, void *param) {
dev_addr_name devinfo;
APP_FACTORY_TRACE(1, "%s", __func__);
#ifdef __WATCHER_DOG_RESET__
app_wdt_close();
#endif
hal_cpu_wake_lock(APP_FACT_CPU_WAKE_LOCK);
app_stop_10_second_timer(APP_PAIR_TIMER_ID);
app_stop_10_second_timer(APP_POWEROFF_TIMER_ID);
app_status_indication_set(APP_STATUS_INDICATION_TESTMODE1);
osTimerStop(app_bt_accessmode_timer);
if (!bt_error_check_timer_id) {
bt_error_check_timer_id =
osTimerCreate(osTimer(bt_error_check_timer), osTimerPeriodic, NULL);
}
if (bt_error_check_timer_id != NULL) {
osTimerStart(bt_error_check_timer_id, 1000);
}
test_mode_type = 2;
app_battery_stop();
pmu_sleep_en(0);
BESHCI_Close();
btdrv_hciopen();
btdrv_ins_patch_test_init();
bt_drv_reg_op_key_gen_after_reset(false);
btdrv_hci_reset();
#ifndef BT_50_FUNCTION
btdrv_sleep_config(0);
#endif
osDelay(2000);
btdrv_testmode_start();
#ifndef BT_50_FUNCTION
btdrv_feature_default();
devinfo.btd_addr = bt_addr;
devinfo.ble_addr = ble_addr;
devinfo.localname = BT_LOCAL_NAME;
devinfo.ble_name = BT_LOCAL_NAME;
nvrec_dev_localname_addr_init(&devinfo);
btdrv_write_localinfo((char *)devinfo.localname,
strlen(devinfo.localname) + 1, devinfo.btd_addr);
#endif
bt_drv_extra_config_after_init();
btdrv_hcioff();
#ifdef __BT_DEBUG_TPORTS__
{
extern void bt_enable_tports(void);
bt_enable_tports();
// hal_iomux_tportopen();
}
#endif
btdrv_uart_bridge_loop();
}
int app_factorymode_bt_xtalcalib_proc(void) {
uint32_t capval = 0x80;
int nRet;
APP_FACTORY_TRACE(1, "%s", __func__);
hal_cpu_wake_lock(APP_FACT_CPU_WAKE_LOCK);
APP_FACTORY_TRACE(1, "calib default, capval:%d", capval);
btdrv_hciopen();
btdrv_hci_reset();
#ifndef BT_50_FUNCTION
btdrv_ins_patch_test_init();
#endif
btdrv_hcioff();
capval = 0x80;
bt_drv_calib_open();
nRet = bt_drv_calib_result_porc(&capval);
bt_drv_calib_close();
TRACE(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!calib ret:%d, capval:%d", nRet, capval);
if (!nRet)
nvrec_dev_set_xtal_fcap((unsigned int)capval);
return nRet;
}
void app_factorymode_bt_xtalcalib(APP_KEY_STATUS *status, void *param) {
APP_FACTORY_TRACE(1, "%s", __func__);
app_factorymode_bt_xtalcalib_proc();
}
#endif