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