From d6c1d96051c5efda7996f95892704ce63e0d4bee Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 14 Dec 2014 01:04:59 +0100 Subject: [PATCH] emulator: Add skeleton for virtual PHY handling --- Makefile.tools | 1 + emulator/le.c | 4 +++ emulator/phy.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ emulator/phy.h | 30 ++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 emulator/phy.c create mode 100644 emulator/phy.h diff --git a/Makefile.tools b/Makefile.tools index ef4162b34..bc827fedf 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -49,6 +49,7 @@ emulator_btvirt_SOURCES = emulator/main.c monitor/bt.h \ emulator/btdev.h emulator/btdev.c \ emulator/bthost.h emulator/bthost.c \ emulator/smp.c \ + emulator/phy.h emulator/phy.c \ emulator/amp.h emulator/amp.c \ emulator/le.h emulator/le.c emulator_btvirt_LDADD = lib/libbluetooth-internal.la src/libshared-mainloop.la diff --git a/emulator/le.c b/emulator/le.c index a3f2e1b31..a39cdde5f 100644 --- a/emulator/le.c +++ b/emulator/le.c @@ -42,6 +42,7 @@ #include "monitor/mainloop.h" #include "monitor/bt.h" +#include "phy.h" #include "le.h" #define WHITE_LIST_SIZE 16 @@ -57,6 +58,7 @@ struct bt_le { volatile int ref_count; int vhci_fd; + struct bt_phy *phy; struct bt_crypto *crypto; uint8_t event_mask[16]; @@ -1181,6 +1183,7 @@ struct bt_le *bt_le_new(void) mainloop_add_fd(hci->vhci_fd, EPOLLIN, vhci_read_callback, hci, NULL); + hci->phy = bt_phy_new(); hci->crypto = bt_crypto_new(); return bt_le_ref(hci); @@ -1205,6 +1208,7 @@ void bt_le_unref(struct bt_le *hci) return; bt_crypto_unref(hci->crypto); + bt_phy_unref(hci->phy); mainloop_remove_fd(hci->vhci_fd); diff --git a/emulator/phy.c b/emulator/phy.c new file mode 100644 index 000000000..30808df6d --- /dev/null +++ b/emulator/phy.c @@ -0,0 +1,67 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2011-2012 Intel Corporation + * Copyright (C) 2004-2010 Marcel Holtmann + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "phy.h" + +struct bt_phy { + volatile int ref_count; +}; + +struct bt_phy *bt_phy_new(void) +{ + struct bt_phy *phy; + + phy = calloc(1, sizeof(*phy)); + if (!phy) + return NULL; + + return bt_phy_ref(phy); +} + +struct bt_phy *bt_phy_ref(struct bt_phy *phy) +{ + if (!phy) + return NULL; + + __sync_fetch_and_add(&phy->ref_count, 1); + + return phy; +} + +void bt_phy_unref(struct bt_phy *phy) +{ + if (!phy) + return; + + if (__sync_sub_and_fetch(&phy->ref_count, 1)) + return; + + free(phy); +} diff --git a/emulator/phy.h b/emulator/phy.h new file mode 100644 index 000000000..a070e0353 --- /dev/null +++ b/emulator/phy.h @@ -0,0 +1,30 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2011-2012 Intel Corporation + * Copyright (C) 2004-2010 Marcel Holtmann + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +struct bt_phy; + +struct bt_phy *bt_phy_new(void); + +struct bt_phy *bt_phy_ref(struct bt_phy *phy); +void bt_phy_unref(struct bt_phy *phy); -- 2.47.3