blob: 717026db6ed2dc57c1f1b22e4061b360ac11a9fe [file] [log] [blame]
Darin Petkov2a0614e2012-10-22 15:28:41 +02001#!/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
9PROGRAM=$(basename $0)
10FLAGS_HELP="Usage:
11
12 ${PROGRAM}
13
14or
15
16 ${PROGRAM} [-n <network_id>] [-u <username] [-p <password] <apn>
17
18or
19
20 ${PROGRAM} -c
21"
22
23. /usr/share/misc/shflags
24
25FLIMFLAM=org.chromium.flimflam
26IMANAGER=${FLIMFLAM}.Manager
27ISERVICE=${FLIMFLAM}.Service
28APN_PROPERTY=Cellular.APN
29LAST_GOOD_APN_PROPERTY=Cellular.LastGoodAPN
30
31LIBDIR=/usr/lib64
32if [ ! -d "${LIBDIR}" ]; then
33 LIBDIR=/usr/lib
34fi
Darin Petkovd998f2b2012-10-24 10:19:29 +020035SET_APN_HELPER=${LIBDIR}/shill/shims/set-apn-helper
Darin Petkov2a0614e2012-10-22 15:28:41 +020036
37usage() {
38 echo $*
39 echo
40 flags_help
41 exit 1
42}
43
44dbus() {
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
53get_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
61display_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
80set_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
100clear_apn() {
101 local service="$1"
102
103 echo "Clearing APN for service ${service}"
104 dbus "${service}" "${ISERVICE}.ClearProperty" "string:${APN_PROPERTY}"
105}
106
107get_services() {
108 dbus / "${IMANAGER}.GetProperties" 2>/dev/null \
109 | sed -n "/\/Services\//{s/.* //p}"
110}
111
112get_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
120get_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
133get_cellular_service_network_id() {
134 local service="$1"
135 get_service_property "${service}" "Cellular.ServingOperator\/[0-9]\/code"
136}
137
138service="$(get_first_cellular_service)"
139if [ -z "${service}" ]; then
140 echo "No cellular service exists."
141 exit 1
142fi
143
144if [ $# -lt 1 ]; then
145 display_apn "${service}"
146fi
147
148DEFINE_string 'network_id' "" 'network ID (MCCMNC) of the operator with which the APN should be used' 'n'
149DEFINE_string 'username' "" 'username to be used with the APN' 'u'
150DEFINE_string 'password' "" 'password to be used with the APN' 'p'
151DEFINE_boolean 'clear' false 'clear any APN that has been previously set' 'c'
152FLAGS "$@" || exit 1
153eval set -- "${FLAGS_ARGV}"
154
155network_id="${FLAGS_network_id}"
156if [ -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
162fi
163
164if [ "${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}"
171else
172 if [ $# -ne 0 ]; then
173 usage "Too many arguments."
174 fi
175 clear_apn "${service}"
176fi