• 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 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     int32_t GetSessionId();
48     int32_t CreateSocketServer();
49     int32_t BindSocketServer();
50     void ReleaseSession();
51 
52 private:
53     struct SessionDataHeader {
54         uint16_t version;
55         uint8_t fragFlag;
56         uint32_t dataType;
57         uint32_t seqNum;
58         uint32_t totalLen;
59         uint16_t subSeq;
60         uint32_t dataLen;
61     };
62 
63     using DCameraSendFuc = int32_t (DCameraSoftbusSession::*)(std::shared_ptr<DataBuffer>& buffer);
64     int32_t SendBytes(std::shared_ptr<DataBuffer>& buffer);
65     int32_t SendStream(std::shared_ptr<DataBuffer>& buffer);
66     void DealRecvData(std::shared_ptr<DataBuffer>& buffer);
67     void PackRecvData(std::shared_ptr<DataBuffer>& buffer);
68     void AssembleNoFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara);
69     void AssembleFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara);
70     int32_t CheckUnPackBuffer(SessionDataHeader& headerPara);
71     void GetFragDataLen(uint8_t *ptrPacket, SessionDataHeader& headerPara);
72     int32_t UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc);
73     void MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len);
74     void PostData(std::shared_ptr<DataBuffer>& buffer);
75     uint16_t U16Get(const uint8_t *ptr);
76     uint32_t U32Get(const uint8_t *ptr);
77     void ResetAssembleFrag();
78     void SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen, const uint32_t offset);
79 
80     enum {
81         FRAG_NULL = 0,
82         FRAG_START,
83         FRAG_MID,
84         FRAG_END,
85         FRAG_START_END,
86     };
87 
88     static const uint32_t BINARY_DATA_MAX_TOTAL_LEN = 100 * 1024 * 1024;
89     static const uint32_t BINARY_DATA_MAX_LEN = 4 * 1024 * 1024;
90     static const uint32_t BINARY_DATA_PACKET_MAX_LEN = 4 * 1024 * 1024;
91     static const uint32_t BINARY_DATA_PACKET_RESERVED_BUFFER = 512;
92     static const uint16_t PROTOCOL_VERSION = 1;
93     static const uint16_t HEADER_UINT8_NUM = 1;
94     static const uint16_t HEADER_UINT16_NUM = 2;
95     static const uint16_t HEADER_UINT32_NUM = 4;
96     static const uint16_t BINARY_HEADER_FRAG_LEN = 21;
97 
98     static const uint32_t BINARY_HEADER_FRAG_OFFSET = 2;
99     static const uint32_t BINARY_HEADER_DATATYPE_OFFSET = 3;
100     static const uint32_t BINARY_HEADER_SEQNUM_OFFSET = 7;
101     static const uint32_t BINARY_HEADER_TOTALLEN_OFFSET = 11;
102     static const uint32_t BINARY_HEADER_SUBSEQ_OFFSET = 15;
103     static const uint32_t BINARY_HEADER_DATALEN_OFFSET = 17;
104 
105     std::shared_ptr<DataBuffer> packBuffer_;
106     bool isWaiting_;
107     uint32_t nowSeq_;
108     uint32_t nowSubSeq_;
109     uint32_t offset_;
110     uint32_t totalLen_;
111 
112 private:
113     std::string myDevId_;
114     std::string mySessionName_;
115     std::string peerDevId_;
116     std::string peerSessionName_;
117     std::shared_ptr<ICameraChannelListener> listener_;
118     int32_t sessionId_;
119     DCameraSofbutState state_;
120     DCameraSessionMode mode_;
121     std::map<DCameraSessionMode, DCameraSendFuc> sendFuncMap_;
122     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
123 };
124 } // namespace DistributedHardware
125 } // namespace OHOS
126 #endif // OHOS_DCAMERA_SOFTBUS_SESSION_H
127