diff --git a/lua/camlua.h b/lua/camlua.h index ba5335d..815de30 100644 --- a/lua/camlua.h +++ b/lua/camlua.h @@ -1,17 +1 @@ -#ifndef CAM_LUA_H -#define CAM_LUA_H - -#include -#include -#include - -LUALIB_API int luaopen_ptp(lua_State *L); - -lua_State *cam_new_task(int *id); -int lua_script_run_loop(int id); -int cam_run_lua_script(const char *buffer); -int cam_run_lua_script_async(const char *buffer); - -const char *cam_lua_get_error(); - -#endif \ No newline at end of file +#ifndef CAM_LUA_H #define CAM_LUA_H #include #include #include LUALIB_API int luaopen_ptp(lua_State *L); lua_State *cam_new_task(int *id); int lua_script_run_loop(int id); int cam_run_lua_script(const char *buffer); int cam_run_lua_script_async(const char *buffer); const char *cam_lua_get_error(); #endif \ No newline at end of file diff --git a/src/cl_backend.h b/src/cl_backend.h index bdfa844..3b6ff3c 100644 --- a/src/cl_backend.h +++ b/src/cl_backend.h @@ -46,7 +46,7 @@ int ptp_send_bulk_packets(struct PtpRuntime *r, int length); int ptp_receive_bulk_packets(struct PtpRuntime *r); int ptp_read_int(struct PtpRuntime *r, void *to, int length); -int ptp_device_close(struct PtpRuntime *r); +int ptp_device_close(struct PtpRuntime *r); // TODO: Disconnect, confusing with ptp_close // Upload file data as packets, but upload r->data till length first int ptp_fsend_packets(struct PtpRuntime *r, int length, FILE *stream); @@ -62,6 +62,6 @@ int ptpip_connect_events(struct PtpRuntime *r, char *addr, int port); int ptpip_event_send(struct PtpRuntime *r, void *data, int size); int ptpip_event_read(struct PtpRuntime *r, void *data, int size); -int ptpip_close(struct PtpRuntime *r); +int ptpip_close(struct PtpRuntime *r); // TODO: Disconnect, confusing with ptp_close #endif diff --git a/src/transport.c b/src/transport.c index 109fcc9..de3b34c 100644 --- a/src/transport.c +++ b/src/transport.c @@ -157,51 +157,60 @@ int ptpip_write_packet(struct PtpRuntime *r, int of) { int ptpusb_read_all_packets(struct PtpRuntime *r) { int read = 0; - while (1) { - int rc = 0; - while (rc <= 0 && r->wait_for_response) { - rc = ptp_cmd_read(r, r->data + read, r->max_packet_size); + // Try and get the first 512 bytes + int rc = 0; + while (rc <= 0 && r->wait_for_response) { + rc = ptp_cmd_read(r, r->data + read, r->max_packet_size); - r->wait_for_response--; + r->wait_for_response--; - if (rc > 0) break; + if (rc < 0) break; - if (r->wait_for_response) { - ptp_verbose_log("Trying again..."); - CAMLIB_SLEEP(CAMLIB_WAIT_MS); - } + if (r->wait_for_response) { + ptp_verbose_log("Trying again..."); + CAMLIB_SLEEP(CAMLIB_WAIT_MS); } - r->wait_for_response = r->response_wait_default; + } + r->wait_for_response = r->response_wait_default; - if (rc < 0) { - ptp_verbose_log("Failed to read packets: %d\n", rc); - return PTP_IO_ERR; - } + if (rc < 0) { + ptp_verbose_log("Failed to read packets: %d\n", rc); + return PTP_IO_ERR; + } - read += rc; + read += rc; - // TODO: unsigned/unsigned compare - if (read >= r->data_length - r->max_packet_size) { - rc = ptp_buffer_resize(r, read + r->max_packet_size); - if (rc) return rc; - } + if (read < 12) { + ptp_verbose_log("Couldn't get basic packet: %d\n", rc); + return PTP_IO_ERR; + } - if (rc != r->max_packet_size) { - ptp_verbose_log("receive_bulk_packets: Read %d bytes\n", read); - struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data); + struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data); - // Read the response packet if only a data packet was sent (as per spec, always is 12 bytes) - if (c->type == PTP_PACKET_TYPE_DATA) { - rc = ptp_cmd_read(r, r->data + read, r->max_packet_size); - ptp_verbose_log("receive_bulk_packets: Received response packet: %d\n", rc); - read += rc; - } + // 512 is always enough for the response packet + if (c->type == PTP_PACKET_TYPE_RESPONSE) { + return 0; + } - ptp_verbose_log("receive_bulk_packets: Return code: 0x%X\n", ptp_get_return_code(r)); + // Reallocate enough to fill the rest of data and response packet + if (c->type == PTP_PACKET_TYPE_DATA && r->data_length < (c->length + r->max_packet_size)) { + rc = ptp_buffer_resize(r, c->length + r->max_packet_size); + c = (struct PtpBulkContainer *)(r->data); + if (rc) return rc; + } - return read; - } + while (read < c->length) { + rc = ptp_cmd_read(r, r->data + read, r->max_packet_size); + if (rc < 0) return PTP_IO_ERR; + read += rc; } + + ptp_verbose_log("Read %d bytes\n", read); + + rc = ptp_cmd_read(r, r->data + read, r->max_packet_size); + if (rc < 0) return PTP_IO_ERR; + + return 0; } // For USB packets over IP, we can't do any LibUSB/LibWPD performance tricks. reads will just time out.