2 import time, socket, os, stat, atexit, errno, struct
3 from datetime import datetime
4 import RPi.GPIO as GPIO
5 SO_PEERCRED = 17 # DO - NOT - TOUCH
6 GPIO.setmode(GPIO.BOARD)
8 # ******** definitions *********
10 # TODO: loglevel, log like a real daemon
12 print (datetime.now(),what)
14 # send to client for information but don't care if it arrives
15 def waynesend (conn, what):
19 log("Couldn't send %s" % str(what))
21 # for command not found: do nothing with the pins and send a "0" to the client
27 # delete a file, don't care if it did not exist in the first place
32 # only ignore error if it was "file didn't exist"
33 if e.errno != errno.ENOENT:
36 # commands: on a pin do a series of timed on/off switches
38 # name is for logging and also used for mapping command names to instances of this class
39 # actionsanddelays is a list of pairs: (bool to set on pin, delay in seconds to wait afterwards)
40 def __init__ (self, name, pinnumber, actionsanddelays):
43 self.todo = actionsanddelays
44 GPIO.setup(pinnumber, GPIO.OUT)
45 log ("Pin %d set to be an output pin for %s." % (pinnumber,name))
46 # actually send the signal to the pins
47 def __call__ (self, conn):
48 for (value,delay) in self.todo:
49 GPIO.output(self.pin, value)
50 log ("%s: Pin %d set to %s." % (self.name,self.pin,str(value)))
55 # ******** configuration *********
58 socketname = "/run/tuer.sock"
59 pinlist = [Pinoutput("open", 12, [(True, 0.3), (False, 5.0)]),
60 Pinoutput("close", 16, [(True, 0.3), (False, 5.0)]),
61 Pinoutput("buzz", 22, [(True, 2.0), (False, 0.1)])]
64 # ******** main *********
65 # at the end do a cleanup
66 atexit.register(GPIO.cleanup);
68 # convert list of pin objects to dictionary for command lookup
71 pindict[pin.name.encode()] = pin
74 sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
75 # delete old socket file and don't bitch around if it's not there
77 # bind socket to file name
78 sock.bind (socketname)
79 # ensure we close and delete the socket when we quit (atexit.register is LIFO!)
80 atexit.register(forcerm, socketname)
81 atexit.register(sock.close)
82 # allow only users in the tuergroup to write to the socket
83 os.chown (socketname, 0, tuergroupid)
84 os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
85 # listen to the people, but not too many at once
89 # FIXME: DoS by opening socket but not sending data, because this loop is single threaded; maybe settimeout helps a bit.
92 conn, addr = sock.accept()
93 # get peer information (TODO use it for logging)
94 (pid, uid, gid) = (struct.unpack('3i', conn.getsockopt(socket.SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i'))))
95 # get some data from the client (enough to hold any valid command)
98 log("received command: %s" % str(data))
99 # lookup the command, if it's not in the dict, use the doNothing function instead
100 # and execute the looked up command or doNothing with the connection, so it can respond to the client
101 pindict.get(data,doNothing)(conn)
102 # close connection cleanly