Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f58305d1 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

Implement opening session for HAL 2.0.

Test: bit BroadcastRadioTests:android.hardware.radio.tests.functional.
      RadioTunerTest\#testOpenTuner
Bug: 69958777

Change-Id: Ie371b395986d28dae5f687eeae16f11e4c204490
parent c77bb380
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@@ -645,7 +646,8 @@ public class RadioManager {
        private final boolean mAf;
        private final boolean mEa;

        FmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
        /** @hide */
        public FmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
                boolean stereo, boolean rds, boolean ta, boolean af, boolean ea) {
            super(region, type, lowerLimit, upperLimit, spacing);
            mStereo = stereo;
@@ -771,7 +773,8 @@ public class RadioManager {

        private final boolean mStereo;

        AmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
        /** @hide */
        public AmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
                boolean stereo) {
            super(region, type, lowerLimit, upperLimit, spacing);
            mStereo = stereo;
@@ -843,10 +846,10 @@ public class RadioManager {
    /** Radio band configuration. */
    public static class BandConfig implements Parcelable {

        final BandDescriptor mDescriptor;
        @NonNull final BandDescriptor mDescriptor;

        BandConfig(BandDescriptor descriptor) {
            mDescriptor = descriptor;
            mDescriptor = Objects.requireNonNull(descriptor);
        }

        BandConfig(int region, int type, int lowerLimit, int upperLimit, int spacing) {
@@ -968,7 +971,8 @@ public class RadioManager {
        private final boolean mAf;
        private final boolean mEa;

        FmBandConfig(FmBandDescriptor descriptor) {
        /** @hide */
        public FmBandConfig(FmBandDescriptor descriptor) {
            super((BandDescriptor)descriptor);
            mStereo = descriptor.isStereoSupported();
            mRds = descriptor.isRdsSupported();
@@ -1204,7 +1208,8 @@ public class RadioManager {
    public static class AmBandConfig extends BandConfig {
        private final boolean mStereo;

        AmBandConfig(AmBandDescriptor descriptor) {
        /** @hide */
        public AmBandConfig(AmBandDescriptor descriptor) {
            super((BandDescriptor)descriptor);
            mStereo = descriptor.isStereoSupported();
        }
+5 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.hardware.radio.ITuner;
import android.hardware.radio.ITunerCallback;
import android.hardware.radio.RadioManager;
import android.os.ParcelableException;
import android.util.Slog;

import com.android.server.SystemService;

@@ -33,6 +34,8 @@ import java.util.Objects;
import java.util.OptionalInt;

public class BroadcastRadioService extends SystemService {
    private static final String TAG = "BcRadioSrv";

    private final ServiceImpl mServiceImpl = new ServiceImpl();

    private final com.android.server.broadcastradio.hal1.BroadcastRadioService mHal1 =
@@ -84,13 +87,14 @@ public class BroadcastRadioService extends SystemService {
        @Override
        public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig,
                boolean withAudio, ITunerCallback callback) {
            Slog.i(TAG, "openTuner(" + moduleId + ", _, " + withAudio + ", _)");
            enforcePolicyAccess();
            if (callback == null) {
                throw new IllegalArgumentException("Callback must not be empty");
            }
            synchronized (mLock) {
                if (mHal2.hasModule(moduleId)) {
                    throw new RuntimeException("Not implemented");
                    return mHal2.openSession(moduleId, callback);
                } else {
                    return mHal1.openTuner(moduleId, bandConfig, withAudio, callback);
                }
+14 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.server.broadcastradio.hal2;

import android.annotation.NonNull;
import android.hardware.radio.ITuner;
import android.hardware.radio.ITunerCallback;
import android.hardware.radio.RadioManager;
import android.hardware.broadcastradio.V2_0.IBroadcastRadio;
import android.hidl.manager.V1_0.IServiceManager;
@@ -28,6 +30,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class BroadcastRadioService {
@@ -76,4 +79,15 @@ public class BroadcastRadioService {
    public boolean hasModule(int id) {
        return mModules.containsKey(id);
    }

    public ITuner openSession(int moduleId, @NonNull ITunerCallback callback) {
        Objects.requireNonNull(callback);

        RadioModule module = mModules.get(moduleId);
        if (module == null) {
            throw new IllegalArgumentException("Invalid module ID");
        }

        return module.openSession(callback);
    }
}
+67 −5
Original line number Diff line number Diff line
@@ -18,12 +18,17 @@ package com.android.server.broadcastradio.hal2;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.broadcastradio.V2_0.AmFmBandRange;
import android.hardware.broadcastradio.V2_0.AmFmRegionConfig;
import android.hardware.broadcastradio.V2_0.Properties;
import android.hardware.broadcastradio.V2_0.Result;
import android.hardware.broadcastradio.V2_0.VendorKeyValue;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.os.ParcelableException;
import android.util.Slog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -35,6 +40,28 @@ import java.util.Set;
class Convert {
    private static final String TAG = "BcRadio2Srv.convert";

    static void throwOnError(String action, int result) {
        switch (result) {
            case Result.OK:
                return;
            case Result.UNKNOWN_ERROR:
                throw new ParcelableException(new RuntimeException(action + ": UNKNOWN_ERROR"));
            case Result.INTERNAL_ERROR:
                throw new ParcelableException(new RuntimeException(action + ": INTERNAL_ERROR"));
            case Result.INVALID_ARGUMENTS:
                throw new IllegalArgumentException(action + ": INVALID_ARGUMENTS");
            case Result.INVALID_STATE:
                throw new IllegalStateException(action + ": INVALID_STATE");
            case Result.NOT_SUPPORTED:
                throw new UnsupportedOperationException(action + ": NOT_SUPPORTED");
            case Result.TIMEOUT:
                throw new ParcelableException(new RuntimeException(action + ": TIMEOUT"));
            default:
                throw new ParcelableException(new RuntimeException(
                        action + ": unknown error (" + result + ")"));
        }
    }

    private static @NonNull Map<String, String>
    vendorInfoFromHal(@Nullable List<VendorKeyValue> info) {
        if (info == null) return Collections.emptyMap();
@@ -94,13 +121,48 @@ class Convert {
        return pTypes.stream().mapToInt(Integer::intValue).toArray();
    }

    private static @NonNull RadioManager.BandDescriptor[]
    amfmConfigToBands(@Nullable AmFmRegionConfig config) {
        if (config == null) return new RadioManager.BandDescriptor[0];

        int len = config.ranges.size();
        List<RadioManager.BandDescriptor> bands = new ArrayList<>(len);

        // Just a dummy value.
        int region = RadioManager.REGION_ITU_1;

        for (AmFmBandRange range : config.ranges) {
            FrequencyBand bandType = Utils.getBand(range.lowerBound);
            if (bandType == FrequencyBand.UNKNOWN) {
                Slog.e(TAG, "Unknown frequency band at " + range.lowerBound + "kHz");
                continue;
            }
            if (bandType == FrequencyBand.FM) {
                bands.add(new RadioManager.FmBandDescriptor(region, RadioManager.BAND_FM,
                    range.lowerBound, range.upperBound, range.spacing,

                    // TODO(b/69958777): stereo, rds, ta, af, ea
                    true, true, true, true, true
                ));
            } else {  // AM
                bands.add(new RadioManager.AmBandDescriptor(region, RadioManager.BAND_AM,
                    range.lowerBound, range.upperBound, range.spacing,

                    // TODO(b/69958777): stereo
                    true
                ));
            }
        }

        return bands.toArray(new RadioManager.BandDescriptor[bands.size()]);
    }

    static @NonNull RadioManager.ModuleProperties
    propertiesFromHal(int id, @NonNull String serviceName, Properties prop) {
    propertiesFromHal(int id, @NonNull String serviceName, @NonNull Properties prop,
            @Nullable AmFmRegionConfig amfmConfig) {
        Objects.requireNonNull(serviceName);
        Objects.requireNonNull(prop);

        // TODO(b/69958423): implement region info
        RadioManager.BandDescriptor[] bands = new RadioManager.BandDescriptor[0];

        int[] supportedIdentifierTypes = prop.supportedIdentifierTypes.stream().
                mapToInt(Integer::intValue).toArray();
        int[] supportedProgramTypes = identifierTypesToProgramTypes(supportedIdentifierTypes);
@@ -123,7 +185,7 @@ class Convert {
                1,      // numAudioSources
                false,  // isCaptureSupported

                bands,
                amfmConfigToBands(amfmConfig),
                true,  // isBgScanSupported is deprecated
                supportedProgramTypes,
                supportedIdentifierTypes,
+46 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2017 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.server.broadcastradio.hal2;

/**
 * A wrapper class for mutable objects to be used in non-mutable contexts
 * (i.e. final variables catched in lambda closures).
 *
 * @param <E> type of boxed value.
 */
final class Mutable<E> {
    /**
     * A mutable value.
     */
    public E value;

    /**
     * Initialize value with null pointer.
     */
    public Mutable() {
        value = null;
    }

    /**
     * Initialize value with specific value.
     *
     * @param value initial value.
     */
    public Mutable(E value) {
        this.value = value;
    }
}
Loading