1 /*
2 * Copyright (c) 2022 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 "screen_data_channel_impl.h"
17
18 #include <securec.h>
19
20 #include "dscreen_constants.h"
21 #include "dscreen_errcode.h"
22 #include "dscreen_hisysevent.h"
23 #include "dscreen_log.h"
24 #include "dscreen_util.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
CreateSession(const std::shared_ptr<IScreenChannelListener> & listener)28 int32_t ScreenDataChannelImpl::CreateSession(const std::shared_ptr<IScreenChannelListener> &listener)
29 {
30 DHLOGI("%s: CreateSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str());
31 if (listener == nullptr) {
32 DHLOGE("%s: Channel listener is null", LOG_TAG);
33 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
34 }
35
36 int32_t ret =
37 SoftbusAdapter::GetInstance().CreateSoftbusSessionServer(PKG_NAME, DATA_SESSION_NAME, peerDevId_);
38 if (ret != DH_SUCCESS) {
39 DHLOGE("%s: Create softbus session failed ret: %d.", LOG_TAG, ret);
40 return ret;
41 }
42
43 std::shared_ptr<ISoftbusListener> softbusListener = shared_from_this();
44 ret = SoftbusAdapter::GetInstance().RegisterSoftbusListener(softbusListener, DATA_SESSION_NAME, peerDevId_);
45 if (ret != DH_SUCCESS) {
46 DHLOGE("%s: Register softbus adapter listener failed ret: %d.", LOG_TAG, ret);
47 return ret;
48 }
49
50 channelListener_ = listener;
51 DHLOGI("%s: Create softbus session success.", LOG_TAG);
52 return DH_SUCCESS;
53 }
54
ReleaseSession()55 int32_t ScreenDataChannelImpl::ReleaseSession()
56 {
57 DHLOGI("%s: ReleaseSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str());
58 int32_t ret = SoftbusAdapter::GetInstance().RemoveSoftbusSessionServer(PKG_NAME, DATA_SESSION_NAME, peerDevId_);
59 if (ret != DH_SUCCESS) {
60 DHLOGE("%s: Release softbus session failed ret: %d.", LOG_TAG, ret);
61 return ret;
62 }
63
64 ret = SoftbusAdapter::GetInstance().UnRegisterSoftbusListener(DATA_SESSION_NAME, peerDevId_);
65 if (ret != DH_SUCCESS) {
66 DHLOGE("%s: UnRegister softbus adapter listener failed ret: %d.", LOG_TAG, ret);
67 return ret;
68 }
69
70 DHLOGI("%s: Release softbus session success.", LOG_TAG);
71 return DH_SUCCESS;
72 }
73
OpenSession()74 int32_t ScreenDataChannelImpl::OpenSession()
75 {
76 DHLOGI("%s: OpenSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str());
77 int32_t sessionId =
78 SoftbusAdapter::GetInstance().OpenSoftbusSession(DATA_SESSION_NAME, DATA_SESSION_NAME, peerDevId_);
79 if (sessionId < 0) {
80 DHLOGE("%s: Open screen session failed, ret: %d", LOG_TAG, sessionId);
81 ReportOptFail(DSCREEN_OPT_FAIL, sessionId, "Open screen session failed");
82 return ERR_DH_SCREEN_TRANS_ERROR;
83 }
84 sessionId_ = sessionId;
85
86 DHLOGI("%s: Open screen session success, sessionId(%d)", LOG_TAG, sessionId_);
87 return DH_SUCCESS;
88 }
89
CloseSession()90 int32_t ScreenDataChannelImpl::CloseSession()
91 {
92 DHLOGI("%s: CloseSession, sessionId(%d)", LOG_TAG, sessionId_);
93 if (sessionId_ == 0) {
94 DHLOGD("%s: Session is not opened.", LOG_TAG);
95 return ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN;
96 }
97
98 int32_t ret = SoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_);
99 if (ret != DH_SUCCESS) {
100 DHLOGE("%s: Close screen session failed ret: %d.", LOG_TAG, ret);
101 return ret;
102 }
103 sessionId_ = 0;
104
105 DHLOGI("%s: Close screen session success.", LOG_TAG);
106 return DH_SUCCESS;
107 }
108
SendData(const std::shared_ptr<DataBuffer> & screenData)109 int32_t ScreenDataChannelImpl::SendData(const std::shared_ptr<DataBuffer> &screenData)
110 {
111 DHLOGD("%s: SendData, sessionId(%d)", LOG_TAG, sessionId_);
112 if (screenData == nullptr) {
113 DHLOGE("%s: Screen data is null", LOG_TAG);
114 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
115 }
116
117 StreamData data = {reinterpret_cast<char *>(screenData->Data()), screenData->Capacity()};
118 StreamData ext = {0};
119 StreamFrameInfo frameInfo = {0};
120
121 int32_t ret = SoftbusAdapter::GetInstance().SendSoftbusStream(sessionId_, &data, &ext, &frameInfo);
122 if (ret != DH_SUCCESS) {
123 DHLOGE("%s: Send screen data failed ret: %d.", LOG_TAG, ret);
124 return ret;
125 }
126
127 return DH_SUCCESS;
128 }
129
OnSessionOpened(int32_t sessionId,int32_t result)130 void ScreenDataChannelImpl::OnSessionOpened(int32_t sessionId, int32_t result)
131 {
132 DHLOGI("%s: OnScreenSessionOpened, sessionId: %d, result: %d", LOG_TAG, sessionId, result);
133 if (result != 0) {
134 DHLOGE("Session open failed.", LOG_TAG);
135 return;
136 }
137
138 std::shared_ptr<IScreenChannelListener> listener = channelListener_.lock();
139 if (listener == nullptr) {
140 DHLOGE("%s: Channel listener is null", LOG_TAG);
141 return;
142 }
143 listener->OnSessionOpened();
144 sessionId_ = sessionId;
145 }
146
OnSessionClosed(int32_t sessionId)147 void ScreenDataChannelImpl::OnSessionClosed(int32_t sessionId)
148 {
149 DHLOGI("%s: OnScreenSessionClosed, sessionId(%d).", LOG_TAG, sessionId);
150 std::shared_ptr<IScreenChannelListener> listener = channelListener_.lock();
151 if (listener == nullptr) {
152 DHLOGE("%s: Channel listener is null", LOG_TAG);
153 return;
154 }
155 listener->OnSessionClosed();
156 }
157
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)158 void ScreenDataChannelImpl::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
159 {
160 (void) sessionId;
161 (void) data;
162 (void) dataLen;
163
164 DHLOGD("%s: OnScreenBytesReceived data channel not support yet.", LOG_TAG);
165 }
166
OnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)167 void ScreenDataChannelImpl::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
168 const StreamFrameInfo *param)
169 {
170 (void)ext;
171 (void)param;
172
173 if (data == nullptr) {
174 DHLOGE("%s: Stream data is null.", LOG_TAG);
175 return;
176 }
177
178 std::shared_ptr<IScreenChannelListener> listener = channelListener_.lock();
179 if (listener == nullptr) {
180 DHLOGE("%s: Channel listener is null.", LOG_TAG);
181 return;
182 }
183
184 DHLOGI("%s: OnScreenStreamReceived, sessionId(%d) dataSize(%zu).", LOG_TAG, sessionId, data->bufLen);
185 auto dataBuffer = std::make_shared<DataBuffer>(data->bufLen);
186
187 int32_t ret = memcpy_s(dataBuffer->Data(), dataBuffer->Capacity(), reinterpret_cast<uint8_t *>(data->buf),
188 data->bufLen);
189 if (ret != EOK) {
190 DHLOGE("%s: Data memcpy_s failed.", LOG_TAG);
191 return;
192 }
193 listener->OnDataReceived(dataBuffer);
194 }
195 } // namespace DistributedHardware
196 } // namespace OHOS