1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
22 #include <sys/socket.h>
23 #include <sys/ioctl.h>
27 #include "lib/bluetooth.h"
30 #include "monitor/bt.h"
31 #include "emulator/vhci.h"
32 #include "emulator/btdev.h"
33 #include "emulator/bthost.h"
34 #include "src/shared/util.h"
35 #include "src/shared/queue.h"
36 #include "emulator/hciemu.h"
38 struct hciemu_client {
49 enum btdev_type btdev_type;
51 struct queue *clients;
52 struct queue *post_command_hooks;
55 hciemu_debug_func_t debug_callback;
56 hciemu_destroy_func_t debug_destroy;
59 unsigned int flush_id;
62 struct hciemu_command_hook {
63 hciemu_command_func_t function;
67 static void destroy_command_hook(void *data)
69 struct hciemu_command_hook *hook = data;
80 static void run_command_hook(void *data, void *user_data)
82 struct hciemu_command_hook *hook = data;
83 struct run_data *run_data = user_data;
86 hook->function(run_data->opcode, run_data->data,
87 run_data->len, hook->user_data);
90 static void central_command_callback(uint16_t opcode,
91 const void *data, uint8_t len,
92 btdev_callback callback, void *user_data)
94 struct hciemu *hciemu = user_data;
95 struct run_data run_data = { .opcode = opcode,
96 .data = data, .len = len };
98 btdev_command_default(callback);
100 queue_foreach(hciemu->post_command_hooks, run_command_hook, &run_data);
103 static void client_command_callback(uint16_t opcode,
104 const void *data, uint8_t len,
105 btdev_callback callback, void *user_data)
107 btdev_command_default(callback);
110 static void writev_callback(const struct iovec *iov, int iovlen,
113 GIOChannel *channel = user_data;
117 fd = g_io_channel_unix_get_fd(channel);
119 written = writev(fd, iov, iovlen);
124 static gboolean receive_bthost(GIOChannel *channel, GIOCondition condition,
127 struct bthost *bthost = user_data;
128 unsigned char buf[4096];
132 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
135 fd = g_io_channel_unix_get_fd(channel);
137 len = read(fd, buf, sizeof(buf));
141 bthost_receive_h4(bthost, buf, len);
146 static guint create_source_bthost(int fd, struct bthost *bthost)
151 channel = g_io_channel_unix_new(fd);
153 g_io_channel_set_close_on_unref(channel, TRUE);
154 g_io_channel_set_encoding(channel, NULL, NULL);
155 g_io_channel_set_buffered(channel, FALSE);
157 bthost_set_send_handler(bthost, writev_callback, channel);
159 source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
160 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
161 receive_bthost, bthost, NULL);
163 g_io_channel_unref(channel);
168 static gboolean receive_btdev(GIOChannel *channel, GIOCondition condition,
171 struct btdev *btdev = user_data;
172 unsigned char buf[4096];
176 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
179 fd = g_io_channel_unix_get_fd(channel);
181 len = read(fd, buf, sizeof(buf));
183 if (errno == EAGAIN || errno == EINTR)
192 btdev_receive_h4(btdev, buf, len);
197 static guint create_source_btdev(int fd, struct btdev *btdev)
202 channel = g_io_channel_unix_new(fd);
204 g_io_channel_set_close_on_unref(channel, TRUE);
205 g_io_channel_set_encoding(channel, NULL, NULL);
206 g_io_channel_set_buffered(channel, FALSE);
208 btdev_set_send_handler(btdev, writev_callback, channel);
210 source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
211 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
212 receive_btdev, btdev, NULL);
214 g_io_channel_unref(channel);
219 static bool create_vhci(struct hciemu *hciemu)
223 vhci = vhci_open(hciemu->btdev_type);
227 btdev_set_command_handler(vhci_get_btdev(vhci),
228 central_command_callback, hciemu);
234 struct vhci *hciemu_get_vhci(struct hciemu *hciemu)
242 struct hciemu_client *hciemu_get_client(struct hciemu *hciemu, int num)
244 const struct queue_entry *entry;
249 for (entry = queue_get_entries(hciemu->clients); entry;
250 entry = entry->next, num--) {
258 struct bthost *hciemu_client_host(struct hciemu_client *client)
266 struct bthost *hciemu_client_get_host(struct hciemu *hciemu)
268 struct hciemu_client *client;
273 client = hciemu_get_client(hciemu, 0);
275 return hciemu_client_host(client);
278 static gboolean start_host(gpointer user_data)
280 struct hciemu_client *client = user_data;
282 client->start_source = 0;
284 bthost_start(client->host);
289 static void hciemu_client_destroy(void *data)
291 struct hciemu_client *client = data;
293 if (client->start_source)
294 g_source_remove(client->start_source);
296 g_source_remove(client->host_source);
297 g_source_remove(client->source);
299 bthost_destroy(client->host);
300 btdev_destroy(client->dev);
305 static struct hciemu_client *hciemu_client_new(struct hciemu *hciemu,
308 struct hciemu_client *client;
311 client = new0(struct hciemu_client, 1);
315 client->dev = btdev_create(hciemu->btdev_type, id++);
321 client->host = bthost_create();
323 btdev_destroy(client->dev);
328 btdev_set_command_handler(client->dev, client_command_callback, client);
330 if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC,
332 bthost_destroy(client->host);
333 btdev_destroy(client->dev);
337 client->sock[0] = sv[0];
338 client->sock[1] = sv[1];
340 client->source = create_source_btdev(sv[0], client->dev);
341 client->host_source = create_source_bthost(sv[1], client->host);
342 client->start_source = g_idle_add(start_host, client);
347 struct hciemu *hciemu_new_num(enum hciemu_type type, uint8_t num)
350 struct hciemu *hciemu;
356 hciemu = new0(struct hciemu, 1);
361 case HCIEMU_TYPE_BREDRLE:
362 hciemu->btdev_type = BTDEV_TYPE_BREDRLE;
364 case HCIEMU_TYPE_BREDR:
365 hciemu->btdev_type = BTDEV_TYPE_BREDR;
368 hciemu->btdev_type = BTDEV_TYPE_LE;
370 case HCIEMU_TYPE_LEGACY:
371 hciemu->btdev_type = BTDEV_TYPE_BREDR20;
373 case HCIEMU_TYPE_BREDRLE50:
374 hciemu->btdev_type = BTDEV_TYPE_BREDRLE50;
376 case HCIEMU_TYPE_BREDRLE52:
377 hciemu->btdev_type = BTDEV_TYPE_BREDRLE52;
383 hciemu->post_command_hooks = queue_new();
384 if (!hciemu->post_command_hooks) {
389 if (!create_vhci(hciemu)) {
390 queue_destroy(hciemu->post_command_hooks, NULL);
395 hciemu->clients = queue_new();
397 for (i = 0; i < num; i++) {
398 struct hciemu_client *client = hciemu_client_new(hciemu, i);
401 queue_destroy(hciemu->clients, hciemu_client_destroy);
405 queue_push_tail(hciemu->clients, client);
408 return hciemu_ref(hciemu);
411 struct hciemu *hciemu_new(enum hciemu_type type)
413 return hciemu_new_num(type, 1);
416 struct hciemu *hciemu_ref(struct hciemu *hciemu)
421 __sync_fetch_and_add(&hciemu->ref_count, 1);
426 void hciemu_unref(struct hciemu *hciemu)
431 if (__sync_sub_and_fetch(&hciemu->ref_count, 1))
434 queue_destroy(hciemu->post_command_hooks, destroy_command_hook);
435 queue_destroy(hciemu->clients, hciemu_client_destroy);
437 if (hciemu->flush_id)
438 g_source_remove(hciemu->flush_id);
440 vhci_close(hciemu->vhci);
445 static void bthost_print(const char *str, void *user_data)
447 struct hciemu *hciemu = user_data;
449 util_debug(hciemu->debug_callback, hciemu->debug_data,
453 static void vhci_debug(const char *str, void *user_data)
455 struct hciemu *hciemu = user_data;
457 util_debug(hciemu->debug_callback, hciemu->debug_data,
461 static void btdev_client_debug(const char *str, void *user_data)
463 struct hciemu *hciemu = user_data;
465 util_debug(hciemu->debug_callback, hciemu->debug_data,
469 static void hciemu_client_set_debug(void *data, void *user_data)
471 struct hciemu_client *client = data;
472 struct hciemu *hciemu = user_data;
474 btdev_set_debug(client->dev, btdev_client_debug, hciemu, NULL);
475 bthost_set_debug(client->host, bthost_print, hciemu, NULL);
478 bool hciemu_set_debug(struct hciemu *hciemu, hciemu_debug_func_t callback,
479 void *user_data, hciemu_destroy_func_t destroy)
484 if (hciemu->debug_destroy)
485 hciemu->debug_destroy(hciemu->debug_data);
487 hciemu->debug_callback = callback;
488 hciemu->debug_destroy = destroy;
489 hciemu->debug_data = user_data;
491 vhci_set_debug(hciemu->vhci, vhci_debug, hciemu, NULL);
493 queue_foreach(hciemu->clients, hciemu_client_set_debug, hciemu);
498 const char *hciemu_get_address(struct hciemu *hciemu)
503 if (!hciemu || !hciemu->vhci)
506 dev = vhci_get_btdev(hciemu->vhci);
510 addr = btdev_get_bdaddr(dev);
511 sprintf(hciemu->bdaddr_str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
512 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
513 return hciemu->bdaddr_str;
516 uint8_t *hciemu_get_features(struct hciemu *hciemu)
520 if (!hciemu || !hciemu->vhci)
523 dev = vhci_get_btdev(hciemu->vhci);
527 return btdev_get_features(dev);
530 const uint8_t *hciemu_get_central_bdaddr(struct hciemu *hciemu)
534 if (!hciemu || !hciemu->vhci)
537 dev = vhci_get_btdev(hciemu->vhci);
541 return btdev_get_bdaddr(dev);
544 const uint8_t *hciemu_client_bdaddr(struct hciemu_client *client)
549 return btdev_get_bdaddr(client->dev);
552 const uint8_t *hciemu_get_client_bdaddr(struct hciemu *hciemu)
554 struct hciemu_client *client;
559 client = hciemu_get_client(hciemu, 0);
561 return hciemu_client_bdaddr(client);
564 uint8_t hciemu_get_central_scan_enable(struct hciemu *hciemu)
568 if (!hciemu || !hciemu->vhci)
571 dev = vhci_get_btdev(hciemu->vhci);
575 return btdev_get_scan_enable(dev);
578 uint8_t hciemu_get_central_le_scan_enable(struct hciemu *hciemu)
582 if (!hciemu || !hciemu->vhci)
585 dev = vhci_get_btdev(hciemu->vhci);
589 return btdev_get_le_scan_enable(dev);
592 void hciemu_set_central_le_states(struct hciemu *hciemu,
593 const uint8_t *le_states)
597 if (!hciemu || !hciemu->vhci)
600 dev = vhci_get_btdev(hciemu->vhci);
604 btdev_set_le_states(dev, le_states);
607 void hciemu_set_central_le_al_len(struct hciemu *hciemu, uint8_t len)
611 if (!hciemu || !hciemu->vhci)
614 dev = vhci_get_btdev(hciemu->vhci);
618 btdev_set_al_len(dev, len);
621 void hciemu_set_central_le_rl_len(struct hciemu *hciemu, uint8_t len)
625 if (!hciemu || !hciemu->vhci)
628 dev = vhci_get_btdev(hciemu->vhci);
632 btdev_set_rl_len(dev, len);
635 const uint8_t *hciemu_get_central_adv_addr(struct hciemu *hciemu,
640 if (!hciemu || !hciemu->vhci)
643 dev = vhci_get_btdev(hciemu->vhci);
647 return btdev_get_adv_addr(dev, handle);
650 bool hciemu_add_central_post_command_hook(struct hciemu *hciemu,
651 hciemu_command_func_t function, void *user_data)
653 struct hciemu_command_hook *hook;
658 hook = new0(struct hciemu_command_hook, 1);
662 hook->function = function;
663 hook->user_data = user_data;
665 if (!queue_push_tail(hciemu->post_command_hooks, hook)) {
673 bool hciemu_clear_central_post_command_hooks(struct hciemu *hciemu)
678 queue_remove_all(hciemu->post_command_hooks,
679 NULL, NULL, destroy_command_hook);
683 int hciemu_add_hook(struct hciemu *hciemu, enum hciemu_hook_type type,
684 uint16_t opcode, hciemu_hook_func_t function,
687 enum btdev_hook_type hook_type;
690 if (!hciemu || !hciemu->vhci)
693 dev = vhci_get_btdev(hciemu->vhci);
698 case HCIEMU_HOOK_PRE_CMD:
699 hook_type = BTDEV_HOOK_PRE_CMD;
701 case HCIEMU_HOOK_POST_CMD:
702 hook_type = BTDEV_HOOK_POST_CMD;
704 case HCIEMU_HOOK_PRE_EVT:
705 hook_type = BTDEV_HOOK_PRE_EVT;
707 case HCIEMU_HOOK_POST_EVT:
708 hook_type = BTDEV_HOOK_POST_EVT;
714 return btdev_add_hook(dev, hook_type, opcode, function, user_data);
717 bool hciemu_del_hook(struct hciemu *hciemu, enum hciemu_hook_type type,
720 enum btdev_hook_type hook_type;
723 if (!hciemu || !hciemu->vhci)
726 dev = vhci_get_btdev(hciemu->vhci);
731 case HCIEMU_HOOK_PRE_CMD:
732 hook_type = BTDEV_HOOK_PRE_CMD;
734 case HCIEMU_HOOK_POST_CMD:
735 hook_type = BTDEV_HOOK_POST_CMD;
737 case HCIEMU_HOOK_PRE_EVT:
738 hook_type = BTDEV_HOOK_PRE_EVT;
740 case HCIEMU_HOOK_POST_EVT:
741 hook_type = BTDEV_HOOK_POST_EVT;
747 return btdev_del_hook(dev, hook_type, opcode);
750 static bool client_is_pending(const void *data, const void *match_data)
752 struct hciemu_client *client = (struct hciemu_client *)data;
755 if (!client->source || !client->host_source)
758 for (i = 0; i < 2; ++i) {
759 if (!ioctl(client->sock[i], TIOCOUTQ, &used) && used > 0)
761 if (!ioctl(client->sock[i], TIOCINQ, &used) && used > 0)
768 static gboolean flush_client_events(gpointer user_data)
770 struct hciemu *hciemu = user_data;
772 if (queue_find(hciemu->clients, client_is_pending, NULL))
775 hciemu->flush_id = 0;
777 util_debug(hciemu->debug_callback, hciemu->debug_data, "vhci: resume");
779 vhci_pause_input(hciemu->vhci, false);
784 void hciemu_flush_client_events(struct hciemu *hciemu)
786 if (hciemu->flush_id || !hciemu->vhci)
789 util_debug(hciemu->debug_callback, hciemu->debug_data, "vhci: pause");
790 vhci_pause_input(hciemu->vhci, true);
791 hciemu->flush_id = g_idle_add(flush_client_events, hciemu);