• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_DM_COMM_TOOL_H
17 #define OHOS_DM_COMM_TOOL_H
18 
19 #include <mutex>
20 #include <queue>
21 #include "dm_transport.h"
22 #include "dm_transport_msg.h"
23 
24 #include "ffrt.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 using EventCallback = std::function<void ()>;
29 class DMCommTool : public std::enable_shared_from_this<DMCommTool> {
30 public:
31     DMCommTool();
32     virtual ~DMCommTool() = default;
33     static std::shared_ptr<DMCommTool> GetInstance();
34     void Init();
35     void UnInit();
36 
37     int32_t SendUserIds(const std::string rmtNetworkId, const std::vector<uint32_t> &foregroundUserIds,
38         const std::vector<uint32_t> &backgroundUserIds);
39 
40     int32_t SendUninstAppObj(int32_t userId, int32_t tokenId, const std::string &networkId);
41 
42     int32_t SendUnBindAppObj(int32_t userId, int32_t tokenId, const std::string &extra, const std::string &networkId,
43         const std::string &udid);
44 
45     void RspLocalFrontOrBackUserIds(const std::string &rmtNetworkId, const std::vector<uint32_t> &foregroundUserIds,
46         const std::vector<uint32_t> &backgroundUserIds, int32_t socketId);
47     int32_t CreateUserStopMessage(int32_t stopUserId, std::string &msgStr);
48     int32_t SendMsg(const std::string rmtNetworkId, int32_t msgType, const std::string &msg);
49     int32_t SendUserStop(const std::string rmtNetworkId, int32_t stopUserId);
50     int32_t ParseUserStopMessage(const std::string &msgStr, int32_t &stopUserId);
51     void ProcessReceiveUserStopEvent(const std::shared_ptr<InnerCommMsg> commMsg);
52     void RspUserStop(const std::string rmtNetworkId, int32_t socketId, int32_t stopUserId);
53     void ProcessResponseUserStopEvent(const std::shared_ptr<InnerCommMsg> commMsg);
54     int32_t RspAppUninstall(const std::string &rmtNetworkId, int32_t socketId);
55     int32_t RspAppUnbind(const std::string &rmtNetworkId, int32_t socketId);
56     void StopSocket(const std::string &networkId);
57     void ProcessReceiveRspAppUninstallEvent(const std::shared_ptr<InnerCommMsg> commMsg);
58     void ProcessReceiveRspAppUnbindEvent(const std::shared_ptr<InnerCommMsg> commMsg);
59 
60     class DMCommToolEventHandler : public AppExecFwk::EventHandler {
61     public:
62         DMCommToolEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,
63             std::shared_ptr<DMCommTool> dmCommToolPtr);
64         ~DMCommToolEventHandler() override = default;
65         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
66         void HandleEvent(const std::shared_ptr<DMCommTool> &dmCommToolPtr, uint32_t eventId,
67             const std::shared_ptr<InnerCommMsg> &commMsg, const UserIdsMsg &userIdsMsg);
68         void HandleLocalUserIdEvent(const std::shared_ptr<DMCommTool> &dmCommToolPtr, uint32_t eventId,
69             const std::shared_ptr<InnerCommMsg> &commMsg, const UserIdsMsg &userIdsMsg);
70     private:
71         void ParseUserIdsMsg(std::shared_ptr<InnerCommMsg> commMsg, UserIdsMsg &userIdsMsg);
72         std::weak_ptr<DMCommTool> dmCommToolWPtr_;
73     };
74     std::shared_ptr<DMCommTool::DMCommToolEventHandler> GetEventHandler();
75     const std::shared_ptr<DMTransport> GetDMTransportPtr();
76 
77     void ProcessReceiveUserIdsEvent(const std::shared_ptr<InnerCommMsg> commMsg);
78     void ProcessResponseUserIdsEvent(const std::shared_ptr<InnerCommMsg> commMsg);
79     int32_t SendLogoutAccountInfo(const std::string &rmtNetworkId, const std::string &accountId, int32_t userId);
80     void ProcessReceiveLogoutEvent(const std::shared_ptr<InnerCommMsg> commMsg);
81     int32_t StartCommonEvent(std::string commonEventType, EventCallback eventCallback);
82     void ProcessReceiveCommonEvent(const std::shared_ptr<InnerCommMsg> commMsg);
83     void ProcessResponseCommonEvent(const std::shared_ptr<InnerCommMsg> commMsg);
84     void ProcessReceiveUninstAppEvent(const std::shared_ptr<InnerCommMsg> &commMsg);
85     void ProcessReceiveUnBindAppEvent(const std::shared_ptr<InnerCommMsg> &commMsg);
86 private:
87     std::shared_ptr<DMTransport> dmTransportPtr_;
88     std::shared_ptr<DMCommTool::DMCommToolEventHandler> eventHandler_;
89     mutable std::mutex eventMutex_;
90     std::shared_ptr<ffrt::queue> eventQueue_;
91 };
92 } // DistributedHardware
93 } // OHOS
94 #endif