Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Jun 2010 01:51:33 +1000 (EST)
From:      Ian Smith <smithi@nimnet.asn.au>
To:        David DEMELIER <demelier.david@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Automatic shutdown with devd.
Message-ID:  <20100602001405.T27982@sola.nimnet.asn.au>
In-Reply-To: <20100601120131.A070C10657ED@hub.freebsd.org>
References:  <20100601120131.A070C10657ED@hub.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
In freebsd-questions Digest, Vol 313, Issue 4, Message: 26
On Tue, 1 Jun 2010 10:55:08 +0200 David DEMELIER <demelier.david@gmail.com> wrote:

 > I recently asked to make an automatic shutdown when I excess a
 > specific percent. I ran devd with -Dd flags to run in background and
 > when the battery was at a critical state it said :
 > 
 > Processing event '!system=ACPI subsystem=CMBAT type=\_SB_.BAT0 notify=0x80'
 > Pushing table
 > setting system=ACPI
 > setting subsystem=CMBAT
 > setting type=\_SB_.BAT0
 > setting notify=0x80

You should be aware that notify 0x80 for CMBAT indicates 'BST' or 
Battery State Change; you'll get these on shifting to any new state.

You can check the new state with 'sysctl -n hw.acpi.battery.state'. 
'acpiconf -i0' shows a translation between state masks and names.

 > Then I tried (for testing) something like this in my /etc/devd.conf
 > 
 > notify 10 {
 >         match "system"          "ACPI";
 >         match "subsystem"       "CMBAT";
 >         match "notify"          "0x80";
 >         action "logger LETGOSHUTDOWN";
 > };
 > 
 > And then I can see the following output in /var/log/messages :
 > 
 > Jun  1 10:48:54 Melon power_profile: changed to 'performance'

Reflecting your AC line state changing from Battery to AC.  devd.conf 
and /etc/rc.d/power_profile have good clues for handling devd notifies.

 > Jun  1 10:48:56 Melon root: LETGOSHUTDOWN
 > Jun  1 10:49:12 Melon root: LETGOSHUTDOWN
 > Jun  1 10:51:06 Melon last message repeated 2 times
 > 
 > It works, but the problem is that it makes this even the cable is
 > plugged ! i.e : the computer was not powered on so with 3% of
 > remaining time but AC plugged in after booting it (always with the AC
 > plugged in) these messages appears too. I guess the ACPI/CMBAT do not
 > care if there is an AC plugged in or not.

See /sys/dev/acpica/acpi_cmbat.c for the gory details.

Yes, ACAD and CMBAT are independent subsystems, so rather than an inline 
action like "logger .." here, you might follow the examples to run your 
own script, passing the notify to that (if you may also want to check 
for notify 0x81, BIF battery info changes, though these occur rarely)

 > Is there a way to make this conditional to do only if the laptop is
 > not charging, AC plugged in ?

Your script can check whether the AC power is on with:

	AC=`sysctl -n hw.acpi.acline`
	if [ $AC = 1 ]; then
		exit 0		# or whatever, when on AC power
	elif [ $AC = 0 ]; then
		:		# do whatever when on battery	
	else
		:		# AC/Battery state unknown ..
	fi

You could try just logging all state changes for a while; from critical 
charging to charging to high to discharging to critical discharging, I 
think that's the lot .. you can also check hw.acpi.battery.life etc.

However, your system should do an 'emergency suspend' on critical low 
battery anyway .. usually set at 1% capacity but some BIOS will let you 
adjust that (see acpiconf -i0).  Only if suspend/resume works of course.

cheers, Ian



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100602001405.T27982>