|
Packit Service |
8264ee |
/*
|
|
Packit Service |
8264ee |
*
|
|
Packit Service |
8264ee |
* BlueZ - Bluetooth protocol stack for Linux
|
|
Packit Service |
8264ee |
*
|
|
Packit Service |
8264ee |
* Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
|
|
Packit Service |
8264ee |
* Copyright (C) 2014 Google Inc.
|
|
Packit Service |
8264ee |
*
|
|
Packit Service |
8264ee |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
8264ee |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
8264ee |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
8264ee |
* (at your option) any later version.
|
|
Packit Service |
8264ee |
*
|
|
Packit Service |
8264ee |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
8264ee |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
8264ee |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
8264ee |
* GNU General Public License for more details.
|
|
Packit Service |
8264ee |
*/
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
8264ee |
#include <config.h>
|
|
Packit Service |
8264ee |
#endif
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#define _GNU_SOURCE
|
|
Packit Service |
8264ee |
#include <ctype.h>
|
|
Packit Service |
8264ee |
#include <stdbool.h>
|
|
Packit Service |
8264ee |
#include <stdlib.h>
|
|
Packit Service |
8264ee |
#include <sys/types.h>
|
|
Packit Service |
8264ee |
#include <sys/stat.h>
|
|
Packit Service |
8264ee |
#include <fcntl.h>
|
|
Packit Service |
8264ee |
#include <errno.h>
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#include <glib.h>
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#include "lib/bluetooth.h"
|
|
Packit Service |
8264ee |
#include "lib/hci.h"
|
|
Packit Service |
8264ee |
#include "lib/sdp.h"
|
|
Packit Service |
8264ee |
#include "lib/uuid.h"
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#include "src/shared/util.h"
|
|
Packit Service |
8264ee |
#include "src/shared/att.h"
|
|
Packit Service |
8264ee |
#include "src/shared/queue.h"
|
|
Packit Service |
8264ee |
#include "src/shared/gatt-db.h"
|
|
Packit Service |
8264ee |
#include "src/shared/gatt-client.h"
|
|
Packit Service |
8264ee |
#include "src/plugin.h"
|
|
Packit Service |
8264ee |
#include "src/adapter.h"
|
|
Packit Service |
8264ee |
#include "src/device.h"
|
|
Packit Service |
8264ee |
#include "src/profile.h"
|
|
Packit Service |
8264ee |
#include "src/service.h"
|
|
Packit Service |
8264ee |
#include "src/log.h"
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
#define GAP_UUID16 0x1800
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* Generic Attribute/Access Service */
|
|
Packit Service |
8264ee |
struct gas {
|
|
Packit Service |
8264ee |
struct btd_device *device;
|
|
Packit Service |
8264ee |
struct gatt_db *db;
|
|
Packit Service |
8264ee |
struct bt_gatt_client *client;
|
|
Packit Service |
8264ee |
struct gatt_db_attribute *attr;
|
|
Packit Service |
8264ee |
};
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void gas_free(struct gas *gas)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
gatt_db_unref(gas->db);
|
|
Packit Service |
8264ee |
bt_gatt_client_unref(gas->client);
|
|
Packit Service |
8264ee |
btd_device_unref(gas->device);
|
|
Packit Service |
8264ee |
g_free(gas);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static char *name2utf8(const uint8_t *name, uint16_t len)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
char utf8_name[HCI_MAX_NAME_LENGTH + 2];
|
|
Packit Service |
8264ee |
int i;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (g_utf8_validate((const char *) name, len, NULL))
|
|
Packit Service |
8264ee |
return g_strndup((char *) name, len);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
len = MIN(len, sizeof(utf8_name) - 1);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
memset(utf8_name, 0, sizeof(utf8_name));
|
|
Packit Service |
8264ee |
strncpy(utf8_name, (char *) name, len);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* Assume ASCII, and replace all non-ASCII with spaces */
|
|
Packit Service |
8264ee |
for (i = 0; utf8_name[i] != '\0'; i++) {
|
|
Packit Service |
8264ee |
if (!isascii(utf8_name[i]))
|
|
Packit Service |
8264ee |
utf8_name[i] = ' ';
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* Remove leading and trailing whitespace characters */
|
|
Packit Service |
8264ee |
g_strstrip(utf8_name);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
return g_strdup(utf8_name);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void read_device_name_cb(bool success, uint8_t att_ecode,
|
|
Packit Service |
8264ee |
const uint8_t *value, uint16_t length,
|
|
Packit Service |
8264ee |
void *user_data)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct gas *gas = user_data;
|
|
Packit Service |
8264ee |
char *name;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!success) {
|
|
Packit Service |
8264ee |
DBG("Reading device name failed with ATT errror: %u",
|
|
Packit Service |
8264ee |
att_ecode);
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!length)
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
name = name2utf8(value, length);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
DBG("GAP Device Name: %s", name);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
btd_device_device_set_name(gas->device, name);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
g_free(name);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void handle_device_name(struct gas *gas, uint16_t value_handle)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
if (!bt_gatt_client_read_long_value(gas->client, value_handle, 0,
|
|
Packit Service |
8264ee |
read_device_name_cb, gas, NULL))
|
|
Packit Service |
8264ee |
DBG("Failed to send request to read device name");
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void read_appearance_cb(bool success, uint8_t att_ecode,
|
|
Packit Service |
8264ee |
const uint8_t *value, uint16_t length,
|
|
Packit Service |
8264ee |
void *user_data)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct gas *gas = user_data;
|
|
Packit Service |
8264ee |
uint16_t appearance;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!success) {
|
|
Packit Service |
8264ee |
DBG("Reading appearance failed with ATT error: %u", att_ecode);
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* The appearance value is a 16-bit unsigned integer */
|
|
Packit Service |
8264ee |
if (length != 2) {
|
|
Packit Service |
8264ee |
DBG("Malformed appearance value");
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
appearance = get_le16(value);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
DBG("GAP Appearance: 0x%04x", appearance);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
device_set_appearance(gas->device, appearance);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void handle_appearance(struct gas *gas, uint16_t value_handle)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
if (!bt_gatt_client_read_value(gas->client, value_handle,
|
|
Packit Service |
8264ee |
read_appearance_cb, gas, NULL))
|
|
Packit Service |
8264ee |
DBG("Failed to send request to read appearance");
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static bool uuid_cmp(uint16_t u16, const bt_uuid_t *uuid)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
bt_uuid_t lhs;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
bt_uuid16_create(&lhs, u16);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
return bt_uuid_cmp(&lhs, uuid) == 0;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void handle_characteristic(struct gatt_db_attribute *attr,
|
|
Packit Service |
8264ee |
void *user_data)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct gas *gas = user_data;
|
|
Packit Service |
8264ee |
uint16_t value_handle;
|
|
Packit Service |
8264ee |
bt_uuid_t uuid;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
|
|
Packit Service |
8264ee |
NULL, &uuid)) {
|
|
Packit Service |
8264ee |
error("Failed to obtain characteristic data");
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (uuid_cmp(GATT_CHARAC_DEVICE_NAME, &uuid))
|
|
Packit Service |
8264ee |
handle_device_name(gas, value_handle);
|
|
Packit Service |
8264ee |
else if (uuid_cmp(GATT_CHARAC_APPEARANCE, &uuid))
|
|
Packit Service |
8264ee |
handle_appearance(gas, value_handle);
|
|
Packit Service |
8264ee |
else {
|
|
Packit Service |
8264ee |
char uuid_str[MAX_LEN_UUID_STR];
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* TODO: Support peripheral privacy feature */
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
|
|
Packit Service |
8264ee |
DBG("Unsupported characteristic: %s", uuid_str);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void handle_gap_service(struct gas *gas)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
gatt_db_service_foreach_char(gas->attr, handle_characteristic, gas);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static int gap_probe(struct btd_service *service)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct btd_device *device = btd_service_get_device(service);
|
|
Packit Service |
8264ee |
struct gas *gas = btd_service_get_user_data(service);
|
|
Packit Service |
8264ee |
char addr[18];
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
ba2str(device_get_address(device), addr);
|
|
Packit Service |
8264ee |
DBG("GAP profile probe (%s)", addr);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* Ignore, if we were probed for this device already */
|
|
Packit Service |
8264ee |
if (gas) {
|
|
Packit Service |
8264ee |
error("Profile probed twice for the same device!");
|
|
Packit Service |
8264ee |
return -1;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas = g_new0(struct gas, 1);
|
|
Packit Service |
8264ee |
if (!gas)
|
|
Packit Service |
8264ee |
return -1;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas->device = btd_device_ref(device);
|
|
Packit Service |
8264ee |
btd_service_set_user_data(service, gas);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
return 0;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void gap_remove(struct btd_service *service)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct btd_device *device = btd_service_get_device(service);
|
|
Packit Service |
8264ee |
struct gas *gas;
|
|
Packit Service |
8264ee |
char addr[18];
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
ba2str(device_get_address(device), addr);
|
|
Packit Service |
8264ee |
DBG("GAP profile remove (%s)", addr);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas = btd_service_get_user_data(service);
|
|
Packit Service |
8264ee |
if (!gas) {
|
|
Packit Service |
8264ee |
error("GAP service not handled by profile");
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas_free(gas);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void foreach_gap_service(struct gatt_db_attribute *attr, void *user_data)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct gas *gas = user_data;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (gas->attr) {
|
|
Packit Service |
8264ee |
error("More than one GAP service exists for this device");
|
|
Packit Service |
8264ee |
return;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas->attr = attr;
|
|
Packit Service |
8264ee |
handle_gap_service(gas);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void gas_reset(struct gas *gas)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
gas->attr = NULL;
|
|
Packit Service |
8264ee |
gatt_db_unref(gas->db);
|
|
Packit Service |
8264ee |
gas->db = NULL;
|
|
Packit Service |
8264ee |
bt_gatt_client_unref(gas->client);
|
|
Packit Service |
8264ee |
gas->client = NULL;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static int gap_accept(struct btd_service *service)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct btd_device *device = btd_service_get_device(service);
|
|
Packit Service |
8264ee |
struct gatt_db *db = btd_device_get_gatt_db(device);
|
|
Packit Service |
8264ee |
struct bt_gatt_client *client = btd_device_get_gatt_client(device);
|
|
Packit Service |
8264ee |
struct gas *gas = btd_service_get_user_data(service);
|
|
Packit Service |
8264ee |
char addr[18];
|
|
Packit Service |
8264ee |
bt_uuid_t gap_uuid;
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
ba2str(device_get_address(device), addr);
|
|
Packit Service |
8264ee |
DBG("GAP profile accept (%s)", addr);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!gas) {
|
|
Packit Service |
8264ee |
error("GAP service not handled by profile");
|
|
Packit Service |
8264ee |
return -1;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas->db = gatt_db_ref(db);
|
|
Packit Service |
8264ee |
gas->client = bt_gatt_client_clone(client);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
/* Handle the GAP services */
|
|
Packit Service |
8264ee |
bt_uuid16_create(&gap_uuid, GAP_UUID16);
|
|
Packit Service |
8264ee |
gatt_db_foreach_service(db, &gap_uuid, foreach_gap_service, gas);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
if (!gas->attr) {
|
|
Packit Service |
8264ee |
error("GAP attribute not found");
|
|
Packit Service |
8264ee |
gas_reset(gas);
|
|
Packit Service |
8264ee |
return -1;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
btd_service_connecting_complete(service, 0);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
return 0;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static int gap_disconnect(struct btd_service *service)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
struct gas *gas = btd_service_get_user_data(service);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
gas_reset(gas);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
btd_service_disconnecting_complete(service, 0);
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
return 0;
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static struct btd_profile gap_profile = {
|
|
Packit Service |
8264ee |
.name = "gap-profile",
|
|
Packit Service |
8264ee |
.remote_uuid = GAP_UUID,
|
|
Packit Service |
8264ee |
.device_probe = gap_probe,
|
|
Packit Service |
8264ee |
.device_remove = gap_remove,
|
|
Packit Service |
8264ee |
.accept = gap_accept,
|
|
Packit Service |
8264ee |
.disconnect = gap_disconnect,
|
|
Packit Service |
8264ee |
};
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static int gap_init(void)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
return btd_profile_register(&gap_profile);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
static void gap_exit(void)
|
|
Packit Service |
8264ee |
{
|
|
Packit Service |
8264ee |
btd_profile_unregister(&gap_profile);
|
|
Packit Service |
8264ee |
}
|
|
Packit Service |
8264ee |
|
|
Packit Service |
8264ee |
BLUETOOTH_PLUGIN_DEFINE(gap, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
|
|
Packit Service |
8264ee |
gap_init, gap_exit)
|