Difference between revisions of "Advanced Wifi/wrapper"

From WebOS Internals
Jump to navigation Jump to search
m (some typos corrections)
m (changes to the script missing $ corrected...)
Line 4: Line 4:
  
 
  #!/bin/sh
 
  #!/bin/sh
+
 
 
  ## This is wrapper around wpa_supplicant binary.
 
  ## This is wrapper around wpa_supplicant binary.
 
  ##
 
  ##
Line 38: Line 38:
 
  ##
 
  ##
 
  ## To use it:
 
  ## To use it:
  ## - first define new network in the configuration file,
+
  ## - first define new network(s) in the configuration file,
 
  ##  use any option You wish, use wpa_supplicant config file format:
 
  ##  use any option You wish, use wpa_supplicant config file format:
 
  ##    every option on single line
 
  ##    every option on single line
Line 47: Line 47:
 
  ##  You just configured, select security type - Open
 
  ##  You just configured, select security type - Open
 
  ## - Enjoy new wifi connection
 
  ## - Enjoy new wifi connection
   
+
  ## - If there are more than one network in the area, pease turn wifi off and on between
 +
##  each new network definition in GUI
 
   
 
   
 
  WPA_CONFIG="/media/internal/.wpa_supplicant.conf"
 
  WPA_CONFIG="/media/internal/.wpa_supplicant.conf"
Line 62: Line 63:
 
  "
 
  "
 
   READING_NETWORK=0
 
   READING_NETWORK=0
  UNKNOWN_NET=0
 
 
   
 
   
 
   # read and apply user configuration
 
   # read and apply user configuration
Line 92: Line 92:
 
  NET_ENTRY=`$WPA_CLI list_networks | grep "$NET_SSID"`
 
  NET_ENTRY=`$WPA_CLI list_networks | grep "$NET_SSID"`
 
  if [ "o$NET_ENTRY" = "o" ]; then
 
  if [ "o$NET_ENTRY" = "o" ]; then
    UNKNOWN_NET=1
+
    # skipp this network, until it'sconfigured via GUI
    NET_ID=`"$WPA_CLI" ADD_NETWORK | tail -n1`
+
    READING_NETWORK=0
  "$WPA_CLI" SET_NETWORK "$NET_ID" "$OPTION" "$VALUE" >/dev/null
 
 
  else
 
  else
 
    NET_ID="${NET_ENTRY%% *}"
 
    NET_ID="${NET_ENTRY%% *}"
Line 114: Line 113:
 
  fi
 
  fi
 
  READING_NETWORK=0
 
  READING_NETWORK=0
UNKNOWN_NET=0
 
 
  unset NET_ENTRY NET_ID NET_STATUS OPTION VALUE
 
  unset NET_ENTRY NET_ID NET_STATUS OPTION VALUE
 
  ;;
 
  ;;
Line 134: Line 132:
 
  if [ "o$1" = "o--SETUP-WAIT-AND-CLEANUP" ]; then
 
  if [ "o$1" = "o--SETUP-WAIT-AND-CLEANUP" ]; then
 
   shift
 
   shift
  /usr/sbin/wpa_supplicant --SLEEP-AND-UPDATE &
 
 
   
 
   
 
   ##just to be sure
 
   ##just to be sure
Line 147: Line 144:
 
     echo "" >>/tmp/.wpa_supplicant.conf
 
     echo "" >>/tmp/.wpa_supplicant.conf
 
     echo "update_config=1" >>/tmp/.wpa_supplicant.conf
 
     echo "update_config=1" >>/tmp/.wpa_supplicant.conf
 +
echo "params: $@" >>/tmp/aaa_params
 
     /usr/sbin/wpa_supplicant.real "$@" -B
 
     /usr/sbin/wpa_supplicant.real "$@" -B
 
     merge_wifi_settings
 
     merge_wifi_settings
Line 152: Line 150:
 
     /usr/sbin/wpa_supplicant.real "$@" -B
 
     /usr/sbin/wpa_supplicant.real "$@" -B
 
   fi
 
   fi
 +
 +
  /usr/sbin/wpa_supplicant --SLEEP-AND-UPDATE &
 
   
 
   
 
   # WAIT
 
   # WAIT
Line 161: Line 161:
 
     pkill wpa_supplicant
 
     pkill wpa_supplicant
 
   fi
 
   fi
    rm -rf /tmp/.wpa_supplicant.tmp
+
  rm -rf /tmp/.wpa_supplicant.tmp
 
   
 
   
 
  elif [ "o$1" = "o--SLEEP-AND-UPDATE" ]; then
 
  elif [ "o$1" = "o--SLEEP-AND-UPDATE" ]; then
 
   # check some basic stuff - is there executable wpa_cli?
 
   # check some basic stuff - is there executable wpa_cli?
 
   # and does user configuration exists?
 
   # and does user configuration exists?
   if [ -x "$WPA_CLI" -a -f "WPA_CONFIG" ]; then
+
   if [ -x "$WPA_CLI" -a -f "$WPA_CONFIG" ]; then
 
     ## - can we reach wpa_supplicant?
 
     ## - can we reach wpa_supplicant?
 
     "$WPA_CLI" status >/dev/null || exit $?
 
     "$WPA_CLI" status >/dev/null || exit $?
Line 172: Line 172:
 
     while true; do
 
     while true; do
 
       sleep 5
 
       sleep 5
       merge_wifi_settings || exit
+
       merge_wifi_settings
 
     done
 
     done
 
   else
 
   else
Line 180: Line 180:
 
       sleep 30
 
       sleep 30
 
     done
 
     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
 
   fi
 
  else
 
  else

Revision as of 11:22, 6 November 2012

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.


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

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

## no user editable settings belove here
OIFS="$IFS"

## 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 to ssid 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'sconfigured 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
                    # yes, so no need to reconfigure wpa_supplicant
                    READING_NETWORK=0
                  else
                    # no, so we disable network to 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
}


## PmWiFiService will send SIGKILL to wpa_supplicant process, which could
## leave wpa_supplicant.real running and cause problems later.
## 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
echo "params: $@" >>/tmp/aaa_params
    /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 11:04, 1 November 2012 (PDT)