• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_DCAMERA_SOFTBUS_SESSION_H
17 #define OHOS_DCAMERA_SOFTBUS_SESSION_H
18 
19 #include "event_handler.h"
20 #include <string>
21 
22 #include "icamera_channel.h"
23 #include "icamera_channel_listener.h"
24 #include "transport/trans_type.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 typedef enum {
29     DCAMERA_SOFTBUS_STATE_CLOSED = 0,
30     DCAMERA_SOFTBUS_STATE_OPENED = 1,
31 } DCameraSofbutState;
32 
33 class DCameraSoftbusSession {
34 public:
35     DCameraSoftbusSession();
36     DCameraSoftbusSession(std::string myDhId, std::string myDevId, std::string mySessionName, std::string peerDevId,
37         std::string peerSessionName, std::shared_ptr<ICameraChannelListener> listener, DCameraSessionMode mode);
38     ~DCameraSoftbusSession();
39     int32_t CloseSession();
40     int32_t OnSessionOpened(int32_t socket, std::string networkId);
41     int32_t OnSessionClose(int32_t sessionId);
42     int32_t OnDataReceived(std::shared_ptr<DataBuffer>& buffer);
43     int32_t SendData(DCameraSessionMode mode, std::shared_ptr<DataBuffer>& buffer);
44     std::string GetPeerDevId();
45     std::string GetPeerSessionName();
46     std::string GetMySessionName();
47     std::string GetMyDhId();
48     int32_t GetSessionId();
49     int32_t CreateSocketServer();
50     int32_t BindSocketServer();
51     void ReleaseSession();
52 
53 private:
54     struct SessionDataHeader {
55         uint16_t version;
56         uint8_t fragFlag;
57         uint32_t dataType;
58         uint32_t seqNum;
59         uint32_t totalLen;
60         uint16_t subSeq;
61         uint32_t dataLen;
62     };
63 
64     using DCameraSendFuc = int32_t (DCameraSoftbusSession::*)(std::shared_ptr<DataBuffer>& buffer);
65     int32_t SendBytes(std::shared_ptr<DataBuffer>& buffer);
66     int32_t SendStream(std::shared_ptr<DataBuffer>& buffer);
67     void DealRecvData(std::shared_ptr<DataBuffer>& buffer);
68     void PackRecvData(std::shared_ptr<DataBuffer>& buffer);
69     void AssembleNoFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara);
70     void AssembleFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara);
71     int32_t CheckUnPackBuffer(SessionDataHeader& headerPara);
72     void GetFragDataLen(uint8_t *ptrPacket, SessionDataHeader& headerPara);
73     int32_t UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc);
74     void MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len);
75     void PostData(std::shared_ptr<DataBuffer>& buffer);
76     uint16_t U16Get(const uint8_t *ptr);
77     uint32_t U32Get(const uint8_t *ptr);
78     void ResetAssembleFrag();
79     void SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen, const uint32_t offset);
80 
81     enum {
82         FRAG_NULL = 0,
83         FRAG_START,
84         FRAG_MID,
85         FRAG_END,
86         FRAG_START_END,
87     };
88 
89     static const uint32_t BINARY_DATA_MAX_TOTAL_LEN = 100 * 1024 * 1024;
90     static const uint32_t BINARY_DATA_MAX_LEN = 4 * 1024 * 1024;
91     static const uint32_t BINARY_DATA_PACKET_MAX_LEN = 4 * 1024 * 1024;
92     static const uint32_t BINARY_DATA_PACKET_RESERVED_BUFFER = 512;
93     static const uint16_t PROTOCOL_VERSION = 1;
94     static const uint16_t HEADER_UINT8_NUM = 1;
95     static const uint16_t HEADER_UINT16_NUM = 2;
96     static const uint16_t HEADER_UINT32_NUM = 4;
97     static const uint16_t BINARY_HEADER_FRAG_LEN = 21;
98 
99     static const uint32_t BINARY_HEADER_FRAG_OFFSET = 2;
100     static const uint32_t BINARY_HEADER_DATATYPE_OFFSET = 3;
101     static const uint32_t BINARY_HEADER_SEQNUM_OFFSET = 7;
102     static const uint32_t BINARY_HEADER_TOTALLEN_OFFSET = 11;
103     static const uint32_t BINARY_HEADER_SUBSEQ_OFFSET = 15;
104     static const uint32_t BINARY_HEADER_DATALEN_OFFSET = 17;
105 
106     std::shared_ptr<DataBuffer> packBuffer_;
107     bool isWaiting_;
108     uint32_t nowSeq_;
109     uint32_t nowSubSeq_;
110     uint32_t offset_;
111     uint32_t totalLen_;
112 
113 private:
114     std::string myDhId_;
115     std::string myDevId_;
116     std::string mySessionName_;
117     std::string peerDevId_;
118     std::string peerSessionName_;
119     std::shared_ptr<ICameraChannelListener> listener_;
120     int32_t sessionId_;
121     DCameraSofbutState state_;
122     DCameraSessionMode mode_;
123     std::map<DCameraSessionMode, DCameraSendFuc> sendFuncMap_;
124     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
125 };
126 } // namespace DistributedHardware
127 } // namespace OHOS
128 #endif // OHOS_DCAMERA_SOFTBUS_SESSION_H
129