3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <readline/readline.h>
37 #include <readline/history.h>
40 #include "gdbus/gdbus.h"
41 #include "monitor/uuid.h"
45 #define PROFILE_PATH "/org/bluez/profile"
46 #define PROFILE_INTERFACE "org.bluez.GattProfile1"
48 /* String display constants */
49 #define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF
50 #define COLORED_CHG COLOR_YELLOW "CHG" COLOR_OFF
51 #define COLORED_DEL COLOR_RED "DEL" COLOR_OFF
53 static GList *services;
54 static GList *characteristics;
55 static GList *descriptors;
56 static GList *managers;
58 static void print_service(GDBusProxy *proxy, const char *description)
61 const char *uuid, *text;
64 if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
67 dbus_message_iter_get_basic(&iter, &uuid);
69 if (g_dbus_proxy_get_property(proxy, "Primary", &iter) == FALSE)
72 dbus_message_iter_get_basic(&iter, &primary);
74 text = uuidstr_to_str(uuid);
78 rl_printf("%s%s%s%s Service\n\t%s\n\t%s\n",
79 description ? "[" : "",
81 description ? "] " : "",
82 primary ? "Primary" : "Secondary",
83 g_dbus_proxy_get_path(proxy),
87 void gatt_add_service(GDBusProxy *proxy)
89 services = g_list_append(services, proxy);
91 print_service(proxy, COLORED_NEW);
94 void gatt_remove_service(GDBusProxy *proxy)
98 l = g_list_find(services, proxy);
102 services = g_list_delete_link(services, l);
104 print_service(proxy, COLORED_DEL);
107 static void print_characteristic(GDBusProxy *proxy, const char *description)
109 DBusMessageIter iter;
110 const char *uuid, *text;
112 if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
115 dbus_message_iter_get_basic(&iter, &uuid);
117 text = uuidstr_to_str(uuid);
121 rl_printf("%s%s%sCharacteristic\n\t%s\n\t%s\n",
122 description ? "[" : "",
124 description ? "] " : "",
125 g_dbus_proxy_get_path(proxy),
129 static gboolean characteristic_is_child(GDBusProxy *characteristic)
132 DBusMessageIter iter;
133 const char *service, *path;
135 if (!g_dbus_proxy_get_property(characteristic, "Service", &iter))
138 dbus_message_iter_get_basic(&iter, &service);
140 for (l = services; l; l = g_list_next(l)) {
141 GDBusProxy *proxy = l->data;
143 path = g_dbus_proxy_get_path(proxy);
145 if (!strcmp(path, service))
152 void gatt_add_characteristic(GDBusProxy *proxy)
154 if (!characteristic_is_child(proxy))
157 characteristics = g_list_append(characteristics, proxy);
159 print_characteristic(proxy, COLORED_NEW);
162 void gatt_remove_characteristic(GDBusProxy *proxy)
166 l = g_list_find(characteristics, proxy);
170 characteristics = g_list_delete_link(characteristics, l);
172 print_characteristic(proxy, COLORED_DEL);
175 static void print_descriptor(GDBusProxy *proxy, const char *description)
177 DBusMessageIter iter;
178 const char *uuid, *text;
180 if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
183 dbus_message_iter_get_basic(&iter, &uuid);
185 text = uuidstr_to_str(uuid);
189 rl_printf("%s%s%sDescriptor\n\t%s\n\t%s\n",
190 description ? "[" : "",
192 description ? "] " : "",
193 g_dbus_proxy_get_path(proxy),
197 static gboolean descriptor_is_child(GDBusProxy *characteristic)
200 DBusMessageIter iter;
201 const char *service, *path;
203 if (!g_dbus_proxy_get_property(characteristic, "Characteristic", &iter))
206 dbus_message_iter_get_basic(&iter, &service);
208 for (l = characteristics; l; l = g_list_next(l)) {
209 GDBusProxy *proxy = l->data;
211 path = g_dbus_proxy_get_path(proxy);
213 if (!strcmp(path, service))
220 void gatt_add_descriptor(GDBusProxy *proxy)
222 if (!descriptor_is_child(proxy))
225 descriptors = g_list_append(descriptors, proxy);
227 print_descriptor(proxy, COLORED_NEW);
230 void gatt_remove_descriptor(GDBusProxy *proxy)
234 l = g_list_find(descriptors, proxy);
238 descriptors = g_list_delete_link(descriptors, l);
240 print_descriptor(proxy, COLORED_DEL);
243 static void list_attributes(const char *path, GList *source)
247 for (l = source; l; l = g_list_next(l)) {
248 GDBusProxy *proxy = l->data;
249 const char *proxy_path;
251 proxy_path = g_dbus_proxy_get_path(proxy);
253 if (!g_str_has_prefix(proxy_path, path))
256 if (source == services) {
257 print_service(proxy, NULL);
258 list_attributes(proxy_path, characteristics);
259 } else if (source == characteristics) {
260 print_characteristic(proxy, NULL);
261 list_attributes(proxy_path, descriptors);
262 } else if (source == descriptors)
263 print_descriptor(proxy, NULL);
267 void gatt_list_attributes(const char *path)
269 list_attributes(path, services);
272 static GDBusProxy *select_proxy(const char *path, GList *source)
276 for (l = source; l; l = g_list_next(l)) {
277 GDBusProxy *proxy = l->data;
279 if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
286 GDBusProxy *gatt_select_attribute(const char *path)
290 proxy = select_proxy(path, services);
294 proxy = select_proxy(path, characteristics);
298 return select_proxy(path, descriptors);
301 static char *attribute_generator(const char *text, int state, GList *source)
303 static int index, len;
311 for (list = g_list_nth(source, index); list;
312 list = g_list_next(list)) {
313 GDBusProxy *proxy = list->data;
318 path = g_dbus_proxy_get_path(proxy);
320 if (!strncmp(path, text, len))
327 char *gatt_attribute_generator(const char *text, int state)
329 static GList *list = NULL;
339 list1 = g_list_copy(characteristics);
340 list1 = g_list_concat(list1, g_list_copy(descriptors));
342 list = g_list_copy(services);
343 list = g_list_concat(list, list1);
346 return attribute_generator(text, state, list);
349 static void read_reply(DBusMessage *message, void *user_data)
352 DBusMessageIter iter, array;
356 dbus_error_init(&error);
358 if (dbus_set_error_from_message(&error, message) == TRUE) {
359 rl_printf("Failed to read: %s\n", error.name);
360 dbus_error_free(&error);
364 dbus_message_iter_init(message, &iter);
366 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
367 rl_printf("Invalid response to read\n");
371 dbus_message_iter_recurse(&iter, &array);
372 dbus_message_iter_get_fixed_array(&array, &value, &len);
375 rl_printf("Unable to parse value\n");
379 rl_hexdump(value, len);
382 static void read_attribute(GDBusProxy *proxy)
384 if (g_dbus_proxy_method_call(proxy, "ReadValue", NULL, read_reply,
385 NULL, NULL) == FALSE) {
386 rl_printf("Failed to read\n");
390 rl_printf("Attempting to read %s\n", g_dbus_proxy_get_path(proxy));
393 void gatt_read_attribute(GDBusProxy *proxy)
397 iface = g_dbus_proxy_get_interface(proxy);
398 if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
399 !strcmp(iface, "org.bluez.GattDescriptor1")) {
400 read_attribute(proxy);
404 rl_printf("Unable to read attribute %s\n",
405 g_dbus_proxy_get_path(proxy));
408 static void write_reply(DBusMessage *message, void *user_data)
412 dbus_error_init(&error);
414 if (dbus_set_error_from_message(&error, message) == TRUE) {
415 rl_printf("Failed to write: %s\n", error.name);
416 dbus_error_free(&error);
421 static void write_setup(DBusMessageIter *iter, void *user_data)
423 struct iovec *iov = user_data;
424 DBusMessageIter array;
426 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "y", &array);
427 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
428 &iov->iov_base, iov->iov_len);
429 dbus_message_iter_close_container(iter, &array);
432 static void write_attribute(GDBusProxy *proxy, char *arg)
439 for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) {
446 if (i >= G_N_ELEMENTS(value)) {
447 rl_printf("Too much data\n");
451 val = strtol(entry, &endptr, 0);
452 if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
453 rl_printf("Invalid value at index %d\n", i);
460 iov.iov_base = value;
463 if (g_dbus_proxy_method_call(proxy, "WriteValue", write_setup,
464 write_reply, &iov, NULL) == FALSE) {
465 rl_printf("Failed to write\n");
469 rl_printf("Attempting to write %s\n", g_dbus_proxy_get_path(proxy));
472 void gatt_write_attribute(GDBusProxy *proxy, const char *arg)
476 iface = g_dbus_proxy_get_interface(proxy);
477 if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
478 !strcmp(iface, "org.bluez.GattDescriptor1")) {
479 write_attribute(proxy, (char *) arg);
483 rl_printf("Unable to write attribute %s\n",
484 g_dbus_proxy_get_path(proxy));
487 static void notify_reply(DBusMessage *message, void *user_data)
489 bool enable = GPOINTER_TO_UINT(user_data);
492 dbus_error_init(&error);
494 if (dbus_set_error_from_message(&error, message) == TRUE) {
495 rl_printf("Failed to %s notify: %s\n",
496 enable ? "start" : "stop", error.name);
497 dbus_error_free(&error);
501 rl_printf("Notify %s\n", enable == TRUE ? "started" : "stopped");
504 static void notify_attribute(GDBusProxy *proxy, bool enable)
509 method = "StartNotify";
511 method = "StopNotify";
513 if (g_dbus_proxy_method_call(proxy, method, NULL, notify_reply,
514 GUINT_TO_POINTER(enable), NULL) == FALSE) {
515 rl_printf("Failed to %s notify\n", enable ? "start" : "stop");
520 void gatt_notify_attribute(GDBusProxy *proxy, bool enable)
524 iface = g_dbus_proxy_get_interface(proxy);
525 if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
526 notify_attribute(proxy, enable);
530 rl_printf("Unable to notify attribute %s\n",
531 g_dbus_proxy_get_path(proxy));
534 static void register_profile_setup(DBusMessageIter *iter, void *user_data)
536 wordexp_t *w = user_data;
537 DBusMessageIter uuids, opt;
538 const char *path = PROFILE_PATH;
541 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
543 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &uuids);
544 for (i = 0; i < w->we_wordc; i++)
545 dbus_message_iter_append_basic(&uuids, DBUS_TYPE_STRING,
547 dbus_message_iter_close_container(iter, &uuids);
549 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
550 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
551 DBUS_TYPE_STRING_AS_STRING
552 DBUS_TYPE_VARIANT_AS_STRING
553 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
555 dbus_message_iter_close_container(iter, &opt);
559 static void register_profile_reply(DBusMessage *message, void *user_data)
563 dbus_error_init(&error);
565 if (dbus_set_error_from_message(&error, message) == TRUE) {
566 rl_printf("Failed to register profile: %s\n", error.name);
567 dbus_error_free(&error);
571 rl_printf("Profile registered\n");
574 void gatt_add_manager(GDBusProxy *proxy)
576 managers = g_list_append(managers, proxy);
579 void gatt_remove_manager(GDBusProxy *proxy)
581 managers = g_list_remove(managers, proxy);
584 static int match_proxy(const void *a, const void *b)
586 GDBusProxy *proxy1 = (void *) a;
587 GDBusProxy *proxy2 = (void *) b;
589 return strcmp(g_dbus_proxy_get_path(proxy1),
590 g_dbus_proxy_get_path(proxy2));
593 static DBusMessage *release_profile(DBusConnection *conn,
594 DBusMessage *msg, void *user_data)
596 g_dbus_unregister_interface(conn, PROFILE_PATH, PROFILE_INTERFACE);
598 return dbus_message_new_method_return(msg);
601 static const GDBusMethodTable methods[] = {
602 { GDBUS_METHOD("Release", NULL, NULL, release_profile) },
606 void gatt_register_profile(DBusConnection *conn, GDBusProxy *proxy,
611 l = g_list_find_custom(managers, proxy, match_proxy);
613 rl_printf("Unable to find GattManager proxy\n");
617 if (g_dbus_register_interface(conn, PROFILE_PATH,
618 PROFILE_INTERFACE, methods,
619 NULL, NULL, NULL, NULL) == FALSE) {
620 rl_printf("Failed to register profile object\n");
624 if (g_dbus_proxy_method_call(l->data, "RegisterProfile",
625 register_profile_setup,
626 register_profile_reply, w,
628 rl_printf("Failed register profile\n");
633 static void unregister_profile_reply(DBusMessage *message, void *user_data)
635 DBusConnection *conn = user_data;
638 dbus_error_init(&error);
640 if (dbus_set_error_from_message(&error, message) == TRUE) {
641 rl_printf("Failed to unregister profile: %s\n", error.name);
642 dbus_error_free(&error);
646 rl_printf("Profile unregistered\n");
648 g_dbus_unregister_interface(conn, PROFILE_PATH, PROFILE_INTERFACE);
651 static void unregister_profile_setup(DBusMessageIter *iter, void *user_data)
653 const char *path = PROFILE_PATH;
655 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
658 void gatt_unregister_profile(DBusConnection *conn, GDBusProxy *proxy)
662 l = g_list_find_custom(managers, proxy, match_proxy);
664 rl_printf("Unable to find GattManager proxy\n");
668 if (g_dbus_proxy_method_call(l->data, "UnregisterProfile",
669 unregister_profile_setup,
670 unregister_profile_reply, conn,
672 rl_printf("Failed unregister profile\n");