2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "bluetooth-api.h"
25 #include "bt-service-common.h"
26 #include "bt-service-util.h"
28 static GSList *req_list = NULL;
30 /* available request id : 0 ~ 244 */
31 #define BT_REQUEST_ID_RANGE_MAX 245
33 static int assigned_id;
34 static gboolean req_id_used[BT_REQUEST_ID_RANGE_MAX];
36 void _bt_init_request_id(void)
39 memset(req_id_used, 0x00, sizeof(req_id_used));
42 int _bt_assign_request_id(void)
46 index = assigned_id + 1;
48 if (index >= BT_REQUEST_ID_RANGE_MAX)
51 while (req_id_used[index] == TRUE) {
52 if (index == assigned_id) {
54 BT_ERR("All request ID is used");
60 if (index >= BT_REQUEST_ID_RANGE_MAX)
65 req_id_used[index] = TRUE;
70 void _bt_delete_request_id(int request_id)
72 ret_if(request_id >= BT_REQUEST_ID_RANGE_MAX);
73 ret_if(request_id < 0);
75 req_id_used[request_id] = FALSE;
78 void _bt_init_request_list(void)
80 _bt_clear_request_list();
83 /* insert request next to head */
84 int _bt_insert_request_list(int req_id, int service_function,
85 char *name, GDBusMethodInvocation *context)
89 info = g_malloc0(sizeof(request_info_t));
90 info->req_id = req_id;
91 info->service_function = service_function;
92 info->context = context;
94 req_list = g_slist_append(req_list, info);
96 return BLUETOOTH_ERROR_NONE;
99 request_info_t *_bt_get_request_info(int req_id)
102 request_info_t *info;
104 for (l = req_list; l != NULL; l = g_slist_next(l)) {
109 if (info->req_id == req_id)
116 /* delete request which has the target req_id */
117 int _bt_delete_request_list(int req_id)
120 request_info_t *info;
122 for (l = req_list; l != NULL; l = g_slist_next(l)) {
127 if (info->req_id == req_id) {
128 req_list = g_slist_remove(req_list, info);
129 _bt_delete_request_id(info->req_id);
131 return BLUETOOTH_ERROR_NONE;
135 return BLUETOOTH_ERROR_NOT_FOUND;
138 void _bt_clear_request_list(void)
141 g_slist_foreach(req_list, (GFunc)g_free, NULL);
142 g_slist_free(req_list);
148 void _bt_service_convert_uuid_type_to_string(char *str, const unsigned char *uuid)
151 ret_if(uuid == NULL);
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]);
159 void _bt_service_convert_uuid_string_to_type(unsigned char *uuid, const char *str)
162 const char *ptr = str;
165 ret_if(uuid == NULL);
169 memcpy(uuid, BT_SERVICE_BASE_UUID, BT_UUID_LENGTH_MAX);
171 while (*ptr && i < BT_UUID_LENGTH_MAX) {
177 if (sscanf(ptr, "%02hhx", &(uuid[i])) != 1)
187 char * _bt_service_convert_hex_to_string(unsigned char *hex, int len)
191 int str_len = (3 * len) + 1;
193 retv_if(hex == NULL, NULL);
194 retv_if(len <= 0, NULL);
196 str = g_malloc0(str_len * sizeof(char));
197 for (i = 0; i < len; i++)
198 snprintf(str + (i * 3), 3 * (len - i), "%02X ", hex[i]);