2016-08-09 19:32:46 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 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 <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2017-08-09 16:34:12 +08:00
|
|
|
#include <stdbool.h>
|
2016-08-09 19:32:46 +08:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
|
|
|
|
#include "gdbus/gdbus.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "advertising.h"
|
|
|
|
|
|
|
|
#define AD_PATH "/org/bluez/advertising"
|
|
|
|
#define AD_IFACE "org.bluez.LEAdvertisement1"
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
struct ad_data {
|
|
|
|
uint8_t data[25];
|
|
|
|
uint8_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct service_data {
|
|
|
|
char *uuid;
|
|
|
|
struct ad_data data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct manufacturer_data {
|
|
|
|
uint16_t id;
|
|
|
|
struct ad_data data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ad {
|
|
|
|
bool registered;
|
|
|
|
char *type;
|
2017-08-10 17:30:57 +08:00
|
|
|
char *local_name;
|
2017-08-10 19:15:49 +08:00
|
|
|
uint16_t local_appearance;
|
2017-08-09 16:34:12 +08:00
|
|
|
char **uuids;
|
|
|
|
size_t uuids_len;
|
|
|
|
struct service_data service;
|
|
|
|
struct manufacturer_data manufacturer;
|
|
|
|
bool tx_power;
|
|
|
|
bool name;
|
|
|
|
bool appearance;
|
2017-08-10 19:15:49 +08:00
|
|
|
} ad = {
|
|
|
|
.local_appearance = UINT16_MAX,
|
|
|
|
};
|
2016-08-09 19:32:46 +08:00
|
|
|
|
|
|
|
static void ad_release(DBusConnection *conn)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.registered = false;
|
2016-08-09 19:32:46 +08:00
|
|
|
|
|
|
|
g_dbus_unregister_interface(conn, AD_PATH, AD_IFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *release_advertising(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
rl_printf("Advertising released\n");
|
|
|
|
|
|
|
|
ad_release(conn);
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable ad_methods[] = {
|
|
|
|
{ GDBUS_METHOD("Release", NULL, NULL, release_advertising) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter dict;
|
|
|
|
const char *path = AD_PATH;
|
|
|
|
|
|
|
|
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, &dict);
|
|
|
|
dbus_message_iter_close_container(iter, &dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == FALSE) {
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.registered = true;
|
2016-08-09 19:32:46 +08:00
|
|
|
rl_printf("Advertising object registered\n");
|
|
|
|
} else {
|
|
|
|
rl_printf("Failed to register advertisement: %s\n", error.name);
|
|
|
|
dbus_error_free(&error);
|
|
|
|
|
|
|
|
if (g_dbus_unregister_interface(conn, AD_PATH,
|
|
|
|
AD_IFACE) == FALSE)
|
|
|
|
rl_printf("Failed to unregister advertising object\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_type(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
const char *type = "peripheral";
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
if (!ad.type || strlen(ad.type) > 0)
|
|
|
|
type = ad.type;
|
2016-08-09 19:32:46 +08:00
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &type);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:16:58 +08:00
|
|
|
static gboolean uuids_exists(const GDBusPropertyTable *property, void *data)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
return ad.uuids_len != 0;
|
2016-08-09 20:16:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_uuids(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter array;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "as", &array);
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
for (i = 0; i < ad.uuids_len; i++)
|
2016-08-09 20:16:58 +08:00
|
|
|
dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
|
2017-08-09 16:34:12 +08:00
|
|
|
&ad.uuids[i]);
|
2016-08-09 20:16:58 +08:00
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &array);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:55:46 +08:00
|
|
|
static void append_array_variant(DBusMessageIter *iter, int type, void *val,
|
|
|
|
int n_elements)
|
|
|
|
{
|
|
|
|
DBusMessageIter variant, array;
|
|
|
|
char type_sig[2] = { type, '\0' };
|
|
|
|
char array_sig[3] = { DBUS_TYPE_ARRAY, type, '\0' };
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
|
|
|
|
array_sig, &variant);
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
|
|
|
|
type_sig, &array);
|
|
|
|
|
|
|
|
if (dbus_type_is_fixed(type) == TRUE) {
|
|
|
|
dbus_message_iter_append_fixed_array(&array, type, val,
|
|
|
|
n_elements);
|
|
|
|
} else if (type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) {
|
|
|
|
const char ***str_array = val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_elements; i++)
|
|
|
|
dbus_message_iter_append_basic(&array, type,
|
|
|
|
&((*str_array)[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(&variant, &array);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &variant);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dict_append_basic_array(DBusMessageIter *dict, int key_type,
|
|
|
|
const void *key, int type, void *val,
|
|
|
|
int n_elements)
|
|
|
|
{
|
|
|
|
DBusMessageIter entry;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
|
|
|
|
NULL, &entry);
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(&entry, key_type, key);
|
|
|
|
|
|
|
|
append_array_variant(&entry, type, val, n_elements);
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(dict, &entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dict_append_array(DBusMessageIter *dict, const char *key, int type,
|
|
|
|
void *val, int n_elements)
|
|
|
|
{
|
|
|
|
dict_append_basic_array(dict, DBUS_TYPE_STRING, &key, type, val,
|
|
|
|
n_elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean service_data_exists(const GDBusPropertyTable *property,
|
|
|
|
void *data)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
return ad.service.uuid != NULL;
|
2016-08-10 18:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_service_data(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter dict;
|
2017-08-09 16:34:12 +08:00
|
|
|
struct ad_data *data = &ad.service.data;
|
2016-08-10 18:55:46 +08:00
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &dict);
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
dict_append_array(&dict, ad.service.uuid, DBUS_TYPE_BYTE, &data->data,
|
|
|
|
data->len);
|
2016-08-10 18:55:46 +08:00
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &dict);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-10 20:15:46 +08:00
|
|
|
static gboolean manufacturer_data_exists(const GDBusPropertyTable *property,
|
|
|
|
void *data)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
return ad.manufacturer.id != 0;
|
2016-08-10 20:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_manufacturer_data(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter dict;
|
2017-08-09 16:34:12 +08:00
|
|
|
struct ad_data *data = &ad.manufacturer.data;
|
2016-08-10 20:15:46 +08:00
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{qv}", &dict);
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
dict_append_basic_array(&dict, DBUS_TYPE_UINT16, &ad.manufacturer.id,
|
|
|
|
DBUS_TYPE_BYTE, &data->data, data->len);
|
2016-08-10 20:15:46 +08:00
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &dict);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
static gboolean includes_exists(const GDBusPropertyTable *property, void *data)
|
2016-08-11 18:38:36 +08:00
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
return ad.tx_power || ad.name || ad.appearance;
|
2016-08-11 18:38:36 +08:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
static gboolean get_includes(const GDBusPropertyTable *property,
|
2016-08-11 18:38:36 +08:00
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
DBusMessageIter array;
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "as", &array);
|
|
|
|
|
|
|
|
if (ad.tx_power) {
|
|
|
|
const char *str = "tx-power";
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ad.name) {
|
|
|
|
const char *str = "local-name";
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ad.appearance) {
|
|
|
|
const char *str = "appearance";
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_iter_close_container(iter, &array);
|
|
|
|
|
2016-08-11 18:38:36 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:30:57 +08:00
|
|
|
static gboolean local_name_exits(const GDBusPropertyTable *property, void *data)
|
|
|
|
{
|
|
|
|
return ad.local_name ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_local_name(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &ad.local_name);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:15:49 +08:00
|
|
|
static gboolean appearance_exits(const GDBusPropertyTable *property, void *data)
|
|
|
|
{
|
|
|
|
return ad.local_appearance != UINT16_MAX ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean get_appearance(const GDBusPropertyTable *property,
|
|
|
|
DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16,
|
|
|
|
&ad.local_appearance);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:32:46 +08:00
|
|
|
static const GDBusPropertyTable ad_props[] = {
|
|
|
|
{ "Type", "s", get_type },
|
2016-08-09 20:16:58 +08:00
|
|
|
{ "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists },
|
2016-08-10 18:55:46 +08:00
|
|
|
{ "ServiceData", "a{sv}", get_service_data, NULL, service_data_exists },
|
2016-08-10 20:15:46 +08:00
|
|
|
{ "ManufacturerData", "a{qv}", get_manufacturer_data, NULL,
|
|
|
|
manufacturer_data_exists },
|
2017-08-09 16:34:12 +08:00
|
|
|
{ "Includes", "as", get_includes, NULL, includes_exists },
|
2017-08-10 17:30:57 +08:00
|
|
|
{ "LocalName", "s", get_local_name, NULL, local_name_exits },
|
2017-08-10 19:15:49 +08:00
|
|
|
{ "Appearance", "q", get_appearance, NULL, appearance_exits },
|
2016-08-09 19:32:46 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
if (ad.registered) {
|
2016-08-09 19:32:46 +08:00
|
|
|
rl_printf("Advertisement is already registered\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.type = g_strdup(type);
|
2016-08-09 19:32:46 +08:00
|
|
|
|
|
|
|
if (g_dbus_register_interface(conn, AD_PATH, AD_IFACE, ad_methods,
|
|
|
|
NULL, ad_props, NULL, NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to register advertising object\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_dbus_proxy_method_call(manager, "RegisterAdvertisement",
|
|
|
|
register_setup, register_reply,
|
|
|
|
conn, NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to register advertising\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path = AD_PATH;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == FALSE) {
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.registered = false;
|
2016-08-09 19:32:46 +08:00
|
|
|
rl_printf("Advertising object unregistered\n");
|
|
|
|
if (g_dbus_unregister_interface(conn, AD_PATH,
|
|
|
|
AD_IFACE) == FALSE)
|
|
|
|
rl_printf("Failed to unregister advertising object\n");
|
|
|
|
} else {
|
|
|
|
rl_printf("Failed to unregister advertisement: %s\n",
|
|
|
|
error.name);
|
|
|
|
dbus_error_free(&error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ad_unregister(DBusConnection *conn, GDBusProxy *manager)
|
|
|
|
{
|
|
|
|
if (!manager)
|
|
|
|
ad_release(conn);
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
if (!ad.registered)
|
2017-02-14 17:27:50 +08:00
|
|
|
return;
|
|
|
|
|
2016-08-09 19:32:46 +08:00
|
|
|
if (g_dbus_proxy_method_call(manager, "UnregisterAdvertisement",
|
|
|
|
unregister_setup, unregister_reply,
|
|
|
|
conn, NULL) == FALSE) {
|
|
|
|
rl_printf("Failed to unregister advertisement method\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 20:16:58 +08:00
|
|
|
|
|
|
|
void ad_advertise_uuids(const char *arg)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
g_strfreev(ad.uuids);
|
|
|
|
ad.uuids = NULL;
|
|
|
|
ad.uuids_len = 0;
|
2016-08-09 20:16:58 +08:00
|
|
|
|
|
|
|
if (!arg || !strlen(arg))
|
|
|
|
return;
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.uuids = g_strsplit(arg, " ", -1);
|
|
|
|
if (!ad.uuids) {
|
2016-08-09 20:16:58 +08:00
|
|
|
rl_printf("Failed to parse input\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.uuids_len = g_strv_length(ad.uuids);
|
2016-08-09 20:16:58 +08:00
|
|
|
}
|
2016-08-10 18:55:46 +08:00
|
|
|
|
|
|
|
static void ad_clear_service(void)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
g_free(ad.service.uuid);
|
|
|
|
memset(&ad.service, 0, sizeof(ad.service));
|
2016-08-10 18:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_service(const char *arg)
|
|
|
|
{
|
|
|
|
wordexp_t w;
|
|
|
|
unsigned int i;
|
2017-08-09 16:34:12 +08:00
|
|
|
struct ad_data *data;
|
2016-08-10 18:55:46 +08:00
|
|
|
|
|
|
|
if (wordexp(arg, &w, WRDE_NOCMD)) {
|
|
|
|
rl_printf("Invalid argument\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ad_clear_service();
|
|
|
|
|
|
|
|
if (w.we_wordc == 0)
|
|
|
|
goto done;
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.service.uuid = g_strdup(w.we_wordv[0]);
|
|
|
|
data = &ad.service.data;
|
2016-08-10 18:55:46 +08:00
|
|
|
|
|
|
|
for (i = 1; i < w.we_wordc; i++) {
|
|
|
|
long int val;
|
|
|
|
char *endptr = NULL;
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
if (i >= G_N_ELEMENTS(data->data)) {
|
2016-08-10 18:55:46 +08:00
|
|
|
rl_printf("Too much data\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = strtol(w.we_wordv[i], &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
|
|
|
|
rl_printf("Invalid value at index %d\n", i);
|
|
|
|
ad_clear_service();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
data->data[data->len] = val;
|
|
|
|
data->len++;
|
2016-08-10 18:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
wordfree(&w);
|
|
|
|
}
|
2016-08-10 20:15:46 +08:00
|
|
|
|
|
|
|
static void ad_clear_manufacturer(void)
|
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
memset(&ad.manufacturer, 0, sizeof(ad.manufacturer));
|
2016-08-10 20:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_manufacturer(const char *arg)
|
|
|
|
{
|
|
|
|
wordexp_t w;
|
|
|
|
unsigned int i;
|
|
|
|
char *endptr = NULL;
|
|
|
|
long int val;
|
2017-08-09 16:34:12 +08:00
|
|
|
struct ad_data *data;
|
2016-08-10 20:15:46 +08:00
|
|
|
|
|
|
|
if (wordexp(arg, &w, WRDE_NOCMD)) {
|
|
|
|
rl_printf("Invalid argument\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ad_clear_manufacturer();
|
|
|
|
|
|
|
|
if (w.we_wordc == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
val = strtol(w.we_wordv[0], &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || val > UINT16_MAX) {
|
|
|
|
rl_printf("Invalid manufacture id\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.manufacturer.id = val;
|
|
|
|
data = &ad.manufacturer.data;
|
2016-08-10 20:15:46 +08:00
|
|
|
|
|
|
|
for (i = 1; i < w.we_wordc; i++) {
|
2017-08-09 16:34:12 +08:00
|
|
|
if (i >= G_N_ELEMENTS(data->data)) {
|
2016-08-10 20:15:46 +08:00
|
|
|
rl_printf("Too much data\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = strtol(w.we_wordv[i], &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
|
|
|
|
rl_printf("Invalid value at index %d\n", i);
|
|
|
|
ad_clear_service();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
data->data[data->len] = val;
|
|
|
|
data->len++;
|
2016-08-10 20:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
wordfree(&w);
|
|
|
|
}
|
2016-08-11 18:38:36 +08:00
|
|
|
|
|
|
|
|
2017-08-09 16:34:12 +08:00
|
|
|
void ad_advertise_tx_power(bool value)
|
|
|
|
{
|
|
|
|
ad.tx_power = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_name(bool value)
|
|
|
|
{
|
|
|
|
ad.name = value;
|
2017-08-10 17:30:57 +08:00
|
|
|
|
|
|
|
if (!value)
|
|
|
|
free(ad.local_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_local_name(const char *name)
|
|
|
|
{
|
|
|
|
free(ad.local_name);
|
|
|
|
ad.local_name = strdup(name);
|
2017-08-09 16:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_appearance(bool value)
|
2016-08-11 18:38:36 +08:00
|
|
|
{
|
2017-08-09 16:34:12 +08:00
|
|
|
ad.appearance = value;
|
2017-08-10 19:15:49 +08:00
|
|
|
|
|
|
|
if (!value)
|
|
|
|
ad.local_appearance = UINT16_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ad_advertise_local_appearance(uint16_t value)
|
|
|
|
{
|
|
|
|
ad.local_appearance = value;
|
2016-08-11 18:38:36 +08:00
|
|
|
}
|