tws.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "include.h"
  2. #include "api.h"
  3. #if BT_TWS_EN
  4. extern const char *bt_get_local_name(void);
  5. #define BT_TWS_FEATS ((TWS_FEAT_MS_SWITCH*BT_TWS_MS_SWITCH_EN) | \
  6. ((TWS_FEAT_TSCO|TWS_FEAT_TSCO_RING)*BT_TWS_SCO_EN) | \
  7. (TWS_FEAT_CODEC_AAC*BT_A2DP_AAC_AUDIO_EN) | \
  8. (TWS_FEAT_CODEC_MSBC*BT_HFP_MSBC_EN) | \
  9. (TWS_FEAT_CODEC_HIRES*(BT_A2DP_LHDC_AUDIO_EN || BT_A2DP_LDAC_AUDIO_EN)) )
  10. //bit[7:6]: pair mode
  11. //0: 使用蓝牙名称配对
  12. //1: 使用蓝牙ID配对
  13. //bit[5]: MS bonding
  14. //0: 主从不组队绑定
  15. //1: 主从组队绑定
  16. //bit[3:0]: pair operation
  17. //0: 使用内置自动配对,上电先回连TWS,再回连手机
  18. //1: 使用api操作,不自动配对,上电不回连
  19. //2: 使用按键调用内置配对,上电先回连TWS,再回连手机
  20. #define TWS_PAIR_MODE ((BT_TWS_PAIR_MODE<<6))
  21. uint8_t cfg_bt_tws_pair_mode = TWS_PAIR_MODE;
  22. uint16_t cfg_bt_tws_feat = BT_TWS_FEATS;
  23. uint8_t cfg_bt_nor_connect_times = 5; //设置上电回连手机次数, 1.28s * n, 不小于5次
  24. uint8_t cfg_bt_tws_search_slave_times = 3; //设置TWS搜索次数, 1.25s * n, 不小于2次
  25. uint8_t cfg_bt_tws_connect_times = 4; //设置上电回连TWS次数, 1.28s * n, 不小于4次
  26. uint16_t cfg_bt_tws_sup_to_connect_times = 100; //设置TWS断线回连次数, (1.28s + 1) * n, 设置(-1)为一直回连
  27. uint8_t cfg_bt_tws_limit_pair_time = 0; //设置TWS上电、出仓配对限制时间, 单位80ms, 0为不限制(L、R规定时间内上电、出仓才能配对)
  28. uint8_t cfg_bt_tws_tick_freq = 1;
  29. //pair_mode选择ID配对时,用该函数获取ID
  30. uint32_t bt_tws_get_pair_id(void)
  31. {
  32. return BT_TWS_PAIR_ID;
  33. }
  34. bool bt_tws_name_suffix_replace(char *buf)
  35. {
  36. if (sys_cb.name_suffix_en) {
  37. if(buf[0] == 'L') {
  38. buf[0] = 'R';
  39. return true;
  40. } else if(buf[0] == 'R') {
  41. buf[0] = 'L';
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. void bt_tws_set_discon(uint8_t reason)
  48. {
  49. if(reason != 0 && reason != 0xff) {
  50. sys_cb.pwrdwn_tone_en = 1;
  51. sys_cb.discon_reason = 1; //同步关机
  52. func_cb.sta = FUNC_PWROFF;
  53. }
  54. }
  55. //tws搜索配对时,获取channel避免左左(或右右)配对
  56. bool bt_tws_get_channel_cfg(uint8_t *channel)
  57. {
  58. if(xcfg_cb.bt_tws_lr_mode == 9) {
  59. *channel = sys_cb.tws_left_channel; //GPIOx有接地为左声道
  60. return true;
  61. } else if(10 == xcfg_cb.bt_tws_lr_mode) { //配置选择为左声道
  62. *channel = 1;
  63. return true;
  64. } else if(11 == xcfg_cb.bt_tws_lr_mode) { //配置选择为右声道
  65. *channel = 0;
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool bt_tws_get_channel(uint8_t *channel)
  71. {
  72. if (bt_tws_get_channel_cfg(channel)) {
  73. return true;
  74. } else if (sys_cb.vusb_force_channel) {
  75. *channel = sys_cb.vusb_force_channel - 1;
  76. return true;
  77. }
  78. return false;
  79. }
  80. #if (CHARGE_BOX_TYPE == CBOX_SSW) || QTEST_EN
  81. void bt_tws_channel_read(void)
  82. {
  83. u8 channel;
  84. if (!bt_tws_get_channel_cfg(&channel)) {
  85. param_tws_channel_read();
  86. }
  87. }
  88. u8 bt_tws_get_tws_channel(void)
  89. {
  90. uint8_t channel;
  91. if (!bt_tws_get_channel_cfg(&channel)) {
  92. return NO_DISTRIBUTION;
  93. }
  94. if (channel) {
  95. return LEFT_CHANNEL;
  96. }
  97. return RIGHT_CHANNEL;
  98. }
  99. u8 bt_tws_get_tws_role(void)
  100. {
  101. u8 ms_role = 0;
  102. if (sys_cb.tws_force_channel == 1) {//1 固定为左声道
  103. if(xcfg_cb.bt_tws_lr_mode == 2) {
  104. ms_role = 1;
  105. }
  106. } else if (sys_cb.tws_force_channel == 2) {
  107. if(xcfg_cb.bt_tws_lr_mode != 2) {
  108. ms_role = 1;
  109. }
  110. }
  111. return ms_role;
  112. }
  113. bool bt_tws_channel_check(u8 channel)
  114. {
  115. u8 tws_channel = bt_tws_get_tws_channel();
  116. if (tws_channel != NO_DISTRIBUTION) {
  117. if (tws_channel == LEFT_CHANNEL) {
  118. tws_channel = LEFT_CHANNEL_USER;
  119. } else {
  120. tws_channel = RIGHT_CHANNEL_USER;
  121. }
  122. #if (CHARGE_BOX_TYPE == CBOX_SSW)
  123. if ((channel != tws_channel)&&(!qtest_get_mode())) {
  124. return false;
  125. }
  126. #endif
  127. } else if ((channel == LEFT_CHANNEL_USER) || (channel == RIGHT_CHANNEL_USER)) {
  128. if (channel == RIGHT_CHANNEL_USER) {
  129. sys_cb.tws_force_channel = 1;
  130. } else {
  131. sys_cb.tws_force_channel = 2;
  132. }
  133. param_tws_channel_write(sys_cb.tws_force_channel); //自动分配的方案通过电池仓自动固定左右声道
  134. }else if ((channel == NOTFIX_LCHANEL_USER) || (channel == NOTFIX_RCHANEL_USER)) {
  135. if (channel == NOTFIX_RCHANEL_USER) {
  136. sys_cb.tws_force_channel = 1;
  137. } else {
  138. sys_cb.tws_force_channel = 2;
  139. }
  140. }
  141. return true;
  142. }
  143. #endif
  144. #if USER_INEAR_DETECT_EN
  145. void bt_update_ear_sta(void)
  146. {
  147. bt_tws_sync_setting(); //通知对方
  148. }
  149. //api, 设置耳机状态
  150. void bt_set_ear_sta(bool ear_sta)
  151. {
  152. sys_cb.loc_ear_sta = ear_sta; //0=戴入,1=取下
  153. bt_update_ear_sta();
  154. }
  155. #endif // USER_INEAR_DETECT_EN
  156. //tws一些同步信息,例如EQ, 语言等,param最大是12byte
  157. bool bt_tws_get_info(uint8_t *param)
  158. {
  159. u8 offset = 0;
  160. #if LANG_SELECT == LANG_EN_ZH
  161. param[offset] = sys_cb.lang_id & 0x0f; //bit0~3
  162. #endif
  163. #if EQ_MODE_EN
  164. param[offset] |= (u8)sys_cb.eq_mode<<4;
  165. #endif
  166. offset++;
  167. param[offset++] = sys_cb.hfp_vol; //同步初始通话音量
  168. #if USER_INEAR_DETECT_EN
  169. param[offset++] |= sys_cb.loc_ear_sta; //同步入耳状态
  170. #endif // USER_INEAR_DETECT_EN
  171. #if ABP_EN
  172. param[offset++] = sys_cb.abp_mode;
  173. #endif // ABP_EN
  174. #if ASR_EN
  175. param[offset++] = sys_cb.asr_enable;
  176. #endif
  177. #if ANC_ALG_DYVOL_FF_EN
  178. param[offset++] = sys_cb.dyvol_local;
  179. param[offset++] = sys_cb.dyvol_real;
  180. param[offset++] = sys_cb.dyvol_overlap;
  181. #endif // ANC_ALG_DYVOL_FF_EN
  182. return true;
  183. }
  184. void bt_tws_set_info(uint8_t *param)
  185. {
  186. uint8_t flag = 0,tmp = 0, offset = 0;
  187. #if LANG_SELECT == LANG_EN_ZH
  188. tmp = (param[offset] & 0x0f);
  189. if(xcfg_cb.lang_id >= 2 && tmp < 2 && tmp != sys_cb.lang_id) { //bit0~3
  190. sys_cb.lang_id = tmp;
  191. msg_enqueue(EVT_BT_SET_LANG_ID);
  192. }
  193. #endif
  194. #if EQ_MODE_EN
  195. tmp = (param[offset] & 0xf0)>>4;
  196. if(tmp < 6 && tmp != sys_cb.eq_mode) { //bit4~7
  197. sys_cb.eq_mode = tmp;
  198. msg_enqueue(EVT_BT_SET_EQ);
  199. }
  200. #endif
  201. offset++;
  202. sys_cb.hfp_vol = param[offset++]; //同步初始通话音量
  203. #if USER_INEAR_DETECT_EN
  204. tmp = param[offset++];
  205. if(tmp != sys_cb.rem_ear_sta) {
  206. sys_cb.rem_ear_sta = tmp; //同步入耳状态
  207. }
  208. #endif // USER_INEAR_DETECT_EN
  209. #if ABP_EN
  210. tmp = param[offset++];
  211. if (tmp != sys_cb.abp_mode) {
  212. sys_cb.abp_mode = tmp;
  213. bt_tws_req_alarm_abp(sys_cb.abp_mode);
  214. }
  215. #endif // ABP_EN
  216. #if ASR_EN
  217. sys_cb.asr_enable = param[offset++];
  218. #endif
  219. #if ANC_ALG_DYVOL_FF_EN
  220. sys_cb.dyvol_remote = param[offset++];
  221. if (!bt_tws_is_slave()) {
  222. bsp_anc_alg_dyvol_set_vol();
  223. } else {
  224. sys_cb.dyvol_real = param[offset++];
  225. sys_cb.dyvol_overlap = param[offset++];
  226. #if ANC_ALG_DYVOL_FF_LOCAL_VOL_EN
  227. bsp_anc_alg_dyvol_set_local_gain();
  228. #endif // ANC_ALG_DYVOL_FF_LOCAL_VOL_EN
  229. }
  230. #endif // ANC_ALG_DYVOL_FF_EN
  231. tmp = flag; //避免编译警告
  232. }
  233. //tws同步用户自定义的一些数据,通过 bt_tws_sync_custom_data() 进行回调
  234. uint16_t tws_get_custom_data(uint8_t *buf)
  235. {
  236. uint16_t len = 0;
  237. #if GFPS_EN
  238. if(len == 0){
  239. len = gfps_tws_get_data(buf);
  240. }
  241. #endif
  242. if(len){
  243. bt_tws_sync_custom_data();
  244. }
  245. return len;
  246. }
  247. uint16_t tws_set_custom_data(uint8_t *data_ptr, uint16_t size)
  248. {
  249. //tag:data_ptr[0]
  250. #if GFPS_EN
  251. if(data_ptr[0] == TWS_SYNC_TAG_GFP){
  252. gfps_tws_set_data(&data_ptr[1], size - 1);
  253. }
  254. #endif
  255. return 0;
  256. }
  257. void bt_tws_role_change(u32 param)
  258. {
  259. // u8 new_role = param;
  260. // u8 adv_en = (param>>8);
  261. // if(new_role == 0 && adv_en) {
  262. // } else {
  263. // }
  264. }
  265. void tws_user_key_process(uint32_t *opcode)
  266. {
  267. //*opcode
  268. app_tws_user_key_process(opcode);
  269. }
  270. #if BT_TWS_PUBLIC_ADDR_EN
  271. void bt_tws_get_public_addr(uint8_t *remote_addr, uint8_t *rand_key)
  272. {
  273. uint8_t local_addr[6];
  274. #if LE_DUEROS_DMA_EN
  275. uint8_t channel = 0; //1:左 、 0:右
  276. bt_tws_get_channel(&channel);
  277. if (channel == 1) {
  278. memcpy(remote_addr, xcfg_cb.bt_addr, 6);
  279. }
  280. return;
  281. #endif
  282. #if IODM_TEST_EN
  283. if(!bt_get_qr_addr(local_addr))
  284. #endif
  285. {
  286. memcpy(local_addr, xcfg_cb.bt_addr, 6);
  287. }
  288. u8 header_flag = 1;
  289. for (int i = 0; i < 6; i++) {
  290. remote_addr[i] ^= local_addr[i];
  291. if (remote_addr[i] == 0 && header_flag) {
  292. remote_addr[i] = local_addr[i];
  293. } else {
  294. header_flag = 0;
  295. remote_addr[i] ^= rand_key[i];
  296. // printf(" 0x%02x, //%d\n", rand_key[i], i);
  297. }
  298. }
  299. }
  300. #endif
  301. #if BT_TWS_DBG_EN
  302. u8 tws_dbg_ind[24] = {
  303. 'T', 'W', 'S', //SPP-TAG
  304. 0x14, //[0] len
  305. 0xff, //[1] data_type
  306. 0x42, 0x06, //[2:3] comany_id
  307. 0x00, 0x00, 0x00, //[4:6] reserved
  308. 0x00, //[7] headset_type
  309. 0xff, //[8] rssi_left_tws
  310. 0xfe, //[9] rssi_right_tws
  311. 0x01, //[10] rssi_left_phone
  312. 0x02, //[11] rssi_right_phone
  313. 0xff, //[12] per_left_tws
  314. 0xff, //[13] per_right_tws
  315. 0xff, //[14] per_left_phone
  316. 0xff, //[15] per_right_phone
  317. 0x00, //[16] battery_left
  318. 0x00, //[17] battery_right
  319. 0x00, //[18] battery_house
  320. 0x00, //[19] unknow
  321. 0x00, //[20] color
  322. };
  323. void bt_tws_report_dgb(void)
  324. {
  325. u8 *ind = tws_dbg_ind + 3;
  326. s8 *rssi = (s8 *)(ind + 8);
  327. ind[19] = sys_cb.tws_left_channel; //0=right, 1=left
  328. if(bt_tws_get_link_rssi(rssi, sys_cb.tws_left_channel)) {
  329. bt_spp_tx(SPP_SERVICE_CH0, tws_dbg_ind, 24);
  330. // ble_set_adv_data(tws_dbg_ind+3, 21);
  331. // ble_adv_en();
  332. }
  333. }
  334. #endif // BT_TWS_DBG_EN
  335. #endif // BT_TWS_EN