Add initial support for USB output communication
[libam7xxx.git] / picoproj.c
index 2629d02..a5e5075 100644 (file)
 #include <string.h>
 #include <endian.h>
 #include <errno.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <libusb.h>
+
+#define AM7x01_VENDOR_ID  0x1de1
+#define AM7x01_PRODUCT_ID 0xc101
+
+static libusb_device_handle *dev;
 
 typedef enum {
        AM7x01_PACKET_TYPE_INIT    = 0x01,
@@ -129,6 +141,11 @@ static void dump_header(struct header *h)
        fflush(stdout);
 }
 
+static inline unsigned int in_80chars(unsigned int i)
+{
+       return ((i+1) % (80/3));
+}
+
 static void dump_buffer(uint8_t *buffer, unsigned int len)
 {
        unsigned int i;
@@ -137,14 +154,25 @@ static void dump_buffer(uint8_t *buffer, unsigned int len)
                return;
 
        for (i = 0; i < len; i++) {
-               printf("%02hhX%c", buffer[i], (i < len - 1) ? ' ' : '\n');
+               printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
        }
        fflush(stdout);
 }
 
 static int send_data(uint8_t *buffer, unsigned int len)
 {
+       int ret;
+       int transferred;
+
        dump_buffer(buffer, len);
+
+       ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
+       if (ret != 0 || (unsigned int)transferred != len) {
+               fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
+                       ret, transferred, len);
+               return ret;
+       }
+
        return 0;
 }
 
@@ -199,15 +227,133 @@ static int send_image(am7x01_image_format format,
        return send_data(image, size);
 }
 
+static void usage(char *name)
+{
+       printf("usage: %s [OPTIONS]\n\n", name);
+       printf("OPTIONS:\n");
+       printf("\t-f <filename>\t\tthe image file to upload\n");
+       printf("\t-F <format>\t\tthe image format to use (default is JPEG).\n");
+       printf("\t\t\t\tSUPPORTED FORMATS:\n");
+       printf("\t\t\t\t\t1 - JPEG\n");
+       printf("\t-W <image width>\tthe width of the image to upload\n");
+       printf("\t-H <image height>\tthe height of the image to upload\n");
+       printf("\t-h \t\t\tthis help message\n");
+}
 
 int main(int argc, char *argv[])
 {
        int ret;
+       int exit_code = EXIT_SUCCESS;
+       int opt;
+
+       char filename[FILENAME_MAX] = {0};
+       int image_fd = -1;
+       int format = AM7x01_IMAGE_FORMAT_JPEG;
+       int width = 800;
+       int height = 480;
+       uint8_t *image = NULL;
+       unsigned int size = 59475;
+
+       while ((opt = getopt(argc, argv, "f:F:W:H:h")) != -1) {
+               switch (opt) {
+               case 'f':
+                       strncpy(filename, optarg, FILENAME_MAX);
+                       break;
+               case 'F':
+                       format = atoi(optarg);
+                       if (format != 1) {
+                               fprintf(stderr, "Unsupported format\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               case 'W':
+                       width = atoi(optarg);
+                       if (width < 0) {
+                               fprintf(stderr, "Unsupported width\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               case 'H':
+                       height = atoi(optarg);
+                       if (height < 0) {
+                               fprintf(stderr, "Unsupported height\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               default: /* '?' */
+                       usage(argv[0]);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       if (filename[0] != '\0') {
+               struct stat st;
+               
+               image_fd = open(filename, O_RDONLY);
+               if (image_fd < 0) {
+                       perror("open");
+                       exit_code = EXIT_FAILURE;
+                       goto out;
+               }
+               if (fstat(image_fd, &st) < 0) {
+                       perror("fstat");
+                       exit_code = EXIT_FAILURE;
+                       goto out_close_image_fd;
+               }
+               size = st.st_size;
+
+               image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
+               if (image == NULL) {
+                       perror("mmap");
+                       exit_code = EXIT_FAILURE;
+                       goto out_close_image_fd;
+               }
+       }
 
-       ret = send_image(AM7x01_IMAGE_FORMAT_JPEG, 800, 480, 59475);
+       libusb_init(NULL);
+       libusb_set_debug(NULL, 3);
+
+       dev = libusb_open_device_with_vid_pid(NULL,
+                                             AM7x01_VENDOR_ID,
+                                             AM7x01_PRODUCT_ID);
+       if (dev == NULL) {
+               errno = ENODEV;
+               perror("libusb_open_device_with_vid_pid");
+               exit_code = EXIT_FAILURE;
+               goto out_libusb_exit;
+       }
+
+       libusb_set_configuration(dev, 1);
+       libusb_claim_interface(dev, 0);
+
+       ret = send_image(format, width, height, image, size);
        if (ret < 0) {
                perror("send_image");
-               exit(EXIT_FAILURE);
+               exit_code = EXIT_FAILURE;
+               goto cleanup;
+       }
+
+       exit_code = EXIT_SUCCESS;
+
+cleanup:
+       libusb_close(dev);
+
+out_libusb_exit:
+       libusb_exit(NULL);
+
+       if (image != NULL) {
+               ret = munmap(image, size);
+               if (ret < 0)
+                       perror("munmap");
        }
-       exit(EXIT_SUCCESS);
+
+out_close_image_fd:
+       if (image_fd >= 0) {
+               ret = close(image_fd);
+               if (ret < 0)
+                       perror("close");
+       }
+
+out:
+       exit(exit_code);
 }