7a8f80502a163af312be053e5eb62d48ff700204
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-util.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 <string.h>
19 #include <glib.h>
20 #include <dlog.h>
21 #include <gio/gio.h>
22 #include <dlfcn.h>
23
24 #include "bluetooth-api.h"
25 #include "bt-service-common.h"
26 #include "bt-service-util.h"
27
28 static GSList *req_list = NULL;
29
30 /* available request id : 0 ~ 244 */
31 #define BT_REQUEST_ID_RANGE_MAX 245
32
33 static int assigned_id;
34 static gboolean req_id_used[BT_REQUEST_ID_RANGE_MAX];
35
36 bt_plugin_info_t *headed_plugin_info = NULL;
37
38 void _bt_init_request_id(void)
39 {
40         assigned_id = 0;
41         memset(req_id_used, 0x00, BT_REQUEST_ID_RANGE_MAX);
42 }
43
44 int _bt_assign_request_id(void)
45 {
46         int index;
47
48         index = assigned_id + 1;
49
50         if (index >= BT_REQUEST_ID_RANGE_MAX)
51                 index = 0;
52
53         while (req_id_used[index] == TRUE) {
54                 if (index == assigned_id) {
55                         /* No available ID */
56                         BT_ERR("All request ID is used");
57                         return -1;
58                 }
59
60                 index++;
61
62                 if (index >= BT_REQUEST_ID_RANGE_MAX)
63                         index = 0;
64         }
65
66         assigned_id = index;
67         req_id_used[index] = TRUE;
68
69         return assigned_id;
70 }
71
72 void _bt_delete_request_id(int request_id)
73 {
74         ret_if(request_id >= BT_REQUEST_ID_RANGE_MAX);
75         ret_if(request_id < 0);
76
77         req_id_used[request_id] = FALSE;
78 }
79
80 void _bt_init_request_list(void)
81 {
82         _bt_clear_request_list();
83 }
84
85 /* insert request next to head */
86 int _bt_insert_request_list(int req_id, int service_function,
87                         char *name, GDBusMethodInvocation *context)
88 {
89         request_info_t *info;
90
91         info = g_malloc0(sizeof(request_info_t));
92         info->req_id = req_id;
93         info->service_function = service_function;
94         info->context = context;
95
96         req_list = g_slist_append(req_list, info);
97
98         return BLUETOOTH_ERROR_NONE;
99 }
100
101 request_info_t *_bt_get_request_info(int req_id)
102 {
103         GSList *l;
104         request_info_t *info;
105
106         for (l = req_list; l != NULL; l = g_slist_next(l)) {
107                 info = l->data;
108                 if (info == NULL)
109                         continue;
110
111                 if (info->req_id == req_id)
112                         return info;
113         }
114
115         return NULL;
116 }
117
118 /* delete request which has the target req_id */
119 int _bt_delete_request_list(int req_id)
120 {
121         GSList *l;
122         request_info_t *info;
123
124         for (l = req_list; l != NULL; l = g_slist_next(l)) {
125                 info = l->data;
126                 if (info == NULL)
127                         continue;
128
129                 if (info->req_id == req_id) {
130                         req_list = g_slist_remove(req_list, info);
131                         _bt_delete_request_id(info->req_id);
132                         g_free(info);
133                         return BLUETOOTH_ERROR_NONE;
134                 }
135         }
136
137         return BLUETOOTH_ERROR_NOT_FOUND;
138 }
139
140 void _bt_clear_request_list(void)
141 {
142         if (req_list) {
143                 g_slist_foreach(req_list, (GFunc)g_free, NULL);
144                 g_slist_free(req_list);
145                 req_list = NULL;
146         }
147 }
148
149 void bluetooth_plugin_init()
150 {
151         headed_plugin_info = g_malloc0(sizeof(bt_plugin_info_t));
152         if (!headed_plugin_info) {
153                 BT_ERR("Can not memory alloc headed plugin");
154                 return;
155         }
156
157         /* check ARCH 64 or 32*/
158         if (!access(FILEPATH_ARCH_64, 0)) {
159                 BT_INFO("plugin loading for ARCH 64");
160                 headed_plugin_info->handle_headed = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
161         } else {
162                 BT_INFO("plugin loading for ARCH 32");
163                 headed_plugin_info->handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
164         }
165
166         if (!headed_plugin_info->handle_headed) {
167                 BT_ERR("Can not load plugin %s", dlerror());
168                 headed_plugin_info->plugin_headed_enabled = FALSE;
169                 return;
170         }
171
172         headed_plugin_info->headed_plugin = dlsym(headed_plugin_info->handle_headed, "headed_plugin");
173         if (!headed_plugin_info->headed_plugin) {
174                 BT_ERR("Can not load symbol : %s", dlerror());
175                 dlclose(headed_plugin_info->handle_headed);
176                 headed_plugin_info->plugin_headed_enabled = FALSE;
177                 return;
178         }
179
180         headed_plugin_info->plugin_headed_enabled = TRUE;
181         BT_INFO("Bluetooth Headed Plugin Initialized");
182 }
183
184 void bluetooth_plugin_deinit()
185 {
186         BT_INFO("Bluetooth Headed Plugin Deintialized");
187         if (!headed_plugin_info->plugin_headed_enabled) {
188                 g_free(headed_plugin_info);
189                 headed_plugin_info = NULL;
190                 return;
191         }
192
193         dlclose(headed_plugin_info->handle_headed);
194         headed_plugin_info->plugin_headed_enabled = FALSE;
195         g_free(headed_plugin_info);
196         headed_plugin_info = NULL;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212