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 TELEPHONY_SESSION_ADAPTER_H
17 #define TELEPHONY_SESSION_ADAPTER_H
18 
19 #include <memory>
20 #include "ffrt.h"
21 #include "transport/socket.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 constexpr int32_t INVALID_SOCKET_ID = -1;
26 constexpr int32_t QOS_MIN_BW = 4 * 1024 * 1024;
27 constexpr int32_t QOS_MAX_LATENCY = 10000;
28 constexpr const char* PACKET_NAME = "ohos.telephony.callmanager";
29 constexpr const char* SESSION_NAME = "ohos.telephony.callmanager.distributed_communication";
30 
31 class ISessionCallback {
32 public:
33     ISessionCallback() = default;
34     virtual ~ISessionCallback() = default;
35     virtual void OnConnected() = 0;
36     virtual void OnReceiveMsg(const char* data, uint32_t dataLen) = 0;
37 };
38 
39 class SessionAdapter {
40 public:
41     explicit SessionAdapter(const std::shared_ptr<ISessionCallback> &callback);
42     virtual ~SessionAdapter() = default;
43 
44     virtual void Create(const std::string &localName) = 0;
45     virtual void Destroy() = 0;
46     virtual void Connect(const std::string &peerDevId, const std::string &localName, const std::string &peerName) = 0;
47     virtual void Disconnect() = 0;
48     virtual void OnSessionBind(int32_t socket) = 0;
49     virtual void OnSessionShutdown(int32_t socket) = 0;
50 
51     bool IsReady();
52     void SendMsg(const void *data, uint32_t len);
53     void OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen);
54 
55     static void OnBind(int32_t socket, PeerSocketInfo info);
56     static void OnShutdown(int32_t socket, ShutdownReason reason);
57     static void OnBytes(int32_t socket, const void *data, uint32_t dataLen);
58     static void OnError(int32_t socket, int32_t errCode);
59 
60 protected:
61     ISocketListener listener_{};
62     std::shared_ptr<ISessionCallback> callback_{};
63     ffrt::mutex mutex_{};
64     int32_t socket_{INVALID_SOCKET_ID};
65 };
66 
67 } // namespace Telephony
68 } // namespace OHOS
69 
70 #endif // TELEPHONY_SESSION_ADAPTER_H
71