Home | History | Annotate | Download | only in dataconnection
      1 /*
      2 * Copyright (C) 2011-2014 MediaTek Inc.
      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.dataconnection;
     18 
     19 import com.android.internal.util.AsyncChannel;
     20 import com.android.internal.util.Protocol;
     21 import com.android.internal.telephony.PhoneConstants;
     22 
     23 import android.os.Message;
     24 import android.util.Log;
     25 
     26 public class DcSwitchAsyncChannel extends AsyncChannel {
     27     private static final boolean DBG = true;
     28     private static final boolean VDBG = false;
     29     private static final String LOG_TAG = "DcSwitchAsyncChannel";
     30 
     31     private int tagId = 0;
     32     private DcSwitchState mDcSwitchState;
     33 
     34     // ***** Event codes for driving the state machine
     35     private static final int BASE = Protocol.BASE_DATA_CONNECTION_TRACKER + 0x00002000;
     36     static final int REQ_CONNECT = BASE + 0;
     37     static final int RSP_CONNECT = BASE + 1;
     38     static final int REQ_DISCONNECT = BASE + 2;
     39     static final int RSP_DISCONNECT = BASE + 3;
     40     static final int REQ_IS_IDLE_STATE = BASE + 4;
     41     static final int RSP_IS_IDLE_STATE = BASE + 5;
     42     static final int REQ_IS_IDLE_OR_DEACTING_STATE = BASE + 6;
     43     static final int RSP_IS_IDLE_OR_DEACTING_STATE = BASE + 7;
     44 
     45     private static final int CMD_TO_STRING_COUNT = RSP_IS_IDLE_OR_DEACTING_STATE - BASE + 1;
     46     private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
     47     static {
     48         sCmdToString[REQ_CONNECT - BASE] = "REQ_CONNECT";
     49         sCmdToString[RSP_CONNECT - BASE] = "RSP_CONNECT";
     50         sCmdToString[REQ_DISCONNECT - BASE] = "REQ_DISCONNECT";
     51         sCmdToString[RSP_DISCONNECT - BASE] = "RSP_DISCONNECT";
     52         sCmdToString[REQ_IS_IDLE_STATE - BASE] = "REQ_IS_IDLE_STATE";
     53         sCmdToString[RSP_IS_IDLE_STATE - BASE] = "RSP_IS_IDLE_STATE";
     54         sCmdToString[REQ_IS_IDLE_OR_DEACTING_STATE - BASE] = "REQ_IS_IDLE_OR_DEACTING_STATE";
     55         sCmdToString[RSP_IS_IDLE_OR_DEACTING_STATE - BASE] = "RSP_IS_IDLE_OR_DEACTING_STATE";
     56     }
     57 
     58     protected static String cmdToString(int cmd) {
     59         cmd -= BASE;
     60         if ((cmd >= 0) && (cmd < sCmdToString.length)) {
     61             return sCmdToString[cmd];
     62         } else {
     63             return AsyncChannel.cmdToString(cmd + BASE);
     64         }
     65     }
     66 
     67     public DcSwitchAsyncChannel(DcSwitchState dcSwitchState, int id) {
     68         mDcSwitchState = dcSwitchState;
     69         tagId = id;
     70     }
     71 
     72     public void reqConnect(String type) {
     73         sendMessage(REQ_CONNECT, type);
     74         if (DBG) log("reqConnect");
     75     }
     76 
     77     public int rspConnect(Message response) {
     78         int retVal = response.arg1;
     79         if (DBG) log("rspConnect=" + retVal);
     80         return retVal;
     81     }
     82 
     83     public int connectSync(String type) {
     84         Message response = sendMessageSynchronously(REQ_CONNECT, type);
     85         if ((response != null) && (response.what == RSP_CONNECT)) {
     86             return rspConnect(response);
     87         } else {
     88             log("rspConnect error response=" + response);
     89             return PhoneConstants.APN_REQUEST_FAILED;
     90         }
     91     }
     92 
     93     public void reqDisconnect(String type) {
     94         sendMessage(REQ_DISCONNECT, type);
     95         if (DBG) log("reqDisconnect");
     96     }
     97 
     98     public int rspDisconnect(Message response) {
     99         int retVal = response.arg1;
    100         if (DBG) log("rspDisconnect=" + retVal);
    101         return retVal;
    102     }
    103 
    104     public int disconnectSync(String type) {
    105         Message response = sendMessageSynchronously(REQ_DISCONNECT, type);
    106         if ((response != null) && (response.what == RSP_DISCONNECT)) {
    107             return rspDisconnect(response);
    108         } else {
    109             log("rspDisconnect error response=" + response);
    110             return PhoneConstants.APN_REQUEST_FAILED;
    111         }
    112     }
    113 
    114     public void reqIsIdle() {
    115         sendMessage(REQ_IS_IDLE_STATE);
    116         if (DBG) log("reqIsIdle");
    117     }
    118 
    119     public boolean rspIsIdle(Message response) {
    120         boolean retVal = response.arg1 == 1;
    121         if (DBG) log("rspIsIdle=" + retVal);
    122         return retVal;
    123     }
    124 
    125     public boolean isIdleSync() {
    126         Message response = sendMessageSynchronously(REQ_IS_IDLE_STATE);
    127         if ((response != null) && (response.what == RSP_IS_IDLE_STATE)) {
    128             return rspIsIdle(response);
    129         } else {
    130             log("rspIsIndle error response=" + response);
    131             return false;
    132         }
    133     }
    134 
    135     public void reqIsIdleOrDeacting() {
    136         sendMessage(REQ_IS_IDLE_OR_DEACTING_STATE);
    137         if (DBG) log("reqIsIdleOrDeacting");
    138     }
    139 
    140     public boolean rspIsIdleOrDeacting(Message response) {
    141         boolean retVal = response.arg1 == 1;
    142         if (DBG) log("rspIsIdleOrDeacting=" + retVal);
    143         return retVal;
    144     }
    145 
    146     public boolean isIdleOrDeactingSync() {
    147         Message response = sendMessageSynchronously(REQ_IS_IDLE_OR_DEACTING_STATE);
    148         if ((response != null) && (response.what == RSP_IS_IDLE_OR_DEACTING_STATE)) {
    149             return rspIsIdleOrDeacting(response);
    150         } else {
    151             log("rspIsIndleOrDeacting error response=" + response);
    152             return false;
    153         }
    154     }
    155 
    156     @Override
    157     public String toString() {
    158         return mDcSwitchState.getName();
    159     }
    160 
    161     private void log(String s) {
    162         Log.d(LOG_TAG, "[DcSwitchAsyncChannel-" + tagId + "]: " + s);
    163     }
    164 }
    165