-/* picoproj - communication with AM7xxx based USB pico projectors
+/* picoproj - test program for libam7xxx
*
* Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
*
#include <stdio.h>
#include <stdlib.h>
-#include <stdint.h>
#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>
-/*
- * Examples of a packet headers:
- *
- * Image widget:
- * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- * +02|00|00|00|00|10|3e|10|01|00|00|00|20|03|00|00|e0|01|00|00|53|E8|00|00+
- * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- *
- * Brightness widget:
- * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- * +04|00|00|00|00|0c|ff|ff|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00+
- * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- */
-
-#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;
-};
-
-struct header{
- uint32_t packet_type;
- uint32_t unknown1;
- uint32_t unknown2;
- uint32_t width;
- uint32_t height;
- uint32_t payload_size;
-};
-
-struct packet {
- struct header header;
- uint8_t payload[];
-};
-
-static struct buffer *packet_allocate_buffer(struct packet *p)
-{
- struct buffer *buffer;
-
- if (p == NULL) {
- perror("packet NULL");
- return NULL;
- }
-
- buffer = malloc(sizeof(*buffer));
- if (buffer == NULL) {
- perror("malloc buffer");
- return NULL;
- }
-
- buffer->len = sizeof(p->header); /* + p->header.payload_size; */
-
- buffer->data = malloc(buffer->len);
- if (buffer->data == NULL) {
- perror("malloc buffer->data");
- free(buffer);
- return NULL;
- }
- return buffer;
-}
-
-static void packet_free_buffer(struct buffer *buffer)
-{
- free(buffer->data);
- free(buffer);
- buffer = NULL;
-}
-
-static int packet_pack(struct packet *p, struct buffer *buffer)
-{
- 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);
-
- tmp = htole32(p->header.payload_size);
- memcpy(buffer->data + offset, &tmp, sizeof(p->header.payload_size));
- offset += sizeof(p->header.payload_size);
+#include "am7xxx.h"
- /* TODO memcpy payload of size */
-
- return 0;
-}
-
-static int packet_unpack(struct buffer *buffer, struct packet *p)
+static void usage(char *name)
{
- 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;
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.packet_type));
- p->header.packet_type = le32toh(tmp);
- offset += sizeof(p->header.packet_type);
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.unknown1));
- p->header.unknown1 = le32toh(tmp);
- offset += sizeof(p->header.unknown1);
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.unknown2));
- p->header.unknown2 = le32toh(tmp);
- offset += sizeof(p->header.unknown2);
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.width));
- p->header.width = le32toh(tmp);
- offset += sizeof(p->header.width);
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.height));
- p->header.height = le32toh(tmp);
- offset += sizeof(p->header.height);
-
- memcpy(&tmp, buffer->data + offset, sizeof(p->header.payload_size));
- p->header.payload_size = le32toh(tmp);
- offset += sizeof(p->header.payload_size);
-
- /* malloc & memcpy payload of size p->header.payload_size */
-
- return 0;
+ 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_header(struct packet *p)
+int main(int argc, char *argv[])
{
- 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);
-}
-
-static void packet_dump_buffer(struct buffer *buffer)
-{
- 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;
+ am7xxx_device dev;
+ int format = AM7XXX_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);
+ 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;
+ }
+ }
- buffer = packet_allocate_buffer(&p1);
- if (buffer == NULL) {
- fprintf(stderr, "Cannot allocate the buffer.\n");
- exit(EXIT_FAILURE);
+ dev = am7xxx_init();
+ if (dev == NULL) {
+ perror("am7xxx_init");
+ exit_code = EXIT_FAILURE;
+ goto out_munmap;
}
- ret = packet_pack(&p1, buffer);
+ ret = am7xxx_send_image(dev, format, width, height, image, size);
if (ret < 0) {
- fprintf(stderr, "Cannot pack the packet.\n");
- exit(EXIT_FAILURE);
+ perror("am7xxx_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:
+ am7xxx_shutdown(dev);
- packet_dump_header(&p2);
+out_munmap:
+ if (image != NULL) {
+ ret = munmap(image, size);
+ if (ret < 0)
+ perror("munmap");
+ }
- 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);
}