pict
Documentation for pict is still a work-in-progress
 
Loading...
Searching...
No Matches
libpict.h
Go to the documentation of this file.
1
2// Copyright 2022 by Daniel C (https://github.com/petabyt/libpict)
3#ifndef PTP_LIB_H
4#define PTP_LIB_H
5
6#include <stdio.h>
7#include <stdint.h>
8#include <pthread.h>
9
10#include "ptp.h"
11
12// Max timeout for command read/writes
13#define PTP_TIMEOUT 1000
14
15// How much ms to wait for r->wait_for_response
16#define PTP_WAIT_MS 1000
17
18// Conforms to POSIX 2001, some compilers may not have it
19#ifndef PTP_SLEEP
20 #include <unistd.h>
21 //int usleep(unsigned int usec);
22 #define PTP_SLEEP(ms) usleep((unsigned int)(ms) * 1000)
23#endif
24
25#ifdef WIN32
26#define PUB __declspec(dllexport)
27#else
28#define PUB
29#endif
30
31// 1mb default buffer size
32#define PTP_DEFAULT_SIZE 1000000
33
34// Logging+panic mechanism, define it yourself or link in log.c
36PUB void ptp_verbose_log(char *fmt, ...);
38PUB void ptp_error_log(char *fmt, ...);
40PUB __attribute__ ((noreturn)) void ptp_panic(char *fmt, ...);
42void ptp_report_read_progress(unsigned int size);
43
69
71const char *ptp_perror(int rc);
72
75 PTP_DEV_EMPTY = 0,
76 PTP_DEV_EOS = 1, // EOS DSLR/Mirrorless systems
77 PTP_DEV_CANON = 2, // Older powershot cameras
78 PTP_DEV_NIKON = 3,
79 PTP_DEV_SONY = 4,
80 PTP_DEV_FUJI = 5,
81 PTP_DEV_PANASONIC = 6,
82};
83
84// Wrapper types for vendor specific capture modes
85enum ImageFormats {
86 IMG_FORMAT_ETC = 0,
87 IMG_FORMAT_RAW = 1,
88 IMG_FORMAT_STD = 2,
89 IMG_FORMAT_HIGH = 3,
90 IMG_FORMAT_RAW_JPEG = 4,
91};
92
96 PTP_IP = (1 << 0),
97 PTP_IP_USB = (1 << 1), // TCP-based, but using USB-style packets (Fujifilm)
98 PTP_USB = (1 << 2),
99 PTP_BLE = (1 << 3), // I can only dream...
100};
101
104 void *next;
105 void *prev;
106 int code;
107 unsigned int memb_size;
108 unsigned int memb_cnt;
109 void *data;
110};
111
117
120
124
130
133 uint8_t *data;
135 unsigned int data_length;
137 unsigned int data_filled_length;
138
141 unsigned int max_packet_size;
142
146
149 unsigned int data_phase_length;
150
153
155 void *userdata;
156
158 pthread_mutex_t *mutex;
159
163
166
171
174
175 // TODO: Fudge uses this, should be moved to userdata struct
176 void *oc;
177};
178
181 uint16_t code;
182 const char *name;
183 int value;
184 const char *str_value;
185};
186
188 unsigned int data_of;
189 unsigned int n_entries;
190 unsigned int index;
191 void *ptr;
192};
193
196 uint16_t code;
197 uint32_t params[5];
198 unsigned int param_length;
199};
200
202struct PtpArray {
203 uint32_t length;
204 uint32_t data[];
205};
206
210
213PUB unsigned int ptp_get_param_length(struct PtpRuntime *r);
214
217PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i);
218
222
225PUB uint8_t *ptp_get_payload(struct PtpRuntime *r);
226
229PUB unsigned int ptp_get_payload_length(struct PtpRuntime *r);
230
232PUB struct PtpRuntime *ptp_new(int options);
233
236PUB void ptp_reset(struct PtpRuntime *r);
237
239PUB void ptp_init(struct PtpRuntime *r);
240
242PUB void ptp_close(struct PtpRuntime *r);
243
245PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd);
246
248PUB int ptp_send_data(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int length);
249
251PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
252
254PUB void ptp_mutex_unlock(struct PtpRuntime *r);
255
259
261PUB void ptp_mutex_lock(struct PtpRuntime *r);
262
265PUB int ptp_device_type(struct PtpRuntime *r);
266
269PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode);
270
273PUB int ptp_check_prop(struct PtpRuntime *r, int code);
274
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, const 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) {
289 ((uint8_t *)buf)[0] = out;
290 return 1;
291}
292inline static int ptp_write_u16(void *buf, uint16_t out) {
293 uint8_t *b = (uint8_t *)buf;
294 b[0] = out & 0xFF;
295 b[1] = (out >> 8) & 0xFF;
296 return 2;
297}
298inline static int ptp_write_u32(void *buf, uint32_t out) {
299 uint8_t *b = (uint8_t *)buf;
300 b[0] = out & 0xFF;
301 b[1] = (out >> 8) & 0xFF;
302 b[2] = (out >> 16) & 0xFF;
303 b[3] = (out >> 24) & 0xFF;
304 return 4;
305}
306inline static int ptp_read_u8(const void *buf, uint8_t *out) {
307 const uint8_t *b = (const uint8_t *)buf;
308 *out = b[0];
309 return 1;
310}
311inline static int ptp_read_u16(const void *buf, uint16_t *out) {
312 const uint8_t *b = (const uint8_t *)buf;
313 *out = (uint16_t)b[0] | ((uint16_t)b[1] << 8);
314 return 2;
315}
316inline static int ptp_read_u32(const void *buf, uint32_t *out) {
317 const uint8_t *b = (const uint8_t *)buf;
318 *out = (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
319 return 4;
320}
321inline static int ptp_read_u64(const void *buf, uint64_t *out) {
322 uint32_t lo, hi;
323 ptp_read_u32(buf, &lo);
324 ptp_read_u32((const uint8_t *)buf + 4, &hi);
325 *out = ((uint64_t)hi << 32) | lo;
326 return 8;
327}
328
329// Build a new PTP/IP or PTP/USB command packet in r->data
330unsigned int ptp_new_cmd_packet(struct PtpRuntime *r, const struct PtpCommand *cmd);
331
332// Only for PTP_USB or PTP_USB_IP use
333int ptpusb_new_data_packet(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int data_length);
334
335// Only use for PTP_IP
336unsigned int ptpip_data_start_packet(struct PtpRuntime *r, unsigned int data_length);
337unsigned int ptpip_data_end_packet(struct PtpRuntime *r, const void *data, unsigned int data_length);
338
339// Used only by ptp_open_session
340__attribute__((deprecated))
341void ptp_update_transaction(struct PtpRuntime *r, int t);
342
343// Set avail info for prop
344void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, unsigned int memb_size, unsigned int cnt, void *data);
345
346__attribute__((deprecated))
347void *ptp_dup_payload(struct PtpRuntime *r);
348
351int ptp_dump(struct PtpRuntime *r);
352
353#include "cl_data.h"
354#include "cl_backend.h"
355#include "cl_ops.h"
356#include "cl_enum.h"
357#include "cl_bind.h"
358
359// Backwards compatibility (mostly renamed functions)
360#ifdef PTP_OLD_FUNC_COMPAT
361 #define ptp_get_last_transaction(...) ptp_get_last_transaction_id(__VA_ARGS__)
362 #define ptp_generic_new(...) ptp_new(__VA_ARGS__)
363 #define ptp_generic_close(...) ptp_close(__VA_ARGS__)
364 #define ptp_generic_reset(...) ptp_reset(__VA_ARGS__)
365 #define ptp_generic_init(...) ptp_init(__VA_ARGS__)
366 #define ptp_generic_send(...) ptp_send(__VA_ARGS__)
367 #define ptp_generic_send_data(...) ptp_send_data(__VA_ARGS__)
368#endif
369
370#undef PUB // avoid conflict
371
372#endif
PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd)
Send a command request to the device with no data phase.
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.
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.
PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec)
Try and get an event from the camera over int endpoint (USB-only)
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.
PUB void ptp_close(struct PtpRuntime *r)
Frees PtpRuntime data buffer - doesn't free the actual structure, or device info (yet)
PtpConnType
Tells lib what backend and packet style to use.
Definition libpict.h:95
PUB int ptp_send_data(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int length)
Send a command request to the device with a data phase (thread safe)
PUB uint8_t * ptp_get_payload(struct PtpRuntime *r)
Get ptr of packet payload in data buffer, after packet header.
PUB unsigned int ptp_get_payload_length(struct PtpRuntime *r)
Get length of payload returned by ptp_get_payload.
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.
PUB unsigned int ptp_get_param_length(struct PtpRuntime *r)
Get number of parameters in packet in data buffer.
PUB void ptp_verbose_log(char *fmt,...)
Verbose log debugging info, could be called dozens of times per second.
PtpGeneralError
Library errors, not PTP return codes.
Definition libpict.h:45
@ PTP_CHECK_CODE
response code is not PTP_RC_OK
Definition libpict.h:62
@ PTP_NO_DEVICE
No device found (USB)
Definition libpict.h:48
@ PTP_RUNTIME_ERR
Unexpected, unhandled, or illegal behavior.
Definition libpict.h:58
@ PTP_COMMAND_IGNORED
No response.
Definition libpict.h:67
@ PTP_IO_ERR
General IO or communication error.
Definition libpict.h:56
@ PTP_OPEN_FAIL
Found device, but failed to connect.
Definition libpict.h:52
@ PTP_CANCELED
Operation (such as download) was canceled by another thread.
Definition libpict.h:64
@ PTP_NO_PERM
EPERM or other permission denied error.
Definition libpict.h:50
@ PTP_UNSUPPORTED
Operation or functionality isn't implemented or supported.
Definition libpict.h:60
@ PTP_OUT_OF_MEM
malloc failed
Definition libpict.h:54
PtpVendors
Unique camera types - each type should have similar opcodes and behavior.
Definition libpict.h:74
PUB void ptp_mutex_unlock(struct PtpRuntime *r)
Unlock the IO mutex (unless it was kept locked)
PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size)
Mostly for internal use - realloc the data buffer.
PUB __attribute__((noreturn)) void ptp_panic(char *fmt
Client has no way out, crash the application.
PUB int ptp_get_return_code(struct PtpRuntime *r)
Returns the return code (RC) currently in the data buffer.
PUB void ptp_error_log(char *fmt,...)
Used for critical IO errors, (not runtime errors)
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 void ptp_report_read_progress(unsigned int size)
Update UI progress on download progress.
PUB int ptp_device_type(struct PtpRuntime *r)
Gets type of device from r->di.
Generic Struct for arrays.
Definition libpict.h:202
Generic PTP command structure - accepted by operation API.
Definition libpict.h:195
Stream reader state struct for struct PtpGenericEvent.
Definition libpict.h:187
Generic event / property change.
Definition libpict.h:180
Linked list to handle currently possible values for a property.
Definition libpict.h:103
Represents a single device connection.
Definition libpict.h:114
void * userdata
Free pointer to hold per ptp session information.
Definition libpict.h:155
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 libpict.h:116
FILE * comm_dump
If non-NULL, all reads/writes will be logged to this file.
Definition libpict.h:173
uint8_t response_wait_default
Default value for wait_for_response.
Definition libpict.h:165
unsigned int data_filled_length
Size of valid PTP data in data in bytes.
Definition libpict.h:137
unsigned int max_packet_size
Definition libpict.h:141
void * comm_backend
For session comm/io structures (holds backend instance pointers)
Definition libpict.h:152
uint8_t * data
Global buffer for data reading and writing.
Definition libpict.h:133
struct PtpPropAvail * avail
For devices that implement it, this will hold a linked list of properties and an array of their suppo...
Definition libpict.h:170
unsigned int data_phase_length
For Windows compatibility, this is set to indicate lenth for a data packet that will be sent after a ...
Definition libpict.h:149
int transaction
Current transaction ID.
Definition libpict.h:127
int session
Current session ID.
Definition libpict.h:129
pthread_mutex_t * mutex
Optional (see PTP_DONT_USE_MUTEX)
Definition libpict.h:158
unsigned int data_length
Size of data in bytes.
Definition libpict.h:135
struct PtpDeviceInfo * di
Info about current connection, used to detect camera type, supported opodes, etc.
Definition libpict.h:145
uint8_t wait_for_response
Optionally wait up to 256 seconds for a response. Some PTP operations require this,...
Definition libpict.h:162
uint8_t operation_kill_switch
Set to 1 when it is no longer safe to run operations (device is unresponsive, pipe issues)
Definition libpict.h:119
uint8_t connection_type
One of enum PtpConnType.
Definition libpict.h:123