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 <netinet/in.h>
22 #include <netinet/ip.h>
25 #include "src/shared/util.h"
26 #include "src/shared/mainloop.h"
30 #define BT_PHY_PORT 45023
33 volatile int ref_count;
37 bt_phy_callback_func_t callback;
46 } __attribute__ ((packed));
48 static bool get_random_bytes(void *buf, size_t num_bytes)
53 fd = open("/dev/urandom", O_RDONLY);
57 len = read(fd, buf, num_bytes);
67 static void phy_rx_callback(int fd, uint32_t events, void *user_data)
69 struct bt_phy *phy = user_data;
72 struct bt_phy_hdr hdr;
73 unsigned char buf[4096];
76 if (events & (EPOLLERR | EPOLLHUP)) {
77 mainloop_remove_fd(fd);
81 iov[0].iov_base = &hdr;
82 iov[0].iov_len = sizeof(hdr);
83 iov[1].iov_base = buf;
84 iov[1].iov_len = sizeof(buf);
86 memset(&msg, 0, sizeof(msg));
90 len = recvmsg(phy->rx_fd, &msg, MSG_DONTWAIT);
94 if ((size_t) len < sizeof(hdr))
97 if (le64_to_cpu(hdr.id) == phy->id)
100 if (len - sizeof(hdr) != le16_to_cpu(hdr.len))
104 phy->callback(le16_to_cpu(hdr.type),
105 buf, len - sizeof(hdr), phy->user_data);
108 static int create_rx_socket(void)
110 struct sockaddr_in addr;
113 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
117 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
122 memset(&addr, 0, sizeof(addr));
123 addr.sin_family = AF_INET;
124 addr.sin_port = htons(BT_PHY_PORT);
125 addr.sin_addr.s_addr = INADDR_BROADCAST;
127 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
135 static int create_tx_socket(void)
139 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
143 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0) {
151 struct bt_phy *bt_phy_new(void)
155 phy = calloc(1, sizeof(*phy));
159 phy->rx_fd = create_rx_socket();
160 if (phy->rx_fd < 0) {
165 phy->tx_fd = create_tx_socket();
166 if (phy->tx_fd < 0) {
172 mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
174 if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
179 bt_phy_send(phy, BT_PHY_PKT_NULL, NULL, 0);
181 return bt_phy_ref(phy);
184 struct bt_phy *bt_phy_ref(struct bt_phy *phy)
189 __sync_fetch_and_add(&phy->ref_count, 1);
194 void bt_phy_unref(struct bt_phy *phy)
199 if (__sync_sub_and_fetch(&phy->ref_count, 1))
202 mainloop_remove_fd(phy->rx_fd);
210 bool bt_phy_send(struct bt_phy *phy, uint16_t type,
211 const void *data, size_t size)
213 return bt_phy_send_vector(phy, type, data, size, NULL, 0, NULL, 0);
216 bool bt_phy_send_vector(struct bt_phy *phy, uint16_t type,
217 const void *data1, size_t size1,
218 const void *data2, size_t size2,
219 const void *data3, size_t size3)
221 struct bt_phy_hdr hdr;
222 struct sockaddr_in addr;
230 memset(&addr, 0, sizeof(addr));
231 addr.sin_family = AF_INET;
232 addr.sin_port = htons(BT_PHY_PORT);
233 addr.sin_addr.s_addr = INADDR_BROADCAST;
235 memset(&msg, 0, sizeof(msg));
236 msg.msg_name = &addr;
237 msg.msg_namelen = sizeof(addr);
241 memset(&hdr, 0, sizeof(hdr));
242 hdr.id = cpu_to_le64(phy->id);
243 hdr.flags = cpu_to_le32(0);
244 hdr.type = cpu_to_le16(type);
245 hdr.len = cpu_to_le16(size1 + size2 + size3);
247 iov[msg.msg_iovlen].iov_base = &hdr;
248 iov[msg.msg_iovlen].iov_len = sizeof(hdr);
251 if (data1 && size1 > 0) {
252 iov[msg.msg_iovlen].iov_base = (void *) data1;
253 iov[msg.msg_iovlen].iov_len = size1;
257 if (data2 && size2 > 0) {
258 iov[msg.msg_iovlen].iov_base = (void *) data2;
259 iov[msg.msg_iovlen].iov_len = size2;
263 if (data3 && size3 > 0) {
264 iov[msg.msg_iovlen].iov_base = (void *) data3;
265 iov[msg.msg_iovlen].iov_len = size3;
269 memset(&addr, 0, sizeof(addr));
270 addr.sin_family = AF_INET;
271 addr.sin_port = htons(BT_PHY_PORT);
272 addr.sin_addr.s_addr = INADDR_BROADCAST;
274 len = sendmsg(phy->tx_fd, &msg, MSG_DONTWAIT);
281 bool bt_phy_register(struct bt_phy *phy, bt_phy_callback_func_t callback,
287 phy->callback = callback;
288 phy->user_data = user_data;