Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / profiles / gap / gas.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012  Instituto Nokia de Tecnologia - INdT
6  *  Copyright (C) 2014  Google Inc.
7  *
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.
12  *
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.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <ctype.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #include <glib.h>
32
33 #include "lib/bluetooth.h"
34 #include "lib/hci.h"
35 #include "lib/sdp.h"
36 #include "lib/uuid.h"
37
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"
48 #include "src/log.h"
49
50 #define GAP_UUID16 0x1800
51
52 /* Generic Attribute/Access Service */
53 struct gas {
54         struct btd_device *device;
55         struct gatt_db *db;
56         struct bt_gatt_client *client;
57         struct gatt_db_attribute *attr;
58 };
59
60 static GSList *devices;
61
62 static void gas_free(struct gas *gas)
63 {
64         gatt_db_unref(gas->db);
65         bt_gatt_client_unref(gas->client);
66         btd_device_unref(gas->device);
67         g_free(gas);
68 }
69
70 static int cmp_device(gconstpointer a, gconstpointer b)
71 {
72         const struct gas *gas = a;
73         const struct btd_device *device = b;
74
75         return gas->device == device ? 0 : -1;
76 }
77
78 static char *name2utf8(const uint8_t *name, uint16_t len)
79 {
80         char utf8_name[HCI_MAX_NAME_LENGTH + 2];
81         int i;
82
83         if (g_utf8_validate((const char *) name, len, NULL))
84                 return g_strndup((char *) name, len);
85
86         len = MIN(len, sizeof(utf8_name) - 1);
87
88         memset(utf8_name, 0, sizeof(utf8_name));
89         strncpy(utf8_name, (char *) name, len);
90
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]))
94                         utf8_name[i] = ' ';
95         }
96
97         /* Remove leading and trailing whitespace characters */
98         g_strstrip(utf8_name);
99
100         return g_strdup(utf8_name);
101 }
102
103 static void read_device_name_cb(bool success, uint8_t att_ecode,
104                                         const uint8_t *value, uint16_t length,
105                                         void *user_data)
106 {
107         struct gas *gas = user_data;
108         char *name;
109
110         if (!success) {
111                 DBG("Reading device name failed with ATT errror: %u",
112                                                                 att_ecode);
113                 return;
114         }
115
116         if (!length)
117                 return;
118
119         name = name2utf8(value, length);
120
121         DBG("GAP Device Name: %s", name);
122
123         btd_device_device_set_name(gas->device, name);
124
125         g_free(name);
126 }
127
128 static void handle_device_name(struct gas *gas, uint16_t value_handle)
129 {
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");
133 }
134
135 static void read_appearance_cb(bool success, uint8_t att_ecode,
136                                         const uint8_t *value, uint16_t length,
137                                         void *user_data)
138 {
139         struct gas *gas = user_data;
140         uint16_t appearance;
141
142         if (!success) {
143                 DBG("Reading appearance failed with ATT error: %u", att_ecode);
144                 return;
145         }
146
147         /* The appearance value is a 16-bit unsigned integer */
148         if (length != 2) {
149                 DBG("Malformed appearance value");
150                 return;
151         }
152
153         appearance = get_le16(value);
154
155         DBG("GAP Appearance: 0x%04x", appearance);
156
157         device_set_appearance(gas->device, appearance);
158 }
159
160 static void handle_appearance(struct gas *gas, uint16_t value_handle)
161 {
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");
165 }
166
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,
170                                         void *user_data)
171 {
172         struct gas *gas = user_data;
173         uint8_t rpa_res_support;
174
175         if (!success) {
176                 DBG("Reading RPA Resolution Char Value failed with ATT error: %u", att_ecode);
177                 return;
178         }
179
180         /* The RPA Resolution Char Value value is a 8-bit unsigned integer */
181         if (length != 1) {
182                 DBG("Malformed RPA resolution char value");
183                 return;
184         }
185
186         rpa_res_support = *value;
187
188         DBG("GAP RPA Resolution Char Value: %d", rpa_res_support);
189
190         device_set_rpa_res_char_value(gas->device, rpa_res_support);
191 }
192
193 static void handle_rpa_res_characteristic_value(struct gas *gas, uint16_t value_handle)
194 {
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");
198 }
199 #endif
200
201 static bool uuid_cmp(uint16_t u16, const bt_uuid_t *uuid)
202 {
203         bt_uuid_t lhs;
204
205         bt_uuid16_create(&lhs, u16);
206
207         return bt_uuid_cmp(&lhs, uuid) == 0;
208 }
209
210 static void handle_characteristic(struct gatt_db_attribute *attr,
211                                                                 void *user_data)
212 {
213         struct gas *gas = user_data;
214         uint16_t value_handle;
215         bt_uuid_t uuid;
216
217         if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
218                                                                 &uuid)) {
219                 error("Failed to obtain characteristic data");
220                 return;
221         }
222
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);
230 #endif
231         else {
232                 char uuid_str[MAX_LEN_UUID_STR];
233
234                 /* TODO: Support peripheral privacy feature */
235
236                 bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
237                 DBG("Unsupported characteristic: %s", uuid_str);
238         }
239 }
240
241 static void handle_gap_service(struct gas *gas)
242 {
243         gatt_db_service_foreach_char(gas->attr, handle_characteristic, gas);
244 }
245
246 static int gap_driver_probe(struct btd_service *service)
247 {
248         struct btd_device *device = btd_service_get_device(service);
249         struct gas *gas;
250         GSList *l;
251         char addr[18];
252
253         ba2str(device_get_address(device), addr);
254         DBG("GAP profile probe (%s)", addr);
255
256         /* Ignore, if we were probed for this device already */
257         l = g_slist_find_custom(devices, device, cmp_device);
258         if (l) {
259                 error("Profile probed twice for the same device!");
260                 return -1;
261         }
262
263         gas = g_new0(struct gas, 1);
264         if (!gas)
265                 return -1;
266
267         gas->device = btd_device_ref(device);
268         devices = g_slist_append(devices, gas);
269
270         return 0;
271 }
272
273 static void gap_driver_remove(struct btd_service *service)
274 {
275         struct btd_device *device = btd_service_get_device(service);
276         struct gas *gas;
277         GSList *l;
278         char addr[18];
279
280         ba2str(device_get_address(device), addr);
281         DBG("GAP profile remove (%s)", addr);
282
283         l = g_slist_find_custom(devices, device, cmp_device);
284         if (!l) {
285                 error("GAP service not handled by profile");
286                 return;
287         }
288
289         gas = l->data;
290
291         devices = g_slist_remove(devices, gas);
292         gas_free(gas);
293 }
294
295 static void foreach_gap_service(struct gatt_db_attribute *attr, void *user_data)
296 {
297         struct gas *gas = user_data;
298
299         if (gas->attr) {
300                 error("More than one GAP service exists for this device");
301                 return;
302         }
303
304         gas->attr = attr;
305         handle_gap_service(gas);
306 }
307
308 static int gap_driver_accept(struct btd_service *service)
309 {
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);
313         struct gas *gas;
314         GSList *l;
315         char addr[18];
316         bt_uuid_t gap_uuid;
317
318         ba2str(device_get_address(device), addr);
319         DBG("GAP profile accept (%s)", addr);
320
321         l = g_slist_find_custom(devices, device, cmp_device);
322         if (!l) {
323                 error("GAP service not handled by profile");
324                 return -1;
325         }
326
327         gas = l->data;
328
329         /* Clean-up any old client/db and acquire the new ones */
330         gas->attr = NULL;
331         gatt_db_unref(gas->db);
332         bt_gatt_client_unref(gas->client);
333
334         gas->db = gatt_db_ref(db);
335         gas->client = bt_gatt_client_ref(client);
336
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);
340
341         return 0;
342 }
343
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
350 };
351
352 static int gap_init(void)
353 {
354         devices = NULL;
355
356         btd_profile_register(&gap_profile);
357
358         return 0;
359 }
360
361 static void gap_exit(void)
362 {
363         btd_profile_unregister(&gap_profile);
364 }
365
366 BLUETOOTH_PLUGIN_DEFINE(gap, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
367                                                         gap_init, gap_exit)