Blob: gatt.c
Blob id: ca49d58ec03ecb87af73a2c9d08d966d9d7ae7b2
Size: 11.0 KB
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | // SPDX-License-Identifier: LGPL-2.1-or-later /* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2017 Intel Corporation. All rights reserved. * * */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <stdbool.h> #include <sys/uio.h> #include <wordexp.h> #include <glib.h> #include "src/shared/io.h" #include "src/shared/shell.h" #include "src/shared/util.h" #include "gdbus/gdbus.h" #include "bluetooth/bluetooth.h" #include "bluetooth/uuid.h" #include "tools/mesh-gatt/node.h" #include "tools/mesh-gatt/util.h" #include "tools/mesh-gatt/gatt.h" #include "tools/mesh-gatt/prov.h" #include "tools/mesh-gatt/net.h" #define MESH_PROV_DATA_OUT_UUID_STR "00002adc-0000-1000-8000-00805f9b34fb" #define MESH_PROXY_DATA_OUT_UUID_STR "00002ade-0000-1000-8000-00805f9b34fb" static struct io *write_io; static uint16_t write_mtu; static struct io *notify_io; static uint16_t notify_mtu; struct write_data { GDBusProxy *proxy; void *user_data; struct iovec iov; GDBusReturnFunction cb; uint8_t *gatt_data; uint8_t gatt_len; }; struct notify_data { GDBusProxy *proxy; bool enable; GDBusReturnFunction cb; void *user_data; }; bool mesh_gatt_is_child(GDBusProxy *proxy, GDBusProxy *parent, const char *name) { DBusMessageIter iter; const char *parent_path; if (!parent) return FALSE; if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE) return FALSE; dbus_message_iter_get_basic(&iter, &parent_path); if (!strcmp(parent_path, g_dbus_proxy_get_path(parent))) return TRUE; else return FALSE; } /* Refactor this once actual MTU is available */ #define GATT_MTU 23 static void write_data_free(void *user_data) { struct write_data *data = user_data; free(data->gatt_data); free(data); } uint16_t mesh_gatt_sar(uint8_t **pkt, uint16_t size) { const uint8_t *data = *pkt; uint8_t gatt_hdr = *data++; uint8_t type = gatt_hdr & GATT_TYPE_MASK; static uint8_t gatt_size; static uint8_t gatt_pkt[67]; print_byte_array("GATT-RX:\t", *pkt, size); if (size < 1) { gatt_pkt[0] = GATT_TYPE_INVALID; /* TODO: Disconnect GATT per last paragraph sec 6.6 */ return 0; } size--; switch (gatt_hdr & GATT_SAR_MASK) { case GATT_SAR_FIRST: gatt_size = 1; gatt_pkt[0] = type; /* TODO: Start Proxy Timeout */ /* fall through */ case GATT_SAR_CONTINUE: if (gatt_pkt[0] != type || gatt_size + size > MAX_GATT_SIZE) { /* Invalidate packet and return failure */ gatt_pkt[0] = GATT_TYPE_INVALID; /* TODO: Disconnect GATT per last paragraph sec 6.6 */ return 0; } memcpy(gatt_pkt + gatt_size, data, size); gatt_size += size; /* We are good to this point, but incomplete */ return 0; default: case GATT_SAR_COMPLETE: gatt_size = 1; gatt_pkt[0] = type; /* fall through */ case GATT_SAR_LAST: if (gatt_pkt[0] != type || gatt_size + size > MAX_GATT_SIZE) { /* Invalidate packet and return failure */ gatt_pkt[0] = GATT_TYPE_INVALID; /* Disconnect GATT per last paragraph sec 6.6 */ return 0; } memcpy(gatt_pkt + gatt_size, data, size); gatt_size += size; *pkt = gatt_pkt; return gatt_size; } } static int sock_send(struct io *io, struct iovec *iov, size_t iovlen) { struct msghdr msg; int ret; memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = iovlen; ret = sendmsg(io_get_fd(io), &msg, MSG_NOSIGNAL); if (ret < 0) { ret = -errno; bt_shell_printf("sendmsg: %s", strerror(-ret)); } return ret; } static bool sock_write(struct io *io, void *user_data) { struct write_data *data = user_data; struct iovec iov[2]; uint8_t sar; uint8_t max_len; if (data == NULL) return true; max_len = write_mtu ? write_mtu - 3 - 1 : GATT_MTU - 3 - 1; print_byte_array("GATT-TX:\t", data->gatt_data, data->gatt_len); sar = data->gatt_data[0]; data->iov.iov_base = data->gatt_data + 1; data->iov.iov_len--; sar = data->gatt_data[0] & GATT_TYPE_MASK; data->gatt_len--; if (data->gatt_len > max_len) { sar |= GATT_SAR_FIRST; data->iov.iov_len = max_len; } iov[0].iov_base = &sar; iov[0].iov_len = sizeof(sar); while (1) { int err; iov[1] = data->iov; err = sock_send(io, iov, 2); if (err < 0) { write_data_free(data); return false; } switch (sar & GATT_SAR_MASK) { case GATT_SAR_FIRST: case GATT_SAR_CONTINUE: data->gatt_len -= max_len; data->iov.iov_base = data->iov.iov_base + max_len; sar &= GATT_TYPE_MASK; if (max_len < data->gatt_len) { data->iov.iov_len = max_len; sar |= GATT_SAR_CONTINUE; } else { data->iov.iov_len = data->gatt_len; sar |= GATT_SAR_LAST; } break; default: if(data->cb) data->cb(NULL, data->user_data); write_data_free(data); return true; } } } static void write_io_destroy(void) { io_destroy(write_io); write_io = NULL; write_mtu = 0; } static void notify_io_destroy(void) { io_destroy(notify_io); notify_io = NULL; notify_mtu = 0; } static bool sock_hup(struct io *io, void *user_data) { bt_shell_printf("%s closed\n", io == notify_io ? "Notify" : "Write"); if (io == notify_io) notify_io_destroy(); else write_io_destroy(); return false; } static struct io *sock_io_new(int fd) { struct io *io; io = io_new(fd); io_set_close_on_destroy(io, true); io_set_disconnect_handler(io, sock_hup, NULL, NULL); return io; } static void acquire_write_reply(DBusMessage *message, void *user_data) { struct write_data *data = user_data; DBusError error; int fd; dbus_error_init(&error); if (dbus_set_error_from_message(&error, message) == TRUE) { dbus_error_free(&error); bt_shell_printf("Failed to write\n"); write_data_free(data); return; } if ((dbus_message_get_args(message, NULL, DBUS_TYPE_UNIX_FD, &fd, DBUS_TYPE_UINT16, &write_mtu, DBUS_TYPE_INVALID) == false)) { bt_shell_printf("Invalid AcquireWrite response\n"); return; } bt_shell_printf("AcquireWrite success: fd %d MTU %u\n", fd, write_mtu); write_io = sock_io_new(fd); sock_write(write_io, data); } static void acquire_setup(DBusMessageIter *iter, void *user_data) { DBusMessageIter dict; dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); dbus_message_iter_close_container(iter, &dict); } bool mesh_gatt_write(GDBusProxy *proxy, uint8_t *buf, uint16_t len, GDBusReturnFunction cb, void *user_data) { struct write_data *data; if (!buf || !len) return false; if (len > 69) return false; data = g_new0(struct write_data, 1); if (!data) return false; /* TODO: should keep in queue in case we need to cancel write? */ data->gatt_len = len; data->gatt_data = util_memdup(buf, len); data->gatt_data[0] &= GATT_TYPE_MASK; data->iov.iov_base = data->gatt_data; data->iov.iov_len = len; data->proxy = proxy; data->user_data = user_data; data->cb = cb; if (write_io) return sock_write(write_io, data); if (g_dbus_proxy_method_call(proxy, "AcquireWrite", acquire_setup, acquire_write_reply, data, NULL) == FALSE) { bt_shell_printf("Failed to AcquireWrite\n"); write_data_free(data); return false; } return true; } static void notify_reply(DBusMessage *message, void *user_data) { struct notify_data *data = user_data; DBusError error; dbus_error_init(&error); if (dbus_set_error_from_message(&error, message) == TRUE) { bt_shell_printf("Failed to %s notify: %s\n", data->enable ? "start" : "stop", error.name); dbus_error_free(&error); goto done; } bt_shell_printf("Notify %s\n", data->enable ? "started" : "stopped"); done: if (data->cb) data->cb(message, data->user_data); g_free(data); } static bool sock_read(struct io *io, bool prov, void *user_data) { struct mesh_node *node = user_data; struct msghdr msg; struct iovec iov; uint8_t buf[512]; uint8_t *res; int fd = io_get_fd(io); ssize_t len, len_sar; if (io != notify_io) return true; iov.iov_base = buf; iov.iov_len = sizeof(buf); memset(&msg, 0, sizeof(msg)); msg.msg_iov = &iov; msg.msg_iovlen = 1; while ((len = recvmsg(fd, &msg, MSG_DONTWAIT))) { if (len < 0) { if (errno == EAGAIN) break; return false; } res = buf; len_sar = mesh_gatt_sar(&res, len); if (len_sar) { if (prov) prov_data_ready(node, res, len_sar); else net_data_ready(res, len_sar); } } /* When socket is orderly closed, then recvmsg returns 0 */ if (len == 0) return false; return true; } static bool sock_read_prov(struct io *io, void *user_data) { return sock_read(io, true, user_data); } static bool sock_read_proxy(struct io *io, void *user_data) { return sock_read(io, false, user_data); } static void acquire_notify_reply(DBusMessage *message, void *user_data) { struct notify_data *data = user_data; DBusMessageIter iter; DBusError error; int fd; const char *uuid; dbus_error_init(&error); if (dbus_set_error_from_message(&error, message) == TRUE) { dbus_error_free(&error); if (g_dbus_proxy_method_call(data->proxy, "StartNotify", NULL, notify_reply, data, NULL) == FALSE) { bt_shell_printf("Failed to StartNotify\n"); g_free(data); } return; } if (notify_io) { io_destroy(notify_io); notify_io = NULL; } notify_mtu = 0; if ((dbus_message_get_args(message, NULL, DBUS_TYPE_UNIX_FD, &fd, DBUS_TYPE_UINT16, ¬ify_mtu, DBUS_TYPE_INVALID) == false)) { if (g_dbus_proxy_method_call(data->proxy, "StartNotify", NULL, notify_reply, data, NULL) == FALSE) { bt_shell_printf("Failed to StartNotify\n"); g_free(data); } return; } bt_shell_printf("AcquireNotify success: fd %d MTU %u\n", fd, notify_mtu); if (g_dbus_proxy_get_property(data->proxy, "UUID", &iter) == FALSE) goto done; notify_io = sock_io_new(fd); dbus_message_iter_get_basic(&iter, &uuid); if (!bt_uuid_strcmp(uuid, MESH_PROV_DATA_OUT_UUID_STR)) io_set_read_handler(notify_io, sock_read_prov, data->user_data, NULL); else if (!bt_uuid_strcmp(uuid, MESH_PROXY_DATA_OUT_UUID_STR)) io_set_read_handler(notify_io, sock_read_proxy, data->user_data, NULL); done: if (data->cb) data->cb(message, data->user_data); g_free(data); } bool mesh_gatt_notify(GDBusProxy *proxy, bool enable, GDBusReturnFunction cb, void *user_data) { struct notify_data *data; DBusMessageIter iter; const char *method; GDBusSetupFunction setup = NULL; data = g_new0(struct notify_data, 1); data->proxy = proxy; data->enable = enable; data->cb = cb; data->user_data = user_data; if (enable == TRUE) { if (g_dbus_proxy_get_property(proxy, "NotifyAcquired", &iter)) { method = "AcquireNotify"; cb = acquire_notify_reply; setup = acquire_setup; } else { method = "StartNotify"; cb = notify_reply; } } else { if (notify_io) { notify_io_destroy(); if (cb) cb(NULL, user_data); g_free(data); return true; } else { method = "StopNotify"; cb = notify_reply; } } if (g_dbus_proxy_method_call(proxy, method, setup, cb, data, NULL) == FALSE) { bt_shell_printf("Failed to %s\n", method); return false; } return true; } |