if-sock.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "if-main.h"
  12. #include "pollhandler.h"
  13. #include "../hal-utils.h"
  14. const btsock_interface_t *if_sock = NULL;
  15. SINTMAP(btsock_type_t, -1, "(unknown)")
  16. DELEMENT(BTSOCK_RFCOMM),
  17. DELEMENT(BTSOCK_SCO),
  18. DELEMENT(BTSOCK_L2CAP),
  19. ENDMAP
  20. #define MAX_LISTEN_FD 15
  21. static int listen_fd[MAX_LISTEN_FD];
  22. static int listen_fd_count;
  23. static const char * const uuids[] = {
  24. "00001101", "00001105", "0000112f", NULL
  25. };
  26. /*
  27. * This function reads data from file descriptor and
  28. * prints it to the user
  29. */
  30. static void receive_from_client(struct pollfd *pollfd)
  31. {
  32. char buf[16];
  33. /*
  34. * Buffer for lines:
  35. * 41 42 43 20 20 00 31 32 00 07 04 00 00 00 00 00 ABC .12.....
  36. */
  37. char outbuf[sizeof(buf) * 4 + 2];
  38. int i;
  39. int ret;
  40. if (pollfd->revents & POLLHUP) {
  41. haltest_error("Disconnected fd=%d\n", pollfd->fd);
  42. poll_unregister_fd(pollfd->fd, receive_from_client);
  43. } else if (pollfd->revents & POLLIN) {
  44. haltest_info("receiving from client fd=%d\n", pollfd->fd);
  45. do {
  46. memset(outbuf, ' ', sizeof(outbuf));
  47. outbuf[sizeof(outbuf) - 1] = 0;
  48. ret = recv(pollfd->fd, buf, sizeof(buf), MSG_DONTWAIT);
  49. for (i = 0; i < ret; ++i)
  50. sprintf(outbuf + i * 3, "%02X ",
  51. (unsigned) buf[i]);
  52. outbuf[i * 3] = ' ';
  53. for (i = 0; i < ret; ++i)
  54. sprintf(outbuf + 48 + i, "%c",
  55. (isprint(buf[i]) ? buf[i] : '.'));
  56. if (ret > 0)
  57. haltest_info("%s\n", outbuf);
  58. } while (ret > 0);
  59. } else {
  60. /* For now disconnect on all other events */
  61. haltest_error("Poll event %x\n", pollfd->revents);
  62. poll_unregister_fd(pollfd->fd, receive_from_client);
  63. }
  64. }
  65. /*
  66. * This function read from fd socket information about
  67. * connected socket
  68. */
  69. static void receive_sock_connect_signal(struct pollfd *pollfd)
  70. {
  71. sock_connect_signal_t cs;
  72. char addr_str[MAX_ADDR_STR_LEN];
  73. if (pollfd->revents & POLLIN) {
  74. int ret;
  75. poll_unregister_fd(pollfd->fd, receive_sock_connect_signal);
  76. ret = read(pollfd->fd, &cs, sizeof(cs));
  77. if (ret != sizeof(cs)) {
  78. haltest_info("Read on connect return %d\n", ret);
  79. return;
  80. }
  81. haltest_info("Connection to %s channel %d status=%d\n",
  82. bt_bdaddr_t2str(&cs.bd_addr, addr_str),
  83. cs.channel, cs.status);
  84. if (cs.status == 0)
  85. poll_register_fd(pollfd->fd, POLLIN,
  86. receive_from_client);
  87. }
  88. if (pollfd->revents & POLLHUP) {
  89. haltest_error("Disconnected fd=%d revents=0x%X\n", pollfd->fd,
  90. pollfd->revents);
  91. poll_unregister_fd(pollfd->fd, receive_sock_connect_signal);
  92. }
  93. }
  94. /*
  95. * This function read from fd socket information about
  96. * incoming connection and starts monitoring new connection
  97. * on file descriptor read from fd.
  98. */
  99. static void read_accepted(int fd)
  100. {
  101. int ret;
  102. struct msghdr msg;
  103. struct iovec iv;
  104. char cmsgbuf[CMSG_SPACE(1)];
  105. struct cmsghdr *cmsgptr;
  106. sock_connect_signal_t cs;
  107. int accepted_fd = -1;
  108. char addr_str[MAX_ADDR_STR_LEN];
  109. memset(&msg, 0, sizeof(msg));
  110. memset(&iv, 0, sizeof(iv));
  111. memset(cmsgbuf, 0, sizeof(cmsgbuf));
  112. iv.iov_base = &cs;
  113. iv.iov_len = sizeof(cs);
  114. msg.msg_iov = &iv;
  115. msg.msg_iovlen = 1;
  116. msg.msg_control = cmsgbuf;
  117. msg.msg_controllen = sizeof(cmsgbuf);
  118. do {
  119. ret = recvmsg(fd, &msg, MSG_NOSIGNAL);
  120. } while (ret < 0 && errno == EINTR);
  121. if (ret < 16 ||
  122. (msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0)
  123. haltest_error("Failed to accept connection\n");
  124. for (cmsgptr = CMSG_FIRSTHDR(&msg);
  125. cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  126. int count;
  127. if (cmsgptr->cmsg_level != SOL_SOCKET ||
  128. cmsgptr->cmsg_type != SCM_RIGHTS)
  129. continue;
  130. memcpy(&accepted_fd, CMSG_DATA(cmsgptr), sizeof(accepted_fd));
  131. count = ((cmsgptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
  132. if (count != 1)
  133. haltest_error("Failed to accept descriptors count=%d\n",
  134. count);
  135. break;
  136. }
  137. haltest_info("Incoming connection from %s channel %d status=%d fd=%d\n",
  138. bt_bdaddr_t2str(&cs.bd_addr, addr_str),
  139. cs.channel, cs.status, accepted_fd);
  140. poll_register_fd(accepted_fd, POLLIN, receive_from_client);
  141. }
  142. /* handles incoming connections on socket */
  143. static void client_connected(struct pollfd *pollfd)
  144. {
  145. haltest_info("client connected %x\n", pollfd->revents);
  146. if (pollfd->revents & POLLHUP)
  147. poll_unregister_fd(pollfd->fd, client_connected);
  148. else if (pollfd->revents & POLLIN)
  149. read_accepted(pollfd->fd);
  150. }
  151. /* listen */
  152. static void listen_c(int argc, const char **argv, enum_func *enum_func,
  153. void **user)
  154. {
  155. if (argc == 3) {
  156. *user = TYPE_ENUM(btsock_type_t);
  157. *enum_func = enum_defines;
  158. } else if (argc == 5) {
  159. *user = (void *) uuids;
  160. *enum_func = enum_strings;
  161. }
  162. }
  163. static void listen_p(int argc, const char **argv)
  164. {
  165. btsock_type_t type;
  166. const char *service_name;
  167. bt_uuid_t service_uuid;
  168. int channel;
  169. int sock_fd = -1;
  170. int flags;
  171. RETURN_IF_NULL(if_sock);
  172. /* Socket type */
  173. if (argc < 3) {
  174. haltest_error("No socket type specified\n");
  175. return;
  176. }
  177. type = str2btsock_type_t(argv[2]);
  178. if ((int) type == -1)
  179. type = atoi(argv[2]);
  180. /* service name */
  181. if (argc < 4) {
  182. haltest_error("No service name specified\n");
  183. return;
  184. }
  185. service_name = argv[3];
  186. /* uuid */
  187. if (argc < 5) {
  188. haltest_error("No uuid specified\n");
  189. return;
  190. }
  191. str2bt_uuid_t(argv[4], &service_uuid);
  192. /* channel */
  193. channel = argc > 5 ? atoi(argv[5]) : 0;
  194. /* flags */
  195. flags = argc > 6 ? atoi(argv[6]) : 0;
  196. if (listen_fd_count >= MAX_LISTEN_FD) {
  197. haltest_error("Max (%d) listening sockets exceeded\n",
  198. listen_fd_count);
  199. return;
  200. }
  201. EXEC(if_sock->listen, type, service_name,
  202. &service_uuid.uu[0], channel, &sock_fd, flags);
  203. if (sock_fd > 0) {
  204. int channel = 0;
  205. int ret = read(sock_fd, &channel, 4);
  206. if (ret != 4)
  207. haltest_info("Read channel failed\n");
  208. haltest_info("Channel returned from first read %d\n", channel);
  209. listen_fd[listen_fd_count++] = sock_fd;
  210. poll_register_fd(sock_fd, POLLIN, client_connected);
  211. }
  212. }
  213. /* connect */
  214. static void connect_c(int argc, const char **argv, enum_func *enum_func,
  215. void **user)
  216. {
  217. if (argc == 3) {
  218. *enum_func = enum_devices;
  219. } else if (argc == 4) {
  220. *user = TYPE_ENUM(btsock_type_t);
  221. *enum_func = enum_defines;
  222. } else if (argc == 5) {
  223. *user = (void *) uuids;
  224. *enum_func = enum_strings;
  225. }
  226. }
  227. static void connect_p(int argc, const char **argv)
  228. {
  229. bt_bdaddr_t addr;
  230. btsock_type_t type;
  231. bt_uuid_t uuid;
  232. int channel;
  233. int sock_fd = -1;
  234. int flags;
  235. /* Address */
  236. if (argc <= 2) {
  237. haltest_error("No address specified\n");
  238. return;
  239. }
  240. str2bt_bdaddr_t(argv[2], &addr);
  241. /* Socket type */
  242. if (argc <= 3) {
  243. haltest_error("No socket type specified\n");
  244. return;
  245. }
  246. type = str2btsock_type_t(argv[3]);
  247. if ((int) type == -1)
  248. type = atoi(argv[3]);
  249. /* uuid */
  250. if (argc <= 4) {
  251. haltest_error("No uuid specified\n");
  252. return;
  253. }
  254. str2bt_uuid_t(argv[4], &uuid);
  255. /* channel */
  256. if (argc <= 5) {
  257. haltest_error("No channel specified\n");
  258. return;
  259. }
  260. channel = atoi(argv[5]);
  261. /* flags */
  262. flags = argc <= 6 ? 0 : atoi(argv[6]);
  263. RETURN_IF_NULL(if_sock);
  264. EXEC(if_sock->connect, &addr, type, &uuid.uu[0], channel, &sock_fd,
  265. flags);
  266. if (sock_fd > 0) {
  267. int channel = 0;
  268. int ret = read(sock_fd, &channel, 4);
  269. if (ret != 4)
  270. haltest_info("Read channel failed\n");
  271. haltest_info("Channel returned from first read %d\n", channel);
  272. listen_fd[listen_fd_count++] = sock_fd;
  273. poll_register_fd(sock_fd, POLLIN, receive_sock_connect_signal);
  274. }
  275. }
  276. /* Methods available in btsock_interface_t */
  277. static struct method methods[] = {
  278. STD_METHODCH(listen,
  279. "<sock_type> <srvc_name> <uuid> [<channel>] [<flags>]"),
  280. STD_METHODCH(connect,
  281. "<addr> <sock_type> <uuid> <channel> [<flags>]"),
  282. END_METHOD
  283. };
  284. const struct interface sock_if = {
  285. .name = "socket",
  286. .methods = methods
  287. };