Home | History | Annotate | Download | only in gsm
      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 
     17 package com.android.internal.telephony.gsm;
     18 
     19 import android.os.Message;
     20 import android.util.Log;
     21 import android.util.Patterns;
     22 import android.text.TextUtils;
     23 
     24 import com.android.internal.telephony.DataConnection;
     25 import com.android.internal.telephony.Phone;
     26 import com.android.internal.telephony.PhoneBase;
     27 import com.android.internal.telephony.RILConstants;
     28 import com.android.internal.telephony.RetryManager;
     29 
     30 /**
     31  * {@hide}
     32  */
     33 public class GsmDataConnection extends DataConnection {
     34 
     35     private static final String LOG_TAG = "GSM";
     36 
     37     //***** Instance Variables
     38     protected int mProfileId = RILConstants.DATA_PROFILE_DEFAULT;
     39     protected String mActiveApnType = Phone.APN_TYPE_DEFAULT;
     40     //***** Constructor
     41     private GsmDataConnection(PhoneBase phone, String name, int id, RetryManager rm) {
     42         super(phone, name, id, rm);
     43     }
     44 
     45     /**
     46      * Create the connection object
     47      *
     48      * @param phone the Phone
     49      * @param id the connection id
     50      * @param rm the RetryManager
     51      * @return GsmDataConnection that was created.
     52      */
     53     static GsmDataConnection makeDataConnection(PhoneBase phone, int id, RetryManager rm) {
     54         synchronized (mCountLock) {
     55             mCount += 1;
     56         }
     57         GsmDataConnection gsmDc = new GsmDataConnection(phone, "GsmDC-" + mCount, id, rm);
     58         gsmDc.start();
     59         if (DBG) gsmDc.log("Made " + gsmDc.getName());
     60         return gsmDc;
     61     }
     62 
     63     /**
     64      * Begin setting up a data connection, calls setupDataCall
     65      * and the ConnectionParams will be returned with the
     66      * EVENT_SETUP_DATA_CONNECTION_DONE AsyncResul.userObj.
     67      *
     68      * @param cp is the connection parameters
     69      */
     70     @Override
     71     protected
     72     void onConnect(ConnectionParams cp) {
     73         mApn = cp.apn;
     74 
     75         if (DBG) log("Connecting to carrier: '" + mApn.carrier
     76                 + "' APN: '" + mApn.apn
     77                 + "' proxy: '" + mApn.proxy + "' port: '" + mApn.port);
     78 
     79         createTime = -1;
     80         lastFailTime = -1;
     81         lastFailCause = FailCause.NONE;
     82 
     83         // msg.obj will be returned in AsyncResult.userObj;
     84         Message msg = obtainMessage(EVENT_SETUP_DATA_CONNECTION_DONE, cp);
     85         msg.obj = cp;
     86 
     87         int authType = mApn.authType;
     88         if (authType == -1) {
     89             authType = (mApn.user != null) ? RILConstants.SETUP_DATA_AUTH_PAP_CHAP :
     90                 RILConstants.SETUP_DATA_AUTH_NONE;
     91         }
     92 
     93         String protocol;
     94         if (phone.getServiceState().getRoaming()) {
     95             protocol = mApn.roamingProtocol;
     96         } else {
     97             protocol = mApn.protocol;
     98         }
     99 
    100         phone.mCM.setupDataCall(
    101                 Integer.toString(getRadioTechnology(RILConstants.SETUP_DATA_TECH_GSM)),
    102                 Integer.toString(mProfileId),
    103                 mApn.apn, mApn.user, mApn.password,
    104                 Integer.toString(authType),
    105                 protocol, msg);
    106     }
    107 
    108     public void setProfileId(int profileId) {
    109         mProfileId = profileId;
    110     }
    111 
    112     public int getProfileId() {
    113         return mProfileId;
    114     }
    115 
    116     public void setActiveApnType(String apnType) {
    117         mActiveApnType = apnType;
    118     }
    119 
    120     @Override
    121     public String toString() {
    122         return "State=" + getCurrentState().getName() + " Apn=" + mApn +
    123                " create=" + createTime + " lastFail=" + lastFailTime +
    124                " lastFailCause=" + lastFailCause;
    125     }
    126 
    127     @Override
    128     protected boolean isDnsOk(String[] domainNameServers) {
    129         if (NULL_IP.equals(domainNameServers[0]) && NULL_IP.equals(domainNameServers[1])
    130                 && !phone.isDnsCheckDisabled()) {
    131             // Work around a race condition where QMI does not fill in DNS:
    132             // Deactivate PDP and let DataConnectionTracker retry.
    133             // Do not apply the race condition workaround for MMS APN
    134             // if Proxy is an IP-address.
    135             // Otherwise, the default APN will not be restored anymore.
    136             if (!mApn.types[0].equals(Phone.APN_TYPE_MMS)
    137                 || !isIpAddress(mApn.mmsProxy)) {
    138                 log(String.format(
    139                         "isDnsOk: return false apn.types[0]=%s APN_TYPE_MMS=%s isIpAddress(%s)=%s",
    140                         mApn.types[0], Phone.APN_TYPE_MMS, mApn.mmsProxy,
    141                         isIpAddress(mApn.mmsProxy)));
    142                 return false;
    143             }
    144         }
    145         return true;
    146     }
    147 
    148     @Override
    149     protected void log(String s) {
    150         Log.d(LOG_TAG, "[" + getName() + "] " + s);
    151     }
    152 
    153     private boolean isIpAddress(String address) {
    154         if (address == null) return false;
    155 
    156         return Patterns.IP_ADDRESS.matcher(address).matches();
    157     }
    158 }
    159