Navigation
Home
gpl
lampd
v1.0
lamp.c








































lamp.c
   
   /*
    * lamp.c
    *
    *  Written by Daan Vreeken
    *   Danovitsch@hotmail.com
    *   (or Danovitsch@Danovitsch.dnsq.org)
    *
    *  Published under the terms of the GNU Public License 2.0
    *  (or any later version)
    *
    */
   
   
   #include "lampd.h"
   
   
   
   
   #define Lamps			3
   
   
   struct SAction
   {
   	char		Name[10];
   	unsigned char	State;
   };
   
   const struct SAction		Cmds[] = {"off",Off,"on",On,"blink",Blink,"",0};
   
   const struct SAction		Options[] = {"default",FlagDefault,"get",FlagGet,"",0};
   
   
   
   
   
   void HandlePacket(int Sock)
   {
   	struct sockaddr_in	InAddr;
   	int			InAddrSize;
   
   	char			Response[2000];
   	int			ResponseSize;
   	struct SOutPacket	*Out = (struct SOutPacket *)&Response;
   	char			*End = (char *)&Response;
   	
   	struct SAction		*Cmd;
   	
   	
   		
   	ResponseSize = recvfrom(Sock,&Response,sizeof(Response),0,(struct sockaddr *)&InAddr,&InAddrSize);
   	End+=ResponseSize;
   	
   	while ((char *)Out<End)
   	{
   		printf("\n");
   		printf("LampNr: %d\n",Out->LampNr);
   		
   		Cmd=(struct SAction *)&Cmds;
   		while (Cmd->Name[0])
   		{
   			if (Cmd->State==Out->State)
   				printf(" State: %s\n",Cmd->Name);
   			Cmd++;
   		}
   		
   		if (Out->Timeout==0)
   			printf(" (running default)\n");
   		else
   			printf(" Timeout: %d\n",Out->Timeout);
   		
   		Out++;
   	}
   }
   
   
   
   
   int main(int argc, char *argv[])
   {
   	int			Cnt;
   	
   	char			RemoteAddr[20];
   	struct sockaddr_in	Addr;
   	int			Len;
   	int			Sock;
   	
   	int			Flag = 0;
   	
   	struct SAction		*Cmd = (struct SAction *)Cmds;
   	struct SAction		*Option;
   	
   	struct SInPacket	Request;
   	int			LampNr;
   	
   	fd_set			FD;
   	struct timeval		Timeout;
   	int			GotSomething = 1;
   	
   	
   	
   	if (argc<2)
   	{
   		printf("Usage: lamp [remotehost] [lampnr] [state] (other options)\n");
   		exit(1);
   	}
   	
   	
   	Flag=0;
   	Request.NewState=Off;
   	
   	
   	for (Cnt=2; Cnt<argc; Cnt++)
   	{
   		Option=(struct SAction *)&Options;
   		
   		while (Option->Name[0]!=0)
   		{
   			if (!strcasecmp(argv[Cnt],Option->Name))
   			{
   				printf("option: %s\n",Option->Name);
   				Flag|=Option->State;
   			}
   			Option++;
   		}
   		
   		
   		if (!strcasecmp(argv[Cnt],"set"))
   		{
   			printf("option: set\n");
   			
   			Flag|=FlagSet;
   			
   			Cnt++;
   			while (Cmd->Name[0]!=0)
   			{
   				if (!strcasecmp(argv[Cnt],Cmd->Name))
   				{
   					printf(" action: %s\n",Cmd->Name);
   					Request.NewState=Cmd->State;
   				}
   				Cmd++;
   			}
   		}
   	}
   	
   	///Create out udp socket
   	//
   	sprintf(RemoteAddr,"%s",argv[1]);
   	memset(&(Addr.sin_zero),0,8);
   	Addr.sin_family=AF_INET;
   	Addr.sin_port=htons(LampPort);
   	Addr.sin_addr.s_addr=inet_addr(RemoteAddr);
   	Len=sizeof(Addr);
   	
   	Sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
   	if (Sock==-1)
   	{
   		printf("Could not create udp socket!\n");
   		exit(1);
   	}
   
   	sscanf(argv[2],"%d",&LampNr);
   	
   	
   	Request.Cmd=Flag;
   	Request.LampNr=LampNr;
   	if (Request.LampNr>=Lamps)
   	{
   		printf("Wrong lamp number!\n");
   		exit(1);
   	}
   	sendto(Sock,&Request,sizeof(Request),0,(struct sockaddr *)&Addr,Len);
   
   	Timeout.tv_sec=1;
   	Timeout.tv_usec=0;
   
   	///main loop
   	//
   	while (GotSomething)
   	{
   		FD_ZERO(&FD);
   		FD_SET(Sock,&FD);
   		
   		GotSomething=select(Sock+1,&FD,NULL,NULL,&Timeout);
   		
   		if (GotSomething==1)
   		{
   			HandlePacket(Sock);
   		}
   	}
   
   
   	/// close all that we opened and get out of here...
   	//
   	close(Sock);
   }
   
   
   
   
   
   
   
   
   

syntax highlighted by Code2HTML, v. 0.9.1


Email me with questions/comments : Daan <Daan @ pa4dan . nl>