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 REMOTE_EXECUTOR_PROXY_H
17 #define REMOTE_EXECUTOR_PROXY_H
18 
19 #include <functional>
20 
21 #include "attributes.h"
22 #include "co_auth_client_callback.h"
23 #include "co_auth_client_defines.h"
24 #include "executor_callback_interface.h"
25 #include "remote_connect_manager.h"
26 #include "remote_msg_util.h"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 class RemoteExecutorProxy : public std::enable_shared_from_this<RemoteExecutorProxy> {
32 public:
33     RemoteExecutorProxy(std::string connectionName, ExecutorInfo registerInfo);
34     virtual ~RemoteExecutorProxy();
35 
36     ResultCode Start();
37 
38     // ConnectionListener
39     void OnMessage(const std::string &connectionName, const std::string &srcEndPoint,
40         const std::shared_ptr<Attributes> &request, std::shared_ptr<Attributes> &reply);
41     void OnConnectStatus(const std::string &connectionName, ConnectStatus connectStatus);
42 
43     // ExecutorCallbackInterface
44     void OnMessengerReady(const std::shared_ptr<ExecutorMessenger> &messenger,
45         const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList);
46     int32_t OnBeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command);
47     int32_t OnEndExecute(uint64_t scheduleId, const Attributes &command);
48     int32_t OnSendData(uint64_t scheduleId, const Attributes &data);
49 
50     void OnErrorFinish(uint64_t scheduleId);
51 
52 private:
53     // ExecutorMessengerInterface
54     int32_t ProcSendDataMsg(Attributes &data);
55     int32_t ProcFinishMsg(Attributes &data);
56 
57     std::recursive_mutex mutex_;
58     std::shared_ptr<ConnectionListener> connectionCallback_ = nullptr;
59     std::shared_ptr<ExecutorRegisterCallback> executorCallback_ = nullptr;
60     std::shared_ptr<ExecutorMessenger> messenger_ = nullptr;
61 
62     uint64_t executorIndex_ = 0;
63     std::string connectionName_ = "";
64     ExecutorInfo registerInfo_ = {};
65     std::string endPointName_ = "";
66 };
67 } // namespace UserAuth
68 } // namespace UserIam
69 } // namespace OHOS
70 #endif // REMOTE_EXECUTOR_PROXY_H