summaryrefslogtreecommitdiff
path: root/liveview/liveview.cpp
blob: f4b7434a581398acc4e6c5516344094d26bd8563 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <QtEndian>

#include "liveview.h"

using namespace sowatch;
QTM_USE_NAMESPACE

#define PROTOCOL_DEBUG 1

LiveView::LiveView(ConfigKey* settings, QObject* parent) :
	BluetoothWatch(QBluetoothAddress(settings->value("address").toString()), parent),
	_settings(settings->getSubkey(QString(), this)),
    _sendTimer(new QTimer(this))
{
	_sendTimer->setInterval(DelayBetweenMessages);
	connect(_sendTimer, SIGNAL(timeout()), SLOT(handleSendTimerTick()));
}

LiveView::~LiveView()
{

}

QPaintEngine* LiveView::paintEngine() const
{
	return 0; // TODO
}

int LiveView::metric(PaintDeviceMetric metric) const
{
	return 0; // TODO
}

QString LiveView::model() const
{
	return "liveview";
}

QStringList LiveView::buttons() const
{
	return QStringList();
}

bool LiveView::busy() const
{
	return false; // TODO
}

void LiveView::setDateTime(const QDateTime& dateTime)
{
	// It seems LiveView _requests_ the current date rather than expecting
	// the phone to be sending it.
	// Wonder what will happen during DST changes?
	// Do nothing here.
}

void LiveView::queryDateTime()
{
	// LiveView does not support this.
}

QDateTime LiveView::dateTime() const
{
	return QDateTime::currentDateTime();
}

void LiveView::queryBatteryLevel()
{

}
int LiveView::batteryLevel() const
{
	return 0; // TODO
}

void LiveView::queryCharging()
{

}

bool LiveView::charging() const
{
	return false; // TODO
}

void LiveView::displayIdleScreen()
{

}

void LiveView::displayNotification(Notification *notification)
{

}

void LiveView::displayApplication()
{

}

void LiveView::vibrate(int msecs)
{

}

void LiveView::setupBluetoothWatch()
{
	connect(_socket, SIGNAL(readyRead()), SLOT(handleDataReceived()));
	updateDisplayProperties();
}

void LiveView::desetupBluetoothWatch()
{

}

void LiveView::send(const Message &msg)
{
	_sendingMsgs.enqueue(msg);
	if (!_sendTimer->isActive()) {
		_sendTimer->start();
	}
}

void LiveView::updateDisplayProperties()
{
	static const char *software_version = "0.0.3";

	send(Message(GetDisplayProperties,
	             QByteArray(software_version, strlen(software_version) + 1)));
}

void LiveView::updateSoftwareVersion()
{
	send(Message(GetSoftwareVersion, QByteArray(1, 0)));
}

void LiveView::enableLed()
{
	QByteArray data;
	data.append(char(0xFF));
	data.append(char(0xFF));
	data.append(char(0x00));
	data.append(char(0x64));
	data.append(char(0x00));
	data.append(char(0xFA));

	send(Message(EnableLed, data));
}

void LiveView::handleMessage(const Message &msg)
{
	send(Message(Ack, QByteArray(1, msg.type)));
	switch (msg.type) {
	case GetDisplayPropertiesResponse:
		handleDisplayProperties(msg);
		break;
	}
}

void LiveView::handleDisplayProperties(const Message &msg)
{
	updateSoftwareVersion();
}

void LiveView::handleDataReceived()
{
#pragma pack(push)
#pragma pack(1)
	static const int HEADER_SIZE = 6;
	union header_t {
		char c[HEADER_SIZE];
		struct header_fields_t {
			quint8 msg_type;
			quint8 header_len;
			quint32 data_len;
		} h;
	} header;
#pragma pack(pop)

	Q_ASSERT(sizeof(header) == HEADER_SIZE);

	do {
		qint64 dataRead;

		qDebug() << "received" << _socket->bytesAvailable() << "bytes";

		if (_receivingMsg.type == NoMessage) {
			/* Still not received even the packet type */
			/* Receive the full header. */
			if (_socket->bytesAvailable() < HEADER_SIZE) {
				/* Still not enough data available. */
				return; /* Wait for more, if non blocking. */
			}

			dataRead = _socket->read(header.c, HEADER_SIZE);
#if PROTOCOL_DEBUG
			qDebug() << "received header" << QByteArray(header.c, HEADER_SIZE).toHex();
#endif
			if (dataRead < HEADER_SIZE) {
				qWarning() << "Short read";
				return;
			}

			_receivingMsg.type = static_cast<MessageType>(header.h.msg_type);
			if (header.h.header_len != HEADER_SIZE - 2) {
				qWarning() << "Unexpected header length:" << header.h.header_len;
			}

			unsigned long data_size = qFromBigEndian(header.h.data_len);
			if (data_size > 1048576) {
				// If input packet is > 1 MiB, consider a protocol error.
				qWarning() << "Too large data size: " << data_size;
				data_size = 0;
			}
			_receivingMsg.data.resize(data_size);

			qDebug() << "got header (type=" << _receivingMsg.type <<
			            "size=" << data_size << ")";
		}

		/* We have the header; now, try to get the complete packet. */
		if (_socket->bytesAvailable() < _receivingMsg.data.size()) {
#if PROTOCOL_DEBUG
			qDebug() << "Waiting for more data" << _socket->bytesAvailable() << "/" << _receivingMsg.data.size();
#endif
			return; /* Wait for more. */
		}

		dataRead = _socket->read(_receivingMsg.data.data(), _receivingMsg.data.size());
		if (dataRead < _receivingMsg.data.size()) {
			qWarning() << "Short read";
			return;
		}

#if PROTOCOL_DEBUG
		qDebug() << "received" << _receivingMsg.type << _receivingMsg.data.toHex();
#endif
		handleMessage(_receivingMsg);

		// Prepare for the next packet
		_receivingMsg.data.clear();
		_receivingMsg.type = NoMessage;
	} while (_socket->bytesAvailable() > 0);
}

void LiveView::handleSendTimerTick()
{
	static const int HEADER_SIZE = 6;
	qDebug() << "Send tick";
	// If there are packets to be sent...
	if (!_sendingMsgs.empty()) {
		// Send a message to the watch
		Message msg = _sendingMsgs.dequeue();
		const quint32 data_size = msg.data.size();
		QByteArray packet;

		Q_ASSERT(_connected && _socket);

		packet.resize(HEADER_SIZE + data_size);
		packet[0] = msg.type;
		packet[1] = HEADER_SIZE - 2;
		packet[2] = (data_size & 0xFF000000U) >> 24;
		packet[3] = (data_size & 0x00FF0000U) >> 16;
		packet[4] = (data_size & 0x0000FF00U) >>  8;
		packet[5] = (data_size & 0x000000FFU);
		packet.replace(HEADER_SIZE, data_size, msg.data);

#if PROTOCOL_DEBUG
		qDebug() << "sending" << packet.toHex();
#endif

		_socket->write(packet);
	}
	// If we just finished sending all packets...
	if (_sendingMsgs.empty()) {
		// Stop the send timer to save battery
		_sendTimer->stop();
	}
}