gpiopower poweroff

Moderators: odroid, mdrjr

gpiopower poweroff

Unread postby maranov » Tue May 08, 2018 1:56 am

Hi all!

I have a problem getting shutdown button working with my C2 w/ minimal Ubuntu 16.04. I followed this tutorial:
https://wiki.odroid.com/odroid-c2/application_note/gpio/gpio_key_wakeup

And the device wakes up fine, but I can't get the button to also shut it down. The setting via GUI from the tutorial doesn't apply since I'm using the image without any GUI. AFAIK this should be handled by systemd-logind, but setting HandlePowerKey and other settings in /etc/systemd/logind.conf doesn't seem to do anything.

I currently get around the problem by using another GPIO input to shut it down. It works, but it's not ideal. Any ideas? Thanks.

---

The shutdown service, if anyone is interested:

/bin/butoff:
Code: Select all
#!/bin/bash
GPIO_N=219
echo $GPIO_N > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio$GPIO_N/direction
while true; do
    GPIO_VALUE=`cat /sys/class/gpio/gpio$GPIO_N/value`
    if [ $GPIO_VALUE -eq 0 ]; then
        poweroff
    fi
    sleep 1
done


/etc/systemd/system/butoff.service:
Code: Select all
[Unit]
Description=Off button
Requires=network.target

[Service]
Type=simple
ExecStart=/bin/butoff

[Install]
WantedBy=default.target


Make the script executable, enabled & start the service:
Code: Select all
sudo chmod u+x /bin/butoff
sudo systemctl enable butoff
sudo systemctl start butoff
maranov
 
Posts: 2
Joined: Sat May 05, 2018 10:50 pm
languages_spoken: english
ODROIDs: HC-1, C2, C1+

Re: gpiopower poweroff

Unread postby joy » Tue May 08, 2018 11:39 am

Hi maranov,
You're right.
When we stay before at log-in screen, Power button operation with powerkey event doesn't work.

Your workaround looks fine!
But as you mentioned, let's find the better way.
I will also check another solution.
Last edited by joy on Tue May 08, 2018 5:19 pm, edited 2 times in total.
joy
 
Posts: 455
Joined: Fri Oct 02, 2015 1:44 pm
languages_spoken: english
ODROIDs: ODROID-C1+, XU4, X

Re: gpiopower poweroff

Unread postby mad_ady » Tue May 08, 2018 12:23 pm

If you can make the gpio register as a keyboard (with uinput maybe) and have it send KEY_POWEROFF (I don't remember the exact name), then it should work. But ideally the code shouldn't be polling for gpio state, but react to an interrupt generated by the gpio.
The keyboard code from the 3.5" lcd could be used as a starting point.
On xu4 there's also a gpio_keys input device which handles input from the power button. Maybe it can be extended for other systems as well.
User avatar
mad_ady
 
Posts: 3649
Joined: Wed Jul 15, 2015 5:00 pm
Location: Bucharest, Romania
languages_spoken: english
ODROIDs: XU4, C1+, C2

Re: gpiopower poweroff

Unread postby joy » Tue May 08, 2018 3:03 pm

Hi mad_ady,
Thank you for your reply.
As I tried to check key event under the node /dev/event2, the powerkey event comes on Log-in display but it's available to enter poweroff state. :(
But after log-in and entering ubuntu mate main display, powerbutton operation works normally.
So I'm trying to adjust powerbutton event with acpi.
So far, I'm not sure this way is correct and there is no side effect.

Hello maranov,
Could you try the following way using acpi?

1. Install acpi
Code: Select all
$ sudo apt-get install acpid


2. Modify powerbtn.sh
Then, there is a default file of powerbtn.sh in /etc/acpi/.
You need to modify the file.
Code: Select all
$ vi /etc/acpi/powerbtn.sh

Block all of default contents in powerbtn.sh
and add these 2 lines at top of the file.
Code: Select all
# Shuts down as soon as power button is pressed on NUC
/sbin/shutdown -h now "Power button pressed"
exit 0

* reference
https://gist.github.com/ManuelMeraz/27f ... db3cc7c7ae

3. Register the service
Code: Select all
$ sudo systemctl enable acpid.service
$ sudo systemctl start acpid.service


4. Edit /etc/systemd/logind.conf and replace [b]#HandlePowerKey=poweroff with HandlePowerKey=ignore.[/b]
Code: Select all
$ sudo vi /etc/systemd/logind.conf

Code: Select all
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See logind.conf(5) for details.

[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
HandlePowerKey=ignore
#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate
#HandleLidSwitch=suspend
#HandleLidSwitchDocked=ignore
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#HoldoffTimeoutSec=30s
#IdleAction=ignore
#IdleActionSec=30min
#RuntimeDirectorySize=10%
#RemoveIPC=yes
#UserTasksMax=12288


Please note that only power off operation is available even though you edit the power button action as "Ask me" or "Suspend"
using [Power Management Preferences] menu.
joy
 
Posts: 455
Joined: Fri Oct 02, 2015 1:44 pm
languages_spoken: english
ODROIDs: ODROID-C1+, XU4, X

Re: gpiopower poweroff

Unread postby maranov » Tue May 08, 2018 11:19 pm

mad_ady: agreed, I don't like polling & sleep solutions either.

joy: acpid works. Thanks.

I've followed your instructions, just for powerbtn.sh I made a new file instead of modifying the old one:
Code: Select all
sudo mv /etc/acpi/powerbtn.sh /etc/acpi/powerbtn.sh.bak


New /etc/acpi/powerbtn.sh:
Code: Select all
#!/bin/sh

/sbin/shutdown -h now "Power button pressed"
exit 0


And made it executable:
Code: Select all
sudo chmod +x /etc/acpi/powerbtn.sh


I guess this is a systemd limitation?
maranov
 
Posts: 2
Joined: Sat May 05, 2018 10:50 pm
languages_spoken: english
ODROIDs: HC-1, C2, C1+

Re: gpiopower poweroff

Unread postby joy » Wed May 09, 2018 10:22 am

maranov wrote:I guess this is a systemd limitation?

Hi, maranov.
Thank you for sharing the result and your modification.
I will update the relevant wiki page soon.
(updated)
https://wiki.odroid.com/odroid-c2/appli ... 1_-_ubuntu

Well... actually I have no idea about it,
but it seems it depends on logind under systemd.
I've tried to adjust PowerKey-related parameters under logind,
but so far I got no result. :(
joy
 
Posts: 455
Joined: Fri Oct 02, 2015 1:44 pm
languages_spoken: english
ODROIDs: ODROID-C1+, XU4, X


Return to Ubuntu

Who is online

Users browsing this forum: No registered users and 1 guest