bt_manager_connect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2019 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief bt manager connect.
  9. */
  10. #define SYS_LOG_DOMAIN "bt manager"
  11. #include <os_common_api.h>
  12. #include <zephyr.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stream.h>
  17. #include <sys_event.h>
  18. #include <acts_bluetooth/host_interface.h>
  19. #include <mem_manager.h>
  20. #include <bt_manager.h>
  21. #include "bt_manager_inner.h"
  22. #include <property_manager.h>
  23. #include "btservice_api.h"
  24. #include "bt_porting_inner.h"
  25. #define BT_MAX_AUTOCONN_DEV 3
  26. #define BT_TRY_AUTOCONN_DEV 2
  27. #define BT_BASE_DEFAULT_RECONNECT_TRY 3
  28. #define BT_BASE_STARTUP_RECONNECT_TRY 3
  29. #define BT_BASE_TIMEOUT_RECONNECT_TRY 15
  30. #define BT_TWS_SLAVE_DISCONNECT_RETRY 10
  31. #define BT_BASE_RECONNECT_INTERVAL 6000
  32. #define BT_PROFILE_RECONNECT_TRY 3
  33. #define BT_PROFILE_RECONNECT_INTERVAL 3000
  34. void bt_manager_startup_reconnect(void)
  35. {
  36. uint8_t connect_phone_num, connect_phone_cnt;
  37. int cnt, i;
  38. struct autoconn_info *info;
  39. struct bt_set_autoconn reconnect_param;
  40. info = bt_mem_malloc(sizeof(struct autoconn_info)*BT_MAX_AUTOCONN_DEV);
  41. if (info == NULL) {
  42. SYS_LOG_ERR("malloc failed");
  43. return;
  44. }
  45. cnt = btif_br_get_auto_reconnect_info(info, BT_MAX_AUTOCONN_DEV);
  46. if (cnt == 0) {
  47. goto reconnnect_ext;
  48. }
  49. connect_phone_cnt = 0;
  50. connect_phone_num = bt_manager_config_connect_phone_num();
  51. cnt = (cnt > BT_TRY_AUTOCONN_DEV) ? BT_TRY_AUTOCONN_DEV : cnt;
  52. for (i = 0; i < BT_MAX_AUTOCONN_DEV; i++) {
  53. /* As tws master/slave, connect first */
  54. if (info[i].addr_valid && (info[i].tws_role == BTSRV_TWS_MASTER || info[i].tws_role == BTSRV_TWS_SLAVE) &&
  55. (info[i].a2dp || info[i].avrcp || info[i].hfp || info[i].hid)) {
  56. memset(&reconnect_param, 0, sizeof(reconnect_param));
  57. memcpy(reconnect_param.addr.val, info[i].addr.val, sizeof(bd_address_t));
  58. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  59. reconnect_param.base_try = BT_BASE_STARTUP_RECONNECT_TRY;
  60. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  61. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  62. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  63. btif_br_auto_reconnect(&reconnect_param);
  64. info[i].addr_valid = 0;
  65. cnt--;
  66. if (info[i].tws_role == BTSRV_TWS_SLAVE) {
  67. goto reconnnect_ext;
  68. } else {
  69. break;
  70. }
  71. }
  72. }
  73. for (i = 0; i < BT_MAX_AUTOCONN_DEV; i++) {
  74. /* Connect phone second */
  75. if (cnt == 0 || connect_phone_cnt == connect_phone_num) {
  76. break;
  77. }
  78. if (info[i].addr_valid &&
  79. (info[i].a2dp || info[i].avrcp || info[i].hfp|| info[i].hid) &&
  80. (info[i].tws_role == BTSRV_TWS_NONE) && (info[i].trs_mode == 0)) {
  81. memset(&reconnect_param, 0, sizeof(reconnect_param));
  82. memcpy(reconnect_param.addr.val, info[i].addr.val, sizeof(bd_address_t));
  83. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  84. reconnect_param.base_try = BT_BASE_STARTUP_RECONNECT_TRY;
  85. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  86. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  87. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  88. btif_br_auto_reconnect(&reconnect_param);
  89. info[i].addr_valid = 0;
  90. connect_phone_cnt++;
  91. cnt--;
  92. }
  93. }
  94. for (i = 0; i < BT_MAX_AUTOCONN_DEV; i++) {
  95. /* Transmit connect third */
  96. if (cnt == 0) {
  97. break;
  98. }
  99. if (info[i].addr_valid &&
  100. (info[i].a2dp || info[i].avrcp || info[i].hfp|| info[i].hid) &&
  101. (info[i].tws_role == BTSRV_TWS_NONE) && (info[i].trs_mode == 1)) {
  102. memset(&reconnect_param, 0, sizeof(reconnect_param));
  103. memcpy(reconnect_param.addr.val, info[i].addr.val, sizeof(bd_address_t));
  104. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  105. reconnect_param.base_try = BT_BASE_STARTUP_RECONNECT_TRY;
  106. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  107. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  108. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  109. btif_br_auto_reconnect(&reconnect_param);
  110. info[i].addr_valid = 0;
  111. cnt--;
  112. break; /* Only connect one device */
  113. }
  114. }
  115. reconnnect_ext:
  116. bt_mem_free(info);
  117. }
  118. void bt_manager_startup_reconnect_phone(void)
  119. {
  120. uint8_t connect_phone_num, connect_phone_cnt;
  121. int cnt, i;
  122. struct autoconn_info *info;
  123. struct bt_set_autoconn reconnect_param;
  124. info = bt_mem_malloc(sizeof(struct autoconn_info)*BT_MAX_AUTOCONN_DEV);
  125. if (info == NULL) {
  126. SYS_LOG_ERR("malloc failed");
  127. return;
  128. }
  129. cnt = btif_br_get_auto_reconnect_info(info, BT_MAX_AUTOCONN_DEV);
  130. if (cnt == 0) {
  131. goto reconnnect_ext;
  132. }
  133. connect_phone_cnt = 0;
  134. connect_phone_num = bt_manager_config_connect_phone_num();
  135. cnt = (cnt > BT_TRY_AUTOCONN_DEV) ? BT_TRY_AUTOCONN_DEV : cnt;
  136. for (i = 0; i < BT_TRY_AUTOCONN_DEV; i++) {
  137. if (info[i].addr_valid &&
  138. (info[i].a2dp || info[i].avrcp || info[i].hfp|| info[i].hid)) {
  139. if ((connect_phone_cnt == connect_phone_num) ||
  140. (info[i].tws_role != BTSRV_TWS_NONE) || info[i].trs_mode) {
  141. /* Config connect phone number */
  142. continue;
  143. }
  144. memset(&reconnect_param, 0, sizeof(reconnect_param));
  145. memcpy(reconnect_param.addr.val, info[i].addr.val, sizeof(bd_address_t));
  146. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  147. reconnect_param.base_try = BT_BASE_STARTUP_RECONNECT_TRY;
  148. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  149. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  150. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  151. btif_br_auto_reconnect(&reconnect_param);
  152. connect_phone_cnt++;
  153. cnt--;
  154. if (cnt == 0) {
  155. /* Reconnect tow device,
  156. * or as tws slave, just reconnect master, not reconnect phone
  157. */
  158. break;
  159. }
  160. }
  161. }
  162. reconnnect_ext:
  163. bt_mem_free(info);
  164. }
  165. static bool bt_manager_check_reconnect_enable(void)
  166. {
  167. bool auto_reconnect = true;
  168. char temp[16];
  169. memset(temp, 0, sizeof(temp));
  170. #ifdef CONFIG_PROPERTY
  171. if (property_get(CFG_AUTO_RECONNECT, temp, 16) > 0) {
  172. if (strcmp(temp, "false") == 0) {
  173. auto_reconnect = false;
  174. }
  175. }
  176. #endif
  177. return auto_reconnect;
  178. }
  179. void bt_manager_disconnected_reason(void *param)
  180. {
  181. struct bt_disconnect_reason *p_param = (struct bt_disconnect_reason *)param;
  182. struct bt_set_autoconn reconnect_param;
  183. SYS_LOG_INF("BMC tws_role %d", p_param->tws_role);
  184. if (!bt_manager_check_reconnect_enable()) {
  185. SYS_LOG_WRN("BMC disable do reconnect\n");
  186. return;
  187. }
  188. if (p_param->tws_role == BTSRV_TWS_MASTER) {
  189. /* Just let slave device do reconnect */
  190. return;
  191. }
  192. if (p_param->reason != BT_HCI_ERR_REMOTE_USER_TERM_CONN &&
  193. p_param->reason != BT_HCI_ERR_REMOTE_POWER_OFF &&
  194. p_param->reason != BT_HCI_ERR_LOCALHOST_TERM_CONN &&
  195. p_param->reason != BT_HCI_ERR_AUTH_FAIL){
  196. memset(&reconnect_param, 0, sizeof(reconnect_param));
  197. memcpy(reconnect_param.addr.val, p_param->addr.val, sizeof(bd_address_t));
  198. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  199. if (p_param->tws_role == BTSRV_TWS_SLAVE) {
  200. reconnect_param.base_try = BT_TWS_SLAVE_DISCONNECT_RETRY;
  201. } else {
  202. reconnect_param.base_try = BT_BASE_DEFAULT_RECONNECT_TRY;
  203. }
  204. if (p_param->tws_role == BTSRV_TWS_NONE &&
  205. ((p_param->reason == BT_HCI_ERR_CONN_TIMEOUT)
  206. || (p_param->reason == BT_HCI_ERR_LL_RESP_TIMEOUT))) {
  207. reconnect_param.base_try = BT_BASE_TIMEOUT_RECONNECT_TRY;
  208. }
  209. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  210. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  211. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  212. btif_br_auto_reconnect(&reconnect_param);
  213. }
  214. }
  215. static bool halted_phone = false;
  216. void bt_manager_halt_phone(void)
  217. {
  218. #ifdef CONFIG_TWS
  219. /**only master need halt phone*/
  220. if (bt_manager_tws_get_dev_role() != BTSRV_TWS_MASTER) {
  221. return;
  222. }
  223. #endif
  224. bt_manager_set_visible(false);
  225. bt_manager_set_connectable(false);
  226. btif_br_auto_reconnect_stop();
  227. btif_br_disconnect_device(BTSRV_DISCONNECT_PHONE_MODE);
  228. halted_phone = true;
  229. SYS_LOG_INF("BMC halt phone");
  230. }
  231. void bt_manager_resume_phone(void)
  232. {
  233. if(halted_phone) {
  234. //bt_manager_startup_reconnect_phone();
  235. //bt_manager_set_visible(true);
  236. //bt_manager_set_connectable(true);
  237. halted_phone = false;
  238. //SYS_LOG_INF("BMC resume phone");
  239. }
  240. }
  241. void bt_manager_set_visible(bool visible)
  242. {
  243. btif_br_set_discoverable(visible);
  244. }
  245. void bt_manager_set_connectable(bool connectable)
  246. {
  247. btif_br_set_connnectable(connectable);
  248. }
  249. bool bt_manager_is_auto_reconnect_runing(void)
  250. {
  251. return btif_br_is_auto_reconnect_runing();
  252. }
  253. int bt_manager_stop_auto_reconnect(void)
  254. {
  255. return btif_br_auto_reconnect_stop();
  256. }
  257. int bt_manager_get_addr_linkkey(bd_address_t *addr, uint8_t *linkkey)
  258. {
  259. int ret;
  260. uint8_t key[16];
  261. ret = btif_br_get_addr_linkkey(addr, key);
  262. if (ret == 0 && linkkey) {
  263. memcpy(linkkey, key, 16);
  264. }
  265. return ret;
  266. }
  267. int bt_manager_get_linkkey(struct bt_linkkey_info *info, uint8_t cnt)
  268. {
  269. return btif_br_get_linkkey(info, cnt);
  270. }
  271. int bt_manager_update_linkkey(struct bt_linkkey_info *info, uint8_t cnt)
  272. {
  273. return btif_br_update_linkkey(info, cnt);
  274. }
  275. int bt_manager_write_ori_linkkey(bd_address_t *addr, uint8_t *link_key)
  276. {
  277. return btif_br_write_ori_linkkey(addr, link_key);
  278. }
  279. int bt_manager_get_connected_dev_rssi(bd_address_t *addr,btsrv_get_rssi_result_cb cb)
  280. {
  281. struct bt_get_rssi_param param;
  282. memset(&param,0,sizeof(struct bt_get_rssi_param));
  283. memcpy(&param.addr,addr,sizeof(bd_address_t));
  284. param.cb = cb;
  285. return btif_br_get_connected_device_rssi(&param);
  286. }
  287. int bt_manager_get_actived_dev_rssi(int8_t *rssi)
  288. {
  289. int ret;
  290. ret = btif_br_get_actived_device_rssi(rssi);
  291. SYS_LOG_INF("BMC RSSI %d",*rssi);
  292. return ret;
  293. }
  294. void bt_manager_clear_linkkey(bd_address_t *addr)
  295. {
  296. btif_br_clean_linkkey(addr);
  297. }
  298. /* Ble encrypted, phone discover ble use 2s, device discover ancs/ams use 2s. */
  299. #define BR_RESOLVE_CONNECT_DELAY_TIME (4000)
  300. static uint8_t br_resolve_pending;
  301. static bd_address_t br_resolve_addr;
  302. static void br_resolve_work_handler(struct k_work *work)
  303. {
  304. struct bt_set_autoconn reconnect_param;
  305. SYS_LOG_INF("BMC do resolve br connect");
  306. memset(&reconnect_param, 0, sizeof(reconnect_param));
  307. memcpy(&reconnect_param.addr, &br_resolve_addr, sizeof(bd_address_t));
  308. reconnect_param.strategy = BTSRV_AUTOCONN_ALTERNATE;
  309. reconnect_param.base_try = BT_BASE_DEFAULT_RECONNECT_TRY;
  310. reconnect_param.profile_try = BT_PROFILE_RECONNECT_TRY;
  311. reconnect_param.base_interval = BT_BASE_RECONNECT_INTERVAL;
  312. reconnect_param.profile_interval = BT_PROFILE_RECONNECT_INTERVAL;
  313. reconnect_param.force_connect = 1;
  314. reconnect_param.connect_hfp = 1;
  315. //reconnect_param.connect_a2dp = 1;
  316. //reconnect_param.connect_avrcp = 1;
  317. //reconnect_param.connect_hfp_first = 1;
  318. btif_br_auto_reconnect(&reconnect_param);
  319. bt_wake_unlock();
  320. br_resolve_pending = 0;
  321. }
  322. OS_DELAY_WORK_DEFINE(br_resolve_work, br_resolve_work_handler);
  323. void bt_manager_br_resolve_connect(void *param)
  324. {
  325. struct bt_br_resolve_param *resolve_param = param;
  326. if (resolve_param->br_connected) {
  327. SYS_LOG_INF("BMC br resolve already connected!");
  328. } else {
  329. if (!br_resolve_pending) {
  330. br_resolve_pending = 1;
  331. memcpy(&br_resolve_addr, &resolve_param->addr, sizeof(bd_address_t));
  332. os_delayed_work_submit(&br_resolve_work, BR_RESOLVE_CONNECT_DELAY_TIME);
  333. bt_wake_lock();
  334. } else {
  335. SYS_LOG_INF("BMC already pending resolve connect");
  336. }
  337. }
  338. }