phy.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/socket.h>
  20. #include <netinet/in.h>
  21. #include <netinet/ip.h>
  22. #include <time.h>
  23. #include "src/shared/util.h"
  24. #include "src/shared/mainloop.h"
  25. #include "phy.h"
  26. #define BT_PHY_PORT 45023
  27. struct bt_phy {
  28. volatile int ref_count;
  29. int rx_fd;
  30. int tx_fd;
  31. uint64_t id;
  32. bt_phy_callback_func_t callback;
  33. void *user_data;
  34. };
  35. struct bt_phy_hdr {
  36. uint64_t id;
  37. uint32_t flags;
  38. uint16_t type;
  39. uint16_t len;
  40. } __attribute__ ((packed));
  41. static bool get_random_bytes(void *buf, size_t num_bytes)
  42. {
  43. ssize_t len;
  44. int fd;
  45. fd = open("/dev/urandom", O_RDONLY);
  46. if (fd < 0)
  47. return false;
  48. len = read(fd, buf, num_bytes);
  49. close(fd);
  50. if (len < 0)
  51. return false;
  52. return true;
  53. }
  54. static void phy_rx_callback(int fd, uint32_t events, void *user_data)
  55. {
  56. struct bt_phy *phy = user_data;
  57. struct msghdr msg;
  58. struct iovec iov[2];
  59. struct bt_phy_hdr hdr;
  60. unsigned char buf[4096];
  61. ssize_t len;
  62. if (events & (EPOLLERR | EPOLLHUP)) {
  63. mainloop_remove_fd(fd);
  64. return;
  65. }
  66. iov[0].iov_base = &hdr;
  67. iov[0].iov_len = sizeof(hdr);
  68. iov[1].iov_base = buf;
  69. iov[1].iov_len = sizeof(buf);
  70. memset(&msg, 0, sizeof(msg));
  71. msg.msg_iov = iov;
  72. msg.msg_iovlen = 2;
  73. len = recvmsg(phy->rx_fd, &msg, MSG_DONTWAIT);
  74. if (len < 0)
  75. return;
  76. if ((size_t) len < sizeof(hdr))
  77. return;
  78. if (le64_to_cpu(hdr.id) == phy->id)
  79. return;
  80. if (len - sizeof(hdr) != le16_to_cpu(hdr.len))
  81. return;
  82. if (phy->callback)
  83. phy->callback(le16_to_cpu(hdr.type),
  84. buf, len - sizeof(hdr), phy->user_data);
  85. }
  86. static int create_rx_socket(void)
  87. {
  88. struct sockaddr_in addr;
  89. int fd, opt = 1;
  90. fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  91. if (fd < 0)
  92. return -1;
  93. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
  94. close(fd);
  95. return -1;
  96. }
  97. memset(&addr, 0, sizeof(addr));
  98. addr.sin_family = AF_INET;
  99. addr.sin_port = htons(BT_PHY_PORT);
  100. addr.sin_addr.s_addr = INADDR_BROADCAST;
  101. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  102. close(fd);
  103. return -1;
  104. }
  105. return fd;
  106. }
  107. static int create_tx_socket(void)
  108. {
  109. int fd, opt = 1;
  110. fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  111. if (fd < 0)
  112. return -1;
  113. if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0) {
  114. close(fd);
  115. return -1;
  116. }
  117. return fd;
  118. }
  119. struct bt_phy *bt_phy_new(void)
  120. {
  121. struct bt_phy *phy;
  122. phy = calloc(1, sizeof(*phy));
  123. if (!phy)
  124. return NULL;
  125. phy->rx_fd = create_rx_socket();
  126. if (phy->rx_fd < 0) {
  127. free(phy);
  128. return NULL;
  129. }
  130. phy->tx_fd = create_tx_socket();
  131. if (phy->tx_fd < 0) {
  132. close(phy->rx_fd);
  133. free(phy);
  134. return NULL;
  135. }
  136. mainloop_add_fd(phy->rx_fd, EPOLLIN, phy_rx_callback, phy, NULL);
  137. if (!get_random_bytes(&phy->id, sizeof(phy->id))) {
  138. srandom(time(NULL));
  139. phy->id = random();
  140. }
  141. bt_phy_send(phy, BT_PHY_PKT_NULL, NULL, 0);
  142. return bt_phy_ref(phy);
  143. }
  144. struct bt_phy *bt_phy_ref(struct bt_phy *phy)
  145. {
  146. if (!phy)
  147. return NULL;
  148. __sync_fetch_and_add(&phy->ref_count, 1);
  149. return phy;
  150. }
  151. void bt_phy_unref(struct bt_phy *phy)
  152. {
  153. if (!phy)
  154. return;
  155. if (__sync_sub_and_fetch(&phy->ref_count, 1))
  156. return;
  157. mainloop_remove_fd(phy->rx_fd);
  158. close(phy->tx_fd);
  159. close(phy->rx_fd);
  160. free(phy);
  161. }
  162. bool bt_phy_send(struct bt_phy *phy, uint16_t type,
  163. const void *data, size_t size)
  164. {
  165. return bt_phy_send_vector(phy, type, data, size, NULL, 0, NULL, 0);
  166. }
  167. bool bt_phy_send_vector(struct bt_phy *phy, uint16_t type,
  168. const void *data1, size_t size1,
  169. const void *data2, size_t size2,
  170. const void *data3, size_t size3)
  171. {
  172. struct bt_phy_hdr hdr;
  173. struct sockaddr_in addr;
  174. struct msghdr msg;
  175. struct iovec iov[4];
  176. ssize_t len;
  177. if (!phy)
  178. return false;
  179. memset(&addr, 0, sizeof(addr));
  180. addr.sin_family = AF_INET;
  181. addr.sin_port = htons(BT_PHY_PORT);
  182. addr.sin_addr.s_addr = INADDR_BROADCAST;
  183. memset(&msg, 0, sizeof(msg));
  184. msg.msg_name = &addr;
  185. msg.msg_namelen = sizeof(addr);
  186. msg.msg_iov = iov;
  187. msg.msg_iovlen = 0;
  188. memset(&hdr, 0, sizeof(hdr));
  189. hdr.id = cpu_to_le64(phy->id);
  190. hdr.flags = cpu_to_le32(0);
  191. hdr.type = cpu_to_le16(type);
  192. hdr.len = cpu_to_le16(size1 + size2 + size3);
  193. iov[msg.msg_iovlen].iov_base = &hdr;
  194. iov[msg.msg_iovlen].iov_len = sizeof(hdr);
  195. msg.msg_iovlen++;
  196. if (data1 && size1 > 0) {
  197. iov[msg.msg_iovlen].iov_base = (void *) data1;
  198. iov[msg.msg_iovlen].iov_len = size1;
  199. msg.msg_iovlen++;
  200. }
  201. if (data2 && size2 > 0) {
  202. iov[msg.msg_iovlen].iov_base = (void *) data2;
  203. iov[msg.msg_iovlen].iov_len = size2;
  204. msg.msg_iovlen++;
  205. }
  206. if (data3 && size3 > 0) {
  207. iov[msg.msg_iovlen].iov_base = (void *) data3;
  208. iov[msg.msg_iovlen].iov_len = size3;
  209. msg.msg_iovlen++;
  210. }
  211. memset(&addr, 0, sizeof(addr));
  212. addr.sin_family = AF_INET;
  213. addr.sin_port = htons(BT_PHY_PORT);
  214. addr.sin_addr.s_addr = INADDR_BROADCAST;
  215. len = sendmsg(phy->tx_fd, &msg, MSG_DONTWAIT);
  216. if (len < 0)
  217. return false;
  218. return true;
  219. }
  220. bool bt_phy_register(struct bt_phy *phy, bt_phy_callback_func_t callback,
  221. void *user_data)
  222. {
  223. if (!phy)
  224. return false;
  225. phy->callback = callback;
  226. phy->user_data = user_data;
  227. return true;
  228. }