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