1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "gx_validation_strings.h"
5 #include "gx_validation_verify.h"
6 
7 #ifdef __linux__
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <errno.h>
11 #endif
12 
13 extern void gx_validation_print_test_result(int result);
14 
15 static char line[256];
16 static FILE *golden_file = NULL;
17 static int   golden_file_frame_size = 0;
18 static int   golden_file_total_frames = 0;
19 static int   num_mismatch = 0;
20 
21 #ifdef __linux__
22 static pthread_t frame_read_thread;
23 static sem_t     frame_read_semaphore;
24 static sem_t     frame_verify_semaphore;
25 #endif
26 
27 #define MAX_NUM_FRAMES 3
28 
29 typedef struct frame_info_struct{
30     char                     *frame_buffer;
31     int                       frame_id;
32     struct frame_info_struct *next;
33 }frame_info;
34 
35 static frame_info  frame_list[MAX_NUM_FRAMES];
36 static frame_info *frame_list_head = NULL;
37 static frame_info *frame_list_tail = NULL;
38 static int         frame_list_read_count = 0;
39 
read_line(FILE * file)40 static int read_line(FILE *file)
41 {
42     char ch;
43     int count = 0;
44 
45     memset(line, 0, sizeof(line));
46 
47     ch = getc(file);
48 
49     while((ch != '\n') && (ch != EOF))
50     {
51         if(count == sizeof(line))
52             count = 0;
53         line[count++] = ch;
54 
55         ch = getc(file);
56     }
57 
58     if(ch == EOF)
59     {
60         return(-1);
61     }
62 
63     line[count] = 0;
64 
65     return(count);
66 
67 }
68 
get_frame_id(FILE * pFile,int * frame_id)69 void get_frame_id(FILE *pFile, int *frame_id)
70 {
71     *frame_id = -1;
72 
73     while(*frame_id == -1)
74     {
75         read_line(pFile);
76 
77         if(strncmp(line, FRAME_ID, strlen(FRAME_ID)) == 0)
78             *frame_id = atoi(&line[strlen(FRAME_ID)]);
79 
80     }
81     return;
82 
83 
84 }
read_file_header(FILE * pFile,int * file_format,int * total_frame,char ** color_format,int * bits_per_pixel,int * width,int * height)85 int read_file_header(FILE *pFile, int *file_format, int *total_frame, char **color_format, int *bits_per_pixel, int *width, int *height)
86 {
87 int len;
88 
89     *file_format = *total_frame = *bits_per_pixel = *width = *height = 0;
90     while(!(*total_frame) || !(*bits_per_pixel) || !(*width) || !(*height))
91     {
92         len = read_line(pFile);
93 
94         if(len < 0)
95         {
96             *total_frame = 0;
97             return (-1);
98         }
99 
100         if(strncmp(line, FILE_FORMAT, strlen(FILE_FORMAT)) == 0)
101             *file_format = atoi(&line[strlen(FILE_FORMAT)]);
102 
103         else if(strncmp(line, TOTAL_FRAMES, strlen(TOTAL_FRAMES)) == 0)
104             *total_frame = atoi(&line[strlen(TOTAL_FRAMES)]);
105 
106         else if(strncmp(line, COLOR_DEPTH, strlen(COLOR_DEPTH)) == 0)
107         {
108 
109             if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_24XRGB, strlen(line) - strlen(COLOR_DEPTH)) == 0)
110             {
111                 if (color_format)
112                 {
113                     *color_format = COLOR_FORMAT_24XRGB;
114                 }
115                 *bits_per_pixel = 32;
116             }
117             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_565RGB, strlen(line) - strlen(COLOR_DEPTH)) == 0)
118             {
119                 if (color_format)
120                 {
121                     *color_format = COLOR_FORMAT_565RGB;
122                 }
123                 *bits_per_pixel = 16;
124             }
125             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_565BGR, strlen(line) - strlen(COLOR_DEPTH)) == 0)
126             {
127                 if (color_format)
128                 {
129                     *color_format = COLOR_FORMAT_565BGR;
130                 }
131                 *bits_per_pixel = 16;
132             }
133             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_1555XRGB, strlen(line) - strlen(COLOR_DEPTH)) == 0)
134             {
135                 if (color_format)
136                 {
137                     *color_format = COLOR_FORMAT_1555XRGB;
138                 }
139                 *bits_per_pixel = 16;
140             }
141             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_4444ARGB, strlen(line) - strlen(COLOR_DEPTH)) == 0)
142             {
143                 if (color_format)
144                 {
145                     *color_format = COLOR_FORMAT_4444ARGB;
146                 }
147                 *bits_per_pixel = 16;
148             }
149             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_8BPP, strlen(line) - strlen(COLOR_DEPTH)) == 0)
150             {
151                 if (color_format)
152                 {
153                     *color_format = COLOR_FORMAT_8BPP;
154                 }
155                 *bits_per_pixel = 8;
156             }
157             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_332RGB, strlen(line) - strlen(COLOR_DEPTH)) == 0)
158             {
159                 if(color_format)
160                 {
161                     *color_format = COLOR_FORMAT_332RGB;
162                 }
163                 *bits_per_pixel = 8;
164             }
165             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_4BPP, strlen(line) - strlen(COLOR_DEPTH)) == 0)
166             {
167                 if (color_format)
168                 {
169                      *color_format = COLOR_FORMAT_4BPP;
170                 }
171                 *bits_per_pixel = 4;
172             }
173             else if (strncmp(line + strlen(COLOR_DEPTH), COLOR_FORMAT_1BPP, strlen(line) - strlen(COLOR_DEPTH)) == 0)
174             {
175                 if (color_format)
176                 {
177                     *color_format = COLOR_FORMAT_1BPP;
178                 }
179                 *bits_per_pixel = 1;
180             }
181         }
182 
183         else if(strncmp(line, WIDTH, strlen(WIDTH)) == 0)
184             *width = atoi(&line[strlen(WIDTH)]);
185 
186         else if(strncmp(line, HEIGHT, strlen(HEIGHT)) == 0)
187             *height = atoi(&line[strlen(HEIGHT)]);
188 
189     }
190 
191     if(*file_format > FILE_FORMAT_VERSION_CURRENT)
192     {
193         printf("Incorrect file format: %d, current version is %d\n", *file_format, FILE_FORMAT_VERSION_CURRENT);
194         return(-1);
195     }
196 
197     return(0);
198 }
199 
get_frame_comment(FILE * fp,char * buffer,int buf_size)200 void get_frame_comment(FILE *fp, char *buffer, int buf_size)
201 {
202 int copy_size = 0;
203 int end = 0;
204     memset(buffer, 0, buf_size);
205 
206     do
207     {
208 
209         read_line(fp);
210 
211         if(strncmp(line, FRAME_COMMENT, strlen(FRAME_COMMENT)) == 0)
212         {
213             if((int)(strlen(line) - strlen(FRAME_COMMENT)) <= (buf_size - 1))
214                 copy_size = strlen(line) - strlen(FRAME_COMMENT);
215             else
216                 copy_size = buf_size - 1;
217             memcpy(buffer, &line[strlen(FRAME_COMMENT)], copy_size);
218 
219         }
220         if(strncmp(line, FRAME_DATA, strlen(FRAME_DATA)) == 0)
221             end = 1;
222     }while(!end);
223 
224 }
225 
find_frame_data(FILE * fp)226 void find_frame_data(FILE *fp)
227 {
228 
229 
230     do
231     {
232         read_line(fp);
233 
234         if(strncmp(line, FRAME_DATA, strlen(FRAME_DATA)) == 0)
235             return;
236     }while(1);
237 
238 }
239 
cleanup(FILE * fp,char * buffer)240 void cleanup(FILE *fp, char *buffer)
241 {
242     if(fp)
243         fclose(fp);
244 
245     if(buffer)
246         free(buffer);
247 }
248 
249 #ifdef __linux__
frame_read(void * args)250 static void *frame_read(void *args)
251 {
252 int result;
253 
254     while(1)
255     {
256         sem_wait(&frame_read_semaphore);
257 
258         get_frame_id(golden_file, &frame_list_tail->frame_id);
259 
260         find_frame_data(golden_file);
261 
262         result = fread(frame_list_tail->frame_buffer, 1, golden_file_frame_size, golden_file);
263         if(result != golden_file_frame_size)
264         {
265             gx_validation_print_test_result(TEST_FAIL);
266             printf("Reading error\n");
267             exit(1);
268         }
269 
270         frame_list_tail = frame_list_tail->next;
271         sem_post(&frame_verify_semaphore);
272         frame_list_read_count++;
273         if(frame_list_read_count == golden_file_total_frames)
274         {
275             break;
276         }
277     }
278 
279     return NULL;
280 }
281 
gx_validation_verify_start(char * test_name,char * golden_file_name,int checksum)282 int gx_validation_verify_start(char *test_name, char *golden_file_name, int checksum)
283 {
284 int color_format;
285 int color_depth;
286 int width;
287 int height;
288 int loop;
289 
290     /* Open golden file. */
291     golden_file = fopen(golden_file_name, "rb");
292     if(!golden_file)
293     {
294         gx_validation_print_test_result(TEST_FAIL);
295         printf("Golden file is missing, unable to compare output\n");
296         exit(1);
297     }
298 
299     /* Read golden file header. */
300     read_file_header(golden_file, &color_format, &golden_file_total_frames, NULL,
301                      &color_depth, &width, &height);
302 
303 
304     if(checksum)
305     {
306         golden_file_frame_size = sizeof(unsigned int);
307     }
308     else
309     {
310         golden_file_frame_size = (width * color_depth >> 3) * height;
311     }
312 
313     frame_list_head = frame_list;
314     frame_list_tail = frame_list;
315     for(loop = 0; loop < MAX_NUM_FRAMES; loop++)
316     {
317         if(loop < MAX_NUM_FRAMES - 1)
318         {
319             frame_list[loop].next = &frame_list[loop + 1];
320         }
321         else
322         {
323             frame_list[loop].next = frame_list_head;
324         }
325 
326         frame_list[loop].frame_buffer = (char*)malloc(golden_file_frame_size);
327 
328         if(frame_list[loop].frame_buffer == NULL)
329         {
330             gx_validation_print_test_result(TEST_FAIL);
331             printf("Failed to allocate requested memory\n");
332             exit(1);
333         }
334     }
335 
336     /* Create semaphores to synchornize between threads*/
337     if((sem_init(&frame_read_semaphore, 0, MAX_NUM_FRAMES) != 0) ||
338        (sem_init(&frame_verify_semaphore, 0, 0) != 0))
339     {
340          gx_validation_print_test_result(TEST_FAIL);
341          printf("Semaphore initialization failed!");
342          exit(1);
343     }
344 
345     frame_list_read_count = 0;
346 
347     /* Create thread to read frame buffer. */
348     if(pthread_create(&frame_read_thread, NULL, frame_read, NULL) != 0)
349     {
350         gx_validation_print_test_result(TEST_FAIL);
351         printf("Thread creation failed!\n");
352         exit(1);
353     }
354 
355     return 0;
356 }
357 
gx_validation_verify_end(int total_frames)358 int gx_validation_verify_end(int total_frames)
359 {
360 int loop;
361 
362     /* Destroy semaphores. */
363     sem_destroy(&frame_read_semaphore);
364     sem_destroy(&frame_verify_semaphore);
365 
366     if(golden_file)
367     {
368         fclose(golden_file);
369         golden_file = NULL;
370     }
371 
372     for(loop = 0; loop < MAX_NUM_FRAMES; loop++)
373     {
374         free(frame_list[loop].frame_buffer);
375         frame_list[loop].frame_buffer = NULL;
376     }
377 
378     if(total_frames != golden_file_total_frames)
379     {
380         /* frame number dismatch. */
381         if(num_mismatch == 0)
382         {
383             gx_validation_print_test_result(TEST_FAIL);
384         }
385 
386         printf("Total frame number is not match\n");
387         exit(1);
388     }
389 
390     if(num_mismatch)
391     {
392         printf("Total %d, Failed %d\n", total_frames, num_mismatch);
393         exit(1);
394     }
395 
396     gx_validation_print_test_result(TEST_SUCCESS);
397     return(GX_VALIDATION_SUCCESS);
398 }
399 
gx_validation_verify_one_frame(void * frame_data,int frame_data_size,int frame_id)400 int gx_validation_verify_one_frame(void *frame_data, int frame_data_size, int frame_id)
401 {
402 int result = -1;
403 int status = GX_VALIDATION_SUCCESS;
404 
405     while(1)
406     {
407         result = sem_wait(&frame_verify_semaphore);
408 
409         if(result == 0)
410         {
411             /* Success. */
412             break;
413         }
414         else
415         {
416             if(errno == EINTR)
417             {
418                 /* The call was interrupted by a signal handler, continue wait. */
419                 continue;
420             }
421             else
422             {
423                 /* Unexpected error. */
424                 printf("sem_wait error!\n");
425                 exit(1);
426             }
427         }
428     }
429 
430     if(frame_id != frame_list_head->frame_id)
431     {
432         /* frame id dismatch. */
433         if(num_mismatch == 0)
434         {
435             gx_validation_print_test_result(TEST_FAIL);
436         }
437 
438         printf("golden_file_frame_id = %d, test_frame_id = %d.\n", frame_list_head->frame_id, frame_id);
439         exit(1);
440     }
441 
442     if(memcmp(frame_data, frame_list_head->frame_buffer, frame_data_size))
443     {
444         /* frame data dismatch. */
445         if(num_mismatch == 0)
446         {
447             gx_validation_print_test_result(TEST_FAIL);
448         }
449 
450         num_mismatch++;
451         printf("Frame %d is different.\n", frame_id);
452         status = GX_VALIDATION_DISMATCH;
453     }
454     frame_list_head = frame_list_head->next;
455     sem_post(&frame_read_semaphore);
456 
457     return(status);
458 }
459 #endif // __linux__
460