Merge "Fix NULL Returns in bt-api/bt-hid-device.c" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-util.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <string.h>
25 #include <glib.h>
26 #include <dlog.h>
27 #include <gio/gio.h>
28
29 #include "bluetooth-api.h"
30 #include "bt-service-common.h"
31 #include "bt-service-util.h"
32
33 static GSList *req_list = NULL;
34
35 /* available request id : 0 ~ 244 */
36 #define BT_REQUEST_ID_RANGE_MAX 245
37
38 static int assigned_id;
39 static gboolean req_id_used[BT_REQUEST_ID_RANGE_MAX];
40
41 void _bt_init_request_id(void)
42 {
43         assigned_id = 0;
44         memset(req_id_used, 0x00, BT_REQUEST_ID_RANGE_MAX);
45 }
46
47 int _bt_assign_request_id(void)
48 {
49         int index;
50
51         index = assigned_id + 1;
52
53         if (index >= BT_REQUEST_ID_RANGE_MAX)
54                 index = 0;
55
56         while (req_id_used[index] == TRUE) {
57                 if (index == assigned_id) {
58                         /* No available ID */
59                         BT_ERR("All request ID is used");
60                         return -1;
61                 }
62
63                 index++;
64
65                 if (index >= BT_REQUEST_ID_RANGE_MAX)
66                         index = 0;
67         }
68
69         assigned_id = index;
70         req_id_used[index] = TRUE;
71
72         return assigned_id;
73 }
74
75 void _bt_delete_request_id(int request_id)
76 {
77         ret_if(request_id >= BT_REQUEST_ID_RANGE_MAX);
78         ret_if(request_id < 0);
79
80         req_id_used[request_id] = FALSE;
81 }
82
83 void _bt_init_request_list(void)
84 {
85         _bt_clear_request_list();
86 }
87
88 /* insert request next to head */
89 int _bt_insert_request_list(int req_id, int service_function,
90                         char *name, GDBusMethodInvocation *context)
91 {
92         request_info_t *info;
93
94         info = g_malloc0(sizeof(request_info_t));
95         /* Fix : NULL_RETURNS */
96         retv_if(info == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
97
98         info->req_id = req_id;
99         info->service_function = service_function;
100         info->context = context;
101
102         req_list = g_slist_append(req_list, info);
103
104         return BLUETOOTH_ERROR_NONE;
105 }
106
107 request_info_t *_bt_get_request_info(int req_id)
108 {
109         GSList *l;
110         request_info_t *info;
111
112         for (l = req_list; l != NULL; l = g_slist_next(l)) {
113                 info = l->data;
114                 if (info == NULL)
115                         continue;
116
117                 if (info->req_id == req_id)
118                         return info;
119         }
120
121         return NULL;
122 }
123
124 /* delete request which has the target req_id */
125 int _bt_delete_request_list(int req_id)
126 {
127         GSList *l;
128         request_info_t *info;
129
130         for (l = req_list; l != NULL; l = g_slist_next(l)) {
131                 info = l->data;
132                 if (info == NULL)
133                         continue;
134
135                 if (info->req_id == req_id) {
136                         req_list = g_slist_remove(req_list, info);
137                         _bt_delete_request_id(info->req_id);
138                         g_free(info);
139                         return BLUETOOTH_ERROR_NONE;
140                 }
141         }
142
143         return BLUETOOTH_ERROR_NOT_FOUND;
144 }
145
146 void _bt_clear_request_list(void)
147 {
148         if (req_list) {
149                 g_slist_foreach(req_list, (GFunc)g_free, NULL);
150                 g_slist_free(req_list);
151                 req_list = NULL;
152         }
153 }
154