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, ...);
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
152 struct PtpCommPriv *comm_priv;
153
157
159 void *userdata;
160
162 struct PtpUserPriv *priv;
163
165 pthread_mutex_t *mutex;
166
170
173
178
181
182 // TODO: Fudge uses this, should be moved to userdata struct
183 void *oc;
184};
185
188 uint16_t code;
189 const char *name;
190 int value;
191 const char *str_value;
192};
193
195 unsigned int data_of;
196 unsigned int n_entries;
197 unsigned int index;
198 void *ptr;
199};
200
203 uint16_t code;
204 uint32_t params[5];
205 unsigned int param_length;
206};
207
209struct PtpArray {
210 uint32_t length;
211 uint32_t data[];
212};
213
217
220PUB unsigned int ptp_get_param_length(struct PtpRuntime *r);
221
224PUB uint32_t ptp_get_param(struct PtpRuntime *r, int i);
225
229
232PUB uint8_t *ptp_get_payload(struct PtpRuntime *r);
233
236PUB unsigned int ptp_get_payload_length(struct PtpRuntime *r);
237
239PUB struct PtpRuntime *ptp_new(int options);
240
243PUB void ptp_reset(struct PtpRuntime *r);
244
246PUB void ptp_init(struct PtpRuntime *r);
247
249PUB void ptp_close(struct PtpRuntime *r);
250
252PUB int ptp_send(struct PtpRuntime *r, struct PtpCommand *cmd);
253
255PUB int ptp_send_data(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int length);
256
258PUB int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec);
259
261PUB void ptp_mutex_unlock(struct PtpRuntime *r);
262
266
268PUB void ptp_mutex_lock(struct PtpRuntime *r);
269
272PUB int ptp_device_type(struct PtpRuntime *r);
273
276PUB int ptp_check_opcode(struct PtpRuntime *r, int opcode);
277
280PUB int ptp_check_prop(struct PtpRuntime *r, int code);
281
284PUB int ptp_buffer_resize(struct PtpRuntime *r, size_t size);
285
286// Data structure functions
287PUB int ptp_write_unicode_string(char *dat, const char *string);
288PUB int ptp_read_unicode_string(char *buffer, const char *dat, int max);
289PUB int ptp_read_utf8_string(void *dat, char *string, int max);
290PUB int ptp_read_string(uint8_t *dat, char *string, int max);
291PUB int ptp_write_string(uint8_t *dat, const char *string);
292PUB int ptp_write_utf8_string(void *dat, const char *string);
293PUB int ptp_read_uint16_array(const uint8_t *dat, uint16_t *buf, int max, int *length);
294PUB int ptp_read_uint16_array_s(uint8_t *bs, uint8_t *be, uint16_t *buf, int max, int *length);
295inline static int ptp_write_u8(void *buf, uint8_t out) {
296 ((uint8_t *)buf)[0] = out;
297 return 1;
298}
299inline static int ptp_write_u16(void *buf, uint16_t out) {
300 uint8_t *b = (uint8_t *)buf;
301 b[0] = out & 0xFF;
302 b[1] = (out >> 8) & 0xFF;
303 return 2;
304}
305inline static int ptp_write_u32(void *buf, uint32_t out) {
306 uint8_t *b = (uint8_t *)buf;
307 b[0] = out & 0xFF;
308 b[1] = (out >> 8) & 0xFF;
309 b[2] = (out >> 16) & 0xFF;
310 b[3] = (out >> 24) & 0xFF;
311 return 4;
312}
313inline static int ptp_read_u8(const void *buf, uint8_t *out) {
314 const uint8_t *b = (const uint8_t *)buf;
315 *out = b[0];
316 return 1;
317}
318inline static int ptp_read_u16(const void *buf, uint16_t *out) {
319 const uint8_t *b = (const uint8_t *)buf;
320 *out = (uint16_t)b[0] | ((uint16_t)b[1] << 8);
321 return 2;
322}
323inline static int ptp_read_u32(const void *buf, uint32_t *out) {
324 const uint8_t *b = (const uint8_t *)buf;
325 *out = (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 24);
326 return 4;
327}
328inline static int ptp_read_u64(const void *buf, uint64_t *out) {
329 uint32_t lo, hi;
330 ptp_read_u32(buf, &lo);
331 ptp_read_u32((const uint8_t *)buf + 4, &hi);
332 *out = ((uint64_t)hi << 32) | lo;
333 return 8;
334}
335
336// Build a new PTP/IP or PTP/USB command packet in r->data
337unsigned int ptp_new_cmd_packet(struct PtpRuntime *r, const struct PtpCommand *cmd);
338
339// Only for PTP_USB or PTP_USB_IP use
340int ptpusb_new_data_packet(struct PtpRuntime *r, const struct PtpCommand *cmd, const void *data, unsigned int data_length);
341
342// Only use for PTP_IP
343unsigned int ptpip_data_start_packet(struct PtpRuntime *r, unsigned int data_length);
344unsigned int ptpip_data_end_packet(struct PtpRuntime *r, const void *data, unsigned int data_length);
345
346// Set avail info for prop
347void ptp_set_prop_avail_info(struct PtpRuntime *r, int code, unsigned int memb_size, unsigned int cnt, void *data);
348
351int ptp_dump(struct PtpRuntime *r);
352
354int ptp_run_lua(const char *filename);
355
356#include "cl_data.h"
357#include "cl_backend.h"
358#include "cl_ops.h"
359#include "cl_enum.h"
360#include "cl_bind.h"
361
362#undef PUB // avoid conflict
363
364#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: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.
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.
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:209
Generic PTP command structure - accepted by operation API.
Definition libpict.h:202
Stream reader state struct for struct PtpGenericEvent.
Definition libpict.h:194
Generic event / property change.
Definition libpict.h:187
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:159
struct PtpUserPriv * priv
Struct to hold per ptp session information, can be set freely.
Definition libpict.h:162
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:180
uint8_t response_wait_default
Default value for wait_for_response.
Definition libpict.h:172
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
struct PtpCommPriv * comm_priv
Private instance of comm/io structure.
Definition libpict.h:152
void * comm_backend
void* pointer for comm/io instance
Definition libpict.h:156
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:177
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:165
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:169
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