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