tdebluez
adapterImpl.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Adapter Implementation of bluez5 for libtdebluez
4  *
5  * Copyright (C) 2018 Emanoil Kotsev <deloptes@gmail.com>
6  *
7  *
8  * This file is part of libtdebluez.
9  *
10  * libtdebluez is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * libtdebluez is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with kbluetooth; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 #include <linux/rfkill.h>
27 #include <unistd.h>
28 
29 #include <tqfile.h>
30 #include <tqregexp.h>
31 #include <tqdir.h>
32 
33 #include <tqdbusproxy.h>
34 #include <tqmessagebox.h>
35 #include <tdelocale.h>
36 
37 #include "adapterImpl.h"
38 
39 namespace TDEBluetooth
40 {
41 
42 AdapterImpl::AdapterImpl(const TQString& service, const TQString& path, TQObject* parent, const char* name) :
43  Adapter1Proxy(service, path, parent, name) /*,properties(service,path,parent,name)*/
44 {
45 }
46 
48 {
49 }
50 
51 void AdapterImpl::powerOn(bool state)
52 {
53  // https://www.kernel.org/doc/Documentation/rfkill.txt
54  // http://jwhsmith.net/2015/02/manipulating-rfkill-using-devices-programmatically/
55  // https://cpp.hotexamples.com/examples/-/-/rfkill_alloc/cpp-rfkill_alloc-function-examples.html
56  // https://github.com/systemd/systemd/blob/main/src/rfkill/rfkill.c
57 
58  TQString device = getPath();
59  device = device.replace(TQRegExp("^/.*/"), "");
60  int hcidx = -1;
61  TQDir d("/sys/class/rfkill");
62  d.setFilter(TQDir::Dirs);
63  for (int i = 0; i < d.count(); i++)
64  {
65  // expected is rfkill<n>
66  TQFile f("/sys/class/rfkill/" + d[i] + "/name");
67  TQString content;
68  if (f.exists() && f.open(IO_ReadOnly))
69  {
70  TQTextStream stream(&f);
71  content = stream.readLine();
72  f.close();
73  }
74  else
75  {
76  continue;
77  }
78  if (content.startsWith(device))
79  {
80  TQFile f("/sys/class/rfkill/" + d[i] + "/index");
81  if (f.exists() && f.open(IO_ReadOnly))
82  {
83  TQTextStream stream(&f);
84  hcidx = stream.readLine().toUInt();
85  f.close();
86  }
87  break;
88  }
89  }
90 
91  if (hcidx < 0)
92  {
93  // error handling
94  tqDebug(i18n("Index for the device %1 not found").arg(device));
95  return;
96  }
97 
98  struct rfkill_event event = { 0 };
99 
100  TQFile file("/dev/rfkill");
101  if (!file.open(IO_ReadWrite))
102  {
103  tqDebug(i18n("Failed to open %1").arg(file.name()));
104  return;
105  }
106 
107  event.idx = hcidx;
108  event.op = RFKILL_OP_CHANGE;
109  if (state)
110  event.soft = 0;
111  else
112  event.soft = 1;
113 
114  tqDebug(i18n("Bluetooth device %1 switches: idx(%2), soft(%3).").arg(device).arg(event.idx).arg(event.soft));
115 
116  if (write(file.handle(), &event, sizeof(event)) < 0)
117  tqDebug(i18n("Failed to write to %1").arg(file.name()));
118  file.close();
119 }
120 
122 {
123  return TQString(m_baseProxy->path());
124 }
125 
126 void AdapterImpl::slotSetAlias(const TQString& alias)
127 {
128  TQT_DBusError dbuserror;
129  setAlias(alias, dbuserror);
130  if (dbuserror.isValid())
131  tqDebug(i18n("AdapterImpl::slotSetAlias(%1) failed: %2").arg(alias).arg(dbuserror.message()));
132 }
133 
135 {
136  TQT_DBusError dbuserror;
137  setDiscoverableTimeout(timeout, dbuserror);
138  if (dbuserror.isValid())
139  tqDebug(i18n("AdapterImpl::slotSetTimeout(%1) failed: %2").arg(timeout).arg(dbuserror.message()));
140 }
141 
142 } // namespace TDEBluetooth
143 
144 #include "adapterImpl.moc"
145 // End of File
void powerOn(bool state)
Definition: adapterImpl.cpp:51
void slotSetTimeout(int timeout)
void slotSetAlias(const TQString &alias)
AdapterImpl(const TQString &service, const TQString &path, TQObject *parent=0, const char *name=0)
Definition: adapterImpl.cpp:42
TQT_DBusProxy * m_baseProxy
Definition: adapter1Proxy.h:83
virtual void setAlias(const TQString &value, TQT_DBusError &error)
virtual void setDiscoverableTimeout(TQ_UINT32 value, TQT_DBusError &error)