ofono_depend() { before interface } _config_vars="$_config_vars modem context_type context_apn context_username context_password" ofono_modem_state() { python - "$@" <<\EOF import dbus import sys bus = dbus.SystemBus() path = sys.argv[1] modem = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.Modem') props = modem.GetProperties() if props['Online']: sys.exit(0) elif props['Powered']: sys.exit(1) sys.exit(2) EOF } ofono_find_context() { python - "$@" <<\EOF import dbus import sys iface = sys.argv[1] bus = dbus.SystemBus() manager = dbus.Interface(bus.get_object('org.ofono', '/'), 'org.ofono.Manager') modems = manager.GetModems() def r(path): print path sys.exit(0) for modem in modems: path = modem[0] connmgr = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.ConnectionManager') ctxs = connmgr.GetContexts() for ctx in ctxs: ctxpath = ctx[0] props = ctx[1] if props['Name'] == iface: r(ctxpath) for k in ('Settings', 'IPv6.Settings'): if not props.has_key(k): continue settings = props[k] if not settings.has_key('Interface'): continue if settings['Interface'] == iface: r(ctxpath) sys.exit(1) EOF } ofono_set_property() { local objpath="$1" type="$2" prop="$3" value="$4" dbus-send \ --system \ --dest=org.ofono \ --type=method_call \ "$objpath" \ "org.ofono.$type.SetProperty" \ string:"$prop" \ variant:"$value" } ofono_set_context_property() { local context="$1" prop="$2" value="$3" ofono_set_property "$context" ConnectionContext "$prop" "$value" } ofono_find_or_create_context() { local iface="$1" modem="$2" context_type="$3" ofono_find_context "$iface" && return path="$( dbus-send \ --system \ --dest=org.ofono \ --type=method_call \ --print-reply=literal \ "$modem" \ org.ofono.ConnectionManager.AddContext \ string:"$context_type" )" ofono_set_context_property $path 'Name' string:"$IFACE" \ || return $? echo $path } ofono_sh_context_settings() { local context="$1" python - "$context" <<\EOF import dbus import sys bus = dbus.SystemBus() path = sys.argv[1] context = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.ConnectionContext') props = context.GetProperties() for p in ('', 'IPv6.'): settings = props['%sSettings' % (p,)] for key in settings.keys(): v = settings[key] if key == 'DomainNameServers': v = ' '.join(v) v = v.replace("'", r"'\''") if p: key = "%s_%s" % (p[:-1], key) print "local s_%s='%s'" % (key, v) EOF } ofono_prepare_static() { local IFVAR="$1" s_Address="$2" s_DomainNameServers="$3" s_Gateway="$4" local nodns nogateway opts eval opts=\$dhcp_${IFVAR} for opt in ${opts}; do case "$opt" in nodns) nodns=1 ;; nogateway) nogateway=1 ;; esac done local config eval config=\"\$config_$IFVAR\" perl -e 'exit shift=~m/\Q'"$s_Address"'\E/' "$config" && eval config_$IFVAR=\"$s_Address \$config_$IFVAR\" if [[ -n "$s_DomainNameServers" && -z "$nodns" ]]; then eval dns_servers_$IFVAR=\"\$dns_servers_$IFVAR $s_DomainNameServers\" fi if [ -z "$nogateway" ]; then [ -n "$s_Gateway" ] && s_Gateway=" via $s_Gateway" local my_metric eval my_metric=\"\$metric_$IFVAR\" [[ -n "$my_metric" && "$my_metric" -gt 0 ]] || my_metric="$metric" [[ -n "$my_metric" && "$my_metric" -gt 0 ]] || my_metric=4000 local routes="$(_get_array "routes_$IFVAR") default$s_Gateway metric $my_metric" eval routes_$IFVAR=\"\$routes\" fi } ofono_pre_start() { [ "${IFACE:0:4}" == 'gprs' ] || return 0 local context context_apn context_password context_type context_username modem modemstate eval context_type=\"\$context_type_${IFVAR}\" [ -n "$context_type" ] || context_type=internet eval context_apn=\"\$context_apn_${IFVAR}\" if [ -z "$context_apn" ]; then eerror "context_apn_$IFVAR is not set!" return 1 fi eval context_username=\"\$context_username_${IFVAR}\" eval context_password=\"\$context_password_${IFVAR}\" eval context_protocol=\"\$context_protocol_${IFVAR}\" eval modem=\"/\$modem_${IFVAR}\" if [ "$modem" = '/' ]; then eerror "modem_$IFVAR is not set!" return 1 fi ofono_modem_state "$modem" modemstate="$?" if [ "$modemstate" -gt 1 ]; then ebegin "Powering on modem '$modem'" if ! ofono_set_property "$modem" Modem Powered boolean:true; then eend 1 return 1 fi # Wait for modem to startup # FIXME: this should actually wait on the state sleep 5 fi if [ "$modemstate" -gt 0 ]; then ebegin "Setting modem '$modem' online" if ! ofono_set_property "$modem" Modem Online boolean:true; then eend 1 return 1 fi # Wait for modem to get online # FIXME: this should actually wait on the state sleep 5 fi ebegin "Activating context" context="$(ofono_find_or_create_context "$IFACE" "$modem" "$context_type")" ofono_set_context_property "$context" AccessPointName string:"$context_apn" ofono_set_context_property "$context" Username string:"$context_username" ofono_set_context_property "$context" Password string:"$context_password" ofono_set_context_property "$context" Protocol string:"$context_protocol" ofono_set_property "$modem" ConnectionManager Powered boolean:true ofono_set_context_property "$context" Active boolean:true # FIXME: this should actually wait on the state sleep 5 eval "$(ofono_sh_context_settings "$context")" if [ "$s_Interface" != "$IFACE" ] && [ "$s_IPv6_Interface" != "$IFACE" ]; then eend 1 eerror "Created interface $s_Interface instead of $IFACE" # FIXME: kill & destroy bad context return 1 fi if [ -n "$s_Interface" ]; then case "$s_Method" in dhcp) local config eval config=\"\$config_$IFVAR\" grep -q 'dhcp' <<<"$config" || eval config_$IFVAR=\"dhcp \$config_$IFVAR\" ;; static) ofono_prepare_static "$IFVAR" "$s_Address" "$s_DomainNameServers" "$s_Gateway" ;; esac fi if [ -n "$s_IPv6_Interface" ]; then ofono_prepare_static "$IFVAR" "$s_IPv6_Address" "$s_IPv6_DomainNameServers" "$s_IPv6_Gateway" fi _up } ofono_post_stop() { [ "${IFACE:0:4}" == 'gprs' ] || return 0 context="$(ofono_find_context "$IFACE")" if [ -z "$context" ]; then eerror "Cannot find context for $IFACE" return 1 fi ofono_set_context_property "$context" Active boolean:false }