DcSwitchAsyncChannel.java revision bda761320929f714951c328bfec6a51a1978db97
1/*
2 * Copyright (C) 2014 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.dataconnection;
18
19import com.android.internal.util.AsyncChannel;
20import com.android.internal.util.Protocol;
21import com.android.internal.telephony.PhoneConstants;
22
23import android.net.NetworkRequest;
24import android.os.Message;
25import android.telephony.Rlog;
26
27public class DcSwitchAsyncChannel extends AsyncChannel {
28    private static final boolean DBG = true;
29    private static final boolean VDBG = false;
30    private static final String LOG_TAG = "DcSwitchAsyncChannel";
31
32    private int tagId = 0;
33    private DcSwitchStateMachine mDcSwitchState;
34
35    // ***** Event codes for driving the state machine
36    private static final int BASE = Protocol.BASE_DATA_CONNECTION_TRACKER + 0x00002000;
37    static final int REQ_CONNECT = BASE + 0;
38    static final int RSP_CONNECT = BASE + 1;
39    static final int REQ_DISCONNECT = BASE + 2;
40    static final int RSP_DISCONNECT = BASE + 3;
41    static final int REQ_DISCONNECT_ALL = BASE + 4;
42    static final int RSP_DISCONNECT_ALL = BASE + 5;
43    static final int REQ_IS_IDLE_STATE = BASE + 6;
44    static final int RSP_IS_IDLE_STATE = BASE + 7;
45    static final int REQ_IS_IDLE_OR_DETACHING_STATE = BASE + 8;
46    static final int RSP_IS_IDLE_OR_DETACHING_STATE = BASE + 9;
47    static final int EVENT_DATA_ATTACHED = BASE + 10;
48    static final int EVENT_DATA_DETACHED = BASE + 11;
49
50    private static final int CMD_TO_STRING_COUNT = EVENT_DATA_DETACHED - BASE + 1;
51    private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
52    static {
53        sCmdToString[REQ_CONNECT - BASE] = "REQ_CONNECT";
54        sCmdToString[RSP_CONNECT - BASE] = "RSP_CONNECT";
55        sCmdToString[REQ_DISCONNECT - BASE] = "REQ_DISCONNECT";
56        sCmdToString[RSP_DISCONNECT - BASE] = "RSP_DISCONNECT";
57        sCmdToString[REQ_DISCONNECT_ALL - BASE] = "REQ_DISCONNECT_ALL";
58        sCmdToString[RSP_DISCONNECT_ALL - BASE] = "RSP_DISCONNECT_ALL";
59        sCmdToString[REQ_IS_IDLE_STATE - BASE] = "REQ_IS_IDLE_STATE";
60        sCmdToString[RSP_IS_IDLE_STATE - BASE] = "RSP_IS_IDLE_STATE";
61        sCmdToString[REQ_IS_IDLE_OR_DETACHING_STATE - BASE] = "REQ_IS_IDLE_OR_DETACHING_STATE";
62        sCmdToString[RSP_IS_IDLE_OR_DETACHING_STATE - BASE] = "RSP_IS_IDLE_OR_DETACHING_STATE";
63        sCmdToString[EVENT_DATA_ATTACHED - BASE] = "EVENT_DATA_ATTACHED";
64        sCmdToString[EVENT_DATA_DETACHED - BASE] = "EVENT_DATA_DETACHED";
65    }
66
67    public static class RequestInfo {
68        boolean executed;
69        NetworkRequest request;
70        int priority;
71
72        public RequestInfo(NetworkRequest request, int priority) {
73            this.request = request;
74            this.priority = priority;
75        }
76
77        @Override
78        public String toString() {
79            return "[ request=" + request + ", executed=" + executed +
80                ", priority=" + priority + "]";
81        }
82    }
83
84    protected static String cmdToString(int cmd) {
85        cmd -= BASE;
86        if ((cmd >= 0) && (cmd < sCmdToString.length)) {
87            return sCmdToString[cmd];
88        } else {
89            return AsyncChannel.cmdToString(cmd + BASE);
90        }
91    }
92
93    public DcSwitchAsyncChannel(DcSwitchStateMachine dcSwitchState, int id) {
94        mDcSwitchState = dcSwitchState;
95        tagId = id;
96    }
97
98    private int rspConnect(Message response) {
99        int retVal = response.arg1;
100        if (DBG) log("rspConnect=" + retVal);
101        return retVal;
102    }
103
104    public int connectSync(RequestInfo apnRequest) {
105        Message response = sendMessageSynchronously(REQ_CONNECT, apnRequest);
106        if ((response != null) && (response.what == RSP_CONNECT)) {
107            return rspConnect(response);
108        } else {
109            if (DBG) log("rspConnect error response=" + response);
110            return PhoneConstants.APN_REQUEST_FAILED;
111        }
112    }
113
114    private int rspDisconnect(Message response) {
115        int retVal = response.arg1;
116        if (DBG) log("rspDisconnect=" + retVal);
117        return retVal;
118    }
119
120    public int disconnectSync(RequestInfo apnRequest) {
121        Message response = sendMessageSynchronously(REQ_DISCONNECT, apnRequest);
122        if ((response != null) && (response.what == RSP_DISCONNECT)) {
123            return rspDisconnect(response);
124        } else {
125            if (DBG) log("rspDisconnect error response=" + response);
126            return PhoneConstants.APN_REQUEST_FAILED;
127        }
128    }
129
130    private int rspDisconnectAll(Message response) {
131        int retVal = response.arg1;
132        if (DBG) log("rspDisconnectAll=" + retVal);
133        return retVal;
134    }
135
136    public int disconnectAllSync() {
137        Message response = sendMessageSynchronously(REQ_DISCONNECT_ALL);
138        if ((response != null) && (response.what == RSP_DISCONNECT_ALL)) {
139            return rspDisconnectAll(response);
140        } else {
141            if (DBG) log("rspDisconnectAll error response=" + response);
142            return PhoneConstants.APN_REQUEST_FAILED;
143        }
144    }
145
146    public void notifyDataAttached() {
147        sendMessage(EVENT_DATA_ATTACHED);
148        if (DBG) log("notifyDataAttached");
149    }
150
151    public void notifyDataDetached() {
152        sendMessage(EVENT_DATA_DETACHED);
153        if (DBG) log("EVENT_DATA_DETACHED");
154    }
155
156    private boolean rspIsIdle(Message response) {
157        boolean retVal = response.arg1 == 1;
158        if (DBG) log("rspIsIdle=" + retVal);
159        return retVal;
160    }
161
162    public boolean isIdleSync() {
163        Message response = sendMessageSynchronously(REQ_IS_IDLE_STATE);
164        if ((response != null) && (response.what == RSP_IS_IDLE_STATE)) {
165            return rspIsIdle(response);
166        } else {
167            if (DBG) log("rspIsIndle error response=" + response);
168            return false;
169        }
170    }
171
172    public void reqIsIdleOrDetaching() {
173        sendMessage(REQ_IS_IDLE_OR_DETACHING_STATE);
174        if (DBG) log("reqIsIdleOrDetaching");
175    }
176
177    public boolean rspIsIdleOrDetaching(Message response) {
178        boolean retVal = response.arg1 == 1;
179        if (DBG) log("rspIsIdleOrDetaching=" + retVal);
180        return retVal;
181    }
182
183    public boolean isIdleOrDetachingSync() {
184        Message response = sendMessageSynchronously(REQ_IS_IDLE_OR_DETACHING_STATE);
185        if ((response != null) && (response.what == RSP_IS_IDLE_OR_DETACHING_STATE)) {
186            return rspIsIdleOrDetaching(response);
187        } else {
188            if (DBG) log("rspIsIdleOrDetaching error response=" + response);
189            return false;
190        }
191    }
192
193    @Override
194    public String toString() {
195        return mDcSwitchState.getName();
196    }
197
198    private void log(String s) {
199        Rlog.d(LOG_TAG, "[DcSwitchAsyncChannel-" + tagId + "]: " + s);
200    }
201
202}
203