コード例 #1
0
ファイル: stm32mon.c プロジェクト: longsleep/ec
int wait_for_ack(int fd)
{
	uint8_t resp;
	int res;
	time_t deadline = time(NULL) + DEFAULT_TIMEOUT;

	while (time(NULL) < deadline) {
		res = read(fd, &resp, 1);
		if ((res < 0) && (errno != EAGAIN)) {
			perror("Failed to read answer");
			return -EIO;
		}
		if (res == 1) {
			if (resp == RESP_ACK)
				return 0;
			else if (resp == RESP_NACK) {
				fprintf(stderr, "NACK\n");
				discard_input(fd);
				return -EINVAL;
			} else {
				fprintf(stderr, "Receive junk: %02x\n", resp);
			}
		}
	}
	fprintf(stderr, "Timeout\n");
	return -ETIMEDOUT;
}
コード例 #2
0
ファイル: guess.c プロジェクト: asgardkm/DP_PMP
#include <stdio.h> /* guess.c - Script 11.4 */
#include <string.h> /* For strcasecmp(). */
void discard_input (void); /* Function prototype. */
int main (void) {
	/* Two character arrays. */
	char guess[10];	const char answer[] = "coy"; /* Word to guess. */
	
	/* Prompt for and read in an answer. */
	printf("Guess what three-letter word I'm thinking of: ");
	scanf("%9s", guess);	
	discard_input();
	
	/* Check for a match, re-prompt if wrong. */
	while (strcasecmp(answer, guess)) {
		printf("Incorrect! Guess again: ");
		scanf("%9s", guess);		
		discard_input();	}
	/* Must be correct to get here. */
	printf("You are correct!");	getchar();	return 0;}
コード例 #3
0
ファイル: tty.c プロジェクト: BiancoZandbergen/RTMINIX3
static void set_raw_tty(int mode)
{
	Terminal in_raw;

	if(mode != tty_mode && mode != -1) {
		if(!handlerIsSet) {
			/* Determine existing TTY settings */
			gtty (STDIN, &in_orig);
			need_tty_reset = 1;

			/* Restore original TTY settings on exit */
			atexit(cleanup_tty);
			handlerIsSet = 1;
		}


		setup_signal();
		signal (SIGALRM, (SIG_CAST) tty_time_out);
	
		/* Change STDIN settings to raw */

		gtty (STDIN, &in_raw);
		if(mode) {
#ifdef USE_SGTTY
			in_raw.sg_flags |= CBREAK;
#else
			in_raw.c_lflag &= ~ICANON;
			in_raw.c_cc[VMIN]=1;
			in_raw.c_cc[VTIME]=0;			
#endif
			stty (STDIN, &in_raw);
		} else {
#ifdef USE_SGTTY
			in_raw.sg_flags &= ~CBREAK;
#else
			in_raw.c_lflag |= ICANON;
#endif
			stty (STDIN, &in_raw);
		}
		tty_mode = mode;
		discard_input(STDIN);
	}
}
コード例 #4
0
ファイル: stm32mon.c プロジェクト: longsleep/ec
int init_monitor(int fd)
{
	int res;
	uint8_t init = CMD_INIT;

	/* Skip in i2c mode */
	if (i2c_adapter != INVALID_I2C_ADAPTER)
		return 0;

	printf("Waiting for the monitor startup ...");
	fflush(stdout);

	while (1) {
		/* Send the command index */
		res = write(fd, &init, 1);
		if (res <= 0) {
			perror("Failed to write command");
			return -1;
		}
		/* Wait for the ACK */
		res = wait_for_ack(fd);
		if (res == 0)
			break;
		if (res == -EINVAL) {
			/* we got NACK'ed, the loader might be already started
			 * let's ping it to check
			 */
			if (command_get_id(fd)) {
				printf("Monitor already started.\n");
				return 0;
			}
		}
		if (res < 0 && res != -ETIMEDOUT)
			return -1;
		fflush(stdout);
	}
	printf("Done.\n");

	/* read trailing chars */
	discard_input(fd);

	return 0;
}
コード例 #5
0
ファイル: sort_template.c プロジェクト: asgardkm/DP_PMP
int main (void) {
	
	/* Define character array. */
	char words[NUM_STRINGS][STR_LEN];
	char input[STR_LEN]; /* For user input. */

	/* Pointers. */
	char *words_ptr[NUM_STRINGS];
	char *temp;
	
	int i, j; /* Loop counters. */
	int count = 0; /* To count number of words entered. */
	
	/* Get up to NUM_STRINGS words. */
	for (i = 0; i < NUM_STRINGS; ++i) {
		/* Prompt and read in word. */
		printf("Enter a word (or 0 to quit): ");
		scanf("%9s", input);
		discard_input();
		
		/* Check for a 0. */
		if (input[0] == '0') break;
		
		/* Copy the input to the array. */
		strncpy(words[i], input, (STR_LEN - 1));
		
		/* Copy the address to the pointer. */
		words_ptr[i] = &words[i][0]; /*The &words[i][0]construct returns the address of the fist character in the newly added string. This address is assigned to the pointer, in the corresponding matrix. It will be these addresses that will be moved around in the sorting process. */
		
		/* Count another word entered. */
		++count;	} /* End of while loop. */

	 printf("A total of %d words were entered.\n", i);
	
	
	
	
	for (i = 0; i < (count - 1); ++i) { /* Loop through each word. */
		
        for (j = (i + 1); j < count; ++j) { /* Loop through each word again. */
			
            if (strcmp(words_ptr[i],words_ptr[j]) > 0) { /* Compare words. */
				
                temp = words_ptr[i]; /* Swap word 1 to the temp location. */
                words_ptr[i] = words_ptr[j]; /* Assign word 2 to word 1. */
                words_ptr[j] = temp; /* Assign temp (original word 1) to word 2. */
				
            } /* End of IF. */

        } /* End of inner FOR. */

    } /* End of outer FOR. */
	printf("In alphabetical order, the words are:\n");                                                                                                                                                 
	/* Print the numbers in a loop. */
	for (i = 0; i < count; i++) {
		printf("%s\n", words_ptr[i]);
	}
	
	getchar();	
	return 0;
	
} /* End of main() function. */
コード例 #6
0
ファイル: stm32mon.c プロジェクト: longsleep/ec
int open_serial(const char *port)
{
	int fd, res;
	struct termios cfg, cfg_copy;

	fd = open(port, O_RDWR | O_NOCTTY);
	if (fd == -1) {
		perror("Unable to open serial port");
		return -1;
	}

	/* put the tty in "raw" mode at the defined baudrate */
	res = tcgetattr(fd, &cfg);
	if (res == -1) {
		perror("Cannot read tty attributes");
		close(fd);
		return -1;
	}
	cfmakeraw(&cfg);
	cfsetspeed(&cfg, baudrate);
	/* serial mode should be 8e1 */
	cfg.c_cflag |= PARENB;
	/* 200 ms timeout */
	cfg.c_cc[VTIME] = 2;
	cfg.c_cc[VMIN] = 0;
	memcpy(&cfg_copy, &cfg, sizeof(cfg_copy));

	/*
	 * tcsetattr() returns success if any of the modifications succeed, so
	 * its return value of zero is not an indication of success, one needs
	 * to check the result explicitely.
	 */
	tcsetattr(fd, TCSANOW, &cfg);
	if (tcgetattr(fd, &cfg)) {
		perror("Failed to re-read tty attributes");
		close(fd);
		return -1;
	}

	if (memcmp(&cfg, &cfg_copy, sizeof(cfg))) {
		/*
		 * On some systems the setting which does not come through is
		 * the parity. We can try continuing without it when using
		 * certain interfaces, let's try.
		 */
		cfg_copy.c_cflag &= ~PARENB;
		if (memcmp(&cfg, &cfg_copy, sizeof(cfg))) {
			/*
			 * Something other than parity failed to get set, this
			 * is an error.
			 */
			perror("Cannot set tty attributes");
			close(fd);
			return -1;
		} else {
			fprintf(stderr, "Failed to enable parity\n");
		}
	}

	discard_input(fd); /* in case were were invoked soon after reset */
	return fd;
}