-
Notifications
You must be signed in to change notification settings - Fork 7
/
smartpen.c
346 lines (287 loc) · 7.21 KB
/
smartpen.c
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
/* vim: set sw=8 noet: */
#include <openobex/obex.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <libusb.h>
#include <assert.h>
#include <arpa/inet.h>
struct obex_state {
obex_t *handle;
int req_done;
char *body;
int body_len;
int got_connid;
int connid;
};
static void obex_requestdone (struct obex_state *state, obex_t *hdl,
obex_object_t *obj, int cmd, int resp)
{
uint8_t header_id;
obex_headerdata_t hdata;
uint32_t hlen;
switch (cmd & ~OBEX_FINAL) {
case OBEX_CMD_CONNECT:
while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
&hdata, &hlen)) {
if (header_id == OBEX_HDR_CONNECTION) {
state->got_connid=1;
state->connid = hdata.bq4;
printf("Connection ID: %d\n",
state->connid);
}
}
break;
case OBEX_CMD_GET:
while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
&hdata, &hlen)) {
if (header_id == OBEX_HDR_BODY ||
header_id == OBEX_HDR_BODY_END) {
if (state->body)
free(state->body);
state->body = malloc(hlen+1);
state->body_len = hlen;
memcpy(state->body, hdata.bs, hlen);
break;
}
}
break;
}
state->req_done++;
}
static void obex_event (obex_t *hdl, obex_object_t *obj, int mode,
int event, int obex_cmd, int obex_rsp)
{
struct obex_state *state;
obex_headerdata_t hd;
int size;
int rc;
state = OBEX_GetUserData(hdl);
if (event == OBEX_EV_PROGRESS) {
hd.bq4 = state->connid;
size = 4;
rc = OBEX_ObjectAddHeader(hdl, obj, OBEX_HDR_CONNECTION,
hd, size, OBEX_FL_FIT_ONE_PACKET);
if (rc < 0) {
printf("oah fail %d\n", rc);
}
return;
}
if (obex_rsp != OBEX_RSP_SUCCESS && obex_rsp != OBEX_RSP_CONTINUE) {
printf("FAIL %x %x\n", obex_rsp, event);
assert(0);
state->req_done++;
return;
}
switch (event) {
case OBEX_EV_REQDONE:
obex_requestdone(state, hdl, obj, obex_cmd, obex_rsp);
break;
default:
printf("Funny event\n");
}
}
static void pen_reset (short vendor, short product)
{
libusb_context *ctx;
libusb_device_handle *dev;
int rc;
printf("swizzle\n");
rc = libusb_init(&ctx);
assert(rc == 0);
dev = libusb_open_device_with_vid_pid(ctx, vendor, product);
libusb_reset_device(dev);
libusb_close(dev);
libusb_exit(ctx);
}
static int swizzle_usb (short vendor, short product)
{
libusb_context *ctx;
libusb_device_handle *dev;
int rc;
printf("swizzle\n");
rc = libusb_init(&ctx);
assert(rc == 0);
dev = libusb_open_device_with_vid_pid(ctx, vendor, product);
if (!dev)
goto out;
libusb_set_configuration(dev, 1);
libusb_set_interface_alt_setting(dev, 1, 0);
libusb_set_interface_alt_setting(dev, 1, 1);
rc = 1;
out:
libusb_close(dev);
libusb_exit(ctx);
return rc;
}
static char *get_named_object(obex_t *handle, char *name, int *len);
obex_t *smartpen_connect(short vendor, short product)
{
obex_t *handle;
obex_object_t *obj;
int rc, num, i;
struct obex_state *state;
obex_interface_t *obex_intf;
obex_headerdata_t hd;
int size, count;
char *buf;
again:
handle = OBEX_Init(OBEX_TRANS_USB, obex_event, 0);
if (!handle)
goto out;
num = OBEX_EnumerateInterfaces(handle);
for (i=0; i<num; i++) {
obex_intf = OBEX_GetInterfaceByIndex(handle, i);
if (!strcmp(obex_intf->usb.manufacturer, "Livescribe"))
break;
}
if (i == num) {
printf("No such device\n");
handle = NULL;
goto out;
}
state = malloc(sizeof(struct obex_state));
if (!state) {
handle = NULL;
goto out;
}
memset(state, 0, sizeof(struct obex_state));
if (!swizzle_usb(vendor, product)) {
handle = NULL;
goto out;
}
rc = OBEX_InterfaceConnect(handle, obex_intf);
if (rc < 0) {
printf("Connect failed %d\n", rc);
handle = NULL;
goto out;
}
OBEX_SetUserData(handle, state);
OBEX_SetTransportMTU(handle, 0x400, 0x400);
obj = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
hd.bs = (unsigned char *)"LivescribeService";
size = strlen((char*)hd.bs)+1;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_TARGET, hd, size, 0);
rc = OBEX_Request(handle, obj);
count = state->req_done;
while (rc == 0 && state->req_done <= count) {
OBEX_HandleInput(handle, 100);
}
if (rc < 0 || !state->got_connid) {
printf("Retry connection...\n");
OBEX_Cleanup(handle);
goto again;
}
buf = get_named_object(handle, "ppdata?key=pp0000", &rc);
if (!buf) {
printf("Retry connection...\n");
OBEX_Cleanup(handle);
pen_reset(vendor, product);
goto again;
}
out:
return handle;
}
static char *get_named_object(obex_t *handle, char *name, int *len)
{
struct obex_state *state;
int req_done;
obex_object_t *obj;
obex_headerdata_t hd;
int size, i;
glong num;
state = OBEX_GetUserData(handle);
obj = OBEX_ObjectNew(handle, OBEX_CMD_GET);
hd.bq4 = state->connid;
size = 4;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION,
hd, size, OBEX_FL_FIT_ONE_PACKET);
hd.bs = (unsigned char *)g_utf8_to_utf16(name, strlen(name),
NULL, &num, NULL);
for (i=0; i<num; i++) {
uint16_t *wchar = (uint16_t*)&hd.bs[i*2];
*wchar = ntohs(*wchar);
}
size = (num+1) * sizeof(uint16_t);
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_NAME, hd, size, OBEX_FL_FIT_ONE_PACKET);
if (OBEX_Request(handle, obj) < 0)
return NULL;
req_done = state->req_done;
while (state->req_done == req_done) {
OBEX_HandleInput(handle, 100);
}
if (state->body) {
*len = state->body_len;
state->body[state->body_len] = '\0';
} else {
*len = 0;
}
return state->body;
}
char *smartpen_get_changelist(obex_t *handle, int starttime)
{
char name[256];
int len;
snprintf(name, sizeof(name), "changelist?start_time=%d", starttime);
return get_named_object(handle, name, &len);
}
void smartpen_disconnect (obex_t *handle)
{
struct obex_state *state;
int req_done;
obex_object_t *obj;
obex_headerdata_t hd;
int size;
state = OBEX_GetUserData(handle);
obj = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
hd.bq4 = state->connid;
size = 4;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION,
hd, size, OBEX_FL_FIT_ONE_PACKET);
if (OBEX_Request(handle, obj) < 0)
return;
req_done = state->req_done;
while (state->req_done == req_done) {
OBEX_HandleInput(handle, 100);
}
}
int smartpen_get_guid (obex_t *handle, FILE *out, char *guid,
long long int start_time)
{
char name[256];
char *buf;
int len;
snprintf(name, sizeof(name), "lspdata?name=%s&start_time=%lld",
guid, start_time);
buf = get_named_object(handle, name, &len);
fwrite(buf, len, 1, out);
return len;
}
int smartpen_get_paperreplay (obex_t *handle, FILE *out,
long long int start_time)
{
char name[256];
char *buf;
int len;
snprintf(name, sizeof(name), "lspdata?name=com.livescribe.paperreplay.PaperReplay&start_time=%lld&returnVersion=0.3&remoteCaller=WIN_LD_200",
start_time);
buf = get_named_object(handle, name, &len);
fwrite(buf, len, 1, out);
return 1;
}
int smartpen_get_penletlist (obex_t *handle, FILE *out)
{
char *name = "penletlist";
char *buf;
int len;
buf = get_named_object(handle, name, &len);
fwrite(buf, len, 1, out);
return 1;
}
char * smartpen_get_peninfo (obex_t *handle)
{
char *name = "peninfo";
int len;
return get_named_object(handle, name, &len);
}