+/* If we're not using GNU C, elide __attribute__
+ * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
+ */
+#ifndef __GNUC__
+# define __attribute__(x) /*NOTHING*/
+#endif
+
+/* Control shared library symbols visibility */
+#if defined _WIN32 || defined __CYGWIN__
+ #define AM7XXX_PUBLIC __declspec(dllexport)
+ #define AM7XXX_LOCAL
+#else
+ #if __GNUC__ >= 4
+ #define AM7XXX_PUBLIC __attribute__ ((visibility ("default")))
+ #define AM7XXX_LOCAL __attribute__ ((visibility ("hidden")))
+ #else
+ #define AM7XXX_PUBLIC
+ #define AM7XXX_LOCAL
+ #endif
+#endif
+
+static void log_message(am7xxx_context *ctx,
+ int level,
+ const char *function,
+ int line,
+ const char *fmt,
+ ...) __attribute__ ((format (printf, 5, 6)));
+
+#define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__)
+#define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__)
+#define warning(ctx, ...) log_message(ctx, AM7XXX_LOG_WARNING, __func__, 0, __VA_ARGS__)
+#define info(ctx, ...) log_message(ctx, AM7XXX_LOG_INFO, __func__, 0, __VA_ARGS__)
+#define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__)
+#define trace(ctx, ...) log_message(ctx, AM7XXX_LOG_TRACE, NULL, 0, __VA_ARGS__)
+
+struct am7xxx_usb_device_descriptor {
+ const char *name;
+ uint16_t vendor_id;
+ uint16_t product_id;
+};
+
+static struct am7xxx_usb_device_descriptor supported_devices[] = {
+ {
+ .name = "Acer C110",
+ .vendor_id = 0x1de1,
+ .product_id = 0xc101,
+ },
+ {
+ .name = "Philips/Sagemcom PicoPix 1020",
+ .vendor_id = 0x21e7,
+ .product_id = 0x000e,
+ },
+};
+
+/* The header size on the wire is known to be always 24 bytes, regardless of
+ * the memory configuration enforced by different architectures or compilers
+ * for struct am7xxx_header
+ */
+#define AM7XXX_HEADER_WIRE_SIZE 24
+
+struct _am7xxx_device {
+ libusb_device_handle *usb_device;
+ uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
+ am7xxx_context *ctx;
+ am7xxx_device *next;
+};
+
+struct _am7xxx_context {
+ libusb_context *usb_context;
+ int log_level;
+ am7xxx_device *devices_list;
+};
+
+typedef enum {
+ AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
+ AM7XXX_PACKET_TYPE_IMAGE = 0x02,
+ AM7XXX_PACKET_TYPE_POWER = 0x04,
+ AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
+} am7xxx_packet_type;
+
+struct am7xxx_generic_header {
+ uint32_t field0;
+ uint32_t field1;
+ uint32_t field2;
+ uint32_t field3;
+};
+
+struct am7xxx_devinfo_header {
+ uint32_t native_width;
+ uint32_t native_height;
+ uint32_t unknown0;
+ uint32_t unknown1;
+};
+
+struct am7xxx_image_header {
+ uint32_t format;
+ uint32_t width;
+ uint32_t height;
+ uint32_t image_size;
+};
+
+struct am7xxx_power_header {
+ uint32_t bit2;
+ uint32_t bit1;
+ uint32_t bit0;
+};
+
+/*
+ * Examples of packet headers:
+ *
+ * Image header:
+ * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
+ *
+ * Power header:
+ * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+ */
+
+struct am7xxx_header {
+ uint32_t packet_type;
+ uint8_t unknown0;
+ uint8_t header_data_len;
+ uint8_t unknown2;
+ uint8_t unknown3;
+ union {
+ struct am7xxx_generic_header data;
+ struct am7xxx_devinfo_header devinfo;
+ struct am7xxx_image_header image;
+ struct am7xxx_power_header power;
+ } header_data;
+};
+
+
+#ifdef DEBUG
+static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
+{
+ if (ctx == NULL || d == NULL)
+ return;
+
+ debug(ctx, "Info header:\n");
+ debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
+ debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
+ debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
+ debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
+}
+
+static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)