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// Units of time in ms to sleep 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, ...);
41
67
69const char *ptp_perror(int rc);
70
73 PTP_DEV_EMPTY = 0,
74 PTP_DEV_EOS = 1, // EOS DSLR/Mirrorless systems
75 PTP_DEV_CANON = 2, // Older powershot cameras
76 PTP_DEV_NIKON = 3,
77 PTP_DEV_SONY = 4,
78 PTP_DEV_FUJI = 5,
79 PTP_DEV_PANASONIC = 6,
80};
81
82// Wrapper types for vendor specific capture modes
83enum ImageFormats {
84 IMG_FORMAT_ETC = 0,
85 IMG_FORMAT_RAW = 1,
86 IMG_FORMAT_STD = 2,
87 IMG_FORMAT_HIGH = 3,
88 IMG_FORMAT_RAW_JPEG = 4,
89};
90
94 PTP_IP = (1 << 0),
95 PTP_IP_USB = (1 << 1), // TCP-based, but using USB-style packets (Fujifilm)
96 PTP_USB = (1 << 2),
97 PTP_BLE = (1 << 3), // I can only dream...
98};
99
102 void *next;
103 void *prev;
104 int code;
105 unsigned int memb_size;
106 unsigned int memb_cnt;
107 void *data;
108};
109
115
118
122
128
131 uint8_t *data;
133 unsigned int data_length;
135 unsigned int data_filled_length;
136
139 unsigned int max_packet_size;
140
144
147 unsigned int data_phase_length;
148
150 struct PtpCommPriv *comm_priv;
151
155
157 void *userdata;
158
160 struct PtpUserPriv *priv;
161
163 pthread_mutex_t *mutex;
164
168
171
176
179
180 // TODO: Fudge uses this, should be moved to userdata struct
181 void *oc;
182};
183
185void ptp_report_read_progress(struct PtpRuntime *r, unsigned int size);
186
189 uint16_t code;
190 const char *name;
191 int value;
192 const char *str_value;
193};
194
196 unsigned int data_of;
197 unsigned int n_entries;
198 unsigned int index;
199 void *ptr;
200};
201
204 uint16_t code;
205 uint32_t params[5];
206 unsigned int param_length;
207};
208
210struct PtpArray {
211 uint32_t length;
212 uint32_t data[];
213};
214
218
221PUB unsigned int ptp_get_param_length(struct PtpRuntime *r);
222
225PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i);
226
230
233PUB uint8_t *ptp_get_payload(struct PtpRuntime *r);
234
237PUB unsigned int ptp_get_payload_length(struct PtpRuntime *r);
238
240PUB struct PtpRuntime *ptp_new(int options);
241
244PUB void ptp_reset(struct PtpRuntime *r);
245
247PUB void ptp_init(struct PtpRuntime *r);
248
250PUB void ptp_close(struct PtpRuntime *r);
251
253PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd);
254
256PUB int ptp_send_data(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int length);
257
259PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
260
262PUB void ptp_mutex_unlock(struct PtpRuntime *r);
263
267
269PUB void ptp_mutex_lock(struct PtpRuntime *r);
270
273PUB int ptp_device_type(struct PtpRuntime *r);
274
277PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode);
278
281PUB int ptp_check_prop(struct PtpRuntime *r, int code);
282
285PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size);
286
287// Data structure functions
288PUB int ptp_write_unicode_string(char *dat, const char *string);
289PUB int ptp_read_unicode_string(char *buffer, const char *dat, int max);
290PUB int ptp_read_utf8_string(void *dat, char *string, int max);
291PUB int ptp_read_string(uint8_t *dat, char *string, int max);
292PUB int ptp_write_string(uint8_t *dat, const char *string);
293PUB int ptp_write_utf8_string(void *dat, const char *string);
294PUB int ptp_read_uint16_array(const uint8_t *dat, uint16_t *buf, int max, int *length);
295PUB int ptp_read_uint16_array_s(uint8_t *bs, uint8_t *be, uint16_t *buf, int max, int *length);
296inline static int ptp_write_u8(void *buf, uint8_t out) {
297 ((uint8_t *)buf)[0] = out;
298 return 1;
299}
300inline static int ptp_write_u16(void *buf, uint16_t out) {
301 uint8_t *b = (uint8_t *)buf;
302 b[0] = out & 0xFF;
303 b[1] = (out >> 8) & 0xFF;
304 return 2;
305}
306inline static int ptp_write_u32(void *buf, uint32_t out) {
307 uint8_t *b = (uint8_t *)buf;
308 b[0] = out & 0xFF;
309 b[1] = (out >> 8) & 0xFF;
310 b[2] = (out >> 16) & 0xFF;
311 b[3] = (out >> 24) & 0xFF;
312 return 4;
313}
314inline static int ptp_read_u8(const void *buf, uint8_t *out) {
315 const uint8_t *b = (const uint8_t *)buf;
316 *out = b[0];
317 return 1;
318}
319inline static int ptp_read_u16(const void *buf, uint16_t *out) {
320 const uint8_t *b = (const uint8_t *)buf;
321 *out = (uint16_t)b[0] | ((uint16_t)b[1] << 8);
322 return 2;
323}
324inline static int ptp_read_u32(const void *buf, uint32_t *out) {
325 const uint8_t *b = (const uint8_t *)buf;
326 *out = (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
327 return 4;
328}
329inline static int ptp_read_u64(const void *buf, uint64_t *out) {
330 uint32_t lo, hi;
331 ptp_read_u32(buf, &lo);
332 ptp_read_u32((const uint8_t *)buf + 4, &hi);
333 *out = ((uint64_t)hi << 32) | lo;
334 return 8;
335}
336
337// Build a new PTP/IP or PTP/USB command packet in r->data
338unsigned int ptp_new_cmd_packet(struct PtpRuntime *r, const struct PtpCommand *cmd);
339
340// Only for PTP_USB or PTP_USB_IP use
341int ptpusb_new_data_packet(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int data_length);
342
343// Only use for PTP_IP
344unsigned int ptpip_data_start_packet(struct PtpRuntime *r, unsigned int data_length);
345unsigned int ptpip_data_end_packet(struct PtpRuntime *r, const void *data, unsigned int data_length);
346
347// Set avail info for prop
348void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, unsigned int memb_size, unsigned int cnt, void *data);
349
352int ptp_dump(struct PtpRuntime *r);
353
355int ptp_run_lua(const char *filename);
356
357#include "cl_data.h"
358#include "cl_backend.h"
359#include "cl_ops.h"
360#include "cl_enum.h"
361#include "cl_bind.h"
362
363#undef PUB // avoid conflict
364
365#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 struct.
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)
Resets all session-specific fields of PtpRuntime This must be called if you plan to use the same PtpR...
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:93
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.
int ptp_run_lua(const char *filename)
Quick function to run script through Lua bindings (if it's compiled in).
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.
void ptp_report_read_progress(struct PtpRuntime *r, unsigned int size)
Update UI progress on download progress.
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:43
@ PTP_CHECK_CODE
response code is not PTP_RC_OK
Definition libpict.h:60
@ PTP_NO_DEVICE
No device found (USB).
Definition libpict.h:46
@ PTP_RUNTIME_ERR
Unexpected, unhandled, or illegal behavior.
Definition libpict.h:56
@ PTP_COMMAND_IGNORED
No response.
Definition libpict.h:65
@ PTP_IO_ERR
General IO or communication error.
Definition libpict.h:54
@ PTP_OPEN_FAIL
Found device, but failed to connect.
Definition libpict.h:50
@ PTP_CANCELED
Operation (such as download) was canceled by another thread.
Definition libpict.h:62
@ PTP_NO_PERM
EPERM or other permission denied error.
Definition libpict.h:48
@ PTP_UNSUPPORTED
Operation or functionality isn't implemented or supported.
Definition libpict.h:58
@ PTP_OUT_OF_MEM
malloc failed
Definition libpict.h:52
PtpVendors
Unique camera types - each type should have similar opcodes and behavior.
Definition libpict.h:72
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 int ptp_device_type(struct PtpRuntime *r)
Gets type of device from r->di.
Generic Struct for arrays.
Definition libpict.h:210
Generic PTP command structure - accepted by operation API.
Definition libpict.h:203
Stream reader state struct for struct PtpGenericEvent.
Definition libpict.h:195
Generic event / property change.
Definition libpict.h:188
Linked list to handle currently possible values for a property.
Definition libpict.h:101
Represents a single device connection.
Definition libpict.h:112
void * userdata
Free pointer to hold per ptp session information.
Definition libpict.h:157
struct PtpUserPriv * priv
Struct to hold per ptp session information, can be set freely.
Definition libpict.h:160
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:114
FILE * comm_dump
If non-NULL, all reads/writes will be logged to this file.
Definition libpict.h:178
uint8_t response_wait_default
Default value for wait_for_response.
Definition libpict.h:170
unsigned int data_filled_length
Size of valid PTP data in data in bytes.
Definition libpict.h:135
unsigned int max_packet_size
Definition libpict.h:139
struct PtpCommPriv * comm_priv
Private instance of comm/io structure.
Definition libpict.h:150
void * comm_backend
void* pointer for comm/io instance
Definition libpict.h:154
uint8_t * data
Global buffer for data reading and writing.
Definition libpict.h:131
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:175
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:147
int transaction
Current transaction ID.
Definition libpict.h:125
int session
Current session ID.
Definition libpict.h:127
pthread_mutex_t * mutex
Optional (see PTP_DONT_USE_MUTEX).
Definition libpict.h:163
unsigned int data_length
Size of data in bytes.
Definition libpict.h:133
struct PtpDeviceInfo * di
Info about current connection, used to detect camera type, supported opodes, etc.
Definition libpict.h:143
uint8_t wait_for_response
Optionally wait up to 256 seconds for a response. Some PTP operations require this,...
Definition libpict.h:167
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:117
uint8_t connection_type
One of enum PtpConnType.
Definition libpict.h:121