diff options
Diffstat (limited to 'recipes-core/multitech/config/chat_wrapper')
-rwxr-xr-x | recipes-core/multitech/config/chat_wrapper | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/recipes-core/multitech/config/chat_wrapper b/recipes-core/multitech/config/chat_wrapper new file mode 100755 index 0000000..9db3188 --- /dev/null +++ b/recipes-core/multitech/config/chat_wrapper @@ -0,0 +1,129 @@ +#!/bin/bash + +# Rules for chat scripts +# No comments allowed at the end of AT+CGDCONT in chat script +# The last AT+CGDCONT= must use the same context as the dialer. +# If desired, The AT+CGDCONT may be prefixed by #MT[[:space:]]+ +# Example: +#MT AT+CGDCONT="IPV6","data","192.168.2.1",0,1,"EXTRA" +# +# If you do not use a comment, the entire AT+CGDCONT command +# must be surrounded by apostrophes. This command will be executed +# twice, once in the chat wrapper, and a 2nd time in the chat +# itself. +# +# The space after "#MT" may be any number including tabs. +# If #MT AT+CGDCONT= is found, only the last one is chosen. +# Any uncommented AT+CGDCONT= is then ignored. +# If there are not #MT AT+CGDCONT= lines, then any line without +# a comment chararacter before AT+CGDONT= is accepted, but only the +# last one in the file. + +NAME=chat_wrapper +CONFIG=/etc/default/${NAME} +function finish +{ + ${LOG} "Launch:" "$@" + exec "$@" + # NOTREACHED +} + +[[ -f $CONFIG ]] || exit 1 + +. ${CONFIG} + +: ${REGWAITTIME:=300} +: ${FINALWAIT:=5} + +: ${LOG:="/usr/bin/logger -t ${NAME} -p daemon.notice"} + +${LOG} Timeout is $REGWAITTIME, execute "$@" +((i=$#)) +chatscript="${!i}" +${LOG} Parsing chat script "$chatscript" + +# CONTEXT is last context string in chat script +CONTEXT=$(egrep "^#MT[[:space:]]+(AT\+CGDCONT=.*)" ${chatscript} | tail -1) +if ((${#CONTEXT} == 0)) ; then + CONTEXT=$(egrep "^[^#]+AT\+CGDCONT=" ${chatscript} | tail -1) + [[ $CONTEXT =~ \'(AT\+CGDCONT=([0-9]+)[^$\']+) ]] +else + [[ $CONTEXT =~ (AT\+CGDCONT=([0-9]+).*)$ ]] +fi + +# CONTEXTNUM is the context number that is configured in the dialer. +CONTEXT="${BASH_REMATCH[1]}" +if ((${#CONTEXT} == 0)) ; then + ${LOG} No context specifiction in the chat script + finish "$@" + # NOTREACHED +fi +((CONTEXTNUM=${BASH_REMATCH[2]})) + + +${LOG} "Using Context ${CONTEXTNUM} based on chat script: ${CONTEXT}" + +# At this point if there is no context number, we can skip everything else. + +# Get Modem's context settings +MCONTEXT=$(/usr/bin/radio-cmd -t10 'AT+CGDCONT?' 2>&1 | tr -d '\r') +[[ $MCONTEXT =~ \+CGDCONT:[[:space:]]+${CONTEXTNUM},\"([^\"]*)\",\"([^\"]*)\",\"([^\"]*)\",([0-9]+),([0-9]+)([^$'\n']*) ]] + +MPDP="${BASH_REMATCH[1]}" +MAPN="${BASH_REMATCH[2]}" +MADDR="${BASH_REMATCH[3]}" +MDCOMP="${BASH_REMATCH[4]}" +MHCOMP="${BASH_REMATCH[5]}" +MFULLBOAT="${BASH_REMATCH[6]}" + +[[ $CONTEXT =~ AT\+CGDCONT=${CONTEXTNUM},\"([^\"]*)\",\"([^\"]*)\"(,\"([^\"]*)\"(,([0-9]+)(,([0-9]+)(,[^\']*))*)*)* ]] +PDP="${BASH_REMATCH[1]}" +APN="${BASH_REMATCH[2]}" +ADDR="${BASH_REMATCH[4]}" # Optional +DCOMP="${BASH_REMATCH[6]}" # Optional +HCOMP="${BASH_REMATCH[8]}" # Optional +FULLBOAT="${BASH_REMATCH[9]}" # Optional + +if ((${#DCOMP} == 0)) ; then + ((DCOMP=0)) # Default +fi +if ((${#HCOMP} == 0)) ; then + ((HCOMP=0)) # Default +fi + +# Only update context on a mismatch between chat and modem. +if [[ $MPDP != $PDP ]] || [[ $MAPN != $APN ]] || \ + [[ $MADDR != $ADDR ]] || ((MDCOMP != DCOMP)) || \ + ((MHCOMP != HCOMP)) || \ + [[ $MFULLBOAT != $FULLBOAT ]] ; then + ${LOG} "Modem context $MPDP,$MAPN,$MADDR,$MDCOMP,$HCOMP does not match chat script" + ${LOG} "$MCONTEXT" + ${LOG} "Dropping registration with carrier to set context" + # Need to deregister + /usr/bin/radio-cmd -t10 'AT+COPS=2' + /usr/bin/radio-cmd -t10 "AT+CGDCONT=${CONTEXTNUM},\"${PDP}\",\"${APN}\",\"${ADDR}\",$DCOMP,${HCOMP}${FULLBOAT}" + /usr/bin/radio-cmd -t10 'AT+COPS=0' + ${LOG} "New context is set. Wait up to $REGWAITTIME seconds to register" + # Wait for registration + uptime=$(cat /proc/uptime) + [[ $uptime =~ ^([^\.]*) ]] + t0=${BASH_REMATCH[1]} + while ! [[ $(/usr/bin/radio-query --netreg) =~ ^REGISTERED$ ]] ; do + sleep 5 + uptime=$(cat /proc/uptime) + [[ $uptime =~ ^([^\.]*) ]] + t1=${BASH_REMATCH[1]} + if ((t1-t0 > REGWAITTIME)) ; then + ${LOG} "$((t1-t0)) seconds has expired" + fi + done + uptime=$(cat /proc/uptime) + [[ $uptime =~ ^([^\.]*) ]] + t1=${BASH_REMATCH[1]} + ${LOG} "Re-registered in $((t1-t0)) seconds -- wait 5 more seconds" + sleep $FINALWAIT +else + ${LOG} "Context $CONTEXTNUM matches and nothing to do." +fi +finish "$@" +# NOTREACHED |