Fix TBT socket client fail issue
[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
121         _bt_delete_event_timer(event_id);
122
123         event_info->event_id = event_id;
124
125         /* Assign a timer id  */
126         event_info->timer_id = g_timeout_add(interval, (GSourceFunc)event_cb, (gpointer)user_data);
127
128         BT_DBG("Create event timer. event id: %d, timer id: %d", event_id, event_info->timer_id);
129
130         timer_list = g_slist_append(timer_list, event_info);
131
132         BT_DBG("-");
133 }
134
135 void _bt_delete_event_timer(int event_id)
136 {
137         GSList *l;
138
139         BT_DBG("+");
140
141         BT_DBG("Remove event timer. event id: %d", event_id);
142
143         for (l = timer_list; l != NULL; l = g_slist_next(l)) {
144                 bt_timer_info_t *info = l->data;
145                 if (info == NULL)
146                         continue;
147
148                 if (info->event_id == event_id) {
149                         BT_DBG("Found the event id");
150                         /* Remove the previous timer */
151                         g_source_remove(info->timer_id);
152                         timer_list = g_slist_remove(timer_list, info);
153                         g_free(info);
154                         break;
155                 }
156         }
157
158         BT_DBG("-");
159 }
160
161 void _bt_delete_all_event_timer(void)
162 {
163         GSList *l;
164
165         BT_DBG("+");
166
167         for (l = timer_list; l != NULL; l = g_slist_next(l)) {
168                 bt_timer_info_t *info = l->data;
169                 if (info == NULL)
170                         continue;
171
172                 g_source_remove(info->timer_id);
173                 timer_list = g_slist_remove(timer_list, info);
174                 g_free(info);
175         }
176
177         g_slist_free(timer_list);
178         timer_list = NULL;
179
180         BT_DBG("-");
181 }
182
183 int _bt_get_sample_device_number(void)
184 {
185         return sample_device_num;
186 }
187
188 bt_remote_dev_info_t *_bt_get_sample_device(int index)
189 {
190         bt_remote_dev_info_t *dev_info;
191
192         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
193
194         dev_info->rssi = sample_devices[index].rssi;
195         dev_info->class = sample_devices[index].class;
196         dev_info->paired = sample_devices[index].paired;
197         dev_info->connected = sample_devices[index].connected;
198         dev_info->trust = sample_devices[index].trust;
199         dev_info->addr_type = sample_devices[index].addr_type;
200
201         dev_info->name = g_strdup(sample_devices[index].name);
202         dev_info->address = g_strdup(sample_devices[index].address);
203
204         dev_info->uuid_count = sample_devices[index].uuid_count;
205
206         if (dev_info->uuid_count > 0) {
207                 int i;
208                 dev_info->uuids = g_malloc0(sizeof(char *) * dev_info->uuid_count);
209
210                 for (i = 0; i < dev_info->uuid_count; i++) {
211                         BT_DBG("uuid[%d]: %s", i, sample_devices[index].uuids[i]);
212                         dev_info->uuids[i] = g_strdup(sample_devices[index].uuids[i].uuid);
213                 }
214         }
215
216         dev_info->manufacturer_data_len = sample_devices[index].manufacturer_data_len;
217
218         if (dev_info->manufacturer_data_len > 0) {
219                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
220                 memcpy(dev_info->manufacturer_data, sample_devices[index].manufacturer_data,
221                                 dev_info->manufacturer_data_len);
222         }
223
224         return dev_info;
225 }
226