3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
6 * Copyright (C) 2014 Google Inc.
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.
26 #include <sys/types.h>
33 #include "lib/bluetooth.h"
38 #include "src/shared/util.h"
39 #include "src/shared/att.h"
40 #include "src/shared/queue.h"
41 #include "src/shared/gatt-db.h"
42 #include "src/shared/gatt-client.h"
43 #include "src/plugin.h"
44 #include "src/adapter.h"
45 #include "src/device.h"
46 #include "src/profile.h"
47 #include "src/service.h"
50 #define GAP_UUID16 0x1800
52 /* Generic Attribute/Access Service */
54 struct btd_device *device;
57 struct bt_gatt_client *client;
58 struct gatt_db_attribute *attr;
61 static GSList *devices;
63 static void gas_free(struct gas *gas)
65 gatt_db_unregister(gas->db, gas->db_id);
66 gatt_db_unref(gas->db);
67 bt_gatt_client_unref(gas->client);
68 btd_device_unref(gas->device);
72 static int cmp_device(gconstpointer a, gconstpointer b)
74 const struct gas *gas = a;
75 const struct btd_device *device = b;
77 return gas->device == device ? 0 : -1;
80 static char *name2utf8(const uint8_t *name, uint16_t len)
82 char utf8_name[HCI_MAX_NAME_LENGTH + 2];
85 if (g_utf8_validate((const char *) name, len, NULL))
86 return g_strndup((char *) name, len);
88 len = MIN(len, sizeof(utf8_name) - 1);
90 memset(utf8_name, 0, sizeof(utf8_name));
91 strncpy(utf8_name, (char *) name, len);
93 /* Assume ASCII, and replace all non-ASCII with spaces */
94 for (i = 0; utf8_name[i] != '\0'; i++) {
95 if (!isascii(utf8_name[i]))
99 /* Remove leading and trailing whitespace characters */
100 g_strstrip(utf8_name);
102 return g_strdup(utf8_name);
105 static void read_device_name_cb(bool success, uint8_t att_ecode,
106 const uint8_t *value, uint16_t length,
109 struct gas *gas = user_data;
113 DBG("Reading device name failed with ATT errror: %u",
121 name = name2utf8(value, length);
123 DBG("GAP Device Name: %s", name);
125 btd_device_device_set_name(gas->device, name);
130 static void handle_device_name(struct gas *gas, uint16_t value_handle)
132 if (!bt_gatt_client_read_long_value(gas->client, value_handle, 0,
133 read_device_name_cb, gas, NULL))
134 DBG("Failed to send request to read device name");
137 static void read_appearance_cb(bool success, uint8_t att_ecode,
138 const uint8_t *value, uint16_t length,
141 struct gas *gas = user_data;
145 DBG("Reading appearance failed with ATT error: %u", att_ecode);
149 /* The appearance value is a 16-bit unsigned integer */
151 DBG("Malformed appearance value");
155 appearance = get_le16(value);
157 DBG("GAP Appearance: 0x%04x", appearance);
159 device_set_appearance(gas->device, appearance);
162 static void handle_appearance(struct gas *gas, uint16_t value_handle)
164 if (!bt_gatt_client_read_value(gas->client, value_handle,
165 read_appearance_cb, gas, NULL))
166 DBG("Failed to send request to read appearance");
169 #ifdef __TIZEN_PATCH__
170 static void read_rpa_res_characteristic_value_cb(bool success, uint8_t att_ecode,
171 const uint8_t *value, uint16_t length,
174 struct gas *gas = user_data;
175 uint8_t rpa_res_support;
178 DBG("Reading RPA Resolution Char Value failed with ATT error: %u", att_ecode);
182 /* The RPA Resolution Char Value value is a 8-bit unsigned integer */
184 DBG("Malformed RPA resolution char value");
188 rpa_res_support = *value;
190 DBG("GAP RPA Resolution Char Value: %d", rpa_res_support);
192 device_set_rpa_res_char_value(gas->device, rpa_res_support);
195 static void handle_rpa_res_characteristic_value(struct gas *gas, uint16_t value_handle)
197 if (!bt_gatt_client_read_value(gas->client, value_handle,
198 read_rpa_res_characteristic_value_cb, gas, NULL))
199 DBG("Failed to send request to read RPA resolution Char Value");
203 static bool uuid_cmp(uint16_t u16, const bt_uuid_t *uuid)
207 bt_uuid16_create(&lhs, u16);
209 return bt_uuid_cmp(&lhs, uuid) == 0;
212 static void handle_characteristic(struct gatt_db_attribute *attr,
215 struct gas *gas = user_data;
216 uint16_t value_handle;
219 if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
221 error("Failed to obtain characteristic data");
225 if (uuid_cmp(GATT_CHARAC_DEVICE_NAME, &uuid))
226 handle_device_name(gas, value_handle);
227 else if (uuid_cmp(GATT_CHARAC_APPEARANCE, &uuid))
228 handle_appearance(gas, value_handle);
229 #ifdef __TIZEN_PATCH__
230 else if (uuid_cmp(GATT_CHARAC_CENTRAL_RPA_RESOLUTION, &uuid))
231 handle_rpa_res_characteristic_value(gas, value_handle);
234 char uuid_str[MAX_LEN_UUID_STR];
236 /* TODO: Support peripheral privacy feature */
238 bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
239 DBG("Unsupported characteristic: %s", uuid_str);
243 static void handle_gap_service(struct gas *gas)
245 gatt_db_service_foreach_char(gas->attr, handle_characteristic, gas);
248 static int gap_driver_probe(struct btd_service *service)
250 struct btd_device *device = btd_service_get_device(service);
255 ba2str(device_get_address(device), addr);
256 DBG("GAP profile probe (%s)", addr);
258 /* Ignore, if we were probed for this device already */
259 l = g_slist_find_custom(devices, device, cmp_device);
261 error("Profile probed twice for the same device!");
265 gas = g_new0(struct gas, 1);
269 gas->device = btd_device_ref(device);
270 devices = g_slist_append(devices, gas);
275 static void gap_driver_remove(struct btd_service *service)
277 struct btd_device *device = btd_service_get_device(service);
282 ba2str(device_get_address(device), addr);
283 DBG("GAP profile remove (%s)", addr);
285 l = g_slist_find_custom(devices, device, cmp_device);
287 error("GAP service not handled by profile");
293 devices = g_slist_remove(devices, gas);
297 static void foreach_gap_service(struct gatt_db_attribute *attr, void *user_data)
299 struct gas *gas = user_data;
302 error("More than one GAP service exists for this device");
307 handle_gap_service(gas);
310 static void service_added(struct gatt_db_attribute *attr, void *user_data)
312 struct gas *gas = user_data;
313 bt_uuid_t uuid, gap_uuid;
315 if (!bt_gatt_client_is_ready(gas->client))
318 gatt_db_attribute_get_service_uuid(attr, &uuid);
319 bt_uuid16_create(&gap_uuid, GAP_UUID16);
321 if (bt_uuid_cmp(&uuid, &gap_uuid))
325 error("More than one GAP service added to device");
329 DBG("GAP service added");
332 handle_gap_service(gas);
335 static void service_removed(struct gatt_db_attribute *attr, void *user_data)
337 struct gas *gas = user_data;
339 if (gas->attr != attr)
342 DBG("GAP service removed");
347 static int gap_driver_accept(struct btd_service *service)
349 struct btd_device *device = btd_service_get_device(service);
350 struct gatt_db *db = btd_device_get_gatt_db(device);
351 struct bt_gatt_client *client = btd_device_get_gatt_client(device);
357 ba2str(device_get_address(device), addr);
358 DBG("GAP profile accept (%s)", addr);
360 l = g_slist_find_custom(devices, device, cmp_device);
362 error("GAP service not handled by profile");
368 /* Clean-up any old client/db and acquire the new ones */
370 gatt_db_unregister(gas->db, gas->db_id);
371 gatt_db_unref(gas->db);
372 bt_gatt_client_unref(gas->client);
374 gas->db = gatt_db_ref(db);
375 gas->client = bt_gatt_client_ref(client);
376 gas->db_id = gatt_db_register(db, service_added, service_removed, gas,
379 /* Handle the GAP services */
380 bt_uuid16_create(&gap_uuid, GAP_UUID16);
381 gatt_db_foreach_service(db, &gap_uuid, foreach_gap_service, gas);
386 static struct btd_profile gap_profile = {
387 .name = "gap-profile",
388 .remote_uuid = GAP_UUID,
389 .device_probe = gap_driver_probe,
390 .device_remove = gap_driver_remove,
391 .accept = gap_driver_accept
394 static int gap_init(void)
398 btd_profile_register(&gap_profile);
403 static void gap_exit(void)
405 btd_profile_unregister(&gap_profile);
408 BLUETOOTH_PLUGIN_DEFINE(gap, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,