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 <android-base/logging.h>
18 #include <android/hardware/tv/tuner/1.0/types.h>
19 #include <android_media_tuner_testing_configuration_V1_0.h>
20 #include <android_media_tuner_testing_configuration_V1_0_enums.h>
21 #include <binder/MemoryDealer.h>
22 #include <hidl/HidlSupport.h>
23 #include <hidl/HidlTransportSupport.h>
24 #include <hidl/Status.h>
25 #include <hidlmemory/FrameworkUtils.h>
26 
27 using namespace std;
28 using namespace android::media::tuner::testing::configuration::V1_0;
29 
30 using android::hardware::tv::tuner::V1_0::DataFormat;
31 using android::hardware::tv::tuner::V1_0::DemuxAlpFilterType;
32 using android::hardware::tv::tuner::V1_0::DemuxFilterAvSettings;
33 using android::hardware::tv::tuner::V1_0::DemuxFilterEvent;
34 using android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
35 using android::hardware::tv::tuner::V1_0::DemuxFilterRecordSettings;
36 using android::hardware::tv::tuner::V1_0::DemuxFilterSectionSettings;
37 using android::hardware::tv::tuner::V1_0::DemuxFilterSettings;
38 using android::hardware::tv::tuner::V1_0::DemuxFilterType;
39 using android::hardware::tv::tuner::V1_0::DemuxIpAddress;
40 using android::hardware::tv::tuner::V1_0::DemuxIpFilterSettings;
41 using android::hardware::tv::tuner::V1_0::DemuxIpFilterType;
42 using android::hardware::tv::tuner::V1_0::DemuxMmtpFilterType;
43 using android::hardware::tv::tuner::V1_0::DemuxRecordScIndexType;
44 using android::hardware::tv::tuner::V1_0::DemuxTlvFilterType;
45 using android::hardware::tv::tuner::V1_0::DemuxTpid;
46 using android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
47 using android::hardware::tv::tuner::V1_0::DvrSettings;
48 using android::hardware::tv::tuner::V1_0::DvrType;
49 using android::hardware::tv::tuner::V1_0::FrontendDvbsSettings;
50 using android::hardware::tv::tuner::V1_0::FrontendDvbtBandwidth;
51 using android::hardware::tv::tuner::V1_0::FrontendDvbtCoderate;
52 using android::hardware::tv::tuner::V1_0::FrontendDvbtConstellation;
53 using android::hardware::tv::tuner::V1_0::FrontendDvbtGuardInterval;
54 using android::hardware::tv::tuner::V1_0::FrontendDvbtHierarchy;
55 using android::hardware::tv::tuner::V1_0::FrontendDvbtPlpMode;
56 using android::hardware::tv::tuner::V1_0::FrontendDvbtSettings;
57 using android::hardware::tv::tuner::V1_0::FrontendDvbtStandard;
58 using android::hardware::tv::tuner::V1_0::FrontendDvbtTransmissionMode;
59 using android::hardware::tv::tuner::V1_0::FrontendSettings;
60 using android::hardware::tv::tuner::V1_0::FrontendStatus;
61 using android::hardware::tv::tuner::V1_0::FrontendStatusType;
62 using android::hardware::tv::tuner::V1_0::FrontendType;
63 using android::hardware::tv::tuner::V1_0::LnbPosition;
64 using android::hardware::tv::tuner::V1_0::LnbTone;
65 using android::hardware::tv::tuner::V1_0::LnbVoltage;
66 using android::hardware::tv::tuner::V1_0::PlaybackSettings;
67 using android::hardware::tv::tuner::V1_0::RecordSettings;
68 using android::hardware::tv::tuner::V1_0::FrontendAtscSettings;
69 
70 const string emptyHardwareId = "";
71 
72 static string mConfigFilePath;
73 
74 #define PROVISION_STR                                      \
75     "{                                                   " \
76     "  \"id\": 21140844,                                 " \
77     "  \"name\": \"Test Title\",                         " \
78     "  \"lowercase_organization_name\": \"Android\",     " \
79     "  \"asset_key\": {                                  " \
80     "  \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\"  " \
81     "  },                                                " \
82     "  \"cas_type\": 1,                                  " \
83     "  \"track_types\": [ ]                              " \
84     "}                                                   "
85 
86 struct FrontendConfig {
87     bool isSoftwareFe;
88     FrontendType type;
89     FrontendSettings settings;
90     vector<FrontendStatusType> tuneStatusTypes;
91     vector<FrontendStatus> expectTuneStatuses;
92 };
93 
94 struct FilterConfig {
95     uint32_t bufferSize;
96     DemuxFilterType type;
97     DemuxFilterSettings settings;
98     bool getMqDesc;
99 
100     bool operator<(const FilterConfig& /*c*/) const { return false; }
101 };
102 
103 struct DvrConfig {
104     DvrType type;
105     uint32_t bufferSize;
106     DvrSettings settings;
107     string playbackInputFile;
108 };
109 
110 struct LnbConfig {
111     string name;
112     LnbVoltage voltage;
113     LnbTone tone;
114     LnbPosition position;
115 };
116 
117 struct TimeFilterConfig {
118     uint64_t timeStamp;
119 };
120 
121 struct DescramblerConfig {
122     uint32_t casSystemId;
123     string provisionStr;
124     vector<uint8_t> hidlPvtData;
125 };
126 
127 struct LiveBroadcastHardwareConnections {
128     bool hasFrontendConnection;
129     string frontendId;
130     string dvrSoftwareFeId;
131     string audioFilterId;
132     string videoFilterId;
133     string sectionFilterId;
134     string ipFilterId;
135     string pcrFilterId;
136     /* list string of extra filters; */
137 };
138 
139 struct ScanHardwareConnections {
140     bool hasFrontendConnection;
141     string frontendId;
142 };
143 
144 struct DvrPlaybackHardwareConnections {
145     bool support;
146     string frontendId;
147     string dvrId;
148     string audioFilterId;
149     string videoFilterId;
150     string sectionFilterId;
151     /* list string of extra filters; */
152 };
153 
154 struct DvrRecordHardwareConnections {
155     bool support;
156     bool hasFrontendConnection;
157     string frontendId;
158     string dvrRecordId;
159     string dvrSoftwareFeId;
160     string recordFilterId;
161     string dvrSourceId;
162 };
163 
164 struct DescramblingHardwareConnections {
165     bool support;
166     bool hasFrontendConnection;
167     string frontendId;
168     string dvrSoftwareFeId;
169     string audioFilterId;
170     string videoFilterId;
171     string descramblerId;
172     string dvrSourceId;
173     /* list string of extra filters; */
174 };
175 
176 struct LnbLiveHardwareConnections {
177     bool support;
178     string frontendId;
179     string audioFilterId;
180     string videoFilterId;
181     string lnbId;
182     vector<string> diseqcMsgs;
183     /* list string of extra filters; */
184 };
185 
186 struct LnbRecordHardwareConnections {
187     bool support;
188     string frontendId;
189     string dvrRecordId;
190     string recordFilterId;
191     string lnbId;
192     vector<string> diseqcMsgs;
193     /* list string of extra filters; */
194 };
195 
196 struct TimeFilterHardwareConnections {
197     bool support;
198     string timeFilterId;
199 };
200 
201 struct TunerTestingConfigReader1_0 {
202   public:
setConfigFilePathTunerTestingConfigReader1_0203     static void setConfigFilePath(string path) { mConfigFilePath = path; }
204 
checkConfigFileExistsTunerTestingConfigReader1_0205     static bool checkConfigFileExists() {
206         auto res = read(mConfigFilePath.c_str());
207         if (res == nullopt) {
208             ALOGW("[ConfigReader] Couldn't read %s."
209                   "Please check tuner_testing_dynamic_configuration.xsd"
210                   "and sample_tuner_vts_config.xml for more details on how to config Tune VTS.",
211                   mConfigFilePath.c_str());
212         }
213         return (res != nullopt);
214     }
215 
readFrontendConfig1_0TunerTestingConfigReader1_0216     static void readFrontendConfig1_0(map<string, FrontendConfig>& frontendMap) {
217         auto hardwareConfig = getHardwareConfig();
218         if (hardwareConfig.hasFrontends()) {
219             // TODO: b/182519645 complete the tune status config
220             vector<FrontendStatusType> types;
221             types.push_back(FrontendStatusType::DEMOD_LOCK);
222             FrontendStatus status;
223             status.isDemodLocked(true);
224             vector<FrontendStatus> statuses;
225             statuses.push_back(status);
226 
227             auto frontends = *hardwareConfig.getFirstFrontends();
228             for (auto feConfig : frontends.getFrontend()) {
229                 string id = feConfig.getId();
230                 if (id.compare(string("FE_DEFAULT")) == 0) {
231                     // overrid default
232                     frontendMap.erase(string("FE_DEFAULT"));
233                 }
234                 FrontendType type;
235                 switch (feConfig.getType()) {
236                     case FrontendTypeEnum::UNDEFINED:
237                         type = FrontendType::UNDEFINED;
238                         break;
239                     // TODO: b/182519645 finish all other frontend settings
240                     case FrontendTypeEnum::ANALOG:
241                         type = FrontendType::ANALOG;
242                         break;
243                     case FrontendTypeEnum::ATSC:
244                         type = FrontendType::ATSC;
245                         frontendMap[id].settings.atsc(readAtscFrontendSettings(feConfig));
246                         break;
247                     case FrontendTypeEnum::ATSC3:
248                         type = FrontendType::ATSC3;
249                         break;
250                     case FrontendTypeEnum::DVBC:
251                         type = FrontendType::DVBC;
252                         break;
253                     case FrontendTypeEnum::DVBS:
254                         type = FrontendType::DVBS;
255                         frontendMap[id].settings.dvbs(readDvbsFrontendSettings(feConfig));
256                         break;
257                     case FrontendTypeEnum::DVBT: {
258                         type = FrontendType::DVBT;
259                         frontendMap[id].settings.dvbt(readDvbtFrontendSettings(feConfig));
260                         break;
261                     }
262                     case FrontendTypeEnum::ISDBS:
263                         type = FrontendType::ISDBS;
264                         break;
265                     case FrontendTypeEnum::ISDBS3:
266                         type = FrontendType::ISDBS3;
267                         break;
268                     case FrontendTypeEnum::ISDBT:
269                         type = FrontendType::ISDBT;
270                         break;
271                     case FrontendTypeEnum::DTMB:
272                         // dtmb will be handled in readFrontendConfig1_1;
273                         continue;
274                     case FrontendTypeEnum::UNKNOWN:
275                         ALOGW("[ConfigReader] invalid frontend type");
276                         return;
277                 }
278                 frontendMap[id].type = type;
279                 frontendMap[id].isSoftwareFe = feConfig.getIsSoftwareFrontend();
280                 // TODO: b/182519645 complete the tune status config
281                 frontendMap[id].tuneStatusTypes = types;
282                 frontendMap[id].expectTuneStatuses = statuses;
283             }
284         }
285     }
286 
readFilterConfig1_0TunerTestingConfigReader1_0287     static void readFilterConfig1_0(map<string, FilterConfig>& filterMap) {
288         auto hardwareConfig = getHardwareConfig();
289         if (hardwareConfig.hasFilters()) {
290             auto filters = *hardwareConfig.getFirstFilters();
291             for (auto filterConfig : filters.getFilter()) {
292                 string id = filterConfig.getId();
293                 if (id.compare(string("FILTER_AUDIO_DEFAULT")) == 0) {
294                     // overrid default
295                     filterMap.erase(string("FILTER_AUDIO_DEFAULT"));
296                 }
297                 if (id.compare(string("FILTER_VIDEO_DEFAULT")) == 0) {
298                     // overrid default
299                     filterMap.erase(string("FILTER_VIDEO_DEFAULT"));
300                 }
301 
302                 DemuxFilterType type;
303                 DemuxFilterSettings settings;
304                 if (!readFilterTypeAndSettings(filterConfig, type, settings)) {
305                     ALOGW("[ConfigReader] invalid filter type");
306                     return;
307                 }
308                 filterMap[id].type = type;
309                 filterMap[id].bufferSize = filterConfig.getBufferSize();
310                 filterMap[id].getMqDesc = filterConfig.getUseFMQ();
311                 filterMap[id].settings = settings;
312             }
313         }
314     }
315 
readDvrConfig1_0TunerTestingConfigReader1_0316     static void readDvrConfig1_0(map<string, DvrConfig>& dvrMap) {
317         auto hardwareConfig = getHardwareConfig();
318         if (hardwareConfig.hasDvrs()) {
319             auto dvrs = *hardwareConfig.getFirstDvrs();
320             for (auto dvrConfig : dvrs.getDvr()) {
321                 string id = dvrConfig.getId();
322                 DvrType type;
323                 switch (dvrConfig.getType()) {
324                     case DvrTypeEnum::PLAYBACK:
325                         type = DvrType::PLAYBACK;
326                         dvrMap[id].settings.playback(readPlaybackSettings(dvrConfig));
327                         break;
328                     case DvrTypeEnum::RECORD:
329                         type = DvrType::RECORD;
330                         dvrMap[id].settings.record(readRecordSettings(dvrConfig));
331                         break;
332                     case DvrTypeEnum::UNKNOWN:
333                         ALOGW("[ConfigReader] invalid DVR type");
334                         return;
335                 }
336                 dvrMap[id].type = type;
337                 dvrMap[id].bufferSize = static_cast<uint32_t>(dvrConfig.getBufferSize());
338                 if (dvrConfig.hasInputFilePath()) {
339                     dvrMap[id].playbackInputFile = dvrConfig.getInputFilePath();
340                 }
341             }
342         }
343     }
344 
readLnbConfig1_0TunerTestingConfigReader1_0345     static void readLnbConfig1_0(map<string, LnbConfig>& lnbMap) {
346         auto hardwareConfig = getHardwareConfig();
347         if (hardwareConfig.hasLnbs()) {
348             auto lnbs = *hardwareConfig.getFirstLnbs();
349             for (auto lnbConfig : lnbs.getLnb()) {
350                 string id = lnbConfig.getId();
351                 if (lnbConfig.hasName()) {
352                     lnbMap[id].name = lnbConfig.getName();
353                 } else {
354                     lnbMap[id].name = emptyHardwareId;
355                 }
356                 lnbMap[id].voltage = static_cast<LnbVoltage>(lnbConfig.getVoltage());
357                 lnbMap[id].tone = static_cast<LnbTone>(lnbConfig.getTone());
358                 lnbMap[id].position = static_cast<LnbPosition>(lnbConfig.getPosition());
359             }
360         }
361     }
362 
readDescramblerConfig1_0TunerTestingConfigReader1_0363     static void readDescramblerConfig1_0(map<string, DescramblerConfig>& descramblerMap) {
364         auto hardwareConfig = getHardwareConfig();
365         if (hardwareConfig.hasDescramblers()) {
366             auto descramblers = *hardwareConfig.getFirstDescramblers();
367             for (auto descramblerConfig : descramblers.getDescrambler()) {
368                 string id = descramblerConfig.getId();
369                 descramblerMap[id].casSystemId =
370                         static_cast<uint32_t>(descramblerConfig.getCasSystemId());
371                 if (descramblerConfig.hasProvisionStr()) {
372                     descramblerMap[id].provisionStr = descramblerConfig.getProvisionStr();
373                 } else {
374                     descramblerMap[id].provisionStr = PROVISION_STR;
375                 }
376                 if (descramblerConfig.hasSesstionPrivatData()) {
377                     auto privateData = descramblerConfig.getSesstionPrivatData();
378                     int size = privateData.size();
379                     descramblerMap[id].hidlPvtData.resize(size);
380                     memcpy(descramblerMap[id].hidlPvtData.data(), privateData.data(), size);
381                 } else {
382                     descramblerMap[id].hidlPvtData.resize(256);
383                 }
384             }
385         }
386     }
387 
readDiseqcMessagesTunerTestingConfigReader1_0388     static void readDiseqcMessages(map<string, vector<uint8_t>>& diseqcMsgMap) {
389         auto hardwareConfig = getHardwareConfig();
390         if (hardwareConfig.hasDiseqcMessages()) {
391             auto msgs = *hardwareConfig.getFirstDiseqcMessages();
392             for (auto msgConfig : msgs.getDiseqcMessage()) {
393                 string name = msgConfig.getMsgName();
394                 for (uint8_t atom : msgConfig.getMsgBody()) {
395                     diseqcMsgMap[name].push_back(atom);
396                 }
397             }
398         }
399     }
400 
readTimeFilterConfig1_0TunerTestingConfigReader1_0401     static void readTimeFilterConfig1_0(map<string, TimeFilterConfig>& timeFilterMap) {
402         auto hardwareConfig = getHardwareConfig();
403         if (hardwareConfig.hasTimeFilters()) {
404             auto timeFilters = *hardwareConfig.getFirstTimeFilters();
405             for (auto timeFilterConfig : timeFilters.getTimeFilter()) {
406                 string id = timeFilterConfig.getId();
407                 timeFilterMap[id].timeStamp =
408                         static_cast<uint64_t>(timeFilterConfig.getTimeStamp());
409             }
410         }
411     }
412 
connectLiveBroadcastTunerTestingConfigReader1_0413     static void connectLiveBroadcast(LiveBroadcastHardwareConnections& live) {
414         auto dataFlow = getDataFlowConfiguration();
415         if (dataFlow.hasClearLiveBroadcast()) {
416             live.hasFrontendConnection = true;
417         } else {
418             live.hasFrontendConnection = false;
419             return;
420         }
421         auto liveConfig = *dataFlow.getFirstClearLiveBroadcast();
422         live.frontendId = liveConfig.getFrontendConnection();
423 
424         live.audioFilterId = liveConfig.getAudioFilterConnection();
425         live.videoFilterId = liveConfig.getVideoFilterConnection();
426         if (liveConfig.hasPcrFilterConnection()) {
427             live.pcrFilterId = liveConfig.getPcrFilterConnection();
428         } else {
429             live.pcrFilterId = emptyHardwareId;
430         }
431         if (liveConfig.hasSectionFilterConnection()) {
432             live.sectionFilterId = liveConfig.getSectionFilterConnection();
433         } else {
434             live.sectionFilterId = emptyHardwareId;
435         }
436         if (liveConfig.hasDvrSoftwareFeConnection()) {
437             live.dvrSoftwareFeId = liveConfig.getDvrSoftwareFeConnection();
438         }
439         if (liveConfig.hasIpFilterConnection()) {
440             live.ipFilterId = liveConfig.getIpFilterConnection();
441         } else {
442             live.ipFilterId = emptyHardwareId;
443         }
444     }
445 
connectScanTunerTestingConfigReader1_0446     static void connectScan(ScanHardwareConnections& scan) {
447         auto dataFlow = getDataFlowConfiguration();
448         if (dataFlow.hasScan()) {
449             scan.hasFrontendConnection = true;
450         } else {
451             scan.hasFrontendConnection = false;
452             return;
453         }
454         auto scanConfig = *dataFlow.getFirstScan();
455         scan.frontendId = scanConfig.getFrontendConnection();
456     }
457 
connectDvrPlaybackTunerTestingConfigReader1_0458     static void connectDvrPlayback(DvrPlaybackHardwareConnections& playback) {
459         auto dataFlow = getDataFlowConfiguration();
460         if (dataFlow.hasDvrPlayback()) {
461             playback.support = true;
462         } else {
463             playback.support = false;
464             return;
465         }
466         auto playbackConfig = *dataFlow.getFirstDvrPlayback();
467         playback.dvrId = playbackConfig.getDvrConnection();
468         playback.audioFilterId = playbackConfig.getAudioFilterConnection();
469         playback.videoFilterId = playbackConfig.getVideoFilterConnection();
470         if (playbackConfig.hasSectionFilterConnection()) {
471             playback.sectionFilterId = playbackConfig.getSectionFilterConnection();
472         } else {
473             playback.sectionFilterId = emptyHardwareId;
474         }
475     }
476 
connectDvrRecordTunerTestingConfigReader1_0477     static void connectDvrRecord(DvrRecordHardwareConnections& record) {
478         auto dataFlow = getDataFlowConfiguration();
479         if (dataFlow.hasDvrRecord()) {
480             record.support = true;
481         } else {
482             record.support = false;
483             return;
484         }
485         auto recordConfig = *dataFlow.getFirstDvrRecord();
486         record.recordFilterId = recordConfig.getRecordFilterConnection();
487         record.dvrRecordId = recordConfig.getDvrRecordConnection();
488         if (recordConfig.hasDvrSoftwareFeConnection()) {
489             record.dvrSoftwareFeId = recordConfig.getDvrSoftwareFeConnection();
490         }
491         if (recordConfig.getHasFrontendConnection()) {
492             record.hasFrontendConnection = true;
493             record.dvrSourceId = emptyHardwareId;
494             record.frontendId = recordConfig.getFrontendConnection();
495         } else {
496             record.hasFrontendConnection = false;
497             record.dvrSourceId = recordConfig.getDvrSourceConnection();
498         }
499     }
500 
connectDescramblingTunerTestingConfigReader1_0501     static void connectDescrambling(DescramblingHardwareConnections& descrambling) {
502         auto dataFlow = getDataFlowConfiguration();
503         if (dataFlow.hasDescrambling()) {
504             descrambling.support = true;
505         } else {
506             descrambling.support = false;
507             return;
508         }
509         auto descConfig = *dataFlow.getFirstDescrambling();
510         descrambling.descramblerId = descConfig.getDescramblerConnection();
511         descrambling.audioFilterId = descConfig.getAudioFilterConnection();
512         descrambling.videoFilterId = descConfig.getVideoFilterConnection();
513         if (descConfig.hasDvrSoftwareFeConnection()) {
514             descrambling.dvrSoftwareFeId = descConfig.getDvrSoftwareFeConnection();
515         }
516         if (descConfig.getHasFrontendConnection()) {
517             descrambling.hasFrontendConnection = true;
518             descrambling.dvrSourceId = emptyHardwareId;
519             descrambling.frontendId = descConfig.getFrontendConnection();
520         } else {
521             descrambling.hasFrontendConnection = false;
522             descrambling.dvrSourceId = descConfig.getDvrSourceConnection();
523         }
524     }
525 
connectLnbLiveTunerTestingConfigReader1_0526     static void connectLnbLive(LnbLiveHardwareConnections& lnbLive) {
527         auto dataFlow = getDataFlowConfiguration();
528         if (dataFlow.hasLnbLive()) {
529             lnbLive.support = true;
530         } else {
531             lnbLive.support = false;
532             return;
533         }
534         auto lnbLiveConfig = *dataFlow.getFirstLnbLive();
535         lnbLive.frontendId = lnbLiveConfig.getFrontendConnection();
536         lnbLive.audioFilterId = lnbLiveConfig.getAudioFilterConnection();
537         lnbLive.videoFilterId = lnbLiveConfig.getVideoFilterConnection();
538         lnbLive.lnbId = lnbLiveConfig.getLnbConnection();
539         if (lnbLiveConfig.hasDiseqcMsgSender()) {
540             for (auto msgName : lnbLiveConfig.getDiseqcMsgSender()) {
541                 lnbLive.diseqcMsgs.push_back(msgName);
542             }
543         }
544     }
545 
connectLnbRecordTunerTestingConfigReader1_0546     static void connectLnbRecord(LnbRecordHardwareConnections& lnbRecord) {
547         auto dataFlow = getDataFlowConfiguration();
548         if (dataFlow.hasLnbRecord()) {
549             lnbRecord.support = true;
550         } else {
551             lnbRecord.support = false;
552             return;
553         }
554         auto lnbRecordConfig = *dataFlow.getFirstLnbRecord();
555         lnbRecord.frontendId = lnbRecordConfig.getFrontendConnection();
556         lnbRecord.recordFilterId = lnbRecordConfig.getRecordFilterConnection();
557         lnbRecord.dvrRecordId = lnbRecordConfig.getDvrRecordConnection();
558         lnbRecord.lnbId = lnbRecordConfig.getLnbConnection();
559         if (lnbRecordConfig.hasDiseqcMsgSender()) {
560             for (auto msgName : lnbRecordConfig.getDiseqcMsgSender()) {
561                 lnbRecord.diseqcMsgs.push_back(msgName);
562             }
563         }
564     }
565 
connectTimeFilterTunerTestingConfigReader1_0566     static void connectTimeFilter(TimeFilterHardwareConnections& timeFilter) {
567         auto dataFlow = getDataFlowConfiguration();
568         if (dataFlow.hasTimeFilter()) {
569             timeFilter.support = true;
570         } else {
571             timeFilter.support = false;
572             return;
573         }
574         auto timeFilterConfig = *dataFlow.getFirstTimeFilter();
575         timeFilter.timeFilterId = timeFilterConfig.getTimeFilterConnection();
576     }
577 
getHardwareConfigTunerTestingConfigReader1_0578     static HardwareConfiguration getHardwareConfig() {
579         return *getTunerConfig().getFirstHardwareConfiguration();
580     }
581 
582   private:
readDvbtFrontendSettingsTunerTestingConfigReader1_0583     static FrontendDvbtSettings readDvbtFrontendSettings(Frontend feConfig) {
584         ALOGW("[ConfigReader] fe type is dvbt");
585         FrontendDvbtSettings dvbtSettings{
586                 .frequency = (uint32_t)feConfig.getFrequency(),
587         };
588         if (!feConfig.hasDvbtFrontendSettings_optional()) {
589             ALOGW("[ConfigReader] no more dvbt settings");
590             return dvbtSettings;
591         }
592         auto dvbt = feConfig.getFirstDvbtFrontendSettings_optional();
593         uint32_t trans = static_cast<uint32_t>(dvbt->getTransmissionMode());
594         if (trans <= (uint32_t)FrontendDvbtTransmissionMode::MODE_32K) {
595             dvbtSettings.transmissionMode = static_cast<FrontendDvbtTransmissionMode>(trans);
596         }
597         dvbtSettings.bandwidth = static_cast<FrontendDvbtBandwidth>(dvbt->getBandwidth());
598         dvbtSettings.isHighPriority = dvbt->getIsHighPriority();
599         dvbtSettings.hierarchy = static_cast<FrontendDvbtHierarchy>(dvbt->getHierarchy());
600         dvbtSettings.hpCoderate = static_cast<FrontendDvbtCoderate>(dvbt->getHpCoderate());
601         dvbtSettings.lpCoderate = static_cast<FrontendDvbtCoderate>(dvbt->getLpCoderate());
602         dvbtSettings.guardInterval =
603                 static_cast<FrontendDvbtGuardInterval>(dvbt->getGuardInterval());
604         dvbtSettings.standard = static_cast<FrontendDvbtStandard>(dvbt->getStandard());
605         dvbtSettings.isMiso = dvbt->getIsMiso();
606         dvbtSettings.plpMode = static_cast<FrontendDvbtPlpMode>(dvbt->getPlpMode());
607         dvbtSettings.plpId = dvbt->getPlpId();
608         dvbtSettings.plpGroupId = dvbt->getPlpGroupId();
609         if (dvbt->hasConstellation()) {
610             dvbtSettings.constellation =
611                     static_cast<FrontendDvbtConstellation>(dvbt->getConstellation());
612         }
613         return dvbtSettings;
614     }
615 
readDvbsFrontendSettingsTunerTestingConfigReader1_0616     static FrontendDvbsSettings readDvbsFrontendSettings(Frontend feConfig) {
617         ALOGW("[ConfigReader] fe type is dvbs");
618         FrontendDvbsSettings dvbsSettings{
619                 .frequency = (uint32_t)feConfig.getFrequency(),
620         };
621         if (!feConfig.hasDvbsFrontendSettings_optional()) {
622             ALOGW("[ConfigReader] no more dvbs settings");
623             return dvbsSettings;
624         }
625         dvbsSettings.symbolRate = static_cast<uint32_t>(
626                 feConfig.getFirstDvbsFrontendSettings_optional()->getSymbolRate());
627         dvbsSettings.inputStreamId = static_cast<uint32_t>(
628                 feConfig.getFirstDvbsFrontendSettings_optional()->getInputStreamId());
629         return dvbsSettings;
630     }
631 
readAtscFrontendSettingsTunerTestingConfigReader1_0632     static FrontendAtscSettings readAtscFrontendSettings(Frontend feConfig) {
633         FrontendAtscSettings atscSettings{
634             .frequency = (uint32_t)feConfig.getFrequency(),
635         };
636         return atscSettings;
637     }
638 
readFilterTypeAndSettingsTunerTestingConfigReader1_0639     static bool readFilterTypeAndSettings(Filter filterConfig, DemuxFilterType& type,
640                                           DemuxFilterSettings& settings) {
641         auto mainType = filterConfig.getMainType();
642         auto subType = filterConfig.getSubType();
643         switch (mainType) {
644             case FilterMainTypeEnum::TS: {
645                 ALOGW("[ConfigReader] filter main type is ts");
646                 type.mainType = DemuxFilterMainType::TS;
647                 switch (subType) {
648                     case FilterSubTypeEnum::UNDEFINED:
649                         type.subType.tsFilterType(DemuxTsFilterType::UNDEFINED);
650                         break;
651                     case FilterSubTypeEnum::SECTION:
652                         type.subType.tsFilterType(DemuxTsFilterType::SECTION);
653                         settings.ts().filterSettings.section(
654                                 readSectionFilterSettings(filterConfig));
655                         break;
656                     case FilterSubTypeEnum::PES:
657                         // TODO: b/182519645 support all the filter settings
658                         /*settings.ts().filterSettings.pesData(
659                                 getPesFilterSettings(filterConfig));*/
660                         type.subType.tsFilterType(DemuxTsFilterType::PES);
661                         break;
662                     case FilterSubTypeEnum::TS:
663                         type.subType.tsFilterType(DemuxTsFilterType::TS);
664                         settings.ts().filterSettings.noinit();
665                         break;
666                     case FilterSubTypeEnum::PCR:
667                         type.subType.tsFilterType(DemuxTsFilterType::PCR);
668                         settings.ts().filterSettings.noinit();
669                         break;
670                     case FilterSubTypeEnum::TEMI:
671                         type.subType.tsFilterType(DemuxTsFilterType::TEMI);
672                         settings.ts().filterSettings.noinit();
673                         break;
674                     case FilterSubTypeEnum::AUDIO:
675                         type.subType.tsFilterType(DemuxTsFilterType::AUDIO);
676                         settings.ts().filterSettings.av(readAvFilterSettings(filterConfig));
677                         break;
678                     case FilterSubTypeEnum::VIDEO:
679                         type.subType.tsFilterType(DemuxTsFilterType::VIDEO);
680                         settings.ts().filterSettings.av(readAvFilterSettings(filterConfig));
681                         break;
682                     case FilterSubTypeEnum::RECORD:
683                         type.subType.tsFilterType(DemuxTsFilterType::RECORD);
684                         settings.ts().filterSettings.record(readRecordFilterSettings(filterConfig));
685                         break;
686                     default:
687                         ALOGW("[ConfigReader] ts subtype is not supported");
688                         return false;
689                 }
690                 if (filterConfig.hasPid()) {
691                     settings.ts().tpid = static_cast<uint32_t>(filterConfig.getPid());
692                 }
693                 break;
694             }
695             case FilterMainTypeEnum::MMTP: {
696                 ALOGW("[ConfigReader] filter main type is mmtp");
697                 type.mainType = DemuxFilterMainType::MMTP;
698                 switch (subType) {
699                     case FilterSubTypeEnum::UNDEFINED:
700                         type.subType.mmtpFilterType(DemuxMmtpFilterType::UNDEFINED);
701                         break;
702                     case FilterSubTypeEnum::SECTION:
703                         type.subType.mmtpFilterType(DemuxMmtpFilterType::SECTION);
704                         settings.mmtp().filterSettings.section(
705                                 readSectionFilterSettings(filterConfig));
706                         break;
707                     case FilterSubTypeEnum::PES:
708                         type.subType.mmtpFilterType(DemuxMmtpFilterType::PES);
709                         // TODO: b/182519645 support all the filter settings
710                         /*settings.mmtp().filterSettings.pesData(
711                                 getPesFilterSettings(filterConfig));*/
712                         break;
713                     case FilterSubTypeEnum::MMTP:
714                         type.subType.mmtpFilterType(DemuxMmtpFilterType::MMTP);
715                         settings.mmtp().filterSettings.noinit();
716                         break;
717                     case FilterSubTypeEnum::AUDIO:
718                         type.subType.mmtpFilterType(DemuxMmtpFilterType::AUDIO);
719                         settings.mmtp().filterSettings.av(readAvFilterSettings(filterConfig));
720                         break;
721                     case FilterSubTypeEnum::VIDEO:
722                         settings.mmtp().filterSettings.av(readAvFilterSettings(filterConfig));
723                         break;
724                     case FilterSubTypeEnum::RECORD:
725                         type.subType.mmtpFilterType(DemuxMmtpFilterType::RECORD);
726                         settings.mmtp().filterSettings.record(
727                                 readRecordFilterSettings(filterConfig));
728                         break;
729                     case FilterSubTypeEnum::DOWNLOAD:
730                         type.subType.mmtpFilterType(DemuxMmtpFilterType::DOWNLOAD);
731                         // TODO: b/182519645 support all the filter settings
732                         /*settings.mmtp().filterSettings.download(
733                                 getDownloadFilterSettings(filterConfig));*/
734                         break;
735                     default:
736                         ALOGW("[ConfigReader] mmtp subtype is not supported");
737                         return false;
738                 }
739                 if (filterConfig.hasPid()) {
740                     settings.mmtp().mmtpPid = static_cast<uint32_t>(filterConfig.getPid());
741                 }
742                 break;
743             }
744             case FilterMainTypeEnum::IP: {
745                 ALOGW("[ConfigReader] filter main type is ip");
746                 type.mainType = DemuxFilterMainType::IP;
747                 switch (subType) {
748                     case FilterSubTypeEnum::UNDEFINED:
749                         type.subType.ipFilterType(DemuxIpFilterType::UNDEFINED);
750                         break;
751                     case FilterSubTypeEnum::SECTION:
752                         type.subType.ipFilterType(DemuxIpFilterType::SECTION);
753                         settings.ip().filterSettings.section(
754                                 readSectionFilterSettings(filterConfig));
755                         break;
756                     case FilterSubTypeEnum::NTP:
757                         type.subType.ipFilterType(DemuxIpFilterType::NTP);
758                         settings.ip().filterSettings.noinit();
759                         break;
760                     case FilterSubTypeEnum::IP: {
761                         DemuxIpFilterSettings ip{
762                                 .ipAddr = readIpAddress(filterConfig),
763                         };
764                         ip.filterSettings.bPassthrough(readPassthroughSettings(filterConfig));
765                         settings.ip(ip);
766                         break;
767                     }
768                     case FilterSubTypeEnum::IP_PAYLOAD:
769                         type.subType.ipFilterType(DemuxIpFilterType::IP_PAYLOAD);
770                         settings.ip().filterSettings.noinit();
771                         break;
772                     case FilterSubTypeEnum::PAYLOAD_THROUGH:
773                         type.subType.ipFilterType(DemuxIpFilterType::PAYLOAD_THROUGH);
774                         settings.ip().filterSettings.noinit();
775                         break;
776                     default:
777                         ALOGW("[ConfigReader] mmtp subtype is not supported");
778                         return false;
779                 }
780                 break;
781             }
782             default:
783                 // TODO: b/182519645 support all the filter configs
784                 ALOGW("[ConfigReader] filter main type is not supported in dynamic config");
785                 return false;
786         }
787         return true;
788     }
789 
readIpAddressTunerTestingConfigReader1_0790     static DemuxIpAddress readIpAddress(Filter filterConfig) {
791         DemuxIpAddress ipAddress;
792         if (!filterConfig.hasIpFilterConfig_optional()) {
793             return ipAddress;
794         }
795         auto ipFilterConfig = filterConfig.getFirstIpFilterConfig_optional();
796         if (ipFilterConfig->hasSrcPort()) {
797             ipAddress.srcPort = ipFilterConfig->getSrcPort();
798         }
799         if (ipFilterConfig->hasDestPort()) {
800             ipAddress.dstPort = ipFilterConfig->getDestPort();
801         }
802         if (ipFilterConfig->getFirstSrcIpAddress()->getIsIpV4()) {
803             memcpy(ipAddress.srcIpAddress.v4().data(),
804                    ipFilterConfig->getFirstSrcIpAddress()->getIp().data(), 4);
805         } else {
806             memcpy(ipAddress.srcIpAddress.v6().data(),
807                    ipFilterConfig->getFirstSrcIpAddress()->getIp().data(), 6);
808         }
809         if (ipFilterConfig->getFirstDestIpAddress()->getIsIpV4()) {
810             memcpy(ipAddress.dstIpAddress.v4().data(),
811                    ipFilterConfig->getFirstDestIpAddress()->getIp().data(), 4);
812         } else {
813             memcpy(ipAddress.dstIpAddress.v6().data(),
814                    ipFilterConfig->getFirstDestIpAddress()->getIp().data(), 6);
815         }
816         return ipAddress;
817     }
818 
readPassthroughSettingsTunerTestingConfigReader1_0819     static bool readPassthroughSettings(Filter filterConfig) {
820         if (!filterConfig.hasIpFilterConfig_optional()) {
821             return false;
822         }
823         auto ipFilterConfig = filterConfig.getFirstIpFilterConfig_optional();
824         if (ipFilterConfig->hasDataPassthrough()) {
825             return ipFilterConfig->getDataPassthrough();
826         }
827         return false;
828     }
829 
readSectionFilterSettingsTunerTestingConfigReader1_0830     static DemuxFilterSectionSettings readSectionFilterSettings(Filter filterConfig) {
831         DemuxFilterSectionSettings settings;
832         if (!filterConfig.hasSectionFilterSettings_optional()) {
833             return settings;
834         }
835         auto section = filterConfig.getFirstSectionFilterSettings_optional();
836         settings.isCheckCrc = section->getIsCheckCrc();
837         settings.isRepeat = section->getIsRepeat();
838         settings.isRaw = section->getIsRaw();
839         return settings;
840     }
841 
readAvFilterSettingsTunerTestingConfigReader1_0842     static DemuxFilterAvSettings readAvFilterSettings(Filter filterConfig) {
843         DemuxFilterAvSettings settings;
844         if (!filterConfig.hasAvFilterSettings_optional()) {
845             return settings;
846         }
847         auto av = filterConfig.getFirstAvFilterSettings_optional();
848         settings.isPassthrough = av->getIsPassthrough();
849         return settings;
850     }
851 
readRecordFilterSettingsTunerTestingConfigReader1_0852     static DemuxFilterRecordSettings readRecordFilterSettings(Filter filterConfig) {
853         DemuxFilterRecordSettings settings;
854         if (!filterConfig.hasRecordFilterSettings_optional()) {
855             return settings;
856         }
857         auto record = filterConfig.getFirstRecordFilterSettings_optional();
858         settings.tsIndexMask = static_cast<uint32_t>(record->getTsIndexMask());
859         settings.scIndexType = static_cast<DemuxRecordScIndexType>(record->getScIndexType());
860         return settings;
861     }
862 
readPlaybackSettingsTunerTestingConfigReader1_0863     static PlaybackSettings readPlaybackSettings(Dvr dvrConfig) {
864         ALOGW("[ConfigReader] dvr type is playback");
865         PlaybackSettings playbackSettings{
866                 .statusMask = static_cast<uint8_t>(dvrConfig.getStatusMask()),
867                 .lowThreshold = static_cast<uint32_t>(dvrConfig.getLowThreshold()),
868                 .highThreshold = static_cast<uint32_t>(dvrConfig.getHighThreshold()),
869                 .dataFormat = static_cast<DataFormat>(dvrConfig.getDataFormat()),
870                 .packetSize = static_cast<uint8_t>(dvrConfig.getPacketSize()),
871         };
872         return playbackSettings;
873     }
874 
readRecordSettingsTunerTestingConfigReader1_0875     static RecordSettings readRecordSettings(Dvr dvrConfig) {
876         ALOGW("[ConfigReader] dvr type is record");
877         RecordSettings recordSettings{
878                 .statusMask = static_cast<uint8_t>(dvrConfig.getStatusMask()),
879                 .lowThreshold = static_cast<uint32_t>(dvrConfig.getLowThreshold()),
880                 .highThreshold = static_cast<uint32_t>(dvrConfig.getHighThreshold()),
881                 .dataFormat = static_cast<DataFormat>(dvrConfig.getDataFormat()),
882                 .packetSize = static_cast<uint8_t>(dvrConfig.getPacketSize()),
883         };
884         return recordSettings;
885     }
886 
getTunerConfigTunerTestingConfigReader1_0887     static TunerConfiguration getTunerConfig() { return *read(mConfigFilePath.c_str()); }
888 
getDataFlowConfigurationTunerTestingConfigReader1_0889     static DataFlowConfiguration getDataFlowConfiguration() {
890         return *getTunerConfig().getFirstDataFlowConfiguration();
891     }
892 };
893