Skip to content

Commit

Permalink
Implement ptpusb_device_list for libwpd, remove copypasted header, Lu…
Browse files Browse the repository at this point in the history
…a fixes
  • Loading branch information
petabyt committed Nov 17, 2024
1 parent 1c8f3c3 commit d3903fe
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 100 deletions.
1 change: 1 addition & 0 deletions src/cl_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct PtpDeviceEntry {
};

/// @brief Get a linked list of USB or PTP Devices
/// @returns linked list of devices or NULL if no devices are connected (or OS error)
/// @memberof PTP/USB
struct PtpDeviceEntry *ptpusb_device_list(struct PtpRuntime *r);

Expand Down
1 change: 1 addition & 0 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void ptp_mutex_lock(struct PtpRuntime *r) {
pthread_mutex_lock(r->mutex);
}

__attribute__((deprecated()))
void ptp_mutex_keep_locked(struct PtpRuntime *r) {
if (r->mutex == NULL) return;
pthread_mutex_lock(r->mutex);
Expand Down
16 changes: 7 additions & 9 deletions src/libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ struct LibUSBBackend {
libusb_device_handle *handle;
};

// TODO: If this is accidentally called in the middle of a connection, it will cause a huge fault
static int ptp_comm_init(struct PtpRuntime *r) {
ptp_reset(r);
int ptp_comm_init(struct PtpRuntime *r) {
if (!r->io_kill_switch) {
ptp_panic("Connection is active\n");
}

// libusb 1.0 has no specificed limit for reads/writes
r->max_packet_size = 512 * 4;
Expand All @@ -33,6 +34,7 @@ static int ptp_comm_init(struct PtpRuntime *r) {
memset(r->comm_backend, 0, sizeof(struct LibUSBBackend));

struct LibUSBBackend *backend = (struct LibUSBBackend *)r->comm_backend;
if (backend == NULL) ptp_panic("");

ptp_verbose_log("Initializing libusb...\n");
libusb_init(&(backend->ctx));
Expand All @@ -44,14 +46,10 @@ static int ptp_comm_init(struct PtpRuntime *r) {
}

struct PtpDeviceEntry *ptpusb_device_list(struct PtpRuntime *r) {
if (r->comm_backend == NULL) {
ptp_verbose_log("comm_backend is NULL\n");
return NULL;
}
ptp_comm_init(r);

if (!r->io_kill_switch) {
ptp_verbose_log("Connection is active\n");
return NULL;
ptp_panic("Connection is active\n");
}

ptp_mutex_lock(r);
Expand Down
69 changes: 63 additions & 6 deletions src/libwpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <libwpd.h>

int ptp_comm_init(struct PtpRuntime *r) {
ptp_reset(r);

// We are not using low-level I/O operations, so this is never used
r->max_packet_size = 512;

Expand All @@ -32,7 +30,6 @@ int ptp_device_init(struct PtpRuntime *r) {
}

ptp_comm_init(r);

struct WpdStruct *wpd = (struct WpdStruct *)(r->comm_backend);
if (wpd == NULL) return PTP_IO_ERR;

Expand Down Expand Up @@ -69,13 +66,73 @@ int ptp_device_init(struct PtpRuntime *r) {
return PTP_NO_DEVICE;
}

// Unimplemented, don't use
struct PtpDeviceEntry *ptpusb_device_list(struct PtpRuntime *r) {
ptp_panic("Unsupported");
ptp_comm_init(r);
struct WpdStruct *wpd = (struct WpdStruct *)(r->comm_backend);

ptp_mutex_lock(r);

int num_devices = 0;
wchar_t **list = wpd_get_devices(wpd, &num_devices);
if (num_devices == 0) {
ptp_mutex_unlock(r);
return NULL;
}

struct PtpDeviceEntry *curr_ent = calloc(1, sizeof(struct PtpDeviceEntry));

struct PtpDeviceEntry *new_list = curr_ent;

for (int i = 0; i < num_devices; i++) {
if (i != 0) {
struct PtpDeviceEntry *next_ent = calloc(1, sizeof(struct PtpDeviceEntry));
next_ent->prev = curr_ent;
next_ent->next = NULL;
curr_ent->next = next_ent;
curr_ent = next_ent;
} else {
curr_ent->prev = NULL;
}

struct LibWPDDescriptor d;
int rc = wpd_parse_io_path(list[i], &d);
if (rc) ptp_panic("wpd_parse_io_path");

curr_ent->product_id = d.product_id;
curr_ent->vendor_id = d.vendor_id;
curr_ent->device_handle_ptr = list[i];
}

ptp_mutex_unlock(r);
return new_list;
}

int ptp_device_open(struct PtpRuntime *r, struct PtpDeviceEntry *entry) {
ptp_panic("Unsupported");
ptp_mutex_lock(r);
struct WpdStruct *wpd = (struct WpdStruct *)(r->comm_backend);

int ret = wpd_open_device(wpd, entry->device_handle_ptr);
if (ret) {
ptp_mutex_unlock(r);
return PTP_OPEN_FAIL;
}

int type = wpd_get_device_type(wpd);
if (type == WPD_DEVICE_TYPE_CAMERA) {
r->io_kill_switch = 0;
r->operation_kill_switch = 0;
ptp_mutex_unlock(r);
return 0;
}

ptp_verbose_log("Device is not a camera!\n");

ptp_mutex_unlock(r);
return PTP_OPEN_FAIL;
}

void ptpusb_free_device_list_entry(void *handle) {

}

int ptp_cmd_write(struct PtpRuntime *r, void *to, int length) {
Expand Down
80 changes: 0 additions & 80 deletions src/libwpd.h

This file was deleted.

23 changes: 23 additions & 0 deletions src/lua/doc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--- Runs PTP_OC_GetDeviceInfo and returns a structure
--- @return table
function ptp.getDeviceInfo()
end
--- Connect to the first available PTP device
--- @return number
function ptp.connect()
end
--- Perform a PTP operation with custom options
--- @param params table have a list of up to 5 integer parameters.
--- @param payload table payload to be sent to device.
--- @return table structure such as {"error": 0, "code": 0x2001, "payload": [0, 1, 2]}
function ptp.sendOperation(opcode, params, payload)
end
--- Run PTP_OC_SetDevicePropValue
--- @param propCode number PTP_DPC_*
--- @param value number value to be set, as a 32 bit integer
function ptp.setProperty(propCode, value)
end
--- Trigger the camera to take a picture
--- @return number Camlib error code
function ptp.takePicture()
end
9 changes: 7 additions & 2 deletions src/lua/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ static int mylua_set_property(lua_State *L) {
static int mylua_device_info(lua_State *L) {
struct PtpRuntime *r = luaptp_get_runtime(L);

if (r->di == NULL) return 1;
struct PtpDeviceInfo di;
int rc = ptp_get_device_info(r, &di);
if (rc) {
lua_pushinteger(L, rc);
return 1;
}

char buffer[4096];
ptp_device_info_json(r->di, buffer, sizeof(buffer));
ptp_device_info_json(&di, buffer, sizeof(buffer));

lua_json_decode(L, buffer, strlen(buffer));

Expand Down
6 changes: 3 additions & 3 deletions src/lua/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int cam_lua_setup(lua_State *L);

int luaopen_cjson(lua_State *l);

static char error_buffer[64];
static char error_buffer[512] = {0};

static struct CamLuaTasks {
int tasks;
Expand Down Expand Up @@ -62,13 +62,13 @@ static int get_task_id(lua_State *L) {

static int lua_script_print(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
ptp_verbose_log("Lua: %s\n", str);
ptp_verbose_log("%s\n", str);
return 1;
}

static int lua_script_toast(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
ptp_verbose_log("Lua: %s\n", str);
ptp_verbose_log("%s\n", str);
return 1;
}

Expand Down

0 comments on commit d3903fe

Please sign in to comment.