1 /*
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
25 using ::aidl::android::media::tv::tunerresourcemanager::ITunerResourceManager;
26 using ::android::defaultServiceManager;
27 using ::android::IBinder;
28 using ::android::interface_cast;
29 using ::android::IServiceManager;
30 using ::android::sp;
31 using ::android::binder::Status;
32 using ::android::content::pm::IPackageManagerNative;
33
34 namespace aidl {
35 namespace android {
36 namespace media {
37 namespace tv {
38 namespace tuner {
39
40 // System Feature defined in PackageManager
41 static const ::android::String16 FEATURE_TUNER(::android::String16("android.hardware.tv.tuner"));
42
43 int32_t TunerHelper::sResourceRequestCount = 0;
44
checkTunerFeature()45 bool 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.
updateTunerResources(const vector<TunerFrontendInfo> & feInfos,const vector<int32_t> & lnbHandles)75 void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
76 const vector<int32_t>& lnbHandles) {
77 ::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 }
updateTunerResources(const vector<TunerFrontendInfo> & feInfos,const vector<TunerDemuxInfo> & demuxInfos,const vector<int32_t> & lnbHandles)86 void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
87 const vector<TunerDemuxInfo>& demuxInfos,
88 const vector<int32_t>& lnbHandles) {
89 ::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 }
102
103 // TODO: create a map between resource id and handles.
getResourceIdFromHandle(int resourceHandle,int)104 int TunerHelper::getResourceIdFromHandle(int resourceHandle, int /*type*/) {
105 return (resourceHandle & 0x00ff0000) >> 16;
106 }
107
getResourceHandleFromId(int id,int resourceType)108 int TunerHelper::getResourceHandleFromId(int id, int resourceType) {
109 // TODO: build up randomly generated id to handle mapping
110 return (resourceType & 0x000000ff) << 24 | (id << 16) | (sResourceRequestCount++ & 0xffff);
111 }
112
113 } // namespace tuner
114 } // namespace tv
115 } // namespace media
116 } // namespace android
117 } // namespace aidl
118