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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Instituto Nokia de Tecnologia - INdT
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <sys/signalfd.h>
#include <glib.h>
#include <dbus/dbus.h>
#include "gdbus/gdbus.h"
#include "src/error.h"
#include "src/shared/util.h"
#define GATT_MGR_IFACE "org.bluez.GattManager1"
#define GATT_SERVICE_IFACE "org.bluez.GattService1"
#define GATT_CHR_IFACE "org.bluez.GattCharacteristic1"
#define GATT_DESCRIPTOR_IFACE "org.bluez.GattDescriptor1"
/* Immediate Alert Service UUID */
#define IAS_UUID "00001802-0000-1000-8000-00805f9b34fb"
#define ALERT_LEVEL_CHR_UUID "00002a06-0000-1000-8000-00805f9b34fb"
#define IAS_UUID1 "A00B"
#define IAS_UUID2 "A00C"
#define IAS_UUID3 "A00D"
#define ALERT_LEVEL_CHR_UUID1 "00002b06-0000-1000-8000-00805f9b34fb"
#define ALERT_LEVEL_CHR_UUID2 "00002c07-0000-1000-8000-00805f9b34fb"
/* Random UUID for testing purpose */
/* Random UUID for testing purpose */
#define READ_WRITE_DESCRIPTOR_UUID "8260c653-1a54-426b-9e36-e84c238bc669"
#define READ_WRITE_DESCRIPTOR_UUID1 "0260c653-1a54-426b-9e36-e84c238bc669"
#define READ_WRITE_DESCRIPTOR_UUID2 "FFFF"
static GMainLoop *main_loop;
static GSList *services;
static DBusConnection *connection;
struct characteristic {
char *service;
char *uuid;
char *path;
uint8_t *value;
int vlen;
const char **props;
};
struct descriptor {
struct characteristic *chr;
char *uuid;
char *path;
uint8_t *value;
int vlen;
const char **props;
};
/*
* Alert Level support Write Without Response only. Supported
* properties are defined at doc/gatt-api.txt. See "Flags"
* property of the GattCharacteristic1.
*/
static const char *ias_alert_level_props[] = { "write-without-response", NULL };
static const char *desc_props[] = { "read", "write", NULL };
static gboolean desc_get_uuid(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct descriptor *desc = user_data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &desc->uuid);
return TRUE;
}
static gboolean desc_get_characteristic(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct descriptor *desc = user_data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
&desc->chr->path);
return TRUE;
}
static bool desc_read(struct descriptor *desc, DBusMessageIter *iter)
{
DBusMessageIter array;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_BYTE_AS_STRING, &array);
if (desc->vlen && desc->value)
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
&desc->value, desc->vlen);
dbus_message_iter_close_container(iter, &array);
return true;
}
static gboolean desc_get_value(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct descriptor *desc = user_data;
printf("Descriptor(%s): Get(\"Value\")\n", desc->uuid);
return desc_read(desc, iter);
}
static void desc_write(struct descriptor *desc, const uint8_t *value, int len)
{
free(desc->value);
desc->value = util_memdup(value, len);
desc->vlen = len;
g_dbus_emit_property_changed(connection, desc->path,
GATT_DESCRIPTOR_IFACE, "Value");
}
static int parse_value(DBusMessageIter *iter, const uint8_t **value, int *len)
{
DBusMessageIter array;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
return -EINVAL;
dbus_message_iter_recurse(iter, &array);
dbus_message_iter_get_fixed_array(&array, value, len);
return 0;
}
static void desc_set_value(const GDBusPropertyTable *property,
DBusMessageIter *iter,
GDBusPendingPropertySet id, void *user_data)
{
struct descriptor *desc = user_data;
const uint8_t *value;
int len;
printf("Descriptor(%s): Set(\"Value\", ...)\n", desc->uuid);
if (parse_value(iter, &value, &len)) {
printf("Invalid value for Set('Value'...)\n");
g_dbus_pending_property_error(id,
ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
return;
}
desc_write(desc, value, len);
g_dbus_pending_property_success(id);
}
static gboolean desc_get_props(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct descriptor *desc = data;
DBusMessageIter array;
int i;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &array);
for (i = 0; desc->props[i]; i++)
dbus_message_iter_append_basic(&array,
DBUS_TYPE_STRING, &desc->props[i]);
dbus_message_iter_close_container(iter, &array);
return TRUE;
}
static const GDBusPropertyTable desc_properties[] = {
{ "UUID", "s", desc_get_uuid },
{ "Characteristic", "o", desc_get_characteristic },
{ "Value", "ay", desc_get_value, desc_set_value, NULL },
{ "Flags", "as", desc_get_props, NULL, NULL },
{ }
};
static gboolean chr_get_uuid(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct characteristic *chr = user_data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &chr->uuid);
return TRUE;
}
static gboolean chr_get_service(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct characteristic *chr = user_data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
&chr->service);
return TRUE;
}
static bool chr_read(struct characteristic *chr, DBusMessageIter *iter)
{
DBusMessageIter array;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_BYTE_AS_STRING, &array);
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
&chr->value, chr->vlen);
dbus_message_iter_close_container(iter, &array);
return true;
}
static gboolean chr_get_value(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
struct characteristic *chr = user_data;
printf("Characteristic(%s): Get(\"Value\")\n", chr->uuid);
return chr_read(chr, iter);
}
static gboolean chr_get_props(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct characteristic *chr = data;
DBusMessageIter array;
int i;
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING, &array);
for (i = 0; chr->props[i]; i++)
dbus_message_iter_append_basic(&array,
DBUS_TYPE_STRING, &chr->props[i]);
dbus_message_iter_close_container(iter, &array);
return TRUE;
}
static void chr_write(struct characteristic *chr, const uint8_t *value, int len)
{
free(chr->value);
chr->value = util_memdup(value, len);
chr->vlen = len;
g_dbus_emit_property_changed(connection, chr->path, GATT_CHR_IFACE,
"Value");
}
static void chr_set_value(const GDBusPropertyTable *property,
DBusMessageIter *iter,
GDBusPendingPropertySet id, void *user_data)
{
struct characteristic *chr = user_data;
const uint8_t *value;
int len;
printf("Characteristic(%s): Set('Value', ...)\n", chr->uuid);
if (!parse_value(iter, &value, &len)) {
printf("Invalid value for Set('Value'...)\n");
g_dbus_pending_property_error(id,
ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
return;
}
chr_write(chr, value, len);
g_dbus_pending_property_success(id);
}
static const GDBusPropertyTable chr_properties[] = {
{ "UUID", "s", chr_get_uuid },
{ "Service", "o", chr_get_service },
{ "Value", "ay", chr_get_value, chr_set_value, NULL },
{ "Flags", "as", chr_get_props, NULL, NULL },
{ }
};
static gboolean service_get_primary(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
dbus_bool_t primary = TRUE;
printf("Get Primary: %s\n", primary ? "True" : "False");
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &primary);
return TRUE;
}
static gboolean service_get_uuid(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
const char *uuid = user_data;
printf("Get UUID: %s\n", uuid);
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
return TRUE;
}
static gboolean service_get_includes(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
{
const char *uuid = user_data;
char service_path[100] = {0,};
DBusMessageIter array;
char *p = NULL;
snprintf(service_path, 100, "/service3");
printf("Get Includes: %s\n", uuid);
p = service_path;
printf("Includes path: %s\n", p);
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
&p);
snprintf(service_path, 100, "/service2");
p = service_path;
printf("Get Includes: %s\n", p);
dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
&p);
dbus_message_iter_close_container(iter, &array);
return TRUE;
}
static gboolean service_exist_includes(const GDBusPropertyTable *property,
void *user_data)
{
const char *uuid = user_data;
printf("Exist Includes: %s\n", uuid);
if (strncmp(uuid, "00001802", 8) == 0)
return TRUE;
return FALSE;
}
static const GDBusPropertyTable service_properties[] = {
{ "Primary", "b", service_get_primary },
{ "UUID", "s", service_get_uuid },
{ "Includes", "ao", service_get_includes, NULL,
service_exist_includes },
{ }
};
static void chr_iface_destroy(gpointer user_data)
{
struct characteristic *chr = user_data;
g_free(chr->uuid);
g_free(chr->service);
free(chr->value);
g_free(chr->path);
g_free(chr);
}
static void desc_iface_destroy(gpointer user_data)
{
struct descriptor *desc = user_data;
g_free(desc->uuid);
free(desc->value);
g_free(desc->path);
g_free(desc);
}
static int parse_options(DBusMessageIter *iter, const char **device)
{
DBusMessageIter dict;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
return -EINVAL;
dbus_message_iter_recurse(iter, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
const char *key;
DBusMessageIter value, entry;
int var;
dbus_message_iter_recurse(&dict, &entry);
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
dbus_message_iter_recurse(&entry, &value);
var = dbus_message_iter_get_arg_type(&value);
if (strcasecmp(key, "device") == 0) {
if (var != DBUS_TYPE_OBJECT_PATH)
return -EINVAL;
dbus_message_iter_get_basic(&value, device);
printf("Device: %s\n", *device);
}
dbus_message_iter_next(&dict);
}
return 0;
}
static DBusMessage *chr_read_value(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct characteristic *chr = user_data;
DBusMessage *reply;
DBusMessageIter iter;
const char *device;
if (!dbus_message_iter_init(msg, &iter))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
if (parse_options(&iter, &device))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
reply = dbus_message_new_method_return(msg);
if (!reply)
return g_dbus_create_error(msg, DBUS_ERROR_NO_MEMORY,
"No Memory");
dbus_message_iter_init_append(reply, &iter);
chr_read(chr, &iter);
return reply;
}
static DBusMessage *chr_write_value(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct characteristic *chr = user_data;
DBusMessageIter iter;
const uint8_t *value;
int len;
const char *device;
dbus_message_iter_init(msg, &iter);
if (parse_value(&iter, &value, &len))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
if (parse_options(&iter, &device))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
chr_write(chr, value, len);
return dbus_message_new_method_return(msg);
}
static DBusMessage *chr_start_notify(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
return g_dbus_create_error(msg, DBUS_ERROR_NOT_SUPPORTED,
"Not Supported");
}
static DBusMessage *chr_stop_notify(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
return g_dbus_create_error(msg, DBUS_ERROR_NOT_SUPPORTED,
"Not Supported");
}
static const GDBusMethodTable chr_methods[] = {
{ GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
GDBUS_ARGS({ "value", "ay" }),
chr_read_value) },
{ GDBUS_ASYNC_METHOD("WriteValue", GDBUS_ARGS({ "value", "ay" },
{ "options", "a{sv}" }),
NULL, chr_write_value) },
{ GDBUS_ASYNC_METHOD("StartNotify", NULL, NULL, chr_start_notify) },
{ GDBUS_METHOD("StopNotify", NULL, NULL, chr_stop_notify) },
{ }
};
static DBusMessage *desc_read_value(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct descriptor *desc = user_data;
DBusMessage *reply;
DBusMessageIter iter;
const char *device;
if (!dbus_message_iter_init(msg, &iter))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
if (parse_options(&iter, &device))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
reply = dbus_message_new_method_return(msg);
if (!reply)
return g_dbus_create_error(msg, DBUS_ERROR_NO_MEMORY,
"No Memory");
dbus_message_iter_init_append(reply, &iter);
desc_read(desc, &iter);
return reply;
}
static DBusMessage *desc_write_value(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
struct descriptor *desc = user_data;
DBusMessageIter iter;
const char *device;
const uint8_t *value;
int len;
if (!dbus_message_iter_init(msg, &iter))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
if (parse_value(&iter, &value, &len))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
if (parse_options(&iter, &device))
return g_dbus_create_error(msg, DBUS_ERROR_INVALID_ARGS,
"Invalid arguments");
desc_write(desc, value, len);
return dbus_message_new_method_return(msg);
}
static const GDBusMethodTable desc_methods[] = {
{ GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
GDBUS_ARGS({ "value", "ay" }),
desc_read_value) },
{ GDBUS_ASYNC_METHOD("WriteValue", GDBUS_ARGS({ "value", "ay" },
{ "options", "a{sv}" }),
NULL, desc_write_value) },
{ }
};
static gboolean register_characteristic(const char *chr_uuid,
const uint8_t *value, int vlen,
const char **props,
const char *desc_uuid,
const char **desc_props,
const char *service_path)
{
struct characteristic *chr;
struct descriptor *desc;
static int id = 1;
chr = g_new0(struct characteristic, 1);
chr->uuid = g_strdup(chr_uuid);
chr->value = util_memdup(value, vlen);
chr->vlen = vlen;
chr->props = props;
chr->service = g_strdup(service_path);
chr->path = g_strdup_printf("%s/characteristic%d", service_path, id++);
if (!g_dbus_register_interface(connection, chr->path, GATT_CHR_IFACE,
chr_methods, NULL, chr_properties,
chr, chr_iface_destroy)) {
printf("Couldn't register characteristic interface\n");
chr_iface_destroy(chr);
return FALSE;
}
if (!desc_uuid)
return TRUE;
desc = g_new0(struct descriptor, 1);
desc->uuid = g_strdup(desc_uuid);
desc->chr = chr;
desc->props = desc_props;
desc->path = g_strdup_printf("%s/descriptor%d", chr->path, id++);
if (!g_dbus_register_interface(connection, desc->path,
GATT_DESCRIPTOR_IFACE,
desc_methods, NULL, desc_properties,
desc, desc_iface_destroy)) {
printf("Couldn't register descriptor interface\n");
g_dbus_unregister_interface(connection, chr->path,
GATT_CHR_IFACE);
desc_iface_destroy(desc);
return FALSE;
}
return TRUE;
}
static char *register_service(const char *uuid)
{
static int id = 1;
char *path;
path = g_strdup_printf("/service%d", id++);
if (!g_dbus_register_interface(connection, path, GATT_SERVICE_IFACE,
NULL, NULL, service_properties,
g_strdup(uuid), g_free)) {
printf("Couldn't register service interface\n");
g_free(path);
return NULL;
}
return path;
}
static void create_services_one(void)
{
char *service_path;
uint8_t level = 0;
service_path = register_service(IAS_UUID);
if (!service_path)
return;
/* Add Alert Level Characteristic to Immediate Alert Service */
if (!register_characteristic(ALERT_LEVEL_CHR_UUID,
&level, sizeof(level),
ias_alert_level_props,
READ_WRITE_DESCRIPTOR_UUID,
desc_props,
service_path)) {
printf("Couldn't register Alert Level characteristic (IAS)\n");
g_dbus_unregister_interface(connection, service_path,
GATT_SERVICE_IFACE);
g_free(service_path);
return;
}
services = g_slist_prepend(services, service_path);
printf("Registered service: %s\n", service_path);
}
static void create_services_two(void)
{
char *service_path;
uint8_t level = 0;
service_path = register_service(IAS_UUID2);
if (!service_path)
return;
if (!register_characteristic(ALERT_LEVEL_CHR_UUID2,
&level, sizeof(level),
ias_alert_level_props,
READ_WRITE_DESCRIPTOR_UUID2,
desc_props,
service_path)) {
printf("Couldn't register Alert Level characteristic (IAS)\n");
g_dbus_unregister_interface(connection, service_path,
GATT_SERVICE_IFACE);
g_free(service_path);
return;
}
services = g_slist_prepend(services, service_path);
printf("Registered service: %s\n", service_path);
}
static void create_services_three(void)
{
char *service_path;
uint8_t level = 0;
service_path = register_service(IAS_UUID3);
if (!service_path)
return;
if (!register_characteristic(ALERT_LEVEL_CHR_UUID1,
&level, sizeof(level),
ias_alert_level_props,
READ_WRITE_DESCRIPTOR_UUID1,
desc_props,
service_path)) {
printf("Couldn't register Alert Level characteristic (IAS)\n");
g_dbus_unregister_interface(connection, service_path,
GATT_SERVICE_IFACE);
g_free(service_path);
return;
}
services = g_slist_prepend(services, service_path);
printf("Registered service: %s\n", service_path);
}
static void register_app_reply(DBusMessage *reply, void *user_data)
{
DBusError derr;
dbus_error_init(&derr);
dbus_set_error_from_message(&derr, reply);
if (dbus_error_is_set(&derr))
printf("RegisterApplication: %s\n", derr.message);
else
printf("RegisterApplication: OK\n");
dbus_error_free(&derr);
}
static void register_app_setup(DBusMessageIter *iter, void *user_data)
{
const char *path = "/";
DBusMessageIter dict;
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &dict);
/* TODO: Add options dictionary */
dbus_message_iter_close_container(iter, &dict);
}
static void register_app(GDBusProxy *proxy)
{
if (!g_dbus_proxy_method_call(proxy, "RegisterApplication",
register_app_setup, register_app_reply,
NULL, NULL)) {
printf("Unable to call RegisterApplication\n");
return;
}
}
static void proxy_added_cb(GDBusProxy *proxy, void *user_data)
{
const char *iface;
iface = g_dbus_proxy_get_interface(proxy);
if (g_strcmp0(iface, GATT_MGR_IFACE))
return;
register_app(proxy);
}
static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
static bool __terminated = false;
struct signalfd_siginfo si;
ssize_t result;
int fd;
if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
return FALSE;
fd = g_io_channel_unix_get_fd(channel);
result = read(fd, &si, sizeof(si));
if (result != sizeof(si))
return FALSE;
switch (si.ssi_signo) {
case SIGINT:
case SIGTERM:
if (!__terminated) {
printf("Terminating\n");
g_main_loop_quit(main_loop);
}
__terminated = true;
break;
}
return TRUE;
}
static guint setup_signalfd(void)
{
GIOChannel *channel;
guint source;
sigset_t mask;
int fd;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
perror("Failed to set signal mask");
return 0;
}
fd = signalfd(-1, &mask, 0);
if (fd < 0) {
perror("Failed to create signal descriptor");
return 0;
}
channel = g_io_channel_unix_new(fd);
g_io_channel_set_close_on_unref(channel, TRUE);
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
source = g_io_add_watch(channel,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
signal_handler, NULL);
g_io_channel_unref(channel);
return source;
}
int main(int argc, char *argv[])
{
GDBusClient *client;
guint signal;
signal = setup_signalfd();
if (signal == 0)
return -errno;
connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
main_loop = g_main_loop_new(NULL, FALSE);
g_dbus_attach_object_manager(connection);
printf("gatt-service unique name: %s\n",
dbus_bus_get_unique_name(connection));
create_services_one();
create_services_two();
create_services_three();
client = g_dbus_client_new(connection, "org.bluez", "/");
g_dbus_client_set_proxy_handlers(client, proxy_added_cb, NULL, NULL,
NULL);
g_main_loop_run(main_loop);
g_dbus_client_unref(client);
g_source_remove(signal);
g_slist_free_full(services, g_free);
dbus_connection_unref(connection);
return 0;
}
|