1 /*
2  * Copyright (c) 2022 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 REMOTE_COMMAND_MANAGER_H
17 #define REMOTE_COMMAND_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "accesstoken_log.h"
25 #include "base_remote_command.h"
26 #include "constant.h"
27 #include "data_validator.h"
28 #include "remote_command_executor.h"
29 #include "rpc_channel.h"
30 
31 namespace OHOS {
32 namespace Security {
33 namespace AccessToken {
34 class RemoteCommandManager final {
35 public:
36     ~RemoteCommandManager();
37 
38     /**
39      * @brief Singleton instance get method.
40      *
41      * @since 1.0
42      * @version 1.0
43      */
44     static RemoteCommandManager &GetInstance();
45 
46     /**
47      * @brief Init method.
48      *
49      * @see
50      * @since 1.0
51      * @version 1.0
52      */
53     void Init();
54 
55     /**
56      * @brief Execute a command now.
57      *
58      * @param udid The udid of a device which you want to execute on. if udid is empty, return -1.
59      * @param command A command extend BaseRemoteCommand. if command is nullptr, return -1.
60      * @return The execute result, returned from RemoteCommandExecutor.
61      * @see RemoteCommandExecutor.ExecuteOneCommand
62      * @since 1.0
63      * @version 1.0
64      */
65     int ExecuteCommand(const std::string &udid, const std::shared_ptr<BaseRemoteCommand>& command);
66 
67     /**
68      * @brief Add a command to buffer.
69      *
70      * @param udid The udid of a device which you want to execute on.
71      * @param command A command extend BaseRemoteCommand.
72      * @return The add result, returned from RemoteCommandExecutor. by now, SUCCESS: 0. INVALID_COMMAND: -14
73      * @see RemoteCommandExecutor.AddCommand
74      * @since 1.0
75      * @version 1.0
76      */
77     int AddCommand(const std::string &udid, const std::shared_ptr<BaseRemoteCommand>& command);
78 
79     /**
80      * @brief Execute all buffered commands for given device.
81      *
82      * @param udid The udid of a device which you want to execute on.
83      * @return The execute result. SUCCESS: 0; FAILURE: -1.
84      * @see RemoteCommandExecutor.ProcessBufferedCommands
85      * @since 1.0
86      * @version 1.0
87      */
88     int ProcessDeviceCommandImmediately(const std::string &udid);
89 
90     /**
91      * @brief Execute all buffered commands for all device asynchronized.
92      *
93      * @return The loop result. SUCCESS: 0.
94      * @see RemoteCommandExecutor.ProcessBufferedCommandsWithThread
95      * @since 1.0
96      * @version 1.0
97      */
98     int Loop();
99 
100     /**
101      * @brief Clear buffered commands.
102      *
103      * @since 1.0
104      * @version 1.0
105      */
106     void Clear();
107 
108     /**
109      * @brief Remove a command from buffer.
110      *
111      * @param udid The udid of a device which you want to remove.
112      */
113     void RemoveCommand(const std::string &udid);
114 
115     /**
116      * @brief For event of device online, prepare channel and build connection with peer device.
117      *
118      * @param peerNodeId The udid of peer device.
119      * @return Result code indicates if notify successfully. SUCCESS: 0, FAILURE: -1.
120      * @since 1.0
121      * @version 1.0
122      */
123     int NotifyDeviceOnline(const std::string &peerNodeId);
124 
125     /**
126      * @brief For event of device offline, clean caches related to peer device.
127      *
128      * @param peerNodeId The peer device's nodeId, maybe uuid or udid .
129      * @return Result code indicates if notify successfully. SUCCESS: 0, FAILURE: -1.
130      * @since 1.0
131      * @version 1.0
132      */
133     int NotifyDeviceOffline(const std::string &peerNodeId);
134 
135     /**
136      * @brief Get remote command executor's channel for given nodeId.
137      *
138      * @param nodeId The peer device's nodeId, maybe uuid or udid or networkId.
139      * @return Channel instance if remote command executor has been created, null otherwise.
140      */
141     std::shared_ptr<RpcChannel> GetExecutorChannel(const std::string &nodeId);
142 
143 private:
144     RemoteCommandManager();
145 
146     // executors buffer
147     std::map<std::string, std::shared_ptr<RemoteCommandExecutor>> executors_;
148     // executors buffer mutex
149     std::mutex mutex_;
150 
151     /**
152      * @brief Fetch an executor from executors buffer. If not found, create one and cache it to buffer.
153      *
154      * @param nodeId The udid of a device which you want to get executor.
155      * @see void
156      * @since 1.0
157      * @version 1.0
158      */
159     std::shared_ptr<RemoteCommandExecutor> GetOrCreateRemoteCommandExecutor(const std::string &nodeId);
160 };
161 }  // namespace AccessToken
162 }  // namespace Security
163 }  // namespace OHOS
164 
165 #endif
166