From e274309d9293777aaaecebccaa29a339a05bd4f9 Mon Sep 17 00:00:00 2001 From: Emanoil Kotsev Date: Mon, 12 Nov 2018 21:18:37 +0100 Subject: Based on KDE3 bluez4 version a TDE bluez5 version was created Signed-off-by: Emanoil Kotsev --- src/libtdebluez/adapterImpl.cpp | 144 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/libtdebluez/adapterImpl.cpp (limited to 'src/libtdebluez/adapterImpl.cpp') 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 + * + * + * 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 +#include + +#include +#include +#include + +#include +#include + +#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 + 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 -- cgit v1.2.1