1 /*
2  * Copyright (c) 2021 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 SA_CLIENT_ADAPTER_H
17 #define SA_CLIENT_ADAPTER_H
18 
19 #include <mutex>
20 
21 #include "client_executor/include/client_factory.h"
22 #include "platform/threadpool/include/thread_pool.h"
23 
24 namespace OHOS {
25 namespace AI {
26 /**
27  * SA client thread class
28  */
29 class ConnectMgrWorker : public IWorker {
30 public:
31     ConnectMgrWorker(const ConfigInfo &configInfo, ClientInfo &clientInfo);
32     ~ConnectMgrWorker() override = default;
33 
34     const char *GetName() const override;
35     bool OneAction() override;
36     bool Initialize() override;
37     void Uninitialize() override;
38 
39 private:
40     ConfigInfo configInfo_;
41     ClientInfo clientInfo_;
42 };
43 
44 class SaClientAdapter : public ClientFactory {
45     FORBID_COPY_AND_ASSIGN(SaClientAdapter);
46     FORBID_CREATE_BY_SELF(SaClientAdapter);
47 
48 public:
49     static SaClientAdapter *GetInstance();
50 
51     static void ReleaseInstance();
52 
53 private:
54     /**
55      * Start a thread {@link StartConnectMgrThread(ConfigInfo, ClientInfo, AlgorithmInfo)}
56      * to Init client and connect to AI SA server.
57      *
58      * @param [in] configInfo Engine configuration information.
59      * @param [out] clientInfo Client information.
60      * @param [in] algorithmInfo Algorithm information.
61      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
62      */
63     int InitAiServer(const ConfigInfo &configInfo, ClientInfo &clientInfo,
64         const AlgorithmInfo &algorithmInfo) override;
65 
66     /**
67      * Stop a thread {@link StopConnectMgrThread()} to destroy client, and disconnect from AI SA server.
68      *
69      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
70      */
71     int CloseAiServer() override;
72 
73     /**
74      * Call SA client, to load algorithm plugin and model based on algorithm information and client information.
75      *
76      * @param [in] clientInfo Client information.
77      * @param [in] algorithmInfo Algorithm information.
78      * @param [in] inputInfo Data information needed to load algorithm plugin.
79      * @param [out] outputInfo The returned data information after loading the algorithm plugin.
80      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
81      */
82     int LoadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
83         const DataInfo &inputInfo, DataInfo &outputInfo) override;
84 
85     /**
86      * Call SA client, to unload model and plugin.
87      *
88      * After the algorithm is executed successfully, the method needs to be called when exiting.
89      *
90      * @param [in] clientInfo Client information.
91      * @param [in] algorithmInfo Algorithm information.
92      * @param [in] inputInfo Data information needed to unload the plugin.
93      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
94      */
95     int UnLoadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
96         const DataInfo &inputInfo) override;
97 
98     /**
99      * Call SA client, to execute algorithm inference synchronously.
100      *
101      * @param [in] clientInfo Client information.
102      * @param [in] AlgorithmInfo Algorithm information.
103      * @param [in] inputInfo Data information needed to synchronous execution algorithm.
104      * @param [out] outputInfo Algorithm inference results.
105      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
106      */
107     int SyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
108         const DataInfo &inputInfo, DataInfo &outputInfo) override;
109 
110     /**
111      * Call SA client, to execute algorithm inference asynchronously.
112      *
113      * The inference result is notified to the client through callback function passed in by the callback
114      * saved by SaAsyncHandler
115      *
116      * @param [in] clientInfo Client information.
117      * @param [in] algorithmInfo Algorithm information.
118      * @param [in] inputInfo Data information needed to asynchronous execution algorithm.
119      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
120      */
121     int AsyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
122         const DataInfo &inputInfo) override;
123 
124     /**
125      * Call SA client, to set the configuration parameters of the engine or plugin.
126      *
127      * @param [in] clientInfo Client information.
128      * @param [in] optionType The type of setting option.
129      * @param [in] inputInfo Configuration parameter needed to set up the engine or plugin.
130      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
131      */
132     int SetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo) override;
133 
134     /**
135      * Call SA client, to get the configuration parameters of the engine or plugin.
136      *
137      * @param [in] clientInfo Client information.
138      * @param [in] optionType The type of getting option.
139      * @param [in] inputInfo Parameter information for getting options.
140      * @param [out] outputInfo The configuration parameter information.
141      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
142      */
143     int GetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo,
144         DataInfo &outputInfo) override;
145 
146 private:
147     static std::mutex instance_mutex_;
148     static SaClientAdapter *instance_;
149 
150     std::shared_ptr<Thread> connectMgrThread_ = nullptr;
151     ConnectMgrWorker *connectMgrWorker_ {nullptr};
152 };
153 } // namespace AI
154 } // namespace OHOS
155 
156 #endif // SA_CLIENT_ADAPTER_H
157