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 #pragma once
18 
19 #include <aidl/android/hardware/tv/tuner/BnFrontend.h>
20 #include <fstream>
21 #include <iostream>
22 #include <thread>
23 #include "Tuner.h"
24 #include "dtv_plugin.h"
25 
26 using namespace std;
27 
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace tv {
32 namespace tuner {
33 
34 class Tuner;
35 
36 const int TUNE_BUFFER_SIZE = 1;        // byte
37 const int TUNE_BUFFER_TIMEOUT = 2000;  // ms
38 
39 class Frontend : public BnFrontend {
40   public:
41     Frontend(FrontendType type, int32_t id);
42 
43     ::ndk::ScopedAStatus setCallback(
44             const std::shared_ptr<IFrontendCallback>& in_callback) override;
45     ::ndk::ScopedAStatus tune(const FrontendSettings& in_settings) override;
46     ::ndk::ScopedAStatus stopTune() override;
47     ::ndk::ScopedAStatus close() override;
48     ::ndk::ScopedAStatus scan(const FrontendSettings& in_settings,
49                               FrontendScanType in_type) override;
50     ::ndk::ScopedAStatus stopScan() override;
51     ::ndk::ScopedAStatus getStatus(const std::vector<FrontendStatusType>& in_statusTypes,
52                                    std::vector<FrontendStatus>* _aidl_return) override;
53     ::ndk::ScopedAStatus setLnb(int32_t in_lnbId) override;
54     ::ndk::ScopedAStatus linkCiCam(int32_t in_ciCamId, int32_t* _aidl_return) override;
55     ::ndk::ScopedAStatus unlinkCiCam(int32_t in_ciCamId) override;
56     ::ndk::ScopedAStatus getHardwareInfo(std::string* _aidl_return) override;
57     ::ndk::ScopedAStatus removeOutputPid(int32_t in_pid) override;
58     ::ndk::ScopedAStatus getFrontendStatusReadiness(
59             const std::vector<FrontendStatusType>& in_statusTypes,
60             std::vector<FrontendStatusReadiness>* _aidl_return) override;
61 
62     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
63 
64     FrontendType getFrontendType();
65     int32_t getFrontendId();
66     string getSourceFile();
67     dtv_plugin* getIptvPluginInterface();
68     string getIptvTransportDescription();
69     dtv_streamer* getIptvPluginStreamer();
70     void readTuneByte(void* buf);
getTuneByteBuffer()71     void* getTuneByteBuffer() { return mTuneByteBuffer; };
72     dtv_streamer* createIptvPluginStreamer(dtv_plugin* interface, const char* transport_desc);
73     dtv_plugin* createIptvPluginInterface();
74     bool isLocked();
75     void getFrontendInfo(FrontendInfo* _aidl_return);
76     void setTunerService(std::shared_ptr<Tuner> tuner);
77 
78   private:
79     virtual ~Frontend();
80     bool supportsSatellite();
81     void scanThreadLoop();
82 
83     std::shared_ptr<IFrontendCallback> mCallback;
84     std::shared_ptr<Tuner> mTuner;
85     FrontendType mType = FrontendType::UNDEFINED;
86     int32_t mId = 0;
87     bool mIsLocked = false;
88     int32_t mCiCamId;
89     std::thread mScanThread;
90     FrontendSettings mFrontendSettings;
91     FrontendScanType mFrontendScanType;
92     std::ifstream mFrontendData;
93     FrontendCapabilities mFrontendCaps;
94     vector<FrontendStatusType> mFrontendStatusCaps;
95     dtv_plugin* mIptvPluginInterface;
96     string mIptvTransportDescription;
97     dtv_streamer* mIptvPluginStreamer;
98     std::thread mIptvFrontendTuneThread;
99     void* mTuneByteBuffer = nullptr;
100 };
101 
102 }  // namespace tuner
103 }  // namespace tv
104 }  // namespace hardware
105 }  // namespace android
106 }  // namespace aidl
107