aboutsummaryrefslogtreecommitdiff
path: root/smartpen.cc
blob: 79e9a0164a20696e58cb394f5dd8516bc931426e (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
/*
 * 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/QStringList>
#include <QtCore/QtEndian>
#include <usb.h>
#include "xmlutils.h"
#include "smartpen.h"

#define PEN_EPOCH (1289335960000LL) // This is probably not correct
#define PEN_MTU 900
#define PEN_TIMEOUT_SECONDS 10

#define INVALID_CID 0xFFFFFFFFU

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

/* Terrible hack comes now: */
struct obex_usb_intf_transport_t {
	struct obex_usb_intf_transport_t *prev, *next;	/* Next and previous interfaces in the list */
	struct usb_device *device;		/* USB device that has the interface */
};

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

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

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

QByteArray Smartpen::getObject(const QString &name)
{
	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();
	}

	qDebug() << "Getting object" << name;

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

	QDateTime start = QDateTime::currentDateTimeUtc();
	QDateTime now;
	do {
		OBEX_HandleInput(_obex, PEN_TIMEOUT_SECONDS);
		now = QDateTime::currentDateTimeUtc();
	} while (_inBuf.isEmpty() && start.secsTo(now) < PEN_TIMEOUT_SECONDS);

	if (_inBuf.isEmpty()) {
		qWarning() << "Did not receive any data in" << start.secsTo(now) << "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;
	}

	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() << QString::fromAscii(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::connectToPen(const Address &addr)
{
	if (_obex) {
		qWarning() << "Already connected";
		return false;
	}

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

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

	obex_interface_t *interfaces, *ls_interface = 0;
	int count = OBEX_FindInterfaces(_obex, &interfaces);
	for (int i = 0; i < count; i++) {
		if (interfaces[i].usb.intf->device->bus->location == addr.first &&
		        interfaces[i].usb.intf->device->devnum == addr.second) {
			ls_interface = &interfaces[i];
		}
	}

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

	usb_dev_handle *handle = usb_open(ls_interface->usb.intf->device);
	if (handle) {
		qDebug() << "resetting usb device";
		usb_reset(handle);
		usb_close(handle);
	} else {
		qWarning() << "could not open usb device for resetting";
	}

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

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

	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";

	OBEX_HandleInput(_obex, PEN_TIMEOUT_SECONDS);

	return _connId != INVALID_CID;
}

void Smartpen::disconnectFromPen()
{
	if (_connId != INVALID_CID) {
		if (_obex) {
			obex_object_t *object = OBEX_ObjectNew(_obex, OBEX_CMD_DISCONNECT);
			Q_ASSERT(object);
			addConnHeader(object);
			OBEX_Request(_obex, object);
			OBEX_HandleInput(_obex, PEN_TIMEOUT_SECONDS);
		}
		_connId = INVALID_CID;
	}
	if (_obex) {
		OBEX_Cleanup(_obex);
		_obex = 0;
	}
	_inBuf.clear();
}

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)
{

	switch (event) {
	case OBEX_EV_PROGRESS:
		if (obex_cmd == OBEX_CMD_GET) {
			// It seems that the pen wants us to add this header on every continue response
			addConnHeader(object);
		}
		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;
		emit error();
		break;
	default:
		qDebug() << "event" << event << obex_cmd << 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;

	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 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 = QByteArray(reinterpret_cast<const char*>(hdata.bs), hlen);
				}
			}
			break;
		default:
			qWarning() << "Failed GET request:" << OBEX_ResponseToString(obex_rsp);
			break;
		}

		break;
	}
}

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 = qFind(&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;
}

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), 0) < 0) {
		qCritical() << "Could not add connection header";
	}
}