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 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
138 BT_ERR("No lines in the file");
142 for (i = 0; lines[i] != NULL; i++) {
143 if (g_str_has_prefix(lines[i], "AddressBlacklist"))
144 if (__bt_agent_find_device_by_address_exactname(
147 if (g_str_has_prefix(lines[i], "ExactNameBlacklist"))
148 if (__bt_agent_find_device_by_address_exactname(
151 if (g_str_has_prefix(lines[i], "PartialNameBlacklist"))
152 if (__bt_agent_find_device_by_partial_name(lines[i],
155 if (g_str_has_prefix(lines[i], "KeyboardAutoPair"))
156 if (__bt_agent_find_device_by_address_exactname(
164 BT_DBG("Found the device");
169 void _bt_agent_release_memory(void)
171 /* Release Malloc Memory*/
174 /* Release Stack Memory*/
178 gboolean _bt_agent_is_auto_response(unsigned int dev_class,
179 const gchar *address, const gchar *name)
181 gboolean is_headset = FALSE;
182 gboolean is_mouse = FALSE;
183 char lap_address[BT_LOWER_ADDRESS_LENGTH];
185 BT_DBG("bt_agent_is_headset_class, [0x%02x] +", dev_class);
190 switch ((dev_class & 0x1f00) >> 8) {
192 switch ((dev_class & 0xfc) >> 2) {
203 case 0x0c: /* Video Camera */
204 case 0x0d: /* Camcorder */
207 /* Other audio device */
213 switch (dev_class & 0xff) {
214 case 0x80: /* 0x80: Pointing device(Mouse) */
218 case 0x40: /* 0x40: input device (BT keyboard) */
219 /* Get the LAP(Lower Address part) */
220 g_strlcpy(lap_address, address, sizeof(lap_address));
222 /* Need to Auto pair the blacklisted Keyboard */
223 if (_bt_agent_is_device_blacklist(lap_address, name) != TRUE) {
224 BT_DBG("Device is not black listed\n");
227 BT_ERR("Device is black listed\n");
238 if ((!is_headset) && (!is_mouse))
241 /* Get the LAP(Lower Address part) */
242 g_strlcpy(lap_address, address, sizeof(lap_address));
244 BT_DBG("Device address = %s\n", address);
245 BT_DBG("Address 3 byte = %s\n", lap_address);
247 if (_bt_agent_is_device_blacklist(lap_address, name)) {
248 BT_ERR("Device is black listed\n");
254 int _bt_agent_generate_passkey(char *passkey, int size)
259 unsigned int value = 0;
267 random_fd = open("/dev/urandom", O_RDONLY);
272 for (i = 0; i < size; i++) {
273 len = read(random_fd, &value, sizeof(value));
275 passkey[i] = '0' + (value % 10);
278 BT_DBG("passkey: %s", passkey);