1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony.gsm;
18
19import android.os.Message;
20import android.util.Log;
21import android.util.Patterns;
22
23import com.android.internal.telephony.DataConnection;
24import com.android.internal.telephony.Phone;
25import com.android.internal.telephony.RILConstants;
26
27/**
28 * {@hide}
29 */
30public class GsmDataConnection extends DataConnection {
31
32    private static final String LOG_TAG = "GSM";
33
34    /** Fail cause of last PDP activate, from RIL_LastPDPActivateFailCause */
35    private static final int PDP_FAIL_OPERATOR_BARRED = 0x08;
36    private static final int PDP_FAIL_INSUFFICIENT_RESOURCES = 0x1A;
37    private static final int PDP_FAIL_MISSING_UKNOWN_APN = 0x1B;
38    private static final int PDP_FAIL_UNKNOWN_PDP_ADDRESS_TYPE = 0x1C;
39    private static final int PDP_FAIL_USER_AUTHENTICATION = 0x1D;
40    private static final int PDP_FAIL_ACTIVATION_REJECT_GGSN = 0x1E;
41    private static final int PDP_FAIL_ACTIVATION_REJECT_UNSPECIFIED = 0x1F;
42    private static final int PDP_FAIL_SERVICE_OPTION_NOT_SUPPORTED = 0x20;
43    private static final int PDP_FAIL_SERVICE_OPTION_NOT_SUBSCRIBED = 0x21;
44    private static final int PDP_FAIL_SERVICE_OPTION_OUT_OF_ORDER = 0x22;
45    private static final int PDP_FAIL_NSAPI_IN_USE      = 0x23;
46    private static final int PDP_FAIL_PROTOCOL_ERRORS   = 0x6F;
47    private static final int PDP_FAIL_ERROR_UNSPECIFIED = 0xffff;
48
49    private static final int PDP_FAIL_REGISTRATION_FAIL = -1;
50    private static final int PDP_FAIL_GPRS_REGISTRATION_FAIL = -2;
51
52    //***** Instance Variables
53    private ApnSetting apn;
54
55    //***** Constructor
56    private GsmDataConnection(GSMPhone phone, String name) {
57        super(phone, name);
58    }
59
60    /**
61     * Create the connection object
62     *
63     * @param phone
64     * @return GsmDataConnection that was created.
65     */
66    static GsmDataConnection makeDataConnection(GSMPhone phone) {
67        synchronized (mCountLock) {
68            mCount += 1;
69        }
70        GsmDataConnection gsmDc = new GsmDataConnection(phone, "GsmDataConnection-" + mCount);
71        gsmDc.start();
72        if (DBG) gsmDc.log("Made " + gsmDc.getName());
73        return gsmDc;
74    }
75
76    /**
77     * Begin setting up a data connection, calls setupDataCall
78     * and the ConnectionParams will be returned with the
79     * EVENT_SETUP_DATA_CONNECTION_DONE AsyncResul.userObj.
80     *
81     * @param cp is the connection parameters
82     */
83    @Override
84    protected
85    void onConnect(ConnectionParams cp) {
86        apn = cp.apn;
87
88        if (DBG) log("Connecting to carrier: '" + apn.carrier
89                + "' APN: '" + apn.apn
90                + "' proxy: '" + apn.proxy + "' port: '" + apn.port);
91
92        setHttpProxy (apn.proxy, apn.port);
93
94        createTime = -1;
95        lastFailTime = -1;
96        lastFailCause = FailCause.NONE;
97
98        // msg.obj will be returned in AsyncResult.userObj;
99        Message msg = obtainMessage(EVENT_SETUP_DATA_CONNECTION_DONE, cp);
100        msg.obj = cp;
101
102        int authType = apn.authType;
103        if (authType == -1) {
104            authType = (apn.user != null) ? RILConstants.SETUP_DATA_AUTH_PAP_CHAP :
105                RILConstants.SETUP_DATA_AUTH_NONE;
106        }
107        phone.mCM.setupDataCall(Integer.toString(RILConstants.SETUP_DATA_TECH_GSM),
108                Integer.toString(RILConstants.DATA_PROFILE_DEFAULT), apn.apn, apn.user,
109                apn.password, Integer.toString(authType), msg);
110    }
111
112    @Override
113    protected void clearSettings() {
114        super.clearSettings();
115        apn = null;
116    }
117
118    @Override
119    public String toString() {
120        return "State=" + getCurrentState().getName() + " Apn=" + apn +
121               " create=" + createTime + " lastFail=" + lastFailTime +
122               " lastFailCause=" + lastFailCause;
123    }
124
125    @Override
126    protected FailCause getFailCauseFromRequest(int rilCause) {
127        FailCause cause;
128
129        switch (rilCause) {
130            case PDP_FAIL_OPERATOR_BARRED:
131                cause = FailCause.OPERATOR_BARRED;
132                break;
133            case PDP_FAIL_INSUFFICIENT_RESOURCES:
134                cause = FailCause.INSUFFICIENT_RESOURCES;
135                break;
136            case PDP_FAIL_MISSING_UKNOWN_APN:
137                cause = FailCause.MISSING_UKNOWN_APN;
138                break;
139            case PDP_FAIL_UNKNOWN_PDP_ADDRESS_TYPE:
140                cause = FailCause.UNKNOWN_PDP_ADDRESS;
141                break;
142            case PDP_FAIL_USER_AUTHENTICATION:
143                cause = FailCause.USER_AUTHENTICATION;
144                break;
145            case PDP_FAIL_ACTIVATION_REJECT_GGSN:
146                cause = FailCause.ACTIVATION_REJECT_GGSN;
147                break;
148            case PDP_FAIL_ACTIVATION_REJECT_UNSPECIFIED:
149                cause = FailCause.ACTIVATION_REJECT_UNSPECIFIED;
150                break;
151            case PDP_FAIL_SERVICE_OPTION_OUT_OF_ORDER:
152                cause = FailCause.SERVICE_OPTION_OUT_OF_ORDER;
153                break;
154            case PDP_FAIL_SERVICE_OPTION_NOT_SUPPORTED:
155                cause = FailCause.SERVICE_OPTION_NOT_SUPPORTED;
156                break;
157            case PDP_FAIL_SERVICE_OPTION_NOT_SUBSCRIBED:
158                cause = FailCause.SERVICE_OPTION_NOT_SUBSCRIBED;
159                break;
160            case PDP_FAIL_NSAPI_IN_USE:
161                cause = FailCause.NSAPI_IN_USE;
162                break;
163            case PDP_FAIL_PROTOCOL_ERRORS:
164                cause = FailCause.PROTOCOL_ERRORS;
165                break;
166            case PDP_FAIL_ERROR_UNSPECIFIED:
167                cause = FailCause.UNKNOWN;
168                break;
169            case PDP_FAIL_REGISTRATION_FAIL:
170                cause = FailCause.REGISTRATION_FAIL;
171                break;
172            case PDP_FAIL_GPRS_REGISTRATION_FAIL:
173                cause = FailCause.GPRS_REGISTRATION_FAIL;
174                break;
175            default:
176                cause = FailCause.UNKNOWN;
177        }
178        return cause;
179    }
180
181    @Override
182    protected boolean isDnsOk(String[] domainNameServers) {
183        if (NULL_IP.equals(dnsServers[0]) && NULL_IP.equals(dnsServers[1])
184                    && !((GSMPhone) phone).isDnsCheckDisabled()) {
185            // Work around a race condition where QMI does not fill in DNS:
186            // Deactivate PDP and let DataConnectionTracker retry.
187            // Do not apply the race condition workaround for MMS APN
188            // if Proxy is an IP-address.
189            // Otherwise, the default APN will not be restored anymore.
190            if (!apn.types[0].equals(Phone.APN_TYPE_MMS)
191                || !isIpAddress(apn.mmsProxy)) {
192                return false;
193            }
194        }
195        return true;
196    }
197
198    @Override
199    protected void log(String s) {
200        Log.d(LOG_TAG, "[" + getName() + "] " + s);
201    }
202
203    public ApnSetting getApn() {
204        return this.apn;
205    }
206
207    private void setHttpProxy(String httpProxy, String httpPort) {
208        if (httpProxy == null || httpProxy.length() == 0) {
209            phone.setSystemProperty("net.gprs.http-proxy", null);
210            return;
211        }
212
213        if (httpPort == null || httpPort.length() == 0) {
214            httpPort = "8080";     // Default to port 8080
215        }
216
217        phone.setSystemProperty("net.gprs.http-proxy",
218                "http://" + httpProxy + ":" + httpPort + "/");
219    }
220
221    private boolean isIpAddress(String address) {
222        if (address == null) return false;
223
224        return Patterns.IP_ADDRESS.matcher(apn.mmsProxy).matches();
225    }
226}
227