1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef DSOFTBUS_HANDLER_H
17c29fa5a6Sopenharmony_ci#define DSOFTBUS_HANDLER_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include "nocopyable.h"
20c29fa5a6Sopenharmony_ci
21c29fa5a6Sopenharmony_ci#include "channel.h"
22c29fa5a6Sopenharmony_ci#include "cooperate_events.h"
23c29fa5a6Sopenharmony_ci#include "i_context.h"
24c29fa5a6Sopenharmony_ci
25c29fa5a6Sopenharmony_cinamespace OHOS {
26c29fa5a6Sopenharmony_cinamespace Msdp {
27c29fa5a6Sopenharmony_cinamespace DeviceStatus {
28c29fa5a6Sopenharmony_cinamespace Cooperate {
29c29fa5a6Sopenharmony_ciclass DSoftbusHandler final {
30c29fa5a6Sopenharmony_ci    class DSoftbusObserver final : public IDSoftbusObserver {
31c29fa5a6Sopenharmony_ci    public:
32c29fa5a6Sopenharmony_ci        DSoftbusObserver(DSoftbusHandler &parent) : parent_(parent) {}
33c29fa5a6Sopenharmony_ci        ~DSoftbusObserver() = default;
34c29fa5a6Sopenharmony_ci
35c29fa5a6Sopenharmony_ci        void OnBind(const std::string &networkId) override
36c29fa5a6Sopenharmony_ci        {
37c29fa5a6Sopenharmony_ci            parent_.OnBind(networkId);
38c29fa5a6Sopenharmony_ci        }
39c29fa5a6Sopenharmony_ci
40c29fa5a6Sopenharmony_ci        void OnShutdown(const std::string &networkId) override
41c29fa5a6Sopenharmony_ci        {
42c29fa5a6Sopenharmony_ci            parent_.OnShutdown(networkId);
43c29fa5a6Sopenharmony_ci        }
44c29fa5a6Sopenharmony_ci
45c29fa5a6Sopenharmony_ci        void OnConnected(const std::string &networkId) override
46c29fa5a6Sopenharmony_ci        {
47c29fa5a6Sopenharmony_ci            parent_.OnConnected(networkId);
48c29fa5a6Sopenharmony_ci        }
49c29fa5a6Sopenharmony_ci
50c29fa5a6Sopenharmony_ci        bool OnPacket(const std::string &networkId, NetPacket &packet) override
51c29fa5a6Sopenharmony_ci        {
52c29fa5a6Sopenharmony_ci            return parent_.OnPacket(networkId, packet);
53c29fa5a6Sopenharmony_ci        }
54c29fa5a6Sopenharmony_ci
55c29fa5a6Sopenharmony_ci        bool OnRawData(const std::string &networkId, const void *data, uint32_t dataLen) override
56c29fa5a6Sopenharmony_ci        {
57c29fa5a6Sopenharmony_ci            return false;
58c29fa5a6Sopenharmony_ci        }
59c29fa5a6Sopenharmony_ci
60c29fa5a6Sopenharmony_ci    private:
61c29fa5a6Sopenharmony_ci        DSoftbusHandler &parent_;
62c29fa5a6Sopenharmony_ci    };
63c29fa5a6Sopenharmony_ci
64c29fa5a6Sopenharmony_cipublic:
65c29fa5a6Sopenharmony_ci    DSoftbusHandler(IContext *env);
66c29fa5a6Sopenharmony_ci    ~DSoftbusHandler();
67c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(DSoftbusHandler);
68c29fa5a6Sopenharmony_ci
69c29fa5a6Sopenharmony_ci    void AttachSender(Channel<CooperateEvent>::Sender sender);
70c29fa5a6Sopenharmony_ci    int32_t OpenSession(const std::string &networkId);
71c29fa5a6Sopenharmony_ci    void CloseSession(const std::string &networkId);
72c29fa5a6Sopenharmony_ci    void CloseAllSessions();
73c29fa5a6Sopenharmony_ci
74c29fa5a6Sopenharmony_ci    int32_t StartCooperate(const std::string &networkId, const DSoftbusStartCooperate &event);
75c29fa5a6Sopenharmony_ci    int32_t StopCooperate(const std::string &networkId, const DSoftbusStopCooperate &event);
76c29fa5a6Sopenharmony_ci    int32_t ComeBack(const std::string &networkId, const DSoftbusComeBack &event);
77c29fa5a6Sopenharmony_ci    int32_t RelayCooperate(const std::string &networkId, const DSoftbusRelayCooperate &event);
78c29fa5a6Sopenharmony_ci    int32_t RelayCooperateFinish(const std::string &networkId, const DSoftbusRelayCooperateFinished &event);
79c29fa5a6Sopenharmony_ci    static std::string GetLocalNetworkId();
80c29fa5a6Sopenharmony_ci
81c29fa5a6Sopenharmony_ciprivate:
82c29fa5a6Sopenharmony_ci    void OnBind(const std::string &networkId);
83c29fa5a6Sopenharmony_ci    void OnShutdown(const std::string &networkId);
84c29fa5a6Sopenharmony_ci    void OnConnected(const std::string &networkId);
85c29fa5a6Sopenharmony_ci    bool OnPacket(const std::string &networkId, NetPacket &packet);
86c29fa5a6Sopenharmony_ci    void SendEvent(const CooperateEvent &event);
87c29fa5a6Sopenharmony_ci    void OnCommunicationFailure(const std::string &networkId);
88c29fa5a6Sopenharmony_ci    void OnStartCooperate(const std::string &networkId, NetPacket &packet);
89c29fa5a6Sopenharmony_ci    void OnStopCooperate(const std::string &networkId, NetPacket &packet);
90c29fa5a6Sopenharmony_ci    void OnComeBack(const std::string &networkId, NetPacket &packet);
91c29fa5a6Sopenharmony_ci    void OnRelayCooperate(const std::string &networkId, NetPacket &packet);
92c29fa5a6Sopenharmony_ci    void OnRelayCooperateFinish(const std::string &networkId, NetPacket &packet);
93c29fa5a6Sopenharmony_ci    void OnSubscribeMouseLocation(const std::string& networKId, NetPacket &packet);
94c29fa5a6Sopenharmony_ci    void OnUnSubscribeMouseLocation(const std::string& networKId, NetPacket &packet);
95c29fa5a6Sopenharmony_ci    void OnReplySubscribeLocation(const std::string& networKId, NetPacket &packet);
96c29fa5a6Sopenharmony_ci    void OnReplyUnSubscribeLocation(const std::string& networKId, NetPacket &packet);
97c29fa5a6Sopenharmony_ci    void OnRemoteMouseLocation(const std::string& networKId, NetPacket &packet);
98c29fa5a6Sopenharmony_ci    void OnRemoteInputDevice(const std::string& networKId, NetPacket &packet);
99c29fa5a6Sopenharmony_ci    void OnRemoteHotPlug(const std::string& networKId, NetPacket &packet);
100c29fa5a6Sopenharmony_ci    int32_t DeserializeDevice(std::shared_ptr<IDevice> device, NetPacket &packet);
101c29fa5a6Sopenharmony_ci
102c29fa5a6Sopenharmony_ci    IContext *env_ { nullptr };
103c29fa5a6Sopenharmony_ci    std::mutex lock_;
104c29fa5a6Sopenharmony_ci    Channel<CooperateEvent>::Sender sender_;
105c29fa5a6Sopenharmony_ci    std::shared_ptr<DSoftbusObserver> observer_;
106c29fa5a6Sopenharmony_ci    std::map<int32_t, std::function<void(const std::string &networkId, NetPacket &packet)>> handles_;
107c29fa5a6Sopenharmony_ci};
108c29fa5a6Sopenharmony_ci} // namespace Cooperate
109c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
110c29fa5a6Sopenharmony_ci} // namespace Msdp
111c29fa5a6Sopenharmony_ci} // namespace OHOS
112c29fa5a6Sopenharmony_ci#endif // DSOFTBUS_HANDLER_H
113