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