1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011 Nokia Corporation
* Copyright (C) 2011 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <errno.h>
#include <gdbus.h>
#include <glib.h>
#include <bluetooth/uuid.h>
#include "dbus-common.h"
#include "att.h"
#include "adapter.h"
#include "device.h"
#include "att-database.h"
#include "log.h"
#include "gatt-service.h"
#include "gattrib.h"
#include "attrib-server.h"
#include "gatt.h"
#include "server.h"
#include "profile.h"
#include "error.h"
#define PHONE_ALERT_STATUS_SVC_UUID 0x180E
#define ALERT_NOTIF_SVC_UUID 0x1811
#define ALERT_STATUS_CHR_UUID 0x2A3F
#define RINGER_CP_CHR_UUID 0x2A40
#define RINGER_SETTING_CHR_UUID 0x2A41
#define ALERT_NOTIF_CP_CHR_UUID 0x2A44
#define UNREAD_ALERT_CHR_UUID 0x2A45
#define NEW_ALERT_CHR_UUID 0x2A46
#define SUPP_NEW_ALERT_CAT_CHR_UUID 0x2A47
#define SUPP_UNREAD_ALERT_CAT_CHR_UUID 0x2A48
#define ALERT_OBJECT_PATH "/org/bluez"
#define ALERT_INTERFACE "org.bluez.Alert"
/* Maximum length for "Text String Information" */
#define NEW_ALERT_MAX_INFO_SIZE 18
enum {
ENABLE_NEW_INCOMING,
ENABLE_UNREAD_CAT,
DISABLE_NEW_INCOMING,
DISABLE_UNREAD_CAT,
NOTIFY_NEW_INCOMING,
NOTIFY_UNREAD_CAT,
};
/* Ringer Setting characteristic values */
enum {
RINGER_SILENT,
RINGER_NORMAL,
};
struct alert_data {
const char *category;
char *srv;
char *path;
guint watcher;
};
static GSList *registered_alerts = NULL;
static uint8_t ringer_setting = RINGER_NORMAL;
static uint8_t alert_status = 0;
static const char * const anp_categories[] = {
"simple",
"email",
"news",
"call",
"missed-call",
"sms-mms",
"voice-mail",
"schedule",
"high-priority",
"instant-message",
};
static const char * const pasp_categories[] = {
"ringer",
"vibrate",
"display",
};
static void alert_data_destroy(gpointer user_data)
{
struct alert_data *alert = user_data;
if (alert->watcher)
g_dbus_remove_watch(btd_get_dbus_connection(), alert->watcher);
g_free(alert->srv);
g_free(alert->path);
g_free(alert);
}
static void alert_destroy(gpointer user_data)
{
g_slist_free_full(registered_alerts, alert_data_destroy);
registered_alerts = NULL;
}
static const char *valid_category(const char *category)
{
unsigned i;
for (i = 0; i < G_N_ELEMENTS(anp_categories); i++) {
if (g_str_equal(anp_categories[i], category))
return anp_categories[i];
}
for (i = 0; i < G_N_ELEMENTS(pasp_categories); i++) {
if (g_str_equal(pasp_categories[i], category))
return pasp_categories[i];
}
return NULL;
}
static struct alert_data *get_alert_data_by_category(const char *category)
{
GSList *l;
struct alert_data *alert;
for (l = registered_alerts; l; l = g_slist_next(l)) {
alert = l->data;
if (g_str_equal(alert->category, category))
return alert;
}
return NULL;
}
static gboolean registered_category(const char *category)
{
struct alert_data *alert;
alert = get_alert_data_by_category(category);
if (alert)
return TRUE;
return FALSE;
}
static gboolean pasp_category(const char *category)
{
unsigned i;
for (i = 0; i < G_N_ELEMENTS(pasp_categories); i++)
if (g_str_equal(category, pasp_categories[i]))
return TRUE;
return FALSE;
}
static gboolean valid_description(const char *category,
const char *description)
{
if (!pasp_category(category)) {
if (strlen(description) >= NEW_ALERT_MAX_INFO_SIZE)
return FALSE;
return TRUE;
}
if (g_str_equal(description, "active") ||
g_str_equal(description, "not active"))
return TRUE;
if (g_str_equal(category, "ringer"))
if (g_str_equal(description, "enabled") ||
g_str_equal(description, "disabled"))
return TRUE;
return FALSE;
}
static gboolean valid_count(const char *category, uint16_t count)
{
if (!pasp_category(category) && count > 0 && count <= 255)
return TRUE;
if (pasp_category(category) && count == 1)
return TRUE;
return FALSE;
}
static void watcher_disconnect(DBusConnection *conn, void *user_data)
{
struct alert_data *alert = user_data;
DBG("Category %s was disconnected", alert->category);
registered_alerts = g_slist_remove(registered_alerts, alert);
alert_data_destroy(alert);
}
static DBusMessage *register_alert(DBusConnection *conn, DBusMessage *msg,
void *data)
{
const char *sender = dbus_message_get_sender(msg);
char *path;
const char *category;
const char *c;
struct alert_data *alert;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &c,
DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
category = valid_category(c);
if (!category) {
DBG("Invalid category: %s", c);
return btd_error_invalid_args(msg);
}
if (registered_category(category)) {
DBG("Category %s already registered", category);
return dbus_message_new_method_return(msg);
}
alert = g_new0(struct alert_data, 1);
alert->srv = g_strdup(sender);
alert->path = g_strdup(path);
alert->category = category;
alert->watcher = g_dbus_add_disconnect_watch(conn, alert->srv,
watcher_disconnect, alert, NULL);
if (alert->watcher == 0) {
alert_data_destroy(alert);
DBG("Could not register disconnect watcher");
return btd_error_failed(msg,
"Could not register disconnect watcher");
}
registered_alerts = g_slist_append(registered_alerts, alert);
DBG("RegisterAlert(\"%s\", \"%s\")", alert->category, alert->path);
return dbus_message_new_method_return(msg);
}
static DBusMessage *new_alert(DBusConnection *conn, DBusMessage *msg,
void *data)
{
const char *sender = dbus_message_get_sender(msg);
const char *category, *description;
struct alert_data *alert;
uint16_t count;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &category,
DBUS_TYPE_UINT16, &count, DBUS_TYPE_STRING,
&description, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
alert = get_alert_data_by_category(category);
if (!alert) {
DBG("Category %s not registered", category);
return btd_error_invalid_args(msg);
}
if (!g_str_equal(alert->srv, sender)) {
DBG("Sender %s is not registered in category %s", sender,
category);
return btd_error_invalid_args(msg);
}
if (!valid_description(category, description)) {
DBG("Description %s is invalid for %s category",
description, category);
return btd_error_invalid_args(msg);
}
if (!valid_count(category, count)) {
DBG("Count %d is invalid for %s category", count, category);
return btd_error_invalid_args(msg);
}
DBG("NewAlert(\"%s\", %d, \"%s\")", category, count, description);
return dbus_message_new_method_return(msg);
}
static DBusMessage *unread_alert(DBusConnection *conn, DBusMessage *msg,
void *data)
{
const char *sender = dbus_message_get_sender(msg);
struct alert_data *alert;
const char *category;
uint16_t count;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &category,
DBUS_TYPE_UINT16, &count,
DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
alert = get_alert_data_by_category(category);
if (!alert) {
DBG("Category %s not registered", category);
return btd_error_invalid_args(msg);
}
if (!g_str_equal(alert->srv, sender)) {
DBG("Sender %s is not registered in category %s", sender,
category);
return btd_error_invalid_args(msg);
}
DBG("category %s, count %d", category, count);
return dbus_message_new_method_return(msg);
}
static uint8_t ringer_cp_write(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
DBG("a = %p", a);
return 0;
}
static uint8_t alert_status_read(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
struct btd_adapter *adapter = user_data;
DBG("a = %p", a);
if (a->data == NULL || a->data[0] != alert_status)
attrib_db_update(adapter, a->handle, NULL, &alert_status,
sizeof(alert_status), NULL);
return 0;
}
static uint8_t ringer_setting_read(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
struct btd_adapter *adapter = user_data;
DBG("a = %p", a);
if (a->data == NULL || a->data[0] != ringer_setting)
attrib_db_update(adapter, a->handle, NULL, &ringer_setting,
sizeof(ringer_setting), NULL);
return 0;
}
static void register_phone_alert_service(struct btd_adapter *adapter)
{
bt_uuid_t uuid;
bt_uuid16_create(&uuid, PHONE_ALERT_STATUS_SVC_UUID);
/* Phone Alert Status Service */
gatt_service_add(adapter, GATT_PRIM_SVC_UUID, &uuid,
/* Alert Status characteristic */
GATT_OPT_CHR_UUID, ALERT_STATUS_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ |
ATT_CHAR_PROPER_NOTIFY,
GATT_OPT_CHR_VALUE_CB, ATTRIB_READ,
alert_status_read, adapter,
/* Ringer Control Point characteristic */
GATT_OPT_CHR_UUID, RINGER_CP_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_WRITE_WITHOUT_RESP,
GATT_OPT_CHR_VALUE_CB, ATTRIB_WRITE,
ringer_cp_write, NULL,
/* Ringer Setting characteristic */
GATT_OPT_CHR_UUID, RINGER_SETTING_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ |
ATT_CHAR_PROPER_NOTIFY,
GATT_OPT_CHR_VALUE_CB, ATTRIB_READ,
ringer_setting_read, adapter,
GATT_OPT_INVALID);
}
static uint8_t supp_new_alert_cat_read(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
struct btd_adapter *adapter = user_data;
uint8_t value[] = {0x00, 0x00};
DBG("a = %p", a);
if (a->data == NULL)
attrib_db_update(adapter, a->handle, NULL, value, sizeof(value),
NULL);
return 0;
}
static uint8_t supp_unread_alert_cat_read(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
struct btd_adapter *adapter = user_data;
uint8_t value[] = {0x00, 0x00};
DBG("a = %p", a);
if (a->data == NULL)
attrib_db_update(adapter, a->handle, NULL, value, sizeof(value),
NULL);
return 0;
}
static uint8_t alert_notif_cp_write(struct attribute *a,
struct btd_device *device,
gpointer user_data)
{
DBG("a = %p", a);
switch (a->data[0]) {
case ENABLE_NEW_INCOMING:
DBG("ENABLE_NEW_INCOMING: 0x%02x", a->data[1]);
break;
case ENABLE_UNREAD_CAT:
DBG("ENABLE_UNREAD_CAT: 0x%02x", a->data[1]);
break;
case DISABLE_NEW_INCOMING:
DBG("DISABLE_NEW_INCOMING: 0x%02x", a->data[1]);
break;
case DISABLE_UNREAD_CAT:
DBG("DISABLE_UNREAD_CAT: 0x%02x", a->data[1]);
break;
case NOTIFY_NEW_INCOMING:
DBG("NOTIFY_NEW_INCOMING: 0x%02x", a->data[1]);
break;
case NOTIFY_UNREAD_CAT:
DBG("NOTIFY_UNREAD_CAT: 0x%02x", a->data[1]);
break;
default:
DBG("0x%02x 0x%02x", a->data[0], a->data[1]);
}
return 0;
}
static void register_alert_notif_service(struct btd_adapter *adapter)
{
bt_uuid_t uuid;
bt_uuid16_create(&uuid, ALERT_NOTIF_SVC_UUID);
/* Alert Notification Service */
gatt_service_add(adapter, GATT_PRIM_SVC_UUID, &uuid,
/* Supported New Alert Category */
GATT_OPT_CHR_UUID, SUPP_NEW_ALERT_CAT_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ,
GATT_OPT_CHR_VALUE_CB, ATTRIB_READ,
supp_new_alert_cat_read, adapter,
/* New Alert */
GATT_OPT_CHR_UUID, NEW_ALERT_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_NOTIFY,
/* Supported Unread Alert Category */
GATT_OPT_CHR_UUID, SUPP_UNREAD_ALERT_CAT_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ,
GATT_OPT_CHR_VALUE_CB, ATTRIB_READ,
supp_unread_alert_cat_read, adapter,
/* Unread Alert Status */
GATT_OPT_CHR_UUID, UNREAD_ALERT_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_NOTIFY,
/* Alert Notification Control Point */
GATT_OPT_CHR_UUID, ALERT_NOTIF_CP_CHR_UUID,
GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_WRITE,
GATT_OPT_CHR_VALUE_CB, ATTRIB_WRITE,
alert_notif_cp_write, NULL,
GATT_OPT_INVALID);
}
static int alert_server_probe(struct btd_profile *p,
struct btd_adapter *adapter)
{
register_phone_alert_service(adapter);
register_alert_notif_service(adapter);
return 0;
}
static void alert_server_remove(struct btd_profile *p,
struct btd_adapter *adapter)
{
}
static struct btd_profile alert_profile = {
.name = "gatt-alert-server",
.adapter_probe = alert_server_probe,
.adapter_remove = alert_server_remove,
};
static const GDBusMethodTable alert_methods[] = {
{ GDBUS_METHOD("RegisterAlert",
GDBUS_ARGS({ "category", "s" },
{ "agent", "o" }), NULL,
register_alert) },
{ GDBUS_METHOD("NewAlert",
GDBUS_ARGS({ "category", "s" },
{ "count", "q" },
{ "description", "s" }), NULL,
new_alert) },
{ GDBUS_METHOD("UnreadAlert",
GDBUS_ARGS({ "category", "s" }, { "count", "q" }), NULL,
unread_alert) },
{ }
};
int alert_server_init(void)
{
if (!g_dbus_register_interface(btd_get_dbus_connection(),
ALERT_OBJECT_PATH, ALERT_INTERFACE,
alert_methods, NULL, NULL, NULL,
alert_destroy)) {
error("D-Bus failed to register %s interface",
ALERT_INTERFACE);
return -EIO;
}
btd_profile_register(&alert_profile);
return 0;
}
void alert_server_exit(void)
{
btd_profile_unregister(&alert_profile);
g_dbus_unregister_interface(btd_get_dbus_connection(),
ALERT_OBJECT_PATH, ALERT_INTERFACE);
}
|