Add support for sending an actual JPEG image
[libam7xxx.git] / picoproj.c
index 9bec914..161be49 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>
+
+typedef enum {
+       AM7x01_PACKET_TYPE_INIT    = 0x01,
+       AM7x01_PACKET_TYPE_IMAGE   = 0x02,
+       AM7x01_PACKET_TYPE_POWER   = 0x04,
+       AM7x01_PACKET_TYPE_UNKNOWN = 0x05,
+} am7x01_packet_type;
+
+typedef enum {
+       AM7x01_IMAGE_FORMAT_JPEG = 1,
+} am7x01_image_format;
+
+typedef enum {
+       AM7x01_POWER_OFF  = 0,
+       AM7x01_POWER_LOW  = 1,
+       AM7x01_POWER_MID  = 2,
+       AM7x01_POWER_HIGH = 3,
+} am7x01_power_mode;
+
+struct image_header {
+       uint32_t format;
+       uint32_t width;
+       uint32_t height;
+       uint32_t image_size;
+};
+
+struct power_header {
+       uint32_t power_low;
+       uint32_t power_mid;
+       uint32_t power_high;
+};
 
 /*
  * Examples of packet headers:
  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  */
 
-#define AM7x01_PACKET_TYPE_INIT           0x01
-#define AM7x01_PACKET_TYPE_IMAGE   0x02
-#define AM7x01_PACKET_TYPE_POWER   0x04
-#define AM7x01_PACKET_TYPE_UNKNOWN 0x05
-
-struct buffer {
-       unsigned int len;
-       uint8_t *data;
+static uint8_t reference_image_header[] = {
+       0x02, 0x00, 0x00, 0x00,
+       0x00,
+       0x10,
+       0x3e,
+       0x10,
+       0x01, 0x00, 0x00, 0x00,
+       0x20, 0x03, 0x00, 0x00,
+       0xe0, 0x01, 0x00, 0x00,
+       0x53, 0xE8, 0x00, 0x00
 };
 
-struct header{
+struct header {
        uint32_t packet_type;
-       uint32_t unknown1;
-       uint32_t unknown2;
-       uint32_t width;
-       uint32_t height;
-       uint32_t payload_size;
+       uint8_t unknown0;
+       uint8_t header_len;
+       uint8_t unknown2;
+       uint8_t unknown3;
+       union {
+               struct image_header image;
+               struct power_header power;
+       } header_data;
 };
 
-struct packet {
-       struct header header;
-       uint8_t payload[];
-};
 
-static struct buffer *packet_allocate_buffer(struct packet *p)
+static void dump_image_header(struct image_header *i)
 {
-       struct buffer *buffer;
+       if (i == NULL)
+               return;
 
-       if (p == NULL) {
-               perror("packet NULL");
-               return NULL;
-       }
+       printf("Image header:\n");
+       printf("format:      0x%08x (%u)\n", i->format, i->format);
+       printf("width:       0x%08x (%u)\n", i->width, i->width);
+       printf("height:      0x%08x (%u)\n", i->height, i->height);
+       printf("image size:  0x%08x (%u)\n", i->image_size, i->image_size);
+}
 
-       buffer = malloc(sizeof(*buffer));
-       if (buffer == NULL) {
-               perror("malloc buffer");
-               return NULL;
-       }
+static void dump_header(struct header *h)
+{
+       if (h == NULL)
+               return;
+
+       printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
+       printf("unknown0:    0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
+       printf("header_len:  0x%02hhx (%hhu)\n", h->header_len, h->header_len);
+       printf("unknown2:    0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
+       printf("unknown3:    0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
 
-       buffer->len = sizeof(p->header); /* + p->header.payload_size; */
+       switch(h->packet_type) {
+       case AM7x01_PACKET_TYPE_IMAGE:
+               dump_image_header(&(h->header_data.image));
+               break;
 
-       buffer->data = malloc(buffer->len);
-       if (buffer->data == NULL) {
-               perror("malloc buffer->data");
-               free(buffer);
-               return NULL;
+       default:
+               printf("Packet type not supported!\n");
+               break;
        }
-       return buffer;
+
+       fflush(stdout);
 }
 
-static void packet_free_buffer(struct buffer *buffer)
+static inline unsigned int in_80chars(unsigned int i)
 {
-       free(buffer->data);
-       free(buffer);
-       buffer = NULL;
+       return ((i+1) % (80/3));
 }
 
-static int packet_pack(struct packet *p, struct buffer *buffer)
+static void dump_buffer(uint8_t *buffer, unsigned int len)
 {
-       unsigned int offset;
-       uint32_t tmp;
-
-       if (p == NULL || buffer == NULL)
-               return -EINVAL;
-
-       /* TODO: check for packet payload being NULL? */
-       if (buffer->data == NULL || buffer->len < sizeof(*p))
-               return -EINVAL;
-
-       offset = 0;
-
-       tmp = htole32(p->header.packet_type);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.packet_type));
-       offset += sizeof(p->header.packet_type);
-
-       tmp = htole32(p->header.unknown1);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.unknown1));
-       offset += sizeof(p->header.unknown1);
-
-       tmp = htole32(p->header.unknown2);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.unknown2));
-       offset += sizeof(p->header.unknown2);
-
-       tmp = htole32(p->header.width);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.width));
-       offset += sizeof(p->header.width);
-
-       tmp = htole32(p->header.height);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.height));
-       offset += sizeof(p->header.height);
+       unsigned int i;
 
-       tmp = htole32(p->header.payload_size);
-       memcpy(buffer->data + offset, &tmp, sizeof(p->header.payload_size));
-       offset += sizeof(p->header.payload_size);
+       if (buffer == NULL || len == 0)
+               return;
 
-       /* TODO memcpy payload of size */
+       for (i = 0; i < len; i++) {
+               printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
+       }
+       fflush(stdout);
+}
 
+static int send_data(uint8_t *buffer, unsigned int len)
+{
+       dump_buffer(buffer, len);
        return 0;
 }
 
-static int packet_unpack(struct buffer *buffer, struct packet *p)
+static int send_header(struct header *h)
 {
-       unsigned int offset;
-       uint32_t tmp;
-
-       if (p == NULL || buffer == NULL)
-               return -EINVAL;
-
-       /* TODO: check for packet payload being NULL? */
-       if (buffer->data == NULL || buffer->len < sizeof(*p))
-               return -EINVAL;
-
-       offset = 0;
+       union {
+               struct header header;
+               uint8_t buffer[sizeof (struct header)];
+       } data;
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.packet_type));
-       p->header.packet_type = le32toh(tmp);
-       offset += sizeof(p->header.packet_type);
+       data.header = *h;
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.unknown1));
-       p->header.unknown1 = le32toh(tmp);
-       offset += sizeof(p->header.unknown1);
+       return send_data(data.buffer, sizeof (struct header));
+}
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.unknown2));
-       p->header.unknown2 = le32toh(tmp);
-       offset += sizeof(p->header.unknown2);
+static int send_image(am7x01_image_format format,
+                     unsigned int width,
+                     unsigned int height,
+                     uint8_t *image,
+                     unsigned int size)
+{
+       int ret;
+       struct header h = {
+               .packet_type = htole32(AM7x01_PACKET_TYPE_IMAGE),
+               .unknown0    = 0x00,
+               .header_len  = sizeof(struct image_header),
+               .unknown2    = 0x3e,
+               .unknown3    = 0x10,
+               .header_data = {
+                       .image = {
+                               .format     = htole32(format),
+                               .width      = htole32(width),
+                               .height     = htole32(height),
+                               .image_size = htole32(size),
+                       },
+               },
+       };
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.width));
-       p->header.width = le32toh(tmp);
-       offset += sizeof(p->header.width);
+       dump_header(&h);
+       printf("\n");
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.height));
-       p->header.height = le32toh(tmp);
-       offset += sizeof(p->header.height);
+       printf("Dump Buffers\n");
+       dump_buffer(reference_image_header, sizeof(struct header));
 
-       memcpy(&tmp, buffer->data + offset, sizeof(p->header.payload_size));
-       p->header.payload_size = le32toh(tmp);
-       offset += sizeof(p->header.payload_size);
+       ret = send_header(&h);
+       if (ret < 0)
+               return ret;
 
-       /* malloc & memcpy payload of size p->header.payload_size */
+       if (image == NULL || size == 0)
+               return 0;
 
-       return 0;
+       return send_data(image, size);
 }
 
-static void packet_dump_header(struct packet *p)
+static void usage(char *name)
 {
-       if (p == NULL)
-               return;
-
-       printf("packet_type: 0x%08x (%u)\n", p->header.packet_type, p->header.packet_type);
-       printf("unknown1:    0x%08x (%u)\n", p->header.unknown1, p->header.unknown1);
-       printf("unknown2:    0x%08x (%u)\n", p->header.unknown2, p->header.unknown2);
-       printf("width:       0x%08x (%u)\n", p->header.width, p->header.width);
-       printf("height:      0x%08x (%u)\n", p->header.height, p->header.height);
-       printf("size:        0x%08x (%u)\n", p->header.payload_size, p->header.payload_size);
-       fflush(stdout);
+       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");
 }
 
-static void packet_dump_buffer(struct buffer *buffer)
+int main(int argc, char *argv[])
 {
-       unsigned int i;
-
-       if (buffer == NULL)
-               return;
-
-       if (buffer->data == NULL)
-               return;
-
-       for (i = 0; i < buffer->len; i++) {
-               printf("%02hhX%c", buffer->data[i], (i < buffer->len - 1) ? ' ' : '\n');
-       }
-       fflush(stdout);
-}
-
-int main(void)
-{
-       struct packet p1  = {
-               .header = {
-                       .packet_type  = AM7x01_PACKET_TYPE_IMAGE,
-                       .unknown1     = le32toh(0x103e1000),
-                       .unknown2     = le32toh(0x00000001),
-                       .width        = 800,
-                       .height       = 480,
-                       .payload_size = 59475,
-               },
-               /* TODO initialize payload */
-       };
-       struct buffer *buffer = NULL;
-       struct packet p2;
        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);
+               }
+       }
 
-       packet_dump_header(&p1);
-
-       buffer = packet_allocate_buffer(&p1);
-       if (buffer == NULL) {
-               fprintf(stderr, "Cannot allocate the buffer.\n");
-               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 = packet_pack(&p1, buffer);
+       ret = send_image(format, width, height, image, size);
        if (ret < 0) {
-               fprintf(stderr, "Cannot pack the packet.\n");
-               exit(EXIT_FAILURE);
+               perror("send_image");
+               exit_code = EXIT_FAILURE;
+               goto cleanup;
        }
 
-       packet_dump_buffer(buffer);
+       exit_code = EXIT_SUCCESS;
 
-       ret = packet_unpack(buffer, &p2);
-       if (ret < 0) {
-               fprintf(stderr, "Cannot unpack the packet.\n");
-               exit(EXIT_FAILURE);
+cleanup:
+       if (image != NULL) {
+               ret = munmap(image, size);
+               if (ret < 0)
+                       perror("munmap");
        }
 
-       packet_dump_header(&p2);
-
-       packet_free_buffer(buffer);
+out_close_image_fd:
+       if (image_fd >= 0) {
+               ret = close(image_fd);
+               if (ret < 0)
+                       perror("close");
+       }
 
-       exit(EXIT_SUCCESS);
+out:
+       exit(exit_code);
 }