/*
fifo_app.c
Shows how to set up a normal user-level Linux process to communicate
with an RT task using FIFOs. FIFOs are queues of data, first-in-first-out,
that are set up as Linux character devices much like a serial port.
There are 64 FIFOs, named /dev/rtf0 through /dev/rtf63. These can be
accessed as usual for character devices, using the Unix open(), read(),
write() and close() functions.
This program opens one FIFO to the RT task for commands, and another
from the RT task for status back. There is handshaking in the RT task
such that status is written back for each command. This simplifies
programming here.
In general, a user task may have numerous FIFOs open between itself
and an RT task. The Unix function select() can be used to read from
the first available FIFO. We don't show that here.
*/
/*
THIS SOFTWARE WAS PRODUCED BY EMPLOYEES OF THE U.S. GOVERNMENT AS PART
OF THEIR OFFICIAL DUTIES AND IS IN THE PUBLIC DOMAIN.
*/
#include
#include /* open() */
#include /* O_RDONLY */
#include
#include "common.h" /* declarations for our command and status */
int main()
{
char * line;
char prompt[] = "> ";
COMMAND_STRUCT command;
STATUS_STRUCT status;
int command_fd;
int status_fd;
int freq;
int retval;
/*
Open the command and status FIFOs.
*/
if ((command_fd = open(RTF_COMMAND_DEV, O_WRONLY)) < 0) {
fprintf(stderr, "error opening %s\n", RTF_COMMAND_DEV);
return 1;
}
if ((status_fd = open(RTF_STATUS_DEV, O_RDONLY)) < 0) {
fprintf(stderr, "error opening %s\n", RTF_STATUS_DEV);
return 1;
}
/*
Now we'll enter a loop, reading input from the terminal, parsing
it and composing a command written to the RT task FIFO. After this
we wait for a response, print it and continue.
We can handle "on", "off", and "freq ", where "on" turns the
speaker on at the last set frequency, "off" turns it off, and
is the frequency we want to set. "quit" quits.
*/
command.command_num = 0;
retval = 0;
line = 0; /* no input yet */
while (! feof(stdin)) {
if (0 != line) free(line); /* free the last line of input, if any */
line = readline(prompt); /* get a new line of input */
if (0 == line) break; /* break if no more input */
if (0 == line[0]) continue; /* go again if line was blank */
/* parse the line and compose a command */
if (! strcmp(line, "on")) {
command.command = SOUND_ON;
} else if (! strcmp(line, "off")) {
command.command = SOUND_OFF;
} else if (1 == sscanf(line, "%*s %d", &freq)) {
command.command = SOUND_FREQ;
command.freq = freq;
} else if (! strcmp(line, "quit")) {
break;
} else {
/* unknown command */
printf("?\n");
continue;
}
/* if we dropped through to here, it's time to send the command */
command.command_num++;
if (sizeof(command) != write(command_fd, &command, sizeof(command))) {
fprintf(stderr, "can't write command\n");
retval = 1;
break;
}
/* now check for the reply */
if (sizeof(status) == read(status_fd, &status, sizeof(status))) {
printf("status: %d %d\n", status.command_num_echo, status.heartbeat);
}
}
close(status_fd);
close(command_fd);
return retval;
}