bt_hfp_demo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "ql_api_osi.h"
  9. #include "ql_api_bt.h"
  10. #include "ql_api_bt_hfp.h"
  11. #include "ql_log.h"
  12. #include "bt_hfp_demo.h"
  13. #define QL_BT_HFP_LOG_LEVEL QL_LOG_LEVEL_INFO
  14. #define QL_BT_HFP_LOG(msg, ...) QL_LOG(QL_BT_HFP_LOG_LEVEL, "ql_BT_HFP_DEMO", msg, ##__VA_ARGS__)
  15. #define QL_BT_HFP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_BT_HFP_DEMO", msg, ##__VA_ARGS__)
  16. ql_task_t bt_hfp_demo_task = NULL;
  17. static ql_bt_addr_s ql_bt_hfp_remote_addr = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x38}};
  18. static unsigned char ql_bt_hfp_volume = 15; //range:1-15
  19. static unsigned char ql_bt_hfp_phone_nubmer[] = "13249166530";
  20. static unsigned char ql_bt_hfp_ctrl_cmd = 2;
  21. static ql_bt_hfp_call_state_e ql_bt_hfp_call = QL_BT_HFP_CALL_NO_CALL_IN_PROGRESS;
  22. static ql_bt_hfp_connection_state_e ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_DISCONNECTED;
  23. extern ql_errcode_bt_e ql_bt_demo_start();
  24. extern ql_errcode_bt_e ql_bt_demo_stop();
  25. extern ql_errcode_bt_e ql_bt_demo_get_state();
  26. extern ql_errcode_bt_e ql_bt_demo_get_local_name();
  27. extern ql_errcode_bt_e ql_bt_demo_set_local_name();
  28. extern ql_errcode_bt_e ql_bt_demo_get_scanmde();
  29. extern ql_errcode_bt_e ql_be_demo_set_scanmode();
  30. ql_errcode_bt_e ql_bt_hfp_demo_connect()
  31. {
  32. ql_errcode_bt_e ret;
  33. ret = ql_bt_hfp_connect(ql_bt_hfp_remote_addr);
  34. if (ret == QL_BT_SUCCESS)
  35. {
  36. QL_BT_HFP_LOG("sucess");
  37. }
  38. else
  39. {
  40. QL_BT_HFP_LOG("error=%x", ret);
  41. }
  42. return ret;
  43. }
  44. ql_errcode_bt_e ql_bt_hfp_demo_disconnect()
  45. {
  46. ql_errcode_bt_e ret;
  47. ret = ql_bt_hfp_disconnect(ql_bt_hfp_remote_addr);
  48. if (ret == QL_BT_SUCCESS)
  49. {
  50. QL_BT_HFP_LOG("sucess");
  51. }
  52. else
  53. {
  54. QL_BT_HFP_LOG("error=%x", ret);
  55. }
  56. return ret;
  57. }
  58. ql_errcode_bt_e ql_bt_hfp_demo_set_volume()
  59. {
  60. ql_errcode_bt_e ret;
  61. ret = ql_bt_hfp_set_volume(ql_bt_hfp_remote_addr, ql_bt_hfp_volume);
  62. if (ret == QL_BT_SUCCESS)
  63. {
  64. QL_BT_HFP_LOG("sucess");
  65. }
  66. else
  67. {
  68. QL_BT_HFP_LOG("error=%x", ret);
  69. }
  70. return ret;
  71. }
  72. ql_errcode_bt_e ql_bt_hfp_demo_reject_call()
  73. {
  74. ql_errcode_bt_e ret;
  75. ret = ql_bt_hfp_reject_call(ql_bt_hfp_remote_addr);
  76. if (ret == QL_BT_SUCCESS)
  77. {
  78. QL_BT_HFP_LOG("sucess");
  79. }
  80. else
  81. {
  82. QL_BT_HFP_LOG("error=%x", ret);
  83. }
  84. return ret;
  85. }
  86. ql_errcode_bt_e ql_bt_hfp_demo_answer_accept_call()
  87. {
  88. ql_errcode_bt_e ret;
  89. ret = ql_bt_hfp_answer_accept_call(ql_bt_hfp_remote_addr);
  90. if (ret == QL_BT_SUCCESS)
  91. {
  92. QL_BT_HFP_LOG("sucess");
  93. }
  94. else
  95. {
  96. QL_BT_HFP_LOG("error=%x", ret);
  97. }
  98. return ret;
  99. }
  100. ql_errcode_bt_e ql_bt_hfp_demo_answer_reject_call()
  101. {
  102. ql_errcode_bt_e ret;
  103. ret = ql_bt_hfp_answer_reject_call(ql_bt_hfp_remote_addr);
  104. if (ret == QL_BT_SUCCESS)
  105. {
  106. QL_BT_HFP_LOG("sucess");
  107. }
  108. else
  109. {
  110. QL_BT_HFP_LOG("error=%x", ret);
  111. }
  112. return ret;
  113. }
  114. ql_errcode_bt_e ql_bt_hfp_demo_dial()
  115. {
  116. ql_errcode_bt_e ret;
  117. ret = ql_bt_hfp_dial(ql_bt_hfp_remote_addr, ql_bt_hfp_phone_nubmer);
  118. if (ret == QL_BT_SUCCESS)
  119. {
  120. QL_BT_HFP_LOG("sucess");
  121. }
  122. else
  123. {
  124. QL_BT_HFP_LOG("error=%x", ret);
  125. }
  126. return ret;
  127. }
  128. ql_errcode_bt_e ql_bt_hfp_demo_redial()
  129. {
  130. ql_errcode_bt_e ret;
  131. ret = ql_bt_hfp_redial(ql_bt_hfp_remote_addr);
  132. if (ret == QL_BT_SUCCESS)
  133. {
  134. QL_BT_HFP_LOG("sucess");
  135. }
  136. else
  137. {
  138. QL_BT_HFP_LOG("error=%x", ret);
  139. }
  140. return ret;
  141. }
  142. ql_errcode_bt_e ql_bt_hfp_demo_vr_enable()
  143. {
  144. ql_errcode_bt_e ret;
  145. ret = ql_bt_hfp_vr_enable(ql_bt_hfp_remote_addr);
  146. if (ret == QL_BT_SUCCESS)
  147. {
  148. QL_BT_HFP_LOG("sucess");
  149. }
  150. else
  151. {
  152. QL_BT_HFP_LOG("error=%x", ret);
  153. }
  154. return ret;
  155. }
  156. ql_errcode_bt_e ql_bt_hfp_demo_vr_disable()
  157. {
  158. ql_errcode_bt_e ret;
  159. ret = ql_bt_hfp_vr_disable(ql_bt_hfp_remote_addr);
  160. if (ret == QL_BT_SUCCESS)
  161. {
  162. QL_BT_HFP_LOG("sucess");
  163. }
  164. else
  165. {
  166. QL_BT_HFP_LOG("error=%x", ret);
  167. }
  168. return ret;
  169. }
  170. ql_errcode_bt_e ql_bt_hfp_demo_three_way_call()
  171. {
  172. ql_errcode_bt_e ret;
  173. ret = ql_bt_hfp_ctrl_three_way_call(ql_bt_hfp_remote_addr, ql_bt_hfp_ctrl_cmd);
  174. if (ret == QL_BT_SUCCESS)
  175. {
  176. QL_BT_HFP_LOG("sucess");
  177. }
  178. else
  179. {
  180. QL_BT_HFP_LOG("error=%x", ret);
  181. }
  182. return ret;
  183. }
  184. static void ql_bt_hfp_notify_cb(void *ind_msg_buf, void *ctx)
  185. {
  186. ql_event_t *event_temp = NULL;
  187. ql_event_t test_event = {0};
  188. if (ind_msg_buf == NULL)
  189. {
  190. return ;
  191. }
  192. event_temp = (ql_event_t *)ind_msg_buf;
  193. switch (event_temp->id)
  194. {
  195. case QUEC_BT_HFP_CONNECT_IND:
  196. case QUEC_BT_HFP_DISCONNECT_IND:
  197. case QUEC_BT_HFP_CALL_IND:
  198. case QUEC_BT_HFP_CALL_SETUP_IND:
  199. case QUEC_BT_HFP_NETWORK_IND:
  200. case QUEC_BT_HFP_NETWORK_SIGNAL_IND:
  201. case QUEC_BT_HFP_BATTERY_IND:
  202. case QUEC_BT_HFP_CALLHELD_IND:
  203. case QUEC_BT_HFP_AUDIO_IND:
  204. case QUEC_BT_HFP_VOLUME_IND:
  205. case QUEC_BT_HFP_NETWORK_TYPE_IND:
  206. case QUEC_BT_HFP_CODEC_IND:
  207. case QUEC_BT_HFP_RING_IND:
  208. {
  209. ql_bt_hfp_event_info_t *temp = (ql_bt_hfp_event_info_t *)event_temp->param2;
  210. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)malloc(sizeof(ql_bt_hfp_event_info_t));
  211. if (hfp_info)
  212. {
  213. memset(hfp_info, 0x00, sizeof(ql_bt_hfp_event_info_t));
  214. hfp_info->state = temp->state;
  215. memcpy(hfp_info->addr.addr, temp->addr.addr, QL_BT_MAC_ADDRESS_SIZE);
  216. test_event.id = event_temp->id;
  217. test_event.param2 = (uint32)hfp_info;
  218. }
  219. }
  220. break;
  221. default:
  222. {
  223. test_event.id = event_temp->id;
  224. test_event.param1 = event_temp->param1;
  225. test_event.param2 = event_temp->param2;
  226. test_event.param3 = event_temp->param3;
  227. }
  228. break;
  229. }
  230. if (test_event.id != 0)
  231. {
  232. ql_rtos_event_send(bt_hfp_demo_task,&test_event);
  233. }
  234. }
  235. /************************************************************************
  236. *以下事件已经通过内核释放内存,不需要继续关注
  237. * QUEC_BT_HFP_CONNECT_IND
  238. * QUEC_BT_HFP_DISCONNECT_IND
  239. * QUEC_BT_HFP_CALL_IND
  240. * QUEC_BT_HFP_CALL_SETUP_IND
  241. * QUEC_BT_HFP_NETWORK_IND
  242. * QUEC_BT_HFP_NETWORK_SIGNAL_IND
  243. * QUEC_BT_HFP_BATTERY_IND
  244. * QUEC_BT_HFP_CALLHELD_IND
  245. * QUEC_BT_HFP_AUDIO_IND
  246. * QUEC_BT_HFP_VOLUME_IND
  247. * QUEC_BT_HFP_NETWORK_TYPE_IND
  248. * QUEC_BT_HFP_RING_IND
  249. * QUEC_BT_HFP_CODEC_IND
  250. ************************************************************************/
  251. static ql_errcode_bt_e ql_bt_hfp_event()
  252. {
  253. ql_event_t test_event = {0};
  254. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  255. QL_BT_STATUS status = (QL_BT_STATUS)(test_event.param1);
  256. if (ql_event_try_wait(&test_event) == 0)
  257. {
  258. if(test_event.id == 0)
  259. {
  260. return ret;
  261. }
  262. status = (QL_BT_STATUS)(test_event.param1);
  263. switch (test_event.id)
  264. {
  265. case QUEC_BT_START_STATUS_IND:
  266. {
  267. if (QL_BT_STATUS_SUCCESS == status)
  268. {
  269. QL_BT_HFP_LOG("start sucess");
  270. ret = ql_bt_demo_get_state();
  271. if (ret != QL_BT_SUCCESS)
  272. {
  273. goto QL_BT_HFP_STOP;
  274. }
  275. ret = ql_bt_demo_get_local_name();
  276. if (ret != QL_BT_SUCCESS)
  277. {
  278. goto QL_BT_HFP_STOP;
  279. }
  280. ret = ql_bt_demo_set_local_name();
  281. if (ret != QL_BT_SUCCESS)
  282. {
  283. goto QL_BT_HFP_STOP;
  284. }
  285. ret = ql_bt_demo_get_local_name();
  286. if (ret != QL_BT_SUCCESS)
  287. {
  288. goto QL_BT_HFP_STOP;
  289. }
  290. ret = ql_bt_demo_get_scanmde();
  291. if (ret != QL_BT_SUCCESS)
  292. {
  293. goto QL_BT_HFP_STOP;
  294. }
  295. ret = ql_be_demo_set_scanmode();
  296. if (ret != QL_BT_SUCCESS)
  297. {
  298. goto QL_BT_HFP_STOP;
  299. }
  300. ret = ql_bt_demo_get_scanmde();
  301. if (ret != QL_BT_SUCCESS)
  302. {
  303. goto QL_BT_HFP_STOP;
  304. }
  305. //
  306. }
  307. else
  308. {
  309. QL_BT_HFP_LOG("start failed");
  310. }
  311. }
  312. break;
  313. case QUEC_BT_STOP_STATUS_IND:
  314. {
  315. if (QL_BT_STATUS_SUCCESS == status)
  316. {
  317. QL_BT_HFP_LOG("stop sucess");
  318. }
  319. else
  320. {
  321. QL_BT_HFP_LOG("stop failed");
  322. }
  323. }
  324. break;
  325. case QUEC_BT_HFP_CONNECT_IND:
  326. {
  327. if (QL_BT_STATUS_SUCCESS == status)
  328. {
  329. QL_BT_HFP_LOG("conn sucess");
  330. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  331. if (hfp_info)
  332. {
  333. //hfp_info point to a malloc addrss,need to free
  334. QL_BT_HFP_LOG("addr=%02x%02x%02x%02x%02x%02x", hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  335. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  336. memcpy(ql_bt_hfp_remote_addr.addr, hfp_info->addr.addr, QL_BT_MAC_ADDRESS_SIZE);
  337. ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_CONNECTED;
  338. free(hfp_info);
  339. }
  340. }
  341. else
  342. {
  343. QL_BT_HFP_LOG("conn failed");
  344. goto QL_BT_HFP_STOP;
  345. }
  346. }
  347. break;
  348. case QUEC_BT_HFP_DISCONNECT_IND:
  349. {
  350. if (QL_BT_STATUS_SUCCESS == status)
  351. {
  352. QL_BT_HFP_LOG("disconn sucess");
  353. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  354. if (hfp_info)
  355. {
  356. //hfp_info point to a malloc addrss,need to free
  357. QL_BT_HFP_LOG("addr=%02x%02x%02x%02x%02x%02x", hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  358. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  359. free(hfp_info);
  360. #if 1
  361. if (ql_bt_hfp_connect_state == QL_BT_HFP_CONNECTION_STATE_DISCONNECTING)
  362. {
  363. ql_rtos_task_sleep_s(3);
  364. ret = ql_bt_hfp_demo_connect();
  365. if (ret != QL_BT_SUCCESS)
  366. {
  367. ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_DISCONNECTED;
  368. goto QL_BT_HFP_STOP;
  369. }
  370. else
  371. {
  372. ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_CONNECTING;
  373. }
  374. }
  375. #else
  376. ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_DISCONNECTED;
  377. #endif
  378. }
  379. }
  380. else
  381. {
  382. QL_BT_HFP_LOG("disconn failed");
  383. goto QL_BT_HFP_STOP;
  384. }
  385. }
  386. break;
  387. case QUEC_BT_HFP_CALL_IND:
  388. {
  389. if (QL_BT_STATUS_SUCCESS == status)
  390. {
  391. QL_BT_HFP_LOG("call sucess");
  392. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  393. if (hfp_info)
  394. {
  395. //hfp_info point to a malloc addrss,need to free
  396. ql_bt_hfp_call_state_e call_state = (ql_bt_hfp_call_state_e)hfp_info->state;
  397. QL_BT_HFP_LOG("call_state=%d,addr=%02x%02x%02x%02x%02x%02x", call_state, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  398. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  399. if (call_state == QL_BT_HFP_CALL_NO_CALL_IN_PROGRESS)
  400. {
  401. if (ql_bt_hfp_call == QL_BT_HFP_CALL_CALL_IN_PROGRESS)
  402. {
  403. ql_bt_hfp_call = call_state;
  404. if (ql_bt_hfp_connect_state == QL_BT_HFP_CONNECTION_STATE_CONNECTED)
  405. {
  406. ret = ql_bt_hfp_demo_disconnect();
  407. if (ret != QL_BT_SUCCESS)
  408. {
  409. goto QL_BT_HFP_STOP;
  410. }
  411. else
  412. {
  413. ql_bt_hfp_connect_state = QL_BT_HFP_CONNECTION_STATE_DISCONNECTING;
  414. }
  415. }
  416. }
  417. }
  418. else
  419. {
  420. if (ql_bt_hfp_call == QL_BT_HFP_CALL_NO_CALL_IN_PROGRESS)
  421. {
  422. ql_bt_hfp_call = call_state;
  423. ret = ql_bt_hfp_demo_set_volume();
  424. if (ret != QL_BT_SUCCESS)
  425. {
  426. goto QL_BT_HFP_STOP;
  427. }
  428. #if 0
  429. ql_rtos_task_sleep_ms(100);
  430. ret = ql_bt_hfp_demo_vr_disable();
  431. if (ret != QL_BT_SUCCESS)
  432. {
  433. goto QL_BT_HFP_STOP;
  434. }
  435. ql_rtos_task_sleep_ms(100);
  436. ret = ql_bt_hfp_demo_vr_enable();
  437. if (ret != QL_BT_SUCCESS)
  438. {
  439. goto QL_BT_HFP_STOP;
  440. }
  441. #endif
  442. //ql_rtos_task_sleep_ms(100);
  443. //ret = ql_bt_hfp_demo_three_way_call();
  444. //if (ret != QL_BT_SUCCESS)
  445. //{
  446. // goto QL_BT_HFP_STOP;
  447. //}
  448. ql_rtos_task_sleep_s(30);
  449. ret = ql_bt_hfp_demo_reject_call();
  450. if (ret != QL_BT_SUCCESS)
  451. {
  452. goto QL_BT_HFP_STOP;
  453. }
  454. }
  455. }
  456. free(hfp_info);
  457. }
  458. }
  459. else
  460. {
  461. QL_BT_HFP_LOG("call failed");
  462. goto QL_BT_HFP_STOP;
  463. }
  464. }
  465. break;
  466. case QUEC_BT_HFP_CALL_SETUP_IND:
  467. {
  468. if (QL_BT_STATUS_SUCCESS == status)
  469. {
  470. QL_BT_HFP_LOG("call set sucess");
  471. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  472. if (hfp_info)
  473. {
  474. //hfp_info point to a malloc addrss,need to free
  475. ql_bt_hfp_callsetup_state_e calls_state = (ql_bt_hfp_callsetup_state_e)hfp_info->state;
  476. QL_BT_HFP_LOG("calls_state=%d,addr=%02x%02x%02x%02x%02x%02x", calls_state, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  477. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  478. free(hfp_info);
  479. }
  480. }
  481. else
  482. {
  483. QL_BT_HFP_LOG("call set failed");
  484. goto QL_BT_HFP_STOP;
  485. }
  486. }
  487. break;
  488. case QUEC_BT_HFP_NETWORK_IND:
  489. {
  490. if (QL_BT_STATUS_SUCCESS == status)
  491. {
  492. QL_BT_HFP_LOG("network sucess");
  493. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  494. if (hfp_info)
  495. {
  496. //hfp_info point to a malloc addrss,need to free
  497. ql_bt_hfp_network_state_e network = (ql_bt_hfp_network_state_e)hfp_info->state;
  498. QL_BT_HFP_LOG("network=%d,addr=%02x%02x%02x%02x%02x%02x", network, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  499. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  500. free(hfp_info);
  501. }
  502. }
  503. else
  504. {
  505. QL_BT_HFP_LOG("network failed");
  506. goto QL_BT_HFP_STOP;
  507. }
  508. }
  509. break;
  510. case QUEC_BT_HFP_NETWORK_SIGNAL_IND:
  511. {
  512. if (QL_BT_STATUS_SUCCESS == status)
  513. {
  514. QL_BT_HFP_LOG("net signal sucess");
  515. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  516. if (hfp_info)
  517. {
  518. //hfp_info point to a malloc addrss,need to free
  519. unsigned char signal = (unsigned char)hfp_info->state;
  520. QL_BT_HFP_LOG("signal=%d,addr=%02x%02x%02x%02x%02x%02x", signal, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  521. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  522. free(hfp_info);
  523. }
  524. }
  525. else
  526. {
  527. QL_BT_HFP_LOG("net signal failed");
  528. goto QL_BT_HFP_STOP;
  529. }
  530. }
  531. break;
  532. case QUEC_BT_HFP_BATTERY_IND:
  533. {
  534. if (QL_BT_STATUS_SUCCESS == status)
  535. {
  536. QL_BT_HFP_LOG("battery sucess");
  537. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  538. if (hfp_info)
  539. {
  540. //hfp_info point to a malloc addrss,need to free
  541. int level = (int)hfp_info->state;
  542. QL_BT_HFP_LOG("level=%d,addr=%02x%02x%02x%02x%02x%02x", level, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  543. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  544. free(hfp_info);
  545. }
  546. }
  547. else
  548. {
  549. QL_BT_HFP_LOG("battery failed");
  550. goto QL_BT_HFP_STOP;
  551. }
  552. }
  553. break;
  554. case QUEC_BT_HFP_CALLHELD_IND:
  555. {
  556. if (QL_BT_STATUS_SUCCESS == status)
  557. {
  558. QL_BT_HFP_LOG("callheld sucess");
  559. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  560. if (hfp_info)
  561. {
  562. //hfp_info point to a malloc addrss,need to free
  563. ql_bt_hfp_callheld_state_e state = (ql_bt_hfp_callheld_state_e)hfp_info->state;
  564. QL_BT_HFP_LOG("state=%d,addr=%02x%02x%02x%02x%02x%02x", state, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  565. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  566. free(hfp_info);
  567. }
  568. }
  569. else
  570. {
  571. QL_BT_HFP_LOG("callheld failed");
  572. goto QL_BT_HFP_STOP;
  573. }
  574. }
  575. break;
  576. case QUEC_BT_HFP_AUDIO_IND:
  577. {
  578. if (QL_BT_STATUS_SUCCESS == status)
  579. {
  580. QL_BT_HFP_LOG("audio sucess");
  581. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  582. if (hfp_info)
  583. {
  584. //hfp_info point to a malloc addrss,need to free
  585. ql_bt_hfp_audio_state_e state = (ql_bt_hfp_audio_state_e)hfp_info->state;
  586. QL_BT_HFP_LOG("state=%d,addr=%02x%02x%02x%02x%02x%02x", state, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  587. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  588. free(hfp_info);
  589. }
  590. }
  591. else
  592. {
  593. QL_BT_HFP_LOG("audio failed");
  594. goto QL_BT_HFP_STOP;
  595. }
  596. }
  597. break;
  598. case QUEC_BT_HFP_VOLUME_IND:
  599. {
  600. if (QL_BT_STATUS_SUCCESS == status)
  601. {
  602. QL_BT_HFP_LOG("volume sucess");
  603. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  604. if (hfp_info)
  605. {
  606. //hfp_info point to a malloc addrss,need to free
  607. ql_bt_hfp_volume_type_e type = (ql_bt_hfp_volume_type_e)hfp_info->state;
  608. unsigned char volume = test_event.param3;
  609. QL_BT_HFP_LOG("type=%d,volume", type, volume);
  610. free(hfp_info);
  611. }
  612. }
  613. else
  614. {
  615. QL_BT_HFP_LOG("volume failed");
  616. goto QL_BT_HFP_STOP;
  617. }
  618. }
  619. break;
  620. case QUEC_BT_HFP_NETWORK_TYPE_IND:
  621. {
  622. if (QL_BT_STATUS_SUCCESS == status)
  623. {
  624. QL_BT_HFP_LOG("net type sucess");
  625. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  626. if (hfp_info)
  627. {
  628. //hfp_info point to a malloc addrss,need to free
  629. ql_bt_hfp_service_type_e type = (ql_bt_hfp_service_type_e)hfp_info->state;
  630. QL_BT_HFP_LOG("type=%d,addr=%02x%02x%02x%02x%02x%02x", type, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  631. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  632. free(hfp_info);
  633. }
  634. }
  635. else
  636. {
  637. QL_BT_HFP_LOG("net type failed");
  638. goto QL_BT_HFP_STOP;
  639. }
  640. }
  641. break;
  642. case QUEC_BT_HFP_CODEC_IND:
  643. {
  644. if (QL_BT_STATUS_SUCCESS == status)
  645. {
  646. QL_BT_HFP_LOG("codec sucess");
  647. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  648. if (hfp_info)
  649. {
  650. //hfp_info point to a malloc addrss,need to free
  651. ql_bt_hfp_codec_type_e type = (ql_bt_hfp_codec_type_e)hfp_info->state;
  652. QL_BT_HFP_LOG("type=%d,addr=%02x%02x%02x%02x%02x%02x", type, hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  653. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  654. free(hfp_info);
  655. }
  656. }
  657. else
  658. {
  659. QL_BT_HFP_LOG("codec failed");
  660. goto QL_BT_HFP_STOP;
  661. }
  662. }
  663. break;
  664. case QUEC_BT_HFP_RING_IND:
  665. {
  666. if (QL_BT_STATUS_SUCCESS == status)
  667. {
  668. QL_BT_HFP_LOG("ring sucess");
  669. ql_bt_hfp_event_info_t *hfp_info = (ql_bt_hfp_event_info_t *)test_event.param2;
  670. if (hfp_info)
  671. {
  672. //hfp_info point to a malloc addrss,need to free
  673. QL_BT_HFP_LOG("addr=%02x%02x%02x%02x%02x%02x", hfp_info->addr.addr[0], hfp_info->addr.addr[1], \
  674. hfp_info->addr.addr[2], hfp_info->addr.addr[3], hfp_info->addr.addr[4], hfp_info->addr.addr[5]);
  675. free(hfp_info);
  676. }
  677. ret = ql_bt_hfp_demo_answer_accept_call();
  678. //ret = ql_bt_hfp_demo_reject_call();
  679. if (ret != QL_BT_SUCCESS)
  680. {
  681. goto QL_BT_HFP_STOP;
  682. }
  683. }
  684. else
  685. {
  686. QL_BT_HFP_LOG("ring failed");
  687. goto QL_BT_HFP_STOP;
  688. }
  689. }
  690. break;
  691. default:
  692. break;
  693. }
  694. return ret;
  695. QL_BT_HFP_STOP:
  696. ret = ql_bt_demo_stop();
  697. }
  698. return ret;
  699. }
  700. void ql_bt_hfp_task_pthread(void *ctx)
  701. {
  702. QlOSStatus err = 0;
  703. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  704. ret = ql_bt_hfp_init(ql_bt_hfp_notify_cb);
  705. if (ret != QL_BT_SUCCESS)
  706. {
  707. goto QL_BT_HFP_NOT_INIT_EXIT;
  708. }
  709. ret = ql_bt_demo_start();
  710. if (ret != QL_BT_SUCCESS)
  711. {
  712. goto QL_BT_HFP_INIT_EXIT;
  713. }
  714. while(1)
  715. {
  716. ret = ql_bt_hfp_event();
  717. if (ret != QL_BT_SUCCESS)
  718. {
  719. break;
  720. }
  721. }
  722. QL_BT_HFP_INIT_EXIT:
  723. ql_bt_hfp_release();
  724. QL_BT_HFP_NOT_INIT_EXIT:
  725. err = ql_rtos_task_delete(NULL);
  726. if(err != QL_OSI_SUCCESS)
  727. {
  728. QL_BT_HFP_LOG("task deleted failed");
  729. }
  730. }
  731. QlOSStatus ql_bt_hfp_demo_init(void)
  732. {
  733. QlOSStatus err = QL_OSI_SUCCESS;
  734. QL_BT_HFP_LOG("enter ql_bt_hfp_demo_init");
  735. err = ql_rtos_task_create(&bt_hfp_demo_task, BT_BLE_DEMO_TASK_STACK_SIZE, BT_BLE_DEMO_TASK_PRIO, "ql_bt_hfp", ql_bt_hfp_task_pthread, NULL, BT_BLE_DEMO_TASK_EVENT_CNT);
  736. return err;
  737. }