5afbc4e16ad7fd47ecfa1b6aa2fc353c51a0dec4
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-util.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string.h>
21 #include <dbus/dbus-glib.h>
22 #include <dbus/dbus.h>
23 #include <glib.h>
24 #include <dlog.h>
25
26 #include "bluetooth-api.h"
27 #include "bt-service-common.h"
28 #include "bt-service-util.h"
29
30 static GSList *req_list = NULL;
31
32 /* available request id : 0 ~ 244 */
33 #define BT_REQUEST_ID_RANGE_MAX 245
34
35 static int assigned_id;
36 static gboolean req_id_used[BT_REQUEST_ID_RANGE_MAX];
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, DBusGMethodInvocation *context)
88 {
89         request_info_t *info;
90
91         info = g_malloc0(sizeof(request_info_t));
92
93         info->req_id = req_id;
94         info->service_function = service_function;
95         info->context = context;
96
97         req_list = g_slist_append(req_list, info);
98
99         return BLUETOOTH_ERROR_NONE;
100 }
101
102 request_info_t *_bt_get_request_info(int req_id)
103 {
104         GSList *l;
105         request_info_t *info;
106
107         for (l = req_list; l != NULL; l = g_slist_next(l)) {
108                 info = l->data;
109                 if (info == NULL)
110                         continue;
111
112                 if (info->req_id == req_id)
113                         return info;
114         }
115
116         return NULL;
117 }
118
119 /* delete request which has the target req_id */
120 int _bt_delete_request_list(int req_id)
121 {
122         GSList *l;
123         request_info_t *info;
124
125         for (l = req_list; l != NULL; l = g_slist_next(l)) {
126                 info = l->data;
127                 if (info == NULL)
128                         continue;
129
130                 if (info->req_id == req_id) {
131                         req_list = g_slist_remove(req_list, info);
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