aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/internal/telephony/RadioInterfaceCapabilityController.java
blob: bab4d121851c5bb48e5f89cb34f601bbb26cac4d (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
/*
 * 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 com.android.internal.telephony;

import android.annotation.NonNull;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.ArraySet;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.telephony.Rlog;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Set;

/**
 * Provides the capabilities that the Radio Interface supports on the current device.
 */
public class RadioInterfaceCapabilityController extends Handler {
    private static final String LOG_TAG =
            RadioInterfaceCapabilityController.class.getSimpleName();

    private static RadioInterfaceCapabilityController sInstance;
    private final RadioConfig mRadioConfig;
    private final CommandsInterface mCommandsInterface;
    private Set<String> mRadioInterfaceCapabilities;
    private final Object mLockRadioInterfaceCapabilities = new Object();
    private static final int EVENT_GET_HAL_DEVICE_CAPABILITIES_DONE = 100;

    /**
     * Init method to instantiate the object
     * Should only be called once.
     */
    public static RadioInterfaceCapabilityController init(final RadioConfig radioConfig,
            final CommandsInterface commandsInterface) {
        synchronized (RadioInterfaceCapabilityController.class) {
            if (sInstance == null) {
                final HandlerThread handlerThread = new HandlerThread("RHC");
                handlerThread.start();
                sInstance = new RadioInterfaceCapabilityController(radioConfig, commandsInterface,
                        handlerThread.getLooper());
            } else {
                Log.wtf(LOG_TAG, "init() called multiple times!  sInstance = " + sInstance);
            }
            return sInstance;
        }
    }

    /**
     * Static method to get instance.
     */
    public static RadioInterfaceCapabilityController getInstance() {
        if (sInstance == null) {
            Log.wtf(LOG_TAG, "getInstance null");
        }

        return sInstance;
    }

    @VisibleForTesting
    public RadioInterfaceCapabilityController(final RadioConfig radioConfig,
            final CommandsInterface commandsInterface, final Looper looper) {
        super(looper);
        mRadioConfig = radioConfig;
        mCommandsInterface = commandsInterface;
        register();
    }

    /**
     * Gets the radio interface capabilities for the device
     */
    @NonNull
    public Set<String> getCapabilities() {
        if (mRadioInterfaceCapabilities == null) {
            // Only incur cost of synchronization block if mRadioInterfaceCapabilities isn't null
            synchronized (mLockRadioInterfaceCapabilities) {
                if (mRadioInterfaceCapabilities == null) {
                    mRadioConfig.getHalDeviceCapabilities(
                            obtainMessage(EVENT_GET_HAL_DEVICE_CAPABILITIES_DONE));
                    try {
                        if (Looper.myLooper() != getLooper()) {
                            mLockRadioInterfaceCapabilities.wait(2000);
                        }
                    } catch (final InterruptedException ignored) {
                    }

                    if (mRadioInterfaceCapabilities == null) {
                        loge("getRadioInterfaceCapabilities: Radio Capabilities not "
                                + "loaded in time");
                        return new ArraySet<>();
                    }
                }
            }
        }
        return mRadioInterfaceCapabilities;
    }

    private void setupCapabilities(final @NonNull AsyncResult ar) {
        if (mRadioInterfaceCapabilities == null) {
            synchronized (mLockRadioInterfaceCapabilities) {
                if (mRadioInterfaceCapabilities == null) {
                    if (ar.exception != null) {
                        loge("setupRadioInterfaceCapabilities: " + ar.exception);
                    }
                    if (ar.result == null) {
                        loge("setupRadioInterfaceCapabilities: ar.result is null");
                        return;
                    }
                    log("setupRadioInterfaceCapabilities: "
                            + "mRadioInterfaceCapabilities now setup");
                    mRadioInterfaceCapabilities =
                            Collections.unmodifiableSet((Set<String>) ar.result);
                    if (mRadioInterfaceCapabilities != null) {
                        unregister();
                    }
                }
                mLockRadioInterfaceCapabilities.notify();
            }
        }
    }

    private void register() {
        // There is no radio HAL, capabilities are irrelevant in this case.
        if (mCommandsInterface == null) {
            mRadioInterfaceCapabilities = Collections.unmodifiableSet(new ArraySet<>());
            return;
        }

        mCommandsInterface.registerForAvailable(this, Phone.EVENT_RADIO_AVAILABLE, null);
    }

    private void unregister() {
        mCommandsInterface.unregisterForAvailable(this);
    }

    @Override
    public void handleMessage(final Message msg) {
        switch (msg.what) {
            case Phone.EVENT_RADIO_AVAILABLE:
            case Phone.EVENT_RADIO_ON:
                getCapabilities();
                break;
            case EVENT_GET_HAL_DEVICE_CAPABILITIES_DONE:
                setupCapabilities((AsyncResult) msg.obj);
                break;
        }
    }

    /**
     * Dump the fields of the instance
     */
    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        pw.println("mRadioConfig=" + mRadioConfig);
    }

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

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