Path: blob/next/external/cache/sources/hcitools/lib/bluetooth.c
17850 views
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2000-2001 Qualcomm Incorporated5* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>6* Copyright (C) 2002-2010 Marcel Holtmann <[email protected]>7*8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17* GNU General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA22*23*/2425#ifdef HAVE_CONFIG_H26#include <config.h>27#endif2829#include <stdio.h>30#include <errno.h>31#include <ctype.h>32#include <stdarg.h>33#include <stdlib.h>34#include <string.h>35#include <sys/socket.h>3637#include "bluetooth.h"38#include "hci.h"3940void baswap(bdaddr_t *dst, const bdaddr_t *src)41{42register unsigned char *d = (unsigned char *) dst;43register const unsigned char *s = (const unsigned char *) src;44register int i;4546for (i = 0; i < 6; i++)47d[i] = s[5-i];48}4950char *batostr(const bdaddr_t *ba)51{52char *str = bt_malloc(18);53if (!str)54return NULL;5556sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",57ba->b[0], ba->b[1], ba->b[2],58ba->b[3], ba->b[4], ba->b[5]);5960return str;61}6263bdaddr_t *strtoba(const char *str)64{65bdaddr_t b;66bdaddr_t *ba = bt_malloc(sizeof(*ba));6768if (ba) {69str2ba(str, &b);70baswap(ba, &b);71}7273return ba;74}7576int ba2str(const bdaddr_t *ba, char *str)77{78return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",79ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);80}8182int str2ba(const char *str, bdaddr_t *ba)83{84int i;8586if (bachk(str) < 0) {87memset(ba, 0, sizeof(*ba));88return -1;89}9091for (i = 5; i >= 0; i--, str += 3)92ba->b[i] = strtol(str, NULL, 16);9394return 0;95}9697int ba2oui(const bdaddr_t *ba, char *str)98{99return sprintf(str, "%2.2X-%2.2X-%2.2X", ba->b[5], ba->b[4], ba->b[3]);100}101102int bachk(const char *str)103{104if (!str)105return -1;106107if (strlen(str) != 17)108return -1;109110while (*str) {111if (!isxdigit(*str++))112return -1;113114if (!isxdigit(*str++))115return -1;116117if (*str == 0)118break;119120if (*str++ != ':')121return -1;122}123124return 0;125}126127int baprintf(const char *format, ...)128{129va_list ap;130int len;131132va_start(ap, format);133len = vprintf(format, ap);134va_end(ap);135136return len;137}138139int bafprintf(FILE *stream, const char *format, ...)140{141va_list ap;142int len;143144va_start(ap, format);145len = vfprintf(stream, format, ap);146va_end(ap);147148return len;149}150151int basprintf(char *str, const char *format, ...)152{153va_list ap;154int len;155156va_start(ap, format);157len = vsnprintf(str, (~0U) >> 1, format, ap);158va_end(ap);159160return len;161}162163int basnprintf(char *str, size_t size, const char *format, ...)164{165va_list ap;166int len;167168va_start(ap, format);169len = vsnprintf(str, size, format, ap);170va_end(ap);171172return len;173}174175void *bt_malloc(size_t size)176{177return malloc(size);178}179180void bt_free(void *ptr)181{182free(ptr);183}184185/* Bluetooth error codes to Unix errno mapping */186int bt_error(uint16_t code)187{188switch (code) {189case 0:190return 0;191case HCI_UNKNOWN_COMMAND:192return EBADRQC;193case HCI_NO_CONNECTION:194return ENOTCONN;195case HCI_HARDWARE_FAILURE:196return EIO;197case HCI_PAGE_TIMEOUT:198return EHOSTDOWN;199case HCI_AUTHENTICATION_FAILURE:200return EACCES;201case HCI_PIN_OR_KEY_MISSING:202return EINVAL;203case HCI_MEMORY_FULL:204return ENOMEM;205case HCI_CONNECTION_TIMEOUT:206return ETIMEDOUT;207case HCI_MAX_NUMBER_OF_CONNECTIONS:208case HCI_MAX_NUMBER_OF_SCO_CONNECTIONS:209return EMLINK;210case HCI_ACL_CONNECTION_EXISTS:211return EALREADY;212case HCI_COMMAND_DISALLOWED:213case HCI_TRANSACTION_COLLISION:214case HCI_ROLE_SWITCH_PENDING:215return EBUSY;216case HCI_REJECTED_LIMITED_RESOURCES:217case HCI_REJECTED_PERSONAL:218case HCI_QOS_REJECTED:219return ECONNREFUSED;220case HCI_HOST_TIMEOUT:221return ETIMEDOUT;222case HCI_UNSUPPORTED_FEATURE:223case HCI_QOS_NOT_SUPPORTED:224case HCI_PAIRING_NOT_SUPPORTED:225case HCI_CLASSIFICATION_NOT_SUPPORTED:226case HCI_UNSUPPORTED_LMP_PARAMETER_VALUE:227case HCI_PARAMETER_OUT_OF_RANGE:228case HCI_QOS_UNACCEPTABLE_PARAMETER:229return EOPNOTSUPP;230case HCI_INVALID_PARAMETERS:231case HCI_SLOT_VIOLATION:232return EINVAL;233case HCI_OE_USER_ENDED_CONNECTION:234case HCI_OE_LOW_RESOURCES:235case HCI_OE_POWER_OFF:236return ECONNRESET;237case HCI_CONNECTION_TERMINATED:238return ECONNABORTED;239case HCI_REPEATED_ATTEMPTS:240return ELOOP;241case HCI_REJECTED_SECURITY:242case HCI_PAIRING_NOT_ALLOWED:243case HCI_INSUFFICIENT_SECURITY:244return EACCES;245case HCI_UNSUPPORTED_REMOTE_FEATURE:246return EPROTONOSUPPORT;247case HCI_SCO_OFFSET_REJECTED:248return ECONNREFUSED;249case HCI_UNKNOWN_LMP_PDU:250case HCI_INVALID_LMP_PARAMETERS:251case HCI_LMP_ERROR_TRANSACTION_COLLISION:252case HCI_LMP_PDU_NOT_ALLOWED:253case HCI_ENCRYPTION_MODE_NOT_ACCEPTED:254return EPROTO;255default:256return ENOSYS;257}258}259260const char *bt_compidtostr(int compid)261{262switch (compid) {263case 0:264return "Ericsson Technology Licensing";265case 1:266return "Nokia Mobile Phones";267case 2:268return "Intel Corp.";269case 3:270return "IBM Corp.";271case 4:272return "Toshiba Corp.";273case 5:274return "3Com";275case 6:276return "Microsoft";277case 7:278return "Lucent";279case 8:280return "Motorola";281case 9:282return "Infineon Technologies AG";283case 10:284return "Cambridge Silicon Radio";285case 11:286return "Silicon Wave";287case 12:288return "Digianswer A/S";289case 13:290return "Texas Instruments Inc.";291case 14:292return "Ceva, Inc. (formerly Parthus Technologies, Inc.)";293case 15:294return "Broadcom Corporation";295case 16:296return "Mitel Semiconductor";297case 17:298return "Widcomm, Inc";299case 18:300return "Zeevo, Inc.";301case 19:302return "Atmel Corporation";303case 20:304return "Mitsubishi Electric Corporation";305case 21:306return "RTX Telecom A/S";307case 22:308return "KC Technology Inc.";309case 23:310return "NewLogic";311case 24:312return "Transilica, Inc.";313case 25:314return "Rohde & Schwarz GmbH & Co. KG";315case 26:316return "TTPCom Limited";317case 27:318return "Signia Technologies, Inc.";319case 28:320return "Conexant Systems Inc.";321case 29:322return "Qualcomm";323case 30:324return "Inventel";325case 31:326return "AVM Berlin";327case 32:328return "BandSpeed, Inc.";329case 33:330return "Mansella Ltd";331case 34:332return "NEC Corporation";333case 35:334return "WavePlus Technology Co., Ltd.";335case 36:336return "Alcatel";337case 37:338return "Philips Semiconductors";339case 38:340return "C Technologies";341case 39:342return "Open Interface";343case 40:344return "R F Micro Devices";345case 41:346return "Hitachi Ltd";347case 42:348return "Symbol Technologies, Inc.";349case 43:350return "Tenovis";351case 44:352return "Macronix International Co. Ltd.";353case 45:354return "GCT Semiconductor";355case 46:356return "Norwood Systems";357case 47:358return "MewTel Technology Inc.";359case 48:360return "ST Microelectronics";361case 49:362return "Synopsis";363case 50:364return "Red-M (Communications) Ltd";365case 51:366return "Commil Ltd";367case 52:368return "Computer Access Technology Corporation (CATC)";369case 53:370return "Eclipse (HQ Espana) S.L.";371case 54:372return "Renesas Technology Corp.";373case 55:374return "Mobilian Corporation";375case 56:376return "Terax";377case 57:378return "Integrated System Solution Corp.";379case 58:380return "Matsushita Electric Industrial Co., Ltd.";381case 59:382return "Gennum Corporation";383case 60:384return "Research In Motion";385case 61:386return "IPextreme, Inc.";387case 62:388return "Systems and Chips, Inc.";389case 63:390return "Bluetooth SIG, Inc.";391case 64:392return "Seiko Epson Corporation";393case 65:394return "Integrated Silicon Solution Taiwan, Inc.";395case 66:396return "CONWISE Technology Corporation Ltd";397case 67:398return "PARROT SA";399case 68:400return "Socket Mobile";401case 69:402return "Atheros Communications, Inc.";403case 70:404return "MediaTek, Inc.";405case 71:406return "Bluegiga";407case 72:408return "Marvell Technology Group Ltd.";409case 73:410return "3DSP Corporation";411case 74:412return "Accel Semiconductor Ltd.";413case 75:414return "Continental Automotive Systems";415case 76:416return "Apple, Inc.";417case 77:418return "Staccato Communications, Inc.";419case 78:420return "Avago Technologies";421case 79:422return "APT Licensing Ltd.";423case 80:424return "SiRF Technology";425case 81:426return "Tzero Technologies, Inc.";427case 82:428return "J&M Corporation";429case 83:430return "Free2move AB";431case 84:432return "3DiJoy Corporation";433case 85:434return "Plantronics, Inc.";435case 86:436return "Sony Ericsson Mobile Communications";437case 87:438return "Harman International Industries, Inc.";439case 88:440return "Vizio, Inc.";441case 89:442return "Nordic Semiconductor ASA";443case 90:444return "EM Microelectronic-Marin SA";445case 91:446return "Ralink Technology Corporation";447case 92:448return "Belkin International, Inc.";449case 93:450return "Realtek Semiconductor Corporation";451case 94:452return "Stonestreet One, LLC";453case 95:454return "Wicentric, Inc.";455case 96:456return "RivieraWaves S.A.S";457case 97:458return "RDA Microelectronics";459case 98:460return "Gibson Guitars";461case 99:462return "MiCommand Inc.";463case 100:464return "Band XI International, LLC";465case 101:466return "Hewlett-Packard Company";467case 102:468return "9Solutions Oy";469case 103:470return "GN Netcom A/S";471case 104:472return "General Motors";473case 105:474return "A&D Engineering, Inc.";475case 106:476return "MindTree Ltd.";477case 107:478return "Polar Electro OY";479case 108:480return "Beautiful Enterprise Co., Ltd.";481case 109:482return "BriarTek, Inc.";483case 110:484return "Summit Data Communications, Inc.";485case 111:486return "Sound ID";487case 112:488return "Monster, LLC";489case 113:490return "connectBlue AB";491case 114:492return "ShangHai Super Smart Electronics Co. Ltd.";493case 115:494return "Group Sense Ltd.";495case 116:496return "Zomm, LLC";497case 117:498return "Samsung Electronics Co. Ltd.";499case 118:500return "Creative Technology Ltd.";501case 119:502return "Laird Technologies";503case 120:504return "Nike, Inc.";505case 121:506return "lesswire AG";507case 122:508return "MStar Semiconductor, Inc.";509case 123:510return "Hanlynn Technologies";511case 124:512return "A & R Cambridge";513case 125:514return "Seers Technology Co. Ltd";515case 126:516return "Sports Tracking Technologies Ltd.";517case 127:518return "Autonet Mobile";519case 128:520return "DeLorme Publishing Company, Inc.";521case 129:522return "WuXi Vimicro";523case 130:524return "Sennheiser Communications A/S";525case 131:526return "TimeKeeping Systems, Inc.";527case 132:528return "Ludus Helsinki Ltd.";529case 133:530return "BlueRadios, Inc.";531case 134:532return "equinox AG";533case 135:534return "Garmin International, Inc.";535case 136:536return "Ecotest";537case 137:538return "GN ReSound A/S";539case 138:540return "Jawbone";541case 139:542return "Topcorn Positioning Systems, LLC";543case 140:544return "Qualcomm Labs, Inc.";545case 141:546return "Zscan Software";547case 142:548return "Quintic Corp.";549case 143:550return "Stollman E+V GmbH";551case 144:552return "Funai Electric Co., Ltd.";553case 145:554return "Advanced PANMOBIL Systems GmbH & Co. KG";555case 146:556return "ThinkOptics, Inc.";557case 147:558return "Universal Electronics, Inc.";559case 148:560return "Airoha Technology Corp.";561case 149:562return "NEC Lighting, Ltd.";563case 150:564return "ODM Technology, Inc.";565case 151:566return "ConnecteDevice Ltd.";567case 152:568return "zer01.tv GmbH";569case 153:570return "i.Tech Dynamic Global Distribution Ltd.";571case 154:572return "Alpwise";573case 155:574return "Jiangsu Toppower Automotive Electronics Co., Ltd.";575case 156:576return "Colorfy, Inc.";577case 157:578return "Geoforce Inc.";579case 158:580return "Bose Corporation";581case 159:582return "Suunto Oy";583case 160:584return "Kensington Computer Products Group";585case 161:586return "SR-Medizinelektronik";587case 162:588return "Vertu Corporation Limited";589case 163:590return "Meta Watch Ltd.";591case 164:592return "LINAK A/S";593case 165:594return "OTL Dynamics LLC";595case 166:596return "Panda Ocean Inc.";597case 167:598return "Visteon Corporation";599case 168:600return "ARP Devices Limited";601case 169:602return "Magneti Marelli S.p.A";603case 170:604return "CAEN RFID srl";605case 171:606return "Ingenieur-Systemgruppe Zahn GmbH";607case 172:608return "Green Throttle Games";609case 173:610return "Peter Systemtechnik GmbH";611case 174:612return "Omegawave Oy";613case 175:614return "Cinetix";615case 176:616return "Passif Semiconductor Corp";617case 177:618return "Saris Cycling Group, Inc";619case 178:620return "Bekey A/S";621case 179:622return "Clarinox Technologies Pty. Ltd.";623case 180:624return "BDE Technology Co., Ltd.";625case 181:626return "Swirl Networks";627case 182:628return "Meso international";629case 183:630return "TreLab Ltd";631case 184:632return "Qualcomm Innovation Center, Inc. (QuIC)";633case 185:634return "Johnson Controls, Inc.";635case 186:636return "Starkey Laboratories Inc.";637case 187:638return "S-Power Electronics Limited";639case 188:640return "Ace Sensor Inc";641case 189:642return "Aplix Corporation";643case 190:644return "AAMP of America";645case 191:646return "Stalmart Technology Limited";647case 192:648return "AMICCOM Electronics Corporation";649case 193:650return "Shenzhen Excelsecu Data Technology Co.,Ltd";651case 194:652return "Geneq Inc.";653case 195:654return "adidas AG";655case 196:656return "LG Electronics";657case 197:658return "Onset Computer Corporation";659case 198:660return "Selfly BV";661case 199:662return "Quuppa Oy.";663case 200:664return "GeLo Inc";665case 201:666return "Evluma";667case 202:668return "MC10";669case 203:670return "Binauric SE";671case 204:672return "Beats Electronics";673case 205:674return "Microchip Technology Inc.";675case 206:676return "Elgato Systems GmbH";677case 207:678return "ARCHOS SA";679case 209:680return "Polar Electro Europe B.V.";681case 210:682return "Dialog Semiconductor B.V.";683case 211:684return "Taixingbang Technology (HK) Co,. LTD.";685case 212:686return "Kawantech";687case 213:688return "Austco Communication Systems";689case 214:690return "Timex Group USA, Inc.";691case 215:692return "Qualcomm Technologies, Inc.";693case 216:694return "Qualcomm Connected Experiences, Inc.";695case 217:696return "Voyetra Turtle Beach";697case 218:698return "txtr GmbH";699case 219:700return "Biosentronics";701case 220:702return "Procter & Gamble";703case 221:704return "Hosiden Corporation";705case 222:706return "Muzik LLC";707case 223:708return "Misfit Wearables Corp";709case 224:710return "Google";711case 225:712return "Danlers Ltd";713case 226:714return "Semilink Inc";715case 227:716return "inMusic Brands, Inc";717case 228:718return "L.S. Research Inc.";719case 229:720return "Eden Software Consultants Ltd.";721case 230:722return "Freshtemp";723case 231:724return "KS Technologies";725case 232:726return "ACTS Technologies";727case 233:728return "Vtrack Systems";729case 234:730return "Nielsen-Kellerman Company";731case 235:732return "Server Technology, Inc.";733case 236:734return "BioResearch Associates";735case 237:736return "Jolly Logic, LLC";737case 238:738return "Above Average Outcomes, Inc.";739case 239:740return "Bitsplitters GmbH";741case 240:742return "PayPal, Inc.";743case 241:744return "Witron Technology Limited";745case 242:746return "Morse Project Inc.";747case 243:748return "Kent Displays Inc.";749case 244:750return "Nautilus Inc.";751case 245:752return "Smartifier Oy";753case 246:754return "Elcometer Limited";755case 247:756return "VSN Technologies Inc.";757case 248:758return "AceUni Corp., Ltd.";759case 65535:760return "internal use";761default:762return "not assigned";763}764}765766767