1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
23 #include "lib/bluetooth.h"
25 #include "btio/btio.h"
27 #include "src/shared/util.h"
28 #include "src/shared/att.h"
29 #include "src/shared/queue.h"
30 #include "attrib/gattrib.h"
36 GDestroyNotify destroy;
37 gpointer destroy_user_data;
38 struct queue *callbacks;
41 struct queue *track_ids;
44 struct attrib_callbacks {
46 GAttribResultFunc result_func;
47 GAttribNotifyFunc notify_func;
48 GDestroyNotify destroy_func;
51 uint16_t notify_handle;
54 GAttrib *g_attrib_new(GIOChannel *io, guint16 mtu, bool ext_signed)
62 fd = g_io_channel_unix_get_fd(io);
63 attr = new0(GAttrib, 1);
70 attr->att = bt_att_new(fd, ext_signed);
74 bt_att_set_close_on_unref(attr->att, true);
75 g_io_channel_set_close_on_unref(io, FALSE);
77 if (!bt_att_set_mtu(attr->att, mtu))
80 attr->buf = malloc0(mtu);
85 attr->callbacks = queue_new();
89 attr->track_ids = queue_new();
93 return g_attrib_ref(attr);
97 bt_att_unref(attr->att);
98 g_io_channel_unref(io);
103 GAttrib *g_attrib_ref(GAttrib *attrib)
108 __sync_fetch_and_add(&attrib->ref_count, 1);
110 DBG("%p: g_attrib_ref=%d ", attrib, attrib->ref_count);
115 static void attrib_callbacks_destroy(void *data)
117 struct attrib_callbacks *cb = data;
119 if (cb->destroy_func)
120 cb->destroy_func(cb->user_data);
125 static void attrib_callbacks_remove(void *data)
127 struct attrib_callbacks *cb = data;
129 if (!data || !queue_remove(cb->parent->callbacks, data))
132 attrib_callbacks_destroy(data);
135 void g_attrib_unref(GAttrib *attrib)
140 DBG("%p: g_attrib_unref=%d ", attrib, attrib->ref_count - 1);
142 if (__sync_sub_and_fetch(&attrib->ref_count, 1))
146 attrib->destroy(attrib->destroy_user_data);
148 bt_att_unref(attrib->att);
150 queue_destroy(attrib->callbacks, attrib_callbacks_destroy);
151 queue_destroy(attrib->track_ids, NULL);
155 g_io_channel_unref(attrib->io);
160 GIOChannel *g_attrib_get_channel(GAttrib *attrib)
168 struct bt_att *g_attrib_get_att(GAttrib *attrib)
176 gboolean g_attrib_set_destroy_function(GAttrib *attrib, GDestroyNotify destroy,
182 attrib->destroy = destroy;
183 attrib->destroy_user_data = user_data;
189 static uint8_t *construct_full_pdu(uint8_t opcode, const void *pdu,
192 uint8_t *buf = malloc0(length + 1);
200 memcpy(buf + 1, pdu, length);
205 static void attrib_callback_result(uint8_t opcode, const void *pdu,
206 uint16_t length, void *user_data)
209 struct attrib_callbacks *cb = user_data;
215 buf = construct_full_pdu(opcode, pdu, length);
219 if (opcode == BT_ATT_OP_ERROR_RSP) {
220 /* Error code is the third byte of the PDU data */
222 status = BT_ATT_ERROR_UNLIKELY;
224 status = ((guint8 *)pdu)[3];
228 cb->result_func(status, buf, length + 1, cb->user_data);
233 static void attrib_callback_notify(struct bt_att_chan *chan, uint8_t opcode,
234 const void *pdu, uint16_t length,
238 struct attrib_callbacks *cb = user_data;
240 if (!cb || !cb->notify_func)
243 if (cb->notify_handle != GATTRIB_ALL_HANDLES && length < 2)
246 if (cb->notify_handle != GATTRIB_ALL_HANDLES &&
247 cb->notify_handle != get_le16(pdu))
250 buf = construct_full_pdu(opcode, pdu, length);
254 cb->notify_func(buf, length + 1, cb->user_data);
259 guint g_attrib_send(GAttrib *attrib, guint id, const guint8 *pdu, guint16 len,
260 GAttribResultFunc func, gpointer user_data,
261 GDestroyNotify notify)
263 struct attrib_callbacks *cb = NULL;
264 bt_att_response_func_t response_cb = NULL;
265 bt_att_destroy_func_t destroy_cb = NULL;
273 if (func || notify) {
274 cb = new0(struct attrib_callbacks, 1);
277 cb->result_func = func;
278 cb->user_data = user_data;
279 cb->destroy_func = notify;
281 queue_push_head(attrib->callbacks, cb);
282 response_cb = attrib_callback_result;
283 destroy_cb = attrib_callbacks_remove;
288 id = bt_att_send(attrib->att, pdu[0], (void *) pdu + 1,
289 len - 1, response_cb, cb, destroy_cb);
293 err = bt_att_resend(attrib->att, id, pdu[0], (void *) pdu + 1,
294 len - 1, response_cb, cb, destroy_cb);
303 * If user what us to use given id, lets keep track on that so we give
304 * user a possibility to cancel ongoing request.
308 queue_push_tail(attrib->track_ids, UINT_TO_PTR(id));
314 gboolean g_attrib_cancel(GAttrib *attrib, guint id)
319 return bt_att_cancel(attrib->att, id);
322 static void cancel_request(void *data, void *user_data)
324 unsigned int id = PTR_TO_UINT(data);
325 GAttrib *attrib = user_data;
327 bt_att_cancel(attrib->att, id);
330 gboolean g_attrib_cancel_all(GAttrib *attrib)
335 queue_foreach(attrib->track_ids, cancel_request, attrib);
336 queue_remove_all(attrib->track_ids, NULL, NULL, NULL);
341 guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle,
342 GAttribNotifyFunc func, gpointer user_data,
343 GDestroyNotify notify)
345 struct attrib_callbacks *cb = NULL;
350 if (func || notify) {
351 cb = new0(struct attrib_callbacks, 1);
354 cb->notify_func = func;
355 cb->notify_handle = handle;
356 cb->user_data = user_data;
357 cb->destroy_func = notify;
359 queue_push_head(attrib->callbacks, cb);
362 if (opcode == GATTRIB_ALL_REQS)
363 opcode = BT_ATT_ALL_REQUESTS;
365 return bt_att_register(attrib->att, opcode, attrib_callback_notify,
366 cb, attrib_callbacks_remove);
369 uint8_t *g_attrib_get_buffer(GAttrib *attrib, size_t *len)
376 mtu = bt_att_get_mtu(attrib->att);
379 * Clients of this expect a buffer to use.
381 * Pdu encoding in shared/att verifies if whole buffer fits the mtu,
382 * thus we should set the buflen also when mtu is reduced. But we
383 * need to reallocate the buffer only if mtu is larger.
385 if (mtu > attrib->buflen)
386 attrib->buf = g_realloc(attrib->buf, mtu);
388 attrib->buflen = mtu;
389 *len = attrib->buflen;
393 gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu)
399 * Clients of this expect a buffer to use.
401 * Pdu encoding in sharred/att verifies if whole buffer fits the mtu,
402 * thus we should set the buflen also when mtu is reduced. But we
403 * need to reallocate the buffer only if mtu is larger.
405 if (mtu > attrib->buflen)
406 attrib->buf = g_realloc(attrib->buf, mtu);
408 attrib->buflen = mtu;
410 return bt_att_set_mtu(attrib->att, mtu);
413 gboolean g_attrib_unregister(GAttrib *attrib, guint id)
418 return bt_att_unregister(attrib->att, id);
421 gboolean g_attrib_unregister_all(GAttrib *attrib)
426 return bt_att_unregister_all(attrib->att);