Advanced Wifi/wrapper

This shell wrapper around wpa_supplicant inject custom wifi settings into running wpa_supplicant. More informations about topic can be found at Advanced_Wifi page.


 * 1) !/bin/sh


 * 1) This is wrapper around wpa_supplicant binary.
 * 2) version 0.0.1 proof of concept
 * 3) no responsibility, no warranty, works for me (Veer 8G, WebOS 2.1.2), YMMV
 * 4) author: Matej Zagiba
 * 5) license: any version of GPL
 * 6) Installation and usage:
 * 7) 1. turn off wifi
 * 8) 2. connect to Your WebOS device and copy original binary file:
 * 9)    # cp /usr/sbin/wpa_supplicant /usr/sbin/wpa_supplicant.real
 * 10) 3. put this file on wpa_supplicant's place and rename it:
 * 11)    # cp /media/internal/wpa_supplicant.wrapper /usr/sbin/wpa_supplicant
 * 12) 4. put customized config in /media/internal/.wpa_supplicant.conf
 * 13)    or any other convenient place. Remember - passwords are stored
 * 14)    in that file. Update configuration file location in WPA_CONFIG variable
 * 15) 5. from http://packages.debian.org/wheezy/wpasupplicant download package
 * 16)    with correct architecture (HP veer uses armel) extract wpa_cli binary
 * 17)    and copy it in place e.g.:
 * 18)    # cp /media/internal/wpa_cli /usr/sbin
 * 19) 6. correct permisions:
 * 20)    # chmod 755 /usr/sbin/wpa_supplicant /usr/sbin/wpa_cli
 * 21) Installation is now completed
 * 22) All networks defined in original wifi application will continue working
 * 23) and can be managed through original interface. Network settings in custom
 * 24) config file will overwrite definitions in built-in application.
 * 25) So for any configuration change in custom networks edit configuration
 * 26) file by Your favorite editor
 * 27) To use it:
 * 28) - first define new network(s) in the configuration file,
 * 29)   use any option You wish, use wpa_supplicant config file format:
 * 30)     every option on single line
 * 31)     "network={" and "}" and comment also on single line,
 * 32)   best way for now is to export settings from another system
 * 33) - then turn on wifi
 * 34) - from WiFi Preferences applet select "Join network" and enter name of the network
 * 35)   You just configured, select security type - Open
 * 36) - Enjoy new wifi connection
 * 37) - If there are more than one network in the area, pease turn wifi off and on between
 * 38)   each new network definition in GUI
 * 1) - then turn on wifi
 * 2) - from WiFi Preferences applet select "Join network" and enter name of the network
 * 3)   You just configured, select security type - Open
 * 4) - Enjoy new wifi connection
 * 5) - If there are more than one network in the area, pease turn wifi off and on between
 * 6)   each new network definition in GUI

WPA_CONFIG="/media/internal/.wpa_supplicant.conf" WPA_CLI="/usr/sbin/wpa_cli"

OIFS="$IFS"
 * 1) no user editable settings belove here


 * 1) define functions

merge_wifi_settings { IFS=" " READING_NETWORK=0

# read and apply user configuration # for LINE_IN in `cat "$WPA_CONFIG"`; do    IFS="$OIFS"

# get rid of leading and trailing blanks and remove comments LINE_IN=`sed -e "s/^space:*//" -e "s/space:*$//" -e "s/^#.*$//" <<EOF $LINE_IN`

# skip empty lines [ "o$LINE_IN" = "o" ] && continue

OPTION="${LINE_IN%%=*}" VALUE="${LINE_IN#*=}"

case "$LINE_IN" in       # start of network block "network={"*)		READING_NETWORK=1		;;	# SSID of the network, fun starts here	# we expect ssid to be first thing set in network block	"ssid="*) NET_SSID="${VALUE#\"}"		NET_SSID="${NET_SSID%\"}" # is such a network allready know by wpa_supplicant? NET_ENTRY=`$WPA_CLI list_networks | grep "$NET_SSID"` if [ "o$NET_ENTRY" = "o" ]; then # skipp this network, until it's configured via GUI READING_NETWORK=0 else NET_ID="${NET_ENTRY%%	*}" NET_STATUS="${NET_ENTRY##*	}" # are we connected to this network? if [ "o$NET_STATUS" = 'o[CURRENT]' ]; then # if yes, no need to reconfigure wpa_supplicant READING_NETWORK=0 else # if no, we disable network to be able modify setting "$WPA_CLI" "DISABLE_NETWORK" "$NET_ID" >/dev/null fi		fi ;;	# end of network block - enable network, if needed and reset variables "}")		if [ "o$READING_NETWORK" = "o1" ]; then		 "$WPA_CLI" "ENABLE_NETWORK" "$NET_ID" >/dev/null		fi		READING_NETWORK=0		unset NET_ENTRY NET_ID NET_STATUS OPTION VALUE		;;	# update current option if reading network definition	*) if [ "o$READING_NETWORK" = "o1" ]; then "$WPA_CLI" SET_NETWORK "$NET_ID" "$OPTION" "$VALUE" >/dev/null fi ;;   esac done }


 * 1) PmWiFiService will send SIGKILL to wpa_supplicant process, which could
 * 2) leave wpa_supplicant.real running and cause problems later.
 * 3) so we do some magic with processes...

if [ "o$1" = "o--SETUP-WAIT-AND-CLEANUP" ]; then shift

##just to be sure if [ -x "$WPA_CLI" ]; then "$WPA_CLI" terminate >/dev/null else pkill wpa_supplicant fi

# SET UP THINGS if [ -f "$WPA_CONFIG" ]; then echo "" >>/tmp/.wpa_supplicant.conf echo "update_config=1" >>/tmp/.wpa_supplicant.conf /usr/sbin/wpa_supplicant.real "$@" -B merge_wifi_settings else /usr/sbin/wpa_supplicant.real "$@" -B fi

/usr/sbin/wpa_supplicant --SLEEP-AND-UPDATE &

# WAIT wait # and CLEAN UP if [ -x "$WPA_CLI" ]; then "$WPA_CLI" terminate >/dev/null else pkill wpa_supplicant fi rm -rf /tmp/.wpa_supplicant.tmp

elif [ "o$1" = "o--SLEEP-AND-UPDATE" ]; then # check some basic stuff - is there executable wpa_cli? # and does user configuration exists? if [ -x "$WPA_CLI" -a -f "$WPA_CONFIG" ]; then ## - can we reach wpa_supplicant? "$WPA_CLI" status >/dev/null || exit $?

while true; do     sleep 5 merge_wifi_settings done else # we still want to be here and do nothing # just waiting to be killed by PmWiFiService ... while true; do     sleep 30 done fi elif [ "o$1" = "o--UPDATE" ]; then # check some basic stuff - is there executable wpa_cli? # and does user configuration exists? if [ -x "$WPA_CLI" -a -f "$WPA_CONFIG" ]; then ## - can we reach wpa_supplicant? "$WPA_CLI" status >/dev/null || exit $?

merge_wifi_settings fi else rm -rf /tmp/.wpa_supplicant.tmp ln -s /usr/sbin/wpa_supplicant /tmp/.wpa_supplicant.tmp exec /tmp/.wpa_supplicant.tmp --SETUP-WAIT-AND-CLEANUP "$@" fi

--Zigi004 03:10, 6 November 2012 (PST)