Difference between revisions of "Application note - Example code for capturing power button event on fitlet2 running Linux"

From fit-PC wiki
Jump to: navigation, search
(Configure power button event handling)   (change visibility)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
* This application note provides an example of how to capture a power button click event on Linux.
 
* This application note provides an example of how to capture a power button click event on Linux.
 
* It has been tested with the below setup:
 
* It has been tested with the below setup:
Device: fitlet2
+
<pre>
CPU: Intel(R) Atom(TM) Processor E3950
+
Device: fitlet2
BIOS: 09/17/2018 American Megatrends Inc. FLT2.0.46.01.00
+
CPU: Intel(R) Atom(TM) Processor E3950
OS: Debian Buster (development)
+
BIOS: 09/17/2018 American Megatrends Inc. FLT2.0.46.01.00
Kernel: 4.13.0-1-amd64
+
OS: Debian Buster (testing)
1. Set BIOS -> Main -> OS selection to [Linux]
+
Kernel: 4.16.0-2-amd64
 +
ISO: debian-buster-DI-alpha3-amd64-netinst.iso
 +
</pre>
  
2. Ignore power button handling  provided by systemd, don't forget to reboot:
+
== fitlet2: required BIOS settings ==
$ sudo sed -i s/".*HandlePowerKey.*"/"HandlePowerKey=ignore"/ /etc/systemd/logind.conf
+
* Press [Delete] button after power-on to enter BIOS settings
$ sudo reboot
+
* Set BIOS -> Main -> OS selection to [Linux]
  
3. Install ACPI related software if not installed yet:
+
== Configure power button event handling ==
$ sudo apt install acpi-support-base acpid
+
* Login as root user (root password required):
 +
<pre>
 +
$ su -
 +
Password:
 +
</pre>
  
$ service acpid status
+
* Ignore power button events handling by systemd, then reboot:
● acpid.service - ACPI event daemon
+
<pre>
  Loaded: loaded (/lib/systemd/system/acpid.service; disabled; vendor preset: enabled)
+
$ sed -i s/".*HandlePowerKey.*"/"HandlePowerKey=ignore"/ /etc/systemd/logind.conf
  Active: active (running) since Tue 2018-10-16 05:56:43 EDT; 1s ago
+
$ reboot
Main PID: 1754 (acpid)
+
</pre>
    Tasks: 1 (limit: 4915)
+
  CGroup: /system.slice/acpid.service
+
          └─1754 /usr/sbin/acpid
+
  
Oct 16 05:56:43 x86-atp-master systemd[1]: Started ACPI event daemon.
+
* Install ACPI related software:
Oct 16 05:56:43 x86-atp-master acpid[1754]: starting up with netlink and the input layer
+
<pre>
Oct 16 05:56:43 x86-atp-master acpid[1754]: 1 rule loaded
+
$ apt install acpi-support-base acpid
Oct 16 05:56:43 x86-atp-master acpid[1754]: waiting for events: event logging is off
+
</pre>
  
4. Verify acpi events are visible in the system, run acpi_listen tool and press power button 2-3 times:
+
* Verify acpi events are visible in the system, run acpi_listen command and press power button 2-3 times:
$ acpi_listen
+
<pre>
button/power PBTN 00000080 00000000
+
$ acpi_listen
button/power LNXPWRBN:00 00000080 00000007
+
button/power PBTN 00000080 00000000
button/power PBTN 00000080 00000000
+
button/power LNXPWRBN:00 00000080 00000007
button/power LNXPWRBN:00 00000080 00000008
+
button/power PBTN 00000080 00000000
 +
button/power LNXPWRBN:00 00000080 00000008
 +
</pre>
 +
NOTE #1: current power button handler is /etc/acpi/powerbtn-acpi-support.sh, but it does not work for some reason
  
5. Edit /etc/acpi/events/powerbtn-acpi-support:
+
NOTE #2: there are 2 events PBTN and LNXPWRBN, we need to handle only one of them, for example PBTN
event=power (PBTN) 00000080
+
* Create custom power button event rule:
action=/etc/acpi/powerbtn-acpi-support-new.sh
+
<pre>
 +
$ cat > /etc/acpi/events/powerbtn-custom-rule << EOF
 +
event=power (PBTN)
 +
action=/etc/acpi/powerbtn-custom-handler.sh
 +
EOF
 +
</pre>
  
6. Create your custom script named /etc/acpi/powerbtn-acpi-support-new.sh:
+
* Create custom power button event handler and make it executable:
#!/bin/sh
+
<pre>
echo  "Hello from PBTN!" > /dev/tty1
+
$ cat > /etc/acpi/powerbtn-custom-handler.sh << EOF
 +
#!/bin/sh
 +
echo  "Hello from PBTN!" > /dev/tty1
 +
EOF
  
7. Make it executable
+
$ chmod +x /etc/acpi/powerbtn-custom-handler.sh
$ chmod +x /etc/acpi/powerbtn-acpi-support-new.sh
+
</pre>
  
8. Restart acpid service to activate the new functionality
+
* Restart acpid service to activate the new functionality:
$ service acpid restart
+
<pre>
 +
$ service acpid restart
 +
</pre>
  
9. Press power button shortly, you should see:
+
* Press power button shortly, you should see "Hello from PBTN!" on the main console
"Hello from PBTN!" on tty1
+

Latest revision as of 08:34, 17 October 2018

  • This application note provides an example of how to capture a power button click event on Linux.
  • It has been tested with the below setup:
Device: fitlet2
CPU: Intel(R) Atom(TM) Processor E3950
BIOS: 09/17/2018 American Megatrends Inc. FLT2.0.46.01.00
OS: Debian Buster (testing)
Kernel: 4.16.0-2-amd64
ISO: debian-buster-DI-alpha3-amd64-netinst.iso

fitlet2: required BIOS settings

  • Press [Delete] button after power-on to enter BIOS settings
  • Set BIOS -> Main -> OS selection to [Linux]

Configure power button event handling

  • Login as root user (root password required):
$ su -
Password:
  • Ignore power button events handling by systemd, then reboot:
$ sed -i s/".*HandlePowerKey.*"/"HandlePowerKey=ignore"/ /etc/systemd/logind.conf
$ reboot
  • Install ACPI related software:
$ apt install acpi-support-base acpid
  • Verify acpi events are visible in the system, run acpi_listen command and press power button 2-3 times:
$ acpi_listen
button/power PBTN 00000080 00000000
button/power LNXPWRBN:00 00000080 00000007
button/power PBTN 00000080 00000000
button/power LNXPWRBN:00 00000080 00000008

NOTE #1: current power button handler is /etc/acpi/powerbtn-acpi-support.sh, but it does not work for some reason

NOTE #2: there are 2 events PBTN and LNXPWRBN, we need to handle only one of them, for example PBTN

  • Create custom power button event rule:
$ cat > /etc/acpi/events/powerbtn-custom-rule << EOF
event=power (PBTN)
action=/etc/acpi/powerbtn-custom-handler.sh
EOF
  • Create custom power button event handler and make it executable:
$ cat > /etc/acpi/powerbtn-custom-handler.sh << EOF
#!/bin/sh
echo  "Hello from PBTN!" > /dev/tty1
EOF

$ chmod +x /etc/acpi/powerbtn-custom-handler.sh
  • Restart acpid service to activate the new functionality:
$ service acpid restart
  • Press power button shortly, you should see "Hello from PBTN!" on the main console