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