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