Skip to content

Commit 0212409

Browse files
committed
More documentation, update to new libwpd backend
1 parent 045546b commit 0212409

File tree

15 files changed

+213
-195
lines changed

15 files changed

+213
-195
lines changed

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CFLAGS += -D CAMLIB_NO_COMPAT -D VERBOSE
77
# - log.c can be replaced with a custom logging mechanism
88
# - ip.c can be replaced with no_ip.c
99
# - libwpd.c or libusb.c can be replaced with no_usb.c
10+
# - bind.c and liveview.c can be omitted together
1011
CAMLIB_CORE := operations.o packet.o enums.o data.o enum_dump.o lib.o canon.o liveview.o bind.o ip.o ml.o conv.o generic.o transport.o log.o
1112
CAMLIB_CORE := $(addprefix src/,$(CAMLIB_CORE))
1213

@@ -17,6 +18,7 @@ EXTRAS := src/canon_adv.o src/object.o
1718
UNIX_CFLAGS = $(shell pkg-config --cflags libusb-1.0)
1819
UNIX_LDFLAGS = $(shell pkg-config --libs libusb-1.0)
1920
UNIX_LIB_FILES := $(CAMLIB_CORE) $(EXTRAS) src/libusb.o
21+
WIN_LIB_FILES := $(CAMLIB_CORE) $(EXTRAS) src/libwpd.o
2022

2123
TARGET ?= l
2224
ifeq ($(TARGET),m)
@@ -36,6 +38,10 @@ all: libcamlib.dll
3638
MINGW := x86_64-w64-mingw32
3739
CC := $(MINGW)-gcc
3840
CPP := $(MINGW)-c++
41+
MINGW_LIBS := -lwpd -luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lmsimg32 -lcomdlg32 -ld2d1 -ldwrite -lole32 -loleaut32 -loleacc -lstdc++ -lgcc -lpthread -lssp -lurlmon -luuid -lws2_32
42+
libcamlib.dll: $(WIN_LIB_FILES)
43+
$(CC) -shared $(WIN_LIB_FILES) $(MINGW_LIBS) -o libcamlib.dll
44+
LDFLAGS := $(MINGW_LIBS)
3945
endif
4046

4147
%.o: %.c
@@ -47,8 +53,8 @@ DEC_FILES := src/dec/main.o src/enums.o src/enum_dump.o src/packet.o src/conv.o
4753
dec: $(DEC_FILES)
4854
$(CC) $(DEC_FILES) $(CFLAGS) -o $@
4955

50-
camlib: src/cli.o $(UNIX_LIB_FILES)
51-
$(CC) src/cli.o $(UNIX_LIB_FILES) $(CFLAGS) $(UNIX_LDFLAGS) -o $@
56+
camlib: src/cli.o $(WIN_LIB_FILES)
57+
$(CC) src/cli.o $(WIN_LIB_FILES) $(CFLAGS) $(LDFLAGS) -o $@
5258

5359
# Run this thing frequently
5460
stringify:

src/bind.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ int bind_get_object_info(struct BindReq *bind, struct PtpRuntime *r) {
146146
return len;
147147
}
148148

149+
int bind_send_object_info(struct BindReq *bind, struct PtpRuntime *r) {
150+
struct PtpObjectInfo oi;
151+
strcpy(oi.filename, "FOO_BAR.JPG");
152+
int x = ptp_send_object_info(r, 0, 0, &oi);
153+
if (x) return bind->out(bind, "{\"error\": %d}", x);
154+
int len = bind->out(bind, "{\"error\": %d}", x);
155+
return len;
156+
}
157+
149158
int bind_custom(struct BindReq *bind, struct PtpRuntime *r) {
150159
struct PtpCommand cmd;
151160
cmd.code = bind->params[0];
@@ -257,6 +266,15 @@ int bind_set_property(struct BindReq *bind, struct PtpRuntime *r) {
257266
return bind->out(bind, "{\"error\": %d}", x);
258267
}
259268

269+
int bind_get_property(struct BindReq *bind, struct PtpRuntime *r) {
270+
int x = ptp_get_prop_value(r, bind->params[0]);
271+
if (x) {
272+
return bind->out(bind, "{\"error\": %d}", x);
273+
} else {
274+
return bind->out(bind, "{\"error\": %d, \"value\": %u}", x, ptp_parse_prop_value(r));
275+
}
276+
}
277+
260278
int bind_get_events(struct BindReq *bind, struct PtpRuntime *r) {
261279
int dev = ptp_device_type(r);
262280

@@ -541,9 +559,11 @@ struct RouteMap {
541559
{"ptp_eos_set_event_mode", bind_eos_set_event_mode},
542560

543561
{"ptp_cancel_af", bind_cancel_af},
544-
562+
// Flip DSLR mirror up - or enter liveview
545563
{"ptp_mirror_up", bind_mirror_up},
564+
// Flip mirror down - or disable liveview
546565
{"ptp_mirror_down", bind_mirror_down},
566+
// Focus lens in/out
547567
{"ptp_drive_lens", bind_drive_lens},
548568
{"ptp_get_liveview_frame", bind_get_liveview_frame},
549569
{"ptp_get_liveview_type", bind_get_liveview_type},
@@ -552,17 +572,21 @@ struct RouteMap {
552572
{"ptp_ml_init_bmp_lv", bind_ml_init_bmp_lv},
553573
{"ptp_init_liveview", bind_liveview_init},
554574
{"ptp_deinit_liveview", bind_liveview_deinit},
575+
// Try and detect manufacturer/type of device connected
555576
{"ptp_get_device_type", bind_get_device_type},
577+
// Get a generic list of events
556578
{"ptp_get_events", bind_get_events},
557579
{"ptp_get_all_props", bind_get_all_props},
558580
{"ptp_set_property", bind_set_property},
581+
{"ptp_get_property", bind_get_property},
559582
{"ptp_get_enums", bind_get_enums},
560583
{"ptp_get_status", bind_get_status},
561584
{"ptp_get_return_code", bind_get_return_code},
562585
{"ptp_get_storage_ids", bind_get_storage_ids},
563586
{"ptp_get_storage_info", bind_get_storage_info},
564587
{"ptp_get_object_handles", bind_get_object_handles},
565588
{"ptp_get_object_info", bind_get_object_info},
589+
{"ptp_send_object_info", bind_send_object_info},
566590
{"ptp_get_thumbnail", bind_get_thumbnail},
567591
{"ptp_get_partial_object", bind_get_partial_object},
568592
{"ptp_download_file", bind_download_file},
@@ -576,5 +600,5 @@ int bind_run_req(struct PtpRuntime *r, struct BindReq *bind) {
576600
}
577601
}
578602

579-
return -1;
603+
return 1;
580604
}

src/camlib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,27 @@ __attribute__ ((noreturn)) void ptp_panic(char *fmt, ...);
3939
/// @brief Camlib library errors, not PTP return codes
4040
enum PtpGeneralError {
4141
PTP_OK = 0,
42+
/// @brief No device found (USB)
4243
PTP_NO_DEVICE = -1,
44+
/// @brief EPERM or other permission denied error
4345
PTP_NO_PERM = -2,
46+
/// @brief Found device, but failed to connect
4447
PTP_OPEN_FAIL = -3,
48+
/// @brief malloc failed
4549
PTP_OUT_OF_MEM = -4,
50+
/// @brief General IO or communication error
4651
PTP_IO_ERR = -5,
52+
/// @brief Unexpected, unhandled, or illegal behavior
4753
PTP_RUNTIME_ERR = -6,
54+
/// @brief Operation or functionality isn't implemented or supported
4855
PTP_UNSUPPORTED = -7,
56+
/// @brief response code is not PTP_RC_OK
4957
PTP_CHECK_CODE = -8,
58+
/// @brief Operation (such as download) was canceled by another thread
5059
PTP_CANCELED = -9,
60+
/// @brief No response
61+
/// @note Used internally only
62+
PTP_COMMAND_IGNORED = -10,
5163
};
5264

5365
/// @brief Evaluates PtpGeneralError into string message

src/cl_backend.h

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#define PTP_TIMEOUT 1000
55

6-
// Linked-list entry for a single USB device
6+
/// @brief Linked-list entry for a single USB device
77
struct PtpDeviceEntry {
88
struct PtpDeviceEntry *prev;
99

@@ -22,46 +22,66 @@ struct PtpDeviceEntry {
2222
struct PtpDeviceEntry *next;
2323
};
2424

25+
/// @brief Initialize communications context for the current thread
2526
int ptp_comm_init(struct PtpRuntime *r);
27+
28+
/// @brief Get a linked list of USB or PTP Devices
29+
/// @memberof PTP/USB
2630
struct PtpDeviceEntry *ptpusb_device_list(struct PtpRuntime *r);
31+
/// @memberof PTP/USB
2732
void ptpusb_free_device_list(struct PtpDeviceEntry *e);
33+
/// @brief Open and connect to a device from the PtpDeviceEntry structure
34+
/// @memberof PTP/USB
2835
int ptp_device_open(struct PtpRuntime *r, struct PtpDeviceEntry *entry);
2936

30-
// Init comm (if not already) and connect to the first device available
37+
/// @brief Runs ptp_comm_init and connect to the first device available
3138
int ptp_device_init(struct PtpRuntime *r);
3239

3340
// Temporary :)
34-
#define ptp_send_bulk_packet DEPRECATED_USE_ptp_cmd_write_INSTEAD
35-
#define ptp_receive_bulk_packet DEPRECATED_USE_ptp_cmd_read_INSTEAD
41+
//#define ptp_send_bulk_packet DEPRECATED_USE_ptp_cmd_write_INSTEAD
42+
//#define ptp_receive_bulk_packet DEPRECATED_USE_ptp_cmd_read_INSTEAD
3643

37-
// Bare IO, send a single 512 byte packet. Return negative or NULL on error.
44+
/// @brief Send data over the raw command endpoint
45+
/// @memberof PTP/USB
3846
int ptp_cmd_write(struct PtpRuntime *r, void *to, int length);
47+
/// @brief Receive raw data over the command endpoint
48+
/// @memberof PTP/USB
3949
int ptp_cmd_read(struct PtpRuntime *r, void *to, int length);
4050

41-
// Reset the pipe, can clear issues
51+
/// @brief Reset the USB device or endpoint if there is communication issues
52+
/// @memberof PTP/USB
4253
int ptp_device_reset(struct PtpRuntime *r);
4354

44-
// Recieve all packets, and whatever else (common logic for all backends)
55+
/// @brief Send packets in r->data
56+
/// @memberof PTP/USB
4557
int ptp_send_bulk_packets(struct PtpRuntime *r, int length);
58+
/// @brief Receive all packets into r->data
59+
/// @memberof PTP/USB
4660
int ptp_receive_bulk_packets(struct PtpRuntime *r);
61+
/// @brief Poll the interrupt endpoint
62+
/// @memberof PTP/USB
4763
int ptp_read_int(struct PtpRuntime *r, void *to, int length);
4864

49-
int ptp_device_close(struct PtpRuntime *r); // TODO: Disconnect, confusing with ptp_close
50-
51-
// Upload file data as packets, but upload r->data till length first
52-
int ptp_fsend_packets(struct PtpRuntime *r, int length, FILE *stream);
53-
54-
// Reads the incoming packet to file, starting after an optional offset
55-
int ptp_freceive_bulk_packets(struct PtpRuntime *r, FILE *stream, int of);
65+
/// @brief Disconnect from the current device
66+
/// @memberof PTP/USB
67+
int ptp_device_close(struct PtpRuntime *r);
5668

57-
int ptpip_connect(struct PtpRuntime *r, const char *addr, int port);
69+
/// @brief Connect to a TCP port on the default network adapter
70+
/// @memberof PTP/IP
71+
int ptpip_connect(struct PtpRuntime *r, const char *addr, int port, int extra_tmout);
72+
/// @memberof PTP/IP
5873
int ptpip_cmd_write(struct PtpRuntime *r, void *data, int size);
74+
/// @memberof PTP/IP
5975
int ptpip_cmd_read(struct PtpRuntime *r, void *data, int size);
6076

77+
/// @memberof PTP/IP
6178
int ptpip_connect_events(struct PtpRuntime *r, const char *addr, int port);
79+
/// @memberof PTP/IP
6280
int ptpip_event_send(struct PtpRuntime *r, void *data, int size);
81+
/// @memberof PTP/IP
6382
int ptpip_event_read(struct PtpRuntime *r, void *data, int size);
6483

65-
int ptpip_close(struct PtpRuntime *r); // TODO: Disconnect, confusing with ptp_close
84+
/// @memberof PTP/IP
85+
int ptpip_close(struct PtpRuntime *r);
6686

6787
#endif

src/cl_bind.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@
44
// Recommended buffer size for bind_run
55
#define PTP_BIND_DEFAULT_SIZE 5000000
66

7-
#define BIND_MAX_PARAM 5
87
#define BIND_MAX_NAME 64
9-
#define BIND_MAX_STRING 128
10-
#define BIND_MAX_BYTES 512
118

129
struct BindReq {
10+
// @brief Argument passed to out or out_bytes
1311
void *arg;
12+
// @brief Output JSON data
1413
int (*out)(struct BindReq *bind, char *fmt, ...);
14+
/// @brief Output raw binary data
1515
int (*out_bytes)(struct BindReq *bind, void *bytes, size_t length);
1616

17-
char *buffer;
18-
int max;
17+
/// @brief Name of command to be run
1918
char name[BIND_MAX_NAME];
2019

21-
int params[BIND_MAX_PARAM];
20+
/// @brief Parameters for command
21+
/// @note These are parsed by command handlers in bind.c, not always passed to the PTP operation
22+
int params[5];
2223
int params_length;
23-
24+
/// @brief String argument, NULL for none
2425
char *string;
25-
26+
/// @brief Data argument, NULL for none
2627
uint8_t *bytes;
2728
int bytes_length;
2829
};
2930

30-
// Run a binding - will return JSON
31-
//int bind_run(struct PtpRuntime *r, char *req, char *buffer, int size);
3231

33-
// Run a binding directly from the structure
32+
// @brief Run a binding directly from an instance of struct BindReq
3433
int bind_run_req(struct PtpRuntime *r, struct BindReq *bind);
3534

36-
//void bind_parse(struct BindReq *br, char *req);
37-
3835
#endif

src/cl_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int ptpip_init_events(struct PtpRuntime *r);
105105

106106
/// @note PTP/IP only
107107
/// @memberof PtpRuntime
108-
int ptpip_init_command_request(struct PtpRuntime *r, char *device_name);
108+
int ptpip_init_command_request(struct PtpRuntime *r, const char *device_name);
109109

110110
// EOS Only functions - not for Canon point and shoot
111111
int ptp_eos_get_viewfinder_data(struct PtpRuntime *r);

src/cli.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ static int run_binding(struct Options *o, struct BindReq *br) {
3939
ptp_open_session(r);
4040

4141
int rc = bind_run_req(r, br);
42+
if (rc > 0) {
43+
printf("Command %s does not exist.\n", br->name);
44+
}
4245
if (rc) return rc;
4346

47+
putchar('\n');
48+
4449
ptp_close_session(r);
4550

4651
ptp_device_close(r);
@@ -62,12 +67,11 @@ static int parse_run(struct Options *o, int argc, char **argv, int i) {
6267
struct BindReq br;
6368
br.params_length = 0;
6469
br.bytes_length = 0;
65-
br.buffer = malloc(5000);
6670
br.string = NULL;
6771
br.out = out_printf;
6872
br.out_bytes = NULL;
6973

70-
memset(br.params, 0, sizeof(int) * BIND_MAX_PARAM);
74+
memset(br.params, 0, sizeof(br.params));
7175
memset(br.name, 0, BIND_MAX_NAME);
7276

7377
strcpy(br.name, name);
@@ -80,11 +84,12 @@ static int parse_run(struct Options *o, int argc, char **argv, int i) {
8084
}
8185

8286
int rc = run_binding(o, &br);
83-
puts(br.buffer);
8487
return rc;
8588
}
8689

8790
int main(int argc, char **argv) {
91+
extern int camlib_verbose;
92+
camlib_verbose = 0;
8893
struct Options o;
8994
for (int i = 1; i < argc; i++) {
9095
if (!strcmp(argv[i], "--help")) {

0 commit comments

Comments
 (0)