Skip to content

Commit

Permalink
daemon: initial commit
Browse files Browse the repository at this point in the history
The daemon is running continuously and waits for commands to control all remote plugs.
See daemon.cpp for instructions.
  • Loading branch information
xkonni committed Nov 6, 2012
1 parent 4243239 commit fc73e7c
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
daemon
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
DESCRIPTION = "makefile"
DESCRIPTION = "RCSwitch on Raspberry Pi"
LICENSE = "GPL"
VERSION = 1.0

CC = g++
CFLAGS += -L/usr/local/lib
CFLAGS += -lwiringPi

default: daemon

default:
$(CC) -Wall test.cpp RCSwitch.cpp -o test $(CFLAGS)
daemon: daemon.cpp
$(CC) -Wall daemon.cpp RCSwitch.cpp -o daemon $(CFLAGS)

test: test.cpp
$(CC) -Wall test.cpp RCSwitch.cpp -o test $(CFLAGS)

clean:
rm -f test
rm -f test daemon
183 changes: 183 additions & 0 deletions daemon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* RCSwitch daemon for the Raspberry Pi
*
* Setup
* Power to pin4
* GND to pin6
* DATA to pin7/gpio4
*
* Usage
* send xxxxxyyz to ip:port
* xxxxx encoding
* 00001 for first channel
* yy plug
* 01 for plug A
* z action
* 0:off|1:on|2:status
*
* Examples of remote actions
* Switch plug 01 on 00001 to on
* echo 00001011 | nc 192.168.1.5 12345
*
* Switch plug 01 on 00001 to off
* echo 00001010 | nc 192.168.1.5 12345
*
* Get status of plug 01 on 00001
* echo 00001012 | nc 192.168.1.5 12345
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "daemon.h"
#include "RCSwitch.h"

int main(int argc, char* argv[]) {
/**
* setup RCSwitch
*/
RCSwitch mySwitch = RCSwitch();
usleep(50000);
mySwitch.enableTransmit(4);
usleep(50000);


nPlugs=10;
int nState[nPlugs];
memset(nState, 0, sizeof(nState));

/**
* setup socket
*/
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = PORT;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");

/*
* start listening
*/
while (true) {
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");


if (strlen(buffer) > 8) {
for (int i=0; i<5; i++) {
nGroup[i] = buffer[i];
}
nGroup[5] = '\0';

for (int i=5;i<7; i++) {
nSwitchNumber = buffer[i]-48;
}
nAction = buffer[7]-48;
}
else {
printf("message corrupted or incomplete");
}

/**
* handle messages
*/
int nAddr = getAddr(nGroup, nSwitchNumber);
char msg[13];
if (nAddr > nPlugs)
printf("Switch invalid: %s::%d\n", nGroup, nSwitchNumber);
else {
switch (nAction) {
/**
* off
*/
case 0:
mySwitch.switchOff(nGroup, nSwitchNumber);
nState[nAddr] = 0;
sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]);
n = write(newsockfd,msg,14);
break;
/**
* on
*/
case 1:
mySwitch.switchOn(nGroup, nSwitchNumber);
nState[nAddr] = 1;
sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]);
n = write(newsockfd,msg,14);
break;
/**
* status
*/
case 2:
printf("nState[%d] %d\n", nAddr, nState[nAddr]);
sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]);
n = write(newsockfd,msg,14);
break;
}
}

if (n < 0)
error("ERROR writing to socket");
}

/**
* terminate
*/
close(newsockfd);
close(sockfd);
return 0;
}

/**
* error output
*/
void error(const char *msg)
{
perror(msg);
exit(1);
}

/**
* calculate the array address of the power state
*/
int getAddr(const char* nGroup, int nSwitchNumber) {
int bin = atoi(nGroup);
int base = 1;
int dec = 0;
int digit;

while(bin!=0)
{
digit=bin%10;
dec=dec+digit*base;
bin=bin/10;
base=base*2;
}

return (dec-1)*5+nSwitchNumber-1;
}

8 changes: 8 additions & 0 deletions daemon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
char nGroup[6];
int nSwitchNumber;
int nAction;
int nPlugs;
int PORT = 12345;

void error(const char *msg);
int getAddr(const char* nGroup, int nSwitchNumber);

0 comments on commit fc73e7c

Please sign in to comment.