forked from jordens/libsmartpen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobex.c
246 lines (201 loc) · 6.49 KB
/
obex.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
#include <openobex/obex.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <glib.h>
static int req_done=0;
volatile int loop = 0;
typedef struct {
obex_t *handle;
int connid;
int err;
char *body;
int body_len;
} obex_state;
static void obex_requestdone (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;
printf("Request done\n");
switch (cmd & ~OBEX_FINAL) {
case OBEX_CMD_CONNECT:
printf("cmd connect\n");
while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
&hdata, &hlen)) {
/* Get the connection identifier */
printf("header %x\n", header_id);
if (header_id == OBEX_HDR_WHO) {
printf("who: %s\n", hdata.bs);
}
}
break;
case OBEX_CMD_DISCONNECT:
printf("cmd disconnect\n");
break;
case OBEX_CMD_PUT:
printf("cmd put\n");
while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
&hdata, &hlen))
printf("header %d\n", header_id);
break;
case OBEX_CMD_GET:
printf("cmd get\n");
while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
&hdata, &hlen)) {
printf("header %x\n", header_id);
if (header_id == OBEX_HDR_BODY || header_id == OBEX_HDR_BODY_END) {
printf("Body!\n\n%s\n\n", hdata.bs);
if (state->body)
free(state->body);
state->body = malloc(hlen);
state->body_len = hlen;
memcpy(state->body, hdata.bs, hlen);
break;
}
}
break;
default:
printf("Funny command\n");
}
req_done++;
}
static void obex_event (obex_t *hdl, obex_object_t *obj, int mode,
int event, int obex_cmd, int obex_rsp) {
obex_state *state;
obex_headerdata_t hd;
int size;
int rc;
state = OBEX_GetUserData(hdl);
if (event == OBEX_EV_PROGRESS) {
hd.bq4 = 0;
size = 4;
rc = OBEX_ObjectAddHeader(hdl, obj, OBEX_HDR_CONNECTION, hd, size, 0);
if (rc < 0) {
printf("oah fail %d\n", rc);
}
return;
}
printf("event!\n");
if (obex_rsp != OBEX_RSP_SUCCESS && obex_rsp != OBEX_RSP_CONTINUE) {
printf("FAIL %x %x\n", obex_rsp, event);
req_done++;
return;
}
switch (event) {
case OBEX_EV_REQDONE:
obex_requestdone(state, hdl, obj, obex_cmd, obex_rsp);
break;
default:
printf("Funny event\n");
}
}
int syncml_obex_send_req () {
obex_t *handle = NULL;
obex_object_t *obj;
obex_state state;
obex_headerdata_t hd;
obex_interface_t *obex_intf;
int size;
int i, num;
char *name;
int fd;
if (!handle) {
handle = OBEX_Init(OBEX_TRANS_USB, obex_event, 0);
state.handle = handle;
state.body = NULL;
if (!handle)
perror("handle");
OBEX_SetUserData(handle, &state);
num = OBEX_EnumerateInterfaces(handle);
for (i=0; i<num; i++) {
obex_intf = OBEX_GetInterfaceByIndex(handle, i);
if (obex_intf->usb.idVendor == 0x1cfb &&
(obex_intf->usb.idProduct == 0x1020 || obex_intf->usb.idProduct == 0x1030))
{
printf("Found Livescribe Pulse pen\n");
break;
}
}
if (i == num) {
return 0;
}
i = OBEX_InterfaceConnect(handle, obex_intf);
printf("connect = %d\n", i);
OBEX_SetTransportMTU(handle, 0x400, 0x400);
obj = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
hd.bs = "LivescribeService";
size = strlen(hd.bs)+1;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_TARGET, hd, size, 0);
if (OBEX_Request(handle, obj) < 0)
printf("request");
while (req_done < 1) {
OBEX_HandleInput(handle, 1);
}
}
printf("CONNECTED\n");
obj = OBEX_ObjectNew(handle, OBEX_CMD_GET);
hd.bq4 = 0;
size = 4;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION, hd, size, OBEX_FL_FIT_ONE_PACKET);
name="changelist?start_time=0";
hd.bs = g_utf8_to_utf16(name, strlen(name), NULL, &num, NULL);
for (i=0; i<num; i++) {
uint16_t *wchar = &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)
printf("foo2\n");
while (req_done < 2) {
OBEX_HandleInput(handle, 1);
}
printf("LISTING DONE\n");
obj = OBEX_ObjectNew(handle, OBEX_CMD_GET);
hd.bq4 = 0;
size = 4;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION, hd, size, OBEX_FL_FIT_ONE_PACKET);
name="lspdata?name=0xbc61f02c87e32f9b&start_time=8782700699";
hd.bs = g_utf8_to_utf16(name, strlen(name), NULL, &num, NULL);
for (i=0; i<num; i++) {
uint16_t *wchar = &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)
printf("foo2\n");
while (req_done < 3) {
OBEX_HandleInput(handle, 100);
}
printf("DATA DONE\n");
fd = open("data", O_WRONLY|O_CREAT);
if (fd < 0) {
perror("open");
}
write(fd, state.body, state.body_len);
close(fd);
obj = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
hd.bq4 = 0;
size = 4;
OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION, hd, size, OBEX_FL_FIT_ONE_PACKET);
if (OBEX_Request(handle, obj) < 0)
printf("foo2\n");
while (OBEX_HandleInput(handle, 100) == 0);
return 1;
}
int main (int argc, char **argv) {
char *data = "This is some exciting data!";
int ret;
ret = syncml_obex_send_req(data, strlen(data), NULL, "x-obex/folder-listing");
if (!ret)
printf("ugh\n");
/*while (!loop) {
loop = 0;
}*/
return 0;
}