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_H
17 #define SA_CLIENT_H
18 
19 #include <chrono>
20 #include <cstdio>
21 #include <cstring>
22 #include <mutex>
23 #include <thread>
24 #include <unistd.h>
25 #include <vector>
26 
27 #include "iproxy_client.h"
28 
29 #include "platform/semaphore/include/simple_event_notifier.h"
30 #include "protocol/retcode_inner/aie_retcode_inner.h"
31 #include "protocol/struct_definition/aie_info_define.h"
32 #include "utils/aie_macros.h"
33 
34 namespace OHOS {
35 namespace AI {
36 typedef void(*CallbackHandle)(int sessionId, const DataInfo &result, int resultCode, int requestId);
37 typedef void(*DeathCallbackHandle)();
38 
39 class SaClient {
40     FORBID_COPY_AND_ASSIGN(SaClient);
41     FORBID_CREATE_BY_SELF(SaClient);
42 
43 public:
44     static SaClient *GetInstance();
45     static void ReleaseInstance();
46 
47     /**
48      * Register SA callback for async processing; Transfer async result to sdk after receiving it
49      *
50      * @param [in] Callback Handle to deal with the async result.
51      */
52     void RegisterSaClientCb(CallbackHandle result);
53 
54     /**
55      * Unregister SA callback for async processing; Transfer async result to sdk after receiving it
56      */
57     void UnRegisterSaClientCb();
58 
59     /**
60      * Save dead callback to transfer dead message of aiserver to sdk/third-party application
61      *
62      * @param [in] Callback Handle to deal with the AI SA server dead message.
63      */
64     void RegisterSaDeathCb(DeathCallbackHandle deathCb);
65 
66     /**
67      * Delete dead callback to transfer dead message of aiserver to sdk/third-party application
68      */
69     void UnRegisterSaDeathCb();
70     CallbackHandle GetSaClientResultCb();
71     DeathCallbackHandle GetSaDeathResultCb();
72 
73     /**
74      * Call SA proxy, to connect the server to get the client ID
75      * and register server dead callback handle to SA manager.
76      *
77      * @param [in] configInfo Engine configuration information.
78      * @param [out] clientInfo Client information.
79      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
80      */
81     int Init(const ConfigInfo &configInfo, ClientInfo &clientInfo);
82 
83     /**
84      * Call SA proxy, to load algorithm plugin and model based on algorithm information and client information.
85      *
86      * @param [in] clientInfo Client information.
87      * @param [in] algorithmInfo Algorithm information.
88      * @param [in] inputInfo Data information needed to load algorithm plugin.
89      * @param [out] outputInfo The returned data information after loading the algorithm plugin.
90      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
91      */
92     int LoadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
93         const DataInfo &inputInfo, DataInfo &outputInfo);
94 
95     /**
96      * Call SA proxy, to unload algorithm plugin and model based on algorithm information and client information.
97      *
98      * @param [in] clientInfo Client information.
99      * @param [in] algorithmInfo Algorithm information.
100      * @param [in] inputInfo Data information needed to load algorithm plugin.
101      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
102      */
103     int UnloadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
104         const DataInfo &inputInfo);
105 
106     /**
107      * Call SA proxy, to execute algorithm inference synchronously.
108      *
109      * @param [in] clientInfo Client information.
110      * @param [in] AlgorithmInfo Algorithm information.
111      * @param [in] inputInfo Data information needed to synchronous execution algorithm.
112      * @param [out] outputInfo Algorithm inference results.
113      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
114      */
115     int SyncExecuteAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
116         const DataInfo &inputInfo, DataInfo &outputInfo);
117 
118     /**
119      * Call SA proxy, to execute algorithm inference asynchronously.
120      *
121      * The inference result is notified to the client through callback function passed in by
122      * {@link RegisterSaClientCb(CallbackHandle result)}.
123      *
124      * @param [in] clientInfo Client information.
125      * @param [in] algorithmInfo Algorithm information.
126      * @param [in] inputInfo Data information needed to asynchronous execution algorithm.
127      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
128      */
129     int AsyncExecuteAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
130         const DataInfo &inputInfo);
131 
132     /**
133      * Call SA proxy, to disconnect the client from the server, release and destroy information of the client.
134      *
135      * @param [in] clientInfo Client information.
136      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
137      */
138     int Destroy(const ClientInfo &clientInfo);
139 
140     /**
141      * Call SA proxy, to set the configuration parameters of the engine or plugin.
142      *
143      * @param [in] clientInfo Client information.
144      * @param [in] optionType The type of setting option.
145      * @param [in] inputInfo Configuration parameter needed to set up the engine or plugin.
146      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
147      */
148     int SetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo);
149 
150     /**
151      * Call SA proxy, to get the configuration parameters of the engine or plugin.
152      *
153      * @param [in] clientInfo Client information.
154      * @param [in] optionType The type of getting option.
155      * @param [in] inputInfo Parameter information for getting options.
156      * @param [out] outputInfo The configuration parameter information.
157      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
158      */
159     int GetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo, DataInfo &outputInfo);
160 
161     /**
162      * Call SA proxy, to register listener for async processing.
163      *
164      * @param [in] clientInfo Client information.
165      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
166      */
167     int RegisterCallback(const ClientInfo &clientInfo);
168 
169     /**
170      * Call SA proxy, to unregister listener for async processing.
171      *
172      * @param [in] clientInfo Client information.
173      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
174      */
175     int UnregisterCallback(const ClientInfo &clientInfo);
176 
177 private:
178     static std::mutex instance_mutex_;
179     static SaClient *instance_;
180     CallbackHandle ResultCb_ {nullptr};
181     DeathCallbackHandle deathCb_ {nullptr};
182 
183     uint32_t deadId_ {0};
184     IClientProxy *proxy_ = nullptr;
185     SvcIdentity svc_ {};
186 };
187 } // namespace AI
188 } // namespace OHOS
189 
190 #endif // SA_CLIENT_H
191