Skip to content

Commit

Permalink
Split reboot (#11)
Browse files Browse the repository at this point in the history
* better comments in services

* Update pidesktop-powerkey.service

* Update pidesktop-shutdown.service

* Create pidesktop-reboot.service

* Update pidesktop-reboot.service

* Update pidesktop-shutdown.service

* Update postinst

add reboot service

* Update postrm

add reboot service

* Create pd-reboot.py

new reboot service file

* split out reboot service
  • Loading branch information
hoopsurfer authored Apr 8, 2018
1 parent 9665baa commit 2f1bd05
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 28 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ clean: pidesktop-base.deb
install: pidesktop-base.deb
sudo dpkg -i pidesktop-base.deb

test:
cp pidesktop-base/usr/share/pidesktop/script/pd-check /usr/share/pidesktop/script
cp pidesktop-base/usr/share/pidesktop/script/pd-clonessd /usr/share/pidesktop/script
cp pidesktop-base/usr/share/pidesktop/python/pd-bootssd.py /usr/share/pidesktop/python
cp pidesktop-base/usr/share/pidesktop/python/pd-fixrtc.py /usr/share/pidesktop/python
cp pidesktop-base/usr/share/pidesktop/python/pd-powerkey.py /usr/share/pidesktop/python
cp pidesktop-base/usr/share/pidesktop/python/pd-shutdown.py /usr/share/pidesktop/python

uninstall:
sudo dpkg -r pidesktop-base

Binary file modified pidesktop-base.deb
Binary file not shown.
2 changes: 1 addition & 1 deletion pidesktop-base/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: pidesktop-base
Version: 0.1.4
Version: 0.1.5
Section: free
Priority: optional
Architecture: all
Expand Down
1 change: 1 addition & 0 deletions pidesktop-base/DEBIAN/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sudo ln -sv /usr/share/pidesktop/python/pd-fixrtc.py /usr/bin/pd-fixrtc

# enable new sevices
sudo systemctl enable pidesktop-powerkey.service
sudo systemctl enable pidesktop-reboot.service
sudo systemctl enable pidesktop-shutdown.service
sudo systemctl list-unit-files | grep pidesktop

Expand Down
4 changes: 3 additions & 1 deletion pidesktop-base/DEBIAN/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ sudo rm -f /usr/bin/pd-fixrtc

# stop systemd services
sudo systemctl stop pidesktop-powerkey.service
sudo systemctl stop pidesktop-reboot.service
sudo systemctl stop pidesktop-shutdown.service
sudo systemctl disable pidesktop-powerkey.service
sudo systemctl disable pidesktop-reboot.service
sudo systemctl disable pidesktop-shutdown.service

# remove any residual systemd files
sudo rm -f /lib/systemd/system/pidesktop-*.service
sudo rm -f /etc/systemd/system/*.target.wants/pidesktop-*.service

# other changes to consider... TODO: automate this
echo "NOTE: The file /boot/config.txt may enable RTC"
echo "NOTE: The file /boot/config.txt may leave RTC enabled"
4 changes: 2 additions & 2 deletions pidesktop-base/lib/systemd/system/pidesktop-powerkey.service
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[Unit]
Description=PiDesktop Power Button Service
Description=pidesktop power button service
After=local-fs.target

[Service]
Type=simple
ExecStart=/usr/bin/python /usr/share/pidesktop/python/pd-powerkey.py
ExecStart=/usr/bin/python -u /usr/share/pidesktop/python/pd-powerkey.py

[Install]
WantedBy=multi-user.target
11 changes: 11 additions & 0 deletions pidesktop-base/lib/systemd/system/pidesktop-reboot.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=pidesktop reboot service
DefaultDependencies=no
Before=reboot.target

[Service]
Type=oneshot
ExecStart=/usr/bin/python -u /usr/share/pidesktop/python/pd-reboot.py

[Install]
WantedBy=reboot.target
6 changes: 3 additions & 3 deletions pidesktop-base/lib/systemd/system/pidesktop-shutdown.service
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Unit]
Description=PiDesktop Shutdown Reboot Service
Description=pidesktop shutdown service
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/python /usr/share/pidesktop/python/pd-shutdown.py
ExecStart=/usr/bin/python -u /usr/share/pidesktop/python/pd-shutdown.py

[Install]
WantedBy=reboot.target shutdown.target
WantedBy=shutdown.target
12 changes: 8 additions & 4 deletions pidesktop-base/usr/share/pidesktop/python/pd-powerkey.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/user/bin/env python
#!/usr/bin/env python
#
# pd-powerkey.py - monitor GPIO to detect power key press
# pd-powerkey.py - monitor GPIO to detect power key press from Power MCU (PCU)
#

import RPi.GPIO as GPIO
import time,os,sys

print("pidesktop: power button service initializing")

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31,GPIO.OUT) # Pi to Power MCU - start shutdown timer
GPIO.setup(33,GPIO.IN) # Power MCU to Pi - power key pressed
GPIO.setup(31,GPIO.OUT) # Pi to PCU - start/stop shutdown timer
GPIO.setup(33,GPIO.IN) # PCU to Pi - detect power key pressed

GPIO.output(31,GPIO.LOW) # tell PCU we are alive
GPIO.output(31,GPIO.HIGH) # cause blink by starting shutdown timer
Expand All @@ -18,11 +20,13 @@

# callback function
def powerkey_pressed(channels):
print("pidesktop: power button press detected, initiating shutdown")
os.system("sync")
os.system("shutdown -h now")
sys.exit()

# wait for power key press
print("pidesktop: power button monitor enabled")
GPIO.add_event_detect(33,GPIO.RISING,callback=powerkey_pressed)

# idle - TODO: use wait
Expand Down
25 changes: 25 additions & 0 deletions pidesktop-base/usr/share/pidesktop/python/pd-reboot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/user/bin/env python

# pd-reboot.py - oneshot service so do your thing and exit
#
# We are in reboot processing either because reboot is running.
#
import RPi.GPIO as GPIO
import os

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # Use physical board pin numbering
GPIO.setup(31,GPIO.OUT) # Pi to Power MCU communication
GPIO.setup(33,GPIO.IN) # Power MCU to Pi on power button

# In practice detecting power button press is unfortunately not reliable, message if detected
if GPIO.input(33):
# Power Key was already pressed - shut the system down immediately
print("pidesktop: reboot service unexpected power button detected")
else:
# reboot initiated do whatever is needed on reboot
# GPIO.output(31,GPIO.HIGH) # tell power MCU and exit immediately
print("pidesktop: reboot service active")

# we're done
print("pidesktop: reboot service completed")
25 changes: 18 additions & 7 deletions pidesktop-base/usr/share/pidesktop/python/pd-shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@
# We are in shutdown processing either because shutdown or reboot is running or because
# the power button was pressed. If we're here because the power button was pressed then
# Power MCU is already in Waiting OFF state and will turn off immediately if it sees
# pin 31 go high. If power button has not been pressed we should inform power MCU
# pin 31 go high so avoid that! If power button has not been pressed we should inform power MCU
# shutdown/reboot is taking place so the shutdown timer can start.
#
# Note: The timer will reset when the Pi powers off so the only purpose REALLY for doing
# this is to force a power off if shutdown hangs for some reason - somewhat lame.
# this is to capture the current system time in the real time clock.

import RPi.GPIO as GPIO
import os

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # Use physical board pin numbering
GPIO.setup(31,GPIO.OUT) # Pi to Power MCU communication
GPIO.setup(33,GPIO.IN) # Power MCU to Pi on power button press

# stash the current system clock setting into the RTC hardware
os.system("/sbin/hwclock --systohc")
GPIO.setup(33,GPIO.IN) # Power MCU to Pi on power button

# In practice detecting power button press is unfortunately not reliable, message if detected
if GPIO.input(33):
# Power Key was already pressed - shut the system down immediately
print("pidesktop: shutdown service initated from power button")
else:
# shutdown or reboot not related to power key
GPIO.output(31,GPIO.HIGH) # tell power MCU and exit immediately
# GPIO.output(31,GPIO.HIGH) # tell power MCU and exit immediately
print("pidesktop: shutdown service active")

# unmount SD card to clean up logs
print("pidesktop: shutdown service unmounting SD card")
os.system("umount /dev/mmcblk0p1")

# stash the current system clock setting into the RTC hardware
print("pidesktop: shutdown service saving system clock to hardware clock")
os.system("/sbin/hwclock --systohc")

# we're done
print("pidesktop: shutdown service completed")
18 changes: 9 additions & 9 deletions pidesktop-base/usr/share/pidesktop/script/pd-check
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/bin/bash

# show script and python files
echo -e "pidesktop\n\n Scripts and python files:\n"
ls -laR /usr/share/pidesktop
# show scripts and python files
echo -e "pidesktop\n\n Script files:\n"
ls -laR /usr/share/pidesktop/*

# show links to pd- commands
echo -e "\n Links to commands:\n"
ls -la /usr/bin/pd-*

# show sevices
echo -e "\n Systemd services:\n"
sudo systemctl list-unit-files | grep pidesktop
systemctl --plain --no-legend list-unit-files 'pidesktop-*'

# show RTC hwclock
echo -e "\n Hardware clock:\n"
sudo hwclock -r
echo -e "\n System and hardware clocks:\n"
timedatectl status

# show System clock
echo -e "\n System clock:\n"
date
# show installed version
echo -e "\n Package status:\n"
dpkg -s pidesktop-base

0 comments on commit 2f1bd05

Please sign in to comment.