1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2010 Nokia Corporation
* Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/uuid.h"
#include "btio/btio.h"
#include "src/log.h"
#include "src/shared/util.h"
#include "src/shared/att.h"
#include "src/shared/gatt-helpers.h"
#include "src/shared/queue.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-client.h"
#include "attrib/att.h"
#include "attrib/gattrib.h"
struct _GAttrib {
int ref_count;
struct bt_att *att;
struct bt_gatt_client *client;
GIOChannel *io;
GDestroyNotify destroy;
gpointer destroy_user_data;
struct queue *callbacks;
uint8_t *buf;
int buflen;
struct queue *track_ids;
};
struct attrib_callbacks {
unsigned int id;
GAttribResultFunc result_func;
GAttribNotifyFunc notify_func;
GDestroyNotify destroy_func;
gpointer user_data;
GAttrib *parent;
uint16_t notify_handle;
};
GAttrib *g_attrib_new(GIOChannel *io, guint16 mtu, bool ext_signed)
{
gint fd;
GAttrib *attr;
if (!io)
return NULL;
fd = g_io_channel_unix_get_fd(io);
attr = new0(GAttrib, 1);
if (!attr)
return NULL;
g_io_channel_ref(io);
attr->io = io;
attr->att = bt_att_new(fd, ext_signed);
if (!attr->att)
goto fail;
bt_att_set_close_on_unref(attr->att, true);
g_io_channel_set_close_on_unref(io, FALSE);
if (!bt_att_set_mtu(attr->att, mtu))
goto fail;
attr->buf = malloc0(mtu);
attr->buflen = mtu;
if (!attr->buf)
goto fail;
attr->callbacks = queue_new();
if (!attr->callbacks)
goto fail;
attr->track_ids = queue_new();
if (!attr->track_ids)
goto fail;
return g_attrib_ref(attr);
fail:
free(attr->buf);
bt_att_unref(attr->att);
g_io_channel_unref(io);
free(attr);
return NULL;
}
GAttrib *g_attrib_ref(GAttrib *attrib)
{
if (!attrib)
return NULL;
__sync_fetch_and_add(&attrib->ref_count, 1);
DBG("%p: g_attrib_ref=%d ", attrib, attrib->ref_count);
return attrib;
}
static void attrib_callbacks_destroy(void *data)
{
struct attrib_callbacks *cb = data;
if (cb->destroy_func)
cb->destroy_func(cb->user_data);
free(data);
}
static void attrib_callbacks_remove(void *data)
{
struct attrib_callbacks *cb = data;
if (!data || !queue_remove(cb->parent->callbacks, data))
return;
attrib_callbacks_destroy(data);
}
void g_attrib_unref(GAttrib *attrib)
{
if (!attrib)
return;
DBG("%p: g_attrib_unref=%d ", attrib, attrib->ref_count - 1);
if (__sync_sub_and_fetch(&attrib->ref_count, 1))
return;
if (attrib->destroy)
attrib->destroy(attrib->destroy_user_data);
bt_gatt_client_unref(attrib->client);
bt_att_unref(attrib->att);
queue_destroy(attrib->callbacks, attrib_callbacks_destroy);
queue_destroy(attrib->track_ids, NULL);
free(attrib->buf);
g_io_channel_unref(attrib->io);
free(attrib);
}
GIOChannel *g_attrib_get_channel(GAttrib *attrib)
{
if (!attrib)
return NULL;
return attrib->io;
}
struct bt_att *g_attrib_get_att(GAttrib *attrib)
{
if (!attrib)
return NULL;
return attrib->att;
}
gboolean g_attrib_set_destroy_function(GAttrib *attrib, GDestroyNotify destroy,
gpointer user_data)
{
if (!attrib)
return FALSE;
attrib->destroy = destroy;
attrib->destroy_user_data = user_data;
return TRUE;
}
static uint8_t *construct_full_pdu(uint8_t opcode, const void *pdu,
uint16_t length)
{
uint8_t *buf = malloc0(length + 1);
if (!buf)
return NULL;
buf[0] = opcode;
if (pdu && length)
memcpy(buf + 1, pdu, length);
return buf;
}
static void attrib_callback_result(uint8_t opcode, const void *pdu,
uint16_t length, void *user_data)
{
uint8_t *buf;
struct attrib_callbacks *cb = user_data;
guint8 status = 0;
if (!cb)
return;
buf = construct_full_pdu(opcode, pdu, length);
if (!buf)
return;
if (opcode == BT_ATT_OP_ERROR_RSP) {
/* Error code is the third byte of the PDU data */
if (length < 4)
status = BT_ATT_ERROR_UNLIKELY;
else
status = ((guint8 *)pdu)[3];
}
if (cb->result_func)
cb->result_func(status, buf, length + 1, cb->user_data);
free(buf);
}
static void attrib_callback_notify(struct bt_att_chan *chan, uint16_t mtu,
uint8_t opcode, const void *pdu,
uint16_t length, void *user_data)
{
uint8_t *buf;
struct attrib_callbacks *cb = user_data;
if (!cb || !cb->notify_func)
return;
if (cb->notify_handle != GATTRIB_ALL_HANDLES && length < 2)
return;
if (cb->notify_handle != GATTRIB_ALL_HANDLES &&
cb->notify_handle != get_le16(pdu))
return;
buf = construct_full_pdu(opcode, pdu, length);
if (!buf)
return;
cb->notify_func(buf, length + 1, cb->user_data);
free(buf);
}
guint g_attrib_send(GAttrib *attrib, guint id, const guint8 *pdu, guint16 len,
GAttribResultFunc func, gpointer user_data,
GDestroyNotify notify)
{
struct attrib_callbacks *cb = NULL;
bt_att_response_func_t response_cb = NULL;
bt_att_destroy_func_t destroy_cb = NULL;
if (!attrib)
return 0;
if (!pdu || !len)
return 0;
if (func || notify) {
cb = new0(struct attrib_callbacks, 1);
if (!cb)
return 0;
cb->result_func = func;
cb->user_data = user_data;
cb->destroy_func = notify;
cb->parent = attrib;
queue_push_head(attrib->callbacks, cb);
response_cb = attrib_callback_result;
destroy_cb = attrib_callbacks_remove;
}
if (id == 0)
id = bt_att_send(attrib->att, pdu[0], (void *) pdu + 1,
len - 1, response_cb, cb, destroy_cb);
else {
int err;
err = bt_att_resend(attrib->att, id, pdu[0], (void *) pdu + 1,
len - 1, response_cb, cb, destroy_cb);
if (err)
return 0;
}
if (!id)
return id;
/*
* If user what us to use given id, lets keep track on that so we give
* user a possibility to cancel ongoing request.
*/
if (cb) {
cb->id = id;
queue_push_tail(attrib->track_ids, UINT_TO_PTR(id));
}
return id;
}
gboolean g_attrib_cancel(GAttrib *attrib, guint id)
{
if (!attrib)
return FALSE;
return bt_att_cancel(attrib->att, id);
}
static void cancel_request(void *data, void *user_data)
{
unsigned int id = PTR_TO_UINT(data);
GAttrib *attrib = user_data;
bt_att_cancel(attrib->att, id);
}
gboolean g_attrib_cancel_all(GAttrib *attrib)
{
if (!attrib)
return FALSE;
queue_foreach(attrib->track_ids, cancel_request, attrib);
queue_remove_all(attrib->track_ids, NULL, NULL, NULL);
return TRUE;
}
static void client_notify_cb(uint16_t value_handle, const uint8_t *value,
uint16_t length, void *user_data)
{
uint8_t *buf = newa(uint8_t, length + 2);
put_le16(value_handle, buf);
if (length)
memcpy(buf + 2, value, length);
attrib_callback_notify(NULL, 0, ATT_OP_HANDLE_NOTIFY, buf, length + 2,
user_data);
}
guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle,
GAttribNotifyFunc func, gpointer user_data,
GDestroyNotify notify)
{
struct attrib_callbacks *cb = NULL;
if (!attrib)
return 0;
if (func || notify) {
cb = new0(struct attrib_callbacks, 1);
if (!cb)
return 0;
cb->notify_func = func;
cb->notify_handle = handle;
cb->user_data = user_data;
cb->destroy_func = notify;
cb->parent = attrib;
queue_push_head(attrib->callbacks, cb);
}
if (opcode == ATT_OP_HANDLE_NOTIFY && attrib->client) {
unsigned int id;
id = bt_gatt_client_register_notify(attrib->client, handle,
NULL, client_notify_cb, cb,
attrib_callbacks_remove);
if (id)
return id;
}
if (opcode == GATTRIB_ALL_REQS)
opcode = BT_ATT_ALL_REQUESTS;
return bt_att_register(attrib->att, opcode, attrib_callback_notify,
cb, attrib_callbacks_remove);
}
uint8_t *g_attrib_get_buffer(GAttrib *attrib, size_t *len)
{
uint16_t mtu;
if (!attrib || !len)
return NULL;
mtu = bt_att_get_mtu(attrib->att);
/*
* Clients of this expect a buffer to use.
*
* Pdu encoding in shared/att verifies if whole buffer fits the mtu,
* thus we should set the buflen also when mtu is reduced. But we
* need to reallocate the buffer only if mtu is larger.
*/
if (mtu > attrib->buflen)
attrib->buf = g_realloc(attrib->buf, mtu);
attrib->buflen = mtu;
*len = attrib->buflen;
return attrib->buf;
}
gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu)
{
if (!attrib)
return FALSE;
/*
* Clients of this expect a buffer to use.
*
* Pdu encoding in sharred/att verifies if whole buffer fits the mtu,
* thus we should set the buflen also when mtu is reduced. But we
* need to reallocate the buffer only if mtu is larger.
*/
if (mtu > attrib->buflen)
attrib->buf = g_realloc(attrib->buf, mtu);
attrib->buflen = mtu;
return bt_att_set_mtu(attrib->att, mtu);
}
gboolean g_attrib_attach_client(GAttrib *attrib, struct bt_gatt_client *client)
{
if (!attrib || !client)
return FALSE;
if (attrib->client)
bt_gatt_client_unref(attrib->client);
attrib->client = bt_gatt_client_clone(client);
if (!attrib->client)
return FALSE;
return TRUE;
}
gboolean g_attrib_unregister(GAttrib *attrib, guint id)
{
if (!attrib)
return FALSE;
return bt_att_unregister(attrib->att, id);
}
gboolean g_attrib_unregister_all(GAttrib *attrib)
{
if (!attrib)
return false;
return bt_att_unregister_all(attrib->att);
}
|