Skip to content

Commit 8fa0c78

Browse files
authored
Move Bluetooth and Broadcast (#52)
1 parent bd8dda6 commit 8fa0c78

73 files changed

Lines changed: 19932 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Source/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@
1717

1818
option(ASCONNECTOR
1919
"Include the asconnector library." OFF)
20+
option(BLUETOOTH
21+
"Include bluetooth library in the build." OFF)
22+
option(BROADCAST
23+
"Include broadcasting library in the build." OFF)
2024

2125
if(ASCONNECTOR)
2226
add_subdirectory(asconnector)
2327
endif()
2428

29+
if(BLUETOOTH)
30+
add_subdirectory(bluetooth)
31+
endif()
32+
33+
if(BROADCAST)
34+
add_subdirectory(broadcast)
35+
endif()
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* If not stated otherwise in this file or this component's LICENSE file the
3+
* following copyright and licenses apply:
4+
*
5+
* Copyright 2021 Metrological
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include "BluetoothUtils.h"
21+
22+
namespace Thunder {
23+
24+
namespace Bluetooth {
25+
26+
int BtUtilsHciForEachDevice(int flag, int (*func)(int dd, int dev_id, long arg),
27+
long arg)
28+
{
29+
struct hci_dev_list_req* dl;
30+
struct hci_dev_req* dr;
31+
int dev_id = -1;
32+
int i, sk, err = 0;
33+
34+
sk = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
35+
if (sk < 0)
36+
return -1;
37+
38+
dl = static_cast<hci_dev_list_req*>(malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)));
39+
if (!dl) {
40+
err = errno;
41+
goto done;
42+
}
43+
44+
memset(dl, 0, HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
45+
46+
dl->dev_num = HCI_MAX_DEV;
47+
dr = dl->dev_req;
48+
49+
if (ioctl(sk, HCIGETDEVLIST, (void*)dl) < 0) {
50+
err = errno;
51+
goto free;
52+
}
53+
54+
for (i = 0; i < dl->dev_num; i++, dr++) {
55+
if (BtUtilsHciTestBit(flag, &dr->dev_opt))
56+
if (!func || func(sk, dr->dev_id, arg)) {
57+
dev_id = dr->dev_id;
58+
break;
59+
}
60+
}
61+
62+
if (dev_id < 0)
63+
err = ENODEV;
64+
65+
free:
66+
free(dl);
67+
68+
done:
69+
close(sk);
70+
errno = err;
71+
72+
return dev_id;
73+
}
74+
75+
int BtUtilsHciDevInfo(int dev_id, struct hci_dev_info* di)
76+
{
77+
int dd, err, ret;
78+
79+
dd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
80+
if (dd < 0)
81+
return dd;
82+
83+
memset(di, 0, sizeof(struct hci_dev_info));
84+
85+
di->dev_id = dev_id;
86+
ret = ioctl(dd, HCIGETDEVINFO, (void*)di);
87+
88+
err = errno;
89+
close(dd);
90+
errno = err;
91+
92+
return ret;
93+
}
94+
95+
int BtUtilsHciDevba(int dev_id, bdaddr_t* bdaddr)
96+
{
97+
struct hci_dev_info di;
98+
99+
memset(&di, 0, sizeof(di));
100+
101+
if (BtUtilsHciDevInfo(dev_id, &di))
102+
return -1;
103+
104+
if (!BtUtilsHciTestBit(HCI_UP, &di.flags)) {
105+
errno = ENETDOWN;
106+
return -1;
107+
}
108+
109+
bacpy(bdaddr, &di.bdaddr);
110+
111+
return 0;
112+
}
113+
114+
int BtUtilsOtherBdaddr(int dd, int dev_id, long arg)
115+
{
116+
struct hci_dev_info di;
117+
di.dev_id = static_cast<uint16_t>(dev_id);
118+
119+
if (ioctl(dd, HCIGETDEVINFO, (void*)&di))
120+
return 0;
121+
122+
if (BtUtilsHciTestBit(HCI_RAW, &di.flags))
123+
return 0;
124+
125+
return bacmp((bdaddr_t*)arg, &di.bdaddr);
126+
}
127+
128+
int BtUtilsSameBdaddr(int dd, int dev_id, long arg)
129+
{
130+
struct hci_dev_info di;
131+
di.dev_id = static_cast<uint16_t>(dev_id);
132+
133+
if (ioctl(dd, HCIGETDEVINFO, (void*)&di))
134+
return 0;
135+
136+
return !bacmp((bdaddr_t*)arg, &di.bdaddr);
137+
}
138+
139+
int BtUtilsHciGetRoute(bdaddr_t* bdaddr)
140+
{
141+
int dev_id;
142+
bdaddr_t* bdaddr_any = static_cast<bdaddr_t*>(calloc(1, sizeof(*bdaddr)));
143+
144+
dev_id = BtUtilsHciForEachDevice(HCI_UP, BtUtilsOtherBdaddr,
145+
(long)(bdaddr ? bdaddr : bdaddr_any));
146+
if (dev_id < 0)
147+
dev_id = BtUtilsHciForEachDevice(HCI_UP, BtUtilsSameBdaddr,
148+
(long)(bdaddr ? bdaddr : bdaddr_any));
149+
150+
free(bdaddr_any);
151+
152+
return dev_id;
153+
}
154+
155+
int BtUtilsBa2Str(const bdaddr_t* ba, char* str)
156+
{
157+
return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
158+
ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
159+
}
160+
161+
int BtUtilsBachk(const char* str)
162+
{
163+
if (!str)
164+
return -1;
165+
166+
if (strlen(str) != 17)
167+
return -1;
168+
169+
while (*str) {
170+
if (!isxdigit(*str++))
171+
return -1;
172+
173+
if (!isxdigit(*str++))
174+
return -1;
175+
176+
if (*str == 0)
177+
break;
178+
179+
if (*str++ != ':')
180+
return -1;
181+
}
182+
183+
return 0;
184+
}
185+
186+
int BtUtilsStr2Ba(const char* str, bdaddr_t* ba)
187+
{
188+
int i;
189+
190+
if (BtUtilsBachk(str) < 0) {
191+
memset(ba, 0, sizeof(*ba));
192+
return -1;
193+
}
194+
195+
for (i = 5; i >= 0; i--, str += 3)
196+
ba->b[i] = strtol(str, NULL, 16);
197+
198+
return 0;
199+
}
200+
201+
int BtUtilsBa2Oui(const bdaddr_t* ba, char* str)
202+
{
203+
return sprintf(str, "%2.2X-%2.2X-%2.2X", ba->b[5], ba->b[4], ba->b[3]);
204+
}
205+
206+
void BtUtilsBaswap(bdaddr_t* dst, const bdaddr_t* src)
207+
{
208+
unsigned char* d = (unsigned char*)dst;
209+
const unsigned char* s = (const unsigned char*)src;
210+
int i;
211+
212+
for (i = 0; i < 6; i++)
213+
d[i] = s[5 - i];
214+
}
215+
216+
} // namespace Bluetooth
217+
218+
} // namespace Thunder

Source/bluetooth/BluetoothUtils.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* If not stated otherwise in this file or this component's LICENSE file the
3+
* following copyright and licenses apply:
4+
*
5+
* Copyright 2021 Metrological
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include "Module.h"
23+
24+
namespace Thunder {
25+
26+
namespace Bluetooth {
27+
28+
EXTERNAL int BtUtilsHciDevba(int dev_id, bdaddr_t *bdaddr);
29+
EXTERNAL int BtUtilsHciGetRoute(bdaddr_t *bdaddr);
30+
EXTERNAL int BtUtilsBa2Str(const bdaddr_t *ba, char *str);
31+
EXTERNAL int BtUtilsStr2Ba(const char *str, bdaddr_t *ba);
32+
EXTERNAL int BtUtilsBa2Oui(const bdaddr_t *ba, char *str);
33+
34+
inline void BtUtilsHciFilterClear(struct hci_filter *f)
35+
{
36+
memset(f, 0, sizeof(*f));
37+
}
38+
39+
inline void BtUtilsHciSetBit(int nr, void *addr)
40+
{
41+
*((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31));
42+
}
43+
44+
inline int BtUtilsHciTestBit(int nr, void *addr)
45+
{
46+
return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31));
47+
}
48+
49+
inline void BtUtilsHciFilterSetEvent(int e, struct hci_filter *f)
50+
{
51+
BtUtilsHciSetBit((e & HCI_FLT_EVENT_BITS), &f->event_mask);
52+
}
53+
54+
inline void BtUtilsHciFilterSetPtype(int t, struct hci_filter *f)
55+
{
56+
BtUtilsHciSetBit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask);
57+
}
58+
59+
} // namespace Bluetooth
60+
61+
} // namespace Thunder

0 commit comments

Comments
 (0)