summaryrefslogtreecommitdiffstats
path: root/src/libtdebluez/adapterImpl.cpp
blob: 703f06fb3c4b410529c5b1fd5611e218044c7a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 *
 *  Adapter Implementation of bluez5 for libtdebluez
 *
 *  Copyright (C) 2018  Emanoil Kotsev <deloptes@gmail.com>
 *
 *
 *  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