1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dcamera_sink_dev.h"
17 
18 #include "anonymous_string.h"
19 #include "dcamera_channel_info_cmd.h"
20 #include "dcamera_info_cmd.h"
21 #include "dcamera_protocol.h"
22 #include "dcamera_sink_access_control.h"
23 #include "dcamera_sink_controller.h"
24 #include "distributed_camera_errno.h"
25 #include "distributed_hardware_log.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
DCameraSinkDev(const std::string & dhId,const sptr<IDCameraSinkCallback> & sinkCallback)29 DCameraSinkDev::DCameraSinkDev(const std::string& dhId, const sptr<IDCameraSinkCallback> &sinkCallback)
30     : dhId_(dhId), sinkCallback_(sinkCallback)
31 {
32     DHLOGI("DCameraSinkDev Constructor dhId: %{public}s", GetAnonyString(dhId_).c_str());
33     isInit_ = false;
34 }
35 
~DCameraSinkDev()36 DCameraSinkDev::~DCameraSinkDev()
37 {
38     if (isInit_) {
39         UnInit();
40     }
41 }
42 
Init()43 int32_t DCameraSinkDev::Init()
44 {
45     DHLOGI("Init dhId: %{public}s", GetAnonyString(dhId_).c_str());
46     accessControl_ = std::make_shared<DCameraSinkAccessControl>();
47     controller_ = std::make_shared<DCameraSinkController>(accessControl_, sinkCallback_);
48     DCameraIndex index("", dhId_);
49     std::vector<DCameraIndex> indexs;
50     indexs.push_back(index);
51     int32_t ret = controller_->Init(indexs);
52     if (ret != DCAMERA_OK) {
53         DHLOGE("init controller failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
54         return ret;
55     }
56 
57     isInit_ = true;
58     DHLOGI("DCameraSinkDev Init %{public}s success", GetAnonyString(dhId_).c_str());
59     return DCAMERA_OK;
60 }
61 
UnInit()62 int32_t DCameraSinkDev::UnInit()
63 {
64     if (controller_ != nullptr) {
65         int32_t ret = controller_->UnInit();
66         if (ret != DCAMERA_OK) {
67             DHLOGE("release controller failed, dhId: %{public}s, ret: %{public}d",
68                    GetAnonyString(dhId_).c_str(), ret);
69         }
70     }
71     isInit_ = false;
72     return DCAMERA_OK;
73 }
74 
SubscribeLocalHardware(const std::string & parameters)75 int32_t DCameraSinkDev::SubscribeLocalHardware(const std::string& parameters)
76 {
77     DHLOGI("enter");
78     (void)parameters;
79     return DCAMERA_OK;
80 }
81 
UnsubscribeLocalHardware()82 int32_t DCameraSinkDev::UnsubscribeLocalHardware()
83 {
84     DHLOGI("enter");
85     return DCAMERA_OK;
86 }
87 
StopCapture()88 int32_t DCameraSinkDev::StopCapture()
89 {
90     DHLOGI("StopCapture dhId: %{public}s", GetAnonyString(dhId_).c_str());
91     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
92     return controller_->StopCapture();
93 }
94 
ChannelNeg(std::string & channelInfo)95 int32_t DCameraSinkDev::ChannelNeg(std::string& channelInfo)
96 {
97     DHLOGI("ChannelNeg dhId: %{public}s", GetAnonyString(dhId_).c_str());
98     if (channelInfo.empty()) {
99         DHLOGE("channelInfo is empty");
100         return DCAMERA_BAD_VALUE;
101     }
102 
103     DCameraChannelInfoCmd channelInfoCmd;
104     int32_t ret = channelInfoCmd.Unmarshal(channelInfo);
105     if (ret != DCAMERA_OK) {
106         DHLOGE("channelInfo unmarshal failed, dhId: %{public}s, ret: %{public}d",
107                GetAnonyString(dhId_).c_str(), ret);
108         return ret;
109     }
110     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
111     return controller_->ChannelNeg(channelInfoCmd.value_);
112 }
113 
GetCameraInfo(std::string & cameraInfo)114 int32_t DCameraSinkDev::GetCameraInfo(std::string& cameraInfo)
115 {
116     DHLOGI("GetCameraInfo dhId: %{public}s", GetAnonyString(dhId_).c_str());
117     std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
118     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
119     int32_t ret = controller_->GetCameraInfo(info);
120     if (ret != DCAMERA_OK) {
121         DHLOGE("get state failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
122         return ret;
123     }
124 
125     DCameraInfoCmd cameraInfoCmd;
126     cameraInfoCmd.type_ = DCAMERA_PROTOCOL_TYPE_MESSAGE;
127     cameraInfoCmd.dhId_ = dhId_;
128     cameraInfoCmd.command_ = DCAMERA_PROTOCOL_CMD_GET_INFO;
129     cameraInfoCmd.value_ = info;
130     ret = cameraInfoCmd.Marshal(cameraInfo);
131     if (ret != DCAMERA_OK) {
132         DHLOGE("cameraInfoCmd marshal failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
133         return ret;
134     }
135     DHLOGI("GetCameraInfo %{public}s success", GetAnonyString(dhId_).c_str());
136     return DCAMERA_OK;
137 }
138 
OpenChannel(std::string & openInfo)139 int32_t DCameraSinkDev::OpenChannel(std::string& openInfo)
140 {
141     DHLOGI("DCameraSinkDev OpenChannel Begin, dhId: %{public}s", GetAnonyString(dhId_).c_str());
142     if (openInfo.empty()) {
143         DHLOGE("openInfo is empty");
144         return DCAMERA_BAD_VALUE;
145     }
146 
147     DCameraOpenInfoCmd cmd;
148     int32_t ret = cmd.Unmarshal(openInfo);
149     if (ret != DCAMERA_OK) {
150         DHLOGE("openInfo unmarshal failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
151         return ret;
152     }
153     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
154     return controller_->OpenChannel(cmd.value_);
155 }
156 
CloseChannel()157 int32_t DCameraSinkDev::CloseChannel()
158 {
159     DHLOGI("CloseChannel dhId: %{public}s", GetAnonyString(dhId_).c_str());
160     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
161     return controller_->CloseChannel();
162 }
163 
GetDhid()164 std::string DCameraSinkDev::GetDhid()
165 {
166     return GetAnonyString(dhId_);
167 }
168 
PauseDistributedHardware(const std::string & networkId)169 int32_t DCameraSinkDev::PauseDistributedHardware(const std::string &networkId)
170 {
171     DHLOGI("Pause distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
172     if (networkId.empty()) {
173         DHLOGE("networkId is empty");
174         return DCAMERA_BAD_VALUE;
175     }
176     if (controller_ == nullptr) {
177         DHLOGE("controller_ is nullptr.");
178         return DCAMERA_BAD_VALUE;
179     }
180 
181     return controller_->PauseDistributedHardware(networkId);
182 }
183 
ResumeDistributedHardware(const std::string & networkId)184 int32_t DCameraSinkDev::ResumeDistributedHardware(const std::string &networkId)
185 {
186     DHLOGI("Resume distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
187     if (networkId.empty()) {
188         DHLOGE("networkId is empty");
189         return DCAMERA_BAD_VALUE;
190     }
191     if (controller_ == nullptr) {
192         DHLOGE("controller_ is nullptr.");
193         return DCAMERA_BAD_VALUE;
194     }
195 
196     return controller_->ResumeDistributedHardware(networkId);
197 }
198 
StopDistributedHardware(const std::string & networkId)199 int32_t DCameraSinkDev::StopDistributedHardware(const std::string &networkId)
200 {
201     DHLOGI("Stop distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
202     if (networkId.empty()) {
203         DHLOGE("networkId is empty");
204         return DCAMERA_BAD_VALUE;
205     }
206     if (controller_ == nullptr) {
207         DHLOGE("controller_ is nullptr.");
208         return DCAMERA_BAD_VALUE;
209     }
210 
211     return controller_->StopDistributedHardware(networkId);
212 }
213 } // namespace DistributedHardware
214 } // namespace OHOS