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;
56 struct bt_gatt_client *client;
57 struct gatt_db_attribute *attr;
60 static GSList *devices;
62 static void gas_free(struct gas *gas)
64 gatt_db_unref(gas->db);
65 bt_gatt_client_unref(gas->client);
66 btd_device_unref(gas->device);
70 static int cmp_device(gconstpointer a, gconstpointer b)
72 const struct gas *gas = a;
73 const struct btd_device *device = b;
75 return gas->device == device ? 0 : -1;
78 static char *name2utf8(const uint8_t *name, uint16_t len)
80 char utf8_name[HCI_MAX_NAME_LENGTH + 2];
83 if (g_utf8_validate((const char *) name, len, NULL))
84 return g_strndup((char *) name, len);
86 len = MIN(len, sizeof(utf8_name) - 1);
88 memset(utf8_name, 0, sizeof(utf8_name));
89 strncpy(utf8_name, (char *) name, len);
91 /* Assume ASCII, and replace all non-ASCII with spaces */
92 for (i = 0; utf8_name[i] != '\0'; i++) {
93 if (!isascii(utf8_name[i]))
97 /* Remove leading and trailing whitespace characters */
98 g_strstrip(utf8_name);
100 return g_strdup(utf8_name);
103 static void read_device_name_cb(bool success, uint8_t att_ecode,
104 const uint8_t *value, uint16_t length,
107 struct gas *gas = user_data;
111 DBG("Reading device name failed with ATT errror: %u",
119 name = name2utf8(value, length);
121 DBG("GAP Device Name: %s", name);
123 btd_device_device_set_name(gas->device, name);
128 static void handle_device_name(struct gas *gas, uint16_t value_handle)
130 if (!bt_gatt_client_read_long_value(gas->client, value_handle, 0,
131 read_device_name_cb, gas, NULL))
132 DBG("Failed to send request to read device name");
135 static void read_appearance_cb(bool success, uint8_t att_ecode,
136 const uint8_t *value, uint16_t length,
139 struct gas *gas = user_data;
143 DBG("Reading appearance failed with ATT error: %u", att_ecode);
147 /* The appearance value is a 16-bit unsigned integer */
149 DBG("Malformed appearance value");
153 appearance = get_le16(value);
155 DBG("GAP Appearance: 0x%04x", appearance);
157 device_set_appearance(gas->device, appearance);
160 static void handle_appearance(struct gas *gas, uint16_t value_handle)
162 if (!bt_gatt_client_read_value(gas->client, value_handle,
163 read_appearance_cb, gas, NULL))
164 DBG("Failed to send request to read appearance");
167 #ifdef __TIZEN_PATCH__
168 static void read_rpa_res_characteristic_value_cb(bool success, uint8_t att_ecode,
169 const uint8_t *value, uint16_t length,
172 struct gas *gas = user_data;
173 uint8_t rpa_res_support;
176 DBG("Reading RPA Resolution Char Value failed with ATT error: %u", att_ecode);
180 /* The RPA Resolution Char Value value is a 8-bit unsigned integer */
182 DBG("Malformed RPA resolution char value");
186 rpa_res_support = *value;
188 DBG("GAP RPA Resolution Char Value: %d", rpa_res_support);
190 device_set_rpa_res_char_value(gas->device, rpa_res_support);
193 static void handle_rpa_res_characteristic_value(struct gas *gas, uint16_t value_handle)
195 if (!bt_gatt_client_read_value(gas->client, value_handle,
196 read_rpa_res_characteristic_value_cb, gas, NULL))
197 DBG("Failed to send request to read RPA resolution Char Value");
201 static bool uuid_cmp(uint16_t u16, const bt_uuid_t *uuid)
205 bt_uuid16_create(&lhs, u16);
207 return bt_uuid_cmp(&lhs, uuid) == 0;
210 static void handle_characteristic(struct gatt_db_attribute *attr,
213 struct gas *gas = user_data;
214 uint16_t value_handle;
217 if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
219 error("Failed to obtain characteristic data");
223 if (uuid_cmp(GATT_CHARAC_DEVICE_NAME, &uuid))
224 handle_device_name(gas, value_handle);
225 else if (uuid_cmp(GATT_CHARAC_APPEARANCE, &uuid))
226 handle_appearance(gas, value_handle);
227 #ifdef __TIZEN_PATCH__
228 else if (uuid_cmp(GATT_CHARAC_CENTRAL_RPA_RESOLUTION, &uuid))
229 handle_rpa_res_characteristic_value(gas, value_handle);
232 char uuid_str[MAX_LEN_UUID_STR];
234 /* TODO: Support peripheral privacy feature */
236 bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
237 DBG("Unsupported characteristic: %s", uuid_str);
241 static void handle_gap_service(struct gas *gas)
243 gatt_db_service_foreach_char(gas->attr, handle_characteristic, gas);
246 static int gap_driver_probe(struct btd_service *service)
248 struct btd_device *device = btd_service_get_device(service);
253 ba2str(device_get_address(device), addr);
254 DBG("GAP profile probe (%s)", addr);
256 /* Ignore, if we were probed for this device already */
257 l = g_slist_find_custom(devices, device, cmp_device);
259 error("Profile probed twice for the same device!");
263 gas = g_new0(struct gas, 1);
267 gas->device = btd_device_ref(device);
268 devices = g_slist_append(devices, gas);
273 static void gap_driver_remove(struct btd_service *service)
275 struct btd_device *device = btd_service_get_device(service);
280 ba2str(device_get_address(device), addr);
281 DBG("GAP profile remove (%s)", addr);
283 l = g_slist_find_custom(devices, device, cmp_device);
285 error("GAP service not handled by profile");
291 devices = g_slist_remove(devices, gas);
295 static void foreach_gap_service(struct gatt_db_attribute *attr, void *user_data)
297 struct gas *gas = user_data;
300 error("More than one GAP service exists for this device");
305 handle_gap_service(gas);
308 static int gap_driver_accept(struct btd_service *service)
310 struct btd_device *device = btd_service_get_device(service);
311 struct gatt_db *db = btd_device_get_gatt_db(device);
312 struct bt_gatt_client *client = btd_device_get_gatt_client(device);
318 ba2str(device_get_address(device), addr);
319 DBG("GAP profile accept (%s)", addr);
321 l = g_slist_find_custom(devices, device, cmp_device);
323 error("GAP service not handled by profile");
329 /* Clean-up any old client/db and acquire the new ones */
331 gatt_db_unref(gas->db);
332 bt_gatt_client_unref(gas->client);
334 gas->db = gatt_db_ref(db);
335 gas->client = bt_gatt_client_ref(client);
337 /* Handle the GAP services */
338 bt_uuid16_create(&gap_uuid, GAP_UUID16);
339 gatt_db_foreach_service(db, &gap_uuid, foreach_gap_service, gas);
344 static struct btd_profile gap_profile = {
345 .name = "gap-profile",
346 .remote_uuid = GAP_UUID,
347 .device_probe = gap_driver_probe,
348 .device_remove = gap_driver_remove,
349 .accept = gap_driver_accept
352 static int gap_init(void)
356 btd_profile_register(&gap_profile);
361 static void gap_exit(void)
363 btd_profile_unregister(&gap_profile);
366 BLUETOOTH_PLUGIN_DEFINE(gap, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,