Add the sender name in signal subscribe function
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / 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 <stdio.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <gio/gio.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 void _bt_init_request_id(void)
37 {
38         assigned_id = 0;
39         memset(req_id_used, 0x00, sizeof(req_id_used));
40 }
41
42 int _bt_assign_request_id(void)
43 {
44         int index;
45
46         index = assigned_id + 1;
47
48         if (index >= BT_REQUEST_ID_RANGE_MAX)
49                 index = 0;
50
51         while (req_id_used[index] == TRUE) {
52                 if (index == assigned_id) {
53                         /* No available ID */
54                         BT_ERR("All request ID is used");
55                         return -1;
56                 }
57
58                 index++;
59
60                 if (index >= BT_REQUEST_ID_RANGE_MAX)
61                         index = 0;
62         }
63
64         assigned_id = index;
65         req_id_used[index] = TRUE;
66
67         return assigned_id;
68 }
69
70 void _bt_delete_request_id(int request_id)
71 {
72         ret_if(request_id >= BT_REQUEST_ID_RANGE_MAX);
73         ret_if(request_id < 0);
74
75         req_id_used[request_id] = FALSE;
76 }
77
78 void _bt_init_request_list(void)
79 {
80         _bt_clear_request_list();
81 }
82
83 /* insert request next to head */
84 int _bt_insert_request_list(int req_id, int service_function,
85                         char *name, GDBusMethodInvocation *context)
86 {
87         request_info_t *info;
88
89         info = g_malloc0(sizeof(request_info_t));
90         info->req_id = req_id;
91         info->service_function = service_function;
92         info->context = context;
93
94         req_list = g_slist_append(req_list, info);
95
96         return BLUETOOTH_ERROR_NONE;
97 }
98
99 request_info_t *_bt_get_request_info(int req_id)
100 {
101         GSList *l;
102         request_info_t *info;
103
104         for (l = req_list; l != NULL; l = g_slist_next(l)) {
105                 info = l->data;
106                 if (info == NULL)
107                         continue;
108
109                 if (info->req_id == req_id)
110                         return info;
111         }
112
113         return NULL;
114 }
115
116 /* delete request which has the target req_id */
117 int _bt_delete_request_list(int req_id)
118 {
119         GSList *l;
120         request_info_t *info;
121
122         for (l = req_list; l != NULL; l = g_slist_next(l)) {
123                 info = l->data;
124                 if (info == NULL)
125                         continue;
126
127                 if (info->req_id == req_id) {
128                         req_list = g_slist_remove(req_list, info);
129                         _bt_delete_request_id(info->req_id);
130                         g_free(info);
131                         return BLUETOOTH_ERROR_NONE;
132                 }
133         }
134
135         return BLUETOOTH_ERROR_NOT_FOUND;
136 }
137
138 void _bt_clear_request_list(void)
139 {
140         if (req_list) {
141                 g_slist_foreach(req_list, (GFunc)g_free, NULL);
142                 g_slist_free(req_list);
143                 req_list = NULL;
144         }
145 }
146
147
148 void _bt_service_convert_uuid_type_to_string(char *str, const unsigned char *uuid)
149 {
150         ret_if(str == NULL);
151         ret_if(uuid == NULL);
152
153         g_snprintf(str, BT_UUID_STRING_SIZE,
154                         "%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
155                         uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
156                         uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
157 }
158
159 void _bt_service_convert_uuid_string_to_type(unsigned char *uuid, const char *str)
160 {
161         int i = 0;
162         const char *ptr = str;
163
164         ret_if(str == NULL);
165         ret_if(uuid == NULL);
166
167         memcpy(uuid, BT_SERVICE_BASE_UUID, BT_UUID_LENGTH_MAX);
168
169         while (*ptr && i < BT_UUID_LENGTH_MAX) {
170                 if (*ptr == '-') {
171                         ptr++;
172                         continue;
173                 }
174
175                 if (sscanf(ptr, "%02hhx", &(uuid[i])) != 1)
176                         break;
177
178                 i++;
179                 ptr += 2;
180         }
181 }
182
183 char * _bt_service_convert_hex_to_string(unsigned char *hex, int len)
184 {
185         int i = 0;
186         char *str = NULL;
187         int str_len = (3 * len) + 1;
188
189         retv_if(hex == NULL, NULL);
190         retv_if(len <= 0, NULL);
191
192         str = g_malloc0(str_len * sizeof(char));
193         for (i = 0; i < len; i++)
194                 snprintf(str + (i * 3), 3 * (len - i), "%02X ", hex[i]);
195
196         str[3 * len] = '\0';
197
198         return str;
199 }
200