blob: fc9a5d61704dbd7954dae8d717ff19a35ee98cde [file] [log] [blame]
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -08001/**
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
17package com.android.server.broadcastradio.hal2;
18
19import android.annotation.NonNull;
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080020import android.annotation.Nullable;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080021import android.hardware.radio.ITuner;
22import android.hardware.radio.ITunerCallback;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080023import android.hardware.radio.RadioManager;
24import android.hardware.broadcastradio.V2_0.IBroadcastRadio;
25import android.hidl.manager.V1_0.IServiceManager;
26import android.os.RemoteException;
27import android.util.Slog;
28
29import java.util.Collection;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Map;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080034import java.util.Objects;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080035import java.util.stream.Collectors;
36
37public 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 Wasilczykdf013262017-12-13 11:47:20 -080079
80 public boolean hasModule(int id) {
81 return mModules.containsKey(id);
82 }
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080083
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080084 public ITuner openSession(int moduleId, @Nullable RadioManager.BandConfig legacyConfig,
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -080085 boolean withAudio, @NonNull ITunerCallback callback) throws RemoteException {
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080086 Objects.requireNonNull(callback);
87
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080088 if (!withAudio) {
89 throw new IllegalArgumentException("Non-audio sessions not supported with HAL 2.x");
90 }
91
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080092 RadioModule module = mModules.get(moduleId);
93 if (module == null) {
94 throw new IllegalArgumentException("Invalid module ID");
95 }
96
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080097 TunerSession session = module.openSession(callback);
98 session.setConfiguration(legacyConfig);
99 return session;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -0800100 }
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -0800101}