示例#1
0
文件: fifo2.c 项目: spo0lsh/c_learn
int main() {
	CircularBuffer cb; /* */
	ElemType elem = {0};
    
    int testBufferSize = 10; /* arbitrary size */
    cbInit(&cb, testBufferSize);
	
    /* Fill buffer with test elements 3 times */
    for (elem.value = 0; elem.value < 3 * testBufferSize; ++ elem.value)
    {
		if(! cbIsFull(&cb)) { /* if not full */
			cbWrite(&cb, &elem);
		} 
	}

    /* Remove and print all elements */
    while (!cbIsEmpty(&cb)) {
        cbRead(&cb, &elem);
        printf("%d\n", elem.value);
    }
 
    cbFree(&cb);
	
	return(0);
}
示例#2
0
文件: main.c 项目: digideskio/afdko
/* Main program */
int main(int argc, char *argv[]) {
	dnaCtx mainDnaCtx = NULL;
	long value;

	/* Extract program name */
	progname = strrchr(argv[0], '/');
	if (progname == NULL) {
		progname = strrchr(argv[0], '\\');
	}
	progname = (progname == NULL) ? argv[0] : progname + 1;
	argc--;
	argv++;
	value = setjmp(mark);
	if (value == -1) {
		return 1;
	}


	/* Initialize */
	cb_dna_memcb.ctx = mainDnaCtx;
	cb_dna_memcb.manage = cb_manage;
	mainDnaCtx = dnaNew(&cb_dna_memcb, DNA_CHECK_ARGS);

	cbctx = cbNew(progname, convert.dir.pfb,
				  convert.dir.otf, convert.dir.cmap, convert.dir.feat, mainDnaCtx);

	script.buf = NULL;
	dnaINIT(mainDnaCtx, script.args, 100, 500);

	convert.dir.pfb[0] = '\0';
	convert.dir.otf[0] = '\0';
	convert.dir.cmap[0] = '\0';
	convert.dir.feat[0] = '\0';
	convert.features = NULL;
	convert.hCMap = NULL;
	convert.vCMap = NULL;
	convert.flags = 0;
	convert.otherflags = 0;
	convert.os2_version = 0;
	convert.fsSelectionMask_on = -1;
	convert.fsSelectionMask_off = -1;

	/* Process args. Call convFont at end. */
	parseArgs(argc, argv, 0);

	fprintf(stderr, "\n");			/* Terminate progress line */

	/* Clean up */
	cbMemFree(cbctx, script.buf);
	dnaFREE(script.args);
	cbFree(cbctx);

	return 0;
}
示例#3
0
文件: circular.c 项目: jpierre03/libs
int main(int argc, char **argv) {
	CircularBuffer cb;
	ElemType elem = {0};

	int testBufferSize = 50; /* arbitrary size */
	cbInit(&cb, testBufferSize);

	/* Fill buffer with test elements 3 times */
	for (elem.value = 0; elem.value < 3 * testBufferSize; ++ elem.value){
		cbWrite(&cb, &elem);
	}

	/* Remove and print all elements */
	while (!cbIsEmpty(&cb)) {
		cbRead(&cb, &elem);
		printf("%d\n", elem.value);
	}

	cbFree(&cb);
	return 0;
}
示例#4
0
/**
 * Handler for Ctrl-C, exits the main loop.
 */
void sigint_handler(int sig) {
  cbFree(&cb);
  IvyStop();
  TimerRemove(tid);
  close(fd);
}
示例#5
0
void remove_proc(void) {
	remove_proc_entry("touchlog", 0);
	remove_proc_entry("touchinfo", 0);
	cbFree(&cb);
}
示例#6
0
int main(int argc, char *argv[]){
	write_uart_error_ptr = &write_uart_error;  //initialize the function pointer to write error
	write_udp_error_ptr = &write_udp_error; 
	write_decode_error_ptr = &write_decode_error;  
	write_log_error_ptr = &write_log_error;  
 
	//parse arguments
	if(argc == 4){
		//first argument is always name of program or empty string
		connection.server_ip=argv[1];
		connection.port_number_lisa_to_pc=atoi(argv[2]);
		connection.port_number_pc_to_lisa=atoi(argv[3]);
	}else{
			printf("wrong parameters: server ip - send port number - receive port number\n");
			exit(EXIT_FAILURE);
	}

	//init log (mount sd card if necessary)

	int err = init_log();
	LOG_err_handler(err,write_log_error_ptr);

	if(err != LOG_ERR_NONE){
		exit(EXIT_FAILURE);		//mounting SD card failed
	}
	
	enable_ptp();

	#if LOGGING > 0
	//init circular data log buffers
	 cbInit(cb_read_lisa, CBSIZE);
	 cbInit(cb_write_lisa, CBSIZE);
	 cbInit(cb_read_ground, CBSIZE);
	 cbInit(cb_write_ground, CBSIZE);
	 #endif

	err = serial_port_setup();
	UART_err_handler(err,write_uart_error_ptr);
	if(err != UART_ERR_NONE){
		exit(EXIT_FAILURE);
	}

	//thread variables
	pthread_t thread_lisa_to_pc,thread_data_logging_lisa,thread_data_logging_ground;

	//create a second thread which executes lisa_to_pc
	if(pthread_create(&thread_lisa_to_pc, NULL, lisa_to_pc,NULL)) {
		error_write(FILENAME,"error creating lisa thread");
		exit(EXIT_FAILURE);
	}

	#if LOGGING > 0

	//create a third thread which executes data_logging_lisa
	if(pthread_create(&thread_data_logging_lisa, NULL, data_logging_lisa,NULL)) {
		error_write(FILENAME,"error creating lisa logging thread");
		exit(EXIT_FAILURE);
	}

	//create a fourth thread which executes data_logging_groundstation
	if(pthread_create(&thread_data_logging_ground, NULL, data_logging_groundstation,NULL)) {
		error_write(FILENAME,"error creating groundstation logging thread");
		exit(EXIT_FAILURE);
	}

	#endif
	/*-------------------------START OF FIRST THREAD: PC TO LISA------------------------*/
	static UDP udp_server;
	uint8_t input_stream[OUTPUT_BUFFER];

	ElemType cb_elem = {0};

	//init the data decode pointers
	init_decoding();

	UDP_err_handler(openUDPServerSocket(&udp_server,connection.port_number_pc_to_lisa,UDP_SOCKET_TIMEOUT),write_udp_error_ptr);

	while(1){

		//1. retreive UDP data form PC from ethernet port.
		err=receiveUDPServerData(&udp_server,(void *)&input_stream,sizeof(input_stream)); //blocking !!!
		UDP_err_handler(err,write_udp_error_ptr);

		if(err==UDP_ERR_NONE){

			#if LOGGING > 0
			
			if(!cbIsFull(cb_write_ground)){
				 memcpy (&cb_elem.value, &input_stream, sizeof(input_stream));
				 cbWrite(cb_write_ground, &cb_elem);
			 }else{
				if(reading_flag_ground==0){
					switch_cb_ground_pointers();
				}else{
					printf("GROUND WRITE WAS NOT READY \n");
				}
			 }
			
			#endif

			int new_length = strip_timestamp(input_stream); //lisa expects a package without a timestamp

			UART_err_handler(serial_port_write(input_stream,new_length),write_uart_error_ptr);
		}

	}
	UART_err_handler(serial_port_close(),write_uart_error_ptr);
	UDP_err_handler(closeUDPServerSocket(&udp_server),write_udp_error_ptr);
	/*------------------------END OF FIRST THREAD------------------------*/


	//wait for the second thread to finish
	if(pthread_join(thread_lisa_to_pc, NULL)) {
		error_write(FILENAME,"error joining thread_lisa_to_pc");
	}

	#if LOGGING > 0

	//wait for the third thread to finish
	if(pthread_join(thread_data_logging_lisa, NULL)) {
		error_write(FILENAME,"error joining thread_data_logging_lisa");
	}


	//wait for the fourth thread to finish
	if(pthread_join(thread_data_logging_ground, NULL)) {
		error_write(FILENAME,"error joining thread_data_logging_ground");
	}

	//free circular buffers
	cbFree(cb_read_lisa);
	cbFree(cb_write_lisa);
	cbFree(cb_read_ground);
	cbFree(cb_write_ground);

	#endif

	return 0;
}
示例#7
0
int main(int argc, char *argv[]) 
{
	int print;
	printf("%d %s\n",argc, argv[0]);
	if(argc >1 && strcmp("-p", argv[1]) == 0) print=1;
	
	time_t nosso_tempo;
	
//************************************************/
	CircularBuffer cb;
    ElemType elem = {0};
    int testBufferSize = 11264; // 176 blocos de 64 palavras
    cbInit(&cb, testBufferSize);
//************************************************/

	FILE *arquivo;
	
	int fd;
	
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"	
	int res;
#pragma GCC diagnostic pop
	
	unsigned short crc16;
	int i,j=0;
	int detected= 0, pronto = 0;
	char *ptr;
	uint16_t crc;
	uint16_t n_mesg, msg_num = 0;
	int offset;
	struct termios oldtio,newtio;
	char buf[BUFFER_SIZE];
	fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
	if (fd < 0) {
		perror(MODEMDEVICE); 
		cbFree(&cb);
		exit(-1); 
	}
	tcgetattr(fd,&oldtio); /* save current port settings */
	bzero(&newtio, sizeof(newtio));
	newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	newtio.c_iflag = IGNPAR;
	newtio.c_oflag = 0;

	/* set input mode (non-canonical, no echo,...) */
	newtio.c_lflag = 0;

	newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
	newtio.c_cc[VMIN]     = 132;   /* blocking read until 132 chars received */

	tcflush(fd, TCIFLUSH);
	tcsetattr(fd,TCSANOW,&newtio);

	arquivo = fopen("raios_detetados.bin", "wb");
	if (arquivo == NULL) {
		perror("Nao foi possível abrir o arquivo!");
		cbFree(&cb);
		close(fd);
		exit(30);
	}
	
	while (1) {     /* loop until we have a terminating condition */
		/* read blocks program execution until a line terminating character is 
			input, even if more than 255 chars are input. If the number
			of characters read is smaller than the number of chars available,
			subsequent reads will return the remaining chars. res will be set
			to the actual number of characters actually read */
		res=read(fd,buf,BUFFER_SIZE);
// 		printf("%d chars - ",res);

	crc=((uint16_t) (0xFF00 & (buf[MSG_SIZE+1]<<8)) + (uint16_t) (0x00FF & buf[MSG_SIZE]));
	n_mesg=((uint16_t) (0xFF00 & (buf[1]<<8)) + (uint16_t) (0x00FF & buf[0]));
// 		char test[]="CRC16 stream simulation.";
		ptr=buf;
		crc16= 0;
		for (i = 0; i < MSG_SIZE; i++) {
			crc16 = update_crc_16(crc16, *ptr);
			ptr++;
		}
		if(crc==crc16){
// 			printf(" CRC OK! -  %4X %4X %d\n", crc, crc16, n_mesg);

			for(i=1;i<65;i++){
				elem.value=((int16_t) (0xFF00 & (buf[2*(i)+1]<<8)) + (int16_t) (0x00FF & buf[2*i]));
				if (detected == 1) {
//					if (n_mesg != msg_num) {
					if (j < 13632) { 
						vetor_disco[j] = elem.value;
						j++;
// 						printf("j = %d\n", j);
					}
					else {
						pronto = 1;
						detected = 0;
						cbWrite(&cb, &elem);
					}
				}
				else {
					cbWrite(&cb, &elem);
				}
				if(print==1){
					printf("%4d ",elem.value);
					if (((i+2) % 32) ==1)
					printf("\n");
				}
			}
		} else {
			clearScreen();
// 			printf("not OK!  %X %X\n", crc, crc16);
			tcflush(fd, TCIFLUSH);
		}
		if (pronto == 1) {
			fwrite(vetor_disco, sizeof(vetor_disco), 1, arquivo);
			printf("Write!!!!!");
			j = 0;
			pronto = 0;
		}
// 		if(j>=testBufferSize){
// 			while (!cbIsEmpty(&cb)) {
	// 					elem = cbRead(&cb);i
// 				for (offset=0;offset<(cb.end-cb.start);offset++){
// 					elem = cbPeek(&cb,offset);
// 					printf("%d\t%d\t%d\t%d\n", elem.value, cb.start, cb.end, cb.size);
// 				}
		if (detected == 0) {
			offset = r_change(&cb,n_mesg); // TODO: o que acontece quando n_mesg retorna a zero?
			if(offset){
				nosso_tempo = time(&nosso_tempo);
				fwrite(&nosso_tempo, sizeof(time_t),1, arquivo);
//				for (i = offset - 2263, j = 0; i < cb.end; i++, j++){ // TODO :: fazer a conta do offset levando em consideração o wrap around.
				for (i = (offset%64 - 37)*64, j = 0; i < (offset%64 +1)*64; i++, j++){
					ElemType temp_elem;
 					temp_elem = cbPeek(&cb, i);
					vetor_disco[j] = temp_elem.value;
				}
				cb.end = cb.start; // Esvazia o buffer circular	
				detected = 1;
				msg_num = n_mesg + 176;
				printf("Detected: %ld %d %d\n",nosso_tempo, n_mesg,msg_num);
			}	
	
		}
// 			}
// 		break;
// 		}
// 			for(i=0;i<64;i++){
// 				printf("%4d ",((uint16_t) (0xFF00 & (buf[2*(i)+1]<<8)) + (uint16_t) (0x00FF & buf[2*i])));
// 				if (((i+2) % 32) ==1)           printf("\n");
// 				}
		}
		tcsetattr(fd,TCSANOW,&oldtio);
		return(0);
}