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 CLIENT_FACTORY_H
17 #define CLIENT_FACTORY_H
18 
19 #include <atomic>
20 #include <map>
21 #include <mutex>
22 
23 #include "client_executor/include/i_client_cb.h"
24 #include "protocol/retcode_inner/aie_retcode_inner.h"
25 #include "protocol/struct_definition/aie_info_define.h"
26 #include "utils/constants/constants.h"
27 
28 namespace OHOS {
29 namespace AI {
30 const int AIE_SESSION_ID_BEGIN = 0;
31 
32 class ClientFactory {
33 public:
34     ClientFactory();
35     virtual ~ClientFactory();
36 
37     /**
38      * Connect the server to get the client ID and initialize the client.
39      *
40      * @param [in] configInfo Engine configuration information.
41      * @param [out] clientInfo Client information.
42      * @param [in] algorithmInfo Algorithm information.
43      * @param [in] cb Service dead callback, Called when the service is dead.
44      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
45      */
46     int ClientInit(const ConfigInfo &configInfo, ClientInfo &clientInfo,
47         const AlgorithmInfo &algorithmInfo, IServiceDeadCb *cb);
48 
49     /**
50      * Load algorithm plugin and model based on algorithm information and client information.
51      *
52      * @param [in] clientInfo Client information.
53      * @param [in] algorithmInfo Algorithm information.
54      * @param [in] inputInfo Data information needed to load algorithm plugin.
55      * @param [out] outputInfo The returned data information after loading the algorithm plugin.
56      * @param [in] cb Callback function, asynchronous inference needs to be passed in,
57      *                synchronous inference is not required.
58      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
59      */
60     int ClientPrepare(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
61         const DataInfo &inputInfo, DataInfo &outputInfo, IClientCb *cb);
62 
63     /**
64      * Set the configuration parameters of the engine or plugin.
65      *
66      * @param [in] clientInfo Client information.
67      * @param [in] optionType The type of setting option.
68      * @param [in] inputInfo Configuration parameter needed to set up the engine or plugin.
69      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
70      */
71     int ClientSetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo);
72 
73     /**
74      * Get the configuration parameters of the engine or plugin.
75      *
76      * @param [in] clientInfo Client information.
77      * @param [in] optionType The type of getting option.
78      * @param [in] inputInfo Parameter information for getting options.
79      * @param [out] outputInfo The configuration parameter information.
80      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
81      */
82     int ClientGetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo,
83         DataInfo &outputInfo);
84 
85     /**
86      * Disconnect the client from the server, release and destroy the client's resources.
87      *
88      * @param [in] clientInfo Client information.
89      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
90      */
91     int ClientDestroy(ClientInfo &clientInfo);
92 
93     /**
94      * Algorithmic inference interface for asynchronous tasks.
95      *
96      * The inference result is notified to the client through callback function passed in by
97      * {@link ClientPrepare(ClientInfo, AlgorithmInfo,DataInfo, DataInfo, IClientCb)}.
98      *
99      * @param [in] clientInfo Client information.
100      * @param [in] algorithmInfo Algorithm information.
101      * @param [in] inputInfo Data information needed to asynchronous execution algorithm.
102      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
103      */
104     int ClientAsyncProcess(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
105         const DataInfo &inputInfo);
106 
107     /**
108      * Algorithmic inference interface for synchronous tasks.
109      *
110      * @param [in] clientInfo Client information.
111      * @param [in] AlgorithmInfo Algorithm information.
112      * @param [in] inputInfo Data information needed to synchronous execution algorithm.
113      * @param [out] outputInfo Algorithm inference results.
114      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
115      */
116     int ClientSyncProcess(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
117         const DataInfo &inputInfo, DataInfo &outputInfo);
118 
119     /**
120      * Unload model and plugin.
121      *
122      * After the algorithm is executed successfully, the method needs to be called when exiting.
123      *
124      * @param [in] clientInfo Client information.
125      * @param [in] algorithmInfo Algorithm information.
126      * @param [in] inputInfo Data information needed to unload the plugin.
127      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
128      */
129     int ClientRelease(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
130         const DataInfo &inputInfo);
131 
132     void SetClientId(int clientId);
133     int GetClientId() const;
134     void SetServerUid(const uid_t clientId);
135     uid_t GetServerUid() const;
136     int GetSessionInfo(int sessionId, int &algorithmType);
137     void ResetClient();
138 
139 private:
140     int GenerateSessionId();
141     bool AddSessionInfo(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo);
142     int EraseSessionInfo(int sessionId);
143     int RegisterCb(int sessionId, IClientCb *cb);
144     int UnRegisterCb(const int sessionId);
145     int RegisterDeadCb(int sessionId, IServiceDeadCb *cb);
146     int UnRegisterDeadCb(const int sessionId);
147     int WaitConnection();
148 
149     virtual int InitAiServer(const ConfigInfo &configInfo, ClientInfo &clientInfo,
150         const AlgorithmInfo &algorithmInfo) = 0;
151     virtual int CloseAiServer() = 0;
152     virtual int LoadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
153         const DataInfo &inputInfo, DataInfo &outputInfo) = 0;
154     virtual int AsyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
155         const DataInfo &inputInfo) = 0;
156     virtual int SyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
157         const DataInfo &inputInfo, DataInfo &outputInfo) = 0;
158     virtual int SetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo) = 0;
159     virtual int GetOption(const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo,
160         DataInfo &outputInfo) = 0;
161     virtual int UnLoadAlgorithm(const ClientInfo &clientInfo, const AlgorithmInfo &algorithmInfo,
162         const DataInfo &inputInfo) = 0;
163 
164 private:
165     static std::mutex sessionIdMutex_;
166 
167     int clientId_ {INVALID_CLIENT_ID};
168     uid_t serverUid_ {INVALID_UID};
169     std::atomic<int> sessionId_ {AIE_SESSION_ID_BEGIN};
170 
171     // map <sessionId, algorithmType>
172     using SessionInfos = std::map<int, int>;
173     SessionInfos sessionInfos_;
174 };
175 
176 ClientFactory *GetClient();
177 } // namespace AI
178 } // namespace OHOS
179 
180 #endif // CLIENT_FACTORY_H
181