2017-03-31 00:03:52 -04:00
|
|
|
#!/usr/bin/env python
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
#
|
|
|
|
# bmcd - BMC Daemon program
|
|
|
|
#
|
|
|
|
# This program works as a background daemon for the RPi BMC. It provides
|
|
|
|
# functionality to interface with the GPIOs in an asynchronous way via two
|
|
|
|
# pipes, one read-only and one write-only from the outside perspective. The read
|
|
|
|
# pipe provides the current system power status while the write pipe listens
|
|
|
|
# for commands.
|
|
|
|
#
|
|
|
|
# Part of the RPiBMC project - (c)2017 Joshua Boniface
|
|
|
|
# This software is licenced under the terms of the GNU GPL version 3. For
|
|
|
|
# details please see LICENSE
|
|
|
|
#
|
|
|
|
|
2017-03-30 13:00:23 -04:00
|
|
|
import socket, os, time, struct
|
2017-03-31 00:03:52 -04:00
|
|
|
from threading import Thread, Event
|
2017-03-30 13:00:23 -04:00
|
|
|
from daemon import runner
|
2017-03-31 00:03:52 -04:00
|
|
|
import RPi.GPIO as GPIO
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# These are GPIOs 1-4
|
2017-03-31 01:26:34 -04:00
|
|
|
gpio_rsw = 17
|
|
|
|
gpio_psw = 18
|
|
|
|
gpio_state = 27
|
|
|
|
gpio_pled = 22
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# The pipes and PID file (sorry I manage this here...)
|
2017-03-30 13:00:23 -04:00
|
|
|
bmcd_state = '/run/bmcd/bmcd.state'
|
|
|
|
bmcd_cmd = '/run/bmcd/bmcd.cmd'
|
|
|
|
pidfile = '/run/bmcd/bmcd.pid'
|
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Event for LED flash (i.e. locator)
|
2017-03-31 00:03:52 -04:00
|
|
|
is_pled_flashing = Event()
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Turn the power LED on
|
2017-03-31 00:03:52 -04:00
|
|
|
def powerled_on():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Turn the power LED off
|
2017-03-31 00:03:52 -04:00
|
|
|
def powerled_off():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Flash the power LED
|
2017-03-31 00:03:52 -04:00
|
|
|
def powerled_flash(is_pled_flashing):
|
|
|
|
while is_pled_flashing.isSet():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(1)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(1)
|
|
|
|
is_pled_flashing.clear()
|
|
|
|
return
|
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Press the power button
|
2017-03-31 00:03:52 -04:00
|
|
|
def powersw_press():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(0.5)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Hold the power button (8 seconds)
|
2017-03-31 00:03:52 -04:00
|
|
|
def powersw_hold():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(8)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Press the reset button
|
2017-03-31 00:03:52 -04:00
|
|
|
def resetsw_press():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_rsw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(0.5)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_rsw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Turn on the locator (subthread)
|
2017-03-31 00:03:52 -04:00
|
|
|
def locate_on():
|
|
|
|
is_pled_flashing.set()
|
|
|
|
t = Thread(name='non-block', target=powerled_flash, args=(is_pled_flashing,))
|
|
|
|
t.start()
|
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Turn off the locator
|
2017-03-31 00:03:52 -04:00
|
|
|
def locate_off():
|
|
|
|
is_pled_flashing.clear()
|
|
|
|
|
2017-06-03 03:03:54 -04:00
|
|
|
# Main loop to read a command from the pipe
|
2017-03-31 00:03:52 -04:00
|
|
|
def readcmd():
|
2017-03-31 01:02:18 -04:00
|
|
|
fcmd = open(bmcd_cmd, 'r+', 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
while True:
|
|
|
|
line = fcmd.readline()
|
|
|
|
try:
|
|
|
|
globals()[line.rstrip()]()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2017-06-03 03:03:54 -04:00
|
|
|
# Main loop to write power state to the pipe
|
2017-03-31 00:03:52 -04:00
|
|
|
def writestate(is_pled_flashing):
|
|
|
|
while True:
|
|
|
|
state_now = GPIO.input(gpio_state)
|
2017-03-31 01:02:18 -04:00
|
|
|
fstate = open(bmcd_state, 'w+', 0)
|
2017-03-31 00:40:10 -04:00
|
|
|
fstate.write(str(state_now) + '\n')
|
2017-03-31 01:02:18 -04:00
|
|
|
fstate.close()
|
2017-03-31 00:03:52 -04:00
|
|
|
if not is_pled_flashing.isSet():
|
|
|
|
if state_now == 1:
|
|
|
|
powerled_on()
|
|
|
|
else:
|
|
|
|
powerled_off()
|
|
|
|
|
|
|
|
time.sleep(1)
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# Main app definiton - sets default states and runs main loop
|
2017-03-30 13:00:23 -04:00
|
|
|
class App():
|
|
|
|
def __init__(self):
|
|
|
|
self.stdin_path = '/dev/null'
|
2017-03-31 01:59:12 -04:00
|
|
|
self.stdout_path = '/var/log/bmcd.log'
|
|
|
|
self.stderr_path = '/var/log/bmcd.log'
|
2017-03-30 13:00:23 -04:00
|
|
|
self.pidfile_path = pidfile
|
|
|
|
self.pidfile_timeout = 5
|
|
|
|
def run(self):
|
|
|
|
if not os.path.exists(bmcd_state):
|
|
|
|
os.mkfifo(bmcd_state)
|
|
|
|
if not os.path.exists(bmcd_cmd):
|
|
|
|
os.mkfifo(bmcd_cmd)
|
|
|
|
|
2017-03-31 01:26:34 -04:00
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.setup(gpio_state, GPIO.IN)
|
2017-03-31 00:03:52 -04:00
|
|
|
GPIO.setup(gpio_psw, GPIO.OUT)
|
2017-03-31 01:26:34 -04:00
|
|
|
GPIO.setup(gpio_rsw, GPIO.OUT)
|
2017-03-31 00:03:52 -04:00
|
|
|
GPIO.setup(gpio_pled, GPIO.OUT)
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-03-31 00:03:52 -04:00
|
|
|
t1 = Thread(target=readcmd)
|
|
|
|
t2 = Thread(target=writestate, args=(is_pled_flashing,))
|
|
|
|
t1.setDaemon(True)
|
|
|
|
t2.setDaemon(True)
|
|
|
|
t1.start()
|
|
|
|
t2.start()
|
|
|
|
while True:
|
|
|
|
pass
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-06-03 02:50:32 -04:00
|
|
|
# App definition - start it up
|
2017-03-30 13:00:23 -04:00
|
|
|
app = App()
|
|
|
|
daemon_runner = runner.DaemonRunner(app)
|
|
|
|
daemon_runner.do_action()
|