bt_spp_demo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_bt.h"
  17. #include "ql_api_bt_spp.h"
  18. #include "ql_log.h"
  19. #include "bt_spp_demo.h"
  20. #define QL_BT_SPP_LOG_LEVEL QL_LOG_LEVEL_INFO
  21. #define QL_BT_SPP_LOG(msg, ...) QL_LOG(QL_BT_SPP_LOG_LEVEL, "ql_bt_spp_DEMO", msg, ##__VA_ARGS__)
  22. #define QL_BT_SPP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_bt_spp_DEMO", msg, ##__VA_ARGS__)
  23. #define QL_BT_DEMO_ADDR_MAX_SIZE 17
  24. ql_task_t bt_demo_task = NULL;
  25. ql_bt_inquiry_type_e bt_inquiry_type = QL_BT_SERVICE_ALL;
  26. ql_bt_addr_s bt_connection_addr = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x38}};
  27. extern ql_errcode_bt_e ql_bt_demo_start();
  28. extern ql_errcode_bt_e ql_bt_demo_stop();
  29. extern ql_errcode_bt_e ql_bt_demo_get_state();
  30. extern ql_errcode_bt_e ql_bt_demo_get_local_name();
  31. extern ql_errcode_bt_e ql_bt_demo_set_local_name();
  32. extern ql_errcode_bt_e ql_bt_demo_get_scanmde();
  33. extern ql_errcode_bt_e ql_be_demo_set_scanmode();
  34. ql_errcode_bt_e ql_bt_demo_start_inquiry()
  35. {
  36. ql_errcode_bt_e ret;
  37. ret = ql_classic_bt_start_inquiry(bt_inquiry_type);
  38. if (ret == QL_BT_SUCCESS)
  39. {
  40. QL_BT_SPP_LOG("sucess");
  41. }
  42. else
  43. {
  44. QL_BT_SPP_LOG("error=%x", ret);
  45. }
  46. return ret;
  47. }
  48. ql_errcode_bt_e ql_bt_demo_cancel_inquiry()
  49. {
  50. ql_errcode_bt_e ret;
  51. ret = ql_classic_bt_cancel_inquiry();
  52. if (ret == QL_BT_SUCCESS)
  53. {
  54. QL_BT_SPP_LOG("sucess");
  55. }
  56. else
  57. {
  58. QL_BT_SPP_LOG("error=%x", ret);
  59. }
  60. return ret;
  61. }
  62. static ql_errcode_bt_e ql_bt_demo_connect()
  63. {
  64. ql_errcode_bt_e ret;
  65. ql_bt_addr_s connect_info;
  66. connect_info.addr[0] = 0x66;
  67. connect_info.addr[1] = 0xCA;
  68. connect_info.addr[2] = 0xC9;
  69. connect_info.addr[3] = 0xA2;
  70. connect_info.addr[4] = 0x3E;
  71. connect_info.addr[5] = 0x38;
  72. ret = ql_bt_spp_connect(connect_info);
  73. if (ret == QL_BT_SUCCESS)
  74. {
  75. QL_BT_SPP_LOG("sucess");
  76. }
  77. else
  78. {
  79. QL_BT_SPP_LOG("error=%x", ret);
  80. }
  81. return ret;
  82. }
  83. static ql_errcode_bt_e ql_bt_demo_disconnect()
  84. {
  85. ql_errcode_bt_e ret;
  86. ret = ql_bt_spp_disconnect();
  87. if (ret == QL_BT_SUCCESS)
  88. {
  89. QL_BT_SPP_LOG("sucess");
  90. }
  91. else
  92. {
  93. QL_BT_SPP_LOG("error=%x", ret);
  94. }
  95. return ret;
  96. }
  97. static ql_errcode_bt_e ql_bt_demo_spp_send_data()
  98. {
  99. ql_errcode_bt_e ret;
  100. uint8 spp_info[10]="123456789";
  101. ret = ql_bt_spp_data_send(spp_info,9);
  102. if (ret == QL_BT_SUCCESS)
  103. {
  104. QL_BT_SPP_LOG("sucess");
  105. }
  106. else
  107. {
  108. QL_BT_SPP_LOG("error=%x", ret);
  109. }
  110. return ret;
  111. }
  112. void ql_bt_notify_cb(void *ind_msg_buf, void *ctx)
  113. {
  114. ql_event_t *ql_event = NULL;
  115. if (ind_msg_buf == NULL)
  116. {
  117. return ;
  118. }
  119. ql_event = (ql_event_t *)ind_msg_buf;
  120. if (ql_event->id != 0)
  121. {
  122. ql_rtos_event_send(bt_demo_task,ql_event);
  123. }
  124. }
  125. static ql_errcode_bt_e ql_bt_spp_event()
  126. {
  127. ql_event_t test_event = {0};
  128. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  129. QL_BT_STATUS status;
  130. if (ql_event_try_wait(&test_event) == 0)
  131. {
  132. if (test_event.id == 0)
  133. {
  134. return ret;
  135. }
  136. status = (QL_BT_STATUS)(test_event.param1);
  137. switch (test_event.id)
  138. {
  139. case QUEC_BT_START_STATUS_IND:
  140. {
  141. if (QL_BT_STATUS_SUCCESS == status)
  142. {
  143. QL_BT_SPP_LOG("start sucess");
  144. ret = ql_bt_demo_get_state();
  145. if (ret == QL_BT_SUCCESS)
  146. {
  147. ql_be_demo_set_scanmode();
  148. ret = ql_bt_demo_set_local_name();
  149. if (ret != QL_BT_SUCCESS)
  150. {
  151. goto QL_BT_SPP_CLIENT_STOP;
  152. }
  153. else
  154. {
  155. ql_bt_demo_start_inquiry();
  156. }
  157. }
  158. else
  159. {
  160. goto QL_BT_SPP_CLIENT_STOP;
  161. }
  162. }
  163. else
  164. {
  165. QL_BT_SPP_LOG("start failed");
  166. }
  167. }
  168. break;
  169. case QUEC_BT_STOP_STATUS_IND:
  170. {
  171. if (QL_BT_STATUS_SUCCESS == status)
  172. {
  173. QL_BT_SPP_LOG("stop sucess");
  174. }
  175. else
  176. {
  177. QL_BT_SPP_LOG("stop failed");
  178. }
  179. }
  180. break;
  181. case QUEC_BT_INQUIRY_IND:
  182. {
  183. if (QL_BT_STATUS_SUCCESS == status)
  184. {
  185. QL_BT_SPP_LOG("inqury sucess");
  186. ql_bt_info_s *pdev_info = NULL;
  187. unsigned char addr_string[QL_BT_DEMO_ADDR_MAX_SIZE + 1] = {0};
  188. unsigned char str_ptr[256];
  189. void *pData = (void *)(test_event.param2);
  190. if (pData == NULL)
  191. {
  192. QL_BT_SPP_LOG("inqury pData is NULL");
  193. break;
  194. }
  195. pdev_info = (ql_bt_info_s *)pData;
  196. memset(addr_string,0x00,sizeof(addr_string));
  197. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", pdev_info->addr.addr[0], pdev_info->addr.addr[1], pdev_info->addr.addr[2], pdev_info->addr.addr[3], pdev_info->addr.addr[4], pdev_info->addr.addr[5]);
  198. memset(str_ptr,0x00,sizeof(str_ptr));
  199. sprintf((char *)str_ptr, "%s%s,%s,%x", "INQ:", addr_string, (char *)pdev_info->name, pdev_info->dev_class);
  200. QL_BT_SPP_LOG("inqury str_ptr=%s",str_ptr);
  201. }
  202. else
  203. {
  204. QL_BT_SPP_LOG("inqury failed");
  205. goto QL_BT_SPP_CLIENT_STOP;
  206. }
  207. }
  208. break;
  209. case QUEC_BT_INQUIRY_END_IND:
  210. {
  211. if (QL_BT_STATUS_SUCCESS == status)
  212. {
  213. QL_BT_SPP_LOG("inqury end sucess");
  214. ql_bt_demo_cancel_inquiry();
  215. ql_bt_demo_connect();
  216. }
  217. else
  218. {
  219. QL_BT_SPP_LOG("inqury end failed");
  220. goto QL_BT_SPP_CLIENT_STOP;
  221. }
  222. }
  223. break;
  224. case QUEC_BT_SPP_CONNECT_IND:
  225. {
  226. if (QL_BT_STATUS_SUCCESS == status)
  227. {
  228. ret = ql_bt_demo_spp_send_data();
  229. if (ret != QL_BT_SUCCESS)
  230. {
  231. goto QL_BT_SPP_CLIENT_STOP;
  232. }
  233. }
  234. else
  235. {
  236. QL_BT_SPP_LOG("connect failed");
  237. goto QL_BT_SPP_CLIENT_STOP;
  238. }
  239. }
  240. break;
  241. case QUEC_BT_SPP_REVDATA_IND:
  242. {
  243. if (QL_BT_STATUS_SUCCESS == status)
  244. {
  245. QL_BT_SPP_LOG("spp recv data sucess");
  246. void *pData = (void *)(test_event.param2);
  247. if (pData == NULL)
  248. {
  249. QL_BT_SPP_LOG("inqury pData is NULL");
  250. break;
  251. }
  252. ret = ql_bt_demo_spp_send_data();
  253. if (ret != QL_BT_SUCCESS)
  254. {
  255. goto QL_BT_SPP_CLIENT_STOP;
  256. }
  257. ql_bt_demo_disconnect();
  258. }
  259. else
  260. {
  261. QL_BT_SPP_LOG("spp send data failed");
  262. goto QL_BT_SPP_CLIENT_STOP;
  263. }
  264. }
  265. break;
  266. case QUEC_BT_SPP_DISCONNECT_IND:
  267. {
  268. if (QL_BT_STATUS_SUCCESS == status)
  269. {
  270. QL_BT_SPP_LOG("disconnect sucess");
  271. goto QL_BT_SPP_CLIENT_STOP;
  272. }
  273. else
  274. {
  275. QL_BT_SPP_LOG("disconnect failed");
  276. goto QL_BT_SPP_CLIENT_STOP;
  277. }
  278. }
  279. break;
  280. default:
  281. break;
  282. }
  283. return ret;
  284. QL_BT_SPP_CLIENT_STOP:
  285. ql_bt_demo_stop();
  286. }
  287. return ret;
  288. }
  289. void ql_bt_spp_demo_task_pthread(void *ctx)
  290. {
  291. QlOSStatus err = 0;
  292. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  293. ret = ql_bt_spp_init(ql_bt_notify_cb);
  294. if (ret != QL_BT_SUCCESS)
  295. {
  296. goto QL_BT_SPP_SERVER_NOT_INIT_EIXT;
  297. }
  298. ret = ql_bt_demo_start();
  299. if (ret != QL_BT_SUCCESS)
  300. {
  301. goto QL_BT_SPP_SERVER_INIT_EIXT;
  302. }
  303. while(1)
  304. {
  305. ql_bt_spp_event();
  306. //if (ret != QL_BT_SUCCESS)
  307. //{
  308. // break;
  309. //}
  310. }
  311. QL_BT_SPP_SERVER_INIT_EIXT:
  312. ql_bt_spp_release();
  313. QL_BT_SPP_SERVER_NOT_INIT_EIXT:
  314. err = ql_rtos_task_delete(NULL);
  315. if(err != QL_OSI_SUCCESS)
  316. {
  317. QL_BT_SPP_LOG("task deleted failed");
  318. }
  319. }
  320. QlOSStatus ql_bt_spp_demo_init(void)
  321. {
  322. QlOSStatus err = QL_OSI_SUCCESS;
  323. QL_BT_SPP_LOG("enter ql_bt_spp_demo_init");
  324. err = ql_rtos_task_create(&bt_demo_task, BT_BLE_DEMO_TASK_STACK_SIZE, BT_BLE_DEMO_TASK_PRIO, "ql_spp_demo", ql_bt_spp_demo_task_pthread, NULL, BT_BLE_DEMO_TASK_EVENT_CNT);
  325. return err;
  326. }