1 /*
2  * Copyright (c) 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_DSCHED_TRANSPORT_SOFTBUS_ADAPTER_H
17 #define OHOS_DSCHED_TRANSPORT_SOFTBUS_ADAPTER_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "dsched_softbus_session.h"
23 #include "idata_listener.h"
24 #include "single_instance.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 constexpr uint32_t INTERCEPT_STRING_LENGTH = 20;
30 constexpr uint32_t DSCHED_MAX_RECV_DATA_LEN = 104857600;
31 
32 constexpr int32_t DSCHED_QOS_TYPE_MIN_BW = 40 * 1024 * 1024;
33 constexpr int32_t DSCHED_QOS_TYPE_MAX_LATENCY = 6000;
34 constexpr int32_t DSCHED_QOS_TYPE_MIN_LATENCY = 1000;
35 
36 const std::string SOCKET_DMS_SESSION_NAME = "ohos.distributedschedule.dms.connect";
37 const std::string SOCKET_DMS_PKG_NAME = "dms";
38 }
39 
40 typedef enum {
41     SERVICE_TYPE_INVALID = -1,
42     SERVICE_TYPE_CONTINUE = 0,
43     SERVICE_TYPE_COLLABORATION = 1,
44 } DSchedServiceType;
45 
46 class DSchedTransportSoftbusAdapter {
47 DECLARE_SINGLE_INSTANCE_BASE(DSchedTransportSoftbusAdapter);
48 public:
49     int32_t InitChannel();
50     int32_t ConnectDevice(const std::string &peerDeviceId, int32_t &sessionId);
51     void DisconnectDevice(const std::string &peerDeviceId);
52     int32_t ReleaseChannel();
53     int32_t SendData(int32_t sessionId, int32_t dataType, std::shared_ptr<DSchedDataBuffer> dataBuffer);
54     int32_t SendBytesBySoftbus(int32_t sessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer);
55     void OnBind(int32_t sessionId, const std::string &peerDeviceId);
56     void OnShutdown(int32_t sessionId, bool isSelfCalled);
57     void OnBytes(int32_t sessionId, const void *data, uint32_t dataLen);
58     void OnDataReady(int32_t sessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer, uint32_t dataType);
59     void RegisterListener(int32_t serviceType, std::shared_ptr<IDataListener> listener);
60     void UnregisterListener(int32_t serviceType, std::shared_ptr<IDataListener> listener);
61     void SetCallingTokenId(int32_t callingTokenId);
62     bool GetSessionIdByDeviceId(const std::string &peerDeviceId, int32_t &sessionId);
63 
64 private:
65     DSchedTransportSoftbusAdapter();
66     ~DSchedTransportSoftbusAdapter();
67     int32_t CreateServerSocket();
68     int32_t CreateClientSocket(const std::string &peerDeviceId);
69     int32_t CreateSessionRecord(int32_t sessionId, const std::string &peerDeviceId, bool isServer);
70     int32_t AddNewPeerSession(const std::string &peerDeviceId, int32_t &sessionId);
71     void ShutdownSession(const std::string &peerDeviceId, int32_t sessionId);
72     void NotifyListenersSessionShutdown(int32_t sessionId, bool isSelfCalled);
73 
74 private:
75     std::map<int32_t, std::shared_ptr<DSchedSoftbusSession>> sessions_;
76     std::map<int32_t, std::vector<std::shared_ptr<IDataListener>>> listeners_;
77 
78     std::mutex sessionMutex_;
79     std::mutex listenerMutex_;
80     int32_t serverSocket_ = 0;
81     std::string localSessionName_;
82     int32_t callingTokenId_ = 0;
83 };
84 }  // namespace DistributedSchedule
85 }  // namespace OHOS
86 #endif  // OHOS_DSCHED_TRANSPORT_SOFTBUS_ADAPTER_H
87