aboutsummaryrefslogtreecommitdiff
path: root/smartpen.cc
blob: 1282e33726ddf63c37a256ec92f867a70aff7213 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
 * scribiu -- read notebooks and voice memos from Livescribe pens
 * Copyright (C) 2015 Javier S. Pedro <javier@javispedro.com>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDeadlineTimer>
#include <QtCore/QThread>
#include <QtCore/QStringList>
#include <QtCore/QtEndian>
#include <libusb-1.0/libusb.h>
#include "xmlutils.h"
#include "smartpen.h"

#define PEN_EPOCH (1289335960000LL)
#define PEN_MTU OBEX_MAXIMUM_MTU
#define PEN_TIMEOUT_SECONDS 5

#define INVALID_CID 0xFFFFFFFFU

static const char pen_serial_chars[] = "ABCDEFGHJKMNPQRSTUWXYZ23456789";
static const unsigned int pen_serial_num_chars = sizeof(pen_serial_chars) - 1;

Smartpen::Smartpen(QObject *parent) :
    QObject(parent), _obex(0), _connId(INVALID_CID), _reqComplete(false), _continueReceived(0)
{
}

Smartpen::~Smartpen()
{
	if (_connId != INVALID_CID || _obex) {
		disconnectFromPen();
	}
}

bool Smartpen::isConnected() const
{
	return _obex && _connId != INVALID_CID;
}

QByteArray Smartpen::getObject(const QString &name)
{
	qDebug() << "Getting object" << name;

	prepareRequest();

	obex_object_t *obj = OBEX_ObjectNew(_obex, OBEX_CMD_GET);
	Q_ASSERT(obj);

	addConnHeader(obj);

	obex_headerdata_t hd;
	QByteArray encodedName = encodeUtf16(name);
	hd.bs = reinterpret_cast<const uint8_t*>(encodedName.constData());
	if (OBEX_ObjectAddHeader(_obex, obj, OBEX_HDR_NAME, hd, encodedName.size(), 0) < 0) {
		qCritical("Could not add name header");
		OBEX_ObjectDelete(_obex, obj);
		return QByteArray();
	}

	if (OBEX_Request(_obex, obj) < 0) {
		qWarning() << "Get object request failed";
		OBEX_ObjectDelete(_obex, obj);
		return QByteArray();
	}

	waitForRequestComplete(PEN_TIMEOUT_SECONDS);

	QByteArray result;
	qSwap(_inBuf, result);

	return result;
}

QString Smartpen::getParameter(Parameters parameter)
{
	QString objectName = QString("ppdata?key=pp%1").arg(int(parameter), 4);
	QByteArray data = getObject(objectName);
	QXmlStreamReader r(data);

	advanceToFirstChildElement(r, "xml");
	advanceToFirstChildElement(r, "parameter");

	if (!r.atEnd()) {
		QXmlStreamAttributes attrs = r.attributes();
		return attrs.value("value").toString();
	}

	return QString();
}

QString Smartpen::getPenName()
{
	QString name = getParameter(PenName);
	if (name.isEmpty()) {
		return name; // Empty string if unknown name
	}

	QByteArray hex = QByteArray::fromHex(name.mid(2).toLatin1());
	return QString::fromUtf8(hex);
}

QVariantMap Smartpen::getPenInfo()
{
	QVariantMap result;
	QByteArray data = getObject("peninfo");
	QXmlStreamReader r(data);

	advanceToFirstChildElement(r, "xml");
	advanceToFirstChildElement(r, "peninfo");

	if (!r.atEnd()) {
		Q_ASSERT(r.isStartElement() && r.name() == "peninfo");
		QString penId = r.attributes().value("penid").toString();
		result["penid"] = penId;
		result["penserial"] = toPenSerial(penId.mid(2).toULongLong(0, 16));

		while (r.readNextStartElement()) {
			if (r.name() == "battery") {
				result["battery"] = r.attributes().value("level").toString();
				r.skipCurrentElement();
			} else if (r.name() == "time") {
				result["time"] = r.attributes().value("absolute").toString();
				r.skipCurrentElement();
			} else {
				r.skipCurrentElement();
			}
		}
	} else {
		qWarning() << "Could not parse peninfo XML";
	}

	return result;
}

QList<Smartpen::ChangeReport> Smartpen::getChangeList(PenTime from)
{
	QList<ChangeReport> result;
	QByteArray data = getObject(QString("changelist?start_time=%1").arg(from));
	QXmlStreamReader r(data);

	qDebug() << "changelist:" << QString::fromLatin1(data);

	advanceToFirstChildElement(r, "xml");
	advanceToFirstChildElement(r, "changelist");

	if (!r.atEnd()) {
		Q_ASSERT(r.isStartElement() && r.name() == "changelist");

		while (r.readNextStartElement()) {
			if (r.name() == "lsp") {
				QXmlStreamAttributes attrs = r.attributes();
				ChangeReport report;
				if (attrs.hasAttribute("guid")) {
					report.guid = attrs.value("guid").toString();
					report.title = attrs.value("title").toString();

					// Figure out the change record endtime to enumerating the
					// endtime of all the record's changed pages
					report.endTime = 0;
					while (r.readNextStartElement()) {
						if (r.name() == "page") {
							QXmlStreamAttributes attrs = r.attributes();
							PenTime endTime = attrs.value("end_time").toString().toLongLong();
							if (endTime > report.endTime) report.endTime = endTime;
						}
						r.skipCurrentElement();
					}

					result.append(report);
				} else if (attrs.hasAttribute("classname")) {
					report.className = attrs.value("classname").toString();
					report.title = attrs.value("title").toString();
					report.endTime = attrs.value("end_time").toString().toLongLong();
					result.append(report);
					r.skipCurrentElement();
				} else {
					qWarning() << "Unknown change report format";
					r.skipCurrentElement();
				}
			} else {
				r.skipCurrentElement();
			}
		}

	} else {
		qWarning() << "Could not parse changelist XML";
	}

	return result;
}

QByteArray Smartpen::getLspData(const QString &name, PenTime from)
{
	return getObject(QString("lspdata?name=%1&start_time=%2").arg(name).arg(from));
}

QByteArray Smartpen::getPaperReplay(PenTime from)
{
	return getObject(QString("lspdata?name=com.livescribe.paperreplay.PaperReplay&start_time=%1&returnVersion=0.3&remoteCaller=WIN_LD_200").arg(from));
}

qint64 Smartpen::toPenTime(const QDateTime &dt)
{
	if (dt.isValid()) {
		return dt.toMSecsSinceEpoch() - PEN_EPOCH;
	} else {
		return 0;
	}
}

QDateTime Smartpen::fromPenTime(qint64 t)
{
	if (t) {
		return QDateTime::fromMSecsSinceEpoch(t + PEN_EPOCH).toLocalTime();
	} else {
		return QDateTime();
	}
}

QString Smartpen::toPenSerial(quint64 id)
{
	QString serial;
	serial.reserve(3 + 1 + 3 + 1 + 3 + 1 + 2 + 1);

	serial.append(toPenSerialSegment(id >> 32, 3));
	serial.append('-');
	serial.append(toPenSerialSegment(id, 6).mid(0, 3));
	serial.append('-');
	serial.append(toPenSerialSegment(id, 6).mid(3, 3));
	serial.append('-');
	serial.append(toPenSerialSegment(id % 0x36D, 2));

	return serial;
}

quint64 Smartpen::toPenId(const QString &serial)
{
	QStringList segments = serial.split('-');
	if (segments.size() != 4) {
		return 0;
	}

	quint64 id = quint64(fromPenSerialSegment(segments[0])) << 32;
	id |= fromPenSerialSegment(segments[1] + segments[2]) * 0x36D;
	id |= fromPenSerialSegment(segments[3]);
	return id;
}

bool Smartpen::reset(const Address &addr)
{
	libusb_context *ctx = 0;
	libusb_device **devlist = 0;
	libusb_device *dev = 0;
	libusb_device_handle *handle = 0;
	ssize_t ndevs;
	int err = 0;

	err = libusb_init(&ctx);
	if (err != 0) {
		qWarning() << "libusb_init failed:" << err;
		goto err0;
	}

	ndevs = libusb_get_device_list(ctx, &devlist);
	if (ndevs < 0) {
		qWarning() << "libusb_get_device_list failed:" << err;
		goto err1;
	}

	for (ssize_t i = 0; i < ndevs; ++i) {
		if (libusb_get_bus_number(devlist[i]) == addr.first &&
		        libusb_get_device_address(devlist[i]) == addr.second) {
			dev = devlist[i];
		}
	}

	if (!dev) {
		qWarning() << "could not find device in libusb";
		err = -ENODEV;
		goto err2;
	}

	err = libusb_open(dev, &handle);
	if (err != 0) {
		qWarning() << "libusb_open failed:" << err;
		goto err2;
	}

	err = libusb_reset_device(handle);
	if (err != 0) {
		qWarning() << "libusb_reset_device failed: " << err;
		goto err3;
	}

	qDebug() << "USB device resetted";

err3:
	libusb_close(handle);
err2:
	libusb_free_device_list(devlist, 1);
err1:
	libusb_exit(ctx);
err0:
	return err == 0;
}

bool Smartpen::connectToPen(const Address &addr)
{
	if (_obex) {
		qWarning() << "Already connected";
		return false;
	}

	_connId = INVALID_CID;

	_obex = OBEX_Init(OBEX_TRANS_USB, obexEventCb, OBEX_FL_CLOEXEC);
	Q_ASSERT(_obex);

	OBEX_SetUserData(_obex, this);
	OBEX_SetTransportMTU(_obex, PEN_MTU, PEN_MTU);

	obex_interface_t *ls_interface = 0;
	int count = OBEX_EnumerateInterfaces(_obex);
	for (int i = 0; i < count; i++) {
		obex_interface_t *intf = OBEX_GetInterfaceByIndex(_obex, i);
		if (intf->usb.bus_number == addr.first &&
		        intf->usb.device_address == addr.second) {
			ls_interface = intf;
		}
	}

	if (!ls_interface) {
		qWarning() << "Could not find Lightscribe interface on device:" << addr;
		return false;
	}

	qDebug() << "Connecting to" << ls_interface->usb.product;

	int err = OBEX_InterfaceConnect(_obex, ls_interface) ;
	if (err < 0) {
		qWarning() << "Could not connect to Livescribe interface" << strerror(-err);
		return false;
	}

	prepareRequest();

	static const char * livescribe_service = "LivescribeService";
	obex_object_t *object = OBEX_ObjectNew(_obex, OBEX_CMD_CONNECT);
	obex_headerdata_t hd;
	int hd_len;

	Q_ASSERT(object);

	hd.bs = reinterpret_cast<const quint8*>(livescribe_service);
	hd_len = strlen(livescribe_service) + 1;
	if (OBEX_ObjectAddHeader(_obex, object, OBEX_HDR_TARGET, hd, hd_len, 0) < 0) {
		qWarning() << "Failed to add Target header";
		OBEX_ObjectDelete(_obex, object);
		return false;
	}

	if (OBEX_Request(_obex, object) < 0) {
		qWarning() << "Failed to make connection request";
		OBEX_ObjectDelete(_obex, object);
		return false;
	}

	qDebug() << "Connection in progress";

	waitForRequestComplete(PEN_TIMEOUT_SECONDS);

	return _connId != INVALID_CID;
}

void Smartpen::disconnectFromPen()
{
	if (_connId != INVALID_CID) {
		if (_obex) {
			OBEX_CancelRequest(_obex, 0);

			prepareRequest();

			obex_object_t *object = OBEX_ObjectNew(_obex, OBEX_CMD_DISCONNECT);
			Q_ASSERT(object);
			addConnHeader(object);
			if (OBEX_Request(_obex, object) == 0) {
				qDebug() << "Send disconnect";
				waitForRequestComplete(PEN_TIMEOUT_SECONDS);
			}
		}
		_connId = INVALID_CID;
	}
	if (_obex) {
		OBEX_TransportDisconnect(_obex);
		OBEX_Cleanup(_obex);
		_obex = 0;
	}
	_inBuf.clear();
	qDebug() << "Disconnected";
}

void Smartpen::obexEventCb(obex_t *handle, obex_object_t *obj,
                           int mode, int event, int obex_cmd, int obex_rsp)
{
	Smartpen *smartpen = static_cast<Smartpen*>(OBEX_GetUserData(handle));
	Q_UNUSED(mode);
	smartpen->handleObexEvent(obj, event, obex_cmd, obex_rsp);
}

void Smartpen::handleObexEvent(obex_object_t *object,
                               int event, int obex_cmd, int obex_rsp)
{
	// Special flag used for temporarily ignoring the synthetic events generated during OBEX_CancelRequest
	static bool aborting_continue = false;
	if (aborting_continue) return;

	switch (event) {
	case OBEX_EV_PROGRESS:
		break;
	case OBEX_EV_REQDONE:
		qDebug() << "event reqdone cmd=" << obex_cmd << " rsp=" << OBEX_ResponseToString(obex_rsp);
		handleObexRequestDone(object, obex_cmd, obex_rsp);
		break;
	case OBEX_EV_LINKERR:
		qWarning() << "link error cmd=" << obex_cmd << " rsp=" << OBEX_ResponseToString(obex_rsp);
		emit linkError("Link error");
		break;
	case OBEX_EV_PARSEERR:
		qWarning() << "parse error cmd=" << obex_cmd << " rsp=" << OBEX_ResponseToString(obex_rsp);
		emit linkError("Protocol error");
		break;
	case OBEX_EV_CONTINUE:
		// The standard "Continue" messages sent by OpenObex "forget" to include the ConnectionID, confusing the pen
		// We're going to therefore implement the "continue" state machine on our own
		handleObexContinue(object, obex_cmd);

		// Cancel OpenObex's Continue message, avoiding the spurious events that generates
		Q_ASSERT(!aborting_continue);
		aborting_continue = true;
		OBEX_CancelRequest(_obex, 0);
		aborting_continue = false;
		break;
	default:
		qDebug() << "event unknown=" << event << " cmd=" << obex_cmd << " rsp=" << OBEX_ResponseToString(obex_rsp);
		break;
	}
}

void Smartpen::handleObexRequestDone(obex_object_t *object, int obex_cmd, int obex_rsp)
{
	quint8 header_id;
	obex_headerdata_t hdata;
	quint32 hlen;

	_reqComplete = true;

	switch (obex_cmd & ~OBEX_FINAL) {
	case OBEX_CMD_CONNECT:
		switch (obex_rsp) {
		case OBEX_RSP_SUCCESS:
			while (OBEX_ObjectGetNextHeader(_obex, object, &header_id, &hdata, &hlen)) {
				if (header_id == OBEX_HDR_CONNECTION) {
					Q_ASSERT(_connId == INVALID_CID);
					_connId = hdata.bq4;
					qDebug() << "Connection established, id:" << _connId;
				}
			}
			break;
		default:
			qWarning() << "Failed connection request:" << OBEX_ResponseToString(obex_rsp);
			emit linkError("OBEX connection error");
			break;
		}
		break;
	case OBEX_CMD_DISCONNECT:
		switch (obex_rsp) {
		case OBEX_RSP_SUCCESS:
			qDebug() << "Disconnected succesfully";
			_connId = INVALID_CID;
			break;
		default:
			qWarning() << "Failed disconnection request:" << OBEX_ResponseToString(obex_rsp);
			_connId = INVALID_CID;
			break;
		}

		break;
	case OBEX_CMD_GET:
		switch (obex_rsp) {
		case OBEX_RSP_SUCCESS:
			qDebug() << "GET request succesful";
			while (OBEX_ObjectGetNextHeader(_obex, object, &header_id, &hdata, &hlen)) {
				if (header_id == OBEX_HDR_BODY || header_id == OBEX_HDR_BODY_END) {
					_inBuf.append(reinterpret_cast<const char*>(hdata.bs), hlen);
				}
			}
			break;
		default:
			qWarning() << "Failed GET request:" << OBEX_ResponseToString(obex_rsp);
			break;
		}

		break;
	}
}

void Smartpen::handleObexContinue(obex_object_t *object, int obex_cmd)
{
	const uint8_t *data;
	int len;

	_continueReceived++;

	switch (obex_cmd & ~OBEX_FINAL) {
	case OBEX_CMD_GET:
		len = OBEX_ObjectReadStream(_obex, object, &data);
		if (len > 0) {
			_inBuf.append(reinterpret_cast<const char*>(data), len);
		}
		break;
	}
}

void Smartpen::prepareRequest()
{
	_reqComplete = false;
	_continueReceived = 0;
	_inBuf.clear();
}

bool Smartpen::waitForRequestComplete(int timeout)
{
	QDeadlineTimer timer(timeout * 1000UL);
	timer.setTimerType(Qt::CoarseTimer);

	int cmd = OBEX_ObjectGetCommand(_obex, NULL);

	do {
		if (OBEX_HandleInput(_obex, timer.remainingTime() / 1000) < 0) {
			qWarning() << "OBEX_HandleInput failed";
			break;
		}
		if (_continueReceived) {
			sendContinue(cmd);
			_continueReceived--;

			// Reset timeout
			timer.setRemainingTime(timeout * 1000UL);
		}
	} while (!_reqComplete && !timer.hasExpired());

	if (!_reqComplete) {
		qWarning() << "Did not complete request in" << timeout << "seconds";
		emit linkError("Timeout");
		return false;
	}

	return true;
}

void Smartpen::addConnHeader(obex_object_t *obj) const
{
	obex_headerdata_t hd;
	hd.bq4 = _connId;
	if (OBEX_ObjectAddHeader(_obex, obj, OBEX_HDR_CONNECTION, hd, sizeof(hd.bq4), OBEX_FL_FIT_ONE_PACKET) < 0) {
		qCritical() << "Could not add connection header";
	}
}

bool Smartpen::sendContinue(int obex_cmd)
{
	obex_object_t *obj = OBEX_ObjectNew(_obex, obex_cmd);
	Q_ASSERT(obj);

	addConnHeader(obj);

	if (OBEX_Request(_obex, obj) < 0) {
		qWarning() << "Send continue failed";
		OBEX_ObjectDelete(_obex, obj);
		return false;
	}

	return true;
}

QString Smartpen::toPenSerialSegment(quint32 id, int len)
{

	QString segment(len, Qt::Uninitialized);

	for (int i = 0; i < len; i++) {
		segment[len - (i + 1)] = pen_serial_chars[id % pen_serial_num_chars];
		id /= pen_serial_num_chars;
	}

	return segment;
}

quint32 Smartpen::fromPenSerialSegment(const QString &s)
{
	const int len = s.length();
	quint32 id = 0;

	for (int i = 0; i < len; i++) {
		uint val = std::find(&pen_serial_chars[0], &pen_serial_chars[pen_serial_num_chars], s[i]) - &pen_serial_chars[0];
		if (val >= pen_serial_num_chars) return 0;
		id = val + id * pen_serial_num_chars;
	}

	return id;
}

QByteArray Smartpen::encodeUtf16(const QString &s)
{
	const int size = s.size();
	QByteArray data((size + 1) * sizeof(quint16), Qt::Uninitialized);
	quint16 *p = reinterpret_cast<quint16*>(data.data());
	for (int i = 0; i < size; i++) {
		p[i] = qToBigEndian(s.at(i).unicode());
	}
	p[size] = 0;
	return data;
}