bt_manager_tws.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 a2dp profile.
  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 <acts_ringbuf.h>
  18. #include <bt_manager.h>
  19. #include <sys_event.h>
  20. #include <app_manager.h>
  21. #include <power_manager.h>
  22. #include "bt_manager_inner.h"
  23. #include "audio_policy.h"
  24. #include "app_switch.h"
  25. #include "btservice_api.h"
  26. #include "media_player.h"
  27. #include <volume_manager.h>
  28. #include <audio_system.h>
  29. #include <input_manager.h>
  30. #include "bt_porting_inner.h"
  31. #ifdef CONFIG_PROPERTY
  32. #include "property_manager.h"
  33. #endif
  34. #define TWS_PAIR_TYR_TIMES 3
  35. #define TWS_TIMETO_BT_CLOCK(a) ((a) * 10000 / 3125)
  36. static tws_runtime_observer_t *tws_observer;
  37. struct tws_sync_event_t {
  38. sys_snode_t node;
  39. uint32_t event;
  40. uint32_t bt_clock;
  41. };
  42. struct bt_manager_tws_context_t {
  43. sys_slist_t tws_sync_event_list;
  44. os_work tws_irq_work;
  45. uint8_t irq_req_flag:1;
  46. uint8_t slave_actived:1;
  47. uint8_t record_low_latency:1;
  48. uint8_t peer_version;
  49. uint32_t peer_feature;
  50. uint8_t source_stream_type;
  51. uint8_t sink_tws_mode;
  52. uint8_t sink_drc_mode;
  53. uint8_t sink_volume;
  54. };
  55. struct bt_manager_tws_context_t tws_context;
  56. static OS_MUTEX_DEFINE(bt_manager_tws_mutex);
  57. static void bt_manager_tws_process_command(uint8_t *data, uint8_t len);
  58. #ifdef CONFIG_BT_TWS_US281B
  59. static void bt_manager_tws_process_us281b_command(uint8_t *data, uint8_t len);
  60. #endif
  61. static void _bt_manager_irq_work(os_work *work)
  62. {
  63. struct tws_sync_event_t *temp, *event_item;
  64. os_mutex_lock(&bt_manager_tws_mutex, OS_FOREVER);
  65. if (!sys_slist_is_empty(&tws_context.tws_sync_event_list)) {
  66. SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&tws_context.tws_sync_event_list, event_item, temp, node) {
  67. if (event_item && event_item->bt_clock <= btif_tws_get_bt_clock()) {
  68. struct app_msg msg = {0};
  69. msg.type = MSG_SYS_EVENT;
  70. msg.cmd = event_item->event;
  71. send_async_msg("main", &msg);
  72. sys_slist_find_and_remove(&tws_context.tws_sync_event_list, &event_item->node);
  73. SYS_LOG_INF("event %d bt_clock %d\n", event_item->event, event_item->bt_clock);
  74. bt_mem_free(event_item);
  75. }
  76. }
  77. }
  78. os_mutex_unlock(&bt_manager_tws_mutex);
  79. }
  80. static void _bt_manager_tws_add_event(uint32_t event_id, uint32_t bt_clock)
  81. {
  82. struct tws_sync_event_t *tws_sync_event = NULL;
  83. struct app_msg msg = {0};
  84. if (!bt_manager_config_enable_tws_sync_event()) {
  85. msg.type = MSG_SYS_EVENT;
  86. msg.cmd = event_id;
  87. send_async_msg("main", &msg);
  88. return;
  89. }
  90. SYS_LOG_INF("event %d bt_clock %d\n", event_id, bt_clock);
  91. os_mutex_lock(&bt_manager_tws_mutex, OS_FOREVER);
  92. tws_sync_event = bt_mem_malloc(sizeof(struct tws_sync_event_t));
  93. if (!tws_sync_event) {
  94. goto exit;
  95. }
  96. tws_sync_event->event = event_id;
  97. tws_sync_event->bt_clock = bt_clock;
  98. sys_slist_append(&tws_context.tws_sync_event_list, (sys_snode_t *)tws_sync_event);
  99. SYS_LOG_INF("event %d bt_clock %d\n", event_id, bt_clock);
  100. exit:
  101. os_mutex_unlock(&bt_manager_tws_mutex);
  102. }
  103. static void bt_manager_tws_stream_type_to_tws_mode(uint8_t stream_type, uint8_t *tws_mode, uint8_t *drc_mode)
  104. {
  105. switch (stream_type) {
  106. case AUDIO_STREAM_LOCAL_MUSIC:
  107. *tws_mode = TWS_MODE_MUSIC_TWS;
  108. *drc_mode = DRC_MODE_OFF;
  109. break;
  110. case AUDIO_STREAM_LINEIN:
  111. *tws_mode = TWS_MODE_AUX_TWS;
  112. *drc_mode = DRC_MODE_AUX;
  113. break;
  114. default:
  115. *tws_mode = TWS_MODE_BT_TWS;
  116. *drc_mode = DRC_MODE_NORMAL;
  117. break;
  118. }
  119. }
  120. static void bt_manager_tws_tws_mode_to_stream_type(uint8_t tws_mode, uint8_t drc_mode, uint8_t *stream_type)
  121. {
  122. switch (tws_mode) {
  123. case TWS_MODE_AUX_TWS:
  124. *stream_type = AUDIO_STREAM_LINEIN;
  125. break;
  126. case TWS_MODE_MUSIC_TWS:
  127. *stream_type = AUDIO_STREAM_LOCAL_MUSIC;
  128. break;
  129. default:
  130. *stream_type = AUDIO_STREAM_MUSIC;
  131. break;
  132. }
  133. }
  134. static void us281b_sink_start_play(uint8_t *data, int len)
  135. {
  136. uint8_t cmd[6], stream_type;
  137. bt_manager_tws_tws_mode_to_stream_type(tws_context.sink_tws_mode, tws_context.sink_drc_mode, &stream_type);
  138. cmd[0] = TWS_STATUS_EVENT;
  139. cmd[1] = BT_TWS_START_PLAY;
  140. cmd[2] = stream_type; /* exf_stream_type */
  141. cmd[3] = data[0];
  142. cmd[4] = data[1];
  143. cmd[5] = tws_context.sink_volume;
  144. bt_manager_tws_process_command(cmd, 6);
  145. }
  146. static void bt_manager_set_sink_volume(void)
  147. {
  148. uint8_t cmd[3];
  149. uint16_t volume_limit = 0;
  150. cmd[0] = TWS_SYNC_M2S_UI_SET_VOL_LIMIT_CMD;
  151. memcpy(&cmd[1], &volume_limit, sizeof(volume_limit));
  152. btif_tws_send_command(cmd, 3);
  153. #ifdef CONFIG_AUDIO
  154. int volume = audio_system_get_stream_volume(tws_context.source_stream_type);
  155. if (volume >= 0) {
  156. cmd[0] = TWS_SYNC_M2S_UI_SET_VOL_VAL_CMD;
  157. cmd[1] = (uint8_t)volume;
  158. btif_tws_send_command(cmd, 2);
  159. }
  160. #endif
  161. }
  162. static void bt_manager_set_sink_spk_pos(uint8_t pos)
  163. {
  164. uint8_t cmd[2];
  165. cmd[0] = TWS_SYNC_M2S_UI_SWITCH_POS_CMD;
  166. cmd[1] = pos;
  167. btif_tws_send_command(cmd, 2);
  168. }
  169. /* return, 0: not pair tws device, other pair tws device */
  170. static int bt_manager_tws_discover_check_device(void *param, int param_size)
  171. {
  172. struct btsrv_discover_result *result = param;
  173. uint8_t pre_mac[3];
  174. if (result->name == NULL) {
  175. return 0;
  176. }
  177. if (bt_manager_config_get_tws_compare_high_mac()) {
  178. bt_manager_config_set_pre_bt_mac(pre_mac);
  179. if (result->addr.val[5] != pre_mac[0] ||
  180. result->addr.val[4] != pre_mac[1] ||
  181. result->addr.val[3] != pre_mac[2]) {
  182. return 0;
  183. }
  184. }
  185. #ifdef CONFIG_PROPERTY
  186. uint8_t name[33];
  187. memset(name, 0, sizeof(name));
  188. property_get(CFG_BT_NAME, name, 32);
  189. if (strlen(name) != result->len || memcmp(result->name, name, result->len)) {
  190. return 0;
  191. }
  192. #endif
  193. if (bt_manager_config_get_tws_compare_device_id() &&
  194. memcmp(result->device_id, bt_manager_config_get_device_id(), sizeof(result->device_id))) {
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. static int _bt_manager_tws_event_notify(int event_type, void *param, int param_size)
  200. {
  201. int ret = 0;
  202. struct update_version_param *in_param;
  203. switch (event_type) {
  204. case BTSRV_TWS_DISCOVER_CHECK_DEVICE:
  205. ret = bt_manager_tws_discover_check_device(param, param_size);
  206. break;
  207. case BTSRV_TWS_DISCONNECTED:
  208. SYS_LOG_INF("tws disconnected");
  209. tws_observer = NULL;
  210. tws_context.peer_version = 0x0;
  211. system_set_low_latencey_mode((tws_context.record_low_latency ? true : false));
  212. bt_manager_set_status(BT_STATUS_TWS_UNPAIRED);
  213. if (!strcmp("tws", app_manager_get_current_app())) {
  214. sys_event_notify(SYS_EVENT_TWS_DISCONNECTED);
  215. app_switch_unlock(1);
  216. app_switch("btmusic", APP_SWITCH_CURR, true);
  217. }
  218. if (system_check_low_latencey_mode()) {
  219. #ifdef CONFIG_BT_BLE
  220. bt_manager_resume_ble();
  221. #endif
  222. }
  223. break;
  224. case BTSRV_TWS_CONNECTED:
  225. SYS_LOG_INF("tws connected");
  226. tws_observer = btif_tws_get_tws_observer();
  227. bt_manager_set_status(BT_STATUS_TWS_PAIRED);
  228. if (btif_tws_get_dev_role() == BTSRV_TWS_SLAVE) {
  229. app_switch("tws", APP_SWITCH_CURR, true);
  230. app_switch_lock(1);
  231. #ifdef CONFIG_POWER
  232. power_manager_sync_slave_battery_state();
  233. #endif
  234. }
  235. if (system_check_low_latencey_mode()) {
  236. #ifdef CONFIG_BT_BLE
  237. bt_manager_halt_ble();
  238. #endif
  239. }
  240. break;
  241. case BTSRV_TWS_RESTART_PLAY:
  242. SYS_LOG_INF("tws restart");
  243. bt_manager_event_notify(BT_REQ_RESTART_PLAY, NULL, 0);
  244. break;
  245. case BTSRV_TWS_START_PLAY:
  246. SYS_LOG_INF("tws start");
  247. break;
  248. case BTSRV_TWS_PLAY_SUSPEND:
  249. SYS_LOG_INF("tws suspend");
  250. break;
  251. case BTSRV_TWS_READY_PLAY:
  252. SYS_LOG_INF("tws ready");
  253. break;
  254. case BTSRV_TWS_EVENT_SYNC:
  255. #ifdef CONFIG_BT_TWS_US281B
  256. bt_manager_tws_process_us281b_command(param, param_size);
  257. #else
  258. bt_manager_tws_process_command(param, param_size);
  259. #endif
  260. break;
  261. case BTSRV_TWS_A2DP_CONNECTED:
  262. SYS_LOG_INF("a2dp connected");
  263. break;
  264. case BTSRV_TWS_HFP_CONNECTED:
  265. SYS_LOG_INF("hfp connected");
  266. break;
  267. case BTSRV_TWS_IRQ_CB:
  268. {
  269. /* Be carefull, call back in tws irq context */
  270. os_work_submit(&tws_context.tws_irq_work);
  271. break;
  272. }
  273. case BTSRV_TWS_UNPROC_PENDING_START_CB:
  274. SYS_LOG_INF("Pending start");
  275. bt_manager_event_notify(BT_REQ_RESTART_PLAY, NULL, 0);
  276. break;
  277. case BTSRV_TWS_UPDATE_BTPLAY_MODE:
  278. if (btif_tws_get_dev_role() != BTSRV_TWS_MASTER) {
  279. break;
  280. }
  281. tws_context.slave_actived = *((uint8_t *)param) ? 1: 0;
  282. if (tws_context.slave_actived) {
  283. if (!bt_manager_tws_check_is_woodpecker()) {
  284. #ifdef CONFIG_PROPERTY
  285. int mode = property_get_int("TWS_MODE", MEDIA_OUTPUT_MODE_LEFT);
  286. #else
  287. int mode = MEDIA_OUTPUT_MODE_LEFT;
  288. #endif
  289. if(mode == MEDIA_OUTPUT_MODE_LEFT) {
  290. bt_manager_set_sink_spk_pos(TWS_SPK_RIGHT);
  291. } else {
  292. bt_manager_set_sink_spk_pos(TWS_SPK_LEFT);
  293. }
  294. }
  295. bt_manager_set_sink_volume();
  296. tws_observer = btif_tws_get_tws_observer();
  297. bt_manager_event_notify(BT_REQ_RESTART_PLAY, NULL, 0);
  298. } else {
  299. tws_observer = NULL;
  300. }
  301. SYS_LOG_INF("Slave %s actived mode\n", tws_context.slave_actived ? "enter" : "exit");
  302. break;
  303. case BTSRV_TWS_UPDATE_PEER_VERSION:
  304. in_param = param;
  305. tws_context.peer_version = in_param->versoin;
  306. tws_context.peer_feature = in_param->feature;
  307. SYS_LOG_INF("peer version 0x%x, feature 0x%x", tws_context.peer_version, tws_context.peer_feature);
  308. tws_context.record_low_latency = system_check_low_latencey_mode() ? 1 : 0;
  309. if (bt_manager_tws_check_support_feature(TWS_FEATURE_LOW_LATENCY)) {
  310. system_set_low_latencey_mode((tws_context.record_low_latency ? true : false));
  311. } else {
  312. system_set_low_latencey_mode(false);
  313. }
  314. break;
  315. case BTSRV_TWS_SINK_START_PLAY:
  316. us281b_sink_start_play((uint8_t *)param, param_size);
  317. break;
  318. case BTSRV_TWS_PAIR_FAILED:
  319. SYS_LOG_INF("Tws pair failed");
  320. break;
  321. case BTSRV_TWS_EXPECT_TWS_ROLE:
  322. ret = bt_manager_config_expect_tws_connect_role();
  323. break;
  324. case BTSRV_TWS_SCO_DATA:
  325. // TO do
  326. break;
  327. default:
  328. break;
  329. }
  330. return ret;
  331. }
  332. static void us281b_event_report_input(uint32_t key_event)
  333. {
  334. static uint32_t pre_key_type = 0;
  335. static uint8_t pre_key_value = 0;
  336. static uint32_t duration = 0;
  337. uint32_t send_key, key_type, key_value;
  338. key_type = key_event & KEY_TYPE_ALL;
  339. key_value = (key_event >> 8) & 0xFF;
  340. /* ZS285A don't care short down */
  341. if (key_type == KEY_TYPE_SHORT_DOWN) {
  342. return;
  343. }
  344. /* ZS281B long up is short up, so 285A need convert short up after hold to long up */
  345. if (key_type == KEY_TYPE_SHORT_UP
  346. && pre_key_value == key_value
  347. && pre_key_type == KEY_TYPE_HOLD) {
  348. key_type = KEY_TYPE_LONG_UP;
  349. }
  350. if(pre_key_value == key_value && pre_key_type == key_type
  351. && os_uptime_get_32() - duration < 300) {
  352. key_type = KEY_TYPE_DOUBLE_CLICK;
  353. }
  354. send_key = key_type | key_value;
  355. SYS_LOG_INF("key 0x%x convert to 0x%x\n", key_event, send_key);
  356. sys_event_report_input(send_key);
  357. pre_key_value = key_value;
  358. pre_key_type = key_type;
  359. duration = os_uptime_get_32();
  360. }
  361. static void bt_manager_tws_update_tws_mode(uint8_t stream_type)
  362. {
  363. uint8_t tws_mode, drc_mode;
  364. bt_manager_tws_stream_type_to_tws_mode(stream_type, &tws_mode, &drc_mode);
  365. btif_tws_update_tws_mode(tws_mode, drc_mode);
  366. }
  367. static void bt_manager_tws_process_command(uint8_t *data, uint8_t len)
  368. {
  369. uint8_t event = data[0];
  370. uint32_t event_param;
  371. memcpy(&event_param, &data[1], 4);
  372. SYS_LOG_INF("event %d, event_param 0x%x", event, event_param);
  373. switch (event) {
  374. case TWS_INPUT_EVENT:
  375. {
  376. if (!bt_manager_tws_check_is_woodpecker()) {
  377. us281b_event_report_input(event_param);
  378. } else {
  379. sys_event_report_input(event_param);
  380. }
  381. break;
  382. }
  383. case TWS_UI_EVENT:
  384. {
  385. uint32_t bt_clock;
  386. memcpy(&bt_clock, &data[5], 4);
  387. _bt_manager_tws_add_event(event_param, bt_clock);
  388. break;
  389. }
  390. case TWS_SYSTEM_EVENT:
  391. {
  392. sys_event_send_message(event_param);
  393. break;
  394. }
  395. case TWS_VOLUME_EVENT:
  396. {
  397. SYS_LOG_INF("media_type %d music_vol %d\n", data[1], data[2]);
  398. #ifdef CONFIG_VOLUME_MANAGER
  399. system_volume_sync_remote_to_device(data[1], data[2]);
  400. #endif
  401. break;
  402. }
  403. case TWS_BATTERY_EVENT:
  404. {
  405. #ifdef CONFIG_POWER
  406. power_manager_set_slave_battery_state((event_param >> 24) & 0xff, (event_param & 0xffffff));
  407. #endif
  408. break;
  409. }
  410. case TWS_STATUS_EVENT:
  411. {
  412. SYS_LOG_INF("media type %d, codec_id %d sample_rate %d volume %d\n", data[2], data[3], data[4], data[5]);
  413. bt_manager_event_notify(data[1], data, 6);
  414. break;
  415. }
  416. }
  417. }
  418. #ifdef CONFIG_BT_TWS_US281B
  419. static void bt_manager_tws_process_us281b_command(uint8_t *data, uint8_t len)
  420. {
  421. uint8_t comvert_data[5], stream_type;
  422. uint32_t param;
  423. switch (data[0]) {
  424. case TWS_SYNC_S2M_UI_UPDATE_BAT_EV:
  425. comvert_data[0] = TWS_BATTERY_EVENT;
  426. param = (data[1]&0xF)*(4200000 - 3200000)/10 + 3200000;
  427. param |= (((data[1]&0xF)*10) << 24);
  428. memcpy(&comvert_data[1], &param, sizeof(param));
  429. bt_manager_tws_process_command(comvert_data, (sizeof(param) + 1));
  430. break;
  431. case TWS_SYNC_S2M_UI_UPDATE_KEY_EV:
  432. comvert_data[0] = TWS_INPUT_EVENT;
  433. memcpy(&comvert_data[1], &data[1], 4);
  434. bt_manager_tws_process_command(comvert_data, 5);
  435. break;
  436. case TWS_SYNC_M2S_UI_SET_VOL_VAL_CMD:
  437. bt_manager_tws_tws_mode_to_stream_type(tws_context.sink_tws_mode, tws_context.sink_drc_mode, &stream_type);
  438. tws_context.sink_volume = data[1];
  439. comvert_data[0] = TWS_VOLUME_EVENT;
  440. comvert_data[1] = stream_type;
  441. comvert_data[2] = tws_context.sink_volume;
  442. bt_manager_tws_process_command(comvert_data, 3);
  443. break;
  444. case TWS_SYNC_M2S_UI_SET_VOL_LIMIT_CMD:
  445. SYS_LOG_INF("sink_vol_limit\n");
  446. break;
  447. case TWS_SYNC_M2S_UI_SWITCH_POS_CMD:
  448. SYS_LOG_INF("switch_pos %d\n", data[1]);
  449. bt_manager_event_notify(BT_TWS_CHANNEL_MODE_SWITCH, &data[1], 1);
  450. break;
  451. case TWS_SYNC_M2S_UI_POWEROFF_CMD:
  452. comvert_data[0] = TWS_UI_EVENT;
  453. param = SYS_EVENT_POWER_OFF;
  454. memcpy(&comvert_data[1], &param, sizeof(param));
  455. bt_manager_tws_process_command(comvert_data, (sizeof(param) + 1));
  456. break;
  457. case TWS_SYNC_M2S_UI_SWITCH_TWS_MODE_CMD:
  458. tws_context.sink_tws_mode = data[1];
  459. tws_context.sink_drc_mode = data[2];
  460. SYS_LOG_INF("sink tws/drc mode %d %d\n", tws_context.sink_tws_mode, tws_context.sink_drc_mode);
  461. break;
  462. case TWS_SYNC_M2S_START_PLAYER:
  463. SYS_LOG_INF("media type %d, codec_id %d sample_rate %d volume %d\n", data[2], data[3], data[4], data[5]);
  464. bt_manager_event_notify(data[1], data, 6);
  465. break;
  466. case TWS_SYNC_M2S_STOP_PLAYER:
  467. SYS_LOG_INF("stop player \n");
  468. bt_manager_event_notify(data[1], data, 2);
  469. break;
  470. default:
  471. SYS_LOG_INF("Wait todo cmd_id 0x%x\n", data[0]);
  472. break;
  473. }
  474. }
  475. #endif
  476. void bt_manager_tws_send_event(uint8_t event, uint32_t event_param)
  477. {
  478. uint8_t tws_event_data[5];
  479. memset(tws_event_data, 0, sizeof(tws_event_data));
  480. tws_event_data[0] = event;
  481. memcpy(&tws_event_data[1], &event_param, 4);
  482. bt_manager_tws_send_command(tws_event_data, sizeof(tws_event_data));
  483. }
  484. void bt_manager_tws_send_event_sync(uint8_t event, uint32_t event_param)
  485. {
  486. uint8_t tws_event_data[10];
  487. uint32_t bt_clock = btif_tws_get_bt_clock() + TWS_TIMETO_BT_CLOCK(100);
  488. memset(tws_event_data, 0, sizeof(tws_event_data));
  489. tws_event_data[0] = event;
  490. memcpy(&tws_event_data[1], &event_param, 4);
  491. memcpy(&tws_event_data[5], &bt_clock, 4);
  492. bt_manager_tws_send_command(tws_event_data, sizeof(tws_event_data));
  493. _bt_manager_tws_add_event(event_param, bt_clock);
  494. }
  495. void bt_manager_tws_notify_start_play(uint8_t media_type, uint8_t codec_id, uint8_t sample_rate)
  496. {
  497. uint8_t tws_event_data[6];
  498. bool bt_play;
  499. bool local_play;
  500. if (btif_tws_get_dev_role() != BTSRV_TWS_MASTER) {
  501. return;
  502. }
  503. memset(tws_event_data, 0, sizeof(tws_event_data));
  504. tws_event_data[0] = TWS_STATUS_EVENT;
  505. tws_event_data[1] = BT_TWS_START_PLAY;
  506. tws_event_data[2] = media_type;
  507. tws_event_data[3] = codec_id;
  508. tws_event_data[4] = sample_rate;
  509. #ifdef CONFIG_AUDIO
  510. tws_event_data[5] = audio_system_get_stream_volume(media_type);
  511. #endif
  512. bt_play = (media_type == AUDIO_STREAM_MUSIC)? true : false;
  513. local_play = (media_type == AUDIO_STREAM_LOCAL_MUSIC)? true : false;
  514. btif_tws_set_bt_local_play(bt_play, local_play);
  515. bt_manager_tws_send_command(tws_event_data, sizeof(tws_event_data));
  516. }
  517. void bt_manager_tws_notify_stop_play(void)
  518. {
  519. uint8_t tws_event_data[5];
  520. if (btif_tws_get_dev_role() != BTSRV_TWS_MASTER) {
  521. return;
  522. }
  523. memset(tws_event_data, 0, sizeof(tws_event_data));
  524. tws_event_data[0] = TWS_STATUS_EVENT;
  525. tws_event_data[1] = BT_TWS_STOP_PLAY;
  526. bt_manager_tws_send_command(tws_event_data, sizeof(tws_event_data));
  527. }
  528. #ifdef CONFIG_BT_TWS_US281B
  529. static void bt_manager_tws_system_event_to_us281b(uint8_t *data, int len)
  530. {
  531. uint8_t cmd[2];
  532. uint32_t event;
  533. memcpy(&event, &data[1], sizeof(event));
  534. switch (event) {
  535. case MSG_POWER_OFF:
  536. cmd[0] = TWS_SYNC_M2S_UI_POWEROFF_CMD;
  537. cmd[1] = TWS_POWEROFF_KEY;
  538. btif_tws_send_command(cmd, 2);
  539. break;
  540. default:
  541. SYS_LOG_INF("Wait todo event %d\n", event);
  542. }
  543. }
  544. void bt_manager_tws_to_us281b_send_command(uint8_t *command, int command_len)
  545. {
  546. uint8_t cmd[8], event;
  547. uint32_t param;
  548. memset(cmd, 0, sizeof(cmd));
  549. event = command[0];
  550. switch (event) {
  551. /* Tws master to slave event */
  552. case TWS_UI_EVENT:
  553. /* Current not send tts event to slave */
  554. break;
  555. case TWS_VOLUME_EVENT:
  556. cmd[0] = TWS_SYNC_M2S_UI_SET_VOL_VAL_CMD;
  557. /* command[1]: media_type, not support send */
  558. cmd[1] = command[2];
  559. btif_tws_send_command(cmd, 2);
  560. break;
  561. case TWS_SYSTEM_EVENT:
  562. bt_manager_tws_system_event_to_us281b(command, command_len);
  563. break;
  564. case TWS_STATUS_EVENT:
  565. if (command[1] == BT_TWS_START_PLAY) {
  566. /* Sync tws mode and volume */
  567. SYS_LOG_INF("START_PLAY %d,%d,%d,%d\n", command[2], command[3], command[4], command[5]);
  568. bt_manager_tws_update_tws_mode(command[2]);
  569. if (!bt_manager_tws_check_support_feature(TWS_FEATURE_UI_STARTSTOP_CMD)) {
  570. cmd[0] = TWS_SYNC_M2S_UI_SET_VOL_VAL_CMD;
  571. cmd[1] = command[5]; /* volume */
  572. btif_tws_send_command_sync(cmd, 2);
  573. } else {
  574. command[0] = TWS_SYNC_M2S_START_PLAYER;
  575. command[1] = BT_TWS_START_PLAY;
  576. btif_tws_send_command_sync(command, 6);
  577. }
  578. } else if (command[1] == BT_TWS_STOP_PLAY) {
  579. if (bt_manager_tws_check_support_feature(TWS_FEATURE_UI_STARTSTOP_CMD)) {
  580. command[0] = TWS_SYNC_M2S_STOP_PLAYER;
  581. command[1] = BT_TWS_STOP_PLAY;
  582. btif_tws_send_command_sync(command, 2);
  583. }
  584. }
  585. break;
  586. /* Tws slave to master event */
  587. case TWS_BATTERY_EVENT:
  588. memcpy(&param, &command[1], sizeof(param));
  589. cmd[0] = TWS_SYNC_S2M_UI_UPDATE_BAT_EV;
  590. cmd[1] = (uint8_t)param;
  591. btif_tws_send_command(cmd, 2);
  592. break;
  593. case TWS_INPUT_EVENT:
  594. cmd[0] = TWS_SYNC_S2M_UI_UPDATE_KEY_EV;
  595. memcpy(&cmd[1], &command[1], 4);
  596. btif_tws_send_command(cmd, 5);
  597. break;
  598. default:
  599. SYS_LOG_INF("Wait todo event: %d\n", event);
  600. break;
  601. }
  602. }
  603. #endif
  604. void bt_manager_tws_send_command(uint8_t *command, int command_len)
  605. {
  606. #ifdef CONFIG_BT_TWS_US281B
  607. bt_manager_tws_to_us281b_send_command(command, command_len);
  608. #else
  609. btif_tws_send_command(command, command_len);
  610. #endif
  611. }
  612. void bt_manager_tws_send_sync_command(uint8_t *command, int command_len)
  613. {
  614. #ifdef CONFIG_BT_TWS_US281B
  615. /* Change to 281b command */
  616. #else
  617. btif_tws_send_command_sync(command, command_len);
  618. #endif
  619. }
  620. void bt_manager_tws_sync_volume_to_slave(uint32_t media_type, uint32_t music_vol)
  621. {
  622. uint8_t tws_event_data[4];
  623. if (btif_tws_get_dev_role() == BTSRV_TWS_MASTER) {
  624. memset(tws_event_data, 0, sizeof(tws_event_data));
  625. tws_event_data[0] = TWS_VOLUME_EVENT;
  626. tws_event_data[1] = media_type;
  627. tws_event_data[2] = music_vol;
  628. bt_manager_tws_send_command(tws_event_data, sizeof(tws_event_data));
  629. SYS_LOG_INF("media_type %d music_vol %d\n", media_type, music_vol);
  630. }
  631. }
  632. void bt_manager_tws_cancel_wait_pair(void)
  633. {
  634. btif_tws_cancel_wait_pair();
  635. }
  636. void bt_manager_tws_wait_pair(void)
  637. {
  638. uint8_t i;
  639. if (btif_tws_is_in_connecting()) {
  640. btif_br_auto_reconnect_stop();
  641. i = 10;
  642. while (i-- > 0) {
  643. os_sleep(20);
  644. if (!btif_tws_is_in_connecting()) {
  645. SYS_LOG_INF("Stop tws connecting\n");
  646. break;
  647. }
  648. }
  649. }
  650. if ((bt_manager_get_connected_dev_num() < BT_MANAGER_MAX_BR_NUM) &&
  651. btif_tws_can_do_pair()) {
  652. bt_manager_set_status(BT_STATUS_MASTER_WAIT_PAIR);
  653. btif_tws_wait_pair(TWS_PAIR_TYR_TIMES);
  654. } else {
  655. btif_tws_cancel_wait_pair();
  656. }
  657. }
  658. void bt_manager_tws_disconnect_and_wait_pair(void)
  659. {
  660. uint16_t i;
  661. btif_br_auto_reconnect_stop();
  662. btif_br_disconnect_device(BTSRV_DISCONNECT_ALL_MODE);
  663. if (btif_tws_is_in_connecting()) {
  664. btif_br_auto_reconnect_stop();
  665. i = 10;
  666. while (i-- > 0) {
  667. os_sleep(20);
  668. if (!btif_tws_is_in_connecting()) {
  669. SYS_LOG_INF("Stop tws connecting\n");
  670. break;
  671. }
  672. }
  673. }
  674. i = 1000;
  675. while (i-- > 0) {
  676. os_sleep(2);
  677. if (!btif_br_get_connected_device_num()) {
  678. SYS_LOG_INF("all disconnected \n");
  679. break;
  680. }
  681. }
  682. bt_manager_set_status(BT_STATUS_MASTER_WAIT_PAIR);
  683. btif_tws_wait_pair(TWS_PAIR_TYR_TIMES);
  684. }
  685. tws_runtime_observer_t *bt_manager_tws_get_runtime_observer(void)
  686. {
  687. if (btif_tws_get_dev_role() == BTSRV_TWS_MASTER
  688. || btif_tws_get_dev_role() == BTSRV_TWS_SLAVE) {
  689. return tws_observer;
  690. }
  691. return NULL;
  692. }
  693. void bt_manager_tws_init(void)
  694. {
  695. btif_tws_init(_bt_manager_tws_event_notify);
  696. memset(&tws_context, 0, sizeof(struct bt_manager_tws_context_t));
  697. sys_slist_init(&tws_context.tws_sync_event_list);
  698. os_work_init(&tws_context.tws_irq_work, _bt_manager_irq_work);
  699. tws_observer = NULL;
  700. }
  701. int bt_manager_tws_get_dev_role(void)
  702. {
  703. return btif_tws_get_dev_role();
  704. }
  705. uint8_t bt_manager_tws_get_peer_version(void)
  706. {
  707. return tws_context.peer_version;
  708. }
  709. bool bt_manager_tws_check_is_woodpecker(void)
  710. {
  711. if (btif_tws_get_dev_role() != BTSRV_TWS_NONE) {
  712. return IS_WOODPECKER_VERSION(tws_context.peer_version);
  713. } else {
  714. return true;
  715. }
  716. }
  717. bool bt_manager_tws_check_support_feature(uint32_t feature)
  718. {
  719. if (btif_tws_get_dev_role() != BTSRV_TWS_NONE) {
  720. if ((get_tws_current_feature() & feature) && (tws_context.peer_feature & feature)) {
  721. return true;
  722. } else {
  723. return false;
  724. }
  725. } else {
  726. return true;
  727. }
  728. }
  729. void bt_manager_tws_set_stream_type(uint8_t stream_type)
  730. {
  731. uint8_t cmd[2];
  732. int volume = 0;
  733. tws_context.source_stream_type = stream_type;
  734. /* Event not connected, update to tws service */
  735. bt_manager_tws_update_tws_mode(stream_type);
  736. if (btif_tws_get_dev_role() == BTSRV_TWS_MASTER) {
  737. #ifdef CONFIG_AUDIO
  738. volume = audio_system_get_stream_volume(stream_type);
  739. #endif
  740. if (volume >= 0) {
  741. cmd[0] = TWS_SYNC_M2S_UI_SET_VOL_VAL_CMD;
  742. cmd[1] = (uint8_t)volume;
  743. btif_tws_send_command(cmd, 2);
  744. }
  745. }
  746. }
  747. void bt_manager_tws_set_codec(uint8_t codec)
  748. {
  749. btif_tws_set_codec(codec);
  750. }
  751. void bt_manager_tws_channel_mode_switch(void)
  752. {
  753. #if CONFIG_TWS_AUDIO_OUT_MODE == 0
  754. uint8_t cmd[2];
  755. #ifdef CONFIG_PROPERTY
  756. int channel_mode = property_get_int("TWS_MODE", MEDIA_OUTPUT_MODE_LEFT);
  757. #else
  758. int channel_mode = MEDIA_OUTPUT_MODE_LEFT;
  759. #endif
  760. if (btif_tws_get_dev_role() != BTSRV_TWS_MASTER) {
  761. return;
  762. }
  763. cmd[0] = TWS_SYNC_M2S_UI_SWITCH_POS_CMD;
  764. if (channel_mode == MEDIA_OUTPUT_MODE_LEFT) {
  765. channel_mode = MEDIA_OUTPUT_MODE_RIGHT;
  766. cmd[1] = MEDIA_OUTPUT_MODE_LEFT;
  767. } else {
  768. channel_mode = MEDIA_OUTPUT_MODE_LEFT;
  769. cmd[1] = MEDIA_OUTPUT_MODE_RIGHT;
  770. }
  771. btif_tws_send_command(cmd, 2);
  772. #ifdef CONFIG_PROPERTY
  773. property_set_int("TWS_MODE", channel_mode);
  774. #endif
  775. #ifdef CONFIG_MEDIA_PLAYER
  776. media_player_t *player = media_player_get_current_main_player();
  777. if (player) {
  778. if (channel_mode == MEDIA_OUTPUT_MODE_LEFT || channel_mode == MEDIA_OUTPUT_MODE_RIGHT) {
  779. media_player_set_output_mode(player, channel_mode);
  780. }
  781. }
  782. SYS_LOG_INF("%d\n",channel_mode);
  783. #endif
  784. #endif
  785. }