#!/bin/ch /* 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 <cgi.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* open() */ #include <fcntl.h> /* O_RDONLY */ #include <readline/readline.h> #include "common.h" /* declarations for our command and status */ int main() { char * line; char prompt[] = "> "; static COMMAND_STRUCT command; STATUS_STRUCT status; int command_fd; int status_fd; static double freq; int retval; class CRequest Request; class CResponse Response; chchar *frequency, *onoff, *commands; /* 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 <Hz>", where "on" turns the speaker on at the last set frequency, "off" turns it off, and <Hz> is the frequency we want to set. "quit" quits. */ command.command_num = 0; retval = 0; line = 0; /* no input yet */ Response.begin(); Response.title("Web speaker control"); onoff = Request.getForm("onoff"); commands = Request.getForm("command"); /* parse the line and compose a command */ if (! strcmp(onoff, "On")) { command.command = SOUND_ON; }else if (! strcmp(onoff, "Off")) { command.command = SOUND_OFF; } frequency= Request.getForm("x"); freq = strtod(frequency, NULL); command.freq = freq; /* 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; } /* 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); } /* set a new web page */ printf("<HTML><HEAD>\n"); printf(" <TITLE>Web RTAI Control </TITLE>\n"); printf(" <body bgcolor=\"#FFFFFF\" text=\"#000000\" vlink=\"#FF0000\">\n"); printf(" </HEAD><BODY>"); printf(" <H1>\n"); printf(" CGI-Based RTAI Application </H1>\n"); printf(" <H2>\n"); printf(" ----- Speaker Control </H2>"); printf(" <HR><PRE>"); printf(" <FORM METHOD=\"POST\" ACTION=\"/cgi-bin/chcgi/rtai/fifo_app_cgi.ch\">"); printf(" Frequency: <INPUT NAME=\"x\" VALUE=\"%d\" SIZE=\"10\">", command.freq); printf(" <p>\n"); printf(" Turn on /off speaker\n"); printf(" <L>\n"); if(command.command==SOUND_OFF){ printf(" <D> <INPUT TYPE=\"radio\" NAME=\"onoff\" VALUE=\"On\"> <I>On.</I>\n"); printf(" <D> <INPUT TYPE=\"radio\" NAME=\"onoff\" VALUE=\"Off\" CHECKED> <I>Off.</I>\n"); } else{ printf(" <D> <INPUT TYPE=\"radio\" NAME=\"onoff\" VALUE=\"On\" CHECKED> <I>On.</I>\n"); printf(" <D> <INPUT TYPE=\"radio\" NAME=\"onoff\" VALUE=\"Off\"> <I>Off.</I>\n"); } printf(" </L>\n"); printf("\n<p>"); printf("<p> Echo information from controlled object:\n"); printf("<p> Success! "); if(status.command==SOUND_ON) printf("status: speaker on, freqency %d, heartbeat %d </p>", status.freq, status.heartbeat); else printf("status: speaker off, freqency %d, heartbeat %d </p>", status.freq, status.heartbeat); printf("<p>\n"); printf(" <INPUT TYPE=submit VALUE=\"start\">\n"); printf("</FORM>\n"); printf("<HR>\n"); printf("</BODY>\n"); printf("</HTML>\n"); Response.end(); close(status_fd); close(command_fd); return retval; }