diff options
author | Emanoil Kotsev <[email protected]> | 2018-11-12 21:18:37 +0100 |
---|---|---|
committer | Emanoil Kotsev <[email protected]> | 2023-01-14 03:44:08 +0000 |
commit | e274309d9293777aaaecebccaa29a339a05bd4f9 (patch) | |
tree | a00349c31b90cdedaa6e351dfe93950b55903dce /src/libtdebluez/adapterImpl.cpp | |
parent | 63c233987977aa48b701edeb47079a6153359fbe (diff) | |
download | tdebluez-e274309d9293777aaaecebccaa29a339a05bd4f9.tar.gz tdebluez-e274309d9293777aaaecebccaa29a339a05bd4f9.zip |
Based on KDE3 bluez4 version a TDE bluez5 version was created
Signed-off-by: Emanoil Kotsev <[email protected]>
Diffstat (limited to 'src/libtdebluez/adapterImpl.cpp')
-rw-r--r-- | src/libtdebluez/adapterImpl.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/libtdebluez/adapterImpl.cpp b/src/libtdebluez/adapterImpl.cpp new file mode 100644 index 0000000..703f06f --- /dev/null +++ b/src/libtdebluez/adapterImpl.cpp @@ -0,0 +1,144 @@ +/* + * + * Adapter Implementation of bluez5 for libtdebluez + * + * Copyright (C) 2018 Emanoil Kotsev <[email protected]> + * + * + * This file is part of libtdebluez. + * + * libtdebluez is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * libtdebluez 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with kbluetooth; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <linux/rfkill.h> +#include <unistd.h> + +#include <tqfile.h> +#include <tqregexp.h> +#include <tqdir.h> + +#include <tqdbusproxy.h> +#include <tqmessagebox.h> + +#include "adapterImpl.h" + +namespace TDEBluetooth +{ + +AdapterImpl::AdapterImpl(const TQString& service, const TQString& path, TQObject* parent, const char* name) : + Adapter1Proxy(service, path, parent, name) /*,properties(service,path,parent,name)*/ +{ +} + +AdapterImpl::~AdapterImpl() +{ +} + +void AdapterImpl::powerOn(bool state) +{ + // https://www.kernel.org/doc/Documentation/rfkill.txt + // http://jwhsmith.net/2015/02/manipulating-rfkill-using-devices-programmatically/ + // https://cpp.hotexamples.com/examples/-/-/rfkill_alloc/cpp-rfkill_alloc-function-examples.html + // https://github.com/systemd/systemd/blob/main/src/rfkill/rfkill.c + + TQString device = getPath(); + device = device.replace(TQRegExp("^/.*/"), ""); + int hcidx = -1; + TQDir d("/sys/class/rfkill"); + d.setFilter(TQDir::Dirs); + for (int i = 0; i < d.count(); i++) + { + // expected is rfkill<n> + TQFile f("/sys/class/rfkill/" + d[i] + "/name"); + TQString content; + if (f.exists() && f.open(IO_ReadOnly)) + { + TQTextStream stream(&f); + content = stream.readLine(); + f.close(); + } + else + { + continue; + } + if (content.startsWith(device)) + { + TQFile f("/sys/class/rfkill/" + d[i] + "/index"); + if (f.exists() && f.open(IO_ReadOnly)) + { + TQTextStream stream(&f); + hcidx = stream.readLine().toUInt(); + f.close(); + } + break; + } + } + + if (hcidx < 0) + { + // error handling + tqDebug("Index for the device %s not found", device.local8Bit().data()); + return; + } + + struct rfkill_event event = { 0 }; + + TQFile file("/dev/rfkill"); + if (!file.open(IO_ReadWrite)) + { + tqDebug("Failed to open %s", file.name().utf8().data()); + return; + } + + event.idx = hcidx; + event.op = RFKILL_OP_CHANGE; + if (state) + event.soft = 0; + else + event.soft = 1; + + tqDebug("Bluetooth device %s switches: idx(%i), soft(%d).", device.local8Bit().data(), event.idx, event.soft); + + if (write(file.handle(), &event, sizeof(event)) < 0) + tqDebug("Failed to write to %s", file.name().utf8().data()); + file.close(); +} + +TQString AdapterImpl::getPath() +{ + return TQString(m_baseProxy->path()); +} + +void AdapterImpl::slotSetAlias(const TQString& alias) +{ + TQT_DBusError error; + setAlias(alias, error); + if (error.isValid()) + tqDebug("AdapterImpl::slotSetAlias(%s) failed: %s", alias.utf8().data(), error.message().utf8().data()); +} + +void AdapterImpl::slotSetTimeout(int timeout) +{ + TQT_DBusError error; + setDiscoverableTimeout(timeout, error); + if (error.isValid()) + tqDebug("AdapterImpl::slotSetTimeout(%i) failed: %s", timeout, error.message().utf8().data()); +} + +} // namespace TDEBluetooth + +#include "adapterImpl.moc" +// End of File |