Serialize struct am7xxx_header properly before sending it on the wire
[libam7xxx.git] / src / picoproj.c
1 /* picoproj - test program for libam7xxx
2  *
3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27
28 #include "am7xxx.h"
29
30 static void usage(char *name)
31 {
32         printf("usage: %s [OPTIONS]\n\n", name);
33         printf("OPTIONS:\n");
34         printf("\t-f <filename>\t\tthe image file to upload\n");
35         printf("\t-F <format>\t\tthe image format to use (default is JPEG).\n");
36         printf("\t\t\t\tSUPPORTED FORMATS:\n");
37         printf("\t\t\t\t\t1 - JPEG\n");
38         printf("\t\t\t\t\t2 - YUV - NV12\n");
39         printf("\t-W <image width>\tthe width of the image to upload\n");
40         printf("\t-H <image height>\tthe height of the image to upload\n");
41         printf("\t-h \t\t\tthis help message\n");
42 }
43
44 int main(int argc, char *argv[])
45 {
46         int ret;
47         int exit_code = EXIT_SUCCESS;
48         int opt;
49
50         char filename[FILENAME_MAX] = {0};
51         int image_fd = -1;
52         am7xxx_device dev;
53         int format = AM7XXX_IMAGE_FORMAT_JPEG;
54         int width = 800;
55         int height = 480;
56         uint8_t *image = NULL;
57         unsigned int size = 59475;
58
59         while ((opt = getopt(argc, argv, "f:F:W:H:h")) != -1) {
60                 switch (opt) {
61                 case 'f':
62                         strncpy(filename, optarg, FILENAME_MAX);
63                         break;
64                 case 'F':
65                         format = atoi(optarg);
66                         switch(format) {
67                         case AM7XXX_IMAGE_FORMAT_JPEG:
68                                 fprintf(stdout, "JPEG format\n");
69                                 break;
70                         case AM7XXX_IMAGE_FORMAT_NV12:
71                                 fprintf(stdout, "NV12 format\n");
72                                 break;
73                         default:
74                                 fprintf(stderr, "Unsupported format\n");
75                                 exit(EXIT_FAILURE);
76                         }
77                         break;
78                 case 'W':
79                         width = atoi(optarg);
80                         if (width < 0) {
81                                 fprintf(stderr, "Unsupported width\n");
82                                 exit(EXIT_FAILURE);
83                         }
84                         break;
85                 case 'H':
86                         height = atoi(optarg);
87                         if (height < 0) {
88                                 fprintf(stderr, "Unsupported height\n");
89                                 exit(EXIT_FAILURE);
90                         }
91                         break;
92                 case 'h':
93                         usage(argv[0]);
94                         exit(EXIT_SUCCESS);
95                         break;
96                 default: /* '?' */
97                         usage(argv[0]);
98                         exit(EXIT_FAILURE);
99                 }
100         }
101
102         if (filename[0] != '\0') {
103                 struct stat st;
104                 
105                 image_fd = open(filename, O_RDONLY);
106                 if (image_fd < 0) {
107                         perror("open");
108                         exit_code = EXIT_FAILURE;
109                         goto out;
110                 }
111                 if (fstat(image_fd, &st) < 0) {
112                         perror("fstat");
113                         exit_code = EXIT_FAILURE;
114                         goto out_close_image_fd;
115                 }
116                 size = st.st_size;
117
118                 image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
119                 if (image == NULL) {
120                         perror("mmap");
121                         exit_code = EXIT_FAILURE;
122                         goto out_close_image_fd;
123                 }
124         }
125
126         dev = am7xxx_init();
127         if (dev == NULL) {
128                 perror("am7xxx_init");
129                 exit_code = EXIT_FAILURE;
130                 goto out_munmap;
131         }
132
133         ret = am7xxx_send_image(dev, format, width, height, image, size);
134         if (ret < 0) {
135                 perror("am7xxx_send_image");
136                 exit_code = EXIT_FAILURE;
137                 goto cleanup;
138         }
139
140         exit_code = EXIT_SUCCESS;
141
142 cleanup:
143         am7xxx_shutdown(dev);
144
145 out_munmap:
146         if (image != NULL) {
147                 ret = munmap(image, size);
148                 if (ret < 0)
149                         perror("munmap");
150         }
151
152 out_close_image_fd:
153         if (image_fd >= 0) {
154                 ret = close(image_fd);
155                 if (ret < 0)
156                         perror("close");
157         }
158
159 out:
160         exit(exit_code);
161 }