pref home next
day7: write software


I want to control everything via a website, so I want a C program that communicates with a web apllet.
The communication will go via a text file. The paerserv program will write the status to a file which the webserver can read everytime it is needed.
If the user wants to toggle a value, the parserv program has to be running, so I will use a FIFO there, ParServ will just sit and wait until some data is sent to the FIFO.
The GetStatus function can be a lot shorter, it doesn't need to return a value, and checknumber has no use either. Maybe later it will.
// parserv.c   -  e.boelen  -  17 august 2002
// I have connected 4 flip flops to the parallel port, their outputs will be read
// after that you can toggle the flip flops via the FIFO which can for example be controlled from a website

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h> /* needed for ioperm() */
#include <asm/io.h> /* for outb() and inb() */

#define FIFO_FILE       "/home/eddo/programming/MYFIFO"
#define STAT_FILE       "/home/eddo/programming/stat.txt"
#define PORT 0x378

int main(int argc, char *argv[])
{
  int GetStatus(int,int);
  void Toggle(int,int), print_version(), usage();
  int portnr,silent,noreceive,i,status;
  FILE *fp;
  char readbuf[80];
  for (i=1;i<argc;i++) {
        	if (argv[i][0] == '-') {
		  switch (argv[i][1]) {
       		    case 'd':
         	      //DaemonMode=1;
         	      //init_daemon();
         	    break;
       		    case 'n':
		      noreceive=1;
                    break;
	            case 's':
		      silent=1;
                    break;
       		    case 'v':
         	      print_version();
         	    break;
       		    case 'h':
         	      usage();
       		    case '-':
			if (strncmp ("version", ( argv[i]+2), 7) == 0)
			print_version ();
			else if (strcmp ("help", ( argv[i]+2)) == 0)
			usage ();
		    break;
      			}
		}
	}

 	/* Create the FIFO if it does not exist */
        umask(0);
        mknod(FIFO_FILE, S_IFIFO|0666, 0);
	if(silent!=1) printf("server running, accepting data on %s , send signals to port %x\n",FIFO_FILE,PORT);
        while(1)  // endless loop, prints data when recieved
        {
                fp = fopen(FIFO_FILE, "r");
                fgets(readbuf, 80, fp);
                if(silent!=1)printf("Received string.. %s \n", readbuf);
                fclose(fp);
		portnr=atoi(readbuf);
		if (portnr<=0 || portnr>=5){
		printf("portnr out of range : %d should be 1 to 4\nChecking status only\n",portnr);
		status = GetStatus(1,silent);
		}
		else{
			// printf("checking status  \n");
			status = GetStatus(portnr,silent);
			if (status!=0){
			  if(silent!=1) printf("status port %d is on, turning off...\n",portnr);
			  Toggle(portnr,silent);
			}
			else{
			  if(silent!=1) printf("status port %d is off, turning on...\n",portnr);
			  Toggle(portnr,silent);
			}
			usleep(100);
			status = GetStatus(portnr,silent);
		}
	}
        return(0);
}


int GetStatus(int check,int silent){
	int status=0,readport,getdata;
	FILE *fp;
	readport=PORT+1;
	//printf("checking if port %d is ON",check);
	//// read data from parallel port
	if (ioperm(readport,1,1)) {
		printf("Sorry, you were not able to gain access to the ports\n");
		printf("You must be root to control ports\n");
		getdata=0;
	}
	else {
        	getdata=inb(readport);
		getdata=getdata-15;// assume inputs 5,6,7,8 (binair status 15) are always ON
	}

	printf("readport gave: %d \n",getdata);

	fp = fopen(STAT_FILE, "w");
	// decode the binair code and write results to STAT_FILE
	if ( (getdata-128)>=0) {
			printf(" - input 1 - OFF \n");// input 1 is inverted
			fprintf(fp,"0\n");
			getdata=getdata-128;}
	else {
		printf(" - input 1 - ON\n");
		if (check==1) status=1;
		fprintf(fp,"1\n");
		}

	if ( (getdata-64)>=0){
			printf(" - input 2 - ON \n");
			if (check==2) status=1;
			fprintf(fp,"1\n");
			getdata=getdata-64;}
	else {
		printf(" - input 2 - OFF \n");
		fprintf(fp,"0\n"); }

	if ( (getdata-32)>=0){
			printf(" - input 3 - ON \n");
			if (check==3)status=1;
			fprintf(fp,"1\n");
			getdata=getdata-32;}
	else {
		printf(" - input 3 - OFF \n");
		fprintf(fp,"0\n"); }

	if ( (getdata-16)>=0){
			printf(" - input 4 - ON \n \n");
			if (check==3)status=1;
			fprintf(fp,"1\n");}
	else {
		printf(" - input 4 - OFF \n");
		fprintf(fp,"0\n"); }

	fclose(fp);
	return(status);
}

int Toggle(int portnr,int silent){
	int value;
	// figure out what to send
	//printf ("enter a output (1-4) you want to toggle: \n");
	//scanf ("%d",&toggleport);
	//while (toggleport<=0||toggleport>=5){
	//	printf("enter a number between 1 and 4 for the port you want to toggle: \n");
	//	scanf ("%d",&toggleport);
	//}

	if(silent!=1) printf ("sending to port: %d...\n",portnr);
	value=portnr;
	if (portnr==3) value=4; // have to toggle only the 3rd bit which has binairy number 4
	if (portnr==4) value=8; // fourth bit

	//send the data to PORT
	ioperm(PORT,1,1);
	outb(value,PORT);
	sleep(1);
	outb(0,PORT);

	//done
	//printf("done\n");
	sleep(1);

}

void print_version(void){
printf("parallel port server \n"
       "e.boelen	     \n"
       "v0.1 16 may 2002     \n" );
       exit(0);
}

void usage(void){
printf("parallel port server v0.1\n"
       "usage:  [options]    \n"
       "--help  this help\n"
       "--version show version info \n"
       "-d deamon mode (To Be Done)\n"
       "-n no receive, status will be read from file\n"
       "-s silent mode\n"  );
       exit(0);
}