Fix incorrect memory validation for g_malloc*()
[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
23 #include "bluetooth-api.h"
24 #include "bt-service-common.h"
25 #include "bt-service-util.h"
26
27 static GSList *req_list = NULL;
28
29 /* available request id : 0 ~ 244 */
30 #define BT_REQUEST_ID_RANGE_MAX 245
31
32 static int assigned_id;
33 static gboolean req_id_used[BT_REQUEST_ID_RANGE_MAX];
34
35 void _bt_init_request_id(void)
36 {
37         assigned_id = 0;
38         memset(req_id_used, 0x00, BT_REQUEST_ID_RANGE_MAX);
39 }
40
41 int _bt_assign_request_id(void)
42 {
43         int index;
44
45         index = assigned_id + 1;
46
47         if (index >= BT_REQUEST_ID_RANGE_MAX)
48                 index = 0;
49
50         while (req_id_used[index] == TRUE) {
51                 if (index == assigned_id) {
52                         /* No available ID */
53                         BT_ERR("All request ID is used");
54                         return -1;
55                 }
56
57                 index++;
58
59                 if (index >= BT_REQUEST_ID_RANGE_MAX)
60                         index = 0;
61         }
62
63         assigned_id = index;
64         req_id_used[index] = TRUE;
65
66         return assigned_id;
67 }
68
69 void _bt_delete_request_id(int request_id)
70 {
71         ret_if(request_id >= BT_REQUEST_ID_RANGE_MAX);
72         ret_if(request_id < 0);
73
74         req_id_used[request_id] = FALSE;
75 }
76
77 void _bt_init_request_list(void)
78 {
79         _bt_clear_request_list();
80 }
81
82 /* insert request next to head */
83 int _bt_insert_request_list(int req_id, int service_function,
84                         char *name, GDBusMethodInvocation *context)
85 {
86         request_info_t *info;
87
88         info = g_malloc0(sizeof(request_info_t));
89         info->req_id = req_id;
90         info->service_function = service_function;
91         info->context = context;
92
93         req_list = g_slist_append(req_list, info);
94
95         return BLUETOOTH_ERROR_NONE;
96 }
97
98 request_info_t *_bt_get_request_info(int req_id)
99 {
100         GSList *l;
101         request_info_t *info;
102
103         for (l = req_list; l != NULL; l = g_slist_next(l)) {
104                 info = l->data;
105                 if (info == NULL)
106                         continue;
107
108                 if (info->req_id == req_id)
109                         return info;
110         }
111
112         return NULL;
113 }
114
115 /* delete request which has the target req_id */
116 int _bt_delete_request_list(int req_id)
117 {
118         GSList *l;
119         request_info_t *info;
120
121         for (l = req_list; l != NULL; l = g_slist_next(l)) {
122                 info = l->data;
123                 if (info == NULL)
124                         continue;
125
126                 if (info->req_id == req_id) {
127                         req_list = g_slist_remove(req_list, info);
128                         _bt_delete_request_id(info->req_id);
129                         g_free(info);
130                         return BLUETOOTH_ERROR_NONE;
131                 }
132         }
133
134         return BLUETOOTH_ERROR_NOT_FOUND;
135 }
136
137 void _bt_clear_request_list(void)
138 {
139         if (req_list) {
140                 g_slist_foreach(req_list, (GFunc)g_free, NULL);
141                 g_slist_free(req_list);
142                 req_list = NULL;
143         }
144 }
145