4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <stacktrim.h>
28 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
31 gboolean _bt_agent_is_hid_keyboard(unsigned int dev_class)
33 switch ((dev_class & 0x1f00) >> 8) {
35 switch ((dev_class & 0xc0) >> 6) {
50 static gboolean __bt_agent_find_device_by_address_exactname(char *buffer,
56 pch = strtok_r(buffer, "= ,", &last);
61 while ((pch = strtok_r(NULL, ",", &last))) {
62 if (0 == g_strcmp0(pch, address)) {
63 BT_DBG("Match found\n");
70 static gboolean __bt_agent_find_device_by_partial_name(char *buffer,
71 const char *partial_name)
76 pch = strtok_r(buffer, "= ,", &last);
81 while ((pch = strtok_r(NULL, ",", &last))) {
82 if (g_str_has_prefix(partial_name, pch)) {
83 BT_DBG("Match found\n");
90 gboolean _bt_agent_is_device_blacklist(const char *address,
102 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
105 BT_ERR("Unable to open blacklist file");
109 fseek(fp, 0, SEEK_END);
112 BT_DBG("size is not a positive number");
119 buffer = g_malloc0(sizeof(char) * size);
120 /* Fix : NULL_RETURNS */
121 if (buffer == NULL) {
122 BT_ERR("Fail to allocate memory");
126 result = fread((char *)buffer, 1, size, fp);
128 if (result != size) {
129 BT_ERR("Read Error");
134 BT_DBG("Buffer = %s", buffer);
136 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
140 BT_ERR("No lines in the file");
144 for (i = 0; lines[i] != NULL; i++) {
145 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
146 if (__bt_agent_find_device_by_address_exactname(
149 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
150 if (__bt_agent_find_device_by_address_exactname(
153 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
154 if (__bt_agent_find_device_by_partial_name(lines[i],
157 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
158 if (__bt_agent_find_device_by_address_exactname(
166 BT_DBG("Found the device");
171 void _bt_agent_release_memory(void)
173 /* Release Malloc Memory*/
176 /* Release Stack Memory*/
180 gboolean _bt_agent_is_auto_response(unsigned int dev_class,
181 const gchar *address, const gchar *name)
183 gboolean is_headset = FALSE;
184 gboolean is_mouse = FALSE;
185 char lap_address[BT_LOWER_ADDRESS_LENGTH];
187 BT_DBG("bt_agent_is_headset_class, [0x%02x] +", dev_class);
192 switch ((dev_class & 0x1f00) >> 8) {
194 switch ((dev_class & 0xfc) >> 2) {
205 case 0x0c: /* Video Camera */
206 case 0x0d: /* Camcorder */
209 /* Other audio device */
215 switch (dev_class & 0xff) {
216 case 0x80: /* 0x80: Pointing device(Mouse) */
220 case 0x40: /* 0x40: input device (BT keyboard) */
221 /* Get the LAP(Lower Address part) */
222 g_strlcpy(lap_address, address, sizeof(lap_address));
224 /* Need to Auto pair the blacklisted Keyboard */
225 if (_bt_agent_is_device_blacklist(lap_address, name) != TRUE) {
226 BT_DBG("Device is not black listed\n");
229 BT_ERR("Device is black listed\n");
240 if ((!is_headset) && (!is_mouse))
243 /* Get the LAP(Lower Address part) */
244 g_strlcpy(lap_address, address, sizeof(lap_address));
246 BT_DBG("Device address = %s\n", address);
247 BT_DBG("Address 3 byte = %s\n", lap_address);
249 if (_bt_agent_is_device_blacklist(lap_address, name)) {
250 BT_ERR("Device is black listed\n");
256 int _bt_agent_generate_passkey(char *passkey, int size)
261 unsigned int value = 0;
269 random_fd = open("/dev/urandom", O_RDONLY);
274 for (i = 0; i < size; i++) {
275 len = read(random_fd, &value, sizeof(value));
277 passkey[i] = '0' + (value % 10);
280 BT_DBG("passkey: %s", passkey);