summaryrefslogtreecommitdiff
path: root/telephony/java/com/android/internal/telephony/gsm/PdpConnection.java
blob: d8d75e1541324a070d5df645aa4238161f27b0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony.gsm;

import android.os.*;
import com.android.internal.telephony.CommandsInterface;
import android.database.Cursor;
import android.provider.Telephony;
import android.util.EventLog;
import android.util.Log;

import java.util.ArrayList;

/**
 * {@hide}
 */
public class PdpConnection extends Handler {

    private static final String LOG_TAG = "GSM";
    private static final boolean DBG  = true;
    private static final boolean FAKE_FAIL = false;

    public enum PdpState {
        ACTIVE,     /* has active pdp context */
        ACTIVATING, /* during connecting process */
        INACTIVE;    /* has empty pdp context */

        public String toString() {
            switch (this) {
                case ACTIVE: return "active";
                case ACTIVATING: return "setting up";
                default: return "inactive";
            }
        }

        public boolean isActive() {
            return this == ACTIVE;
        }

        public boolean isInactive() {
            return this == INACTIVE;
        }
    }

    public enum PdpFailCause {
        NONE,
        BAD_APN,
        BAD_PAP_SECRET,
        BARRED,
        USER_AUTHENTICATION,
        SERVICE_OPTION_NOT_SUPPORTED,
        SERVICE_OPTION_NOT_SUBSCRIBED,
        SIM_LOCKED,
        RADIO_OFF,
        NO_SIGNAL,
        NO_DATA_PLAN,
        RADIO_NOT_AVIALABLE,
        SUSPENED_TEMPORARY,
        RADIO_ERROR_RETRY,
        UNKNOWN;

        public boolean isPermanentFail() {
            return (this == RADIO_OFF);
        }

        public String toString() {
            switch (this) {
                case NONE: return "no error";
                case BAD_APN: return "bad apn";
                case BAD_PAP_SECRET:return "bad pap secret";
                case BARRED: return "barred";
                case USER_AUTHENTICATION: return "error user autentication";
                case SERVICE_OPTION_NOT_SUPPORTED: return "data not supported";
                case SERVICE_OPTION_NOT_SUBSCRIBED: return "datt not subcribed";
                case SIM_LOCKED: return "sim locked";
                case RADIO_OFF: return "radio is off";
                case NO_SIGNAL: return "no signal";
                case NO_DATA_PLAN: return "no data plan";
                case RADIO_NOT_AVIALABLE: return "radio not available";
                case SUSPENED_TEMPORARY: return "suspend temporary";
                case RADIO_ERROR_RETRY: return "transient radio error";
                default: return "unknown data error";
            }
        }
    }

    /** Fail cause of last PDP activate, from RIL_LastPDPActivateFailCause */
    private static final int PDP_FAIL_RIL_BARRED = 8;
    private static final int PDP_FAIL_RIL_BAD_APN = 27;
    private static final int PDP_FAIL_RIL_USER_AUTHENTICATION = 29;
    private static final int PDP_FAIL_RIL_SERVICE_OPTION_NOT_SUPPORTED = 32;
    private static final int PDP_FAIL_RIL_SERVICE_OPTION_NOT_SUBSCRIBED = 33;
    private static final int PDP_FAIL_RIL_ERROR_UNSPECIFIED = 0xffff;

    //***** Event codes
    private static final int EVENT_SETUP_PDP_DONE = 1;
    private static final int EVENT_GET_LAST_FAIL_DONE = 2;
    private static final int EVENT_LINK_STATE_CHANGED = 3;
    private static final int EVENT_DEACTIVATE_DONE = 4;
    private static final int EVENT_FORCE_RETRY = 5;

    //***** Tag IDs for EventLog
    private static final int EVENT_LOG_BAD_DNS_ADDRESS = 50100;

    //***** Instance Variables
    private GSMPhone phone;
    private String pdp_name;
    private PdpState state;
    private Message onConnectCompleted;
    private Message onDisconnect;
    private int cid;
    private long createTime;
    private long lastFailTime;
    private PdpFailCause lastFailCause;
    private ApnSetting apn;
    private String interfaceName;
    private String ipAddress;
    private String gatewayAddress;
    private String[] dnsServers;

    private static final String NULL_IP = "0.0.0.0";

    // dataLink is only used to support pppd link
    DataLink dataLink;
    // receivedDisconnectReq is set when disconnect pdp link during activating
    private boolean receivedDisconnectReq;

    //***** Constructor
    PdpConnection(GSMPhone phone) {
        this.phone = phone;
        this.state = PdpState.INACTIVE;
        onConnectCompleted = null;
        onDisconnect = null;
        this.cid = -1;
        this.createTime = -1;
        this.lastFailTime = -1;
        this.lastFailCause = PdpFailCause.NONE;
        this.apn = null;
        this.dataLink = null;
        receivedDisconnectReq = false;
        this.dnsServers = new String[2];

        if (SystemProperties.get("ro.radio.use-ppp","no").equals("yes")) {
            dataLink = new PppLink(phone.mDataConnection);
            dataLink.setOnLinkChange(this, EVENT_LINK_STATE_CHANGED, null);
        }
    }

    /**
     * Setup PDP connection for provided apn
     * @param apn for this connection
     * @param onCompleted notify success or not after down
     */
    void connect(ApnSetting apn, Message onCompleted) {
        if (DBG) log("Connecting to carrier: '" + apn.carrier
                + "' APN: '" + apn.apn
                + "' proxy: '" + apn.proxy + "' port: '" + apn.port);

        setHttpProxy (apn.proxy, apn.port);

        state = PdpState.ACTIVATING;
        this.apn = apn;
        onConnectCompleted = onCompleted;
        createTime = -1;
        lastFailTime = -1;
        lastFailCause = PdpFailCause.NONE;
        receivedDisconnectReq = false;

        if (FAKE_FAIL) {
            // for debug before baseband implement error in setup PDP
            if (apn.apn.equalsIgnoreCase("badapn")){
                notifyFail(PdpFailCause.BAD_APN, onConnectCompleted);
                return;
            }
        }

        phone.mCM.setupDefaultPDP(apn.apn, apn.user, apn.password,
                obtainMessage(EVENT_SETUP_PDP_DONE));
    }

    void disconnect(Message msg) {
        onDisconnect = msg;
        if (state == PdpState.ACTIVE) {
            if (dataLink != null) {
                dataLink.disconnect();
            }

            if (phone.mCM.getRadioState().isOn()) {
                phone.mCM.deactivateDefaultPDP(cid, obtainMessage(EVENT_DEACTIVATE_DONE, msg));
            }
        } else if (state == PdpState.ACTIVATING) {
            receivedDisconnectReq = true;
        }
    }

    private void
    setHttpProxy(String httpProxy, String httpPort) {
        if (httpProxy == null || httpProxy.length() == 0) {
            phone.setSystemProperty("net.gprs.http-proxy", null);
            return;
        }

        if (httpPort == null || httpPort.length() == 0) {
            httpPort = "8080";     // Default to port 8080
        }

        phone.setSystemProperty("net.gprs.http-proxy",
                "http://" + httpProxy + ":" + httpPort + "/");
    }

    public String toString() {
        return "State=" + state + " Apn=" + apn +
               " create=" + createTime + " lastFail=" + lastFailTime +
               " lastFailCause=" + lastFailCause;
    }

    public long getConnectionTime() {
        return createTime;
    }

    public long getLastFailTime() {
        return lastFailTime;
    }

    public PdpFailCause getLastFailCause() {
        return lastFailCause;
    }

    public ApnSetting getApn() {
        return apn;
    }

    String getInterface() {
        return interfaceName;
    }

    String getIpAddress() {
        return ipAddress;
    }

    String getGatewayAddress() {
        return gatewayAddress;
    }

    String[] getDnsServers() {
        return dnsServers;
    }

    public PdpState getState() {
        return state;
    }

    private void notifyFail(PdpFailCause cause, Message onCompleted) {
        if (onCompleted == null) return;

        state = PdpState.INACTIVE;
        lastFailCause = cause;
        lastFailTime = System.currentTimeMillis();
        onConnectCompleted = null;

        if (DBG) log("Notify PDP fail at " + lastFailTime
                + " due to " + lastFailCause);

        AsyncResult.forMessage(onCompleted, cause, new Exception());
        onCompleted.sendToTarget();
    }

    private void notifySuccess(Message onCompleted) {
        if (onCompleted == null) return;

        state = PdpState.ACTIVE;
        createTime = System.currentTimeMillis();
        onConnectCompleted = null;
        onCompleted.arg1 = cid;

        if (DBG) log("Notify PDP success at " + createTime);

        AsyncResult.forMessage(onCompleted);
        onCompleted.sendToTarget();
    }

    private void notifyDisconnect(Message msg) {
        if (DBG) log("Notify PDP disconnect");
        
        if (msg != null) {
            AsyncResult.forMessage(msg);
            msg.sendToTarget();
        }
        clearSettings();
    }

    void clearSettings() {
        state = PdpState.INACTIVE;
        receivedDisconnectReq = false;
        createTime = -1;
        lastFailTime = -1;
        lastFailCause = PdpFailCause.NONE;
        apn = null;
        onConnectCompleted = null;
        interfaceName = null;
        ipAddress = null;
        gatewayAddress = null;
        dnsServers[0] = null;
        dnsServers[1] = null;
    }

    private void onLinkStateChanged(DataLink.LinkState linkState) {
        switch (linkState) {
            case LINK_UP:
                notifySuccess(onConnectCompleted);
                break;

            case LINK_DOWN:
            case LINK_EXITED:
                phone.mCM.getLastPdpFailCause(
                        obtainMessage (EVENT_GET_LAST_FAIL_DONE));
                break;
        }
    }

    private PdpFailCause getFailCauseFromRequest(int rilCause) {
        PdpFailCause cause;

        switch (rilCause) {
            case PDP_FAIL_RIL_BARRED:
                cause = PdpFailCause.BARRED;
                break;
            case PDP_FAIL_RIL_BAD_APN:
                cause = PdpFailCause.BAD_APN;
                break;
            case PDP_FAIL_RIL_USER_AUTHENTICATION:
                cause = PdpFailCause.USER_AUTHENTICATION;
                break;
            case PDP_FAIL_RIL_SERVICE_OPTION_NOT_SUPPORTED:
                cause = PdpFailCause.SERVICE_OPTION_NOT_SUPPORTED;
                break;
            case PDP_FAIL_RIL_SERVICE_OPTION_NOT_SUBSCRIBED:
                cause = PdpFailCause.SERVICE_OPTION_NOT_SUBSCRIBED;
                break;
            default:
                cause = PdpFailCause.UNKNOWN;
        }
        return cause;
    }


    private void log(String s) {
        Log.d(LOG_TAG, "[PdpConnection] " + s);
    }

    @Override
    public void handleMessage(Message msg) {
        AsyncResult ar;

        switch (msg.what) {
            case EVENT_SETUP_PDP_DONE:
                ar = (AsyncResult) msg.obj;

                if (ar.exception != null) {
                    Log.e(LOG_TAG, "PDP Context Init failed " + ar.exception);
                    
                    if (receivedDisconnectReq) {
                        // Don't bother reporting the error if there's already a
                        // pending disconnect request, since DataConnectionTracker
                        // has already updated its state.
                        notifyDisconnect(onDisconnect);
                    } else {
                        if ( ar.exception instanceof CommandException &&
                                ((CommandException) (ar.exception)).getCommandError()
                                == CommandException.Error.RADIO_NOT_AVAILABLE) {
                            notifyFail(PdpFailCause.RADIO_NOT_AVIALABLE,
                                    onConnectCompleted);
                        } else {
                            phone.mCM.getLastPdpFailCause(
                                    obtainMessage(EVENT_GET_LAST_FAIL_DONE));
                        }
                    }
                } else {
                    if (receivedDisconnectReq) {
                        // Don't bother reporting success if there's already a
                        // pending disconnect request, since DataConnectionTracker
                        // has already updated its state.
                        disconnect(onDisconnect);
                    } else {
                        String[] response = ((String[]) ar.result);
                        cid = Integer.parseInt(response[0]);
                        
                        if (response.length > 2) {
                            interfaceName = response[1];
                            ipAddress = response[2];
                            String prefix = "net." + interfaceName + ".";
                            gatewayAddress = SystemProperties.get(prefix + "gw");
                            dnsServers[0] = SystemProperties.get(prefix + "dns1");
                            dnsServers[1] = SystemProperties.get(prefix + "dns2");
                            if (DBG) {
                                log("interface=" + interfaceName + " ipAddress=" + ipAddress
                                    + " gateway=" + gatewayAddress + " DNS1=" + dnsServers[0]
                                    + " DNS2=" + dnsServers[1]);
                            }

                            if (NULL_IP.equals(dnsServers[0]) && NULL_IP.equals(dnsServers[1])) {
                                // Work around a race condition where QMI does not fill in DNS:
                                // Deactivate PDP and let DataConnectionTracker retry.
                                EventLog.writeEvent(EVENT_LOG_BAD_DNS_ADDRESS, dnsServers[0]);
                                phone.mCM.deactivateDefaultPDP(cid,
                                        obtainMessage(EVENT_FORCE_RETRY));
                                break;
                            }
                        }
                        
                        if (dataLink != null) {
                            dataLink.connect();
                        } else {
                            onLinkStateChanged(DataLink.LinkState.LINK_UP);
                        }
                        
                        if (DBG) log("PDP setup on cid = " + cid);
                    }
                }
                break;
            case EVENT_FORCE_RETRY:
                if (receivedDisconnectReq) {
                    notifyDisconnect(onDisconnect);
                } else {
                    ar = (AsyncResult) msg.obj;
                    notifyFail(PdpFailCause.RADIO_ERROR_RETRY, onConnectCompleted);
                }
                break;
            case EVENT_GET_LAST_FAIL_DONE:
                if (receivedDisconnectReq) {
                    // Don't bother reporting the error if there's already a
                    // pending disconnect request, since DataConnectionTracker
                    // has already updated its state.
                    notifyDisconnect(onDisconnect);
                } else {
                    ar = (AsyncResult) msg.obj;
                    PdpFailCause cause = PdpFailCause.UNKNOWN;
                    
                    if (ar.exception == null) {
                        int rilFailCause = ((int[]) (ar.result))[0];
                        cause = getFailCauseFromRequest(rilFailCause);
                    }
                    notifyFail(cause, onConnectCompleted);
                }

                break;
            case EVENT_LINK_STATE_CHANGED:
                ar = (AsyncResult) msg.obj;
                DataLink.LinkState ls  = (DataLink.LinkState) ar.result;
                onLinkStateChanged(ls);
                break;
            case EVENT_DEACTIVATE_DONE:
                ar = (AsyncResult) msg.obj;
                notifyDisconnect((Message) ar.userObj);
                break;
        }
    }
}