1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2000-2001 Qualcomm Incorporated 6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk (at) qualcomm.com> 7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel (at) holtmann.org> 8 * 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 */ 25 26 #ifdef HAVE_CONFIG_H 27 #include <config.h> 28 #endif 29 30 #include <stdio.h> 31 #include <errno.h> 32 #include <ctype.h> 33 #include <stdarg.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/socket.h> 37 38 #include "bluetooth.h" 39 #include "hci.h" 40 41 void baswap(bdaddr_t *dst, const bdaddr_t *src) 42 { 43 register unsigned char *d = (unsigned char *) dst; 44 register const unsigned char *s = (const unsigned char *) src; 45 register int i; 46 47 for (i = 0; i < 6; i++) 48 d[i] = s[5-i]; 49 } 50 51 char *batostr(const bdaddr_t *ba) 52 { 53 char *str = bt_malloc(18); 54 if (!str) 55 return NULL; 56 57 sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", 58 ba->b[0], ba->b[1], ba->b[2], 59 ba->b[3], ba->b[4], ba->b[5]); 60 61 return str; 62 } 63 64 bdaddr_t *strtoba(const char *str) 65 { 66 bdaddr_t b; 67 bdaddr_t *ba = bt_malloc(sizeof(*ba)); 68 69 if (ba) { 70 str2ba(str, &b); 71 baswap(ba, &b); 72 } 73 74 return ba; 75 } 76 77 int ba2str(const bdaddr_t *ba, char *str) 78 { 79 return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", 80 ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]); 81 } 82 83 int str2ba(const char *str, bdaddr_t *ba) 84 { 85 bdaddr_t b; 86 int i; 87 88 if (bachk(str) < 0) { 89 memset(ba, 0, sizeof(*ba)); 90 return -1; 91 } 92 93 for (i = 0; i < 6; i++, str += 3) 94 b.b[i] = strtol(str, NULL, 16); 95 96 baswap(ba, &b); 97 98 return 0; 99 } 100 101 int ba2oui(const bdaddr_t *ba, char *str) 102 { 103 return sprintf(str, "%2.2X-%2.2X-%2.2X", ba->b[5], ba->b[4], ba->b[3]); 104 } 105 106 int bachk(const char *str) 107 { 108 if (!str) 109 return -1; 110 111 if (strlen(str) != 17) 112 return -1; 113 114 while (*str) { 115 if (!isxdigit(*str++)) 116 return -1; 117 118 if (!isxdigit(*str++)) 119 return -1; 120 121 if (*str == 0) 122 break; 123 124 if (*str++ != ':') 125 return -1; 126 } 127 128 return 0; 129 } 130 131 int baprintf(const char *format, ...) 132 { 133 va_list ap; 134 int len; 135 136 va_start(ap, format); 137 len = vprintf(format, ap); 138 va_end(ap); 139 140 return len; 141 } 142 143 int bafprintf(FILE *stream, const char *format, ...) 144 { 145 va_list ap; 146 int len; 147 148 va_start(ap, format); 149 len = vfprintf(stream, format, ap); 150 va_end(ap); 151 152 return len; 153 } 154 155 int basprintf(char *str, const char *format, ...) 156 { 157 va_list ap; 158 int len; 159 160 va_start(ap, format); 161 len = vsnprintf(str, (~0U) >> 1, format, ap); 162 va_end(ap); 163 164 return len; 165 } 166 167 int basnprintf(char *str, size_t size, const char *format, ...) 168 { 169 va_list ap; 170 int len; 171 172 va_start(ap, format); 173 len = vsnprintf(str, size, format, ap); 174 va_end(ap); 175 176 return len; 177 } 178 179 void *bt_malloc(size_t size) 180 { 181 return malloc(size); 182 } 183 184 void bt_free(void *ptr) 185 { 186 free(ptr); 187 } 188 189 /* Bluetooth error codes to Unix errno mapping */ 190 int bt_error(uint16_t code) 191 { 192 switch (code) { 193 case 0: 194 return 0; 195 case HCI_UNKNOWN_COMMAND: 196 return EBADRQC; 197 case HCI_NO_CONNECTION: 198 return ENOTCONN; 199 case HCI_HARDWARE_FAILURE: 200 return EIO; 201 case HCI_PAGE_TIMEOUT: 202 return EHOSTDOWN; 203 case HCI_AUTHENTICATION_FAILURE: 204 return EACCES; 205 case HCI_PIN_OR_KEY_MISSING: 206 return EINVAL; 207 case HCI_MEMORY_FULL: 208 return ENOMEM; 209 case HCI_CONNECTION_TIMEOUT: 210 return ETIMEDOUT; 211 case HCI_MAX_NUMBER_OF_CONNECTIONS: 212 case HCI_MAX_NUMBER_OF_SCO_CONNECTIONS: 213 return EMLINK; 214 case HCI_ACL_CONNECTION_EXISTS: 215 return EALREADY; 216 case HCI_COMMAND_DISALLOWED: 217 case HCI_TRANSACTION_COLLISION: 218 case HCI_ROLE_SWITCH_PENDING: 219 return EBUSY; 220 case HCI_REJECTED_LIMITED_RESOURCES: 221 case HCI_REJECTED_PERSONAL: 222 case HCI_QOS_REJECTED: 223 return ECONNREFUSED; 224 case HCI_HOST_TIMEOUT: 225 return ETIMEDOUT; 226 case HCI_UNSUPPORTED_FEATURE: 227 case HCI_QOS_NOT_SUPPORTED: 228 case HCI_PAIRING_NOT_SUPPORTED: 229 case HCI_CLASSIFICATION_NOT_SUPPORTED: 230 case HCI_UNSUPPORTED_LMP_PARAMETER_VALUE: 231 case HCI_PARAMETER_OUT_OF_RANGE: 232 case HCI_QOS_UNACCEPTABLE_PARAMETER: 233 return EOPNOTSUPP; 234 case HCI_INVALID_PARAMETERS: 235 case HCI_SLOT_VIOLATION: 236 return EINVAL; 237 case HCI_OE_USER_ENDED_CONNECTION: 238 case HCI_OE_LOW_RESOURCES: 239 case HCI_OE_POWER_OFF: 240 return ECONNRESET; 241 case HCI_CONNECTION_TERMINATED: 242 return ECONNABORTED; 243 case HCI_REPEATED_ATTEMPTS: 244 return ELOOP; 245 case HCI_REJECTED_SECURITY: 246 case HCI_PAIRING_NOT_ALLOWED: 247 case HCI_INSUFFICIENT_SECURITY: 248 return EACCES; 249 case HCI_UNSUPPORTED_REMOTE_FEATURE: 250 return EPROTONOSUPPORT; 251 case HCI_SCO_OFFSET_REJECTED: 252 return ECONNREFUSED; 253 case HCI_UNKNOWN_LMP_PDU: 254 case HCI_INVALID_LMP_PARAMETERS: 255 case HCI_LMP_ERROR_TRANSACTION_COLLISION: 256 case HCI_LMP_PDU_NOT_ALLOWED: 257 case HCI_ENCRYPTION_MODE_NOT_ACCEPTED: 258 return EPROTO; 259 default: 260 return ENOSYS; 261 } 262 } 263 264 char *bt_compidtostr(int compid) 265 { 266 switch (compid) { 267 case 0: 268 return "Ericsson Technology Licensing"; 269 case 1: 270 return "Nokia Mobile Phones"; 271 case 2: 272 return "Intel Corp."; 273 case 3: 274 return "IBM Corp."; 275 case 4: 276 return "Toshiba Corp."; 277 case 5: 278 return "3Com"; 279 case 6: 280 return "Microsoft"; 281 case 7: 282 return "Lucent"; 283 case 8: 284 return "Motorola"; 285 case 9: 286 return "Infineon Technologies AG"; 287 case 10: 288 return "Cambridge Silicon Radio"; 289 case 11: 290 return "Silicon Wave"; 291 case 12: 292 return "Digianswer A/S"; 293 case 13: 294 return "Texas Instruments Inc."; 295 case 14: 296 return "Parthus Technologies Inc."; 297 case 15: 298 return "Broadcom Corporation"; 299 case 16: 300 return "Mitel Semiconductor"; 301 case 17: 302 return "Widcomm, Inc."; 303 case 18: 304 return "Zeevo, Inc."; 305 case 19: 306 return "Atmel Corporation"; 307 case 20: 308 return "Mitsubishi Electric Corporation"; 309 case 21: 310 return "RTX Telecom A/S"; 311 case 22: 312 return "KC Technology Inc."; 313 case 23: 314 return "Newlogic"; 315 case 24: 316 return "Transilica, Inc."; 317 case 25: 318 return "Rohde & Schwartz GmbH & Co. KG"; 319 case 26: 320 return "TTPCom Limited"; 321 case 27: 322 return "Signia Technologies, Inc."; 323 case 28: 324 return "Conexant Systems Inc."; 325 case 29: 326 return "Qualcomm"; 327 case 30: 328 return "Inventel"; 329 case 31: 330 return "AVM Berlin"; 331 case 32: 332 return "BandSpeed, Inc."; 333 case 33: 334 return "Mansella Ltd"; 335 case 34: 336 return "NEC Corporation"; 337 case 35: 338 return "WavePlus Technology Co., Ltd."; 339 case 36: 340 return "Alcatel"; 341 case 37: 342 return "Philips Semiconductors"; 343 case 38: 344 return "C Technologies"; 345 case 39: 346 return "Open Interface"; 347 case 40: 348 return "R F Micro Devices"; 349 case 41: 350 return "Hitachi Ltd"; 351 case 42: 352 return "Symbol Technologies, Inc."; 353 case 43: 354 return "Tenovis"; 355 case 44: 356 return "Macronix International Co. Ltd."; 357 case 45: 358 return "GCT Semiconductor"; 359 case 46: 360 return "Norwood Systems"; 361 case 47: 362 return "MewTel Technology Inc."; 363 case 48: 364 return "ST Microelectronics"; 365 case 49: 366 return "Synopsys"; 367 case 50: 368 return "Red-M (Communications) Ltd"; 369 case 51: 370 return "Commil Ltd"; 371 case 52: 372 return "Computer Access Technology Corporation (CATC)"; 373 case 53: 374 return "Eclipse (HQ Espana) S.L."; 375 case 54: 376 return "Renesas Technology Corp."; 377 case 55: 378 return "Mobilian Corporation"; 379 case 56: 380 return "Terax"; 381 case 57: 382 return "Integrated System Solution Corp."; 383 case 58: 384 return "Matsushita Electric Industrial Co., Ltd."; 385 case 59: 386 return "Gennum Corporation"; 387 case 60: 388 return "Research In Motion"; 389 case 61: 390 return "IPextreme, Inc."; 391 case 62: 392 return "Systems and Chips, Inc"; 393 case 63: 394 return "Bluetooth SIG, Inc"; 395 case 64: 396 return "Seiko Epson Corporation"; 397 case 65: 398 return "Integrated Silicon Solution Taiwain, Inc."; 399 case 66: 400 return "CONWISE Technology Corporation Ltd"; 401 case 67: 402 return "PARROT SA"; 403 case 68: 404 return "Socket Communications"; 405 case 69: 406 return "Atheros Communications, Inc."; 407 case 70: 408 return "MediaTek, Inc."; 409 case 71: 410 return "Bluegiga"; 411 case 72: 412 return "Marvell Technology Group Ltd."; 413 case 73: 414 return "3DSP Corporation"; 415 case 74: 416 return "Accel Semiconductor Ltd."; 417 case 75: 418 return "Continental Automotive Systems"; 419 case 76: 420 return "Apple, Inc."; 421 case 77: 422 return "Staccato Communications, Inc."; 423 case 78: 424 return "Avago Technologies"; 425 case 79: 426 return "APT Ltd."; 427 case 80: 428 return "SiRF Technology, Inc."; 429 case 81: 430 return "Tzero Technologies, Inc."; 431 case 82: 432 return "J&M Corporation"; 433 case 83: 434 return "Free2move AB"; 435 case 84: 436 return "3DiJoy Corporation"; 437 case 85: 438 return "Plantronics, Inc."; 439 case 86: 440 return "Sony Ericsson Mobile Communications"; 441 case 87: 442 return "Harman International Industries, Inc."; 443 case 88: 444 return "Vizio, Inc."; 445 case 89: 446 return "Nordic Semiconductor ASA"; 447 case 90: 448 return "EM Microelectronic-Marin SA"; 449 case 91: 450 return "Ralink Technology Corporation"; 451 case 92: 452 return "Belkin International, Inc."; 453 case 93: 454 return "Realtek Semiconductor Corporation"; 455 case 94: 456 return "Stonestreet One, LLC"; 457 case 95: 458 return "Wicentric, Inc."; 459 case 96: 460 return "RivieraWaves S.A.S"; 461 case 97: 462 return "RDA Microelectronics"; 463 case 98: 464 return "Gibson Guitars"; 465 case 65535: 466 return "internal use"; 467 default: 468 return "not assigned"; 469 } 470 } 471