examples/kerneltest/console/console.c
author Robert Lehmann <robert.lehmann@sitec-systems.de>
Tue, 28 Jul 2015 16:36:55 +0200
changeset 793 72e9e1064432
parent 467 40efa79d27dd
permissions -rw-r--r--
timers_unix: Fix termination problem of WaitReceiveTaskEnd

The function pthread_kill sends the Signal thread and to the own process.
If you use this construct than the application which calls uses the
canfestival api will terminate at the call of canClose. To avoid that
use pthread_cancel instead of pthread_kill. To use the pthread_cancel call
you need to set the cancel ability in the thread function. That means
you need to call pthread_setcancelstate and pthread_setcanceltype.
For the termination of the thread at any time it is important to set the
cancel type to PTHREAD_CANCEL_ASYNCHRONOUS.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include "console.h"


void showhelp(void) {
	printf("\nCanFestival kernel test example console\n\n");

	printf("start - start example\n");
	printf("stop  - stop example\n");
	printf("quit  - quit console\n");
	printf("\n");
}

int main(int argc,char *argv[])
{
	int canf_ktest_dev, cmd;
	char command[10];
	char device_path[20] = "/dev/";

	// create absolute path name for device
	strcat (device_path, DEVICE_NAME);

	canf_ktest_dev = open (device_path, O_WRONLY);
	
	if (canf_ktest_dev == -1) {
		perror ("Opening device");
		return 1;
	}

	showhelp();
    
	while (1) {
		printf("> ");
		scanf ("%s", &command);

		if (strcmp(command,"start") == 0)
			cmd = CMD_START;
		
		else if (strcmp(command,"stop") == 0)
			cmd = CMD_STOP;
		
		else if (strcmp(command,"quit") == 0)
			break;
		
		else {
			printf("Bad command\n");
			continue;
		}
		
		write(canf_ktest_dev, &cmd, sizeof(cmd));
	}
    
	close(canf_ktest_dev);
	return 0;
}