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
27namespace OHOS {
28namespace DistributedHardware {
29DCameraSoftbusSession::DCameraSoftbusSession()
30{
31    sessionId_ = -1;
32    state_ = DCAMERA_SOFTBUS_STATE_CLOSED;
33    mode_ = DCAMERA_SESSION_MODE_CTRL;
34    ResetAssembleFrag();
35}
36
37DCameraSoftbusSession::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
50DCameraSoftbusSession::~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
64int32_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
85int32_t DCameraSoftbusSession::OnSessionOpened(int32_t socket, std::string networkId)
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, networkId);
92    DHLOGI("open current session end, socket: %{public}d", socket);
93    return DCAMERA_OK;
94}
95
96int32_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
107int32_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
118void 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
128void 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
165void 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
188void 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
237int32_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
268void DCameraSoftbusSession::ResetAssembleFrag()
269{
270    isWaiting_ = false;
271    nowSeq_ = 0;
272    nowSubSeq_ = 0;
273    offset_ = 0;
274    totalLen_ = 0;
275    packBuffer_ = nullptr;
276}
277
278void 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
286void 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
297uint16_t DCameraSoftbusSession::U16Get(const uint8_t *ptr)
298{
299    return (ptr[0] << DCAMERA_SHIFT_8) | ptr[1];
300}
301
302uint32_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
307int32_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
326int32_t DCameraSoftbusSession::CreateSocketServer()
327{
328    int32_t ret = DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(mySessionName_,
329        DCAMERA_CHANNLE_ROLE_SINK, mode_, peerDevId_, peerSessionName_);
330    if (ret != DCAMERA_OK) {
331        DHLOGE("DCameraSoftbusSession CreateSocketServer Error, ret %{public}d", ret);
332        return ret;
333    }
334    return DCAMERA_OK;
335}
336
337int32_t DCameraSoftbusSession::BindSocketServer()
338{
339    int32_t socketId = DCameraSoftbusAdapter::GetInstance().CreateSoftBusSourceSocketClient(myDevId_, peerSessionName_,
340        peerDevId_, mode_, DCAMERA_CHANNLE_ROLE_SOURCE);
341    if (socketId <= 0) {
342        DHLOGE("DCameraSoftbusSession BindSocketServer Error, socketId %{public}d", socketId);
343        return socketId;
344    }
345    OnSessionOpened(socketId, myDevId_);
346    return socketId;
347}
348
349void DCameraSoftbusSession::ReleaseSession()
350{
351    DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(mySessionName_);
352}
353
354int32_t DCameraSoftbusSession::UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc)
355{
356    CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null");
357    uint16_t subSeq = 0;
358    uint32_t seq = 0;
359    uint32_t totalLen = buffer->Size();
360    SessionDataHeader headPara = { PROTOCOL_VERSION, FRAG_START, mode_, seq, totalLen, subSeq };
361    if (buffer->Size() <= BINARY_DATA_PACKET_MAX_LEN) {
362        headPara.fragFlag = FRAG_START_END;
363        headPara.dataLen = buffer->Size();
364        std::shared_ptr<DataBuffer> unpackData = std::make_shared<DataBuffer>(buffer->Size() + BINARY_HEADER_FRAG_LEN);
365        MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
366        int32_t ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
367            buffer->Data(), buffer->Size());
368        if (ret != EOK) {
369            DHLOGE("DCameraSoftbusSession UnPackSendData START_END memcpy_s failed, ret: %{public}d, sess: %{public}s "
370                "peerSess: %{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str());
371            return ret;
372        }
373        return SendBytes(unpackData);
374    }
375    uint32_t offset = 0;
376    while (totalLen > offset) {
377        SetHeadParaDataLen(headPara, totalLen, offset);
378        uint64_t bufferSize = static_cast<uint64_t>(buffer->Size());
379        DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, "
380            "nowTime: %{public}" PRId64" start:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
381        std::shared_ptr<DataBuffer> unpackData =
382            std::make_shared<DataBuffer>(headPara.dataLen + BINARY_HEADER_FRAG_LEN);
383        MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN);
384        int ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN,
385            buffer->Data() + offset, headPara.dataLen);
386        if (ret != EOK) {
387            DHLOGE("DCameraSoftbusSession UnPackSendData memcpy_s failed, ret: %{public}d, sess: %{public}s peerSess: "
388                "%{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str());
389            return ret;
390        }
391        ret = SendBytes(unpackData);
392        if (ret != DCAMERA_OK) {
393            DHLOGE("DCameraSoftbusSession sendData failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s",
394                ret, mySessionName_.c_str(), peerSessionName_.c_str());
395            return ret;
396        }
397        DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, "
398            "nowTime: %{public}" PRId64" end:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs());
399        headPara.subSeq++;
400        headPara.fragFlag = FRAG_MID;
401        offset += headPara.dataLen;
402    }
403    return DCAMERA_OK;
404}
405
406void DCameraSoftbusSession::SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen,
407    const uint32_t offset)
408{
409    if (totalLen >= offset) {
410        if (totalLen - offset > BINARY_DATA_PACKET_MAX_LEN) {
411            headPara.dataLen = BINARY_DATA_PACKET_MAX_LEN - BINARY_DATA_PACKET_RESERVED_BUFFER;
412        } else {
413            headPara.fragFlag = FRAG_END;
414            headPara.dataLen = totalLen - offset;
415        }
416    }
417}
418
419void DCameraSoftbusSession::MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len)
420{
421    uint32_t headerLen = sizeof(uint8_t) * HEADER_UINT8_NUM + sizeof(uint16_t) * HEADER_UINT16_NUM +
422        sizeof(uint32_t) * HEADER_UINT32_NUM;
423    if (headerLen > len) {
424        DHLOGE("MakeFragDataHeader %{public}d over len %{public}d", headerLen, len);
425        return;
426    }
427    uint32_t i = 0;
428    header[i++] = headPara.version >> DCAMERA_SHIFT_8;
429    header[i++] = headPara.version & UINT16_SHIFT_MASK_0;
430    header[i++] = headPara.fragFlag;
431    header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
432    header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
433    header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
434    header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_0);
435    header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
436    header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
437    header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
438    header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_0);
439    header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
440    header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
441    header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
442    header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_0);
443    header[i++] = headPara.subSeq >> DCAMERA_SHIFT_8;
444    header[i++] = headPara.subSeq & UINT16_SHIFT_MASK_0;
445    header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24;
446    header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16;
447    header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8;
448    header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_0);
449}
450
451int32_t DCameraSoftbusSession::SendBytes(std::shared_ptr<DataBuffer>& buffer)
452{
453    if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
454        DHLOGE("DCameraSoftbusSession SendBytes session state %{public}d is not opened sessionId: %{public}d "
455            "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(),
456            GetAnonyString(peerSessionName_).c_str());
457        return DCAMERA_WRONG_STATE;
458    }
459
460    int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusBytes(sessionId_, buffer);
461    if (ret != DCAMERA_OK) {
462        DHLOGE("DCameraSoftbusSession SendBytes sessionId: %{public}d failed: %{public}d peerDevId: %{public}s "
463            "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(),
464            GetAnonyString(peerSessionName_).c_str());
465    }
466    return ret;
467}
468
469int32_t DCameraSoftbusSession::SendStream(std::shared_ptr<DataBuffer>& buffer)
470{
471    if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) {
472        DHLOGE("DCameraSoftbusSession SendStream session state %{public}d is not opened sessionId: %{public}d "
473            "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(),
474            GetAnonyString(peerSessionName_).c_str());
475        return DCAMERA_WRONG_STATE;
476    }
477
478    int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusStream(sessionId_, buffer);
479    if (ret != DCAMERA_OK) {
480        DHLOGE("DCameraSoftbusSession SendStream sessionId: %{public}d failed: %{public}d peerDevId: %{public}s "
481            "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(),
482            GetAnonyString(peerSessionName_).c_str());
483    }
484    return ret;
485}
486
487std::string DCameraSoftbusSession::GetPeerDevId()
488{
489    return peerDevId_;
490}
491
492std::string DCameraSoftbusSession::GetPeerSessionName()
493{
494    return peerSessionName_;
495}
496
497std::string DCameraSoftbusSession::GetMySessionName()
498{
499    return mySessionName_;
500}
501
502int32_t DCameraSoftbusSession::GetSessionId()
503{
504    return sessionId_;
505}
506} // namespace DistributedHardware
507} // namespace OHOS
508