forked from odeke-em/chdkptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptp.c
2398 lines (2145 loc) · 62.8 KB
/
ptp.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
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ptp.c
*
* This file is based on libptp2 by Mariusz Woloszyn
* Copyright (C) 2001-2005 Mariusz Woloszyn <emsi@ipartners.pl>
* Additional modifictions by <reyalp (at) gmail dot com> and other CHDK contributors
*
* libptp2 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 2 of the License, or
* (at your option) any later version.
*
* libptp2 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 libptp2; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "ptp.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) dgettext (PACKAGE, String)
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
# define textdomain(String) (String)
# define gettext(String) (String)
# define dgettext(Domain,Message) (Message)
# define dcgettext(Domain,Message,Type) (Message)
# define bindtextdomain(Domain,Directory) (Domain)
# define _(String) (String)
# define N_(String) (String)
#endif
#include "sockutil.h"
#define CHECK_PTP_RC(result) {uint16_t r=(result); if (r!=PTP_RC_OK) return r;}
#define PTP_CNT_INIT(cnt) {memset(&cnt,0,sizeof(cnt));}
static void
ptp_debug (PTPParams *params, const char *format, ...)
{
va_list args;
va_start (args, format);
if (params->debug_func!=NULL)
params->debug_func (params->data, format, args);
else
{
vfprintf (stderr, format, args);
fprintf (stderr,"\n");
fflush (stderr);
}
va_end (args);
}
/* Pack / unpack functions */
#include "ptp-pack.c"
/* transport buffer functions */
/* read data from buffer up to max size, return length actually read */
int ptp_transport_read_from_buffer(PTPPacketBuffer *b,int max_size, unsigned char *dst) {
int len;
//printf("read_from_buffer pos:%d len:%d want %d\n",b->pos,b->len,max_size);
if(!b->len) {
return 0;
}
if(b->len > max_size) {
len = max_size;
} else {
len = b->len;
}
memcpy(dst,b->data + b->pos,len);
b->len -= len;
b->pos += len;
if(!b->len) {
b->pos = 0;
}
//printf("read_from_buffer pos:%d len:%d got %d\n",b->pos,b->len,len);
return len;
}
/* read at least min_size to buffer, buffer must be empty */
uint16_t ptp_transport_read_to_buffer(PTPParams *params,int min_size) {
PTPPacketBuffer *b = ¶ms->pkt_buf;
if(b->len || b->pos || min_size > PTP_PACKET_BUFFER_SIZE) {
printf("ptp_transport_read_to_buffer bad param %d %d %d\n",b->len,b->pos,min_size);
return PTP_ERROR_BADPARAM; // TODO
}
int total = 0;
unsigned char *p = b->data;
int avail = PTP_PACKET_BUFFER_SIZE;
do {
int rsize=params->read_func(p, avail, params->data);
if(rsize < 0) {
return PTP_ERROR_IO;
}
p += rsize;
avail -= rsize;
total += rsize;
} while(total < min_size);
b->len = total;
return PTP_RC_OK;
}
/* read size form buffer or transport */
uint16_t ptp_control_read_buffered(PTPParams *params,int size, unsigned char *dst) {
PTPPacketBuffer *b = ¶ms->pkt_buf;
int bcount = ptp_transport_read_from_buffer(b,size,dst);
//printf("control_read_buffered bcount %d\n",bcount);
// if less than size came from buffer, we know it's empty
if(bcount < size) {
//printf("control_read_buffered read %d\n",size-bcount);
uint16_t ret = ptp_transport_read_to_buffer(params,size - bcount);
if(ret != PTP_RC_OK) {
return ret;
}
if(ptp_transport_read_from_buffer(b,size-bcount,dst+bcount) != size-bcount) {
printf("ptp_control_read_buffer bad length\n");
return PTP_ERROR_BADPARAM; // TODO
}
}
return PTP_RC_OK;
}
/* ptp/ip send / receive functions */
#ifdef CHDKPTP_PTPIP
// read exactly size dataphase data
uint16_t ptp_tcp_read_data(PTPParams *params, unsigned size, unsigned char *dst)
{
int rtotal = ptp_transport_read_from_buffer(¶ms->pkt_buf,size,dst);
// if less than size in buffer, need to read directly
while(rtotal < size) {
int rlen = params->read_func(dst + rtotal, size - rtotal, params->data);
if(rlen < 0) {
return PTP_ERROR_IO;
}
rtotal += rlen;
}
return PTP_RC_OK;
}
// read the non-payload part of a packet
uint16_t ptp_tcp_read_control(PTPParams *params, void *dst)
{
PTPIPContainer *pkt = dst;
uint16_t ret = ptp_control_read_buffered(params,PTPIP_HEADER_LEN,(unsigned char *)pkt);
if(ret != PTP_RC_OK) {
return ret;
}
uint32_t length = dtoh32(pkt->length);
uint32_t type = dtoh32(pkt->type);
switch(type) {
case PTPIP_TYPE_INIT_CMD_ACK:
//case PTPIP_TYPE_INIT_EVENT_ACK:
case PTPIP_TYPE_INIT_FAIL:
case PTPIP_TYPE_RESP:
case PTPIP_TYPE_START_DATA:
if(length > PTPIP_HEADER_LEN) {
ret = ptp_control_read_buffered(params, length - PTPIP_HEADER_LEN, (unsigned char *)pkt+PTPIP_HEADER_LEN);
}
break;
case PTPIP_TYPE_DATA:
case PTPIP_TYPE_END_DATA:
// transaction id
ret = ptp_control_read_buffered(params, 4, (unsigned char *)pkt+PTPIP_HEADER_LEN);
break; // length includes payload data
default:
printf("unexpected packet type 0x%x\n",type);
ret = PTP_ERROR_BADPARAM;
}
return ret;
}
uint16_t ptp_tcp_sendreq(PTPParams *params, PTPContainer *ptp)
{
PTPIPContainer pkt;
uint16_t ret;
unsigned length = (sizeof(pkt.req) + 8) - 4*(5-ptp->Nparam); // req plus header
memset(&pkt,0,sizeof(pkt));
pkt.length = htod32(length);
pkt.type = htod32(PTPIP_TYPE_REQ);
pkt.req.dataphase = 0; // name from wireshark dissector, appears to be 0
// even if there is a send or receive data phase
// actual meaning unclear
pkt.req.trans_id=htod32(ptp->Transaction_ID);
pkt.req.code=htod16(ptp->Code);
pkt.req.param1=htod32(ptp->Param1);
pkt.req.param2=htod32(ptp->Param2);
pkt.req.param3=htod32(ptp->Param3);
pkt.req.param4=htod32(ptp->Param4);
pkt.req.param5=htod32(ptp->Param5);
ret=params->write_func((unsigned char *)&pkt, length, params->data);
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
return ret;
}
uint16_t
ptp_tcp_senddata (PTPParams* params, PTPContainer* ptp,
unsigned char *data, unsigned int size)
{
uint16_t ret;
unsigned length;
// start data packet
PTPIPContainer pkt;
memset(&pkt,0,sizeof(pkt));
length = 20; // header + transaction ID + 64 bit length
pkt.length = htod32(length);
pkt.type = htod32(PTPIP_TYPE_START_DATA);
pkt.datactl.trans_id = htod32(ptp->Transaction_ID);
pkt.datactl.data_length = size;/*htod64(size);*/ // TODO
ret=params->write_func((unsigned char *)&pkt, length, params->data);
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
// "data packet" ... not clear why this is needed when start data gives size
// allows breaking data into multiple "packets"
memset(&pkt,0,sizeof(pkt));
pkt.length = htod32(12 + size); // header + transaction ID + total data length?
pkt.type = htod32(PTPIP_TYPE_DATA);
ret=params->write_func((unsigned char *)&pkt, 12, params->data); // size is actual size of our packet
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
// write actual data
ret=params->write_func(data, size, params->data);
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
// end data packet
memset(&pkt,0,sizeof(pkt));
pkt.length = 12; // header + transaction ID
pkt.type = htod32(PTPIP_TYPE_END_DATA);
pkt.datactl.trans_id = htod32(ptp->Transaction_ID);
ret=params->write_func((unsigned char *)&pkt, pkt.length, params->data);
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
return ret;
}
uint16_t
ptp_tcp_getresp (PTPParams* params, PTPContainer* resp)
{
uint16_t ret;
static PTPIPContainer pkt;
memset(&pkt,0,sizeof(pkt));
// printf("get_resp\n");
/* read response, it should never be longer than sizeof(pkt) */
ret=params->read_control_func(params,&pkt);
if(ret != PTP_RC_OK) {
printf("read_control_func failed 0x%x\n",ret);
return ret;
}
if (dtoh16(pkt.type)!=PTPIP_TYPE_RESP) {
ret = PTP_ERROR_RESP_EXPECTED;
} else if (dtoh16(pkt.resp.code)!=resp->Code) {
ret = dtoh16(pkt.resp.code);
}
/* build an appropriate PTPContainer */
resp->Code=dtoh16(pkt.resp.code);
resp->SessionID=params->session_id;
resp->Transaction_ID=dtoh32(pkt.resp.trans_id);
resp->Param1=dtoh32(pkt.resp.param1);
resp->Param2=dtoh32(pkt.resp.param2);
resp->Param3=dtoh32(pkt.resp.param3);
resp->Param4=dtoh32(pkt.resp.param4);
resp->Param5=dtoh32(pkt.resp.param5);
resp->Nparam=(dtoh32(pkt.length)-(8+6))/4; // TODO header + code + trans_id
return ret;
}
uint16_t
ptp_tcp_getdata (PTPParams* params, PTPContainer* ptp, PTPGetdataParams *gdparams)
{
PTPIPContainer pkt;
uint64_t total_len;
uint64_t total_read=0;
uint16_t ret;
ret=params->read_control_func(params,&pkt);
if(ret != PTP_RC_OK) {
return ret;
}
if (dtoh32(pkt.type)!=PTPIP_TYPE_START_DATA) {
printf("expected START_DATA\n");
return PTP_ERROR_DATA_EXPECTED;
}
//printf("got START_DATA\n");
// get overall length
total_len=pkt.datactl.data_length;//TODO dtoh64(pkt.datactl.data_length)
unsigned char *buf = NULL;
unsigned char *p;
uint32_t block_size;
if(gdparams->handler) {
if (gdparams->block_size == 0) {
block_size = 1024*1024*2; // TODO
} else if (gdparams->block_size < 1024) {
block_size = 1024;
} else {
block_size = gdparams->block_size;
}
buf = malloc(block_size);
p = buf;
} else {
if(!gdparams->ret_data) {
gdparams->ret_data=malloc(total_len);
}
if(!gdparams->ret_data) {
return PTP_ERROR_NOMEM;
}
p = gdparams->ret_data;
block_size = 0;
}
gdparams->total_size = total_len;
int got_end = 0;
uint32_t pkt_len;
uint32_t block_read = 0;
do {
// read the DATA / END_DATA packet
ret=params->read_control_func(params,&pkt);
if(ret != PTP_RC_OK) {
break;
}
// END_DATA can contain data, may be used instead of DATA packet
if (dtoh32(pkt.type)==PTPIP_TYPE_END_DATA) {
// printf("END_DATA\n");
// TODO should verify that length gives correct total on end
got_end = 1;
} else if (dtoh32(pkt.type)!=PTPIP_TYPE_DATA) {
printf("expected DATA\n");
ret = PTP_ERROR_DATA_EXPECTED;
break;
}
//printf("got %s\n",((dtoh32(pkt.type)==PTPIP_TYPE_DATA)?"DATA":"END_DATA"));
pkt_len = dtoh32(pkt.length) - 12;
if(block_size) {
uint32_t pkt_read = 0;
while(pkt_read < pkt_len) {
uint32_t read_size;
uint32_t block_left = block_size - block_read;
uint32_t pkt_left = pkt_len - pkt_read;
if(block_left < pkt_left) {
read_size = block_left;
} else {
read_size = pkt_left;
}
//printf("read total:%d packet:%d block:%d call:%d\n",(int)total_read,pkt_read,block_read,read_size);
ret=params->read_data_func(params, read_size, p);
if(ret != PTP_RC_OK) {
break;
}
pkt_read += read_size;
p+=read_size;
block_read += read_size;
total_read += read_size;
// reached block size, send to handler
if(block_read == block_size) {
//printf("flush:%d\n",block_size);
ret = gdparams->handler(params,gdparams,block_size,buf);
if(ret != PTP_RC_OK) {
break;
}
// new block
p = buf;
block_read = 0;
}
}
if(ret != PTP_RC_OK) {
break;
}
} else {
ret=params->read_data_func(params, pkt_len, p);
if(ret != PTP_RC_OK) {
break;
}
p += pkt_len;
total_read += pkt_len;
}
} while (total_read < total_len);
// caller responsible for freeing if not handler buf
if(block_size) {
// send any final partial block to the handler
if(ret == PTP_RC_OK && block_read) {
//printf("final flush:%d\n",block_read);
ret = gdparams->handler(params,gdparams,block_read,buf);
}
free(buf);
}
if(ret != PTP_RC_OK) {
return ret;
}
// read the END_DATA packet
if(!got_end) {
ret=params->read_control_func(params,&pkt);
if(ret != PTP_RC_OK) {
return ret;
}
// printf("got END_DATA\n");
if (dtoh32(pkt.type)!=PTPIP_TYPE_END_DATA) {
printf("expected END_DATA\n");
return PTP_ERROR_DATA_EXPECTED;
}
}
return PTP_RC_OK;
}
#endif // CHDKPTP_PTPIP
// read exactly size dataphase data, buffering any excess
uint16_t ptp_usb_read_data(PTPParams *params, unsigned size, unsigned char *dst)
{
int rtotal = ptp_transport_read_from_buffer(¶ms->pkt_buf,size,dst);
// if less than size in buffer, need to read directly
if(rtotal < size) {
// USB only wants to read whole packets
// TODO this is inefficient, if we start unaligned, will user buffer every trans
int toread = size - rtotal;
int rest = toread % PTP_USB_BULK_HS_MAX_PACKET_LEN; // could use connection max packet size
int bulktotal = size - rest;
//printf("read_data size:%d buf:%d bulk:%d rest:%d\n",size,rtotal,bulktotal,rest);
while(rtotal < bulktotal) {
int rlen = params->read_func(dst + rtotal, bulktotal - rtotal, params->data);
if(rlen < 0) {
return PTP_ERROR_IO;
}
rtotal += rlen;
}
uint16_t ret = ptp_transport_read_to_buffer(params,rest);
if(ret != PTP_RC_OK) {
return ret;
}
rtotal += ptp_transport_read_from_buffer(¶ms->pkt_buf,rest,dst+rtotal);
if(rtotal != size) {
printf("usb_read_data size mismatch! %d != %d\n",rtotal,size);
return PTP_ERROR_IO;
}
}
return PTP_RC_OK;
}
// read the non-payload part of a packet
uint16_t ptp_usb_read_control(PTPParams *params, void *dst)
{
PTPUSBBulkContainer *pkt=dst;
uint16_t ret = ptp_control_read_buffered(params,PTP_USB_BULK_HDR_LEN,(unsigned char *)pkt);
if(ret != PTP_RC_OK) {
return ret;
}
uint32_t length = dtoh32(pkt->length);
uint16_t type = dtoh16(pkt->type);
switch(type) {
case PTP_USB_CONTAINER_RESPONSE:
if(length > PTP_USB_BULK_HDR_LEN) {
ret = ptp_control_read_buffered(params, length-PTP_USB_BULK_HDR_LEN, (unsigned char *)pkt+PTP_USB_BULK_HDR_LEN);
}
case PTP_USB_CONTAINER_DATA:
break;
// case PTP_USB_CONTAINER_EVENT:
default:
printf("unexpected packet type 0x%x\n",type);
ret = PTP_ERROR_BADPARAM;
}
return ret;
}
/* usb send / receive functions */
uint16_t
ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
{
static uint16_t ret;
static PTPUSBBulkContainer usbreq;
PTP_CNT_INIT(usbreq);
/* build appropriate USB container */
usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
(sizeof(uint32_t)*(5-req->Nparam)));
usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
usbreq.code=htod16(req->Code);
usbreq.trans_id=htod32(req->Transaction_ID);
usbreq.payload.params.param1=htod32(req->Param1);
usbreq.payload.params.param2=htod32(req->Param2);
usbreq.payload.params.param3=htod32(req->Param3);
usbreq.payload.params.param4=htod32(req->Param4);
usbreq.payload.params.param5=htod32(req->Param5);
/* send it to responder */
ret=params->write_func((unsigned char *)&usbreq,
PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam)),
params->data);
if (ret!=PTP_RC_OK) {
ret = PTP_ERROR_IO;
}
return ret;
}
uint16_t
ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
unsigned char *data, unsigned int size)
{
static uint16_t ret;
static PTPUSBBulkContainer usbdata;
/* build appropriate USB container */
usbdata.length=htod32(PTP_USB_BULK_HDR_LEN+size);
usbdata.type=htod16(PTP_USB_CONTAINER_DATA);
usbdata.code=htod16(ptp->Code);
usbdata.trans_id=htod32(ptp->Transaction_ID);
memcpy(usbdata.payload.data,data,
(size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN);
/* send first part of data */
ret=params->write_func((unsigned char *)&usbdata, PTP_USB_BULK_HDR_LEN+
((size<PTP_USB_BULK_PAYLOAD_LEN)?size:PTP_USB_BULK_PAYLOAD_LEN),
params->data);
if (ret!=PTP_RC_OK)
{
ret = PTP_ERROR_IO;
return ret;
}
if (size > PTP_USB_BULK_PAYLOAD_LEN)
{
/* if everything OK send the rest */
ret=params->write_func (data+PTP_USB_BULK_PAYLOAD_LEN,
size-PTP_USB_BULK_PAYLOAD_LEN, params->data);
if (ret!=PTP_RC_OK)
{
ret = PTP_ERROR_IO;
return ret;
}
}
// If data size is a multiple of the bulk transfer max endpoint size
// then send a dummy 0 length packet to tell the camera the transfer is complete
if ((usbdata.length & (params->max_packet_size-1)) == 0)
params->write_func(data, 0, params->data);
return ret;
}
uint16_t
ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPGetdataParams *gdparams)
{
uint16_t ret;
static PTPUSBBulkContainer usbdata;
PTP_CNT_INIT(usbdata);
/* read data header */
ret = params->read_control_func(params,&usbdata);
if(ret != PTP_RC_OK) {
return ret;
}
if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
return PTP_ERROR_DATA_EXPECTED;
}
if (dtoh16(usbdata.code)!=ptp->Code) {
return dtoh16(usbdata.code);
}
/* evaluate data length */
uint32_t total_len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
gdparams->total_size = total_len;
if(gdparams->handler) {
uint32_t block_size;
if (gdparams->block_size == 0) {
block_size = 1024*1024*2; // TODO
} else if (gdparams->block_size < 1024) {
block_size = 1024;
} else {
block_size = gdparams->block_size;
}
unsigned char *buf = malloc(block_size);
if(!buf) {
return PTP_ERROR_NOMEM;
}
uint32_t to_read = total_len;
while(to_read > 0) {
uint32_t read_size;
if(to_read > block_size) {
read_size = block_size;
} else {
read_size = to_read;
}
//printf("read_data %d\n",read_size);
ret=params->read_data_func(params, read_size, buf);
if(ret != PTP_RC_OK) {
printf("read_data failed 0x%x\n",ret);
break;
}
to_read -= read_size;
//printf("handler %d\n",read_size);
ret = gdparams->handler(params,gdparams,read_size,buf);
if(ret != PTP_RC_OK) {
printf("getdata handler failed 0x%x\n",ret);
break;
}
}
free(buf);
return ret;
} else {
if(!gdparams->ret_data) {
gdparams->ret_data=malloc(total_len);
}
if(!gdparams->ret_data) {
return PTP_ERROR_NOMEM;
}
return params->read_data_func(params, total_len, gdparams->ret_data);
}
}
uint16_t
ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
{
uint16_t ret = PTP_RC_OK;
PTPUSBBulkContainer usbresp;
PTP_CNT_INIT(usbresp);
ret = params->read_control_func(params,&usbresp);
if(ret != PTP_RC_OK) {
return ret;
}
if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
ret = PTP_ERROR_RESP_EXPECTED;
} else
if (dtoh16(usbresp.code)!=resp->Code) {
ret = dtoh16(usbresp.code);
}
if (ret!=PTP_RC_OK) {
return ret;
}
/* build an appropriate PTPContainer */
resp->Code=dtoh16(usbresp.code);
resp->SessionID=params->session_id;
resp->Transaction_ID=dtoh32(usbresp.trans_id);
resp->Param1=dtoh32(usbresp.payload.params.param1);
resp->Param2=dtoh32(usbresp.payload.params.param2);
resp->Param3=dtoh32(usbresp.payload.params.param3);
resp->Param4=dtoh32(usbresp.payload.params.param4);
resp->Param5=dtoh32(usbresp.payload.params.param5);
resp->Nparam=(dtoh32(usbresp.length)-12)/4;
return ret;
}
/* major PTP functions */
/* Transaction data phase description */
#define PTP_DP_NODATA 0x0000 /* no data phase */
#define PTP_DP_SENDDATA 0x0001 /* sending data */
#define PTP_DP_GETDATA 0x0002 /* receiving data */
#define PTP_DP_DATA_MASK 0x00ff /* data phase mask */
/**
* ptp_transaction:
* params: PTPParams*
* PTPContainer* ptp - general ptp container
* uint16_t flags - lower 8 bits - data phase description
* unsigned int sendlen - senddata phase data length
* char** data - send or receive data buffer pointer
*
* Performs PTP transaction. ptp is a PTPContainer with appropriate fields
* filled in (i.e. operation code and parameters). It's up to caller to do
* so.
* The flags decide whether the transaction has a data phase and what is its
* direction (send or receive).
* If transaction is sending data the sendlen should contain its length in
* bytes, otherwise it's ignored.
* The data should contain an address of a pointer to data going to be sent
* or is filled with such a pointer address if data are received depending
* od dataphase direction (send or received) or is beeing ignored (no
* dataphase).
* The memory for a pointer should be preserved by the caller, if data are
* beeing retreived the appropriate amount of memory is beeing allocated
* (the caller should handle that!).
*
* Return values: Some PTP_RC_* code.
* Upon success PTPContainer* ptp contains PTP Response Phase container with
* all fields filled in.
**/
static uint16_t
ptp_transaction (PTPParams* 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;
/* send request */
CHECK_PTP_RC(params->sendreq_func (params, ptp));
/* is there a dataphase? */
switch (flags&PTP_DP_DATA_MASK) {
case PTP_DP_SENDDATA:
CHECK_PTP_RC(params->senddata_func(params, ptp,
(unsigned char*)*data, sendlen));
break;
case PTP_DP_GETDATA: {
PTPGetdataParams gdparams;
gdparams.handler = NULL;
gdparams.ret_data = *data;
uint16_t ret = params->getdata_func(params, ptp,&gdparams);
// if allocated by getdata, set
if(!*data) {
*data = gdparams.ret_data;
}
if(ret != PTP_RC_OK) {
return ret;
}
break;
}
case PTP_DP_NODATA:
break;
default:
return PTP_ERROR_BADPARAM;
}
/* get response */
CHECK_PTP_RC(params->getresp_func(params, ptp));
return PTP_RC_OK;
}
/*
* reyalp - added more flexible data transfer - read chunks and send to callback instead of buffering all
* TODO this is mostly a copy / paste of ptp_transaction now
*/
static uint16_t ptp_getdata_transaction(PTPParams* params, PTPContainer* ptp, PTPGetdataParams *gdparams )
{
if ((params==NULL) || (ptp==NULL) || (gdparams==NULL))
return PTP_ERROR_BADPARAM;
ptp->Transaction_ID=params->transaction_id++;
ptp->SessionID=params->session_id;
/* send request */
CHECK_PTP_RC(params->sendreq_func(params, ptp));
/* get dataphase assumed */
CHECK_PTP_RC(params->getdata_func(params, ptp, gdparams));
/* get response */
CHECK_PTP_RC(params->getresp_func(params, ptp));
return PTP_RC_OK;
}
/* Events handling functions */
/* PTP Events wait for or check mode */
#define PTP_EVENT_CHECK 0x0000 /* waits for */
#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
#define CHECK_INT(usbevent, size) { \
switch(wait) { \
case PTP_EVENT_CHECK: \
result+=params->check_int_func((unsigned char*)&usbevent+result, \
size-result, params->data); \
break; \
case PTP_EVENT_CHECK_FAST: \
result+=params->check_int_fast_func((unsigned char*)&usbevent+result, \
size-result, params->data); \
break; \
default: \
return PTP_ERROR_BADPARAM; \
}\
}
static inline uint16_t
ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
{
int result=0, size=0;
static PTPUSBEventContainer usbevent;
PTP_CNT_INIT(usbevent);
if ((params==NULL) || (event==NULL))
return PTP_ERROR_BADPARAM;
CHECK_INT(usbevent, PTP_USB_INT_PACKET_LEN);
if (result<0)
return PTP_ERROR_IO;
size=dtoh32(usbevent.length);
while (result<size) {
CHECK_INT(usbevent, size);
if (result<0)
return PTP_ERROR_IO;
}
/* if we read anything over interrupt endpoint it must be an event */
/* build an appropriate PTPContainer */
event->Code=dtoh16(usbevent.code);
event->SessionID=params->session_id;
event->Transaction_ID=dtoh32(usbevent.trans_id);
event->Param1=dtoh32(usbevent.param1);
event->Param2=dtoh32(usbevent.param2);
event->Param3=dtoh32(usbevent.param3);
return PTP_RC_OK;
}
uint16_t
ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
ptp_debug(params,"PTP: Checking for Event");
return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
}
uint16_t
ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
ptp_debug(params,"PTP: Waiting for Event");
return ptp_usb_event (params, event, PTP_EVENT_CHECK);
}
/**
* PTP operation functions
*
* all ptp_ functions should take integer parameters
* in host byte order!
**/
/**
* ptp_getdeviceinfo:
* params: PTPParams*
*
* Gets device info dataset and fills deviceinfo structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getdeviceinfo (PTPParams* params, PTPDeviceInfo* deviceinfo)
{
uint16_t ret;
PTPContainer ptp;
char* di=NULL;
ptp_debug(params,"PTP: Obtaining DeviceInfo");
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetDeviceInfo;
ptp.Nparam=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &di);
if (ret == PTP_RC_OK) ptp_unpack_DI(params, di, deviceinfo);
free(di);
return ret;
}
/**
* ptp_opensession:
* params: PTPParams*
* session - session number
*
* Establishes a new session.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_opensession (PTPParams* params, uint32_t session)
{
uint16_t ret;
PTPContainer ptp;
ptp_debug(params,"PTP: Opening session 0x%08x", session);
/* SessonID field of the operation dataset should always
be set to 0 for OpenSession request! */
params->session_id=0x00000000;
/* TransactionID should be set to 0 also! */
params->transaction_id=0x0000000;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_OpenSession;
ptp.Param1=session;
ptp.Nparam=1;
ret=ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL);
/* now set the global session id to current session number */
params->session_id=session;
return ret;
}
/**
* ptp_closesession:
* params: PTPParams*
*
* Closes session.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_closesession (PTPParams* params)
{
PTPContainer ptp;
ptp_debug(params,"PTP: Closing session");
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CloseSession;
ptp.Nparam=0;
return ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL);
}
/**
* ptp_getststorageids:
* params: PTPParams*
*
* Gets array of StorageiDs and fills the storageids structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getstorageids (PTPParams* params, PTPStorageIDs* storageids)
{
uint16_t ret;
PTPContainer ptp;
char* sids=NULL;
ptp_debug(params,"PTP: Obtaining StorageIDs");
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetStorageIDs;
ptp.Nparam=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &sids);
if (ret == PTP_RC_OK) ptp_unpack_SIDs(params, sids, storageids);
free(sids);
return ret;
}
/**
* ptp_getststorageinfo:
* params: PTPParams*
* storageid - StorageID
*
* Gets StorageInfo dataset of desired storage and fills storageinfo
* structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getstorageinfo (PTPParams* params, uint32_t storageid,
PTPStorageInfo* storageinfo)
{
uint16_t ret;
PTPContainer ptp;
char* si=NULL;
ptp_debug(params,"PTP: Obtaining StorageInfo for storage 0x%08x",
storageid);