1 /*
2 * Copyright (c) 2021-2024 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_softbus_session.h"
17
18 #include <securec.h>
19
20 #include "anonymous_string.h"
21 #include "dcamera_softbus_adapter.h"
22 #include "dcamera_utils_tools.h"
23 #include "distributed_camera_constants.h"
24 #include "distributed_camera_errno.h"
25 #include "distributed_hardware_log.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
DCameraSoftbusSession()29 DCameraSoftbusSession::DCameraSoftbusSession()
30 {
31 sessionId_ = -1;
32 state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
33 mode_ = DCAMERA_SESSION_MODE_CTRL;
34 ResetAssembleFrag();
35 }
36
DCameraSoftbusSession(std::string myDevId,std::string mySessionName,std::string peerDevId,std::string peerSessionName,std::shared_ptr<ICameraChannelListener> listener,DCameraSessionMode mode)37 DCameraSoftbusSession::DCameraSoftbusSession(std::string myDevId, std::string mySessionName, std::string peerDevId,
38 std::string peerSessionName, std::shared_ptr<ICameraChannelListener> listener, DCameraSessionMode mode)
39 : myDevId_(myDevId), mySessionName_(mySessionName), peerDevId_(peerDevId), peerSessionName_(peerSessionName),
40 listener_(listener), sessionId_(-1), state_(DCAMERA_SOFTBUS_STATE_CLOSED), mode_(mode)
41 {
42 sendFuncMap_[DCAMERA_SESSION_MODE_CTRL] = &DCameraSoftbusSession::SendBytes;
43 sendFuncMap_[DCAMERA_SESSION_MODE_VIDEO] = &DCameraSoftbusSession::SendStream;
44 sendFuncMap_[DCAMERA_SESSION_MODE_JPEG] = &DCameraSoftbusSession::SendBytes;
45 auto runner = AppExecFwk::EventRunner::Create(mySessionName);
46 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
47 ResetAssembleFrag();
48 }
49
~DCameraSoftbusSession()50 DCameraSoftbusSession::~DCameraSoftbusSession()
51 {
52 if (sessionId_ != -1) {
53 int32_t ret = DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_);
54 if (ret != DCAMERA_OK) {
55 DHLOGE("DCameraSoftbusSession delete failed, ret: %{public}d, sessId: %{public}d peerDevId: %{public}s "
56 "peerSessionName: %{public}s", ret, sessionId_, GetAnonyString(peerDevId_).c_str(),
57 GetAnonyString(peerSessionName_).c_str());
58 }
59 }
60 sendFuncMap_.clear();
61 eventHandler_ = nullptr;
62 }
63
CloseSession()64 int32_t DCameraSoftbusSession::CloseSession()
65 {
66 DHLOGI("close session sessionId: %{public}d peerDevId: %{public}s peerSessionName: %{public}s", sessionId_,
67 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
68 if (sessionId_ == -1) {
69 DHLOGI("current session has already close peerDevId: %{public}s peerSessionName: %{public}s",
70 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
71 return DCAMERA_OK;
72 }
73 int32_t ret = DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_);
74 if (ret != DCAMERA_OK) {
75 DHLOGE("close session failed, ret: %{public}d, peerDevId: %{public}s peerSessionName: %{public}s", ret,
76 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
77 return ret;
78 }
79
80 sessionId_ = -1;
81 state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
82 return DCAMERA_OK;
83 }
84
OnSessionOpened(int32_t socket)85 int32_t DCameraSoftbusSession::OnSessionOpened(int32_t socket)
86 {
87 DHLOGI("open current session start, socket: %{public}d", socket);
88 sessionId_ = socket;
89 state_ = DCAMERA_SOFTBUS_STATE_OPENED;
90 CHECK_AND_RETURN_RET_LOG(listener_ == nullptr, DCAMERA_BAD_VALUE, "listener_ is null.");
91 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_CONNECTED);
92 DHLOGI("open current session end, socket: %{public}d", socket);
93 return DCAMERA_OK;
94 }
95
OnSessionClose(int32_t sessionId)96 int32_t DCameraSoftbusSession::OnSessionClose(int32_t sessionId)
97 {
98 DHLOGI("OnSessionClose sessionId: %{public}d peerDevId: %{public}s peerSessionName: %{public}s", sessionId,
99 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
100 sessionId_ = -1;
101 state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
102 CHECK_AND_RETURN_RET_LOG(listener_ == nullptr, DCAMERA_BAD_VALUE, "listener_ is null.");
103 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_DISCONNECTED);
104 return DCAMERA_OK;
105 }
106
OnDataReceived(std::shared_ptr<DataBuffer> & buffer)107 int32_t DCameraSoftbusSession::OnDataReceived(std::shared_ptr<DataBuffer>& buffer)
108 {
109 auto recvDataFunc = [this, buffer]() mutable {
110 DealRecvData(buffer);
111 };
112 if (eventHandler_ != nullptr) {
113 eventHandler_->PostTask(recvDataFunc);
114 }
115 return DCAMERA_OK;
116 }
117
DealRecvData(std::shared_ptr<DataBuffer> & buffer)118 void DCameraSoftbusSession::DealRecvData(std::shared_ptr<DataBuffer>& buffer)
119 {
120 if (mode_ == DCAMERA_SESSION_MODE_VIDEO) {
121 PostData(buffer);
122 return;
123 }
124 PackRecvData(buffer);
125 return;
126 }
127
PackRecvData(std::shared_ptr<DataBuffer> & buffer)128 void DCameraSoftbusSession::PackRecvData(std::shared_ptr<DataBuffer>& buffer)
129 {
130 if (buffer == nullptr) {
131 DHLOGE("Data buffer is null");
132 return;
133 }
134 uint64_t bufferSize;
135 if (buffer->Size() < BINARY_HEADER_FRAG_LEN) {
136 bufferSize = static_cast<uint64_t>(buffer->Size());
137 DHLOGE("pack recv data error, size: %{public}" PRIu64", sess: %{public}s peerSess: %{public}s",
138 bufferSize, mySessionName_.c_str(), peerSessionName_.c_str());
139 return;
140 }
141 uint8_t *ptrPacket = buffer->Data();
142 SessionDataHeader headerPara;
143 GetFragDataLen(ptrPacket, headerPara);
144 if (buffer->Size() != (headerPara.dataLen + BINARY_HEADER_FRAG_LEN) || headerPara.dataLen > headerPara.totalLen ||
145 headerPara.dataLen > BINARY_DATA_MAX_LEN || headerPara.totalLen > BINARY_DATA_MAX_TOTAL_LEN) {
146 bufferSize = static_cast<uint64_t>(buffer->Size());
147 DHLOGE("pack recv data failed, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d sess: "
148 "%{public}s peerSess: %{public}s", bufferSize, headerPara.dataLen, headerPara.totalLen,
149 mySessionName_.c_str(), peerSessionName_.c_str());
150 return;
151 }
152 bufferSize = static_cast<uint64_t>(buffer->Size());
153 DHLOGD("pack recv data Assemble, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d, nowTime: "
154 "%{public}" PRId64" start", bufferSize, headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs());
155 if (headerPara.fragFlag == FRAG_START_END) {
156 AssembleNoFrag(buffer, headerPara);
157 } else {
158 AssembleFrag(buffer, headerPara);
159 }
160 bufferSize = static_cast<uint64_t>(buffer->Size());
161 DHLOGD("pack recv data Assemble, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d, nowTime: "
162 "%{public}" PRId64" end", bufferSize, headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs());
163 }
164
AssembleNoFrag(std::shared_ptr<DataBuffer> & buffer,SessionDataHeader & headerPara)165 void DCameraSoftbusSession::AssembleNoFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara)
166 {
167 if (headerPara.dataLen != headerPara.totalLen) {
168 DHLOGE("DCameraSoftbusSession PackRecvData failed, dataLen: %{public}d, totalLen: %{public}d, sess: "
169 "%{public}s peerSess: %{public}s", headerPara.dataLen, headerPara.totalLen, mySessionName_.c_str(),
170 peerSessionName_.c_str());
171 return;
172 }
173 if (buffer == nullptr) {
174 DHLOGE("Data buffer is null");
175 return;
176 }
177 std::shared_ptr<DataBuffer> postData = std::make_shared<DataBuffer>(headerPara.dataLen);
178 int32_t ret = memcpy_s(postData->Data(), postData->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN,
179 buffer->Size() - BINARY_HEADER_FRAG_LEN);
180 if (ret != EOK) {
181 DHLOGE("DCameraSoftbusSession PackRecvData failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s",
182 ret, mySessionName_.c_str(), peerSessionName_.c_str());
183 return;
184 }
185 PostData(postData);
186 }
187
AssembleFrag(std::shared_ptr<DataBuffer> & buffer,SessionDataHeader & headerPara)188 void DCameraSoftbusSession::AssembleFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara)
189 {
190 if (buffer == nullptr) {
191 DHLOGE("Data buffer is null");
192 return;
193 }
194 if (headerPara.fragFlag == FRAG_START) {
195 isWaiting_ = true;
196 nowSeq_ = headerPara.seqNum;
197 nowSubSeq_ = headerPara.subSeq;
198 offset_ = 0;
199 totalLen_ = headerPara.totalLen;
200 packBuffer_ = std::make_shared<DataBuffer>(headerPara.totalLen);
201 int32_t ret = memcpy_s(packBuffer_->Data(), packBuffer_->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN,
202 buffer->Size() - BINARY_HEADER_FRAG_LEN);
203 if (ret != EOK) {
204 DHLOGE("DCameraSoftbusSession AssembleFrag failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s",
205 ret, mySessionName_.c_str(), peerSessionName_.c_str());
206 ResetAssembleFrag();
207 return;
208 }
209 offset_ += headerPara.dataLen;
210 }
211
212 if (headerPara.fragFlag == FRAG_MID || headerPara.fragFlag == FRAG_END) {
213 int32_t ret = CheckUnPackBuffer(headerPara);
214 if (ret != DCAMERA_OK) {
215 ResetAssembleFrag();
216 return;
217 }
218
219 nowSubSeq_ = headerPara.subSeq;
220 ret = memcpy_s(packBuffer_->Data() + offset_, packBuffer_->Size() - offset_,
221 buffer->Data() + BINARY_HEADER_FRAG_LEN, buffer->Size() - BINARY_HEADER_FRAG_LEN);
222 if (ret != EOK) {
223 DHLOGE("DCameraSoftbusSession AssembleFrag failed, memcpy_s ret: %{public}d, sess: %{public}s peerSess: "
224 "%{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str());
225 ResetAssembleFrag();
226 return;
227 }
228 offset_ += headerPara.dataLen;
229 }
230
231 if (headerPara.fragFlag == FRAG_END) {
232 PostData(packBuffer_);
233 ResetAssembleFrag();
234 }
235 }
236
CheckUnPackBuffer(SessionDataHeader & headerPara)237 int32_t DCameraSoftbusSession::CheckUnPackBuffer(SessionDataHeader& headerPara)
238 {
239 if (!isWaiting_) {
240 DHLOGE("DCameraSoftbusSession AssembleFrag failed, not start one, sess: %{public}s peerSess: %{public}s",
241 mySessionName_.c_str(), peerSessionName_.c_str());
242 return DCAMERA_BAD_VALUE;
243 }
244
245 if (nowSeq_ != headerPara.seqNum) {
246 DHLOGE("DCameraSoftbusSession AssembleFrag seq error nowSeq: %{public}d actualSeq: %{public}d, sess: "
247 "%{public}s peerSess: %{public}s", nowSeq_, headerPara.seqNum, mySessionName_.c_str(),
248 peerSessionName_.c_str());
249 return DCAMERA_BAD_VALUE;
250 }
251
252 if (nowSubSeq_ + 1 != headerPara.subSeq) {
253 DHLOGE("DCameraSoftbusSession AssembleFrag subSeq error nowSeq: %{public}d actualSeq: %{public}d, "
254 "sess: %{public}s peerSess: %{public}s", nowSubSeq_, headerPara.subSeq, mySessionName_.c_str(),
255 peerSessionName_.c_str());
256 return DCAMERA_BAD_VALUE;
257 }
258
259 if (totalLen_ < headerPara.dataLen + offset_) {
260 DHLOGE("DCameraSoftbusSession AssembleFrag len error cap: %{public}d size: %{public}d, dataLen: "
261 "%{public}d sess: %{public}s peerSess: %{public}s", totalLen_, offset_, headerPara.dataLen,
262 mySessionName_.c_str(), peerSessionName_.c_str());
263 return DCAMERA_BAD_VALUE;
264 }
265 return DCAMERA_OK;
266 }
267
ResetAssembleFrag()268 void DCameraSoftbusSession::ResetAssembleFrag()
269 {
270 isWaiting_ = false;
271 nowSeq_ = 0;
272 nowSubSeq_ = 0;
273 offset_ = 0;
274 totalLen_ = 0;
275 packBuffer_ = nullptr;
276 }
277
PostData(std::shared_ptr<DataBuffer> & buffer)278 void DCameraSoftbusSession::PostData(std::shared_ptr<DataBuffer>& buffer)
279 {
280 std::vector<std::shared_ptr<DataBuffer>> buffers;
281 buffers.push_back(buffer);
282 CHECK_AND_RETURN_LOG(listener_ == nullptr, "listener_ is null.");
283 listener_->OnDataReceived(buffers);
284 }
285
GetFragDataLen(uint8_t * ptrPacket,SessionDataHeader & headerPara)286 void DCameraSoftbusSession::GetFragDataLen(uint8_t *ptrPacket, SessionDataHeader& headerPara)
287 {
288 headerPara.version = U16Get(ptrPacket);
289 headerPara.fragFlag = ptrPacket[BINARY_HEADER_FRAG_OFFSET];
290 headerPara.dataType = U32Get(ptrPacket + BINARY_HEADER_DATATYPE_OFFSET);
291 headerPara.seqNum = U32Get(ptrPacket + BINARY_HEADER_SEQNUM_OFFSET);
292 headerPara.totalLen = U32Get(ptrPacket + BINARY_HEADER_TOTALLEN_OFFSET);
293 headerPara.subSeq = U16Get(ptrPacket + BINARY_HEADER_SUBSEQ_OFFSET);
294 headerPara.dataLen = U32Get(ptrPacket + BINARY_HEADER_DATALEN_OFFSET);
295 }
296
U16Get(const uint8_t * ptr)297 uint16_t DCameraSoftbusSession::U16Get(const uint8_t *ptr)
298 {
299 return (ptr[0] << DCAMERA_SHIFT_8) | ptr[1];
300 }
301
U32Get(const uint8_t * ptr)302 uint32_t DCameraSoftbusSession::U32Get(const uint8_t *ptr)
303 {
304 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | ptr[3];
305 }
306
SendData(DCameraSessionMode mode,std::shared_ptr<DataBuffer> & buffer)307 int32_t DCameraSoftbusSession::SendData(DCameraSessionMode mode, std::shared_ptr<DataBuffer>& buffer)
308 {
309 auto itFunc = sendFuncMap_.find(mode);
310 if (itFunc == sendFuncMap_.end()) {
311 return DCAMERA_NOT_FOUND;
312 }
313 auto memberFunc = itFunc->second;
314 switch (mode) {
315 case DCAMERA_SESSION_MODE_VIDEO:
316 return SendStream(buffer);
317 case DCAMERA_SESSION_MODE_CTRL:
318 case DCAMERA_SESSION_MODE_JPEG:
319 return UnPackSendData(buffer, memberFunc);
320 default:
321 return UnPackSendData(buffer, memberFunc);
322 }
323 return DCAMERA_NOT_FOUND;
324 }
325
UnPackSendData(std::shared_ptr<DataBuffer> & buffer,DCameraSendFuc memberFunc)326 int32_t DCameraSoftbusSession::UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc)
327 {
328 CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null");
329 uint16_t subSeq = 0;
330 uint32_t seq = 0;
331 uint32_t totalLen = buffer->Size();
332 SessionDataHeader headPara = { PROTOCOL_VERSION, FRAG_START, mode_, seq, totalLen, subSeq };
333 if (buffer->Size() <= BINARY_DATA_PACKET_MAX_LEN) {
334 headPara.fragFlag = FRAG_START_END;
335 headPara.dataLen = buffer->Size();
336 std::shared_ptr<DataBuffer> unpackData = std::make_shared<DataBuffer>(buffer->Size() + BINARY_HEADER_FRAG_LEN);
337 MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
338 int32_t ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
339 buffer->Data(), buffer->Size());
340 if (ret != EOK) {
341 DHLOGE("DCameraSoftbusSession UnPackSendData START_END memcpy_s failed, ret: %{public}d, sess: %{public}s "
342 "peerSess: %{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str());
343 return ret;
344 }
345 return SendBytes(unpackData);
346 }
347 uint32_t offset = 0;
348 while (totalLen > offset) {
349 SetHeadParaDataLen(headPara, totalLen, offset);
350 uint64_t bufferSize = static_cast<uint64_t>(buffer->Size());
351 DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, "
352 "nowTime: %{public}" PRId64" start:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
353 std::shared_ptr<DataBuffer> unpackData =
354 std::make_shared<DataBuffer>(headPara.dataLen + BINARY_HEADER_FRAG_LEN);
355 MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
356 int ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
357 buffer->Data() + offset, headPara.dataLen);
358 if (ret != EOK) {
359 DHLOGE("DCameraSoftbusSession UnPackSendData memcpy_s failed, ret: %{public}d, sess: %{public}s peerSess: "
360 "%{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str());
361 return ret;
362 }
363 ret = SendBytes(unpackData);
364 if (ret != DCAMERA_OK) {
365 DHLOGE("DCameraSoftbusSession sendData failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s",
366 ret, mySessionName_.c_str(), peerSessionName_.c_str());
367 return ret;
368 }
369 DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, "
370 "nowTime: %{public}" PRId64" end:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
371 headPara.subSeq++;
372 headPara.fragFlag = FRAG_MID;
373 offset += headPara.dataLen;
374 }
375 return DCAMERA_OK;
376 }
377
SetHeadParaDataLen(SessionDataHeader & headPara,const uint32_t totalLen,const uint32_t offset)378 void DCameraSoftbusSession::SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen,
379 const uint32_t offset)
380 {
381 if (totalLen >= offset) {
382 if (totalLen - offset > BINARY_DATA_PACKET_MAX_LEN) {
383 headPara.dataLen = BINARY_DATA_PACKET_MAX_LEN - BINARY_DATA_PACKET_RESERVED_BUFFER;
384 } else {
385 headPara.fragFlag = FRAG_END;
386 headPara.dataLen = totalLen - offset;
387 }
388 }
389 }
390
MakeFragDataHeader(const SessionDataHeader & headPara,uint8_t * header,uint32_t len)391 void DCameraSoftbusSession::MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len)
392 {
393 uint32_t headerLen = sizeof(uint8_t) * HEADER_UINT8_NUM + sizeof(uint16_t) * HEADER_UINT16_NUM +
394 sizeof(uint32_t) * HEADER_UINT32_NUM;
395 if (headerLen > len) {
396 DHLOGE("MakeFragDataHeader %{public}d over len %{public}d", headerLen, len);
397 return;
398 }
399 uint32_t i = 0;
400 header[i++] = headPara.version >> DCAMERA_SHIFT_8;
401 header[i++] = headPara.version & UINT16_SHIFT_MASK_0;
402 header[i++] = headPara.fragFlag;
403 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
404 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
405 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
406 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_0);
407 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
408 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
409 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
410 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_0);
411 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
412 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
413 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
414 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_0);
415 header[i++] = headPara.subSeq >> DCAMERA_SHIFT_8;
416 header[i++] = headPara.subSeq & UINT16_SHIFT_MASK_0;
417 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
418 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
419 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
420 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_0);
421 }
422
SendBytes(std::shared_ptr<DataBuffer> & buffer)423 int32_t DCameraSoftbusSession::SendBytes(std::shared_ptr<DataBuffer>& buffer)
424 {
425 if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
426 DHLOGE("DCameraSoftbusSession SendBytes session state %{public}d is not opened sessionId: %{public}d "
427 "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(),
428 GetAnonyString(peerSessionName_).c_str());
429 return DCAMERA_WRONG_STATE;
430 }
431
432 int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusBytes(sessionId_, buffer);
433 if (ret != DCAMERA_OK) {
434 DHLOGE("DCameraSoftbusSession SendBytes sessionId: %{public}d failed: %{public}d peerDevId: %{public}s "
435 "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(),
436 GetAnonyString(peerSessionName_).c_str());
437 }
438 return ret;
439 }
440
SendStream(std::shared_ptr<DataBuffer> & buffer)441 int32_t DCameraSoftbusSession::SendStream(std::shared_ptr<DataBuffer>& buffer)
442 {
443 if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
444 DHLOGE("DCameraSoftbusSession SendStream session state %{public}d is not opened sessionId: %{public}d "
445 "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(),
446 GetAnonyString(peerSessionName_).c_str());
447 return DCAMERA_WRONG_STATE;
448 }
449
450 int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusStream(sessionId_, buffer);
451 if (ret != DCAMERA_OK) {
452 DHLOGE("DCameraSoftbusSession SendStream sessionId: %{public}d failed: %{public}d peerDevId: %{public}s "
453 "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(),
454 GetAnonyString(peerSessionName_).c_str());
455 }
456 return ret;
457 }
458
GetPeerDevId()459 std::string DCameraSoftbusSession::GetPeerDevId()
460 {
461 return peerDevId_;
462 }
463
GetPeerSessionName()464 std::string DCameraSoftbusSession::GetPeerSessionName()
465 {
466 return peerSessionName_;
467 }
468
GetMySessionName()469 std::string DCameraSoftbusSession::GetMySessionName()
470 {
471 return mySessionName_;
472 }
473
GetSessionId()474 int32_t DCameraSoftbusSession::GetSessionId()
475 {
476 return sessionId_;
477 }
478 } // namespace DistributedHardware
479 } // namespace OHOS
480