summaryrefslogtreecommitdiff
path: root/sapbtlistener.cc
blob: 40c7ac6ff6ed9cbe1a69777ea21899839fdb6dc9 (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
#include <QtCore/QDebug>

#include "saprotocol.h"
#include "sapbtlistener.h"
#include "sapbtpeer.h"

#ifdef DESKTOP
#include "hfpag.h"
#endif

SAPBTListener::SAPBTListener(QObject *parent) :
    QObject(parent), _server(0)
{
}

SAPBTListener::~SAPBTListener()
{
	stop();
}

void SAPBTListener::start()
{
	if (_server) {
		qWarning() << "Already started";
		return;
	}

	_server = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
	connect(_server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
	if (!_server->listen(QBluetoothAddress(), 0)) {
		qWarning() << "Failed to start Bluetooth listener socket";
		stop();
		return;
	}

	quint8 serverPort = _server->serverPort();

	/*
	 	Service Name: SAP
		Service RecHandle: 0x1000b
		Service Class ID List:
		  UUID 128: a49eb41e-cb06-495c-9f4f-bb80a90cdf00
		Protocol Descriptor List:
		  "L2CAP" (0x0100)
		  "RFCOMM" (0x0003)
			Channel: 5

		Service Name: SAP
		Service RecHandle: 0x1000c
		Service Class ID List:
		  UUID 128: a49eb41e-cb06-495c-9f4f-aa80a90cdf4a
		Protocol Descriptor List:
		  "L2CAP" (0x0100)
		  "RFCOMM" (0x0003)
			Channel: 6
 */

	_service.setServiceName("SAP");
	_service.setServiceDescription("Samsung Accessory Profile");
	_service.setServiceProvider("gearbtteest");

	QBluetoothServiceInfo::Sequence classIds;
	classIds.append(QVariant::fromValue(SAProtocol::dataServiceUuid));
	_service.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classIds);

	QBluetoothServiceInfo::Sequence browseGroupList;
	browseGroupList.append(QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)));
	_service.setAttribute(QBluetoothServiceInfo::BrowseGroupList, browseGroupList);

	QBluetoothServiceInfo::Sequence protocolDescriptorList;
	QBluetoothServiceInfo::Sequence protocol;

	protocol.append(QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)));
	protocolDescriptorList.append(QVariant::fromValue(protocol));
	protocol.clear();

	protocol.append(QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)));
	protocol.append(QVariant::fromValue(serverPort));
	protocolDescriptorList.append(QVariant::fromValue(protocol));
	protocol.clear();

	_service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
	                       protocolDescriptorList);

	if (!_service.registerService()) {
		qWarning() << "Failed to register the SAP service";
	}
}

void SAPBTListener::stop()
{
	if (!_server) {
		return;
	}

	if (!_service.unregisterService()) {
		qWarning() << "Failed to unregister SAP service";
	}

	delete _server;
	_server = 0;
}

void SAPBTListener::nudge(const QBluetoothAddress &address)
{
	QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);

	connect(socket, SIGNAL(connected()), this, SLOT(handleNudgeConnected()));
	connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)),
	        this, SLOT(handleNudgeError(QBluetoothSocket::SocketError)));

	qDebug() << "Nudging" << address.toString();

#if SAILFISH
	// For some reason, using UUIDs here fails on SailfishOS
	socket->connectToService(address, 1);
#else
	socket->connectToService(address, SAProtocol::nudgeServiceUuid); 
#endif

#if DESKTOP
	// At the same time set up and HFP connection to the watch.
	new HfpAg(address, this);
#endif
}

void SAPBTListener::acceptConnection()
{
	qDebug() << "Incoming BT connection";
	QBluetoothSocket *socket = _server->nextPendingConnection();
	if (!socket) {
		qWarning() << "Actually, no incoming connection";
		return;
	}

	qDebug() << "Got connection";

	// TODO Why am I hardcoding the role here
	new SAPBTPeer(SAProtocol::ClientRole, socket, this);
}

void SAPBTListener::handleNudgeConnected()
{
	QBluetoothSocket *socket = static_cast<QBluetoothSocket*>(sender());
	qDebug() << "Nudge connected:" << socket->peerAddress().toString();
	new SAPBTPeer(SAProtocol::ClientRole, socket, this);
}

void SAPBTListener::handleNudgeError(QBluetoothSocket::SocketError error)
{
	QBluetoothSocket *socket = static_cast<QBluetoothSocket*>(sender());
	qWarning() << "Cannot nudge:" << error << socket->errorString();
	socket->abort();
	socket->deleteLater();
}