2015-01-06 00:16:21 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2015-02-04 23:08:03 +08:00
|
|
|
#include <sys/uio.h>
|
2015-03-23 23:23:01 +08:00
|
|
|
#include <wordexp.h>
|
2015-01-06 00:16:21 +08:00
|
|
|
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
#include "src/shared/queue.h"
|
2015-03-01 14:21:46 +08:00
|
|
|
#include "gdbus/gdbus.h"
|
2015-01-06 00:16:21 +08:00
|
|
|
#include "monitor/uuid.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "gatt.h"
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
#define APP_PATH "/org/bluez/app"
|
2015-03-23 23:23:01 +08:00
|
|
|
#define PROFILE_INTERFACE "org.bluez.GattProfile1"
|
2017-06-26 17:32:57 +08:00
|
|
|
#define SERVICE_INTERFACE "org.bluez.GattService1"
|
2017-06-27 00:21:29 +08:00
|
|
|
#define CHRC_INTERFACE "org.bluez.GattCharacteristic1"
|
2015-03-23 23:23:01 +08:00
|
|
|
|
2015-01-06 00:16:21 +08:00
|
|
|
/* String display constants */
|
|
|
|
#define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF
|
|
|
|
#define COLORED_CHG COLOR_YELLOW "CHG" COLOR_OFF
|
|
|
|
#define COLORED_DEL COLOR_RED "DEL" COLOR_OFF
|
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
struct chrc {
|
|
|
|
struct service *service;
|
|
|
|
char *path;
|
|
|
|
char *uuid;
|
|
|
|
char **flags;
|
|
|
|
bool notifying;
|
|
|
|
GList *descs;
|
|
|
|
int value_len;
|
|
|
|
uint8_t *value;
|
|
|
|
};
|
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
struct service {
|
|
|
|
DBusConnection *conn;
|
|
|
|
char *path;
|
|
|
|
char *uuid;
|
|
|
|
bool primary;
|
2017-06-27 00:21:29 +08:00
|
|
|
GList *chrcs;
|
2017-06-26 17:32:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static GList *local_services;
|
2015-01-06 00:16:21 +08:00
|
|
|
static GList *services;
|
2015-01-06 00:34:01 +08:00
|
|
|
static GList *characteristics;
|
2015-01-06 00:50:34 +08:00
|
|
|
static GList *descriptors;
|
2015-03-23 23:23:01 +08:00
|
|
|
static GList *managers;
|
2017-05-25 18:31:32 +08:00
|
|
|
static GList *uuids;
|
2015-01-06 00:16:21 +08:00
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
static void print_service(struct service *service, const char *description)
|
|
|
|
{
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
text = uuidstr_to_str(service->uuid);
|
|
|
|
if (!text)
|
|
|
|
rl_printf("%s%s%s%s Service\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
|
|
|
service->primary ? "Primary" :
|
|
|
|
"Secondary",
|
|
|
|
service->path, service->uuid);
|
|
|
|
else
|
|
|
|
rl_printf("%s%s%s%s Service\n\t%s\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
|
|
|
service->primary ? "Primary" :
|
|
|
|
"Secondary",
|
|
|
|
service->path, service->uuid, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_service_proxy(GDBusProxy *proxy, const char *description)
|
2015-01-06 00:16:21 +08:00
|
|
|
{
|
2017-06-26 17:32:57 +08:00
|
|
|
struct service service;
|
2015-01-06 00:16:21 +08:00
|
|
|
DBusMessageIter iter;
|
2017-06-26 17:32:57 +08:00
|
|
|
const char *uuid;
|
2015-01-06 00:16:21 +08:00
|
|
|
dbus_bool_t primary;
|
|
|
|
|
|
|
|
if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &uuid);
|
|
|
|
|
|
|
|
if (g_dbus_proxy_get_property(proxy, "Primary", &iter) == FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &primary);
|
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
service.path = (char *) g_dbus_proxy_get_path(proxy);
|
|
|
|
service.uuid = (char *) uuid;
|
|
|
|
service.primary = primary;
|
|
|
|
|
|
|
|
print_service(&service, description);
|
2015-01-06 00:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_add_service(GDBusProxy *proxy)
|
|
|
|
{
|
|
|
|
services = g_list_append(services, proxy);
|
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
print_service_proxy(proxy, COLORED_NEW);
|
2015-01-06 00:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_remove_service(GDBusProxy *proxy)
|
|
|
|
{
|
2015-11-12 22:13:33 +08:00
|
|
|
GList *l;
|
|
|
|
|
|
|
|
l = g_list_find(services, proxy);
|
|
|
|
if (!l)
|
|
|
|
return;
|
|
|
|
|
|
|
|
services = g_list_delete_link(services, l);
|
2015-01-06 00:16:21 +08:00
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
print_service_proxy(proxy, COLORED_DEL);
|
2015-01-06 00:16:21 +08:00
|
|
|
}
|
2015-01-06 00:34:01 +08:00
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
static void print_chrc(struct chrc *chrc, const char *description)
|
2015-01-06 00:34:01 +08:00
|
|
|
{
|
2017-06-27 00:21:29 +08:00
|
|
|
const char *text;
|
2015-01-06 00:34:01 +08:00
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
text = uuidstr_to_str(chrc->uuid);
|
2015-01-06 00:34:01 +08:00
|
|
|
if (!text)
|
2016-09-20 22:00:49 +08:00
|
|
|
rl_printf("%s%s%sCharacteristic\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
2017-06-27 00:21:29 +08:00
|
|
|
chrc->path, chrc->uuid);
|
2016-09-20 22:00:49 +08:00
|
|
|
else
|
|
|
|
rl_printf("%s%s%sCharacteristic\n\t%s\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
2017-06-27 00:21:29 +08:00
|
|
|
chrc->path, chrc->uuid, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_characteristic(GDBusProxy *proxy, const char *description)
|
|
|
|
{
|
|
|
|
struct chrc chrc;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
const char *uuid;
|
|
|
|
|
|
|
|
if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &uuid);
|
|
|
|
|
|
|
|
chrc.path = (char *) g_dbus_proxy_get_path(proxy);
|
|
|
|
chrc.uuid = (char *) uuid;
|
|
|
|
|
|
|
|
print_chrc(&chrc, description);
|
2015-01-06 00:34:01 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
static gboolean chrc_is_child(GDBusProxy *characteristic)
|
2015-01-06 00:34:01 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
const char *service, *path;
|
|
|
|
|
|
|
|
if (!g_dbus_proxy_get_property(characteristic, "Service", &iter))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &service);
|
|
|
|
|
|
|
|
for (l = services; l; l = g_list_next(l)) {
|
|
|
|
GDBusProxy *proxy = l->data;
|
|
|
|
|
|
|
|
path = g_dbus_proxy_get_path(proxy);
|
|
|
|
|
|
|
|
if (!strcmp(path, service))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_add_characteristic(GDBusProxy *proxy)
|
|
|
|
{
|
2017-06-27 00:21:29 +08:00
|
|
|
if (!chrc_is_child(proxy))
|
2015-01-06 00:34:01 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
characteristics = g_list_append(characteristics, proxy);
|
|
|
|
|
|
|
|
print_characteristic(proxy, COLORED_NEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_remove_characteristic(GDBusProxy *proxy)
|
|
|
|
{
|
2015-11-12 22:13:33 +08:00
|
|
|
GList *l;
|
|
|
|
|
|
|
|
l = g_list_find(characteristics, proxy);
|
|
|
|
if (!l)
|
2015-01-06 00:34:01 +08:00
|
|
|
return;
|
|
|
|
|
2015-11-12 22:13:33 +08:00
|
|
|
characteristics = g_list_delete_link(characteristics, l);
|
2015-01-06 00:34:01 +08:00
|
|
|
|
|
|
|
print_characteristic(proxy, COLORED_DEL);
|
|
|
|
}
|
2015-01-06 00:50:34 +08:00
|
|
|
|
|
|
|
static void print_descriptor(GDBusProxy *proxy, const char *description)
|
|
|
|
{
|
|
|
|
DBusMessageIter iter;
|
|
|
|
const char *uuid, *text;
|
|
|
|
|
|
|
|
if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &uuid);
|
|
|
|
|
|
|
|
text = uuidstr_to_str(uuid);
|
|
|
|
if (!text)
|
2016-09-20 22:00:49 +08:00
|
|
|
rl_printf("%s%s%sDescriptor\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
|
|
|
g_dbus_proxy_get_path(proxy),
|
|
|
|
uuid);
|
|
|
|
else
|
|
|
|
rl_printf("%s%s%sDescriptor\n\t%s\n\t%s\n\t%s\n",
|
|
|
|
description ? "[" : "",
|
|
|
|
description ? : "",
|
|
|
|
description ? "] " : "",
|
|
|
|
g_dbus_proxy_get_path(proxy),
|
|
|
|
uuid, text);
|
2015-01-06 00:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean descriptor_is_child(GDBusProxy *characteristic)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
const char *service, *path;
|
|
|
|
|
|
|
|
if (!g_dbus_proxy_get_property(characteristic, "Characteristic", &iter))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &service);
|
|
|
|
|
|
|
|
for (l = characteristics; l; l = g_list_next(l)) {
|
|
|
|
GDBusProxy *proxy = l->data;
|
|
|
|
|
|
|
|
path = g_dbus_proxy_get_path(proxy);
|
|
|
|
|
|
|
|
if (!strcmp(path, service))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_add_descriptor(GDBusProxy *proxy)
|
|
|
|
{
|
|
|
|
if (!descriptor_is_child(proxy))
|
|
|
|
return;
|
|
|
|
|
|
|
|
descriptors = g_list_append(descriptors, proxy);
|
|
|
|
|
|
|
|
print_descriptor(proxy, COLORED_NEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_remove_descriptor(GDBusProxy *proxy)
|
|
|
|
{
|
2015-11-12 22:13:33 +08:00
|
|
|
GList *l;
|
|
|
|
|
|
|
|
l = g_list_find(descriptors, proxy);
|
|
|
|
if (!l)
|
2015-01-06 00:50:34 +08:00
|
|
|
return;
|
|
|
|
|
2015-11-12 22:13:33 +08:00
|
|
|
descriptors = g_list_delete_link(descriptors, l);
|
2015-01-06 00:50:34 +08:00
|
|
|
|
|
|
|
print_descriptor(proxy, COLORED_DEL);
|
|
|
|
}
|
2015-02-03 20:38:46 +08:00
|
|
|
|
|
|
|
static void list_attributes(const char *path, GList *source)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = source; l; l = g_list_next(l)) {
|
|
|
|
GDBusProxy *proxy = l->data;
|
|
|
|
const char *proxy_path;
|
|
|
|
|
|
|
|
proxy_path = g_dbus_proxy_get_path(proxy);
|
|
|
|
|
|
|
|
if (!g_str_has_prefix(proxy_path, path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (source == services) {
|
2017-06-26 17:32:57 +08:00
|
|
|
print_service_proxy(proxy, NULL);
|
2015-02-03 20:38:46 +08:00
|
|
|
list_attributes(proxy_path, characteristics);
|
|
|
|
} else if (source == characteristics) {
|
|
|
|
print_characteristic(proxy, NULL);
|
|
|
|
list_attributes(proxy_path, descriptors);
|
|
|
|
} else if (source == descriptors)
|
|
|
|
print_descriptor(proxy, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_list_attributes(const char *path)
|
|
|
|
{
|
|
|
|
list_attributes(path, services);
|
|
|
|
}
|
2015-02-05 21:49:11 +08:00
|
|
|
|
|
|
|
static GDBusProxy *select_proxy(const char *path, GList *source)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = source; l; l = g_list_next(l)) {
|
|
|
|
GDBusProxy *proxy = l->data;
|
|
|
|
|
|
|
|
if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:09:06 +08:00
|
|
|
static GDBusProxy *select_attribute(const char *path)
|
2015-02-05 21:49:11 +08:00
|
|
|
{
|
|
|
|
GDBusProxy *proxy;
|
|
|
|
|
|
|
|
proxy = select_proxy(path, services);
|
|
|
|
if (proxy)
|
|
|
|
return proxy;
|
|
|
|
|
|
|
|
proxy = select_proxy(path, characteristics);
|
|
|
|
if (proxy)
|
|
|
|
return proxy;
|
|
|
|
|
|
|
|
return select_proxy(path, descriptors);
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
static GDBusProxy *select_proxy_by_uuid(GDBusProxy *parent, const char *uuid,
|
|
|
|
GList *source)
|
2017-03-14 19:09:06 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
const char *value;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
|
|
|
|
for (l = source; l; l = g_list_next(l)) {
|
|
|
|
GDBusProxy *proxy = l->data;
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
if (parent && !g_str_has_prefix(g_dbus_proxy_get_path(proxy),
|
|
|
|
g_dbus_proxy_get_path(parent)))
|
|
|
|
continue;
|
|
|
|
|
2017-03-14 19:09:06 +08:00
|
|
|
if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &value);
|
|
|
|
|
|
|
|
if (strcasecmp(uuid, value) == 0)
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
static GDBusProxy *select_attribute_by_uuid(GDBusProxy *parent,
|
|
|
|
const char *uuid)
|
2017-03-14 19:09:06 +08:00
|
|
|
{
|
|
|
|
GDBusProxy *proxy;
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
proxy = select_proxy_by_uuid(parent, uuid, services);
|
2017-03-14 19:09:06 +08:00
|
|
|
if (proxy)
|
|
|
|
return proxy;
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
proxy = select_proxy_by_uuid(parent, uuid, characteristics);
|
2017-03-14 19:09:06 +08:00
|
|
|
if (proxy)
|
|
|
|
return proxy;
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
return select_proxy_by_uuid(parent, uuid, descriptors);
|
2017-03-14 19:09:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *arg)
|
2017-03-14 19:09:06 +08:00
|
|
|
{
|
|
|
|
if (arg[0] == '/')
|
|
|
|
return select_attribute(arg);
|
|
|
|
|
2017-03-14 19:46:21 +08:00
|
|
|
if (parent) {
|
|
|
|
GDBusProxy *proxy = select_attribute_by_uuid(parent, arg);
|
|
|
|
if (proxy)
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return select_attribute_by_uuid(parent, arg);
|
2017-03-14 19:09:06 +08:00
|
|
|
}
|
|
|
|
|
2015-02-05 21:49:11 +08:00
|
|
|
static char *attribute_generator(const char *text, int state, GList *source)
|
|
|
|
{
|
|
|
|
static int index, len;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
index = 0;
|
|
|
|
len = strlen(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list = g_list_nth(source, index); list;
|
|
|
|
list = g_list_next(list)) {
|
|
|
|
GDBusProxy *proxy = list->data;
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
path = g_dbus_proxy_get_path(proxy);
|
|
|
|
|
|
|
|
if (!strncmp(path, text, len))
|
|
|
|
return strdup(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *gatt_attribute_generator(const char *text, int state)
|
|
|
|
{
|
|
|
|
static GList *list = NULL;
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
GList *list1;
|
|
|
|
|
|
|
|
if (list) {
|
|
|
|
g_list_free(list);
|
|
|
|
list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list1 = g_list_copy(characteristics);
|
|
|
|
list1 = g_list_concat(list1, g_list_copy(descriptors));
|
|
|
|
|
|
|
|
list = g_list_copy(services);
|
|
|
|
list = g_list_concat(list, list1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return attribute_generator(text, state, list);
|
|
|
|
}
|
2015-02-04 20:57:51 +08:00
|
|
|
|
|
|
|
static void read_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusError error;
|
|
|
|
DBusMessageIter iter, array;
|
|
|
|
uint8_t *value;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
|
|
|
rl_printf("Failed to read: %s\n", error.name);
|
|
|
|
dbus_error_free(&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_iter_init(message, &iter);
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
|
|
|
|
rl_printf("Invalid response to read\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&iter, &array);
|
|
|
|
dbus_message_iter_get_fixed_array(&array, &value, &len);
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
rl_printf("Unable to parse value\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_hexdump(value, len);
|
|
|
|
}
|
|
|
|
|
2016-05-08 22:24:14 +08:00
|
|
|
static void read_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);
|
|
|
|
/* TODO: Add offset support */
|
|
|
|
dbus_message_iter_close_container(iter, &dict);
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:57:51 +08:00
|
|
|
static void read_attribute(GDBusProxy *proxy)
|
|
|
|
{
|
2016-05-08 22:24:14 +08:00
|
|
|
if (g_dbus_proxy_method_call(proxy, "ReadValue", read_setup, read_reply,
|
2015-02-04 20:57:51 +08:00
|
|
|
NULL, NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to read\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Attempting to read %s\n", g_dbus_proxy_get_path(proxy));
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_read_attribute(GDBusProxy *proxy)
|
|
|
|
{
|
|
|
|
const char *iface;
|
|
|
|
|
|
|
|
iface = g_dbus_proxy_get_interface(proxy);
|
|
|
|
if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
|
|
|
|
!strcmp(iface, "org.bluez.GattDescriptor1")) {
|
|
|
|
read_attribute(proxy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Unable to read attribute %s\n",
|
|
|
|
g_dbus_proxy_get_path(proxy));
|
|
|
|
}
|
2015-02-04 23:08:03 +08:00
|
|
|
|
|
|
|
static void write_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
|
|
|
rl_printf("Failed to write: %s\n", error.name);
|
|
|
|
dbus_error_free(&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
struct iovec *iov = user_data;
|
2016-05-08 22:24:14 +08:00
|
|
|
DBusMessageIter array, dict;
|
2015-02-04 23:08:03 +08:00
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "y", &array);
|
|
|
|
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
|
|
|
|
&iov->iov_base, iov->iov_len);
|
|
|
|
dbus_message_iter_close_container(iter, &array);
|
2016-05-08 22:24:14 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
/* TODO: Add offset support */
|
|
|
|
dbus_message_iter_close_container(iter, &dict);
|
2015-02-04 23:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_attribute(GDBusProxy *proxy, char *arg)
|
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
uint8_t value[512];
|
|
|
|
char *entry;
|
2015-07-08 03:20:33 +08:00
|
|
|
unsigned int i;
|
2015-02-04 23:08:03 +08:00
|
|
|
|
|
|
|
for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) {
|
|
|
|
long int val;
|
|
|
|
char *endptr = NULL;
|
|
|
|
|
|
|
|
if (*entry == '\0')
|
|
|
|
continue;
|
|
|
|
|
2015-07-08 03:20:33 +08:00
|
|
|
if (i >= G_N_ELEMENTS(value)) {
|
2015-02-04 23:08:03 +08:00
|
|
|
rl_printf("Too much data\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = strtol(entry, &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
|
|
|
|
rl_printf("Invalid value at index %d\n", i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[i] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov.iov_base = value;
|
|
|
|
iov.iov_len = i;
|
|
|
|
|
|
|
|
if (g_dbus_proxy_method_call(proxy, "WriteValue", write_setup,
|
|
|
|
write_reply, &iov, NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to write\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Attempting to write %s\n", g_dbus_proxy_get_path(proxy));
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_write_attribute(GDBusProxy *proxy, const char *arg)
|
|
|
|
{
|
|
|
|
const char *iface;
|
|
|
|
|
|
|
|
iface = g_dbus_proxy_get_interface(proxy);
|
|
|
|
if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
|
|
|
|
!strcmp(iface, "org.bluez.GattDescriptor1")) {
|
|
|
|
write_attribute(proxy, (char *) arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Unable to write attribute %s\n",
|
|
|
|
g_dbus_proxy_get_path(proxy));
|
|
|
|
}
|
2015-02-05 23:24:27 +08:00
|
|
|
|
|
|
|
static void notify_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
bool enable = GPOINTER_TO_UINT(user_data);
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
|
|
|
rl_printf("Failed to %s notify: %s\n",
|
|
|
|
enable ? "start" : "stop", error.name);
|
|
|
|
dbus_error_free(&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Notify %s\n", enable == TRUE ? "started" : "stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void notify_attribute(GDBusProxy *proxy, bool enable)
|
|
|
|
{
|
|
|
|
const char *method;
|
|
|
|
|
|
|
|
if (enable == TRUE)
|
|
|
|
method = "StartNotify";
|
|
|
|
else
|
|
|
|
method = "StopNotify";
|
|
|
|
|
|
|
|
if (g_dbus_proxy_method_call(proxy, method, NULL, notify_reply,
|
|
|
|
GUINT_TO_POINTER(enable), NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to %s notify\n", enable ? "start" : "stop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_notify_attribute(GDBusProxy *proxy, bool enable)
|
|
|
|
{
|
|
|
|
const char *iface;
|
|
|
|
|
|
|
|
iface = g_dbus_proxy_get_interface(proxy);
|
|
|
|
if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
|
|
|
|
notify_attribute(proxy, enable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rl_printf("Unable to notify attribute %s\n",
|
|
|
|
g_dbus_proxy_get_path(proxy));
|
|
|
|
}
|
2015-03-23 23:23:01 +08:00
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
static void register_app_setup(DBusMessageIter *iter, void *user_data)
|
2015-03-23 23:23:01 +08:00
|
|
|
{
|
2017-05-25 18:31:32 +08:00
|
|
|
DBusMessageIter opt;
|
|
|
|
const char *path = "/";
|
2015-03-23 23:23:01 +08:00
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
|
|
|
|
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,
|
|
|
|
&opt);
|
|
|
|
dbus_message_iter_close_container(iter, &opt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
static void register_app_reply(DBusMessage *message, void *user_data)
|
2015-03-23 23:23:01 +08:00
|
|
|
{
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
2017-05-25 18:31:32 +08:00
|
|
|
rl_printf("Failed to register application: %s\n", error.name);
|
2015-03-23 23:23:01 +08:00
|
|
|
dbus_error_free(&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
rl_printf("Application registered\n");
|
2015-03-23 23:23:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_add_manager(GDBusProxy *proxy)
|
|
|
|
{
|
|
|
|
managers = g_list_append(managers, proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_remove_manager(GDBusProxy *proxy)
|
|
|
|
{
|
|
|
|
managers = g_list_remove(managers, proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match_proxy(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
GDBusProxy *proxy1 = (void *) a;
|
|
|
|
GDBusProxy *proxy2 = (void *) b;
|
|
|
|
|
|
|
|
return strcmp(g_dbus_proxy_get_path(proxy1),
|
|
|
|
g_dbus_proxy_get_path(proxy2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *release_profile(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
2017-05-25 18:31:32 +08:00
|
|
|
g_dbus_unregister_interface(conn, APP_PATH, PROFILE_INTERFACE);
|
2015-03-23 23:23:01 +08:00
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable methods[] = {
|
|
|
|
{ GDBUS_METHOD("Release", NULL, NULL, release_profile) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
static gboolean get_uuids(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
DBusMessageIter entry;
|
|
|
|
GList *uuid;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
|
|
|
|
DBUS_TYPE_STRING_AS_STRING, &entry);
|
|
|
|
|
|
|
|
for (uuid = uuids; uuid; uuid = g_list_next(uuid->next))
|
|
|
|
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
|
|
|
|
&uuid->data);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &entry);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusPropertyTable properties[] = {
|
|
|
|
{ "UUIDs", "as", get_uuids },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
void gatt_register_app(DBusConnection *conn, GDBusProxy *proxy, wordexp_t *w)
|
2015-03-23 23:23:01 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
2017-05-25 18:31:32 +08:00
|
|
|
unsigned int i;
|
2015-03-23 23:23:01 +08:00
|
|
|
|
|
|
|
l = g_list_find_custom(managers, proxy, match_proxy);
|
|
|
|
if (!l) {
|
|
|
|
rl_printf("Unable to find GattManager proxy\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
for (i = 0; i < w->we_wordc; i++)
|
|
|
|
uuids = g_list_append(uuids, g_strdup(w->we_wordv[i]));
|
|
|
|
|
2017-06-26 17:38:34 +08:00
|
|
|
if (uuids) {
|
|
|
|
if (g_dbus_register_interface(conn, APP_PATH,
|
|
|
|
PROFILE_INTERFACE, methods,
|
|
|
|
NULL, properties, NULL,
|
|
|
|
NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to register application object\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
if (g_dbus_proxy_method_call(l->data, "RegisterApplication",
|
|
|
|
register_app_setup,
|
|
|
|
register_app_reply, w,
|
2015-03-23 23:23:01 +08:00
|
|
|
NULL) == FALSE) {
|
2017-05-25 18:31:32 +08:00
|
|
|
rl_printf("Failed register application\n");
|
|
|
|
g_dbus_unregister_interface(conn, APP_PATH, PROFILE_INTERFACE);
|
2015-03-23 23:23:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 23:44:47 +08:00
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
static void unregister_app_reply(DBusMessage *message, void *user_data)
|
2015-03-23 23:44:47 +08:00
|
|
|
{
|
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
2017-05-25 18:31:32 +08:00
|
|
|
rl_printf("Failed to unregister application: %s\n", error.name);
|
2015-03-23 23:44:47 +08:00
|
|
|
dbus_error_free(&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 17:38:34 +08:00
|
|
|
rl_printf("Application unregistered\n");
|
|
|
|
|
|
|
|
if (!uuids)
|
|
|
|
return;
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
g_list_free_full(uuids, g_free);
|
|
|
|
uuids = NULL;
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(conn, APP_PATH, PROFILE_INTERFACE);
|
2015-03-23 23:44:47 +08:00
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
static void unregister_app_setup(DBusMessageIter *iter, void *user_data)
|
2015-03-23 23:44:47 +08:00
|
|
|
{
|
2017-05-25 18:31:32 +08:00
|
|
|
const char *path = "/";
|
2015-03-23 23:44:47 +08:00
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
void gatt_unregister_app(DBusConnection *conn, GDBusProxy *proxy)
|
2015-03-23 23:44:47 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
l = g_list_find_custom(managers, proxy, match_proxy);
|
|
|
|
if (!l) {
|
|
|
|
rl_printf("Unable to find GattManager proxy\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:32 +08:00
|
|
|
if (g_dbus_proxy_method_call(l->data, "UnregisterApplication",
|
|
|
|
unregister_app_setup,
|
|
|
|
unregister_app_reply, conn,
|
2015-03-23 23:44:47 +08:00
|
|
|
NULL) == FALSE) {
|
|
|
|
rl_printf("Failed unregister profile\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 17:32:57 +08:00
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
static void chrc_free(void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
|
|
|
|
g_free(chrc->path);
|
|
|
|
g_free(chrc->uuid);
|
|
|
|
g_strfreev(chrc->flags);
|
|
|
|
g_free(chrc->value);
|
|
|
|
g_free(chrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chrc_unregister(void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
|
|
|
|
print_chrc(chrc, COLORED_DEL);
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(chrc->service->conn, chrc->path,
|
|
|
|
CHRC_INTERFACE);
|
|
|
|
}
|
|
|
|
|
2017-06-26 17:32:57 +08:00
|
|
|
static void service_free(void *data)
|
|
|
|
{
|
|
|
|
struct service *service = data;
|
|
|
|
|
2017-06-27 00:21:29 +08:00
|
|
|
g_list_free_full(service->chrcs, chrc_unregister);
|
2017-06-26 17:32:57 +08:00
|
|
|
g_free(service->path);
|
|
|
|
g_free(service->uuid);
|
|
|
|
g_free(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean service_get_uuid(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct service *service = data;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &service->uuid);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean service_get_primary(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct service *service = data;
|
|
|
|
dbus_bool_t primary;
|
|
|
|
|
|
|
|
primary = service->primary ? TRUE : FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &primary);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusPropertyTable service_properties[] = {
|
|
|
|
{ "UUID", "s", service_get_uuid },
|
|
|
|
{ "Primary", "b", service_get_primary },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void service_set_primary(const char *input, void *user_data)
|
|
|
|
{
|
|
|
|
struct service *service = user_data;
|
|
|
|
|
|
|
|
if (!strcmp(input, "yes"))
|
|
|
|
service->primary = true;
|
|
|
|
else if (!strcmp(input, "no")) {
|
|
|
|
service->primary = false;
|
|
|
|
} else {
|
|
|
|
rl_printf("Invalid option: %s\n", input);
|
|
|
|
local_services = g_list_remove(local_services, service);
|
|
|
|
print_service(service, COLORED_DEL);
|
|
|
|
g_dbus_unregister_interface(service->conn, service->path,
|
|
|
|
SERVICE_INTERFACE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_register_service(DBusConnection *conn, GDBusProxy *proxy,
|
|
|
|
wordexp_t *w)
|
|
|
|
{
|
|
|
|
struct service *service;
|
|
|
|
bool primary = true;
|
|
|
|
|
|
|
|
service = g_new0(struct service, 1);
|
|
|
|
service->conn = conn;
|
|
|
|
service->uuid = g_strdup(w->we_wordv[0]);
|
|
|
|
service->path = g_strdup_printf("%s/service%p", APP_PATH, service);
|
|
|
|
service->primary = primary;
|
|
|
|
|
|
|
|
if (g_dbus_register_interface(conn, service->path,
|
|
|
|
SERVICE_INTERFACE, NULL, NULL,
|
|
|
|
service_properties, service,
|
|
|
|
service_free) == FALSE) {
|
|
|
|
rl_printf("Failed to register service object\n");
|
|
|
|
service_free(service);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:08:06 +08:00
|
|
|
print_service(service, COLORED_NEW);
|
2017-06-26 17:32:57 +08:00
|
|
|
|
|
|
|
local_services = g_list_append(local_services, service);
|
|
|
|
|
|
|
|
rl_prompt_input(service->path, "Primary (yes/no):", service_set_primary,
|
|
|
|
service);
|
|
|
|
}
|
2017-06-26 18:08:06 +08:00
|
|
|
|
|
|
|
static struct service *service_find(const char *pattern)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = local_services; l; l = g_list_next(l)) {
|
|
|
|
struct service *service = l->data;
|
|
|
|
|
|
|
|
/* match object path */
|
|
|
|
if (!strcmp(service->path, pattern))
|
|
|
|
return service;
|
|
|
|
|
|
|
|
/* match UUID */
|
|
|
|
if (!strcmp(service->uuid, pattern))
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_unregister_service(DBusConnection *conn, GDBusProxy *proxy,
|
|
|
|
wordexp_t *w)
|
|
|
|
{
|
|
|
|
struct service *service;
|
|
|
|
|
|
|
|
service = service_find(w->we_wordv[0]);
|
|
|
|
if (!service) {
|
|
|
|
rl_printf("Failed to unregister service object\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_services = g_list_remove(local_services, service);
|
|
|
|
|
|
|
|
print_service(service, COLORED_DEL);
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(service->conn, service->path,
|
|
|
|
SERVICE_INTERFACE);
|
|
|
|
}
|
2017-06-27 00:21:29 +08:00
|
|
|
|
|
|
|
static gboolean chrc_get_uuid(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &chrc->uuid);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean chrc_get_service(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
|
|
|
|
&chrc->service->path);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean chrc_get_value(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
DBusMessageIter array;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "y", &array);
|
|
|
|
|
|
|
|
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
|
|
|
|
&chrc->value, chrc->value_len);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &array);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean chrc_get_notifying(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
dbus_bool_t value;
|
|
|
|
|
|
|
|
value = chrc->notifying ? TRUE : FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &value);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean chrc_get_flags(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = data;
|
|
|
|
int i;
|
|
|
|
DBusMessageIter array;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &array);
|
|
|
|
|
|
|
|
for (i = 0; chrc->flags[i]; i++)
|
|
|
|
dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
|
|
|
|
&chrc->flags[i]);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &array);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusPropertyTable chrc_properties[] = {
|
|
|
|
{ "UUID", "s", chrc_get_uuid, NULL, NULL },
|
|
|
|
{ "Service", "o", chrc_get_service, NULL, NULL },
|
|
|
|
{ "Value", "ay", chrc_get_value, NULL, NULL },
|
|
|
|
{ "Notifying", "b", chrc_get_notifying, NULL, NULL },
|
|
|
|
{ "Flags", "as", chrc_get_flags, NULL, NULL },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static DBusMessage *read_value(DBusMessage *msg, uint8_t *value,
|
|
|
|
uint16_t value_len)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
DBusMessageIter iter, array;
|
|
|
|
|
|
|
|
reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
dbus_message_iter_init_append(reply, &iter);
|
|
|
|
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "y", &array);
|
|
|
|
dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
|
|
|
|
&value, value_len);
|
|
|
|
dbus_message_iter_close_container(&iter, &array);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *chrc_read_value(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = user_data;
|
|
|
|
|
|
|
|
return read_value(msg, chrc->value, chrc->value_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_value_arg(DBusMessageIter *iter, 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 DBusMessage *chrc_write_value(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = user_data;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
|
|
|
|
dbus_message_iter_init(msg, &iter);
|
|
|
|
|
|
|
|
if (parse_value_arg(&iter, &chrc->value, &chrc->value_len))
|
|
|
|
return g_dbus_create_error(msg,
|
|
|
|
"org.bluez.Error.InvalidArguments",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
rl_printf("[" COLORED_CHG "] Attribute %s written" , chrc->path);
|
|
|
|
|
|
|
|
g_dbus_emit_property_changed(conn, chrc->path, CHRC_INTERFACE, "Value");
|
|
|
|
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *chrc_start_notify(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = user_data;
|
|
|
|
|
|
|
|
if (!chrc->notifying)
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
chrc->notifying = true;
|
|
|
|
rl_printf("[" COLORED_CHG "] Attribute %s notifications enabled",
|
|
|
|
chrc->path);
|
|
|
|
g_dbus_emit_property_changed(conn, chrc->path, CHRC_INTERFACE,
|
|
|
|
"Notifying");
|
|
|
|
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *chrc_stop_notify(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = user_data;
|
|
|
|
|
|
|
|
if (chrc->notifying)
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
chrc->notifying = false;
|
|
|
|
rl_printf("[" COLORED_CHG "] Attribute %s notifications disabled",
|
|
|
|
chrc->path);
|
|
|
|
g_dbus_emit_property_changed(conn, chrc->path, CHRC_INTERFACE,
|
|
|
|
"Notifying");
|
|
|
|
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable chrc_methods[] = {
|
|
|
|
{ GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
|
|
|
|
GDBUS_ARGS({ "value", "ay" }),
|
|
|
|
chrc_read_value) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("WriteValue", GDBUS_ARGS({ "value", "ay" },
|
|
|
|
{ "options", "a{sv}" }),
|
|
|
|
NULL, chrc_write_value) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("StartNotify", NULL, NULL, chrc_start_notify) },
|
|
|
|
{ GDBUS_METHOD("StopNotify", NULL, NULL, chrc_stop_notify) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void chrc_set_value(const char *input, void *user_data)
|
|
|
|
{
|
|
|
|
struct chrc *chrc = user_data;
|
|
|
|
uint8_t value[512];
|
|
|
|
char *entry;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
g_free(chrc->value);
|
|
|
|
|
|
|
|
for (i = 0; (entry = strsep((char **)&input, " \t")) != NULL; i++) {
|
|
|
|
long int val;
|
|
|
|
char *endptr = NULL;
|
|
|
|
|
|
|
|
if (*entry == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i >= G_N_ELEMENTS(value)) {
|
|
|
|
rl_printf("Too much data\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = strtol(entry, &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
|
|
|
|
rl_printf("Invalid value at index %d\n", i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[i] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrc->value_len = i;
|
|
|
|
chrc->value = g_memdup(value, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gatt_register_chrc(DBusConnection *conn, GDBusProxy *proxy, wordexp_t *w)
|
|
|
|
{
|
|
|
|
struct service *service;
|
|
|
|
struct chrc *chrc;
|
|
|
|
|
|
|
|
if (!local_services) {
|
|
|
|
rl_printf("No service registered\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
service = g_list_last(local_services)->data;
|
|
|
|
|
|
|
|
chrc = g_new0(struct chrc, 1);
|
|
|
|
chrc->service = service;
|
|
|
|
chrc->uuid = g_strdup(w->we_wordv[0]);
|
|
|
|
chrc->path = g_strdup_printf("%s/chrc%p", service->path, chrc);
|
|
|
|
chrc->flags = g_strsplit(w->we_wordv[1], ",", -1);
|
|
|
|
|
|
|
|
if (g_dbus_register_interface(conn, chrc->path, CHRC_INTERFACE,
|
|
|
|
chrc_methods, NULL, chrc_properties,
|
|
|
|
chrc, chrc_free) == FALSE) {
|
|
|
|
rl_printf("Failed to register characteristic object\n");
|
|
|
|
chrc_free(chrc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
service->chrcs = g_list_append(service->chrcs, chrc);
|
|
|
|
|
|
|
|
print_chrc(chrc, COLORED_NEW);
|
|
|
|
|
|
|
|
rl_prompt_input(chrc->path, "Enter value:", chrc_set_value, chrc);
|
|
|
|
}
|