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_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::SendStream;
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: %d, sessId: %d peerDevId: %s peerSessionName: %s", ret,
56 sessionId_, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
57 }
58 }
59 sendFuncMap_.clear();
60 eventHandler_ = nullptr;
61 }
62
OpenSession()63 int32_t DCameraSoftbusSession::OpenSession()
64 {
65 DHLOGI("DCameraSoftbusSession OpenSession peerDevId: %s peerSessionName: %s",
66 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
67 int32_t ret = DCameraSoftbusAdapter::GetInstance().OpenSoftbusSession(mySessionName_, peerSessionName_, mode_,
68 peerDevId_);
69 if (ret != DCAMERA_OK) {
70 DHLOGE("DCameraSoftbusSession OpenSession failed, ret: %d, peerDevId: %s peerSessionName: %s", ret,
71 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
72 return ret;
73 }
74
75 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_CONNECTING);
76 return DCAMERA_OK;
77 }
78
CloseSession()79 int32_t DCameraSoftbusSession::CloseSession()
80 {
81 DHLOGI("DCameraSoftbusSession CloseSession sessionId: %d peerDevId: %s peerSessionName: %s", sessionId_,
82 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
83 if (sessionId_ == -1) {
84 DHLOGI("DCameraSoftbusSession CloseSession has already close peerDevId: %s peerSessionName: %s",
85 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
86 return DCAMERA_OK;
87 }
88 int32_t ret = DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_);
89 if (ret != DCAMERA_OK) {
90 DHLOGE("DCameraSoftbusSession CloseSession failed, ret: %d, peerDevId: %s peerSessionName: %s", ret,
91 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
92 return ret;
93 }
94
95 sessionId_ = -1;
96 state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
97 return DCAMERA_OK;
98 }
99
OnSessionOpend(int32_t sessionId,int32_t result)100 int32_t DCameraSoftbusSession::OnSessionOpend(int32_t sessionId, int32_t result)
101 {
102 DHLOGI("DCameraSoftbusSession OnSessionOpend sessionId: %d result: %d peerDevId: %s peerSessionName: %s",
103 sessionId, result, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
104 if (result != DCAMERA_OK) {
105 DHLOGE("DCameraSoftbusSession OnSessionOpend sessionId: %d result: %d peerDevId: %s peerSessionName: %s",
106 sessionId_, result, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
107 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_DISCONNECTED);
108 listener_->OnSessionError(DCAMERA_MESSAGE, DCAMERA_EVENT_OPEN_CHANNEL_ERROR,
109 std::string("softbus internal error"));
110 return result;
111 }
112
113 sessionId_ = sessionId;
114 state_ = DCAMERA_SOFTBUS_STATE_OPENED;
115 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_CONNECTED);
116 return DCAMERA_OK;
117 }
118
OnSessionClose(int32_t sessionId)119 int32_t DCameraSoftbusSession::OnSessionClose(int32_t sessionId)
120 {
121 DHLOGI("DCameraSoftbusSession OnSessionClose sessionId: %d peerDevId: %s peerSessionName: %s", sessionId,
122 GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
123 sessionId_ = -1;
124 state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
125 listener_->OnSessionState(DCAMERA_CHANNEL_STATE_DISCONNECTED);
126 return DCAMERA_OK;
127 }
128
OnDataReceived(std::shared_ptr<DataBuffer> & buffer)129 int32_t DCameraSoftbusSession::OnDataReceived(std::shared_ptr<DataBuffer>& buffer)
130 {
131 auto recvDataFunc = [this, buffer]() mutable {
132 DealRecvData(buffer);
133 };
134 if (eventHandler_ != nullptr) {
135 eventHandler_->PostTask(recvDataFunc);
136 }
137 return DCAMERA_OK;
138 }
139
DealRecvData(std::shared_ptr<DataBuffer> & buffer)140 void DCameraSoftbusSession::DealRecvData(std::shared_ptr<DataBuffer>& buffer)
141 {
142 if (mode_ == DCAMERA_SESSION_MODE_VIDEO) {
143 PostData(buffer);
144 return;
145 }
146 PackRecvData(buffer);
147 return;
148 }
149
PackRecvData(std::shared_ptr<DataBuffer> & buffer)150 void DCameraSoftbusSession::PackRecvData(std::shared_ptr<DataBuffer>& buffer)
151 {
152 if (buffer->Size() < BINARY_HEADER_FRAG_LEN) {
153 DHLOGE("DCameraSoftbusSession PackRecvData failed, size: %d, sess: %s peerSess: %s",
154 buffer->Size(), mySessionName_.c_str(), peerSessionName_.c_str());
155 return;
156 }
157 uint8_t *ptrPacket = buffer->Data();
158 SessionDataHeader headerPara;
159 GetFragDataLen(ptrPacket, headerPara);
160 if (buffer->Size() != (headerPara.dataLen + BINARY_HEADER_FRAG_LEN) || headerPara.dataLen > headerPara.totalLen ||
161 headerPara.dataLen > BINARY_DATA_MAX_LEN || headerPara.totalLen > BINARY_DATA_MAX_TOTAL_LEN) {
162 DHLOGE("DCameraSoftbusSession PackRecvData failed, size: %d, dataLen: %d, totalLen: %d sess: %s peerSess: %s",
163 buffer->Size(), headerPara.dataLen, headerPara.totalLen, mySessionName_.c_str(), peerSessionName_.c_str());
164 return;
165 }
166 DHLOGD("DCameraSoftbusSession PackRecvData Assemble, size: %d, dataLen: %d, totalLen: %d, nowTime: %lld start",
167 buffer->Size(), headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs());
168 if (headerPara.fragFlag == FRAG_START_END) {
169 AssembleNoFrag(buffer, headerPara);
170 } else {
171 AssembleFrag(buffer, headerPara);
172 }
173 DHLOGD("DCameraSoftbusSession PackRecvData Assemble, size: %d, dataLen: %d, totalLen: %d, nowTime: %lld end",
174 buffer->Size(), headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs());
175 }
176
AssembleNoFrag(std::shared_ptr<DataBuffer> & buffer,SessionDataHeader & headerPara)177 void DCameraSoftbusSession::AssembleNoFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara)
178 {
179 if (headerPara.dataLen != headerPara.totalLen) {
180 DHLOGE("DCameraSoftbusSession PackRecvData failed, dataLen: %d, totalLen: %d, sess: %s peerSess: %s",
181 headerPara.dataLen, headerPara.totalLen, mySessionName_.c_str(), peerSessionName_.c_str());
182 return;
183 }
184 std::shared_ptr<DataBuffer> postData = std::make_shared<DataBuffer>(headerPara.dataLen);
185 int32_t ret = memcpy_s(postData->Data(), postData->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN,
186 buffer->Size() - BINARY_HEADER_FRAG_LEN);
187 if (ret != EOK) {
188 DHLOGE("DCameraSoftbusSession PackRecvData failed, ret: %d, sess: %s peerSess: %s",
189 ret, mySessionName_.c_str(), peerSessionName_.c_str());
190 return;
191 }
192 PostData(postData);
193 }
194
AssembleFrag(std::shared_ptr<DataBuffer> & buffer,SessionDataHeader & headerPara)195 void DCameraSoftbusSession::AssembleFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara)
196 {
197 if (headerPara.fragFlag == FRAG_START) {
198 isWaiting_ = true;
199 nowSeq_ = headerPara.seqNum;
200 nowSubSeq_ = headerPara.subSeq;
201 offset_ = 0;
202 totalLen_ = headerPara.totalLen;
203 packBuffer_ = std::make_shared<DataBuffer>(headerPara.totalLen);
204 int32_t ret = memcpy_s(packBuffer_->Data(), packBuffer_->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN,
205 buffer->Size() - BINARY_HEADER_FRAG_LEN);
206 if (ret != EOK) {
207 DHLOGE("DCameraSoftbusSession AssembleFrag failed, ret: %d, sess: %s peerSess: %s",
208 ret, mySessionName_.c_str(), peerSessionName_.c_str());
209 ResetAssembleFrag();
210 return;
211 }
212 offset_ += headerPara.dataLen;
213 }
214
215 if (headerPara.fragFlag == FRAG_MID || headerPara.fragFlag == FRAG_END) {
216 int32_t ret = CheckUnPackBuffer(headerPara);
217 if (ret != DCAMERA_OK) {
218 ResetAssembleFrag();
219 return;
220 }
221
222 nowSubSeq_ = headerPara.subSeq;
223 ret = memcpy_s(packBuffer_->Data() + offset_, packBuffer_->Size() - offset_,
224 buffer->Data() + BINARY_HEADER_FRAG_LEN, buffer->Size() - BINARY_HEADER_FRAG_LEN);
225 if (ret != EOK) {
226 DHLOGE("DCameraSoftbusSession AssembleFrag failed, memcpy_s ret: %d, sess: %s peerSess: %s",
227 ret, mySessionName_.c_str(), peerSessionName_.c_str());
228 ResetAssembleFrag();
229 return;
230 }
231 offset_ += headerPara.dataLen;
232 }
233
234 if (headerPara.fragFlag == FRAG_END) {
235 PostData(packBuffer_);
236 ResetAssembleFrag();
237 }
238 }
239
CheckUnPackBuffer(SessionDataHeader & headerPara)240 int32_t DCameraSoftbusSession::CheckUnPackBuffer(SessionDataHeader& headerPara)
241 {
242 if (!isWaiting_) {
243 DHLOGE("DCameraSoftbusSession AssembleFrag failed, not start one, sess: %s peerSess: %s",
244 mySessionName_.c_str(), peerSessionName_.c_str());
245 return DCAMERA_BAD_VALUE;
246 }
247
248 if (nowSeq_ != headerPara.seqNum) {
249 DHLOGE("DCameraSoftbusSession AssembleFrag seq error nowSeq: %d actualSeq: %d, sess: %s peerSess: %s",
250 nowSeq_, headerPara.seqNum, mySessionName_.c_str(), peerSessionName_.c_str());
251 return DCAMERA_BAD_VALUE;
252 }
253
254 if (nowSubSeq_ + 1 != headerPara.subSeq) {
255 DHLOGE("DCameraSoftbusSession AssembleFrag subSeq error nowSeq: %d actualSeq: %d, sess: %s peerSess: %s",
256 nowSubSeq_, headerPara.subSeq, mySessionName_.c_str(), peerSessionName_.c_str());
257 return DCAMERA_BAD_VALUE;
258 }
259
260 if (totalLen_ < headerPara.dataLen + offset_) {
261 DHLOGE("DCameraSoftbusSession AssembleFrag len error cap: %d size: %d, dataLen: %d sess: %s peerSess: %s",
262 totalLen_, offset_, headerPara.dataLen, mySessionName_.c_str(),
263 peerSessionName_.c_str());
264 return DCAMERA_BAD_VALUE;
265 }
266 return DCAMERA_OK;
267 }
268
ResetAssembleFrag()269 void DCameraSoftbusSession::ResetAssembleFrag()
270 {
271 isWaiting_ = false;
272 nowSeq_ = 0;
273 nowSubSeq_ = 0;
274 offset_ = 0;
275 totalLen_ = 0;
276 packBuffer_ = nullptr;
277 }
278
PostData(std::shared_ptr<DataBuffer> & buffer)279 void DCameraSoftbusSession::PostData(std::shared_ptr<DataBuffer>& buffer)
280 {
281 std::vector<std::shared_ptr<DataBuffer>> buffers;
282 buffers.push_back(buffer);
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 if (mode == DCAMERA_SESSION_MODE_VIDEO) {
315 return (this->*memberFunc)(buffer);
316 }
317
318 return UnPackSendData(buffer, memberFunc);
319 }
320
UnPackSendData(std::shared_ptr<DataBuffer> & buffer,DCameraSendFuc memberFunc)321 int32_t DCameraSoftbusSession::UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc)
322 {
323 uint16_t subSeq = 0;
324 uint32_t seq = 0;
325 uint32_t totalLen = buffer->Size();
326 SessionDataHeader headPara = { PROTOCOL_VERSION, FRAG_START, mode_, seq, totalLen, subSeq };
327
328 if (buffer->Size() <= BINARY_DATA_PACKET_MAX_LEN) {
329 headPara.fragFlag = FRAG_START_END;
330 headPara.dataLen = buffer->Size();
331 std::shared_ptr<DataBuffer> unpackData = std::make_shared<DataBuffer>(buffer->Size() + BINARY_HEADER_FRAG_LEN);
332 MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
333 int32_t ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
334 buffer->Data(), buffer->Size());
335 if (ret != EOK) {
336 DHLOGE("DCameraSoftbusSession UnPackSendData START_END memcpy_s failed, ret: %d, sess: %s peerSess: %s",
337 ret, mySessionName_.c_str(), peerSessionName_.c_str());
338 return ret;
339 }
340 return (this->*memberFunc)(unpackData);
341 }
342
343 uint32_t offset = 0;
344 while (totalLen > offset) {
345 SetHeadParaDataLen(headPara, totalLen, offset);
346 DHLOGD("DCameraSoftbusSession UnPackSendData, size: %d, dataLen: %d, totalLen: %d, nowTime: %lld start:",
347 buffer->Size(), headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
348 std::shared_ptr<DataBuffer> unpackData =
349 std::make_shared<DataBuffer>(headPara.dataLen + BINARY_HEADER_FRAG_LEN);
350 MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
351 int ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
352 buffer->Data() + offset, headPara.dataLen);
353 if (ret != EOK) {
354 DHLOGE("DCameraSoftbusSession UnPackSendData memcpy_s failed, ret: %d, sess: %s peerSess: %s",
355 ret, mySessionName_.c_str(), peerSessionName_.c_str());
356 return ret;
357 }
358 ret = (this->*memberFunc)(unpackData);
359 if (ret != DCAMERA_OK) {
360 DHLOGE("DCameraSoftbusSession sendData failed, ret: %d, sess: %s peerSess: %s",
361 ret, mySessionName_.c_str(), peerSessionName_.c_str());
362 return ret;
363 }
364 DHLOGD("DCameraSoftbusSession UnPackSendData, size: %d, dataLen: %d, totalLen: %d, nowTime: %lld end:",
365 buffer->Size(), headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
366 headPara.subSeq++;
367 headPara.fragFlag = FRAG_MID;
368 offset += headPara.dataLen;
369 }
370 return DCAMERA_OK;
371 }
372
SetHeadParaDataLen(SessionDataHeader & headPara,const uint32_t totalLen,const uint32_t offset)373 void DCameraSoftbusSession::SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen,
374 const uint32_t offset)
375 {
376 if (totalLen - offset > BINARY_DATA_PACKET_MAX_LEN) {
377 headPara.dataLen = BINARY_DATA_PACKET_MAX_LEN;
378 } else {
379 headPara.fragFlag = FRAG_END;
380 headPara.dataLen = totalLen - offset;
381 }
382 }
383
MakeFragDataHeader(const SessionDataHeader & headPara,uint8_t * header,uint32_t len)384 void DCameraSoftbusSession::MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len)
385 {
386 uint32_t headerLen = sizeof(uint8_t) * HEADER_UINT8_NUM + sizeof(uint16_t) * HEADER_UINT16_NUM +
387 sizeof(uint32_t) * HEADER_UINT32_NUM;
388 if (headerLen > len) {
389 DHLOGE("MakeFragDataHeader %d over len %d", headerLen, len);
390 return;
391 }
392 uint32_t i = 0;
393 header[i++] = headPara.version >> DCAMERA_SHIFT_8;
394 header[i++] = headPara.version & UINT16_SHIFT_MASK_0;
395 header[i++] = headPara.fragFlag;
396 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
397 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
398 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
399 header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_0);
400 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
401 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
402 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
403 header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_0);
404 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
405 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
406 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
407 header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_0);
408 header[i++] = headPara.subSeq >> DCAMERA_SHIFT_8;
409 header[i++] = headPara.subSeq & UINT16_SHIFT_MASK_0;
410 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
411 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
412 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
413 header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_0);
414 }
415
SendBytes(std::shared_ptr<DataBuffer> & buffer)416 int32_t DCameraSoftbusSession::SendBytes(std::shared_ptr<DataBuffer>& buffer)
417 {
418 if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
419 DHLOGE("DCameraSoftbusSession SendBytes session state %d is not opened sessionId: %d peerDev: %s peerName: %s",
420 state_, sessionId_, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
421 return DCAMERA_WRONG_STATE;
422 }
423
424 int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusBytes(sessionId_, buffer);
425 if (ret != DCAMERA_OK) {
426 DHLOGE("DCameraSoftbusSession SendBytes sessionId: %d failed: %d peerDevId: %s peerSessionName: %s", sessionId_,
427 ret, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
428 }
429 return ret;
430 }
431
SendStream(std::shared_ptr<DataBuffer> & buffer)432 int32_t DCameraSoftbusSession::SendStream(std::shared_ptr<DataBuffer>& buffer)
433 {
434 if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
435 DHLOGE("DCameraSoftbusSession SendStream session state %d is not opened sessionId: %d peerDev: %s peerName: %s",
436 state_, sessionId_, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
437 return DCAMERA_WRONG_STATE;
438 }
439
440 int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusStream(sessionId_, buffer);
441 if (ret != DCAMERA_OK) {
442 DHLOGE("DCameraSoftbusSession SendStream sessionId: %d failed: %d peerDevId: %s peerSessionName: %s",
443 sessionId_, ret, GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str());
444 }
445 return ret;
446 }
447
GetPeerDevId()448 std::string DCameraSoftbusSession::GetPeerDevId()
449 {
450 return peerDevId_;
451 }
452
GetPeerSessionName()453 std::string DCameraSoftbusSession::GetPeerSessionName()
454 {
455 return peerSessionName_;
456 }
457
GetMySessionName()458 std::string DCameraSoftbusSession::GetMySessionName()
459 {
460 return mySessionName_;
461 }
462 } // namespace DistributedHardware
463 } // namespace OHOS
464