DataConnectionAc.java revision e303d7b31800f1382982cd0117b44ecfbdec08c4
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
17package com.android.internal.telephony;
18
19import com.android.internal.util.AsyncChannel;
20import com.android.internal.util.Protocol;
21
22import android.net.LinkCapabilities;
23import android.net.LinkProperties;
24import android.net.ProxyProperties;
25import android.os.Message;
26
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.Collections;
30import java.util.List;
31
32/**
33 * AsyncChannel to a DataConnection
34 */
35public class DataConnectionAc extends AsyncChannel {
36    private static final boolean DBG = false;
37    private String mLogTag;
38    private List<ApnContext> mApnList = null;
39
40    public DataConnection dataConnection;
41
42    public static final int BASE = Protocol.BASE_DATA_CONNECTION_AC;
43
44    public static final int REQ_IS_INACTIVE = BASE + 0;
45    public static final int RSP_IS_INACTIVE = BASE + 1;
46
47    public static final int REQ_GET_CID = BASE + 2;
48    public static final int RSP_GET_CID = BASE + 3;
49
50    public static final int REQ_GET_APNSETTING = BASE + 4;
51    public static final int RSP_GET_APNSETTING = BASE + 5;
52
53    public static final int REQ_GET_LINK_PROPERTIES = BASE + 6;
54    public static final int RSP_GET_LINK_PROPERTIES = BASE + 7;
55
56    public static final int REQ_SET_LINK_PROPERTIES_HTTP_PROXY = BASE + 8;
57    public static final int RSP_SET_LINK_PROPERTIES_HTTP_PROXY = BASE + 9;
58
59    public static final int REQ_GET_LINK_CAPABILITIES = BASE + 10;
60    public static final int RSP_GET_LINK_CAPABILITIES = BASE + 11;
61
62    public static final int REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE = BASE + 12;
63    public static final int RSP_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE = BASE + 13;
64
65    public static final int REQ_RESET = BASE + 14;
66    public static final int RSP_RESET = BASE + 15;
67
68    public static final int REQ_GET_REFCOUNT = BASE + 16;
69    public static final int RSP_GET_REFCOUNT = BASE + 17;
70
71    /**
72     * enum used to notify action taken or necessary to be
73     * taken after the link property is changed.
74     */
75    public enum LinkPropertyChangeAction {
76        NONE, CHANGED, RESET;
77
78        public static LinkPropertyChangeAction fromInt(int value) {
79            if (value == NONE.ordinal()) {
80                return NONE;
81            } else if (value == CHANGED.ordinal()) {
82                return CHANGED;
83            } else if (value == RESET.ordinal()) {
84                return RESET;
85            } else {
86                throw new RuntimeException("LinkPropertyChangeAction.fromInt: bad value=" + value);
87            }
88        }
89    }
90
91    public DataConnectionAc(DataConnection dc, String logTag) {
92        dataConnection = dc;
93        mLogTag = logTag;
94        mApnList = Collections.synchronizedList(new ArrayList<ApnContext>());
95    }
96
97    /**
98     * Request if the state machine is in the inactive state.
99     * Response {@link #rspIsInactive}
100     */
101    public void reqIsInactive() {
102        sendMessage(REQ_IS_INACTIVE);
103        if (DBG) log("reqIsInactive");
104    }
105
106    /**
107     * Evaluate RSP_IS_INACTIVE.
108     *
109     * @return true if the state machine is in the inactive state.
110     */
111    public boolean rspIsInactive(Message response) {
112        boolean retVal = response.arg1 == 1;
113        if (DBG) log("rspIsInactive=" + retVal);
114        return retVal;
115    }
116
117    /**
118     * @return true if the state machine is in the inactive state.
119     */
120    public boolean isInactiveSync() {
121        Message response = sendMessageSynchronously(REQ_IS_INACTIVE);
122        if ((response != null) && (response.what == RSP_IS_INACTIVE)) {
123            return rspIsInactive(response);
124        } else {
125            log("rspIsInactive error response=" + response);
126            return false;
127        }
128    }
129
130    /**
131     * Request the Connection ID.
132     * Response {@link #rspCid}
133     */
134    public void reqCid() {
135        sendMessage(REQ_GET_CID);
136        if (DBG) log("reqCid");
137    }
138
139    /**
140     * Evaluate a RSP_GET_CID message and return the cid.
141     *
142     * @param response Message
143     * @return connection id or -1 if an error
144     */
145    public int rspCid(Message response) {
146        int retVal = response.arg1;
147        if (DBG) log("rspCid=" + retVal);
148        return retVal;
149    }
150
151    /**
152     * @return connection id or -1 if an error
153     */
154    public int getCidSync() {
155        Message response = sendMessageSynchronously(REQ_GET_CID);
156        if ((response != null) && (response.what == RSP_GET_CID)) {
157            return rspCid(response);
158        } else {
159            log("rspCid error response=" + response);
160            return -1;
161        }
162    }
163
164    /**
165     * Request the Reference Count.
166     * Response {@link #rspRefCount}
167     */
168    public void reqRefCount() {
169        sendMessage(REQ_GET_REFCOUNT);
170        if (DBG) log("reqRefCount");
171    }
172
173    /**
174     * Evaluate a RSP_GET_REFCOUNT message and return the refCount.
175     *
176     * @param response Message
177     * @return ref count or -1 if an error
178     */
179    public int rspRefCount(Message response) {
180        int retVal = response.arg1;
181        if (DBG) log("rspRefCount=" + retVal);
182        return retVal;
183    }
184
185    /**
186     * @return connection id or -1 if an error
187     */
188    public int getRefCountSync() {
189        Message response = sendMessageSynchronously(REQ_GET_REFCOUNT);
190        if ((response != null) && (response.what == RSP_GET_REFCOUNT)) {
191            return rspRefCount(response);
192        } else {
193            log("rspRefCount error response=" + response);
194            return -1;
195        }
196    }
197
198    /**
199     * Request the connections ApnSetting.
200     * Response {@link #rspApnSetting}
201     */
202    public void reqApnSetting() {
203        sendMessage(REQ_GET_APNSETTING);
204        if (DBG) log("reqApnSetting");
205    }
206
207    /**
208     * Evaluate a RSP_APN_SETTING message and return the ApnSetting.
209     *
210     * @param response Message
211     * @return ApnSetting, maybe null
212     */
213    public ApnSetting rspApnSetting(Message response) {
214        ApnSetting retVal = (ApnSetting) response.obj;
215        if (DBG) log("rspApnSetting=" + retVal);
216        return retVal;
217    }
218
219    /**
220     * Get the connections ApnSetting.
221     *
222     * @return ApnSetting or null if an error
223     */
224    public ApnSetting getApnSettingSync() {
225        Message response = sendMessageSynchronously(REQ_GET_APNSETTING);
226        if ((response != null) && (response.what == RSP_GET_APNSETTING)) {
227            return rspApnSetting(response);
228        } else {
229            log("getApnSetting error response=" + response);
230            return null;
231        }
232    }
233
234    /**
235     * Request the connections LinkProperties.
236     * Response {@link #rspLinkProperties}
237     */
238    public void reqLinkProperties() {
239        sendMessage(REQ_GET_LINK_PROPERTIES);
240        if (DBG) log("reqLinkProperties");
241    }
242
243    /**
244     * Evaluate RSP_GET_LINK_PROPERTIES
245     *
246     * @param response
247     * @return LinkProperties, maybe null.
248     */
249    public LinkProperties rspLinkProperties(Message response) {
250        LinkProperties retVal = (LinkProperties) response.obj;
251        if (DBG) log("rspLinkProperties=" + retVal);
252        return retVal;
253    }
254
255    /**
256     * Get the connections LinkProperties.
257     *
258     * @return LinkProperties or null if an error
259     */
260    public LinkProperties getLinkPropertiesSync() {
261        Message response = sendMessageSynchronously(REQ_GET_LINK_PROPERTIES);
262        if ((response != null) && (response.what == RSP_GET_LINK_PROPERTIES)) {
263            return rspLinkProperties(response);
264        } else {
265            log("getLinkProperties error response=" + response);
266            return null;
267        }
268    }
269
270    /**
271     * Request setting the connections LinkProperties.HttpProxy.
272     * Response RSP_SET_LINK_PROPERTIES when complete.
273     */
274    public void reqSetLinkPropertiesHttpProxy(ProxyProperties proxy) {
275        sendMessage(REQ_SET_LINK_PROPERTIES_HTTP_PROXY, proxy);
276        if (DBG) log("reqSetLinkPropertiesHttpProxy proxy=" + proxy);
277    }
278
279    /**
280     * Set the connections LinkProperties.HttpProxy
281     */
282    public void setLinkPropertiesHttpProxySync(ProxyProperties proxy) {
283        Message response =
284            sendMessageSynchronously(REQ_SET_LINK_PROPERTIES_HTTP_PROXY, proxy);
285        if ((response != null) && (response.what == RSP_SET_LINK_PROPERTIES_HTTP_PROXY)) {
286            if (DBG) log("setLinkPropertiesHttpPoxy ok");
287        } else {
288            log("setLinkPropertiesHttpPoxy error response=" + response);
289        }
290    }
291
292    /**
293     * Request update LinkProperties from DataCallState
294     * Response {@link #rspUpdateLinkPropertiesDataCallState}
295     */
296    public void reqUpdateLinkPropertiesDataCallState(DataCallState newState) {
297        sendMessage(REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE, newState);
298        if (DBG) log("reqUpdateLinkPropertiesDataCallState");
299    }
300
301    public LinkPropertyChangeAction rspUpdateLinkPropertiesDataCallState(Message response) {
302        LinkPropertyChangeAction retVal = LinkPropertyChangeAction.fromInt(response.arg1);
303        if (DBG) log("rspUpdateLinkPropertiesState=" + retVal);
304        return retVal;
305    }
306
307    /**
308     * Update link properties in the data connection
309     *
310     * @return true if link property has been updated. false otherwise.
311     */
312    public LinkPropertyChangeAction updateLinkPropertiesDataCallStateSync(DataCallState newState) {
313        Message response =
314            sendMessageSynchronously(REQ_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE, newState);
315        if ((response != null) &&
316            (response.what == RSP_UPDATE_LINK_PROPERTIES_DATA_CALL_STATE)) {
317            return rspUpdateLinkPropertiesDataCallState(response);
318        } else {
319            log("getLinkProperties error response=" + response);
320            return LinkPropertyChangeAction.NONE;
321        }
322    }
323
324    /**
325     * Request the connections LinkCapabilities.
326     * Response {@link #rspLinkCapabilities}
327     */
328    public void reqLinkCapabilities() {
329        sendMessage(REQ_GET_LINK_CAPABILITIES);
330        if (DBG) log("reqLinkCapabilities");
331    }
332
333    /**
334     * Evaluate RSP_GET_LINK_CAPABILITIES
335     *
336     * @param response
337     * @return LinkCapabilites, maybe null.
338     */
339    public LinkCapabilities rspLinkCapabilities(Message response) {
340        LinkCapabilities retVal = (LinkCapabilities) response.obj;
341        if (DBG) log("rspLinkCapabilities=" + retVal);
342        return retVal;
343    }
344
345    /**
346     * Get the connections LinkCapabilities.
347     *
348     * @return LinkCapabilities or null if an error
349     */
350    public LinkCapabilities getLinkCapabilitiesSync() {
351        Message response = sendMessageSynchronously(REQ_GET_LINK_CAPABILITIES);
352        if ((response != null) && (response.what == RSP_GET_LINK_CAPABILITIES)) {
353            return rspLinkCapabilities(response);
354        } else {
355            log("getLinkCapabilities error response=" + response);
356            return null;
357        }
358    }
359
360    /**
361     * Request the connections LinkCapabilities.
362     * Response RSP_RESET when complete
363     */
364    public void reqReset() {
365        sendMessage(REQ_RESET);
366        if (DBG) log("reqReset");
367    }
368
369    /**
370     * Reset the connection and wait for it to complete.
371     */
372    public void resetSync() {
373        Message response = sendMessageSynchronously(REQ_RESET);
374        if ((response != null) && (response.what == RSP_RESET)) {
375            if (DBG) log("restSync ok");
376        } else {
377            log("restSync error response=" + response);
378        }
379    }
380
381    /**
382     * Add ApnContext association.
383     *
384     * @param ApnContext to associate
385     */
386    public void addApnContext(ApnContext apnContext) {
387        if (!mApnList.contains(apnContext)) {
388            mApnList.add(apnContext);
389        }
390    }
391
392    /**
393     * Remove ApnContext associateion.
394     *
395     * @param ApnContext to dissociate
396     */
397    public void removeApnContext(ApnContext apnContext) {
398        mApnList.remove(apnContext);
399    }
400
401    /**
402     * Retrieve collection of ApnContext currently associated with the DataConnectionAc.
403     *
404     * @return Collection of ApnContext
405     */
406    public Collection<ApnContext> getApnList() {
407        return mApnList;
408    }
409
410    private void log(String s) {
411        android.util.Log.d(mLogTag, "DataConnectionAc " + s);
412    }
413}
414