summaryrefslogtreecommitdiffstats
path: root/bluetooth.cpp
blob: 377139c5e180b7fa2077c776e33ebdbfb1ca5e9a (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2018 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "message.h"
#include "messageengine.h"
#include "bluetooth.h"
#include "bluetoothmessage.h"

Bluetooth::Bluetooth (QUrl &url, QObject * parent) :
    QObject(parent),
    m_mloop(nullptr)
{
    m_mloop = new MessageEngine(url);
    QObject::connect(m_mloop, &MessageEngine::connected, this, &Bluetooth::onConnected);
    QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Bluetooth::onDisconnected);
    QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Bluetooth::onMessageReceived);
}

Bluetooth::~Bluetooth()
{
    delete m_mloop;
}

void Bluetooth::generic_command(QString verb, QString value)
{
    BluetoothMessage *tmsg = new BluetoothMessage();
    QJsonObject parameter;

    if (!value.isEmpty())
        parameter.insert("value", value);

    tmsg->createRequest(verb, parameter);
    m_mloop->sendMessage(tmsg);
    tmsg->deleteLater();
}

void Bluetooth::setPower(bool state)
{
    generic_command("power", state ? "true": "false");

    m_power = state;

    emit powerChanged();
}

void Bluetooth::setDiscoverable(bool state)
{
    const QStringList properties { "Pairable", "Discoverable" };
    QStringListIterator propertyIterator(properties);

    while (propertyIterator.hasNext()) {
        BluetoothMessage *tmsg = new BluetoothMessage();
        QJsonObject parameter;

        parameter.insert("Property", propertyIterator.next());
        parameter.insert("value", state ? "true" : "false");

        tmsg->createRequest("set_property", parameter);
        m_mloop->sendMessage(tmsg);
        tmsg->deleteLater();
    }

    m_discoverable = state;

    emit discoverableChanged();
}

void Bluetooth::start_discovery()
{
    generic_command("start_discovery", "");
    generic_command("discovery_result", "");
}

void Bluetooth::stop_discovery()
{
    generic_command("stop_discovery", "");
}

void Bluetooth::remove_device(QString address)
{
    generic_command("remove_device", address);
}

void Bluetooth::pair(QString address)
{
    generic_command("pair", address);
}

void Bluetooth::cancel_pair(QString address)
{
    generic_command("cancel_pair", address);
}

void Bluetooth::connect(QString address, QString uuid)
{
    BluetoothMessage *tmsg = new BluetoothMessage();
    QJsonObject parameter;

    parameter.insert("value", address);
    parameter.insert("uuid", uuid);
    tmsg->createRequest("connect", parameter);
    m_mloop->sendMessage(tmsg);
    tmsg->deleteLater();
}

void Bluetooth::connect(QString address)
{
    generic_command("connect", address);
}

void Bluetooth::disconnect(QString address, QString uuid)
{
    BluetoothMessage *tmsg = new BluetoothMessage();
    QJsonObject parameter;

    parameter.insert("value", address);
    parameter.insert("uuid", uuid);
    tmsg->createRequest("disconnect", parameter);
    m_mloop->sendMessage(tmsg);
    tmsg->deleteLater();
}

void Bluetooth::disconnect(QString address)
{
    generic_command("disconnect", address);
}

void Bluetooth::send_confirmation()
{
    generic_command("send_confirmation", "yes");
}

void Bluetooth::onConnected()
{
    QStringListIterator eventIterator(events);
    BluetoothMessage *tmsg;

    while (eventIterator.hasNext()) {
        tmsg = new BluetoothMessage();
        QJsonObject parameter;
        parameter.insert("value", eventIterator.next());
        tmsg->createRequest("subscribe", parameter);
        m_mloop->sendMessage(tmsg);
        tmsg->deleteLater();
    }

    // get initial power state
    generic_command("power", QString());
}

void Bluetooth::onDisconnected()
{
    QStringListIterator eventIterator(events);
    BluetoothMessage *tmsg;

    while (eventIterator.hasNext()) {
        tmsg = new BluetoothMessage();
        QJsonObject parameter;
        parameter.insert("value", eventIterator.next());
        tmsg->createRequest("unsubscribe", parameter);
        m_mloop->sendMessage(tmsg);
        tmsg->deleteLater();
    }
}

void Bluetooth::onMessageReceived(MessageType type, Message *msg)
{
    if (msg->isEvent() && type == BluetoothEventMessage) {
        BluetoothMessage *tmsg = qobject_cast<BluetoothMessage*>(msg);

        if (tmsg->isConnectionEvent()) {
            emit connectionEvent(tmsg->eventData());
        } else if (tmsg->isRequestConfirmationEvent()) {
            emit requestConfirmationEvent(tmsg->eventData());
        } else if (tmsg->isDeviceAddedEvent()) {
            emit deviceAddedEvent(tmsg->eventData());
        } else if (tmsg->isDeviceRemovedEvent()) {
            emit deviceRemovedEvent(tmsg->eventData());
        } else if (tmsg->isDeviceUpdatedEvent()) {
            emit deviceUpdatedEvent(tmsg->eventData());
        }
    } else if (msg->isReply() && type == GenericMessage) {
        if (this->isDiscoveryListResponse(msg)) {
            emit deviceListEvent(msg->replyData());
        } else if (this->isPowerResponse(msg)) {
            m_power = msg->replyData().value("power").toString() == "on";
            emit powerChanged();
        }
    }

    msg->deleteLater();
}