Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 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 | |
| 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. |
| 75 | void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos, |
Venkatarama Avadhani | 5338e40 | 2024-05-15 11:55:30 +0000 | [diff] [blame] | 76 | const vector<int64_t>& lnbHandles) { |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 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 | } |
Kensuke Miyagi | a5154b3 | 2022-11-07 15:08:05 -0800 | [diff] [blame] | 86 | void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos, |
| 87 | const vector<TunerDemuxInfo>& demuxInfos, |
Venkatarama Avadhani | 5338e40 | 2024-05-15 11:55:30 +0000 | [diff] [blame] | 88 | const vector<int64_t>& lnbHandles) { |
Kensuke Miyagi | a5154b3 | 2022-11-07 15:08:05 -0800 | [diff] [blame] | 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 | } |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 102 | |
| 103 | // TODO: create a map between resource id and handles. |
Venkatarama Avadhani | 5338e40 | 2024-05-15 11:55:30 +0000 | [diff] [blame] | 104 | int TunerHelper::getResourceIdFromHandle(long resourceHandle, int /*type*/) { |
| 105 | return (int)((resourceHandle >> RESOURCE_ID_SHIFT) & RESOURCE_ID_MASK); |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Venkatarama Avadhani | 5338e40 | 2024-05-15 11:55:30 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Generate resource handle for resourceType and id |
| 110 | * Resource Handle Allotment : 64 bits (long) |
| 111 | * 8 bits - resourceType |
| 112 | * 32 bits - id |
| 113 | * 24 bits - resourceRequestCount |
| 114 | */ |
| 115 | long TunerHelper::getResourceHandleFromId(int id, int resourceType) { |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 116 | // TODO: build up randomly generated id to handle mapping |
Venkatarama Avadhani | 5338e40 | 2024-05-15 11:55:30 +0000 | [diff] [blame] | 117 | return static_cast<int64_t>(resourceType & RESOURCE_TYPE_MASK) << RESOURCE_TYPE_SHIFT | |
| 118 | static_cast<int64_t>(id & RESOURCE_ID_MASK) << RESOURCE_ID_SHIFT | |
| 119 | (sResourceRequestCount++ & RESOURCE_COUNT_MASK); |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace tuner |
| 123 | } // namespace tv |
| 124 | } // namespace media |
| 125 | } // namespace android |
| 126 | } // namespace aidl |