Disconnect rfcomm connections
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-emul / bt-service-event-manager.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19
20 #include "bluetooth-api.h"
21 #include "bt-internal-types.h"
22
23 #include "bt-service-common.h"
24 #include "bt-service-event.h"
25 #include "bt-service-event-manager.h"
26
27 #define MAX_DEVICES 100
28
29 struct uuid_def {
30         char *uuid;
31 };
32
33 static struct uuid_def hid_uuids[] = {
34         { "00001124-0000-1000-8000-00805f9b34fb"        },
35         { "00001200-0000-1000-8000-00805f9b34fb"        },
36 };
37
38 static struct uuid_def tizen_mobile_uuids[] = {
39         { "00001800-0000-1000-8000-00805f9b34fb" },
40         { "00001801-0000-1000-8000-00805f9b34fb" },
41         { "0000110c-0000-1000-8000-00805f9b34fb" },
42         { "0000110a-0000-1000-8000-00805f9b34fb" },
43         { "00001132-0000-1000-8000-00805f9b34fb" },
44         { "00001133-0000-1000-8000-00805f9b34fb" },
45         { "0000112f-0000-1000-8000-00805f9b34fb" },
46         { "00001105-0000-1000-8000-00805f9b34fb" },
47         { "0000111f-0000-1000-8000-00805f9b34fb" },
48         { "00001112-0000-1000-8000-00805f9b34fb" },
49 };
50
51 static struct uuid_def mobile_uuids[] = {
52         { "00001105-0000-1000-8000-00805f9b34fb" },
53         { "0000110a-0000-1000-8000-00805f9b34fb" },
54         { "0000110c-0000-1000-8000-00805f9b34fb" },
55         { "00001112-0000-1000-8000-00805f9b34fb" },
56         { "00001115-0000-1000-8000-00805f9b34fb" },
57         { "00001116-0000-1000-8000-00805f9b34fb" },
58         { "0000111f-0000-1000-8000-00805f9b34fb" },
59         { "0000112d-0000-1000-8000-00805f9b34fb" },
60         { "0000112f-0000-1000-8000-00805f9b34fb" },
61         { "00001132-0000-1000-8000-00805f9b34fb" },
62         { "00001200-0000-1000-8000-00805f9b34fb" },
63         { "00001800-0000-1000-8000-00805f9b34fb" },
64         { "00001801-0000-1000-8000-00805f9b34fb" },
65 };
66
67 static struct uuid_def a2dp_only_uuids[] = {
68         { "0000110b-0000-1000-8000-00805f9b34fb" },
69         { "0000110c-0000-1000-8000-00805f9b34fb" },
70         { "0000110d-0000-1000-8000-00805f9b34fb" },
71         { "0000110e-0000-1000-8000-00805f9b34fb" },
72 };
73
74 static struct uuid_def headset_uuids[] = {
75         { "0000111e-0000-1000-8000-00805f9b34fb" },
76         { "00001108-0000-1000-8000-00805f9b34fb" },
77         { "0000110d-0000-1000-8000-00805f9b34fb" },
78         { "0000110b-0000-1000-8000-00805f9b34fb" },
79         { "0000110e-0000-1000-8000-00805f9b34fb" },
80 };
81
82 struct bt_sample_dev_info_t {
83         int rssi;
84         int class;
85         char *address;
86         char *name;
87         struct uuid_def *uuids;
88         unsigned int uuid_count;
89         gboolean paired;
90         bluetooth_connected_link_t connected;
91         gboolean trust;
92         char *manufacturer_data;
93         int manufacturer_data_len;
94         guchar addr_type;
95 };
96
97 static struct bt_sample_dev_info_t sample_devices[] = {
98         { -69, 2360344, "00:1B:66:01:23:1C", "Sennheiser P", a2dp_only_uuids, sizeof(a2dp_only_uuids) / sizeof(a2dp_only_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR},
99         { -70, 5898764, "A8:7C:01:EF:3C:73", "Galaxy S6 edge", mobile_uuids, sizeof(mobile_uuids) / sizeof(mobile_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR},
100         { -58, 2360324, "50:C9:71:56:30:5A", "Jabra SUPREME a4.18.0", headset_uuids, sizeof(headset_uuids) / sizeof(headset_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR},
101         { -75, 5767692, "AC:5A:14:24:B9:33", "Tizen 3.0 Mobile", tizen_mobile_uuids, sizeof(tizen_mobile_uuids) / sizeof(tizen_mobile_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR},
102         { -60, 9600, "34:15:9E:D4:83:B3", "Apple Wireless Mouse", hid_uuids, sizeof(hid_uuids) / sizeof(hid_uuids[0]), FALSE, 0x00, FALSE, NULL, 0, BDADDR_BREDR},
103 };
104
105 typedef struct {
106         int event_id;
107         guint timer_id;
108 } bt_timer_info_t;
109
110 static GSList *timer_list = NULL;
111 static int sample_device_num = sizeof(sample_devices) / sizeof(sample_devices[0]);
112
113 void _bt_create_event_timer(int event_id, int interval, void *event_cb, void *user_data)
114 {
115         bt_timer_info_t *event_info = NULL;
116
117         BT_DBG("+");
118
119         event_info = g_malloc0(sizeof(bt_timer_info_t));
120         /* Fix : NULL_RETURNS */
121         ret_if(event_info == NULL);
122
123         _bt_delete_event_timer(event_id);
124
125         event_info->event_id = event_id;
126
127         /* Assign a timer id  */
128         event_info->timer_id = g_timeout_add(interval, (GSourceFunc)event_cb, (gpointer)user_data);
129
130         BT_DBG("Create event timer. event id: %d, timer id: %d", event_id, event_info->timer_id);
131
132         timer_list = g_slist_append(timer_list, event_info);
133
134         BT_DBG("-");
135 }
136
137 void _bt_delete_event_timer(int event_id)
138 {
139         GSList *l;
140
141         BT_DBG("+");
142
143         BT_DBG("Remove event timer. event id: %d", event_id);
144
145         for (l = timer_list; l != NULL; l = g_slist_next(l)) {
146                 bt_timer_info_t *info = l->data;
147                 if (info == NULL)
148                         continue;
149
150                 if (info->event_id == event_id) {
151                         BT_DBG("Found the event id");
152                         /* Remove the previous timer */
153                         g_source_remove(info->timer_id);
154                         timer_list = g_slist_remove(timer_list, info);
155                         g_free(info);
156                         break;
157                 }
158         }
159
160         BT_DBG("-");
161 }
162
163 void _bt_delete_all_event_timer(void)
164 {
165         GSList *l;
166
167         BT_DBG("+");
168
169         for (l = timer_list; l != NULL; l = g_slist_next(l)) {
170                 bt_timer_info_t *info = l->data;
171                 if (info == NULL)
172                         continue;
173
174                 g_source_remove(info->timer_id);
175                 timer_list = g_slist_remove(timer_list, info);
176                 g_free(info);
177         }
178
179         g_slist_free(timer_list);
180         timer_list = NULL;
181
182         BT_DBG("-");
183 }
184
185 int _bt_get_sample_device_number(void)
186 {
187         return sample_device_num;
188 }
189
190 bt_remote_dev_info_t *_bt_get_sample_device(int index)
191 {
192         bt_remote_dev_info_t *dev_info;
193
194         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
195         retv_if(dev_info == NULL, NULL);
196
197         dev_info->rssi = sample_devices[index].rssi;
198         dev_info->class = sample_devices[index].class;
199         dev_info->paired = sample_devices[index].paired;
200         dev_info->connected = sample_devices[index].connected;
201         dev_info->trust = sample_devices[index].trust;
202         dev_info->addr_type = sample_devices[index].addr_type;
203
204         dev_info->name = g_strdup(sample_devices[index].name);
205         dev_info->address = g_strdup(sample_devices[index].address);
206
207         dev_info->uuid_count = sample_devices[index].uuid_count;
208
209         if (dev_info->uuid_count > 0) {
210                 int i;
211                 dev_info->uuids = g_malloc0(sizeof(char *) * dev_info->uuid_count);
212
213                 for (i = 0; i < dev_info->uuid_count; i++) {
214                         BT_DBG("uuid[%d]: %s", i, sample_devices[index].uuids[i]);
215                         dev_info->uuids[i] = g_strdup(sample_devices[index].uuids[i].uuid);
216                 }
217         }
218
219         dev_info->manufacturer_data_len = sample_devices[index].manufacturer_data_len;
220
221         if (dev_info->manufacturer_data_len > 0) {
222                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
223                 if (dev_info->manufacturer_data)
224                         memcpy(dev_info->manufacturer_data, sample_devices[index].manufacturer_data,
225                                         dev_info->manufacturer_data_len);
226         }
227
228         return dev_info;
229 }
230