summaryrefslogtreecommitdiff
path: root/tests/tests/telephony/current/mockmodem/src/android/telephony/mockmodem/MockModemService.java
blob: b11003292d7b56408737376ab549b0579ba69e6c (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2021 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 android.telephony.mockmodem;

import static android.telephony.mockmodem.MockSimService.MOCK_SIM_PROFILE_ID_DEFAULT;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.radio.RadioError;
import android.hardware.radio.RadioResponseInfo;
import android.hardware.radio.RadioResponseType;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;

import androidx.test.platform.app.InstrumentationRegistry;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class MockModemService extends Service {
    private static final String TAG = "MockModemService";
    private static final String RESOURCE_PACKAGE_NAME = "android";

    public static final int TEST_TIMEOUT_MS = 30000;
    public static final String IRADIOCONFIG_INTERFACE = "android.telephony.mockmodem.iradioconfig";
    public static final String IRADIOMODEM_INTERFACE = "android.telephony.mockmodem.iradiomodem";
    public static final String IRADIOSIM_INTERFACE = "android.telephony.mockmodem.iradiosim";
    public static final String IRADIONETWORK_INTERFACE =
            "android.telephony.mockmodem.iradionetwork";
    public static final String IRADIODATA_INTERFACE = "android.telephony.mockmodem.iradiodata";
    public static final String IRADIOMESSAGING_INTERFACE =
            "android.telephony.mockmodem.iradiomessaging";
    public static final String IRADIOVOICE_INTERFACE = "android.telephony.mockmodem.iradiovoice";
    public static final String IRADIOIMS_INTERFACE = "android.telephony.mockmodem.iradioims";
    public static final String PHONE_ID = "phone_id";

    private static MockModemConfigInterface sMockModemConfigInterface;
    private static IRadioConfigImpl sIRadioConfigImpl;
    private static IRadioModemImpl[] sIRadioModemImpl;
    private static IRadioSimImpl[] sIRadioSimImpl;
    private static IRadioNetworkImpl[] sIRadioNetworkImpl;
    private static IRadioDataImpl[] sIRadioDataImpl;
    private static IRadioMessagingImpl[] sIRadioMessagingImpl;
    private static IRadioVoiceImpl[] sIRadioVoiceImpl;
    private static IRadioImsImpl[] sIRadioImsImpl;

    public static final byte PHONE_ID_0 = 0x00;
    public static final byte PHONE_ID_1 = 0x01;

    public static final int LATCH_MOCK_MODEM_SERVICE_READY = 0;
    public static final int LATCH_RADIO_INTERFACES_READY = 1;
    public static final int LATCH_MAX = 2;

    private static final int IRADIO_CONFIG_INTERFACE_NUMBER = 1;
    private static final int IRADIO_INTERFACE_NUMBER = 6;

    private Context mContext;
    private TelephonyManager mTelephonyManager;
    private int mNumOfSim;
    private int mNumOfPhone;
    private static final int DEFAULT_SUB_ID = 0;

    private Object mLock;
    protected static CountDownLatch[] sLatches;
    private LocalBinder mBinder;

    // For local access of this Service.
    class LocalBinder extends Binder {
        MockModemService getService() {
            return MockModemService.this;
        }
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "Mock Modem Service Created");

        mContext = InstrumentationRegistry.getInstrumentation().getContext();
        mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
        mNumOfSim = getNumPhysicalSlots();
        mNumOfPhone = mTelephonyManager.getActiveModemCount();
        Log.d(TAG, "Support number of phone = " + mNumOfPhone + ", number of SIM = " + mNumOfSim);

        // Number of physical Sim slot should be equals to or greater than number of phone.
        if (mNumOfSim < mNumOfPhone) {
            mNumOfSim = mNumOfPhone;
        }

        mLock = new Object();
        sLatches = new CountDownLatch[LATCH_MAX];

        for (int i = 0; i < LATCH_MAX; i++) {
            sLatches[i] = new CountDownLatch(1);
            if (i == LATCH_RADIO_INTERFACES_READY) {
                int radioServiceSupportedNumber = 0;
                int radioInterfaceNumber = 0;

                try {
                    for (int j = TelephonyManager.HAL_SERVICE_DATA;
                            j <= TelephonyManager.HAL_SERVICE_IMS;
                            j++) {
                        Pair<Integer, Integer> halVersion = mTelephonyManager.getHalVersion(j);
                        if (halVersion.first == -2 && halVersion.second == -2) {
                            Log.d(TAG, "Service: " + j + " unsupported");
                        } else {
                            radioServiceSupportedNumber++;
                        }
                    }
                } catch (NoSuchMethodError | IllegalStateException e) {
                    Log.e(TAG, "Use the default number of interfaces - " + IRADIO_INTERFACE_NUMBER);
                    radioServiceSupportedNumber = IRADIO_INTERFACE_NUMBER;
                }

                radioInterfaceNumber =
                        IRADIO_CONFIG_INTERFACE_NUMBER + mNumOfPhone * radioServiceSupportedNumber;
                sLatches[i] = new CountDownLatch(radioInterfaceNumber);
            } else {
                sLatches[i] = new CountDownLatch(1);
            }
        }

        sMockModemConfigInterface = new MockModemConfigBase(mContext, mNumOfSim, mNumOfPhone);
        sIRadioConfigImpl = new IRadioConfigImpl(this, sMockModemConfigInterface, PHONE_ID_0);
        sIRadioModemImpl = new IRadioModemImpl[mNumOfPhone];
        sIRadioSimImpl = new IRadioSimImpl[mNumOfPhone];
        sIRadioNetworkImpl = new IRadioNetworkImpl[mNumOfPhone];
        sIRadioDataImpl = new IRadioDataImpl[mNumOfPhone];
        sIRadioMessagingImpl = new IRadioMessagingImpl[mNumOfPhone];
        sIRadioVoiceImpl = new IRadioVoiceImpl[mNumOfPhone];
        sIRadioImsImpl = new IRadioImsImpl[mNumOfPhone];
        for (int i = 0; i < mNumOfPhone; i++) {
            sIRadioModemImpl[i] = new IRadioModemImpl(this, sMockModemConfigInterface, i);
            sIRadioSimImpl[i] = new IRadioSimImpl(this, sMockModemConfigInterface, i);
            sIRadioNetworkImpl[i] =
                    new IRadioNetworkImpl(this, mContext, sMockModemConfigInterface, i);
            sIRadioDataImpl[i] = new IRadioDataImpl(this, mContext, sMockModemConfigInterface, i);
            sIRadioMessagingImpl[i] = new IRadioMessagingImpl(this, sMockModemConfigInterface, i);
            sIRadioVoiceImpl[i] = new IRadioVoiceImpl(this, sMockModemConfigInterface, i);
            sIRadioImsImpl[i] = new IRadioImsImpl(this, sMockModemConfigInterface, i);
        }

        mBinder = new LocalBinder();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "Mock Modem Service on distory");
        if (sMockModemConfigInterface != null) {
            sMockModemConfigInterface.destroy();
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {

        String action = intent.getAction();
        if (action == null) {
            countDownLatch(LATCH_MOCK_MODEM_SERVICE_READY);
            Log.i(TAG, "onBind-Local");
            return mBinder;
        }

        byte phoneId = intent.getByteExtra(PHONE_ID, PHONE_ID_0);

        if (phoneId > PHONE_ID_1) {
            Log.e(TAG, "Not suuport for phone " + phoneId);
            return null;
        }

        if (action.startsWith(IRADIOCONFIG_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioConfig " + phoneId);
            return sIRadioConfigImpl;
        } else if (action.startsWith(IRADIOMODEM_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioModem " + phoneId);
            return sIRadioModemImpl[phoneId];
        } else if (action.startsWith(IRADIOSIM_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioSim " + phoneId);
            return sIRadioSimImpl[phoneId];
        } else if (action.startsWith(IRADIONETWORK_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioNetwork " + phoneId);
            return sIRadioNetworkImpl[phoneId];
        } else if (action.startsWith(IRADIODATA_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioData " + phoneId);
            return sIRadioDataImpl[phoneId];
        } else if (action.startsWith(IRADIOMESSAGING_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioMessaging " + phoneId);
            return sIRadioMessagingImpl[phoneId];
        } else if (action.startsWith(IRADIOVOICE_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioVoice " + phoneId);
            return sIRadioVoiceImpl[phoneId];
        } else if (action.startsWith(IRADIOIMS_INTERFACE)) {
            Log.i(TAG, "onBind-IRadioIms " + phoneId);
            return sIRadioImsImpl[phoneId];
        }

        return null;
    }

    public boolean waitForLatchCountdown(int latchIndex) {
        boolean complete = false;
        try {
            CountDownLatch latch;
            synchronized (mLock) {
                latch = sLatches[latchIndex];
            }
            complete = latch.await(TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
        }
        synchronized (mLock) {
            sLatches[latchIndex] = new CountDownLatch(1);
        }
        return complete;
    }

    public boolean waitForLatchCountdown(int latchIndex, long waitMs) {
        boolean complete = false;
        try {
            CountDownLatch latch;
            synchronized (mLock) {
                latch = sLatches[latchIndex];
            }
            complete = latch.await(waitMs, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
        }
        synchronized (mLock) {
            sLatches[latchIndex] = new CountDownLatch(1);
        }
        return complete;
    }

    public void countDownLatch(int latchIndex) {
        synchronized (mLock) {
            sLatches[latchIndex].countDown();
        }
    }

    public int getNumPhysicalSlots() {
        int numPhysicalSlots = MockSimService.MOCK_SIM_SLOT_MIN;
        int resourceId =
                mContext.getResources()
                        .getIdentifier(
                                "config_num_physical_slots", "integer", RESOURCE_PACKAGE_NAME);

        if (resourceId > 0) {
            numPhysicalSlots = mContext.getResources().getInteger(resourceId);
        } else {
            Log.d(TAG, "Fail to get the resource Id, using default: " + numPhysicalSlots);
        }

        if (numPhysicalSlots > MockSimService.MOCK_SIM_SLOT_MAX) {
            Log.d(
                    TAG,
                    "Number of physical Slot ("
                            + numPhysicalSlots
                            + ") > mock sim slot support. Reset to max number supported ("
                            + MockSimService.MOCK_SIM_SLOT_MAX
                            + ").");
            numPhysicalSlots = MockSimService.MOCK_SIM_SLOT_MAX;
        } else if (numPhysicalSlots < MockSimService.MOCK_SIM_SLOT_MIN) {
            Log.d(
                    TAG,
                    "Number of physical Slot ("
                            + numPhysicalSlots
                            + ") < mock sim slot support. Reset to min number supported ("
                            + MockSimService.MOCK_SIM_SLOT_MIN
                            + ").");
            numPhysicalSlots = MockSimService.MOCK_SIM_SLOT_MIN;
        }

        return numPhysicalSlots;
    }

    public RadioResponseInfo makeSolRsp(int serial) {
        RadioResponseInfo rspInfo = new RadioResponseInfo();
        rspInfo.type = RadioResponseType.SOLICITED;
        rspInfo.serial = serial;
        rspInfo.error = RadioError.NONE;

        return rspInfo;
    }

    public RadioResponseInfo makeSolRsp(int serial, int error) {
        RadioResponseInfo rspInfo = new RadioResponseInfo();
        rspInfo.type = RadioResponseType.SOLICITED;
        rspInfo.serial = serial;
        rspInfo.error = error;

        return rspInfo;
    }

    public boolean initialize(int[] simprofiles) {
        Log.d(TAG, "initialize for num of Sim = " + simprofiles.length);
        boolean result = true;

        // Sync mock modem status between modules
        for (int i = 0; i < mNumOfPhone; i++) {
            // Set initial SIM profiles
            if (simprofiles != null && i < simprofiles.length) {
                result = sMockModemConfigInterface.changeSimProfile(i, simprofiles[i], TAG);
            } else {
                result =
                        sMockModemConfigInterface.changeSimProfile(
                                i, MOCK_SIM_PROFILE_ID_DEFAULT, TAG);
            }

            // Sync modem configurations to radio modules
            sMockModemConfigInterface.notifyAllRegistrantNotifications();

            // Connect to telephony framework
            sIRadioModemImpl[i].rilConnected();
        }

        return result;
    }

    public MockModemConfigInterface getMockModemConfigInterface() {
        return sMockModemConfigInterface;
    }

    public IRadioConfigImpl getIRadioConfig() {
        return sIRadioConfigImpl;
    }

    public IRadioModemImpl getIRadioModem() {
        return getIRadioModem(PHONE_ID_0);
    }

    public IRadioModemImpl getIRadioModem(byte phoneId) {
        return sIRadioModemImpl[phoneId];
    }

    public IRadioSimImpl getIRadioSim() {
        return getIRadioSim(PHONE_ID_0);
    }

    public IRadioSimImpl getIRadioSim(byte phoneId) {
        return sIRadioSimImpl[phoneId];
    }

    public IRadioNetworkImpl getIRadioNetwork() {
        return getIRadioNetwork(PHONE_ID_0);
    }

    public IRadioNetworkImpl getIRadioNetwork(byte phoneId) {
        return sIRadioNetworkImpl[phoneId];
    }

    public IRadioVoiceImpl getIRadioVoice() {
        return getIRadioVoice(PHONE_ID_0);
    }

    public IRadioVoiceImpl getIRadioVoice(byte phoneId) {
        return sIRadioVoiceImpl[phoneId];
    }

    public IRadioMessagingImpl getIRadioMessaging() {
        return getIRadioMessaging(PHONE_ID_0);
    }

    public IRadioMessagingImpl getIRadioMessaging(byte phoneId) {
        return sIRadioMessagingImpl[phoneId];
    }

    public IRadioDataImpl getIRadioData() {
        return getIRadioData(PHONE_ID_0);
    }

    public IRadioDataImpl getIRadioData(byte phoneId) {
        return sIRadioDataImpl[phoneId];
    }

    public IRadioImsImpl getIRadioIms() {
        return getIRadioIms(PHONE_ID_0);
    }

    public IRadioImsImpl getIRadioIms(byte phoneId) {
        return sIRadioImsImpl[phoneId];
    }
}