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
59
61const char *ptp_perror(int rc);
62
65 PTP_DEV_EMPTY = 0,
66 PTP_DEV_EOS = 1, // EOS DSLR/Mirrorless systems
67 PTP_DEV_CANON = 2, // Older powershot cameras
68 PTP_DEV_NIKON = 3,
69 PTP_DEV_SONY = 4,
70 PTP_DEV_FUJI = 5,
71 PTP_DEV_PANASONIC = 6,
72};
73
74// Wrapper types for vendor specific capture modes
75enum ImageFormats {
76 IMG_FORMAT_ETC = 0,
77 IMG_FORMAT_RAW = 1,
78 IMG_FORMAT_STD = 2,
79 IMG_FORMAT_HIGH = 3,
80 IMG_FORMAT_RAW_JPEG = 4,
81};
82
86 PTP_IP = (1 << 0),
87 PTP_IP_USB = (1 << 1), // TCP-based, but using USB-style packets (Fujifilm)
88 PTP_USB = (1 << 2),
89 PTP_BLE = (1 << 3), // I can only dream...
90};
91
94 void *next;
95 void *prev;
96 int code;
97 unsigned int memb_size;
98 unsigned int memb_cnt;
99 void *data;
100};
101
107
110
114
120
123 uint8_t *data;
125 unsigned int data_length;
127 unsigned int data_filled_length;
128
131 unsigned int max_packet_size;
132
136
139 unsigned int data_phase_length;
140
142 struct PtpCommPriv *comm_priv;
143
147
150 void (*report_read_progress)(struct PtpRuntime *r, unsigned int size);
151
154 int (*set_extra_socket_settings)(struct PtpRuntime *r, int sockfd);
155
157 void *userdata;
158
160 struct PtpUserPriv *priv;
161
163 pthread_mutex_t *mutex;
164
168
171
176
179};
180
181// Logging+panic mechanism, define it yourself or link in log.c
183PUB void ptp_verbose_log(struct PtpRuntime *r, char *fmt, ...);
185PUB void ptp_error_log(struct PtpRuntime *r, char *fmt, ...);
187PUB __attribute__ ((noreturn)) void ptp_panic(char *fmt, ...);
188
191 uint16_t code;
192 const char *name;
193 int value;
194 const char *str_value;
195};
196
198 unsigned int data_of;
199 unsigned int n_entries;
200 unsigned int index;
201 void *ptr;
202};
203
206 uint16_t code;
207 uint32_t params[5];
208 unsigned int param_length;
209};
210
212struct PtpArray {
213 uint32_t length;
214 uint32_t data[];
215};
216
220
223PUB unsigned int ptp_get_param_length(struct PtpRuntime *r);
224
227PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i);
228
232
235PUB uint8_t *ptp_get_payload(struct PtpRuntime *r);
236
239PUB unsigned int ptp_get_payload_length(struct PtpRuntime *r);
240
242PUB struct PtpRuntime *ptp_new(int options);
243
246PUB void ptp_reset(struct PtpRuntime *r);
247
249PUB void ptp_init(struct PtpRuntime *r);
250
252PUB void ptp_close(struct PtpRuntime *r);
253
255PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd);
256
258PUB int ptp_send_data(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int length);
259
261PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
262
264PUB void ptp_mutex_unlock(struct PtpRuntime *r);
265
269
271PUB void ptp_mutex_lock(struct PtpRuntime *r);
272
275PUB int ptp_device_type(struct PtpRuntime *r);
276
279PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode);
280
283PUB int ptp_check_prop(struct PtpRuntime *r, int code);
284
287PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size);
288
289// Data structure functions
290PUB int ptp_write_unicode_string(char *dat, const char *string);
291PUB int ptp_read_unicode_string(char *buffer, const char *dat, int max);
292PUB int ptp_read_utf8_string(void *dat, char *string, int max);
293PUB int ptp_read_string(uint8_t *dat, char *string, int max);
294PUB int ptp_write_string(uint8_t *dat, const char *string);
295PUB int ptp_write_utf8_string(void *dat, const char *string);
296PUB int ptp_read_uint16_array(const uint8_t *dat, uint16_t *buf, int max, int *length);
297PUB int ptp_read_uint16_array_s(uint8_t *bs, uint8_t *be, uint16_t *buf, int max, int *length);
298inline static int ptp_write_u8(void *buf, uint8_t out) {
299 ((uint8_t *)buf)[0] = out;
300 return 1;
301}
302inline static int ptp_write_u16(void *buf, uint16_t out) {
303 uint8_t *b = (uint8_t *)buf;
304 b[0] = out & 0xFF;
305 b[1] = (out >> 8) & 0xFF;
306 return 2;
307}
308inline static int ptp_write_u32(void *buf, uint32_t out) {
309 uint8_t *b = (uint8_t *)buf;
310 b[0] = out & 0xFF;
311 b[1] = (out >> 8) & 0xFF;
312 b[2] = (out >> 16) & 0xFF;
313 b[3] = (out >> 24) & 0xFF;
314 return 4;
315}
316inline static int ptp_read_u8(const void *buf, uint8_t *out) {
317 const uint8_t *b = (const uint8_t *)buf;
318 *out = b[0];
319 return 1;
320}
321inline static int ptp_read_u16(const void *buf, uint16_t *out) {
322 const uint8_t *b = (const uint8_t *)buf;
323 *out = (uint16_t)b[0] | ((uint16_t)b[1] << 8);
324 return 2;
325}
326inline static int ptp_read_u32(const void *buf, uint32_t *out) {
327 const uint8_t *b = (const uint8_t *)buf;
328 *out = (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
329 return 4;
330}
331inline static int ptp_read_u64(const void *buf, uint64_t *out) {
332 uint32_t lo, hi;
333 ptp_read_u32(buf, &lo);
334 ptp_read_u32((const uint8_t *)buf + 4, &hi);
335 *out = ((uint64_t)hi << 32) | lo;
336 return 8;
337}
338
339// Build a new PTP/IP or PTP/USB command packet in r->data
340unsigned int ptp_new_cmd_packet(struct PtpRuntime *r, const struct PtpCommand *cmd);
341
342// Only for PTP_USB or PTP_USB_IP use
343int ptpusb_new_data_packet(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int data_length);
344
345// Only use for PTP_IP
346unsigned int ptpip_data_start_packet(struct PtpRuntime *r, unsigned int data_length);
347unsigned int ptpip_data_end_packet(struct PtpRuntime *r, const void *data, unsigned int data_length);
348
349// Set avail info for prop
350void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, unsigned int memb_size, unsigned int cnt, void *data);
351
354int ptp_dump(struct PtpRuntime *r);
355
357int ptp_run_lua(const char *filename);
358
359#include "cl_data.h"
360#include "cl_backend.h"
361#include "cl_ops.h"
362#include "cl_enum.h"
363#include "cl_bind.h"
364
365#undef PUB // avoid conflict
366
367#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_verbose_log(struct PtpRuntime *r, char *fmt,...)
Verbose log debugging info, could be called dozens of times per second.
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:85
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.
PUB unsigned int ptp_get_param_length(struct PtpRuntime *r)
Get number of parameters in packet in data buffer.
PtpGeneralError
Library errors, not PTP return codes.
Definition libpict.h:35
@ PTP_CHECK_CODE
response code is not PTP_RC_OK
Definition libpict.h:52
@ PTP_NO_DEVICE
No device found (USB).
Definition libpict.h:38
@ PTP_RUNTIME_ERR
Unexpected, unhandled, or illegal behavior.
Definition libpict.h:48
@ PTP_COMMAND_IGNORED
No response.
Definition libpict.h:57
@ PTP_IO_ERR
General IO or communication error.
Definition libpict.h:46
@ PTP_OPEN_FAIL
Found device, but failed to connect.
Definition libpict.h:42
@ PTP_CANCELED
Operation (such as download) was canceled by another thread.
Definition libpict.h:54
@ PTP_NO_PERM
EPERM or other permission denied error.
Definition libpict.h:40
@ PTP_UNSUPPORTED
Operation or functionality isn't implemented or supported.
Definition libpict.h:50
@ PTP_OUT_OF_MEM
malloc failed
Definition libpict.h:44
PtpVendors
Unique camera types - each type should have similar opcodes and behavior.
Definition libpict.h:64
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_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.
PUB void ptp_error_log(struct PtpRuntime *r, char *fmt,...)
Used for critical IO errors, (not runtime errors).
Generic Struct for arrays.
Definition libpict.h:212
Generic PTP command structure - accepted by operation API.
Definition libpict.h:205
Stream reader state struct for struct PtpGenericEvent.
Definition libpict.h:197
Generic event / property change.
Definition libpict.h:190
Linked list to handle currently possible values for a property.
Definition libpict.h:93
Represents a single device connection.
Definition libpict.h:104
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:106
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:127
unsigned int max_packet_size
Definition libpict.h:131
struct PtpCommPriv * comm_priv
Private instance of comm/io structure.
Definition libpict.h:142
void * comm_backend
void* pointer for comm/io instance
Definition libpict.h:146
uint8_t * data
Global buffer for data reading and writing.
Definition libpict.h:123
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
int(* set_extra_socket_settings)(struct PtpRuntime *r, int sockfd)
Called after ptp/ip socket() call, meant for binding to other interfaces when necessary NULL by defau...
Definition libpict.h:154
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:139
int transaction
Current transaction ID.
Definition libpict.h:117
int session
Current session ID.
Definition libpict.h:119
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:125
void(* report_read_progress)(struct PtpRuntime *r, unsigned int size)
Definition libpict.h:150
struct PtpDeviceInfo * di
Info about current connection, used to detect camera type, supported opodes, etc.
Definition libpict.h:135
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:109
uint8_t connection_type
One of enum PtpConnType.
Definition libpict.h:113