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 #include <stddef.h>
17 #include <stdlib.h>
18 
19 #include "iproxy_server.h"
20 #include "ohos_errno.h"
21 #include "ohos_init.h"
22 #include "samgr_lite.h"
23 #include "service.h"
24 
25 #include "communication_adapter/include/adapter_wrapper.h"
26 #include "platform/os_wrapper/ipc/include/aie_ipc.h"
27 #include "protocol/ipc_interface/ai_service.h"
28 #include "protocol/retcode_inner/aie_retcode_inner.h"
29 #include "protocol/struct_definition/aie_info_define.h"
30 #include "utils/constants/constants.h"
31 #include "utils/log/aie_log.h"
32 
33 static const int STACK_SIZE = 0x800;
34 static const int QUEUE_SIZE = 20;
35 
36 typedef struct AiEngineService {
37     INHERIT_SERVICE;
38     INHERIT_IUNKNOWNENTRY(AiInterface);
39     Identity identity;
40 } AiEngineService;
41 
GetName(Service * service)42 static const char *GetName(Service *service)
43 {
44     return AI_SERVICE;
45 }
46 
Initialize(Service * service,Identity identity)47 static BOOL Initialize(Service *service, Identity identity)
48 {
49     if (service == NULL) {
50         return FALSE;
51     }
52     AiEngineService *hiAiService = (AiEngineService *)service;
53     hiAiService->identity = identity;
54     return TRUE;
55 }
56 
MessageHandle(Service * service,Request * msg)57 static BOOL MessageHandle(Service *service, Request *msg)
58 {
59     return TRUE;
60 }
61 
GetTaskConfig(Service * service)62 static TaskConfig GetTaskConfig(Service *service)
63 {
64     TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK};
65     return config;
66 }
67 
UnParcelClientInfo(IpcIo * request,ClientInfo * clientInfo)68 static int UnParcelClientInfo(IpcIo *request, ClientInfo *clientInfo)
69 {
70     if (request == NULL) {
71         HILOGE("[SaServer]The request is NULL.");
72         return RETCODE_FAILURE;
73     }
74     if (clientInfo == NULL) {
75         HILOGE("[SaServer]The clientInfo is NULL.");
76         return RETCODE_FAILURE;
77     }
78     ReadInt64(request, &(clientInfo->clientVersion));
79     ReadInt32(request, &(clientInfo->clientId));
80     ReadInt32(request, &(clientInfo->sessionId));
81     ReadUint32(request, &(clientInfo->serverUid));
82     ReadUint32(request, &(clientInfo->clientUid));
83 
84     DataInfo dataInfo = {NULL, 0};
85     int retCode = UnParcelDataInfo(request, &dataInfo);
86     if (retCode == RETCODE_SUCCESS) {
87         clientInfo->extendLen = dataInfo.length;
88         clientInfo->extendMsg = dataInfo.data;
89     } else {
90         clientInfo->extendLen = 0;
91         clientInfo->extendMsg = NULL;
92     }
93     return retCode;
94 }
95 
UnParcelAlgorithmInfo(IpcIo * request,AlgorithmInfo * algorithmInfo)96 static int UnParcelAlgorithmInfo(IpcIo *request, AlgorithmInfo *algorithmInfo)
97 {
98     if (request == NULL) {
99         HILOGE("[SaServer]The request is NULL.");
100         return RETCODE_FAILURE;
101     }
102     if (algorithmInfo == NULL) {
103         HILOGE("[SaServer]The algorithmInfo is NULL.");
104         return RETCODE_FAILURE;
105     }
106     ReadInt64(request, &(algorithmInfo->clientVersion));
107     ReadBool(request, &(algorithmInfo->isAsync));
108     ReadInt32(request, &(algorithmInfo->algorithmType));
109     ReadInt64(request, &(algorithmInfo->algorithmVersion));
110     ReadBool(request, &(algorithmInfo->isCloud));
111     ReadInt32(request, &(algorithmInfo->operateId));
112     ReadInt32(request, &(algorithmInfo->requestId));
113 
114     DataInfo dataInfo = {NULL, 0};
115     int retCode = UnParcelDataInfo(request, &dataInfo);
116     if (retCode == RETCODE_SUCCESS) {
117         algorithmInfo->extendLen = dataInfo.length;
118         algorithmInfo->extendMsg = dataInfo.data;
119     } else {
120         algorithmInfo->extendLen = 0;
121         algorithmInfo->extendMsg = NULL;
122     }
123     return retCode;
124 }
125 
FreeClientInfo(ClientInfo * clientInfo)126 static void FreeClientInfo(ClientInfo *clientInfo)
127 {
128     if (clientInfo != NULL && clientInfo->extendMsg != NULL) {
129         free(clientInfo->extendMsg);
130         clientInfo->extendMsg = NULL;
131         clientInfo->extendLen = 0;
132     }
133 }
134 
FreeAlgorithmInfo(AlgorithmInfo * algorithmInfo)135 static void FreeAlgorithmInfo(AlgorithmInfo *algorithmInfo)
136 {
137     if (algorithmInfo != NULL && algorithmInfo->extendMsg != NULL) {
138         free(algorithmInfo->extendMsg);
139         algorithmInfo->extendMsg = NULL;
140         algorithmInfo->extendLen = 0;
141     }
142 }
143 
UnParcelInfo(IpcIo * req,ClientInfo * clientInfo,AlgorithmInfo * algorithmInfo,DataInfo * dataInfo)144 static int UnParcelInfo(IpcIo *req, ClientInfo *clientInfo, AlgorithmInfo *algorithmInfo, DataInfo *dataInfo)
145 {
146     int retCode = UnParcelClientInfo(req, clientInfo);
147     if (retCode != RETCODE_SUCCESS) {
148         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
149         return retCode;
150     }
151 
152     retCode = UnParcelAlgorithmInfo(req, algorithmInfo);
153     if (retCode != RETCODE_SUCCESS) {
154         HILOGE("[SaServer]UnParcelAlgorithmInfo failed, retCode[%d].", retCode);
155         FreeClientInfo(clientInfo);
156         return retCode;
157     }
158 
159     retCode = UnParcelDataInfo(req, dataInfo);
160     if (retCode != RETCODE_SUCCESS) {
161         HILOGE("[SaServer]UnParcelDataInfo failed, retCode[%d].", retCode);
162         FreeClientInfo(clientInfo);
163         FreeAlgorithmInfo(algorithmInfo);
164     }
165     return retCode;
166 }
167 
InitEngine(const ConfigInfo * configInfo)168 static int InitEngine(const ConfigInfo *configInfo)
169 {
170     int clientId = GenerateClient();
171     if (clientId <= 0) {
172         HILOGE("[SaServer]Fail to generate client id.");
173         return INVALID_CLIENT_ID;
174     }
175     return clientId;
176 }
177 
SyncExecuteAlgorithm(const ClientInfo * clientInfo,const AlgorithmInfo * algoInfo,const DataInfo * inputInfo,DataInfo * outputInfo)178 static int SyncExecuteAlgorithm(const ClientInfo *clientInfo, const AlgorithmInfo *algoInfo, const DataInfo *inputInfo,
179     DataInfo *outputInfo)
180 {
181     if (clientInfo == NULL || algoInfo == NULL) {
182         HILOGE("[SaServer]Fail to SyncExecuteAlgorithm, because parameter verification failed.");
183         return RETCODE_NULL_PARAM;
184     }
185     int retCode = SyncExecAlgoWrapper(clientInfo, algoInfo, inputInfo, outputInfo);
186     HILOGD("[SaServer][clientId:%d,sessionId:%d]SyncExecAlgoWrapper finished, retCode is [%d]",
187         clientInfo->clientId, clientInfo->sessionId, retCode);
188     return retCode;
189 }
190 
AsyncExecuteAlgorithm(const ClientInfo * clientInfo,const AlgorithmInfo * algoInfo,const DataInfo * inputInfo)191 static int AsyncExecuteAlgorithm(const ClientInfo *clientInfo, const AlgorithmInfo *algoInfo,
192     const DataInfo *inputInfo)
193 {
194     if (clientInfo == NULL || algoInfo == NULL) {
195         HILOGE("[SaServer]Fail to AsyncExecuteAlgorithm, because parameter verification failed.");
196         return RETCODE_NULL_PARAM;
197     }
198 
199     int retCode = AsyncExecAlgoWrapper(clientInfo, algoInfo, inputInfo);
200     HILOGD("[SaServer][clientId:%d,sessionId:%d]AsyncExecAlgoWrapper finished, retCode is [%d]",
201         clientInfo->clientId, clientInfo->sessionId, retCode);
202     return retCode;
203 }
204 
DestroyEngine(const ClientInfo * clientInfo)205 static int DestroyEngine(const ClientInfo *clientInfo)
206 {
207     if (clientInfo == NULL) {
208         HILOGE("[SaServer]Fail to DestroyEngine, because parameter verification failed.");
209         return RETCODE_NULL_PARAM;
210     }
211     int retCode = RemoveAdapterWrapper(clientInfo);
212     HILOGD("[SaServer][clientId:%d]RemoveAdapterWrapper finished, retCode is [%d].", clientInfo->clientId, retCode);
213     return retCode;
214 }
215 
SetOption(const ClientInfo * clientInfo,int optionType,const DataInfo * inputInfo)216 static int SetOption(const ClientInfo *clientInfo, int optionType, const DataInfo *inputInfo)
217 {
218     if (clientInfo == NULL) {
219         HILOGE("[SaServer]Fail to SetOption, because parameter verification failed.");
220         return RETCODE_SA_SERVICE_EXCEPTION;
221     }
222     int retCode = SetOptionWrapper(clientInfo, optionType, inputInfo);
223     HILOGD("[SaServer][clientId:%d]SetOptionWrapper finished, retCode is [%d].", clientInfo->clientId, retCode);
224     return retCode;
225 }
226 
GetOption(const ClientInfo * clientInfo,int optionType,const DataInfo * inputInfo,DataInfo * outputInfo)227 static int GetOption(const ClientInfo *clientInfo, int optionType, const DataInfo *inputInfo, DataInfo *outputInfo)
228 {
229     if (clientInfo == NULL) {
230         HILOGE("[SaServer]Fail to GetOption, because parameter verification failed.");
231         return RETCODE_NULL_PARAM;
232     }
233     int retCode = GetOptionWrapper(clientInfo, optionType, inputInfo, outputInfo);
234     HILOGD("[SaServer][clientId:%d,sessionId:%d]GetOptionWrapper finished, retCode is [%d]",
235         clientInfo->clientId, clientInfo->sessionId, retCode);
236     return retCode;
237 }
238 
InvokeInitSaEngine(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)239 static int InvokeInitSaEngine(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
240 {
241     HILOGI("[SaServer]InvokeInitSaEngine start.");
242     ConfigInfo configInfo;
243     size_t len = 0;
244     configInfo.description = (char*)ReadString(req, &len);
245     int32_t clientId = aiInterface->InitEngine(&configInfo);
246     WriteInt32(reply, clientId);
247     WriteUint32(reply, getuid());
248     return clientId;
249 }
250 
LoadAlgorithm(const ClientInfo * clientInfo,const AlgorithmInfo * algorithmInfo,const DataInfo * inputInfo,DataInfo * outputInfo)251 static int LoadAlgorithm(const ClientInfo *clientInfo, const AlgorithmInfo *algorithmInfo, const DataInfo *inputInfo,
252     DataInfo *outputInfo)
253 {
254     int retCode = LoadAlgoWrapper(clientInfo, algorithmInfo, inputInfo, outputInfo);
255     HILOGD("[SaServer][clientId:%d,sessionId:%d]LoadAlgoWrapper finished, retCode is [%d]",
256         clientInfo->clientId, clientInfo->sessionId, retCode);
257     return retCode;
258 }
259 
InvokeLoadAlgorithm(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)260 static int InvokeLoadAlgorithm(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
261 {
262     HILOGI("[SaServer]InvokeLoadAlgorithm start.");
263     ClientInfo clientInfo = {0};
264     AlgorithmInfo algorithmInfo = {0};
265     DataInfo inputInfo = {0};
266     int retCode = UnParcelInfo(req, &clientInfo, &algorithmInfo, &inputInfo);
267     if (retCode != RETCODE_SUCCESS) {
268         HILOGE("[SaServer]UnParcelInfo failed, retCode[%d].", retCode);
269         return retCode;
270     }
271 
272     DataInfo outputInfo = {
273         .data = NULL,
274         .length = 0,
275     };
276     retCode = aiInterface->LoadAlgorithm(&clientInfo, &algorithmInfo, &inputInfo, &outputInfo);
277     WriteInt32(reply, retCode);
278     ParcelDataInfo(reply, &outputInfo, clientInfo.clientUid);
279 
280     FreeDataInfo(&outputInfo);
281     FreeClientInfo(&clientInfo);
282     FreeAlgorithmInfo(&algorithmInfo);
283     FreeDataInfo(&inputInfo);
284     return retCode;
285 }
286 
InvokeSyncExecute(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)287 static int InvokeSyncExecute(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
288 {
289     HILOGI("[SaServer]InvokeSyncExecute start.");
290     ClientInfo clientInfo = {0};
291     AlgorithmInfo algorithmInfo = {0};
292     DataInfo inputInfo = {0};
293     int retCode = UnParcelInfo(req, &clientInfo, &algorithmInfo, &inputInfo);
294     if (retCode != RETCODE_SUCCESS) {
295         HILOGE("[SaServer]UnParcelInfo failed, retCode[%d].", retCode);
296         return retCode;
297     }
298 
299     DataInfo outputInfo = {
300         .data = NULL,
301         .length = 0,
302     };
303     retCode = aiInterface->SyncExecuteAlgorithm(&clientInfo, &algorithmInfo, &inputInfo, &outputInfo);
304     WriteInt32(reply, retCode);
305     ParcelDataInfo(reply, &outputInfo, clientInfo.clientUid);
306     FreeDataInfo(&outputInfo);
307 
308     // inputInfo is hold by request, and freed when request is destructed in SaServerAdapter::SyncExecute().
309     FreeClientInfo(&clientInfo);
310     FreeAlgorithmInfo(&algorithmInfo);
311     return retCode;
312 }
313 
InvokeAsyncExecute(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)314 static int InvokeAsyncExecute(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
315 {
316     HILOGI("[SaServer]InvokeAsyncExecute start.");
317     ClientInfo clientInfo = {0};
318     AlgorithmInfo algorithmInfo = {0};
319     DataInfo inputInfo = {0};
320     int retCode = UnParcelInfo(req, &clientInfo, &algorithmInfo, &inputInfo);
321     if (retCode != RETCODE_SUCCESS) {
322         HILOGE("[SaServer]UnParcelInfo failed, retCode[%d].", retCode);
323         return retCode;
324     }
325 
326     retCode = aiInterface->AsyncExecuteAlgorithm(&clientInfo, &algorithmInfo, &inputInfo);
327     // inputInfo is hold by request, and freed when request is destructed in SaServerAdapter::AsyncExecute().
328     FreeClientInfo(&clientInfo);
329     FreeAlgorithmInfo(&algorithmInfo);
330     WriteInt32(reply, retCode);
331     return retCode;
332 }
333 
InvokeDestroyEngine(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)334 static int InvokeDestroyEngine(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
335 {
336     HILOGI("[SaServer]InvokeDestroyEngine start.");
337     ClientInfo clientInfo = {0};
338     int retCode = UnParcelClientInfo(req, &clientInfo);
339     if (retCode != RETCODE_SUCCESS) {
340         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
341         return retCode;
342     }
343 
344     retCode = aiInterface->DestroyEngine(&clientInfo);
345     FreeClientInfo(&clientInfo);
346     WriteInt32(reply, retCode);
347     return retCode;
348 }
349 
InvokeSetOption(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)350 static int InvokeSetOption(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
351 {
352     HILOGI("[SaServer]InvokeSetOption start.");
353     ClientInfo clientInfo = {0};
354     int retCode = UnParcelClientInfo(req, &clientInfo);
355     if (retCode != RETCODE_SUCCESS) {
356         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
357         return retCode;
358     }
359 
360     int optionType;
361     ReadInt32(req, &optionType);
362     DataInfo inputInfo = {0};
363     retCode = UnParcelDataInfo(req, &inputInfo);
364     if (retCode != RETCODE_SUCCESS) {
365         HILOGE("[SaServer]UnParcelDataInfo failed, retCode[%d].", retCode);
366         FreeClientInfo(&clientInfo);
367         FreeDataInfo(&inputInfo);
368         return retCode;
369     }
370 
371     retCode = aiInterface->SetOption(&clientInfo, optionType, &inputInfo);
372     FreeClientInfo(&clientInfo);
373     FreeDataInfo(&inputInfo);
374     WriteInt32(reply, retCode);
375     return retCode;
376 }
377 
InvokeGetOption(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)378 static int InvokeGetOption(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
379 {
380     HILOGI("[SaServer]InvokeGetOption start.");
381     ClientInfo clientInfo = {0};
382     int retCode = UnParcelClientInfo(req, &clientInfo);
383     if (retCode != RETCODE_SUCCESS) {
384         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
385         return retCode;
386     }
387 
388     int optionType;
389     ReadInt32(req, &optionType);
390 
391     DataInfo inputInfo = {0};
392     retCode = UnParcelDataInfo(req, &inputInfo);
393     if (retCode != RETCODE_SUCCESS) {
394         HILOGE("[SaServer]UnParcelDataInfo failed, retCode[%d].", retCode);
395         FreeClientInfo(&clientInfo);
396         FreeDataInfo(&inputInfo);
397         return retCode;
398     }
399 
400     DataInfo outputInfo = {
401         .data = NULL,
402         .length = 0,
403     };
404     retCode = aiInterface->GetOption(&clientInfo, optionType, &inputInfo, &outputInfo);
405     WriteInt32(reply, retCode);
406     ParcelDataInfo(reply, &outputInfo, clientInfo.clientUid);
407 
408     FreeDataInfo(&outputInfo);
409     FreeClientInfo(&clientInfo);
410     FreeDataInfo(&inputInfo);
411     return retCode;
412 }
413 
InvokeRegisterCallback(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)414 static int InvokeRegisterCallback(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
415 {
416     HILOGI("[SaServer]InvokeRegisterCallback start.");
417     SvcIdentity sid;
418     bool ret = ReadRemoteObject(req, &sid);
419     if (!ret) {
420         HILOGE("[SaServer]ReadRemoteObject failed.");
421         return RETCODE_NULL_PARAM;
422     }
423     ClientInfo clientInfo = {0};
424     int retCode = UnParcelClientInfo(req, &clientInfo);
425     if (retCode != RETCODE_SUCCESS) {
426         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
427         return retCode;
428     }
429     retCode = RegisterCallbackWrapper(&clientInfo, &sid);
430     HILOGD("[SaServer][clientId:%d]RegisterCallbackWrapper finished, retCode is [%d].", clientInfo.clientId, retCode);
431 
432     FreeClientInfo(&clientInfo);
433     WriteInt32(reply, retCode);
434     return retCode;
435 }
436 
InvokeUnregisterCallback(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)437 static int InvokeUnregisterCallback(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
438 {
439     HILOGI("[SaServer]InvokeUnRegisterClientCallback start.");
440     ClientInfo clientInfo = {0};
441     int retCode = UnParcelClientInfo(req, &clientInfo);
442     if (retCode != RETCODE_SUCCESS) {
443         HILOGE("[SaServer]UnParcelClientInfo failed, retCode[%d].", retCode);
444         return retCode;
445     }
446     retCode = aiInterface->UnregisterCallback(&clientInfo);
447     FreeClientInfo(&clientInfo);
448     WriteInt32(reply, retCode);
449     return retCode;
450 }
451 
UnregisterCallback(const ClientInfo * clientInfo)452 static int UnregisterCallback(const ClientInfo* clientInfo)
453 {
454     if (clientInfo == NULL) {
455         HILOGE("[SaServer]Fail to SyncExecuteAlgorithm, because parameter verification failed.");
456         return RETCODE_NULL_PARAM;
457     }
458     int retCode = UnregisterCallbackWrapper(clientInfo);
459     HILOGD("[SaServer][clientId:%d]UnregisterCallbackWrapper finished, retCode is [%d].",
460         clientInfo->clientId, retCode);
461     return retCode;
462 }
463 
InvokeUnloadAlgorithm(AiInterface * aiInterface,IpcIo * req,IpcIo * reply)464 static int InvokeUnloadAlgorithm(AiInterface *aiInterface, IpcIo *req, IpcIo *reply)
465 {
466     HILOGI("[SaServer]InvokeUnloadAlgorithm start.");
467     ClientInfo clientInfo = {0};
468     AlgorithmInfo algorithmInfo = {0};
469     DataInfo inputInfo = {0};
470     int retCode = UnParcelInfo(req, &clientInfo, &algorithmInfo, &inputInfo);
471     if (retCode != RETCODE_SUCCESS) {
472         return retCode;
473     }
474 
475     retCode = aiInterface->UnloadAlgorithm(&clientInfo, &algorithmInfo, &inputInfo);
476     FreeClientInfo(&clientInfo);
477     FreeAlgorithmInfo(&algorithmInfo);
478     FreeDataInfo(&inputInfo);
479     WriteInt32(reply, retCode);
480     return retCode;
481 }
482 
UnloadAlgorithm(const ClientInfo * clientInfo,const AlgorithmInfo * algoInfo,const DataInfo * inputInfo)483 static int UnloadAlgorithm(const ClientInfo *clientInfo, const AlgorithmInfo *algoInfo, const DataInfo *inputInfo)
484 {
485     if (clientInfo == NULL || algoInfo == NULL) {
486         HILOGE("[SaServer]Fail to UnloadAlgorithm, because parameter verification failed.");
487         return RETCODE_NULL_PARAM;
488     }
489 
490     int retCode = UnloadAlgoWrapper(clientInfo, algoInfo, inputInfo);
491     HILOGD("[SaServer][clientId:%d,sessionId:%d]UnloadAlgoWrapper finished, retCode is [%d].",
492         clientInfo->clientId, clientInfo->sessionId, retCode);
493     return retCode;
494 }
495 
Invoke(IServerProxy * proxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)496 static int Invoke(IServerProxy *proxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
497 {
498     HILOGI("[SaServer]Begin to call Invoke, funcId is [%d].", funcId);
499     AiInterface *aiInterface = (AiInterface *)proxy;
500     switch (funcId) {
501         case ID_INIT_ENGINE: {
502             InvokeInitSaEngine(aiInterface, req, reply);
503             break;
504         }
505         case ID_LOAD_ALGORITHM: {
506             InvokeLoadAlgorithm(aiInterface, req, reply);
507             break;
508         }
509         case ID_SYNC_EXECUTE_ALGORITHM: {
510             InvokeSyncExecute(aiInterface, req, reply);
511             break;
512         }
513         case ID_DESTROY_ENGINE: {
514             InvokeDestroyEngine(aiInterface, req, reply);
515             break;
516         }
517         case ID_SET_OPTION: {
518             InvokeSetOption(aiInterface, req, reply);
519             break;
520         }
521         case ID_GET_OPTION: {
522             InvokeGetOption(aiInterface, req, reply);
523             break;
524         }
525         case ID_REGISTER_CALLBACK: {
526             InvokeRegisterCallback(aiInterface, req, reply);
527             break;
528         }
529         case ID_UNREGISTER_CALLBACK: {
530             InvokeUnregisterCallback(aiInterface, req, reply);
531             break;
532         }
533         case ID_ASYNC_EXECUTE_ALGORITHM: {
534             InvokeAsyncExecute(aiInterface, req, reply);
535             break;
536         }
537         case ID_UNLOAD_ALGORITHM: {
538             InvokeUnloadAlgorithm(aiInterface, req, reply);
539             break;
540         }
541         default:{
542             break;
543         }
544     }
545     return EC_SUCCESS;
546 }
547 
548 static AiEngineService g_aiEngine = {
549     .GetName = GetName,
550     .Initialize = Initialize,
551     .MessageHandle = MessageHandle,
552     .GetTaskConfig = GetTaskConfig,
553     SERVER_IPROXY_IMPL_BEGIN,
554     .Invoke = Invoke,
555     .InitEngine = InitEngine,
556     .SyncExecuteAlgorithm = SyncExecuteAlgorithm,
557     .AsyncExecuteAlgorithm = AsyncExecuteAlgorithm,
558     .UnloadAlgorithm = UnloadAlgorithm,
559     .DestroyEngine = DestroyEngine,
560     .SetOption = SetOption,
561     .GetOption = GetOption,
562     .UnregisterCallback = UnregisterCallback,
563     .LoadAlgorithm = LoadAlgorithm,
564     IPROXY_END,
565 };
566 
Init(void)567 static void Init(void)
568 {
569     HILOGI("[SaServer]Init start.");
570     SAMGR_GetInstance()->RegisterService((Service *)&g_aiEngine);
571     SAMGR_GetInstance()->RegisterDefaultFeatureApi(AI_SERVICE, GET_IUNKNOWN(g_aiEngine));
572 }
573 SYSEX_SERVICE_INIT(Init);
574