__SAMSUNG_PATCH__ --> __TIZEN_PATCH__
[framework/connectivity/bluez.git] / proximity / reporter.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011  Nokia Corporation
6  *  Copyright (C) 2011  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <glib.h>
30 #include <bluetooth/uuid.h>
31 #include <adapter.h>
32
33 #include "log.h"
34
35 #include "hcid.h"
36 #include "att.h"
37 #include "gattrib.h"
38 #include "attrib-server.h"
39 #include "reporter.h"
40
41 #define IMMEDIATE_ALERT_SVC_UUID        0x1802
42 #define LINK_LOSS_SVC_UUID              0x1803
43 #define TX_POWER_SVC_UUID               0x1804
44 #define ALERT_LEVEL_CHR_UUID            0x2A06
45 #define POWER_LEVEL_CHR_UUID            0x2A07
46
47 enum {
48         NO_ALERT = 0x00,
49         MILD_ALERT = 0x01,
50         HIGH_ALERT = 0x02,
51 };
52
53 static uint16_t tx_power_handle;
54
55 static void register_link_loss(void)
56 {
57         uint16_t start_handle, h;
58         const int svc_size = 3;
59         uint8_t atval[256];
60         bt_uuid_t uuid;
61
62         /* FIXME: Provide the adapter in next function */
63         start_handle = attrib_db_find_avail(NULL, svc_size);
64         if (start_handle == 0) {
65                 error("Not enough free handles to register service");
66                 return;
67         }
68
69         DBG("start_handle=0x%04x", start_handle);
70
71         h = start_handle;
72
73         /* Primary service definition */
74         bt_uuid16_create(&uuid, GATT_PRIM_SVC_UUID);
75         att_put_u16(LINK_LOSS_SVC_UUID, &atval[0]);
76         /* FIXME: Provide the adapter in next function */
77         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 2);
78
79         /* Alert level characteristic */
80         bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
81         atval[0] = ATT_CHAR_PROPER_READ | ATT_CHAR_PROPER_WRITE;
82         att_put_u16(h + 1, &atval[1]);
83         att_put_u16(ALERT_LEVEL_CHR_UUID, &atval[3]);
84         /* FIXME: Provide the adapter in next function */
85         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 5);
86
87         /* Alert level value */
88         bt_uuid16_create(&uuid, ALERT_LEVEL_CHR_UUID);
89         att_put_u8(NO_ALERT, &atval[0]);
90         /* FIXME: Provide the adapter in next function */
91         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NONE, atval, 1);
92
93         g_assert(h - start_handle == svc_size);
94 }
95
96 static void register_tx_power(void)
97 {
98         uint16_t start_handle, h;
99         const int svc_size = 4;
100         uint8_t atval[256];
101         bt_uuid_t uuid;
102
103         /* FIXME: Provide the adapter in next function */
104         start_handle = attrib_db_find_avail(NULL, svc_size);
105         if (start_handle == 0) {
106                 error("Not enough free handles to register service");
107                 return;
108         }
109
110         DBG("start_handle=0x%04x", start_handle);
111
112         h = start_handle;
113
114         /* Primary service definition */
115         bt_uuid16_create(&uuid, GATT_PRIM_SVC_UUID);
116         att_put_u16(TX_POWER_SVC_UUID, &atval[0]);
117         /* FIXME: Provide the adapter in next function */
118         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 2);
119
120         /* Power level characteristic */
121         bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
122         atval[0] = ATT_CHAR_PROPER_READ | ATT_CHAR_PROPER_NOTIFY;
123         att_put_u16(h + 1, &atval[1]);
124         att_put_u16(POWER_LEVEL_CHR_UUID, &atval[3]);
125         /* FIXME: Provide the adapter in next function */
126         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 5);
127
128         /* Power level value */
129         bt_uuid16_create(&uuid, POWER_LEVEL_CHR_UUID);
130         att_put_u8(0x00, &atval[0]);
131         tx_power_handle = h;
132         /* FIXME: Provide the adapter in next function */
133         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 1);
134
135         /* Client characteristic configuration */
136         bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
137         atval[0] = 0x00;
138         atval[1] = 0x00;
139         /* FIXME: Provide the adapter in next function */
140         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NONE, atval, 2);
141
142         g_assert(h - start_handle == svc_size);
143 }
144
145 static void register_immediate_alert(void)
146 {
147         uint16_t start_handle, h;
148         const int svc_size = 3;
149         uint8_t atval[256];
150         bt_uuid_t uuid;
151
152         /* FIXME: Provide the adapter in next function */
153         start_handle = attrib_db_find_avail(NULL, svc_size);
154         if (start_handle == 0) {
155                 error("Not enough free handles to register service");
156                 return;
157         }
158
159         DBG("start_handle=0x%04x", start_handle);
160
161         h = start_handle;
162
163         /* Primary service definition */
164         bt_uuid16_create(&uuid, GATT_PRIM_SVC_UUID);
165         att_put_u16(IMMEDIATE_ALERT_SVC_UUID, &atval[0]);
166         /* FIXME: Provide the adapter in next function */
167         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 2);
168
169         /* Alert level characteristic */
170         bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
171         atval[0] = ATT_CHAR_PROPER_WRITE_WITHOUT_RESP;
172         att_put_u16(h + 1, &atval[1]);
173         att_put_u16(ALERT_LEVEL_CHR_UUID, &atval[3]);
174         /* FIXME: Provide the adapter in next function */
175         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NOT_PERMITTED, atval, 5);
176
177         /* Alert level value */
178         bt_uuid16_create(&uuid, ALERT_LEVEL_CHR_UUID);
179         att_put_u8(NO_ALERT, &atval[0]);
180         /* FIXME: Provide the adapter in next function */
181         attrib_db_add(NULL, h++, &uuid, ATT_NONE, ATT_NONE, atval, 1);
182
183         g_assert(h - start_handle == svc_size);
184 }
185
186 int reporter_init(void)
187 {
188         if (!main_opts.attrib_server) {
189                 DBG("Attribute server is disabled");
190                 return -1;
191         }
192
193         DBG("Proximity Reporter");
194
195         register_link_loss();
196         register_tx_power();
197         register_immediate_alert();
198
199         return 0;
200 }
201
202 void reporter_exit(void)
203 {
204 }