1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2012 Intel Corporation
7 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
20 #include <sys/socket.h>
21 #include <sys/random.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
26 #include "src/shared/util.h"
27 #include "src/shared/mainloop.h"
31 #define BT_PHY_PORT 45023
34 volatile int ref_count;
38 bt_phy_callback_func_t callback;
47 } __attribute__ ((packed));
49 static bool get_random_bytes(void *buf, size_t num_bytes)
54 fd = open("/dev/urandom", O_RDONLY);
58 len = read(fd, buf, num_bytes);
68 static void phy_rx_callback(int fd, uint32_t events, void *user_data)
70 struct bt_phy *phy = user_data;
73 struct bt_phy_hdr hdr;
74 unsigned char buf[4096];
77 if (events & (EPOLLERR | EPOLLHUP)) {
78 mainloop_remove_fd(fd);
82 iov[0].iov_base = &hdr;
83 iov[0].iov_len = sizeof(hdr);
84 iov[1].iov_base = buf;
85 iov[1].iov_len = sizeof(buf);
87 memset(&msg, 0, sizeof(msg));
91 len = recvmsg(phy->rx_fd, &msg, MSG_DONTWAIT);
95 if ((size_t) len < sizeof(hdr))
98 if (le64_to_cpu(hdr.id) == phy->id)
101 if (len - sizeof(hdr) != le16_to_cpu(hdr.len))
105 phy->callback(le16_to_cpu(hdr.type),
106 buf, len - sizeof(hdr), phy->user_data);
109 static int create_rx_socket(void)
111 struct sockaddr_in addr;
114 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
118 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
123 memset(&addr, 0, sizeof(addr));
124 addr.sin_family = AF_INET;
125 addr.sin_port = htons(BT_PHY_PORT);
126 addr.sin_addr.s_addr = INADDR_BROADCAST;
128 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
136 static int create_tx_socket(void)
140 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
144 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0) {
152 struct bt_phy *bt_phy_new(void)
156 phy = calloc(1, sizeof(*phy));
160 phy->rx_fd = create_rx_socket();
161 if (phy->rx_fd < 0) {
166 phy->tx_fd = create_tx_socket();
167 if (phy->tx_fd < 0) {
173 mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
175 if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
176 if (getrandom(&phy->id, sizeof(phy->id), 0) < 0) {
177 mainloop_remove_fd(phy->rx_fd);
185 bt_phy_send(phy, BT_PHY_PKT_NULL, NULL, 0);
187 return bt_phy_ref(phy);
190 struct bt_phy *bt_phy_ref(struct bt_phy *phy)
195 __sync_fetch_and_add(&phy->ref_count, 1);
200 void bt_phy_unref(struct bt_phy *phy)
205 if (__sync_sub_and_fetch(&phy->ref_count, 1))
208 mainloop_remove_fd(phy->rx_fd);
216 bool bt_phy_send(struct bt_phy *phy, uint16_t type,
217 const void *data, size_t size)
219 return bt_phy_send_vector(phy, type, data, size, NULL, 0, NULL, 0);
222 bool bt_phy_send_vector(struct bt_phy *phy, uint16_t type,
223 const void *data1, size_t size1,
224 const void *data2, size_t size2,
225 const void *data3, size_t size3)
227 struct bt_phy_hdr hdr;
228 struct sockaddr_in addr;
236 memset(&addr, 0, sizeof(addr));
237 addr.sin_family = AF_INET;
238 addr.sin_port = htons(BT_PHY_PORT);
239 addr.sin_addr.s_addr = INADDR_BROADCAST;
241 memset(&msg, 0, sizeof(msg));
242 msg.msg_name = &addr;
243 msg.msg_namelen = sizeof(addr);
247 memset(&hdr, 0, sizeof(hdr));
248 hdr.id = cpu_to_le64(phy->id);
249 hdr.flags = cpu_to_le32(0);
250 hdr.type = cpu_to_le16(type);
251 hdr.len = cpu_to_le16(size1 + size2 + size3);
253 iov[msg.msg_iovlen].iov_base = &hdr;
254 iov[msg.msg_iovlen].iov_len = sizeof(hdr);
257 if (data1 && size1 > 0) {
258 iov[msg.msg_iovlen].iov_base = (void *) data1;
259 iov[msg.msg_iovlen].iov_len = size1;
263 if (data2 && size2 > 0) {
264 iov[msg.msg_iovlen].iov_base = (void *) data2;
265 iov[msg.msg_iovlen].iov_len = size2;
269 if (data3 && size3 > 0) {
270 iov[msg.msg_iovlen].iov_base = (void *) data3;
271 iov[msg.msg_iovlen].iov_len = size3;
275 memset(&addr, 0, sizeof(addr));
276 addr.sin_family = AF_INET;
277 addr.sin_port = htons(BT_PHY_PORT);
278 addr.sin_addr.s_addr = INADDR_BROADCAST;
280 len = sendmsg(phy->tx_fd, &msg, MSG_DONTWAIT);
287 bool bt_phy_register(struct bt_phy *phy, bt_phy_callback_func_t callback,
293 phy->callback = callback;
294 phy->user_data = user_data;