bt_a2dp_avrcp_demo.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /** @file
  2. bt_a2dp_demo.c
  3. @brief
  4. This file is demo of bt a2dp.
  5. */
  6. /*================================================================
  7. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  8. Quectel Wireless Solution Proprietary and Confidential.
  9. =================================================================*/
  10. /*=================================================================
  11. EDIT HISTORY FOR MODULE
  12. This section contains comments describing changes made to the module.
  13. Notice that changes are listed in reverse chronological order.
  14. WHEN WHO WHAT, WHERE, WHY
  15. ------------ ------- -------------------------------------------------------------------------------
  16. =================================================================*/
  17. /*===========================================================================
  18. * include files
  19. ===========================================================================*/
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "ql_api_osi.h"
  24. #include "ql_api_bt.h"
  25. #include "ql_api_bt_a2dp.h"
  26. #include "ql_api_bt_avrcp.h"
  27. #include "ql_log.h"
  28. #include "bt_a2dp_avrcp_demo.h"
  29. #include "ql_audio.h"
  30. /*===========================================================================
  31. * Macro Definition
  32. ===========================================================================*/
  33. #define QL_A2DP_AVRCP_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  34. #define QL_A2DP_AVRCP_DEMO_LOG(msg, ...) QL_LOG(QL_A2DP_AVRCP_DEMO_LOG_LEVEL, "QL_A2DP_AVRCP_DEMO", msg, ##__VA_ARGS__)
  35. #define QL_A2DP_AVRCP_DEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("QL_A2DP_AVRCP_DEMO", msg, ##__VA_ARGS__)
  36. #define QL_A2DP_AVRCP_TASK_STACK_SIZE (4*1024)
  37. #define QL_A2DP_AVRCP_TASK_PRIO APP_PRIORITY_NORMAL
  38. #define QL_A2DP_AVRCP_TASK_EVENT_CNT 5
  39. /*===========================================================================
  40. * Struct
  41. ===========================================================================*/
  42. /*===========================================================================
  43. * Enum
  44. ===========================================================================*/
  45. /*===========================================================================
  46. * Variate
  47. ===========================================================================*/
  48. ql_task_t bt_a2dp_avrcp_task = NULL;
  49. ql_bt_addr_s bt_local_addr = {{0x7e, 0x5c, 0x91, 0xa4, 0x40, 0xb4}};
  50. /*===========================================================================
  51. * Functions
  52. ===========================================================================*/
  53. static void ql_bt_a2dp_avrcp_notify_cb(void *ind_msg_buf, void *ctx)
  54. {
  55. ql_event_t *ql_event = NULL;
  56. if (ind_msg_buf == NULL)
  57. {
  58. return ;
  59. }
  60. ql_event = (ql_event_t *)ind_msg_buf;
  61. if (ql_event->id != 0)
  62. {
  63. //ql_rtos_event_send(bt_a2dp_avrcp_task,ql_event);
  64. }
  65. }
  66. static void ql_a2dp_avrcp_demo_thread(void *argv)
  67. {
  68. QlOSStatus err = QL_OSI_SUCCESS;
  69. ql_errcode_bt_e ret;
  70. QUEC_AVRCP_PLAYBACK_STATUS_e avrcp_state;
  71. ql_bt_ble_local_name_s bt_name={0};
  72. int set_vol;
  73. uint8_t get_vol;
  74. ql_bt_visible_mode_e scanmode = QL_BT_PAGE_AND_INQUIRY_ENABLE;
  75. unsigned char str_ptr[32];
  76. ql_set_audio_path_speaker();
  77. ret = ql_bt_a2dp_avrcp_init(ql_bt_a2dp_avrcp_notify_cb);
  78. if (ret != QL_BT_SUCCESS)
  79. {
  80. QL_A2DP_AVRCP_DEMO_LOG("init error");
  81. goto QL_BT_A2DP_AVRCP_NOT_INIT_EXIT;
  82. }
  83. ret = ql_bt_start();
  84. if (ret != QL_BT_SUCCESS)
  85. {
  86. QL_A2DP_AVRCP_DEMO_LOG("start error");
  87. goto QL_BT_A2DP_AVRCP_INIT_EXIT;
  88. }
  89. ql_rtos_task_sleep_ms(2000);
  90. memcpy(bt_name.name, "QL_A2DP_AVRCP_DEMO", 18);
  91. ret = ql_bt_ble_set_localname(bt_name);
  92. if (ret != QL_BT_SUCCESS)
  93. {
  94. QL_A2DP_AVRCP_DEMO_LOG("set name error");
  95. goto QL_BT_A2DP_AVRCP_INIT_EXIT;
  96. }
  97. ret = ql_classic_bt_set_scanmode(scanmode);
  98. if (ret != QL_BT_SUCCESS)
  99. {
  100. QL_A2DP_AVRCP_DEMO_LOG("set scanmode error");
  101. goto QL_BT_A2DP_AVRCP_INIT_EXIT;
  102. }
  103. while (1)
  104. {
  105. QL_A2DP_AVRCP_DEMO_LOG("checking connect");
  106. if((ql_bt_avrcp_connection_state_get() == QUEC_BTAVRCP_CONNECTION_STATE_CONNECTED) && (ql_bt_a2dp_connection_state_get() == QUEC_BTA2DP_CONNECTION_STATE_CONNECTED))
  107. {
  108. QL_A2DP_AVRCP_DEMO_LOG("connect success");
  109. QL_A2DP_AVRCP_DEMO_LOG("get address");
  110. ret = ql_bt_a2dp_get_addr(&bt_local_addr);
  111. if (ret == QL_BT_SUCCESS)
  112. {
  113. memset(str_ptr,0x00,sizeof(str_ptr));
  114. sprintf((char *)str_ptr, "%02x:%02x:%02x:%02x:%02x:%02x", bt_local_addr.addr[0], bt_local_addr.addr[1], bt_local_addr.addr[2], bt_local_addr.addr[3], bt_local_addr.addr[4], bt_local_addr.addr[5]);
  115. QL_A2DP_AVRCP_DEMO_LOG("addr=%s",str_ptr);
  116. }
  117. else
  118. {
  119. QL_A2DP_AVRCP_DEMO_LOG("error=%x", ret);
  120. }
  121. while(1)
  122. {
  123. if((ql_bt_avrcp_connection_state_get() != QUEC_BTAVRCP_CONNECTION_STATE_CONNECTED) || (ql_bt_a2dp_connection_state_get() != QUEC_BTA2DP_CONNECTION_STATE_CONNECTED))
  124. {
  125. QL_A2DP_AVRCP_DEMO_LOG("disconnect");
  126. break;
  127. }
  128. QL_A2DP_AVRCP_DEMO_LOG("checking paly");
  129. ql_bt_avrcp_play_state_get(&avrcp_state);
  130. if(avrcp_state == QUEC_AVRCP_PLAYBACK_STATUS_PLAYING)
  131. {
  132. QL_A2DP_AVRCP_DEMO_LOG("playing");
  133. ql_rtos_task_sleep_ms(3000);
  134. QL_A2DP_AVRCP_DEMO_LOG("pause");
  135. ql_bt_avrcp_pause();
  136. ql_rtos_task_sleep_ms(3000);
  137. QL_A2DP_AVRCP_DEMO_LOG("start");
  138. ql_bt_avrcp_start();
  139. ql_rtos_task_sleep_ms(3000);
  140. QL_A2DP_AVRCP_DEMO_LOG("previ");
  141. ql_bt_avrcp_previ();
  142. ql_rtos_task_sleep_ms(3000);
  143. QL_A2DP_AVRCP_DEMO_LOG("next");
  144. ql_bt_avrcp_next();
  145. ql_rtos_task_sleep_ms(3000);
  146. ql_bt_avrcp_vol_get(&get_vol);
  147. QL_A2DP_AVRCP_DEMO_LOG("get vol %d", get_vol);
  148. ql_rtos_task_sleep_ms(3000);
  149. set_vol = 11;
  150. ql_bt_avrcp_vol_set(set_vol);
  151. QL_A2DP_AVRCP_DEMO_LOG("set vol %d", set_vol);
  152. ql_rtos_task_sleep_ms(3000);
  153. set_vol = 5;
  154. ql_bt_avrcp_vol_set(set_vol);
  155. QL_A2DP_AVRCP_DEMO_LOG("set vol %d", set_vol);
  156. ql_rtos_task_sleep_ms(3000);
  157. QL_A2DP_AVRCP_DEMO_LOG("end");
  158. ql_bt_a2dp_disconnect(bt_local_addr);
  159. ql_bt_a2dp_avrcp_release();
  160. ql_bt_a2dp_avrcp_init(ql_bt_a2dp_avrcp_notify_cb);
  161. break;
  162. }
  163. ql_rtos_task_sleep_s(1);
  164. }
  165. }
  166. ql_rtos_task_sleep_s(1);
  167. }
  168. QL_BT_A2DP_AVRCP_INIT_EXIT:
  169. ql_bt_a2dp_avrcp_release();
  170. ql_bt_stop();
  171. QL_BT_A2DP_AVRCP_NOT_INIT_EXIT:
  172. err = ql_rtos_task_delete(NULL);
  173. if(err != QL_OSI_SUCCESS)
  174. {
  175. QL_A2DP_AVRCP_DEMO_LOG("deleted failed");
  176. }
  177. }
  178. void ql_bt_a2dp_avrcp_app_init(void)
  179. {
  180. QlOSStatus err = QL_OSI_SUCCESS;
  181. err = ql_rtos_task_create(&bt_a2dp_avrcp_task, QL_A2DP_AVRCP_TASK_STACK_SIZE, QL_A2DP_AVRCP_TASK_PRIO, "bt a2dp avrcp", ql_a2dp_avrcp_demo_thread, NULL, QL_A2DP_AVRCP_TASK_EVENT_CNT);
  182. if (err != QL_OSI_SUCCESS)
  183. {
  184. QL_A2DP_AVRCP_DEMO_LOG("Create bt audio task Fail");
  185. }
  186. }