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