camlib
Documentation for camlib is still a work-in-progress
Loading...
Searching...
No Matches
camlib.h
Go to the documentation of this file.
1
2// Main header file for Camlib
3// Copyright 2022 by Daniel C (https://github.com/petabyt/camlib)
4#ifndef PTP_LIB_H
5#define PTP_LIB_H
6
7#include <stdio.h>
8#include <stdint.h>
9#include <pthread.h>
10
11#include "ptp.h"
12
13// Max timeout for command read/writes
14#define PTP_TIMEOUT 1000
15
16// How much ms to wait for r->wait_for_response
17#define CAMLIB_WAIT_MS 1000
18
19// Conforms to POSIX 2001, some compilers may not have it
20#ifndef CAMLIB_SLEEP
21 #include <unistd.h>
22 //int usleep(unsigned int usec);
23 #define CAMLIB_SLEEP(ms) usleep(ms * 1000)
24#endif
25
26#ifdef WIN32
27#define PUB __declspec(dllexport)
28#else
29#define PUB
30#endif
31
32// Logging+panic mechanism, define it yourself or link in log.c
33void ptp_verbose_log(char *fmt, ...);
34__attribute__ ((noreturn)) void ptp_panic(char *fmt, ...);
35
36// 1mb default buffer size
37#define CAMLIB_DEFAULT_SIZE 1000000
38
64
66const char *ptp_perror(int rc);
67
70 PTP_DEV_EMPTY = 0,
71 PTP_DEV_EOS = 1, // EOS DSLR/Mirrorless systems
72 PTP_DEV_CANON = 2, // Older powershot cameras
73 PTP_DEV_NIKON = 3,
74 PTP_DEV_SONY = 4,
75 PTP_DEV_FUJI = 5,
76 PTP_DEV_PANASONIC = 6,
77};
78
79// Wrapper types for vendor specific capture modes
80enum ImageFormats {
81 IMG_FORMAT_ETC = 0,
82 IMG_FORMAT_RAW = 1,
83 IMG_FORMAT_STD = 2,
84 IMG_FORMAT_HIGH = 3,
85 IMG_FORMAT_RAW_JPEG = 4,
86};
87
91 PTP_IP = (1 << 0),
92 PTP_IP_USB = (1 << 1), // TCP-based, but using USB-style packets (Fujifilm)
93 PTP_USB = (1 << 2),
94 PTP_BLE = (1 << 3), // I can only dream...
95};
96
99 void *next;
100 void *prev;
101 int code;
102 int memb_size;
103 int memb_cnt;
104 void *data;
105};
106
112
115
119
122 int session;
123
126 uint8_t *data;
127 int data_length;
128
132
135 struct PtpDeviceInfo *di;
136 int device_type;
137
141
144
146 void *userdata;
147
149 pthread_mutex_t *mutex;
150
154
157
162
163 void *oc;
164};
165
168 uint16_t code;
169 const char *name;
170 int value;
171 const char *str_value;
172};
173
176 uint16_t code;
177 uint32_t params[5];
178 int param_length;
179};
180
182struct PtpArray {
183 uint32_t length;
184 uint32_t data[];
185};
186
191
196
200PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i);
201
206
210PUB uint8_t *ptp_get_payload(struct PtpRuntime *r);
211
216
219PUB struct PtpRuntime *ptp_new(int options);
220
224PUB void ptp_reset(struct PtpRuntime *r);
225
228PUB void ptp_init(struct PtpRuntime *r);
229
232PUB void ptp_close(struct PtpRuntime *r);
233
236PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd);
237
240PUB int ptp_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int length);
241
244PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
245
248PUB void ptp_mutex_unlock(struct PtpRuntime *r);
249
254
257PUB void ptp_mutex_lock(struct PtpRuntime *r);
258
262PUB int ptp_device_type(struct PtpRuntime *r);
263
267PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode);
268
272PUB int ptp_check_prop(struct PtpRuntime *r, int code);
273
277PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size);
278
279// Data structure functions
280PUB int ptp_write_unicode_string(char *dat, const char *string);
281PUB int ptp_read_unicode_string(char *buffer, char *dat, int max);
282PUB int ptp_read_utf8_string(void *dat, char *string, int max);
283PUB int ptp_read_string(uint8_t *dat, char *string, int max);
284PUB int ptp_write_string(uint8_t *dat, const char *string);
285PUB int ptp_write_utf8_string(void *dat, const char *string);
286PUB int ptp_read_uint16_array(const uint8_t *dat, uint16_t *buf, int max, int *length);
287PUB int ptp_read_uint16_array_s(uint8_t *bs, uint8_t *be, uint16_t *buf, int max, int *length);
288inline static int ptp_write_u8 (void *buf, uint8_t out) { ((uint8_t *)buf)[0] = out; return 1; }
289inline static int ptp_write_u16(void *buf, uint16_t out) { ((uint16_t *)buf)[0] = out; return 2; }
290inline static int ptp_write_u32(void *buf, uint32_t out) { ((uint32_t *)buf)[0] = out; return 4; }
291inline static int ptp_read_u32 (const void *buf, uint32_t *out) { *out = ((const uint32_t *)buf)[0]; return 4; }
292inline static int ptp_read_u16 (const void *buf, uint16_t *out) { *out = ((const uint16_t *)buf)[0]; return 2; }
293inline static int ptp_read_u8 (const void *buf, uint8_t *out) { *out = ((const uint8_t *)buf)[0]; return 1; }
294
295// Build a new PTP/IP or PTP/USB command packet in r->data
296int ptp_new_cmd_packet(struct PtpRuntime *r, struct PtpCommand *cmd);
297
298// Only for PTP_USB or PTP_USB_IP use
299int ptp_new_data_packet(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int data_length);
300
301// Only use for PTP_IP
302int ptpip_data_start_packet(struct PtpRuntime *r, int data_length);
303int ptpip_data_end_packet(struct PtpRuntime *r, void *data, int data_length);
304
305// Used only by ptp_open_session
306void ptp_update_transaction(struct PtpRuntime *r, int t);
307
308// Set avail info for prop
309void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, int memb_size, int cnt, void *data);
310
311void *ptp_dup_payload(struct PtpRuntime *r);
312
315int ptp_dump(struct PtpRuntime *r);
316
317#define CAMLIB_INCLUDE_IMPL
318#include "cl_data.h"
319#include "cl_backend.h"
320#include "cl_ops.h"
321#include "cl_enum.h"
322#include "cl_bind.h"
323
324// Backwards compatibility (mostly renamed functions)
325#ifndef CAMLIB_NO_COMPAT
326 #define ptp_get_last_transaction(...) ptp_get_last_transaction_id(__VA_ARGS__)
327 #define ptp_generic_new(...) ptp_new(__VA_ARGS__)
328 #define ptp_generic_close(...) ptp_close(__VA_ARGS__)
329 #define ptp_generic_reset(...) ptp_reset(__VA_ARGS__)
330 #define ptp_generic_init(...) ptp_init(__VA_ARGS__)
331 #define ptp_generic_send(...) ptp_send(__VA_ARGS__)
332 #define ptp_generic_send_data(...) ptp_send_data(__VA_ARGS__)
333#endif
334
335#endif
int ptp_dump(struct PtpRuntime *r)
Write r->data to a file called DUMP.
const char * ptp_perror(int rc)
Evaluates PtpGeneralError into string message.
PtpConnType
Tells lib what backend and packet style to use.
Definition camlib.h:90
PtpGeneralError
Camlib library errors, not PTP return codes.
Definition camlib.h:40
@ PTP_CHECK_CODE
response code is not PTP_RC_OK
Definition camlib.h:57
@ PTP_NO_DEVICE
No device found (USB)
Definition camlib.h:43
@ PTP_RUNTIME_ERR
Unexpected, unhandled, or illegal behavior.
Definition camlib.h:53
@ PTP_COMMAND_IGNORED
No response.
Definition camlib.h:62
@ PTP_IO_ERR
General IO or communication error.
Definition camlib.h:51
@ PTP_OPEN_FAIL
Found device, but failed to connect.
Definition camlib.h:47
@ PTP_CANCELED
Operation (such as download) was canceled by another thread.
Definition camlib.h:59
@ PTP_NO_PERM
EPERM or other permission denied error.
Definition camlib.h:45
@ PTP_UNSUPPORTED
Operation or functionality isn't implemented or supported.
Definition camlib.h:55
@ PTP_OUT_OF_MEM
malloc failed
Definition camlib.h:49
PtpVendors
Unique camera types - each type should have similar opcodes and behavior.
Definition camlib.h:69
Generic Struct for arrays.
Definition camlib.h:182
Generic PTP command structure - accepted by operation API.
Definition camlib.h:175
Generic event / property change.
Definition camlib.h:167
Linked list to handle currently possible values for a property.
Definition camlib.h:98
Represents a single device connection.
Definition camlib.h:109
PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd)
Send a command request to the device with no data phase.
PUB int ptp_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int length)
Send a command request to the device with a data phase (thread safe)
PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode)
Check if an opcode is supported by looking through supported props in r->di.
PUB void ptp_init(struct PtpRuntime *r)
Init PtpRuntime locally - uses default recommended settings (USB)
PUB struct PtpRuntime * ptp_new(int options)
Allocate new PtpRuntime based on bitfield options - see PtpConnType.
void * userdata
Free pointer to hold per ptp session information.
Definition camlib.h:146
uint8_t io_kill_switch
Set to 1 when it is no longer safe to send any data to the device (socket closed, device unplugged)
Definition camlib.h:111
PUB int ptp_get_last_transaction_id(struct PtpRuntime *r)
Get transaction ID of packet in the data buffer.
PUB void ptp_reset(struct PtpRuntime *r)
Reset all session-specific fields of PtpRuntime - both libusb and libwpd backends call this before es...
PUB void ptp_mutex_lock(struct PtpRuntime *r)
Lock the IO mutex - only should be used by backend.
uint8_t response_wait_default
Default value for wait_for_response.
Definition camlib.h:156
PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec)
Try and get an event from the camera over int endpoint (USB-only)
PUB void ptp_close(struct PtpRuntime *r)
Frees PtpRuntime data buffer - doesn't free the actual structure, or device info (yet)
void * comm_backend
For session comm/io structures (holds backend instance pointers)
Definition camlib.h:143
PUB uint8_t * ptp_get_payload(struct PtpRuntime *r)
Get ptr of packet payload in data buffer, after packet header.
PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i)
Get parameter at index i.
PUB int ptp_check_prop(struct PtpRuntime *r, int code)
Check if a property code is supported by looking through supported props in r->di.
uint8_t * data
Global buffer for data reading and writing.
Definition camlib.h:126
struct PtpPropAvail * avail
For devices that implement it, this will hold a linked list of properties and an array of their suppo...
Definition camlib.h:161
int transaction
Definition camlib.h:121
PUB int ptp_get_param_length(struct PtpRuntime *r)
Get number of parameters in packet in data buffer.
int max_packet_size
Definition camlib.h:131
PUB void ptp_mutex_unlock(struct PtpRuntime *r)
Unlock the IO mutex (unless it was kept locked)
pthread_mutex_t * mutex
Optional (see CAMLIB_DONT_USE_MUTEX)
Definition camlib.h:149
PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size)
Mostly for internal use - realloc the data buffer.
int data_phase_length
For Windows compatibility, this is set to indicate lenth for a data packet that will be sent after a ...
Definition camlib.h:140
PUB int ptp_get_return_code(struct PtpRuntime *r)
Returns the return code (RC) currently in the data buffer.
struct PtpDeviceInfo * di
Info about current connection, used to detect camera type, supported opodes, etc.
Definition camlib.h:135
uint8_t wait_for_response
Optionally wait up to 256 seconds for a response. Some PTP operations require this,...
Definition camlib.h:153
uint8_t operation_kill_switch
Set to 1 when it is no longer safe to run operations (device is unresponsive, pipe issues)
Definition camlib.h:114
PUB void ptp_mutex_unlock_thread(struct PtpRuntime *r)
Completely unlock the mutex for the current thread, to ensure there isn't a deadlock....
PUB int ptp_device_type(struct PtpRuntime *r)
Gets type of device from r->di.
uint8_t connection_type
One of enum PtpConnType.
Definition camlib.h:118
PUB int ptp_get_payload_length(struct PtpRuntime *r)
Get length of payload returned by ptp_get_payload.