bt_manager_pbap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 PBAP 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 <sys_event.h>
  18. #include <bt_manager.h>
  19. #include "bt_manager_inner.h"
  20. #include "btservice_api.h"
  21. #define BTMGR_MAX_PBAP_NUM 2
  22. #define MGR_PBAP_INDEX_TO_APPID(x) ((x)|0x80)
  23. #define MGR_PBAP_APPID_TO_INDEX(x) ((x)&(~0x80))
  24. struct btmgr_pbap_info {
  25. uint8_t app_id;
  26. struct btmgr_pbap_cb *cb;
  27. };
  28. static struct btmgr_pbap_info mgr_pbap_info[BTMGR_MAX_PBAP_NUM];
  29. static void *btmgr_pbap_find_free_info(void)
  30. {
  31. int i;
  32. for (i = 0; i < BTMGR_MAX_PBAP_NUM; i++) {
  33. if (mgr_pbap_info[i].app_id == 0) {
  34. mgr_pbap_info[i].app_id = MGR_PBAP_INDEX_TO_APPID(i);
  35. return &mgr_pbap_info[i];
  36. }
  37. }
  38. return NULL;
  39. }
  40. static void btmgr_pbap_free_info(struct btmgr_pbap_info *info)
  41. {
  42. memset(info, 0, sizeof(struct btmgr_pbap_info));
  43. }
  44. static void *btmgr_pbap_find_info_by_app_id(uint8_t app_id)
  45. {
  46. int i;
  47. for (i = 0; i < BTMGR_MAX_PBAP_NUM; i++) {
  48. if (mgr_pbap_info[i].app_id == app_id) {
  49. return &mgr_pbap_info[i];
  50. }
  51. }
  52. return NULL;
  53. }
  54. static void btmgr_pbap_callback(btsrv_pbap_event_e event, uint8_t app_id, void *data, uint8_t size)
  55. {
  56. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  57. uint16_t *max_size;
  58. if (!info) {
  59. return;
  60. }
  61. switch (event) {
  62. case BTSRV_PBAP_CONNECT_FAILED:
  63. if (info->cb->connect_failed) {
  64. info->cb->connect_failed(app_id);
  65. }
  66. btmgr_pbap_free_info(info);
  67. break;
  68. case BTSRV_PBAP_CONNECTED:
  69. if (info->cb->connected) {
  70. info->cb->connected(app_id);
  71. }
  72. break;
  73. case BTSRV_PBAP_DISCONNECTED:
  74. if (info->cb->disconnected) {
  75. info->cb->disconnected(app_id);
  76. }
  77. btmgr_pbap_free_info(info);
  78. break;
  79. case BTSRV_PBAP_MAX_SIZE:
  80. if (info->cb->max_size) {
  81. max_size = data;
  82. info->cb->max_size(app_id, (*max_size));
  83. }
  84. break;
  85. case BTSRV_PBAP_VCARD_RESULT:
  86. if (info->cb->result) {
  87. info->cb->result(app_id, data, size);
  88. }
  89. break;
  90. case BTSRV_PBAP_SETPATH_FINISH:
  91. if (info->cb->setpath_finish) {
  92. info->cb->setpath_finish(app_id);
  93. }
  94. break;
  95. case BTSRV_PBAP_SEARCH_RESULT:
  96. if (info->cb->search_result) {
  97. info->cb->search_result(app_id, data, size);
  98. }
  99. break;
  100. case BTSRV_PBAP_GET_VCARD_FINISH:
  101. if (info->cb->get_vcard_finish) {
  102. info->cb->get_vcard_finish(app_id);
  103. }
  104. break;
  105. case BTSRV_PBAP_END_OF_BODY:
  106. if (info->cb->end_of_body) {
  107. info->cb->end_of_body(app_id);
  108. }
  109. break;
  110. case BTSRV_PBAP_ABORT_FINISH:
  111. if (info->cb->abort) {
  112. info->cb->abort(app_id);
  113. }
  114. break;
  115. }
  116. }
  117. uint8_t btmgr_pbap_connect(bd_address_t *bd, struct btmgr_pbap_cb *cb)
  118. {
  119. int ret;
  120. struct btmgr_pbap_info *info;
  121. struct bt_pbap_op_param param;
  122. if (!bd || !cb) {
  123. return 0;
  124. }
  125. info = btmgr_pbap_find_free_info();
  126. if (!info) {
  127. return 0;
  128. }
  129. info->cb = cb;
  130. memcpy(&param.bd, bd, sizeof(bd_address_t));
  131. param.cmd = BTSRV_PBAP_OP_CMD_CONNECT;
  132. param.app_id = info->app_id;
  133. param.cb = &btmgr_pbap_callback;
  134. ret = btif_pbap_cmd_op(&param);
  135. if (ret) {
  136. btmgr_pbap_free_info(info);
  137. return 0;
  138. }
  139. return info->app_id;
  140. }
  141. int btmgr_pbap_disconnect(uint8_t app_id)
  142. {
  143. struct bt_pbap_op_param param;
  144. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  145. if (!info) {
  146. return -EIO;
  147. }
  148. param.cmd = BTSRV_PBAP_OP_CMD_DISCONNECT;
  149. param.app_id = info->app_id;
  150. return btif_pbap_cmd_op(&param);
  151. }
  152. int btmgr_pbap_get_size(uint8_t app_id, char *path)
  153. {
  154. struct bt_pbap_op_param param;
  155. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  156. if (!path || !info) {
  157. return -EIO;
  158. }
  159. param.cmd = BTSRV_PBAP_OP_CMD_GET_SIZE;
  160. param.pb_path = path;
  161. param.app_id = info->app_id;
  162. return btif_pbap_cmd_op(&param);
  163. }
  164. int btmgr_pbap_get_pb(uint8_t app_id, char *path, uint32_t filter)
  165. {
  166. struct bt_pbap_op_param param;
  167. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  168. if (!path || !info) {
  169. return -EIO;
  170. }
  171. param.cmd = BTSRV_PBAP_OP_CMD_GET_PB;
  172. param.pb_path = path;
  173. param.pb_filter = filter;
  174. param.app_id = info->app_id;
  175. return btif_pbap_cmd_op(&param);
  176. }
  177. int btmgr_pbap_setpath(uint8_t app_id, char *path)
  178. {
  179. struct bt_pbap_op_param param;
  180. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  181. if (!path || !info) {
  182. return -EIO;
  183. }
  184. param.cmd = BTSRV_PBAP_OP_CMD_SET_PATH;
  185. param.pb_path = path;
  186. param.app_id = info->app_id;
  187. return btif_pbap_cmd_op(&param);
  188. }
  189. int btmgr_pbap_get_vcard(uint8_t app_id, char *name, uint8_t len, uint32_t filter)
  190. {
  191. struct bt_pbap_op_param param;
  192. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  193. if (!name || !info || (len > sizeof(param.vcard_name))) {
  194. return -EIO;
  195. }
  196. param.cmd = BTSRV_PBAP_OP_CMD_GET_VCARD;
  197. param.app_id = info->app_id;
  198. param.vcard_name_len = len;
  199. param.vc_filter = filter;
  200. if (len) {
  201. memcpy(param.vcard_name, name, len);
  202. }
  203. return btif_pbap_cmd_op(&param);
  204. }
  205. int btmgr_pbap_get_vcard_continue(uint8_t app_id)
  206. {
  207. struct bt_pbap_op_param param;
  208. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  209. if (!info) {
  210. return -EIO;
  211. }
  212. param.cmd = BTSRV_PBAP_OP_CMD_GET_CONTINUE;
  213. param.app_id = info->app_id;
  214. return btif_pbap_cmd_op(&param);
  215. }
  216. int btmgr_pbap_listing(uint8_t app_id, uint8_t order, uint8_t attr, char *value, uint8_t len)
  217. {
  218. struct bt_pbap_op_param param;
  219. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  220. if (!info || (len > sizeof(param.search_value))) {
  221. return -EIO;
  222. }
  223. param.cmd = BTSRV_PBAP_OP_CMD_LISTING;
  224. param.app_id = info->app_id;
  225. param.order = order;
  226. param.search_attr = attr;
  227. param.search_len = len;
  228. if (len) {
  229. memcpy(param.search_value, value, len);
  230. }
  231. return btif_pbap_cmd_op(&param);
  232. }
  233. int btmgr_pbap_abort(uint8_t app_id)
  234. {
  235. struct bt_pbap_op_param param;
  236. struct btmgr_pbap_info *info = btmgr_pbap_find_info_by_app_id(app_id);
  237. if (!info) {
  238. return -EIO;
  239. }
  240. param.cmd = BTSRV_PBAP_OP_CMD_ABORT;
  241. param.app_id = info->app_id;
  242. return btif_pbap_cmd_op(&param);
  243. }