file_dumper.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2004, 2005 University of Utah and the Flux Group.
 * All rights reserved.
 */

#include "config.h"

#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>

#include <sys/stat.h>
#include <fcntl.h>

#include "log.h"
23
//#include "mtp.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <math.h>

#include "mezz.h"


static volatile unsigned int mezz_frame_count = 0;
static double x_offset = 0.0;
static double y_offset = 0.0;
static double z_offset = 0.0;

static char *mezz_file,*output_file,*input_file;
static int num_frames = 5;
static int frame_interval = 1;
static FILE *output_FILE;
static FILE *input_FILE;

/**
 * Signal handler for SIGUSR1 that updates the mezzanine frame count so the
 * main loop knows there is a new frame available.
 *
 * @param signal The actual signal received.
 */
static void sigusr1(int signal)
{
    assert(signal == SIGUSR1);
    
    mezz_frame_count += 1;
}

53
/* #define ROBOT_HEIGHT 0.12f */
54

55 56 57 58 59
/* void radial_trans(struct robot_position *p_inout) */
/* { */
/*     float distance_from_center, theta, vtheta, offset; */
/*     struct robot_position dp; */
/*     struct robot_position rp; */
60

61 62 63
/*     dp.x = 0.0; */
/*     dp.y = 0.0; */
/*     dp.theta = 0.0; */
64
    
65
/*     rp = *p_inout; */
66

67 68 69 70 71
/*     mtp_polar(&dp, &rp, &distance_from_center, &theta); */
/*     vtheta = atan2f(z_offset, distance_from_center); */
/*     offset = ROBOT_HEIGHT / tanf(vtheta); */
/*     mtp_cartesian(&dp, distance_from_center - offset, theta, p_inout); */
/* } */
72 73 74 75 76 77

/**
 * Transform a local camera coordinate to a real world coordinate.
 *
 * @param p The position to transform.
 */
78 79 80
/* void local2global_posit_trans(struct robot_position *p_inout) */
/* { */
/*     float old_x = p_inout->x; */
81
    
82
/*     assert(p_inout != NULL); */
83
    
84 85 86 87
/*     p_inout->x = p_inout->y + x_offset; */
/*     p_inout->y = old_x + y_offset; */
/*     p_inout->theta -= M_PI_2; */
/* } */
88 89 90

void print_packets(mezz_mmap_t *mm)
{
91
/*     struct robot_position rp; */
92 93
    mezz_objectlist_t *mol;
    int lpc;
94 95 96 97 98 99
    int i,j;
    int done_blobs[100];

    for (i = 0; i < 100; ++i) {
	done_blobs[i] = 0;
    }
100 101 102 103 104 105 106 107 108
    
    assert(mm != NULL);

    mol = &mm->objectlist;

    fprintf(output_FILE,"frame %d (timestamp %f):\n",
	    mezz_frame_count,mm->time
	    );

109 110
    int found_first_blob = 0;

111 112 113
    for (i = 0; i < mm->objectlist.count; ++i) {
	mezz_object_t *obj = &(mm->objectlist.objects[i]);
	if (obj->valid) {
114
	    fprintf(output_FILE,
115 116 117 118 119 120
		    "[%d] a(%f,%f) b(%f,%f)",
		    i,
		    obj->ablob.ox,
		    obj->ablob.oy,
		    obj->bblob.ox,
		    obj->bblob.oy
121 122
		    );
	    fprintf(output_FILE,
123 124 125 126
		    " -- wc(%f,%f,%f)\n",
		    obj->px,
		    obj->py,
		    obj->pa
127
		    );
128
	}
129
    }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    // old "algorithm" for finding which blobs belong to which objects...
    // faulty cause blobs aren't declared as valid/invalid like objects are.

/*     for (i = 0; i < mm->bloblist.count; ++i) { */
/* 	if (found_first_blob) { */
/* 	    break; */
/* 	} */
/* 	for (j = 0; j < mm->bloblist.count; ++j) { */
/* 	    if (found_first_blob) { */
/* 		break; */
/* 	    } */
/* 	    if (j != i) { */
/* 		if (!done_blobs[i] && !done_blobs[j] && */
/* 		    mm->bloblist.blobs[i].object == */
/* 		    mm->bloblist.blobs[j].object ) { */

/* 		    found_first_blob = 1; */

/* 		    done_blobs[i] = done_blobs[j] = 1; */

/* 		    // print out the blob centroids */
/* 		    fprintf(output_FILE, */
/* 			    "a(%f,%f) b(%f,%f)", */
/* 			    mm->bloblist.blobs[i].ox, */
/* 			    mm->bloblist.blobs[i].oy, */
/* 			    mm->bloblist.blobs[j].ox, */
/* 			    mm->bloblist.blobs[j].oy */
/* 			    ); */

/* 		    fprintf(output_FILE, */
/* 			    " -- wc(%f,%f,%f)\n", */
/* 			    mol->objects[0].px, */
/* 			    mol->objects[0].py, */
/* 			    mol->objects[0].pa */
/* 			    ); */


/* 		    fflush(output_FILE); */
/* 		} */
/* 	    } */
/* 	} */
/*     } */

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
}

void usage() {

}

int main(int argc, char *argv[])
{
    int c;
    mezz_mmap_t *mezzmap = NULL;
    struct sigaction sa;
    char **default_section_title;
    int default_section_title_len;
    int section_counter = 0;
    int current_default_section_title_len = 64;

    default_section_title = (char **)malloc(sizeof(char *)*
					    current_default_section_title_len);
    if (default_section_title == NULL) {
	error("no memory for section titles!  :-(\n");
	exit(1);
    }
    
    while ((c = getopt(argc, argv, "x:y:z:n:i:f:F:")) != -1) {
        switch (c) {
	case 'f':
	    input_file = optarg;
	    break;
	case 'F':
	    output_file = optarg;
	    break;
        case 'i':
            frame_interval = atof(optarg);
	    if (frame_interval < 1) {
		error("error: option -i is not a number > 0: %s\n",optarg);
		usage();
		exit(1);
	    }
            break;
        case 'n':
            if (sscanf(optarg, "%d", &num_frames) != 1) {
                error("error: -n option is not a number: %s\n", optarg);
                usage();
                exit(1);
            }
            break;
        case 'x':
	    if (sscanf(optarg, "%lf", &x_offset) != 1) {
                error("error: -x option is not a number: %s\n", optarg);
                usage();
                exit(1);
	    }
            break;
        case 'y':
	    if (sscanf(optarg, "%lf", &y_offset) != 1) {
                error("error: -y option is not a number: %s\n", optarg);
                usage();
                exit(1);
	    }
            break;
	case 'z':
	    if (sscanf(optarg, "%lf", &z_offset) != 1) {
                error("error: -z option is not a number: %s\n", optarg);
                usage();
                exit(1);
	    }
	    break;
        default:
            break;
        }
    }
    
    argv += optind;
    argc -= optind;
    
    if (argc < 1) {
        error("error: missing mezzanine ipc file\n");
        usage();
        exit(1);
    }
    else {
	mezz_file = argv[0];
    }

    signal(SIGPIPE, SIG_IGN);
    
    if (mezz_init(0, mezz_file) == -1) {
261 262
	error("unable to initialize mezzanine\n");
	//errorc("unable to initialize mezzanine\n");
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	exit(2);
    }
    mezzmap = mezz_mmap();
	
    
    unsigned int last_dumped_frame = 0;
    unsigned int frames_needed;
    int i;

    char *section_title = NULL;
    char section_title_buffer[256];

    /* read default section names */
    if (input_file != NULL) {
	input_FILE = fopen(input_file,"r");
	section_counter = 0;

	if (input_FILE != NULL) {
	    while (fgets(section_title_buffer,255,input_FILE) != NULL) {
		if (strlen(section_title_buffer) > 0 && 
		    section_title_buffer[strlen(section_title_buffer)-1] 
		    != '\n'
		    ) {
		    /* we have too much title, truncate the line by reading 
		     * rest of it
		     */
		    int c;
		    while ((c = fgetc(input_FILE)) != EOF && c != '\n')
			;
		}
		
		//printf("herea: %s %d\n",section_title_buffer,
		//       strlen(section_title_buffer));

		// kill the newline, if there was one...
		if (strlen(section_title_buffer) > 0) {
		    //printf("c\n");
		    section_title_buffer[strlen(section_title_buffer)-1] = 
			'\0';
		}


		// if the line is blank or the very first char is a hash, skip
		if (strlen(section_title_buffer) < 1 || 
		    section_title_buffer[0] == '#'
		    ) {
		    continue;
		}

		//printf("hereb\n");

		if (section_counter == default_section_title_len) {
		    // need to realloc
		    
		    if (realloc(default_section_title,
				(default_section_title_len+64)*sizeof(char)
				) == NULL) {
			error("no more memory while reading section"
			      " titles\n");
			exit(1);
		    }
			// null out new pointers...
		    for (i = default_section_title_len; 
			 i < (default_section_title_len + 64);
			 ++i
			 ) {
			default_section_title[i] = NULL;
			}
		    default_section_title_len += 64;
		}

		//printf("herec\n");

		
		default_section_title[section_counter] = (char *)
		    malloc(sizeof(char)*(strlen(section_title_buffer)+1));
		
		if (default_section_title[section_counter] == NULL) {
		    error("error: no memory for section title copy\n");
		    exit(1);
		}

		//printf("hered\n");
		
		strncpy(default_section_title[section_counter],
			section_title_buffer,
			strlen(section_title_buffer)+1
			);
		
		++section_counter;
		//printf("heree\n");
		
	    }
	    printf("Read %d sections.\n",section_counter);
	}

	fclose(input_FILE);
    }

    default_section_title_len = section_counter;
    
    if (output_file != NULL) {
365
	output_FILE = fopen(output_file,"w");
366
	if (output_FILE == NULL) {
367
	    error("error: could not open output file for writing\n");
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	    exit(1);
	}
    }
    else {
	output_FILE = stdout;
    }


    fprintf(output_FILE,
	    "Starting file dump:\n"
	    "  - output file: %s\n"
	    "  - input file: %s\n"
	    "  - x_offset: %f\n"
	    "  - y_offset: %f\n"
	    "  - z_offset: %f\n"
	    "  - number of frames: %d\n"
	    "  - frame interval: %d\n"
	    "\n",
	    output_file,input_file,
	    x_offset,y_offset,z_offset,
	    num_frames,frame_interval
	    );


    /*
     * Install our own SIGUSR1 handler _over_ the one installed by mezz_init,
     * so we can increment the local frame count and really be able to tell
     * when a frame was received.
     */
    sa.sa_handler = SIG_IGN;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGUSR1, &sa, NULL);

    section_counter = 0;
    section_title_buffer[0] = '\0';

    while (1) { /* The main loop. */
	if (default_section_title_len != 0) {
	    if (section_counter >= default_section_title_len) {
		section_title_buffer[0] = '\0';
		section_title = section_title_buffer;
	    }
	    else {
		section_title_buffer[0] = '\0';
		section_title = default_section_title[section_counter];
		++section_counter;
	    }
	}
	else {
	    section_title = section_title_buffer;
	    section_title_buffer[0] = '\0';
	}

	/* prompt for input... */
	fprintf(stdout,"\nSection title [%s]: ",section_title);
	fflush(stdout);
	fgets(section_title_buffer,255,stdin);

	if (strlen(section_title_buffer) == 0) {
	    ;
	}
	else {
	    section_title_buffer[strlen(section_title_buffer)-1] = '\0';
	}

	if (strlen(section_title_buffer) == 0 && strlen(section_title) == 0) {
	    exit(0);
	}

	/* get frame data */
	/* let signals in... */
	/* bit of a race here, but should be no prob */
	last_dumped_frame = mezz_frame_count;
	frames_needed = num_frames;

	if (strlen(section_title_buffer) > 0) {
	    section_title = section_title_buffer;
	}

	fprintf(output_FILE,
		"+++\n"
		"section: %s\n",
		section_title
		);

	sa.sa_handler = sigusr1;
	sigaction(SIGUSR1, &sa, NULL);

	while (frames_needed > 0) {

	    /* one second should always be plently of time to get
	     * a SIGUSR1
	     */
	    sleep(1);

	    /* disable mezz interrupts */
	    //sa.sa_handler = SIG_IGN;
	    //sigaction(SIGUSR1,&sa,NULL);

	    if (mezz_frame_count >= (last_dumped_frame + frame_interval)) {
		/* new frame */
		if (mezz_frame_count != 
		    (last_dumped_frame + frame_interval)
		    ) {
		    // must have missed one!
		    fprintf(output_FILE,"WARNING: missed %d frames!\n",
			    (mezz_frame_count - (last_dumped_frame +
						 frame_interval))
			    );
		}
		last_dumped_frame = mezz_frame_count;
		--frames_needed;
		print_packets(mezzmap);
	    }

	    /* reenable interrupts */
	    //sa.sa_handler = sigusr1;
	    //sigaction(SIGUSR1,&sa,NULL);

	}

	sa.sa_handler = SIG_IGN;
	sigaction(SIGUSR1,&sa,NULL);

	fprintf(output_FILE,"+++\n\n");

    }
    

    mezz_term(0);
    
}