bluetooth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX Client
  5. *
  6. * Copyright (C) 2012 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdlib.h>
  14. #include <errno.h>
  15. #include <inttypes.h>
  16. #include <glib.h>
  17. #include "lib/bluetooth.h"
  18. #include "lib/rfcomm.h"
  19. #include "lib/sdp.h"
  20. #include "lib/sdp_lib.h"
  21. #include "gdbus/gdbus.h"
  22. #include "btio/btio.h"
  23. #include "obexd/src/log.h"
  24. #include "transport.h"
  25. #include "bluetooth.h"
  26. #define BT_RX_MTU 32767
  27. #define BT_TX_MTU 32767
  28. #define OBC_BT_ERROR obc_bt_error_quark()
  29. struct bluetooth_session {
  30. guint id;
  31. bdaddr_t src;
  32. bdaddr_t dst;
  33. uint16_t port;
  34. sdp_session_t *sdp;
  35. sdp_record_t *sdp_record;
  36. GIOChannel *io;
  37. char *service;
  38. obc_transport_func func;
  39. void *user_data;
  40. };
  41. static GSList *sessions = NULL;
  42. static GQuark obc_bt_error_quark(void)
  43. {
  44. return g_quark_from_static_string("obc-bluetooth-error-quark");
  45. }
  46. static void session_destroy(struct bluetooth_session *session)
  47. {
  48. DBG("%p", session);
  49. if (g_slist_find(sessions, session) == NULL)
  50. return;
  51. sessions = g_slist_remove(sessions, session);
  52. if (session->io != NULL) {
  53. g_io_channel_shutdown(session->io, TRUE, NULL);
  54. g_io_channel_unref(session->io);
  55. }
  56. if (session->sdp)
  57. sdp_close(session->sdp);
  58. if (session->sdp_record)
  59. sdp_record_free(session->sdp_record);
  60. g_free(session->service);
  61. g_free(session);
  62. }
  63. static void transport_callback(GIOChannel *io, GError *err, gpointer user_data)
  64. {
  65. struct bluetooth_session *session = user_data;
  66. DBG("");
  67. if (session->func)
  68. session->func(io, err, session->user_data);
  69. if (err != NULL)
  70. session_destroy(session);
  71. }
  72. static GIOChannel *transport_connect(const bdaddr_t *src, const bdaddr_t *dst,
  73. uint16_t port, BtIOConnect function,
  74. gpointer user_data)
  75. {
  76. GIOChannel *io;
  77. GError *err = NULL;
  78. DBG("port %u", port);
  79. if (port > 31) {
  80. io = bt_io_connect(function, user_data,
  81. NULL, &err,
  82. BT_IO_OPT_SOURCE_BDADDR, src,
  83. BT_IO_OPT_DEST_BDADDR, dst,
  84. BT_IO_OPT_PSM, port,
  85. BT_IO_OPT_MODE, BT_IO_MODE_ERTM,
  86. BT_IO_OPT_OMTU, BT_TX_MTU,
  87. BT_IO_OPT_IMTU, BT_RX_MTU,
  88. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  89. BT_IO_OPT_INVALID);
  90. } else {
  91. io = bt_io_connect(function, user_data,
  92. NULL, &err,
  93. BT_IO_OPT_SOURCE_BDADDR, src,
  94. BT_IO_OPT_DEST_BDADDR, dst,
  95. BT_IO_OPT_CHANNEL, port,
  96. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  97. BT_IO_OPT_INVALID);
  98. }
  99. if (io != NULL)
  100. return io;
  101. error("%s", err->message);
  102. g_error_free(err);
  103. return NULL;
  104. }
  105. static void search_callback(uint8_t type, uint16_t status,
  106. uint8_t *rsp, size_t size, void *user_data)
  107. {
  108. struct bluetooth_session *session = user_data;
  109. unsigned int scanned, bytesleft = size;
  110. int seqlen = 0;
  111. uint8_t dataType;
  112. uint16_t port = 0;
  113. GError *gerr = NULL;
  114. if (status || type != SDP_SVC_SEARCH_ATTR_RSP)
  115. goto failed;
  116. scanned = sdp_extract_seqtype(rsp, bytesleft, &dataType, &seqlen);
  117. if (!scanned || !seqlen)
  118. goto failed;
  119. rsp += scanned;
  120. bytesleft -= scanned;
  121. do {
  122. sdp_record_t *rec;
  123. sdp_list_t *protos;
  124. sdp_data_t *data;
  125. int recsize, ch = -1;
  126. recsize = 0;
  127. rec = sdp_extract_pdu(rsp, bytesleft, &recsize);
  128. if (!rec)
  129. break;
  130. if (!recsize) {
  131. sdp_record_free(rec);
  132. break;
  133. }
  134. if (!sdp_get_access_protos(rec, &protos)) {
  135. ch = sdp_get_proto_port(protos, RFCOMM_UUID);
  136. sdp_list_foreach(protos,
  137. (sdp_list_func_t) sdp_list_free, NULL);
  138. sdp_list_free(protos, NULL);
  139. protos = NULL;
  140. }
  141. data = sdp_data_get(rec, 0x0200);
  142. /* PSM must be odd and lsb of upper byte must be 0 */
  143. if (data != NULL && (data->val.uint16 & 0x0101) == 0x0001)
  144. ch = data->val.uint16;
  145. /* Cache the sdp record associated with the service that we
  146. * attempt to connect. This allows reading its application
  147. * specific service attributes. */
  148. if (ch > 0) {
  149. port = ch;
  150. session->sdp_record = rec;
  151. break;
  152. }
  153. sdp_record_free(rec);
  154. scanned += recsize;
  155. rsp += recsize;
  156. bytesleft -= recsize;
  157. } while (scanned < size && bytesleft > 0);
  158. if (port == 0)
  159. goto failed;
  160. session->port = port;
  161. g_io_channel_set_close_on_unref(session->io, FALSE);
  162. g_io_channel_unref(session->io);
  163. session->io = transport_connect(&session->src, &session->dst, port,
  164. transport_callback, session);
  165. if (session->io != NULL) {
  166. sdp_close(session->sdp);
  167. session->sdp = NULL;
  168. return;
  169. }
  170. failed:
  171. if (session->io != NULL) {
  172. g_io_channel_shutdown(session->io, TRUE, NULL);
  173. g_io_channel_unref(session->io);
  174. session->io = NULL;
  175. }
  176. g_set_error(&gerr, OBC_BT_ERROR, -EIO,
  177. "Unable to find service record");
  178. if (session->func)
  179. session->func(session->io, gerr, session->user_data);
  180. g_clear_error(&gerr);
  181. session_destroy(session);
  182. }
  183. static gboolean process_callback(GIOChannel *io, GIOCondition cond,
  184. gpointer user_data)
  185. {
  186. struct bluetooth_session *session = user_data;
  187. if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
  188. return FALSE;
  189. if (sdp_process(session->sdp) < 0)
  190. return FALSE;
  191. return TRUE;
  192. }
  193. static int bt_string2uuid(uuid_t *uuid, const char *string)
  194. {
  195. uint32_t data0, data4;
  196. uint16_t data1, data2, data3, data5;
  197. if (sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
  198. &data0, &data1, &data2, &data3, &data4, &data5) == 6) {
  199. uint8_t val[16];
  200. data0 = g_htonl(data0);
  201. data1 = g_htons(data1);
  202. data2 = g_htons(data2);
  203. data3 = g_htons(data3);
  204. data4 = g_htonl(data4);
  205. data5 = g_htons(data5);
  206. memcpy(&val[0], &data0, 4);
  207. memcpy(&val[4], &data1, 2);
  208. memcpy(&val[6], &data2, 2);
  209. memcpy(&val[8], &data3, 2);
  210. memcpy(&val[10], &data4, 4);
  211. memcpy(&val[14], &data5, 2);
  212. sdp_uuid128_create(uuid, val);
  213. return 0;
  214. }
  215. return -EINVAL;
  216. }
  217. static gboolean service_callback(GIOChannel *io, GIOCondition cond,
  218. gpointer user_data)
  219. {
  220. struct bluetooth_session *session = user_data;
  221. sdp_list_t *search, *attrid;
  222. uint32_t range = 0x0000ffff;
  223. GError *gerr = NULL;
  224. uuid_t uuid;
  225. if (cond & G_IO_NVAL)
  226. return FALSE;
  227. if (cond & G_IO_ERR)
  228. goto failed;
  229. if (sdp_set_notify(session->sdp, search_callback, session) < 0)
  230. goto failed;
  231. if (bt_string2uuid(&uuid, session->service) < 0)
  232. goto failed;
  233. sdp_uuid128_to_uuid(&uuid);
  234. search = sdp_list_append(NULL, &uuid);
  235. attrid = sdp_list_append(NULL, &range);
  236. if (sdp_service_search_attr_async(session->sdp,
  237. search, SDP_ATTR_REQ_RANGE, attrid) < 0) {
  238. sdp_list_free(attrid, NULL);
  239. sdp_list_free(search, NULL);
  240. goto failed;
  241. }
  242. sdp_list_free(attrid, NULL);
  243. sdp_list_free(search, NULL);
  244. g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  245. process_callback, session);
  246. return FALSE;
  247. failed:
  248. g_io_channel_shutdown(session->io, TRUE, NULL);
  249. g_io_channel_unref(session->io);
  250. session->io = NULL;
  251. g_set_error(&gerr, OBC_BT_ERROR, -EIO,
  252. "Unable to find service record");
  253. if (session->func)
  254. session->func(session->io, gerr, session->user_data);
  255. g_clear_error(&gerr);
  256. session_destroy(session);
  257. return FALSE;
  258. }
  259. static sdp_session_t *service_connect(const bdaddr_t *src, const bdaddr_t *dst,
  260. GIOFunc function, gpointer user_data)
  261. {
  262. struct bluetooth_session *session = user_data;
  263. sdp_session_t *sdp;
  264. GIOChannel *io;
  265. DBG("");
  266. sdp = sdp_connect(src, dst, SDP_NON_BLOCKING);
  267. if (sdp == NULL)
  268. return NULL;
  269. io = g_io_channel_unix_new(sdp_get_socket(sdp));
  270. if (io == NULL) {
  271. sdp_close(sdp);
  272. return NULL;
  273. }
  274. g_io_add_watch(io, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  275. function, user_data);
  276. session->io = io;
  277. return sdp;
  278. }
  279. static int session_connect(struct bluetooth_session *session)
  280. {
  281. int err;
  282. DBG("session %p", session);
  283. if (session->port > 0) {
  284. session->io = transport_connect(&session->src, &session->dst,
  285. session->port,
  286. transport_callback,
  287. session);
  288. err = (session->io == NULL) ? -EINVAL : 0;
  289. } else {
  290. session->sdp = service_connect(&session->src, &session->dst,
  291. service_callback, session);
  292. err = (session->sdp == NULL) ? -ENOMEM : 0;
  293. }
  294. return err;
  295. }
  296. static guint bluetooth_connect(const char *source, const char *destination,
  297. const char *service, uint16_t port,
  298. obc_transport_func func, void *user_data)
  299. {
  300. struct bluetooth_session *session;
  301. static guint id = 0;
  302. DBG("src %s dest %s service %s port %u",
  303. source, destination, service, port);
  304. if (destination == NULL)
  305. return 0;
  306. session = g_try_malloc0(sizeof(*session));
  307. if (session == NULL)
  308. return 0;
  309. session->id = ++id;
  310. session->func = func;
  311. session->port = port;
  312. session->user_data = user_data;
  313. str2ba(destination, &session->dst);
  314. str2ba(source, &session->src);
  315. if (session_connect(session) < 0) {
  316. g_free(session);
  317. return 0;
  318. }
  319. session->service = g_strdup(service);
  320. sessions = g_slist_prepend(sessions, session);
  321. return session->id;
  322. }
  323. static void bluetooth_disconnect(guint id)
  324. {
  325. GSList *l;
  326. DBG("");
  327. for (l = sessions; l; l = l->next) {
  328. struct bluetooth_session *session = l->data;
  329. if (session->id == id) {
  330. session_destroy(session);
  331. return;
  332. }
  333. }
  334. }
  335. static int bluetooth_getpacketopt(GIOChannel *io, int *tx_mtu, int *rx_mtu)
  336. {
  337. int sk = g_io_channel_unix_get_fd(io);
  338. int type;
  339. uint16_t omtu = BT_TX_MTU;
  340. uint16_t imtu = BT_RX_MTU;
  341. socklen_t len = sizeof(int);
  342. DBG("");
  343. if (getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
  344. return -errno;
  345. if (type != SOCK_SEQPACKET)
  346. return -EINVAL;
  347. if (!bt_io_get(io, NULL, BT_IO_OPT_OMTU, &omtu,
  348. BT_IO_OPT_IMTU, &imtu,
  349. BT_IO_OPT_INVALID))
  350. return -EINVAL;
  351. if (tx_mtu)
  352. *tx_mtu = omtu;
  353. if (rx_mtu)
  354. *rx_mtu = imtu;
  355. return 0;
  356. }
  357. static const void *bluetooth_getattribute(guint id, int attribute_id)
  358. {
  359. GSList *l;
  360. sdp_data_t *data;
  361. for (l = sessions; l; l = l->next) {
  362. struct bluetooth_session *session = l->data;
  363. if (session->id != id)
  364. continue;
  365. if (session->sdp_record == NULL)
  366. break;
  367. /* Read version since UUID is already known */
  368. if (attribute_id == SDP_ATTR_PFILE_DESC_LIST) {
  369. sdp_list_t *descs;
  370. void *ret = NULL;
  371. if (sdp_get_profile_descs(session->sdp_record,
  372. &descs) < 0)
  373. return NULL;
  374. if (descs && descs->data) {
  375. sdp_profile_desc_t *desc = descs->data;
  376. ret = GINT_TO_POINTER(desc->version);
  377. }
  378. sdp_list_free(descs, free);
  379. return ret;
  380. }
  381. data = sdp_data_get(session->sdp_record, attribute_id);
  382. if (!data)
  383. break;
  384. return &data->val;
  385. }
  386. return NULL;
  387. }
  388. static struct obc_transport bluetooth = {
  389. .name = "Bluetooth",
  390. .connect = bluetooth_connect,
  391. .getpacketopt = bluetooth_getpacketopt,
  392. .disconnect = bluetooth_disconnect,
  393. .getattribute = bluetooth_getattribute,
  394. };
  395. int bluetooth_init(void)
  396. {
  397. DBG("");
  398. return obc_transport_register(&bluetooth);
  399. }
  400. void bluetooth_exit(void)
  401. {
  402. DBG("");
  403. obc_transport_unregister(&bluetooth);
  404. }