forked from odeke-em/chdkptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptpip.c
375 lines (326 loc) · 9.11 KB
/
ptpip.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
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
/* this is just some proof of concept test code*/
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#define WINVER 0x0502
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "sockutil.h"
#define DEFAULT_BUFLEN 512
#define PTPIP_PORT "15740"
#define PTPIP_TYPE_INIT_CMD 0x1
#define PTPIP_TYPE_INIT_CMD_ACK 0x2
#define PTPIP_TYPE_INIT_EVENT 0x3
#define PTPIP_TYPE_INIT_EVENT_ACK 0x4
#define PTPIP_TYPE_INIT_FAIL 0x5
#define PTPIP_TYPE_REQ 0x6
#define PTPIP_TYPE_RESP 0x7
#include "ptp.h"
// guid for connection request, must be 16 bytes, values don't seem to matter
char my_guid[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xde,0xad,0xbe,0xef,0x12,0x34,0x56,0x78
};
// friendly name (i.e windows network name), in utf16 value doesn't seem to matter
char my_name[] = {
'w',0x00,'h',0x00,'e',0x00,'e',0x00,0x00,0x00
};
char cam_guid[16];
typedef struct {
socket_t sock;
socket_t event_sock;
uint32_t transaction_id;
uint32_t session_id;
uint32_t connection_id;
} PTPIPParams;
typedef struct __attribute__ ((__packed__)) {
uint32_t length;
uint32_t type;
uint32_t dataphase;
uint16_t code;
uint32_t trans_id;
struct {
uint32_t param1;
uint32_t param2;
uint32_t param3;
uint32_t param4;
uint32_t param5;
} params;
} PTPIPReq;
typedef struct __attribute__ ((__packed__)) {
uint32_t length;
uint32_t type;
uint16_t code;
uint32_t trans_id;
struct {
uint32_t param1;
uint32_t param2;
uint32_t param3;
uint32_t param4;
uint32_t param5;
} params;
} PTPIPResp;
typedef struct __attribute__ ((__packed__)) {
uint32_t length;
uint32_t type;
uint32_t connection_id;
} PTPIPEventReq;
int init_connection(PTPIPParams *params,char *host,char *port) {
socket_t sock = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
sockutil_startup();
memset( &hints, 0, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
int r = getaddrinfo(host, port, &hints, &result);
if ( r != 0 ) {
printf("getaddrinfo failed with error: %d\n", r);
sockutil_cleanup();
return 0;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a socket for the command channel
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (sock == INVALID_SOCKET) {
printf("socket failed with error: %d %s\n", sockutil_errno(),sockutil_strerror(sockutil_errno()));
return 0;
}
// Connect to camera
r = connect( sock, ptr->ai_addr, (int)ptr->ai_addrlen);
if (r == SOCKET_ERROR) {
sockutil_close(sock);
sock = INVALID_SOCKET;
continue;
}
break;
}
params->sock = sock;
printf("initializing command channel\n");
char sendbuf[38];
char *p = sendbuf;
*(int *)p = 38; // length
p+=4;
*(int *)p = PTPIP_TYPE_INIT_CMD; // init command packet
p+=4;
memcpy(p,my_guid,sizeof(my_guid));
p+=sizeof(my_guid);
memcpy(p,my_name,sizeof(my_name));
p+=sizeof(my_name);
*(int *)p = 0x00010000;
r = send( params->sock, sendbuf, sizeof(sendbuf), 0 );
if (r == SOCKET_ERROR) {
printf("send failed with error: %d %s\n", sockutil_errno(),sockutil_strerror(sockutil_errno()));
return 0;
}
printf("Bytes Sent: %d\n", r);
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
r = recv(params->sock, recvbuf, recvbuflen, 0);
int len;
if ( r > 0 ) {
printf("Bytes received: %d\n", r);
if( r >= 4) {
len = *(int *)recvbuf;
if(len != r) {
printf("size %d != %d\n",len,r);
return 0;
}
int rtype = *(int32_t *)(recvbuf + 4);
printf("packet type %d\n",rtype);
if(rtype != PTPIP_TYPE_INIT_CMD_ACK) {
printf("not ack, aborting\n");
return 0;
}
params->connection_id = *(uint32_t *)(recvbuf + 8);
printf("connection num %d\n",params->connection_id);
} else {
printf("size %d < sizeof(int)",r);
return 0;
}
} else if ( r == 0 ) {
printf("Connection closed\n");
return 0;
} else {
printf("recv failed with error: %d\n", sockutil_errno());
return 0;
}
printf("initializing event channel\n");
// event channel
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a socket for the command channel
sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (sock == INVALID_SOCKET) {
printf("socket failed with error: %d\n", sockutil_errno());
return 0;
}
// Connect to camera
r = connect( sock, ptr->ai_addr, (int)ptr->ai_addrlen);
if (r == SOCKET_ERROR) {
sockutil_close(sock);
sock = INVALID_SOCKET;
continue;
}
break;
}
params->event_sock = sock;
freeaddrinfo(result);
PTPIPEventReq ereq;
ereq.length=sizeof(ereq);
ereq.type=PTPIP_TYPE_INIT_EVENT;
ereq.connection_id = params->connection_id;
r = send( params->event_sock, (char *)&ereq, sizeof(ereq), 0 );
if (r == SOCKET_ERROR) {
printf("send failed with error: %d\n", sockutil_errno());
return 0;
}
printf("Bytes Sent: %d\n", r);
r = recv(params->event_sock, recvbuf, recvbuflen, 0);
if ( r > 0 ) {
printf("Bytes received: %d\n", r);
if( r >= 4) {
len = *(int *)recvbuf;
if(len != r) {
printf("size %d != %d\n",len,r);
return 0;
}
printf("packet type %d\n",*(int *)(recvbuf + 4));
return 1;
} else {
printf("size %d < sizeof(int)",r);
return 0;
}
} else if ( r == 0 ) {
printf("Connection closed\n");
return 0;
} else {
printf("recv failed with error: %d\n", sockutil_errno());
return 0;
}
return 1;
}
uint16_t ptpip_sendreq(PTPIPParams *params, PTPContainer *ptp)
{
PTPIPReq req;
memset(&req,0,sizeof(req));
req.length = sizeof(req) - 4*(5-ptp->Nparam);
req.type = PTPIP_TYPE_REQ;
req.dataphase = 0; // name from wireshark dissector, appears to be 0
// even if there is a send or receive data phase
// actual meaning unclear
req.trans_id=ptp->Transaction_ID;
req.code=ptp->Code;
req.params.param1=ptp->Param1;
req.params.param2=ptp->Param2;
req.params.param3=ptp->Param3;
req.params.param4=ptp->Param4;
req.params.param5=ptp->Param5;
int r = send( params->sock, (char *)&req, req.length, 0);
if (r == SOCKET_ERROR) {
printf("send_req send failed with error: %d\n", sockutil_errno());
return PTP_ERROR_IO;
}
return PTP_RC_OK;
}
uint16_t ptpip_getresp(PTPIPParams *params, PTPContainer *ptp)
{
PTPIPResp resp;
memset(&resp,0,sizeof(resp));
int r = recv(params->sock, (char *)&resp, sizeof(resp), 0);
if ( r > 0 ) {
printf("Bytes received: %d\n", r);
if( r >= 4) {
if(resp.length != r) {
printf("size %d != %d\n",resp.length,r);
return PTP_ERROR_IO;
}
printf("type %d\n",resp.type);
} else {
printf("size %d < sizeof(int)",r);
return PTP_ERROR_IO;
}
} else if ( r == 0 ) {
printf("Connection closed\n");
return PTP_ERROR_IO;
} else {
printf("recv failed with error: %d\n", sockutil_errno());
return PTP_ERROR_IO;
}
ptp->Code = resp.code;
ptp->Transaction_ID = resp.trans_id;
ptp->Param1 = resp.params.param1;
ptp->Param2 = resp.params.param2;
ptp->Param3 = resp.params.param3;
ptp->Param4 = resp.params.param4;
ptp->Param5 = resp.params.param5;
ptp->Nparam = 5 - (sizeof(resp) - (resp.length))/4;
return PTP_RC_OK;
}
uint16_t ptpip_transaction(PTPIPParams *params, PTPContainer *ptp, uint16_t flags, unsigned int sendlen, char**data)
{
if ((params==NULL) || (ptp==NULL))
return PTP_ERROR_BADPARAM;
ptp->Transaction_ID=params->transaction_id++;
ptp->SessionID=params->session_id;
uint16_t ret = ptpip_sendreq(params,ptp);
if(ret != PTP_RC_OK) {
return ret;
}
// NO data phase support!
ret = ptpip_getresp(params,ptp);
return ret;
}
int main(int argc, char **argv)
{
PTPIPParams params;
if (argc != 2) {
printf("usage: %s <camera address>\n", argv[0]);
return 1;
}
memset( ¶ms, 0, sizeof(params) );
if(!init_connection(¶ms,argv[1],PTPIP_PORT)) {
sockutil_close(params.sock);
sockutil_cleanup();
return 1;
}
PTPContainer ptp;
memset(&ptp,0,sizeof(ptp));
ptp.Code=PTP_OC_OpenSession;
ptp.Nparam=1;
ptp.Param1=1;
uint16_t r=ptpip_transaction(¶ms, &ptp, /*PTP_DP_NODATA*/0, 0, NULL);
if ( r != PTP_RC_OK ) {
printf("OpenSession failed 0x%x\n",r);
} else {
printf("OpenSession OK\n");
}
memset(&ptp,0,sizeof(ptp));
ptp.Code=PTP_OC_CHDK;
ptp.Nparam=1;
ptp.Param1=PTP_CHDK_Version;
r=ptpip_transaction(¶ms, &ptp, /*PTP_DP_NODATA*/0, 0, NULL);
if ( r != PTP_RC_OK ) {
printf("PTP_CHDK_Version failed 0x%x\n",r);
} else {
printf("chdk ver np=%d %d,%d\n",ptp.Nparam,ptp.Param1,ptp.Param2);
}
// cleanup
sockutil_close(params.sock);
sockutil_close(params.event_sock);
sockutil_cleanup();
return 0;
}