Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.server.broadcastradio.hal2; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 21 | import android.hardware.radio.ITuner; |
| 22 | import android.hardware.radio.ITunerCallback; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 23 | import android.hardware.radio.RadioManager; |
| 24 | import android.hardware.broadcastradio.V2_0.IBroadcastRadio; |
| 25 | import android.hidl.manager.V1_0.IServiceManager; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Slog; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 34 | import java.util.Objects; |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 35 | import java.util.stream.Collectors; |
| 36 | |
| 37 | public class BroadcastRadioService { |
| 38 | private static final String TAG = "BcRadio2Srv"; |
| 39 | |
| 40 | private final Map<Integer, RadioModule> mModules = new HashMap<>(); |
| 41 | |
| 42 | private static @NonNull List<String> listByInterface(@NonNull String fqName) { |
| 43 | try { |
| 44 | IServiceManager manager = IServiceManager.getService(); |
| 45 | if (manager == null) { |
| 46 | Slog.e(TAG, "Failed to get HIDL Service Manager"); |
| 47 | return Collections.emptyList(); |
| 48 | } |
| 49 | |
| 50 | List<String> list = manager.listByInterface(fqName); |
| 51 | if (list == null) { |
| 52 | Slog.e(TAG, "Didn't get interface list from HIDL Service Manager"); |
| 53 | return Collections.emptyList(); |
| 54 | } |
| 55 | return list; |
| 56 | } catch (RemoteException ex) { |
| 57 | Slog.e(TAG, "Failed fetching interface list", ex); |
| 58 | return Collections.emptyList(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public @NonNull Collection<RadioManager.ModuleProperties> loadModules(int idx) { |
| 63 | Slog.v(TAG, "loadModules(" + idx + ")"); |
| 64 | |
| 65 | for (String serviceName : listByInterface(IBroadcastRadio.kInterfaceName)) { |
| 66 | Slog.v(TAG, "checking service: " + serviceName); |
| 67 | |
| 68 | RadioModule module = RadioModule.tryLoadingModule(idx, serviceName); |
| 69 | if (module != null) { |
| 70 | Slog.i(TAG, "loaded broadcast radio module " + idx + ": " + |
| 71 | serviceName + " (HAL 2.0)"); |
| 72 | mModules.put(idx++, module); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return mModules.values().stream().map(module -> module.mProperties). |
| 77 | collect(Collectors.toList()); |
| 78 | } |
Tomasz Wasilczyk | df01326 | 2017-12-13 11:47:20 -0800 | [diff] [blame] | 79 | |
| 80 | public boolean hasModule(int id) { |
| 81 | return mModules.containsKey(id); |
| 82 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 83 | |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 84 | public ITuner openSession(int moduleId, @Nullable RadioManager.BandConfig legacyConfig, |
Tomasz Wasilczyk | 436128f | 2018-01-08 16:46:09 -0800 | [diff] [blame] | 85 | boolean withAudio, @NonNull ITunerCallback callback) throws RemoteException { |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 86 | Objects.requireNonNull(callback); |
| 87 | |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 88 | if (!withAudio) { |
| 89 | throw new IllegalArgumentException("Non-audio sessions not supported with HAL 2.x"); |
| 90 | } |
| 91 | |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 92 | RadioModule module = mModules.get(moduleId); |
| 93 | if (module == null) { |
| 94 | throw new IllegalArgumentException("Invalid module ID"); |
| 95 | } |
| 96 | |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 97 | TunerSession session = module.openSession(callback); |
| 98 | session.setConfiguration(legacyConfig); |
| 99 | return session; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 100 | } |
Tomasz Wasilczyk | d65b3ca | 2017-12-13 08:26:25 -0800 | [diff] [blame] | 101 | } |