tizen 2.4 release
[framework/connectivity/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         unsigned int db_id;
57         struct bt_gatt_client *client;
58         struct gatt_db_attribute *attr;
59 };
60
61 static GSList *devices;
62
63 static void gas_free(struct gas *gas)
64 {
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);
69         g_free(gas);
70 }
71
72 static int cmp_device(gconstpointer a, gconstpointer b)
73 {
74         const struct gas *gas = a;
75         const struct btd_device *device = b;
76
77         return gas->device == device ? 0 : -1;
78 }
79
80 static char *name2utf8(const uint8_t *name, uint16_t len)
81 {
82         char utf8_name[HCI_MAX_NAME_LENGTH + 2];
83         int i;
84
85         if (g_utf8_validate((const char *) name, len, NULL))
86                 return g_strndup((char *) name, len);
87
88         len = MIN(len, sizeof(utf8_name) - 1);
89
90         memset(utf8_name, 0, sizeof(utf8_name));
91         strncpy(utf8_name, (char *) name, len);
92
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]))
96                         utf8_name[i] = ' ';
97         }
98
99         /* Remove leading and trailing whitespace characters */
100         g_strstrip(utf8_name);
101
102         return g_strdup(utf8_name);
103 }
104
105 static void read_device_name_cb(bool success, uint8_t att_ecode,
106                                         const uint8_t *value, uint16_t length,
107                                         void *user_data)
108 {
109         struct gas *gas = user_data;
110         char *name;
111
112         if (!success) {
113                 DBG("Reading device name failed with ATT errror: %u",
114                                                                 att_ecode);
115                 return;
116         }
117
118         if (!length)
119                 return;
120
121         name = name2utf8(value, length);
122
123         DBG("GAP Device Name: %s", name);
124
125         btd_device_device_set_name(gas->device, name);
126
127         g_free(name);
128 }
129
130 static void handle_device_name(struct gas *gas, uint16_t value_handle)
131 {
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");
135 }
136
137 static void read_appearance_cb(bool success, uint8_t att_ecode,
138                                         const uint8_t *value, uint16_t length,
139                                         void *user_data)
140 {
141         struct gas *gas = user_data;
142         uint16_t appearance;
143
144         if (!success) {
145                 DBG("Reading appearance failed with ATT error: %u", att_ecode);
146                 return;
147         }
148
149         /* The appearance value is a 16-bit unsigned integer */
150         if (length != 2) {
151                 DBG("Malformed appearance value");
152                 return;
153         }
154
155         appearance = get_le16(value);
156
157         DBG("GAP Appearance: 0x%04x", appearance);
158
159         device_set_appearance(gas->device, appearance);
160 }
161
162 static void handle_appearance(struct gas *gas, uint16_t value_handle)
163 {
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");
167 }
168
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,
172                                         void *user_data)
173 {
174         struct gas *gas = user_data;
175         uint8_t rpa_res_support;
176
177         if (!success) {
178                 DBG("Reading RPA Resolution Char Value failed with ATT error: %u", att_ecode);
179                 return;
180         }
181
182         /* The RPA Resolution Char Value value is a 8-bit unsigned integer */
183         if (length != 1) {
184                 DBG("Malformed RPA resolution char value");
185                 return;
186         }
187
188         rpa_res_support = *value;
189
190         DBG("GAP RPA Resolution Char Value: %d", rpa_res_support);
191
192         device_set_rpa_res_char_value(gas->device, rpa_res_support);
193 }
194
195 static void handle_rpa_res_characteristic_value(struct gas *gas, uint16_t value_handle)
196 {
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");
200 }
201 #endif
202
203 static bool uuid_cmp(uint16_t u16, const bt_uuid_t *uuid)
204 {
205         bt_uuid_t lhs;
206
207         bt_uuid16_create(&lhs, u16);
208
209         return bt_uuid_cmp(&lhs, uuid) == 0;
210 }
211
212 static void handle_characteristic(struct gatt_db_attribute *attr,
213                                                                 void *user_data)
214 {
215         struct gas *gas = user_data;
216         uint16_t value_handle;
217         bt_uuid_t uuid;
218
219         if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
220                                                                 &uuid)) {
221                 error("Failed to obtain characteristic data");
222                 return;
223         }
224
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);
232 #endif
233         else {
234                 char uuid_str[MAX_LEN_UUID_STR];
235
236                 /* TODO: Support peripheral privacy feature */
237
238                 bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
239                 DBG("Unsupported characteristic: %s", uuid_str);
240         }
241 }
242
243 static void handle_gap_service(struct gas *gas)
244 {
245         gatt_db_service_foreach_char(gas->attr, handle_characteristic, gas);
246 }
247
248 static int gap_driver_probe(struct btd_service *service)
249 {
250         struct btd_device *device = btd_service_get_device(service);
251         struct gas *gas;
252         GSList *l;
253         char addr[18];
254
255         ba2str(device_get_address(device), addr);
256         DBG("GAP profile probe (%s)", addr);
257
258         /* Ignore, if we were probed for this device already */
259         l = g_slist_find_custom(devices, device, cmp_device);
260         if (l) {
261                 error("Profile probed twice for the same device!");
262                 return -1;
263         }
264
265         gas = g_new0(struct gas, 1);
266         if (!gas)
267                 return -1;
268
269         gas->device = btd_device_ref(device);
270         devices = g_slist_append(devices, gas);
271
272         return 0;
273 }
274
275 static void gap_driver_remove(struct btd_service *service)
276 {
277         struct btd_device *device = btd_service_get_device(service);
278         struct gas *gas;
279         GSList *l;
280         char addr[18];
281
282         ba2str(device_get_address(device), addr);
283         DBG("GAP profile remove (%s)", addr);
284
285         l = g_slist_find_custom(devices, device, cmp_device);
286         if (!l) {
287                 error("GAP service not handled by profile");
288                 return;
289         }
290
291         gas = l->data;
292
293         devices = g_slist_remove(devices, gas);
294         gas_free(gas);
295 }
296
297 static void foreach_gap_service(struct gatt_db_attribute *attr, void *user_data)
298 {
299         struct gas *gas = user_data;
300
301         if (gas->attr) {
302                 error("More than one GAP service exists for this device");
303                 return;
304         }
305
306         gas->attr = attr;
307         handle_gap_service(gas);
308 }
309
310 static void service_added(struct gatt_db_attribute *attr, void *user_data)
311 {
312         struct gas *gas = user_data;
313         bt_uuid_t uuid, gap_uuid;
314
315         if (!bt_gatt_client_is_ready(gas->client))
316                 return;
317
318         gatt_db_attribute_get_service_uuid(attr, &uuid);
319         bt_uuid16_create(&gap_uuid, GAP_UUID16);
320
321         if (bt_uuid_cmp(&uuid, &gap_uuid))
322                 return;
323
324         if (gas->attr) {
325                 error("More than one GAP service added to device");
326                 return;
327         }
328
329         DBG("GAP service added");
330
331         gas->attr = attr;
332         handle_gap_service(gas);
333 }
334
335 static void service_removed(struct gatt_db_attribute *attr, void *user_data)
336 {
337         struct gas *gas = user_data;
338
339         if (gas->attr != attr)
340                 return;
341
342         DBG("GAP service removed");
343
344         gas->attr = NULL;
345 }
346
347 static int gap_driver_accept(struct btd_service *service)
348 {
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);
352         struct gas *gas;
353         GSList *l;
354         char addr[18];
355         bt_uuid_t gap_uuid;
356
357         ba2str(device_get_address(device), addr);
358         DBG("GAP profile accept (%s)", addr);
359
360         l = g_slist_find_custom(devices, device, cmp_device);
361         if (!l) {
362                 error("GAP service not handled by profile");
363                 return -1;
364         }
365
366         gas = l->data;
367
368         /* Clean-up any old client/db and acquire the new ones */
369         gas->attr = NULL;
370         gatt_db_unregister(gas->db, gas->db_id);
371         gatt_db_unref(gas->db);
372         bt_gatt_client_unref(gas->client);
373
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,
377                                                                         NULL);
378
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);
382
383         return 0;
384 }
385
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
392 };
393
394 static int gap_init(void)
395 {
396         devices = NULL;
397
398         btd_profile_register(&gap_profile);
399
400         return 0;
401 }
402
403 static void gap_exit(void)
404 {
405         btd_profile_unregister(&gap_profile);
406 }
407
408 BLUETOOTH_PLUGIN_DEFINE(gap, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
409                                                         gap_init, gap_exit)