blob: dc671109c281441f3aa22c20f909bf27d9921027 [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,
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}
86
87// TODO: create a map between resource id and handles.
88int TunerHelper::getResourceIdFromHandle(int resourceHandle, int /*type*/) {
89 return (resourceHandle & 0x00ff0000) >> 16;
90}
91
92int TunerHelper::getResourceHandleFromId(int id, int resourceType) {
93 // TODO: build up randomly generated id to handle mapping
94 return (resourceType & 0x000000ff) << 24 | (id << 16) | (sResourceRequestCount++ & 0xffff);
95}
96
97} // namespace tuner
98} // namespace tv
99} // namespace media
100} // namespace android
101} // namespace aidl