blob: a03386f3eae2491f6fe9a4e1d9c15b3f4aa530be [file] [log] [blame]
Hongguang093c5f32021-08-09 19:46:34 -07001/*
2 * Copyright 2021 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#include "TunerHelper.h"
18
19#include <aidl/android/media/tv/tunerresourcemanager/ITunerResourceManager.h>
20#include <android/binder_manager.h>
21#include <android/content/pm/IPackageManagerNative.h>
22#include <binder/IServiceManager.h>
23#include <utils/Log.h>
24
25using ::aidl::android::media::tv::tunerresourcemanager::ITunerResourceManager;
26using ::android::defaultServiceManager;
27using ::android::IBinder;
28using ::android::interface_cast;
29using ::android::IServiceManager;
30using ::android::sp;
31using ::android::binder::Status;
32using ::android::content::pm::IPackageManagerNative;
33
34namespace aidl {
35namespace android {
36namespace media {
37namespace tv {
38namespace tuner {
39
40// System Feature defined in PackageManager
41static const ::android::String16 FEATURE_TUNER(::android::String16("android.hardware.tv.tuner"));
42
43int32_t TunerHelper::sResourceRequestCount = 0;
44
45bool TunerHelper::checkTunerFeature() {
46 sp<IServiceManager> serviceMgr = defaultServiceManager();
47 sp<IPackageManagerNative> packageMgr;
48 if (serviceMgr.get() == nullptr) {
49 ALOGE("%s: Cannot find service manager", __func__);
50 return false;
51 }
52
53 sp<IBinder> binder = serviceMgr->waitForService(String16("package_native"));
54 packageMgr = interface_cast<IPackageManagerNative>(binder);
55 if (packageMgr != nullptr) {
56 bool hasFeature = false;
57 Status status = packageMgr->hasSystemFeature(FEATURE_TUNER, 0, &hasFeature);
58 if (!status.isOk()) {
59 ALOGE("%s: hasSystemFeature failed: %s", __func__, status.exceptionMessage().c_str());
60 return false;
61 }
62 if (!hasFeature) {
63 ALOGD("Current device does not support tuner feaure.");
64 return false;
65 }
66 } else {
67 ALOGD("%s: Cannot find package manager.", __func__);
68 return false;
69 }
70
71 return true;
72}
73
74// TODO: update Demux, Descrambler.
75void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
Satish Yalla47e1ff92024-08-16 07:34:23 +000076 const vector<int32_t>& lnbHandles) {
Hongguang093c5f32021-08-09 19:46:34 -070077 ::ndk::SpAIBinder binder(AServiceManager_waitForService("tv_tuner_resource_mgr"));
78 shared_ptr<ITunerResourceManager> tunerRM = ITunerResourceManager::fromBinder(binder);
79 if (tunerRM == nullptr) {
80 return;
81 }
82
83 tunerRM->setFrontendInfoList(feInfos);
84 tunerRM->setLnbInfoList(lnbHandles);
85}
Kensuke Miyagia5154b32022-11-07 15:08:05 -080086void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
87 const vector<TunerDemuxInfo>& demuxInfos,
Satish Yalla47e1ff92024-08-16 07:34:23 +000088 const vector<int32_t>& lnbHandles) {
Kensuke Miyagia5154b32022-11-07 15:08:05 -080089 ::ndk::SpAIBinder binder(AServiceManager_waitForService("tv_tuner_resource_mgr"));
90 shared_ptr<ITunerResourceManager> tunerRM = ITunerResourceManager::fromBinder(binder);
91 if (tunerRM == nullptr) {
92 return;
93 }
94
95 updateTunerResources(feInfos, lnbHandles);
96
97 // for Tuner 2.0 and below, Demux resource is not really managed under TRM
98 if (demuxInfos.size() > 0) {
99 tunerRM->setDemuxInfoList(demuxInfos);
100 }
101}
Hongguang093c5f32021-08-09 19:46:34 -0700102
103// TODO: create a map between resource id and handles.
Satish Yalla47e1ff92024-08-16 07:34:23 +0000104int TunerHelper::getResourceIdFromHandle(int resourceHandle, int /*type*/) {
105 return (resourceHandle & 0x00ff0000) >> 16;
Hongguang093c5f32021-08-09 19:46:34 -0700106}
107
Satish Yalla47e1ff92024-08-16 07:34:23 +0000108int TunerHelper::getResourceHandleFromId(int id, int resourceType) {
Hongguang093c5f32021-08-09 19:46:34 -0700109 // TODO: build up randomly generated id to handle mapping
Satish Yalla47e1ff92024-08-16 07:34:23 +0000110 return (resourceType & 0x000000ff) << 24 | (id << 16) | (sResourceRequestCount++ & 0xffff);
Hongguang093c5f32021-08-09 19:46:34 -0700111}
112
113} // namespace tuner
114} // namespace tv
115} // namespace media
116} // namespace android
117} // namespace aidl