aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/android/internal/telephony/TelephonyDevController.java
blob: 448e5bdc44de1efeb28f429d3239589a32942110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony;

import android.content.res.Resources;
import com.android.internal.telephony.*;
import android.telephony.TelephonyManager;

import android.os.AsyncResult;
import android.telephony.Rlog;
import java.util.BitSet;
import java.util.List;
import java.util.ArrayList;
import android.text.TextUtils;
import android.os.Handler;
import android.os.Message;
import android.os.Registrant;
import android.os.RegistrantList;
import android.telephony.ServiceState;

/**
 * TelephonyDevController - provides a unified view of the
 * telephony hardware resources on a device.
 *
 * manages the set of HardwareConfig for the framework.
 */
public class TelephonyDevController extends Handler {
    private static final String LOG_TAG = "TDC";
    private static final boolean DBG = true;
    private static final Object mLock = new Object();

    private static final int EVENT_HARDWARE_CONFIG_CHANGED = 1;

    private static TelephonyDevController sTelephonyDevController;
    private static ArrayList<HardwareConfig> mModems = new ArrayList<HardwareConfig>();
    private static ArrayList<HardwareConfig> mSims = new ArrayList<HardwareConfig>();

    private static Message sRilHardwareConfig;

    private static void logd(String s) {
        Rlog.d(LOG_TAG, s);
    }

    private static void loge(String s) {
        Rlog.e(LOG_TAG, s);
    }

    public static TelephonyDevController create() {
        synchronized (mLock) {
            if (sTelephonyDevController != null) {
                throw new RuntimeException("TelephonyDevController already created!?!");
            }
            sTelephonyDevController = new TelephonyDevController();
            return sTelephonyDevController;
        }
    }

    public static TelephonyDevController getInstance() {
        synchronized (mLock) {
            if (sTelephonyDevController == null) {
                throw new RuntimeException("TelephonyDevController not yet created!?!");
            }
            return sTelephonyDevController;
        }
    }

    private void initFromResource() {
        Resources resource = Resources.getSystem();
        String[] hwStrings = resource.getStringArray(
            com.android.internal.R.array.config_telephonyHardware);
        if (hwStrings != null) {
            for (String hwString : hwStrings) {
                HardwareConfig hw = new HardwareConfig(hwString);
                if (hw != null) {
                    if (hw.type == HardwareConfig.DEV_HARDWARE_TYPE_MODEM) {
                        updateOrInsert(hw, mModems);
                    } else if (hw.type == HardwareConfig.DEV_HARDWARE_TYPE_SIM) {
                        updateOrInsert(hw, mSims);
                    }
                }
            }
        }
    }

    private TelephonyDevController() {
        initFromResource();

        mModems.trimToSize();
        mSims.trimToSize();
    }

    /**
     * each RIL call this interface to register/unregister the unsolicited hardware
     * configuration callback data it can provide.
     */
    public static void registerRIL(CommandsInterface cmdsIf) {
        /* get the current configuration from this ril... */
        cmdsIf.getHardwareConfig(sRilHardwareConfig);
        /* ... process it ... */
        if (sRilHardwareConfig != null) {
            AsyncResult ar = (AsyncResult) sRilHardwareConfig.obj;
            if (ar.exception == null) {
                handleGetHardwareConfigChanged(ar);
            }
        }
        /* and register for async device configuration change. */
        cmdsIf.registerForHardwareConfigChanged(sTelephonyDevController, EVENT_HARDWARE_CONFIG_CHANGED, null);
    }

    public static void unregisterRIL(CommandsInterface cmdsIf) {
        cmdsIf.unregisterForHardwareConfigChanged(sTelephonyDevController);
    }

    /**
     * handle callbacks from RIL.
     */
    public void handleMessage(Message msg) {
        AsyncResult ar;
        switch (msg.what) {
            case EVENT_HARDWARE_CONFIG_CHANGED:
                if (DBG) logd("handleMessage: received EVENT_HARDWARE_CONFIG_CHANGED");
                ar = (AsyncResult) msg.obj;
                handleGetHardwareConfigChanged(ar);
            break;
            default:
                loge("handleMessage: Unknown Event " + msg.what);
        }
    }

    /**
     * hardware configuration update or insert.
     */
    private static void updateOrInsert(HardwareConfig hw, ArrayList<HardwareConfig> list) {
        int size;
        HardwareConfig item;
        synchronized (mLock) {
            size = list.size();
            for (int i = 0 ; i < size ; i++) {
                item = list.get(i);
                if (item.uuid.compareTo(hw.uuid) == 0) {
                    if (DBG) logd("updateOrInsert: removing: " + item);
                    list.remove(i);
                }
            }
            if (DBG) logd("updateOrInsert: inserting: " + hw);
            list.add(hw);
        }
    }

    /**
     * hardware configuration changed.
     */
    private static void handleGetHardwareConfigChanged(AsyncResult ar) {
        if ((ar.exception == null) && (ar.result != null)) {
            List hwcfg = (List)ar.result;
            for (int i = 0 ; i < hwcfg.size() ; i++) {
                HardwareConfig hw = null;

                hw = (HardwareConfig) hwcfg.get(i);
                if (hw != null) {
                    if (hw.type == HardwareConfig.DEV_HARDWARE_TYPE_MODEM) {
                        updateOrInsert(hw, mModems);
                    } else if (hw.type == HardwareConfig.DEV_HARDWARE_TYPE_SIM) {
                        updateOrInsert(hw, mSims);
                    }
                }
            }
        } else {
            /* error detected, ignore.  are we missing some real time configutation
             * at this point?  what to do...
             */
            loge("handleGetHardwareConfigChanged - returned an error.");
        }
    }

    /**
     * get total number of registered modem.
     */
    public static int getModemCount() {
        synchronized (mLock) {
            int count = mModems.size();
            if (DBG) logd("getModemCount: " + count);
            return count;
        }
    }

    /**
     * get modem at index 'index'.
     */
    public HardwareConfig getModem(int index) {
        synchronized (mLock) {
            if (mModems.isEmpty()) {
                loge("getModem: no registered modem device?!?");
                return null;
            }

            if (index > getModemCount()) {
                loge("getModem: out-of-bounds access for modem device " + index + " max: " + getModemCount());
                return null;
            }

            if (DBG) logd("getModem: " + index);
            return mModems.get(index);
        }
    }

    /**
     * get total number of registered sims.
     */
    public int getSimCount() {
        synchronized (mLock) {
            int count = mSims.size();
            if (DBG) logd("getSimCount: " + count);
            return count;
        }
    }

    /**
     * get sim at index 'index'.
     */
    public HardwareConfig getSim(int index) {
        synchronized (mLock) {
            if (mSims.isEmpty()) {
                loge("getSim: no registered sim device?!?");
                return null;
            }

            if (index > getSimCount()) {
                loge("getSim: out-of-bounds access for sim device " + index + " max: " + getSimCount());
                return null;
            }

            if (DBG) logd("getSim: " + index);
            return mSims.get(index);
        }
    }

    /**
     * get modem associated with sim index 'simIndex'.
     */
    public HardwareConfig getModemForSim(int simIndex) {
        synchronized (mLock) {
            if (mModems.isEmpty() || mSims.isEmpty()) {
                loge("getModemForSim: no registered modem/sim device?!?");
                return null;
            }

            if (simIndex > getSimCount()) {
                loge("getModemForSim: out-of-bounds access for sim device " + simIndex + " max: " + getSimCount());
                return null;
            }

            if (DBG) logd("getModemForSim " + simIndex);

            HardwareConfig sim = getSim(simIndex);
            for (HardwareConfig modem: mModems) {
                if (modem.uuid.equals(sim.modemUuid)) {
                    return modem;
                }
            }

            return null;
        }
    }

    /**
     * get all sim's associated with modem at index 'modemIndex'.
     */
    public ArrayList<HardwareConfig> getAllSimsForModem(int modemIndex) {
        synchronized (mLock) {
            if (mSims.isEmpty()) {
                loge("getAllSimsForModem: no registered sim device?!?");
                return null;
            }

            if (modemIndex > getModemCount()) {
                loge("getAllSimsForModem: out-of-bounds access for modem device " + modemIndex + " max: " + getModemCount());
                return null;
            }

            if (DBG) logd("getAllSimsForModem " + modemIndex);

            ArrayList<HardwareConfig> result = new ArrayList<HardwareConfig>();
            HardwareConfig modem = getModem(modemIndex);
            for (HardwareConfig sim: mSims) {
                if (sim.modemUuid.equals(modem.uuid)) {
                    result.add(sim);
                }
            }
            return result;
        }
    }

    /**
     * get all modem's registered.
     */
    public ArrayList<HardwareConfig> getAllModems() {
        synchronized (mLock) {
            ArrayList<HardwareConfig> modems = new ArrayList<HardwareConfig>();
            if (mModems.isEmpty()) {
                if (DBG) logd("getAllModems: empty list.");
            } else {
                for (HardwareConfig modem: mModems) {
                    modems.add(modem);
                }
            }

            return modems;
        }
    }

    /**
     * get all sim's registered.
     */
    public ArrayList<HardwareConfig> getAllSims() {
        synchronized (mLock) {
            ArrayList<HardwareConfig> sims = new ArrayList<HardwareConfig>();
            if (mSims.isEmpty()) {
                if (DBG) logd("getAllSims: empty list.");
            } else {
                for (HardwareConfig sim: mSims) {
                    sims.add(sim);
                }
            }

            return sims;
        }
    }
}