| shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 1 | /** | 
|  | 2 | * Copyright (c) 2020, 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 | #define LOG_TAG "TunerService" | 
|  | 18 |  | 
|  | 19 | #include <android/binder_manager.h> | 
|  | 20 | #include <utils/Log.h> | 
|  | 21 | #include "TunerService.h" | 
|  | 22 |  | 
| Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 23 | using ::aidl::android::media::tv::tuner::TunerFrontendAnalogCapabilities; | 
|  | 24 | using ::aidl::android::media::tv::tuner::TunerFrontendAtsc3Capabilities; | 
|  | 25 | using ::aidl::android::media::tv::tuner::TunerFrontendAtscCapabilities; | 
|  | 26 | using ::aidl::android::media::tv::tuner::TunerFrontendCableCapabilities; | 
|  | 27 | using ::aidl::android::media::tv::tuner::TunerFrontendCapabilities; | 
|  | 28 | using ::aidl::android::media::tv::tuner::TunerFrontendDvbsCapabilities; | 
|  | 29 | using ::aidl::android::media::tv::tuner::TunerFrontendDvbtCapabilities; | 
|  | 30 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbs3Capabilities; | 
|  | 31 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbsCapabilities; | 
|  | 32 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbtCapabilities; | 
| shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 33 | using ::android::hardware::hidl_vec; | 
|  | 34 | using ::android::hardware::tv::tuner::V1_0::FrontendId; | 
| Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 35 | using ::android::hardware::tv::tuner::V1_0::FrontendType; | 
| shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 36 | using ::android::hardware::tv::tuner::V1_0::Result; | 
|  | 37 |  | 
|  | 38 | namespace android { | 
|  | 39 |  | 
|  | 40 | sp<ITuner> TunerService::mTuner; | 
|  | 41 |  | 
|  | 42 | TunerService::TunerService() {} | 
|  | 43 | TunerService::~TunerService() {} | 
|  | 44 |  | 
|  | 45 | void TunerService::instantiate() { | 
|  | 46 | std::shared_ptr<TunerService> service = | 
|  | 47 | ::ndk::SharedRefBase::make<TunerService>(); | 
|  | 48 | AServiceManager_addService(service->asBinder().get(), getServiceName()); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | Status TunerService::getFrontendIds(std::vector<int32_t>* ids, int32_t* /* _aidl_return */) { | 
|  | 52 | if (mTuner == nullptr) { | 
|  | 53 | // TODO: create a method for init. | 
|  | 54 | mTuner = ITuner::getService(); | 
|  | 55 | if (mTuner == nullptr) { | 
|  | 56 | ALOGE("Failed to get ITuner service."); | 
|  | 57 | return ::ndk::ScopedAStatus::fromServiceSpecificError( | 
|  | 58 | static_cast<int32_t>(Result::UNAVAILABLE)); | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | hidl_vec<FrontendId> feIds; | 
|  | 62 | Result res; | 
|  | 63 | mTuner->getFrontendIds([&](Result r, const hidl_vec<FrontendId>& frontendIds) { | 
|  | 64 | feIds = frontendIds; | 
|  | 65 | res = r; | 
|  | 66 | }); | 
|  | 67 | if (res != Result::SUCCESS) { | 
|  | 68 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); | 
|  | 69 | } | 
|  | 70 | ids->resize(feIds.size()); | 
|  | 71 | std::copy(feIds.begin(), feIds.end(), ids->begin()); | 
|  | 72 |  | 
|  | 73 | return ::ndk::ScopedAStatus::ok(); | 
|  | 74 | } | 
|  | 75 |  | 
| Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 76 | Status TunerService::getFrontendInfo( | 
|  | 77 | int32_t frontendHandle, TunerServiceFrontendInfo* _aidl_return) { | 
|  | 78 | if (mTuner == nullptr) { | 
|  | 79 | // TODO: create a method for init. | 
|  | 80 | mTuner = ITuner::getService(); | 
|  | 81 | if (mTuner == nullptr) { | 
|  | 82 | ALOGE("Failed to get ITuner service."); | 
|  | 83 | return ::ndk::ScopedAStatus::fromServiceSpecificError( | 
|  | 84 | static_cast<int32_t>(Result::UNAVAILABLE)); | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | Result res; | 
|  | 89 | FrontendInfo info; | 
|  | 90 | int feId = getResourceIdFromHandle(frontendHandle); | 
|  | 91 | mTuner->getFrontendInfo(feId, [&](Result r, const FrontendInfo& feInfo) { | 
|  | 92 | info = feInfo; | 
|  | 93 | res = r; | 
|  | 94 | }); | 
|  | 95 | if (res != Result::SUCCESS) { | 
|  | 96 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | TunerServiceFrontendInfo tunerInfo = convertToAidlFrontendInfo(feId, info); | 
|  | 100 | *_aidl_return = tunerInfo; | 
|  | 101 | return ::ndk::ScopedAStatus::ok(); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | TunerServiceFrontendInfo TunerService::convertToAidlFrontendInfo(int feId, FrontendInfo halInfo) { | 
|  | 105 | TunerServiceFrontendInfo info{ | 
|  | 106 | .id = feId, | 
|  | 107 | .type = (int)halInfo.type, | 
|  | 108 | .minFrequency = (int)halInfo.minFrequency, | 
|  | 109 | .maxFrequency = (int)halInfo.maxFrequency, | 
|  | 110 | .minSymbolRate = (int)halInfo.minSymbolRate, | 
|  | 111 | .maxSymbolRate = (int)halInfo.maxSymbolRate, | 
|  | 112 | .acquireRange = (int)halInfo.acquireRange, | 
|  | 113 | .exclusiveGroupId = (int)halInfo.exclusiveGroupId, | 
|  | 114 | }; | 
|  | 115 | for (int i = 0; i < halInfo.statusCaps.size(); i++) { | 
|  | 116 | info.statusCaps.push_back((int)halInfo.statusCaps[i]); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | TunerFrontendCapabilities caps; | 
|  | 120 | switch (halInfo.type) { | 
|  | 121 | case FrontendType::ANALOG: { | 
|  | 122 | TunerFrontendAnalogCapabilities analogCaps{ | 
|  | 123 | .typeCap = (int)halInfo.frontendCaps.analogCaps().typeCap, | 
|  | 124 | .sifStandardCap = (int)halInfo.frontendCaps.analogCaps().sifStandardCap, | 
|  | 125 | }; | 
|  | 126 | caps.set<TunerFrontendCapabilities::analogCaps>(analogCaps); | 
|  | 127 | break; | 
|  | 128 | } | 
|  | 129 | case FrontendType::ATSC: { | 
|  | 130 | TunerFrontendAtscCapabilities atscCaps{ | 
|  | 131 | .modulationCap = (int)halInfo.frontendCaps.atscCaps().modulationCap, | 
|  | 132 | }; | 
|  | 133 | caps.set<TunerFrontendCapabilities::atscCaps>(atscCaps); | 
|  | 134 | break; | 
|  | 135 | } | 
|  | 136 | case FrontendType::ATSC3: { | 
|  | 137 | TunerFrontendAtsc3Capabilities atsc3Caps{ | 
|  | 138 | .bandwidthCap = (int)halInfo.frontendCaps.atsc3Caps().bandwidthCap, | 
|  | 139 | .modulationCap = (int)halInfo.frontendCaps.atsc3Caps().modulationCap, | 
|  | 140 | .timeInterleaveModeCap = | 
|  | 141 | (int)halInfo.frontendCaps.atsc3Caps().timeInterleaveModeCap, | 
|  | 142 | .codeRateCap = (int)halInfo.frontendCaps.atsc3Caps().codeRateCap, | 
|  | 143 | .demodOutputFormatCap = (int)halInfo.frontendCaps.atsc3Caps().demodOutputFormatCap, | 
|  | 144 | .fecCap = (int)halInfo.frontendCaps.atsc3Caps().fecCap, | 
|  | 145 | }; | 
|  | 146 | caps.set<TunerFrontendCapabilities::atsc3Caps>(atsc3Caps); | 
|  | 147 | break; | 
|  | 148 | } | 
|  | 149 | case FrontendType::DVBC: { | 
|  | 150 | TunerFrontendCableCapabilities cableCaps{ | 
|  | 151 | .modulationCap = (int)halInfo.frontendCaps.dvbcCaps().modulationCap, | 
|  | 152 | .codeRateCap = (int)halInfo.frontendCaps.dvbcCaps().fecCap, | 
|  | 153 | .annexCap = (int)halInfo.frontendCaps.dvbcCaps().annexCap, | 
|  | 154 | }; | 
|  | 155 | caps.set<TunerFrontendCapabilities::cableCaps>(cableCaps); | 
|  | 156 | break; | 
|  | 157 | } | 
|  | 158 | case FrontendType::DVBS: { | 
|  | 159 | TunerFrontendDvbsCapabilities dvbsCaps{ | 
|  | 160 | .modulationCap = (int)halInfo.frontendCaps.dvbsCaps().modulationCap, | 
|  | 161 | .codeRateCap = (long)halInfo.frontendCaps.dvbsCaps().innerfecCap, | 
|  | 162 | .standard = (int)halInfo.frontendCaps.dvbsCaps().standard, | 
|  | 163 | }; | 
|  | 164 | caps.set<TunerFrontendCapabilities::dvbsCaps>(dvbsCaps); | 
|  | 165 | break; | 
|  | 166 | } | 
|  | 167 | case FrontendType::DVBT: { | 
|  | 168 | TunerFrontendDvbtCapabilities dvbtCaps{ | 
|  | 169 | .transmissionModeCap = (int)halInfo.frontendCaps.dvbtCaps().transmissionModeCap, | 
|  | 170 | .bandwidthCap = (int)halInfo.frontendCaps.dvbtCaps().bandwidthCap, | 
|  | 171 | .constellationCap = (int)halInfo.frontendCaps.dvbtCaps().constellationCap, | 
|  | 172 | .codeRateCap = (int)halInfo.frontendCaps.dvbtCaps().coderateCap, | 
|  | 173 | .hierarchyCap = (int)halInfo.frontendCaps.dvbtCaps().hierarchyCap, | 
|  | 174 | .guardIntervalCap = (int)halInfo.frontendCaps.dvbtCaps().guardIntervalCap, | 
|  | 175 | .isT2Supported = (bool)halInfo.frontendCaps.dvbtCaps().isT2Supported, | 
|  | 176 | .isMisoSupported = (bool)halInfo.frontendCaps.dvbtCaps().isMisoSupported, | 
|  | 177 | }; | 
|  | 178 | caps.set<TunerFrontendCapabilities::dvbtCaps>(dvbtCaps); | 
|  | 179 | break; | 
|  | 180 | } | 
|  | 181 | case FrontendType::ISDBS: { | 
|  | 182 | TunerFrontendIsdbsCapabilities isdbsCaps{ | 
|  | 183 | .modulationCap = (int)halInfo.frontendCaps.isdbsCaps().modulationCap, | 
|  | 184 | .codeRateCap = (int)halInfo.frontendCaps.isdbsCaps().coderateCap, | 
|  | 185 | }; | 
|  | 186 | caps.set<TunerFrontendCapabilities::isdbsCaps>(isdbsCaps); | 
|  | 187 | break; | 
|  | 188 | } | 
|  | 189 | case FrontendType::ISDBS3: { | 
|  | 190 | TunerFrontendIsdbs3Capabilities isdbs3Caps{ | 
|  | 191 | .modulationCap = (int)halInfo.frontendCaps.isdbs3Caps().modulationCap, | 
|  | 192 | .codeRateCap = (int)halInfo.frontendCaps.isdbs3Caps().coderateCap, | 
|  | 193 | }; | 
|  | 194 | caps.set<TunerFrontendCapabilities::isdbs3Caps>(isdbs3Caps); | 
|  | 195 | break; | 
|  | 196 | } | 
|  | 197 | case FrontendType::ISDBT: { | 
|  | 198 | TunerFrontendIsdbtCapabilities isdbtCaps{ | 
|  | 199 | .modeCap = (int)halInfo.frontendCaps.isdbtCaps().modeCap, | 
|  | 200 | .bandwidthCap = (int)halInfo.frontendCaps.isdbtCaps().bandwidthCap, | 
|  | 201 | .modulationCap = (int)halInfo.frontendCaps.isdbtCaps().modulationCap, | 
|  | 202 | .codeRateCap = (int)halInfo.frontendCaps.isdbtCaps().coderateCap, | 
|  | 203 | .guardIntervalCap = (int)halInfo.frontendCaps.isdbtCaps().guardIntervalCap, | 
|  | 204 | }; | 
|  | 205 | caps.set<TunerFrontendCapabilities::isdbtCaps>(isdbtCaps); | 
|  | 206 | break; | 
|  | 207 | } | 
|  | 208 | default: | 
|  | 209 | break; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | info.caps = caps; | 
|  | 213 | return info; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | int TunerService::getResourceIdFromHandle(int resourceHandle) { | 
|  | 217 | return (resourceHandle & 0x00ff0000) >> 16; | 
|  | 218 | } | 
| shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 219 | } // namespace android |