gattrib.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include <glib.h>
  19. #include "lib/bluetooth.h"
  20. #include "btio/btio.h"
  21. #include "src/log.h"
  22. #include "src/shared/util.h"
  23. #include "src/shared/att.h"
  24. #include "src/shared/queue.h"
  25. #include "attrib/gattrib.h"
  26. struct _GAttrib {
  27. int ref_count;
  28. struct bt_att *att;
  29. GIOChannel *io;
  30. GDestroyNotify destroy;
  31. gpointer destroy_user_data;
  32. struct queue *callbacks;
  33. uint8_t *buf;
  34. int buflen;
  35. struct queue *track_ids;
  36. };
  37. struct attrib_callbacks {
  38. unsigned int id;
  39. GAttribResultFunc result_func;
  40. GAttribNotifyFunc notify_func;
  41. GDestroyNotify destroy_func;
  42. gpointer user_data;
  43. GAttrib *parent;
  44. uint16_t notify_handle;
  45. };
  46. GAttrib *g_attrib_new(GIOChannel *io, guint16 mtu, bool ext_signed)
  47. {
  48. gint fd;
  49. GAttrib *attr;
  50. if (!io)
  51. return NULL;
  52. fd = g_io_channel_unix_get_fd(io);
  53. attr = new0(GAttrib, 1);
  54. if (!attr)
  55. return NULL;
  56. g_io_channel_ref(io);
  57. attr->io = io;
  58. attr->att = bt_att_new(fd, ext_signed);
  59. if (!attr->att)
  60. goto fail;
  61. bt_att_set_close_on_unref(attr->att, true);
  62. g_io_channel_set_close_on_unref(io, FALSE);
  63. if (!bt_att_set_mtu(attr->att, mtu))
  64. goto fail;
  65. attr->buf = malloc0(mtu);
  66. attr->buflen = mtu;
  67. if (!attr->buf)
  68. goto fail;
  69. attr->callbacks = queue_new();
  70. if (!attr->callbacks)
  71. goto fail;
  72. attr->track_ids = queue_new();
  73. if (!attr->track_ids)
  74. goto fail;
  75. return g_attrib_ref(attr);
  76. fail:
  77. free(attr->buf);
  78. bt_att_unref(attr->att);
  79. g_io_channel_unref(io);
  80. free(attr);
  81. return NULL;
  82. }
  83. GAttrib *g_attrib_ref(GAttrib *attrib)
  84. {
  85. if (!attrib)
  86. return NULL;
  87. __sync_fetch_and_add(&attrib->ref_count, 1);
  88. DBG("%p: g_attrib_ref=%d ", attrib, attrib->ref_count);
  89. return attrib;
  90. }
  91. static void attrib_callbacks_destroy(void *data)
  92. {
  93. struct attrib_callbacks *cb = data;
  94. if (cb->destroy_func)
  95. cb->destroy_func(cb->user_data);
  96. free(data);
  97. }
  98. static void attrib_callbacks_remove(void *data)
  99. {
  100. struct attrib_callbacks *cb = data;
  101. if (!data || !queue_remove(cb->parent->callbacks, data))
  102. return;
  103. attrib_callbacks_destroy(data);
  104. }
  105. void g_attrib_unref(GAttrib *attrib)
  106. {
  107. if (!attrib)
  108. return;
  109. DBG("%p: g_attrib_unref=%d ", attrib, attrib->ref_count - 1);
  110. if (__sync_sub_and_fetch(&attrib->ref_count, 1))
  111. return;
  112. if (attrib->destroy)
  113. attrib->destroy(attrib->destroy_user_data);
  114. bt_att_unref(attrib->att);
  115. queue_destroy(attrib->callbacks, attrib_callbacks_destroy);
  116. queue_destroy(attrib->track_ids, NULL);
  117. free(attrib->buf);
  118. g_io_channel_unref(attrib->io);
  119. free(attrib);
  120. }
  121. GIOChannel *g_attrib_get_channel(GAttrib *attrib)
  122. {
  123. if (!attrib)
  124. return NULL;
  125. return attrib->io;
  126. }
  127. struct bt_att *g_attrib_get_att(GAttrib *attrib)
  128. {
  129. if (!attrib)
  130. return NULL;
  131. return attrib->att;
  132. }
  133. gboolean g_attrib_set_destroy_function(GAttrib *attrib, GDestroyNotify destroy,
  134. gpointer user_data)
  135. {
  136. if (!attrib)
  137. return FALSE;
  138. attrib->destroy = destroy;
  139. attrib->destroy_user_data = user_data;
  140. return TRUE;
  141. }
  142. static uint8_t *construct_full_pdu(uint8_t opcode, const void *pdu,
  143. uint16_t length)
  144. {
  145. uint8_t *buf = malloc0(length + 1);
  146. if (!buf)
  147. return NULL;
  148. buf[0] = opcode;
  149. memcpy(buf + 1, pdu, length);
  150. return buf;
  151. }
  152. static void attrib_callback_result(uint8_t opcode, const void *pdu,
  153. uint16_t length, void *user_data)
  154. {
  155. uint8_t *buf;
  156. struct attrib_callbacks *cb = user_data;
  157. guint8 status = 0;
  158. if (!cb)
  159. return;
  160. buf = construct_full_pdu(opcode, pdu, length);
  161. if (!buf)
  162. return;
  163. if (opcode == BT_ATT_OP_ERROR_RSP) {
  164. /* Error code is the third byte of the PDU data */
  165. if (length < 4)
  166. status = BT_ATT_ERROR_UNLIKELY;
  167. else
  168. status = ((guint8 *)pdu)[3];
  169. }
  170. if (cb->result_func)
  171. cb->result_func(status, buf, length + 1, cb->user_data);
  172. free(buf);
  173. }
  174. static void attrib_callback_notify(struct bt_att_chan *chan, uint8_t opcode,
  175. const void *pdu, uint16_t length,
  176. void *user_data)
  177. {
  178. uint8_t *buf;
  179. struct attrib_callbacks *cb = user_data;
  180. if (!cb || !cb->notify_func)
  181. return;
  182. if (cb->notify_handle != GATTRIB_ALL_HANDLES && length < 2)
  183. return;
  184. if (cb->notify_handle != GATTRIB_ALL_HANDLES &&
  185. cb->notify_handle != get_le16(pdu))
  186. return;
  187. buf = construct_full_pdu(opcode, pdu, length);
  188. if (!buf)
  189. return;
  190. cb->notify_func(buf, length + 1, cb->user_data);
  191. free(buf);
  192. }
  193. guint g_attrib_send(GAttrib *attrib, guint id, const guint8 *pdu, guint16 len,
  194. GAttribResultFunc func, gpointer user_data,
  195. GDestroyNotify notify)
  196. {
  197. struct attrib_callbacks *cb = NULL;
  198. bt_att_response_func_t response_cb = NULL;
  199. bt_att_destroy_func_t destroy_cb = NULL;
  200. if (!attrib)
  201. return 0;
  202. if (!pdu || !len)
  203. return 0;
  204. if (func || notify) {
  205. cb = new0(struct attrib_callbacks, 1);
  206. if (!cb)
  207. return 0;
  208. cb->result_func = func;
  209. cb->user_data = user_data;
  210. cb->destroy_func = notify;
  211. cb->parent = attrib;
  212. queue_push_head(attrib->callbacks, cb);
  213. response_cb = attrib_callback_result;
  214. destroy_cb = attrib_callbacks_remove;
  215. }
  216. if (id == 0)
  217. id = bt_att_send(attrib->att, pdu[0], (void *) pdu + 1,
  218. len - 1, response_cb, cb, destroy_cb);
  219. else {
  220. int err;
  221. err = bt_att_resend(attrib->att, id, pdu[0], (void *) pdu + 1,
  222. len - 1, response_cb, cb, destroy_cb);
  223. if (err)
  224. return 0;
  225. }
  226. if (!id)
  227. return id;
  228. /*
  229. * If user what us to use given id, lets keep track on that so we give
  230. * user a possibility to cancel ongoing request.
  231. */
  232. if (cb) {
  233. cb->id = id;
  234. queue_push_tail(attrib->track_ids, UINT_TO_PTR(id));
  235. }
  236. return id;
  237. }
  238. gboolean g_attrib_cancel(GAttrib *attrib, guint id)
  239. {
  240. if (!attrib)
  241. return FALSE;
  242. return bt_att_cancel(attrib->att, id);
  243. }
  244. static void cancel_request(void *data, void *user_data)
  245. {
  246. unsigned int id = PTR_TO_UINT(data);
  247. GAttrib *attrib = user_data;
  248. bt_att_cancel(attrib->att, id);
  249. }
  250. gboolean g_attrib_cancel_all(GAttrib *attrib)
  251. {
  252. if (!attrib)
  253. return FALSE;
  254. queue_foreach(attrib->track_ids, cancel_request, attrib);
  255. queue_remove_all(attrib->track_ids, NULL, NULL, NULL);
  256. return TRUE;
  257. }
  258. guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle,
  259. GAttribNotifyFunc func, gpointer user_data,
  260. GDestroyNotify notify)
  261. {
  262. struct attrib_callbacks *cb = NULL;
  263. if (!attrib)
  264. return 0;
  265. if (func || notify) {
  266. cb = new0(struct attrib_callbacks, 1);
  267. if (!cb)
  268. return 0;
  269. cb->notify_func = func;
  270. cb->notify_handle = handle;
  271. cb->user_data = user_data;
  272. cb->destroy_func = notify;
  273. cb->parent = attrib;
  274. queue_push_head(attrib->callbacks, cb);
  275. }
  276. if (opcode == GATTRIB_ALL_REQS)
  277. opcode = BT_ATT_ALL_REQUESTS;
  278. return bt_att_register(attrib->att, opcode, attrib_callback_notify,
  279. cb, attrib_callbacks_remove);
  280. }
  281. uint8_t *g_attrib_get_buffer(GAttrib *attrib, size_t *len)
  282. {
  283. uint16_t mtu;
  284. if (!attrib || !len)
  285. return NULL;
  286. mtu = bt_att_get_mtu(attrib->att);
  287. /*
  288. * Clients of this expect a buffer to use.
  289. *
  290. * Pdu encoding in shared/att verifies if whole buffer fits the mtu,
  291. * thus we should set the buflen also when mtu is reduced. But we
  292. * need to reallocate the buffer only if mtu is larger.
  293. */
  294. if (mtu > attrib->buflen)
  295. attrib->buf = g_realloc(attrib->buf, mtu);
  296. attrib->buflen = mtu;
  297. *len = attrib->buflen;
  298. return attrib->buf;
  299. }
  300. gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu)
  301. {
  302. if (!attrib)
  303. return FALSE;
  304. /*
  305. * Clients of this expect a buffer to use.
  306. *
  307. * Pdu encoding in sharred/att verifies if whole buffer fits the mtu,
  308. * thus we should set the buflen also when mtu is reduced. But we
  309. * need to reallocate the buffer only if mtu is larger.
  310. */
  311. if (mtu > attrib->buflen)
  312. attrib->buf = g_realloc(attrib->buf, mtu);
  313. attrib->buflen = mtu;
  314. return bt_att_set_mtu(attrib->att, mtu);
  315. }
  316. gboolean g_attrib_unregister(GAttrib *attrib, guint id)
  317. {
  318. if (!attrib)
  319. return FALSE;
  320. return bt_att_unregister(attrib->att, id);
  321. }
  322. gboolean g_attrib_unregister_all(GAttrib *attrib)
  323. {
  324. if (!attrib)
  325. return false;
  326. return bt_att_unregister_all(attrib->att);
  327. }