# [SOLVED] How to start wpa_supplicant on rfkill switch flip?

## Etal

Hi everyone,

My laptop has a switch that controls the wireless, and when I don't need it, I like to turn it off. However, the problem is that when I turn it back on, I need to restart wpa_supplicant.

So my question is, is there a way to trigger a script when I flip the switch, like with ACPI events?

Thanks in advance!Last edited by Etal on Sat Sep 11, 2010 9:11 pm; edited 1 time in total

----------

## toralf

Switching that key should give an ACPI event which can be processed in /etc/acpi/defaults.sh.

----------

## Etal

Nope, it gets logged, but not due to ACPI:

```
logger: ACPI event unhandled: video/brightnessdown BRTDN 00000087 00000000

klogd: iwlagn 0000:03:00.0: RF_KILL bit toggled to enable radio.

klogd: usb 1-1.4: new full speed USB device using ehci_hcd and address 11

klogd: iwlagn 0000:03:00.0: RF_KILL bit toggled to disable radio.

klogd: usb 1-1.4: USB disconnect, address 11
```

(It's not a keyboard button, it's a physical switch)

----------

## toralf

 *AM088 wrote:*   

> [code]video/brightnessdown BRTDN 00000087 00000000

 This can be used in the ACPI script, isn't it ?

----------

## Etal

 *toralf wrote:*   

>  *AM088 wrote:*   [code]video/brightnessdown BRTDN 00000087 00000000 This can be used in the ACPI script, isn't it ?

 

No, that's the brightness-down which gets caught by ACPI. I put it there for contrast with the rfkill event - flipping the switch on and off (the other 4 lines) gets caught by syslog, but there is no ACPI event.

----------

## Etal

Well, turns out hardware events are handles by a piece of software called udev. Who would have known?   :Razz: 

After reading Daniel Drakes udev rules guide, I came up with a solution which somewhat works.

In /etc/udev/rules.d/, I created a file:

```
SUBSYSTEM=="rfkill", ATTR{type}=="wlan", RUN+="/etc/udev/rfkill.sh"
```

and the corresponding script (mode 755): Edit: this script is wrong. See fixed one below

```
#!/bin/bash

if [ "${RFKILL_STATE}" == 2 ]; then

        /etc/init.d/wpa_supplicant stop

else

        /etc/init.d/wpa_supplicant start

fi
```

The only problem is that for some reason, every time I flip the switch, two seemingly identical events fire off. When turning off, they both have $RFKILL_STATE equal to 2, but when I turn it off, one has 0 and the other has 1, which is why I check only for the "2". The problem is that things get wierd if I use software rfkill, but I don't really need to do that.Last edited by Etal on Sat Sep 11, 2010 9:07 pm; edited 1 time in total

----------

## toralf

Wel, with a ThinkPad it is much easier b/c it has an ACPI event for that:

```
logger: ACPI event : ibm/hotkey HKEY 00000080 00007000
```

----------

## Etal

Actually this is a ThinkPad, a T410  :Smile: 

There is an ACPI event on Fn+F5 (which does a software-block), but not the little switch on the side.

Also, after reading through some documentation, I found out that the "normal" RFKILL_STATE is "1", while "0" is software-blocked, and "2" is hardware-blocked.

So the fixed script which works with both soft ahnd hard blocks is:

```
#!/bin/bash

if [ "${RFKILL_STATE}" == 1 ]; then

        /etc/init.d/wpa_supplicant start

else # state is 0 or 2

        /etc/init.d/wpa_supplicant stop

fi
```

Now, on to discover more cool things to do with udev   :Very Happy: 

----------

## DONAHUE

THANKS AM088

----------

## Etal

Glad it helped someone else  :Very Happy: 

----------

## mamac

Etal,

How do you got that ${RFKILL_STATE} variable field with a value?

I can see the value in /sys/class/rfkill/rfkill0/uevent, but still this variable is blank here...

----------

