Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 1 | #!/bin/dash |
| 2 | |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # Set the APN to be used when making connections on a cellular service. |
| 8 | |
| 9 | PROGRAM=$(basename $0) |
| 10 | FLAGS_HELP="Usage: |
| 11 | |
| 12 | ${PROGRAM} |
| 13 | |
| 14 | or |
| 15 | |
| 16 | ${PROGRAM} [-n <network_id>] [-u <username] [-p <password] <apn> |
| 17 | |
| 18 | or |
| 19 | |
| 20 | ${PROGRAM} -c |
| 21 | " |
| 22 | |
| 23 | . /usr/share/misc/shflags |
| 24 | |
| 25 | FLIMFLAM=org.chromium.flimflam |
| 26 | IMANAGER=${FLIMFLAM}.Manager |
| 27 | ISERVICE=${FLIMFLAM}.Service |
| 28 | APN_PROPERTY=Cellular.APN |
| 29 | LAST_GOOD_APN_PROPERTY=Cellular.LastGoodAPN |
| 30 | |
| 31 | LIBDIR=/usr/lib64 |
| 32 | if [ ! -d "${LIBDIR}" ]; then |
| 33 | LIBDIR=/usr/lib |
| 34 | fi |
Darin Petkov | d998f2b | 2012-10-24 10:19:29 +0200 | [diff] [blame] | 35 | SET_APN_HELPER=${LIBDIR}/shill/shims/set-apn-helper |
Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 36 | |
| 37 | usage() { |
| 38 | echo $* |
| 39 | echo |
| 40 | flags_help |
| 41 | exit 1 |
| 42 | } |
| 43 | |
| 44 | dbus() { |
| 45 | local object="$1" |
| 46 | local method="$2" |
| 47 | shift 2 |
| 48 | |
| 49 | dbus-send --system --print-reply --fixed --dest="${FLIMFLAM}" \ |
| 50 | "${object}" "${method}" "$@" |
| 51 | } |
| 52 | |
| 53 | get_apn_info() { |
| 54 | local service="$1" |
| 55 | local property="$2" |
| 56 | |
| 57 | dbus "${service}" "${ISERVICE}.GetProperties" 2>/dev/null \ |
| 58 | | sed -n "/${property}/s/.*\///p" |
| 59 | } |
| 60 | |
| 61 | display_apn() { |
| 62 | local service="$1" |
| 63 | local apn="$(get_apn_info ${service} ${LAST_GOOD_APN_PROPERTY})" |
| 64 | |
| 65 | if [ -n "${apn}" ]; then |
| 66 | echo "Last good APN: " ${apn} |
| 67 | exit 0 |
| 68 | fi |
| 69 | |
| 70 | apn="$(get_apn_info ${service} ${APN_PROPERTY})" |
| 71 | if [ -n "${apn}" ]; then |
| 72 | echo "User specified APN: " ${apn} |
| 73 | exit 0 |
| 74 | fi |
| 75 | |
| 76 | echo "No APN." |
| 77 | exit 0 |
| 78 | } |
| 79 | |
| 80 | set_apn() { |
| 81 | local service="$1" |
| 82 | local apn="$2" |
| 83 | local network_id="$3" |
| 84 | local username="$4" |
| 85 | local password="$5" |
| 86 | local args="apn,${apn},network_id,${network_id}" |
| 87 | |
| 88 | if [ -n "${username}" ]; then |
| 89 | args="${args},username,${username}" |
| 90 | fi |
| 91 | |
| 92 | if [ -n "${password}" ]; then |
| 93 | args="${args},password,${password}" |
| 94 | fi |
| 95 | |
| 96 | echo "Setting APN \"${apn}\" for network ID ${network_id} for service ${service}" |
| 97 | ${SET_APN_HELPER} "${service}" "${APN_PROPERTY}" "${args}" |
| 98 | } |
| 99 | |
| 100 | clear_apn() { |
| 101 | local service="$1" |
| 102 | |
| 103 | echo "Clearing APN for service ${service}" |
| 104 | dbus "${service}" "${ISERVICE}.ClearProperty" "string:${APN_PROPERTY}" |
| 105 | } |
| 106 | |
| 107 | get_services() { |
| 108 | dbus / "${IMANAGER}.GetProperties" 2>/dev/null \ |
| 109 | | sed -n "/\/Services\//{s/.* //p}" |
| 110 | } |
| 111 | |
| 112 | get_service_property() { |
| 113 | local service="$1" |
| 114 | local property="$2" |
| 115 | |
| 116 | dbus "${service}" "${ISERVICE}.GetProperties" 2>/dev/null \ |
| 117 | | sed -n "/\/${property}/{s/.* //p}" |
| 118 | } |
| 119 | |
| 120 | get_first_cellular_service() { |
| 121 | local service |
| 122 | local service_type |
| 123 | |
| 124 | for service in $(get_services); do |
| 125 | service_type="$(get_service_property ${service} Type)" |
| 126 | if [ "${service_type}" = "cellular" ]; then |
| 127 | echo "${service}" |
| 128 | break |
| 129 | fi |
| 130 | done |
| 131 | } |
| 132 | |
| 133 | get_cellular_service_network_id() { |
| 134 | local service="$1" |
| 135 | get_service_property "${service}" "Cellular.ServingOperator\/[0-9]\/code" |
| 136 | } |
| 137 | |
| 138 | service="$(get_first_cellular_service)" |
| 139 | if [ -z "${service}" ]; then |
| 140 | echo "No cellular service exists." |
| 141 | exit 1 |
| 142 | fi |
| 143 | |
| 144 | if [ $# -lt 1 ]; then |
| 145 | display_apn "${service}" |
| 146 | fi |
| 147 | |
| 148 | DEFINE_string 'network_id' "" 'network ID (MCCMNC) of the operator with which the APN should be used' 'n' |
| 149 | DEFINE_string 'username' "" 'username to be used with the APN' 'u' |
| 150 | DEFINE_string 'password' "" 'password to be used with the APN' 'p' |
| 151 | DEFINE_boolean 'clear' false 'clear any APN that has been previously set' 'c' |
| 152 | FLAGS "$@" || exit 1 |
| 153 | eval set -- "${FLAGS_ARGV}" |
| 154 | |
| 155 | network_id="${FLAGS_network_id}" |
| 156 | if [ -z "${FLAGS_network_id}" ]; then |
| 157 | network_id="$(get_cellular_service_network_id ${service})" |
| 158 | if [ -z "${network_id}" ]; then |
| 159 | echo "Cannot determine network ID. Use the -n flag to specify it." |
| 160 | exit 1 |
| 161 | fi |
| 162 | fi |
| 163 | |
| 164 | if [ "${FLAGS_clear}" -ne 0 ]; then |
| 165 | if [ $# -lt 1 ]; then |
| 166 | usage "The APN must be specified." |
| 167 | elif [ $# -gt 1 ]; then |
| 168 | usage "Too many arguments." |
| 169 | fi |
| 170 | set_apn "${service}" "$1" "${network_id}" "${FLAGS_username}" "${FLAGS_password}" |
| 171 | else |
| 172 | if [ $# -ne 0 ]; then |
| 173 | usage "Too many arguments." |
| 174 | fi |
| 175 | clear_apn "${service}" |
| 176 | fi |