Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright (C) 2011 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;
     18 
     19 import com.android.internal.telephony.DataConnection.UpdateLinkPropertyResult;
     20 import com.android.internal.util.AsyncChannel;
     21 import com.android.internal.util.Protocol;
     22 
     23 import android.app.PendingIntent;
     24 import android.net.LinkCapabilities;
     25 import android.net.LinkProperties;
     26 import android.net.ProxyProperties;
     27 import android.os.Message;
     28 
     29 import java.util.ArrayList;
     30 import java.util.Collection;
     31 
     32 /**
     33  * AsyncChannel to a DataConnection
     34  */
     35 public class DataConnectionAc extends AsyncChannel {
     36     private static final boolean DBG = false;
     37     private String mLogTag;
     38 
     39     public DataConnection dataConnection;
     40 
     41     public static final int BASE = Protocol.BASE_DATA_CONNECTION_AC;
     42 
     43     public static final int REQ_IS_INACTIVE = BASE + 0;
     44     public static final int RSP_IS_INACTIVE = BASE + 1;
     45 
     46     public static final int REQ_GET_CID = BASE + 2;
     47     public static final int RSP_GET_CID = BASE + 3;
     48 
     49     public static final int REQ_GET_APNSETTING = BASE + 4;
     50     public static final int RSP_GET_APNSETTING = BASE + 5;
     51 
     52     public static final int REQ_GET_LINK_PROPERTIES = BASE + 6;
     53     public static final int RSP_GET_LINK_PROPERTIES = BASE + 7;
     54 
     55     public static final int REQ_SET_LINK_PROPERTIES_HTTP_PROXY = BASE + 8;
     56     public static final int RSP_SET_LINK_PROPERTIES_HTTP_PROXY = BASE + 9;
     57 
     58     public static final int REQ_GET_LINK_CAPABILITIES = BASE + 10;
     59     public static final int RSP_GET_LINK_CAPABILITIES = BASE + 11;
     60 
     61     public static final int REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE = BASE + 12;
     62     public static final int RSP_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE = BASE + 13;
     63 
     64     public static final int REQ_RESET = BASE + 14;
     65     public static final int RSP_RESET = BASE + 15;
     66 
     67     public static final int REQ_GET_REFCOUNT = BASE + 16;
     68     public static final int RSP_GET_REFCOUNT = BASE + 17;
     69 
     70     public static final int REQ_ADD_APNCONTEXT = BASE + 18;
     71     public static final int RSP_ADD_APNCONTEXT = BASE + 19;
     72 
     73     public static final int REQ_REMOVE_APNCONTEXT = BASE + 20;
     74     public static final int RSP_REMOVE_APNCONTEXT = BASE + 21;
     75 
     76     public static final int REQ_GET_APNCONTEXT_LIST = BASE + 22;
     77     public static final int RSP_GET_APNCONTEXT_LIST = BASE + 23;
     78 
     79     public static final int REQ_SET_RECONNECT_INTENT = BASE + 24;
     80     public static final int RSP_SET_RECONNECT_INTENT = BASE + 25;
     81 
     82     public static final int REQ_GET_RECONNECT_INTENT = BASE + 26;
     83     public static final int RSP_GET_RECONNECT_INTENT = BASE + 27;
     84 
     85     /**
     86      * enum used to notify action taken or necessary to be
     87      * taken after the link property is changed.
     88      */
     89     public enum LinkPropertyChangeAction {
     90         NONE, CHANGED, RESET;
     91 
     92         public static LinkPropertyChangeAction fromInt(int value) {
     93             if (value == NONE.ordinal()) {
     94                 return NONE;
     95             } else if (value == CHANGED.ordinal()) {
     96                 return CHANGED;
     97             } else if (value == RESET.ordinal()) {
     98                 return RESET;
     99             } else {
    100                 throw new RuntimeException("LinkPropertyChangeAction.fromInt: bad value=" + value);
    101             }
    102         }
    103     }
    104 
    105     public DataConnectionAc(DataConnection dc, String logTag) {
    106         dataConnection = dc;
    107         mLogTag = logTag;
    108     }
    109 
    110     /**
    111      * Request if the state machine is in the inactive state.
    112      * Response {@link #rspIsInactive}
    113      */
    114     public void reqIsInactive() {
    115         sendMessage(REQ_IS_INACTIVE);
    116         if (DBG) log("reqIsInactive");
    117     }
    118 
    119     /**
    120      * Evaluate RSP_IS_INACTIVE.
    121      *
    122      * @return true if the state machine is in the inactive state.
    123      */
    124     public boolean rspIsInactive(Message response) {
    125         boolean retVal = response.arg1 == 1;
    126         if (DBG) log("rspIsInactive=" + retVal);
    127         return retVal;
    128     }
    129 
    130     /**
    131      * @return true if the state machine is in the inactive state.
    132      */
    133     public boolean isInactiveSync() {
    134         Message response = sendMessageSynchronously(REQ_IS_INACTIVE);
    135         if ((response != null) && (response.what == RSP_IS_INACTIVE)) {
    136             return rspIsInactive(response);
    137         } else {
    138             log("rspIsInactive error response=" + response);
    139             return false;
    140         }
    141     }
    142 
    143     /**
    144      * Request the Connection ID.
    145      * Response {@link #rspCid}
    146      */
    147     public void reqCid() {
    148         sendMessage(REQ_GET_CID);
    149         if (DBG) log("reqCid");
    150     }
    151 
    152     /**
    153      * Evaluate a RSP_GET_CID message and return the cid.
    154      *
    155      * @param response Message
    156      * @return connection id or -1 if an error
    157      */
    158     public int rspCid(Message response) {
    159         int retVal = response.arg1;
    160         if (DBG) log("rspCid=" + retVal);
    161         return retVal;
    162     }
    163 
    164     /**
    165      * @return connection id or -1 if an error
    166      */
    167     public int getCidSync() {
    168         Message response = sendMessageSynchronously(REQ_GET_CID);
    169         if ((response != null) && (response.what == RSP_GET_CID)) {
    170             return rspCid(response);
    171         } else {
    172             log("rspCid error response=" + response);
    173             return -1;
    174         }
    175     }
    176 
    177     /**
    178      * Request the Reference Count.
    179      * Response {@link #rspRefCount}
    180      */
    181     public void reqRefCount() {
    182         sendMessage(REQ_GET_REFCOUNT);
    183         if (DBG) log("reqRefCount");
    184     }
    185 
    186     /**
    187      * Evaluate a RSP_GET_REFCOUNT message and return the refCount.
    188      *
    189      * @param response Message
    190      * @return ref count or -1 if an error
    191      */
    192     public int rspRefCount(Message response) {
    193         int retVal = response.arg1;
    194         if (DBG) log("rspRefCount=" + retVal);
    195         return retVal;
    196     }
    197 
    198     /**
    199      * @return connection id or -1 if an error
    200      */
    201     public int getRefCountSync() {
    202         Message response = sendMessageSynchronously(REQ_GET_REFCOUNT);
    203         if ((response != null) && (response.what == RSP_GET_REFCOUNT)) {
    204             return rspRefCount(response);
    205         } else {
    206             log("rspRefCount error response=" + response);
    207             return -1;
    208         }
    209     }
    210 
    211     /**
    212      * Request the connections ApnSetting.
    213      * Response {@link #rspApnSetting}
    214      */
    215     public void reqApnSetting() {
    216         sendMessage(REQ_GET_APNSETTING);
    217         if (DBG) log("reqApnSetting");
    218     }
    219 
    220     /**
    221      * Evaluate a RSP_APN_SETTING message and return the ApnSetting.
    222      *
    223      * @param response Message
    224      * @return ApnSetting, maybe null
    225      */
    226     public ApnSetting rspApnSetting(Message response) {
    227         ApnSetting retVal = (ApnSetting) response.obj;
    228         if (DBG) log("rspApnSetting=" + retVal);
    229         return retVal;
    230     }
    231 
    232     /**
    233      * Get the connections ApnSetting.
    234      *
    235      * @return ApnSetting or null if an error
    236      */
    237     public ApnSetting getApnSettingSync() {
    238         Message response = sendMessageSynchronously(REQ_GET_APNSETTING);
    239         if ((response != null) && (response.what == RSP_GET_APNSETTING)) {
    240             return rspApnSetting(response);
    241         } else {
    242             log("getApnSetting error response=" + response);
    243             return null;
    244         }
    245     }
    246 
    247     /**
    248      * Request the connections LinkProperties.
    249      * Response {@link #rspLinkProperties}
    250      */
    251     public void reqLinkProperties() {
    252         sendMessage(REQ_GET_LINK_PROPERTIES);
    253         if (DBG) log("reqLinkProperties");
    254     }
    255 
    256     /**
    257      * Evaluate RSP_GET_LINK_PROPERTIES
    258      *
    259      * @param response
    260      * @return LinkProperties, maybe null.
    261      */
    262     public LinkProperties rspLinkProperties(Message response) {
    263         LinkProperties retVal = (LinkProperties) response.obj;
    264         if (DBG) log("rspLinkProperties=" + retVal);
    265         return retVal;
    266     }
    267 
    268     /**
    269      * Get the connections LinkProperties.
    270      *
    271      * @return LinkProperties or null if an error
    272      */
    273     public LinkProperties getLinkPropertiesSync() {
    274         Message response = sendMessageSynchronously(REQ_GET_LINK_PROPERTIES);
    275         if ((response != null) && (response.what == RSP_GET_LINK_PROPERTIES)) {
    276             return rspLinkProperties(response);
    277         } else {
    278             log("getLinkProperties error response=" + response);
    279             return null;
    280         }
    281     }
    282 
    283     /**
    284      * Request setting the connections LinkProperties.HttpProxy.
    285      * Response RSP_SET_LINK_PROPERTIES when complete.
    286      */
    287     public void reqSetLinkPropertiesHttpProxy(ProxyProperties proxy) {
    288         sendMessage(REQ_SET_LINK_PROPERTIES_HTTP_PROXY, proxy);
    289         if (DBG) log("reqSetLinkPropertiesHttpProxy proxy=" + proxy);
    290     }
    291 
    292     /**
    293      * Set the connections LinkProperties.HttpProxy
    294      */
    295     public void setLinkPropertiesHttpProxySync(ProxyProperties proxy) {
    296         Message response =
    297             sendMessageSynchronously(REQ_SET_LINK_PROPERTIES_HTTP_PROXY, proxy);
    298         if ((response != null) && (response.what == RSP_SET_LINK_PROPERTIES_HTTP_PROXY)) {
    299             if (DBG) log("setLinkPropertiesHttpPoxy ok");
    300         } else {
    301             log("setLinkPropertiesHttpPoxy error response=" + response);
    302         }
    303     }
    304 
    305     /**
    306      * Request update LinkProperties from DataCallState
    307      * Response {@link #rspUpdateLinkPropertiesDataCallState}
    308      */
    309     public void reqUpdateLinkPropertiesDataCallState(DataCallState newState) {
    310         sendMessage(REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE, newState);
    311         if (DBG) log("reqUpdateLinkPropertiesDataCallState");
    312     }
    313 
    314     public UpdateLinkPropertyResult rspUpdateLinkPropertiesDataCallState(Message response) {
    315         UpdateLinkPropertyResult retVal = (UpdateLinkPropertyResult)response.obj;
    316         if (DBG) log("rspUpdateLinkPropertiesState: retVal=" + retVal);
    317         return retVal;
    318     }
    319 
    320     /**
    321      * Update link properties in the data connection
    322      *
    323      * @return the removed and added addresses.
    324      */
    325     public UpdateLinkPropertyResult updateLinkPropertiesDataCallStateSync(DataCallState newState) {
    326         Message response =
    327             sendMessageSynchronously(REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE, newState);
    328         if ((response != null) &&
    329             (response.what == RSP_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE)) {
    330             return rspUpdateLinkPropertiesDataCallState(response);
    331         } else {
    332             log("getLinkProperties error response=" + response);
    333             return new UpdateLinkPropertyResult(new LinkProperties());
    334         }
    335     }
    336 
    337     /**
    338      * Request the connections LinkCapabilities.
    339      * Response {@link #rspLinkCapabilities}
    340      */
    341     public void reqLinkCapabilities() {
    342         sendMessage(REQ_GET_LINK_CAPABILITIES);
    343         if (DBG) log("reqLinkCapabilities");
    344     }
    345 
    346     /**
    347      * Evaluate RSP_GET_LINK_CAPABILITIES
    348      *
    349      * @param response
    350      * @return LinkCapabilites, maybe null.
    351      */
    352     public LinkCapabilities rspLinkCapabilities(Message response) {
    353         LinkCapabilities retVal = (LinkCapabilities) response.obj;
    354         if (DBG) log("rspLinkCapabilities=" + retVal);
    355         return retVal;
    356     }
    357 
    358     /**
    359      * Get the connections LinkCapabilities.
    360      *
    361      * @return LinkCapabilities or null if an error
    362      */
    363     public LinkCapabilities getLinkCapabilitiesSync() {
    364         Message response = sendMessageSynchronously(REQ_GET_LINK_CAPABILITIES);
    365         if ((response != null) && (response.what == RSP_GET_LINK_CAPABILITIES)) {
    366             return rspLinkCapabilities(response);
    367         } else {
    368             log("getLinkCapabilities error response=" + response);
    369             return null;
    370         }
    371     }
    372 
    373     /**
    374      * Request the connections LinkCapabilities.
    375      * Response RSP_RESET when complete
    376      */
    377     public void reqReset() {
    378         sendMessage(REQ_RESET);
    379         if (DBG) log("reqReset");
    380     }
    381 
    382     /**
    383      * Reset the connection and wait for it to complete.
    384      */
    385     public void resetSync() {
    386         Message response = sendMessageSynchronously(REQ_RESET);
    387         if ((response != null) && (response.what == RSP_RESET)) {
    388             if (DBG) log("restSync ok");
    389         } else {
    390             log("restSync error response=" + response);
    391         }
    392     }
    393 
    394     /**
    395      * Request to add ApnContext association.
    396      * Response RSP_ADD_APNCONTEXT when complete.
    397      */
    398     public void reqAddApnContext(ApnContext apnContext) {
    399         Message response = sendMessageSynchronously(REQ_ADD_APNCONTEXT, apnContext);
    400         if (DBG) log("reqAddApnContext");
    401     }
    402 
    403     /**
    404      * Add ApnContext association synchronoulsy.
    405      *
    406      * @param ApnContext to associate
    407      */
    408     public void addApnContextSync(ApnContext apnContext) {
    409         Message response = sendMessageSynchronously(REQ_ADD_APNCONTEXT, apnContext);
    410         if ((response != null) && (response.what == RSP_ADD_APNCONTEXT)) {
    411             if (DBG) log("addApnContext ok");
    412         } else {
    413             log("addApnContext error response=" + response);
    414         }
    415     }
    416 
    417     /**
    418      * Request to remove ApnContext association.
    419      * Response RSP_REMOVE_APNCONTEXT when complete.
    420      */
    421     public void reqRemomveApnContext(ApnContext apnContext) {
    422         Message response = sendMessageSynchronously(REQ_REMOVE_APNCONTEXT, apnContext);
    423         if (DBG) log("reqRemomveApnContext");
    424     }
    425 
    426     /**
    427      * Remove ApnContext associateion.
    428      *
    429      * @param ApnContext to dissociate
    430      */
    431     public void removeApnContextSync(ApnContext apnContext) {
    432         Message response = sendMessageSynchronously(REQ_REMOVE_APNCONTEXT, apnContext);
    433         if ((response != null) && (response.what == RSP_REMOVE_APNCONTEXT)) {
    434             if (DBG) log("removeApnContext ok");
    435         } else {
    436             log("removeApnContext error response=" + response);
    437         }
    438     }
    439 
    440     /**
    441      * Request to retrive ApnContext List associated with DC.
    442      * Response RSP_GET_APNCONTEXT_LIST when complete.
    443      */
    444     public void reqGetApnList(ApnContext apnContext) {
    445         Message response = sendMessageSynchronously(REQ_GET_APNCONTEXT_LIST);
    446         if (DBG) log("reqGetApnList");
    447     }
    448 
    449     /**
    450      * Retrieve Collection of ApnContext from the response message.
    451      *
    452      * @param Message sent from DC in response to REQ_GET_APNCONTEXT_LIST.
    453      * @return Collection of ApnContext
    454      */
    455     public Collection<ApnContext> rspApnList(Message response) {
    456         Collection<ApnContext> retVal = (Collection<ApnContext>)response.obj;
    457         if (retVal == null) retVal = new ArrayList<ApnContext>();
    458         return retVal;
    459     }
    460 
    461     /**
    462      * Retrieve collection of ApnContext currently associated with
    463      * the DataConnectionA synchronously.
    464      *
    465      * @return Collection of ApnContext
    466      */
    467     public Collection<ApnContext> getApnListSync() {
    468         Message response = sendMessageSynchronously(REQ_GET_APNCONTEXT_LIST);
    469         if ((response != null) && (response.what == RSP_GET_APNCONTEXT_LIST)) {
    470             if (DBG) log("getApnList ok");
    471             return rspApnList(response);
    472         } else {
    473             log("getApnList error response=" + response);
    474             // return dummy list with no entry
    475             return new ArrayList<ApnContext>();
    476         }
    477     }
    478 
    479     /**
    480      * Request to set Pending ReconnectIntent to DC.
    481      * Response RSP_SET_RECONNECT_INTENT when complete.
    482      */
    483     public void reqSetReconnectIntent(PendingIntent intent) {
    484         Message response = sendMessageSynchronously(REQ_SET_RECONNECT_INTENT, intent);
    485         if (DBG) log("reqSetReconnectIntent");
    486     }
    487 
    488     /**
    489      * Set pending reconnect intent to DC synchronously.
    490      *
    491      * @param PendingIntent to set.
    492      */
    493     public void setReconnectIntentSync(PendingIntent intent) {
    494         Message response = sendMessageSynchronously(REQ_SET_RECONNECT_INTENT, intent);
    495         if ((response != null) && (response.what == RSP_SET_RECONNECT_INTENT)) {
    496             if (DBG) log("setReconnectIntent ok");
    497         } else {
    498             log("setReconnectIntent error response=" + response);
    499         }
    500     }
    501 
    502     /**
    503      * Request to get Pending ReconnectIntent to DC.
    504      * Response RSP_GET_RECONNECT_INTENT when complete.
    505      */
    506     public void reqGetReconnectIntent() {
    507         Message response = sendMessageSynchronously(REQ_GET_RECONNECT_INTENT);
    508         if (DBG) log("reqGetReconnectIntent");
    509     }
    510 
    511     /**
    512      * Retrieve reconnect intent from response message from DC.
    513      *
    514      * @param Message which contains the reconnect intent.
    515      * @return PendingIntent from the response.
    516      */
    517     public PendingIntent rspReconnectIntent(Message response) {
    518         PendingIntent retVal = (PendingIntent) response.obj;
    519         return retVal;
    520     }
    521 
    522     /**
    523      * Retrieve reconnect intent currently set in DC synchronously.
    524      *
    525      * @return PendingIntent reconnect intent current ly set in DC
    526      */
    527     public PendingIntent getReconnectIntentSync() {
    528         Message response = sendMessageSynchronously(REQ_GET_RECONNECT_INTENT);
    529         if ((response != null) && (response.what == RSP_GET_RECONNECT_INTENT)) {
    530             if (DBG) log("getReconnectIntent ok");
    531             return rspReconnectIntent(response);
    532         } else {
    533             log("getReconnectIntent error response=" + response);
    534             return null;
    535         }
    536     }
    537 
    538     private void log(String s) {
    539         android.util.Log.d(mLogTag, "DataConnectionAc " + s);
    540     }
    541 }
    542