• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "communication_adapter/include/sa_client_proxy.h"
17 
18 #include <pthread.h>
19 
20 #include "iproxy_client.h"
21 #include "samgr_lite.h"
22 
23 #include "platform/os_wrapper/ipc/include/aie_ipc.h"
24 #include "protocol/ipc_interface/ai_service.h"
25 #include "protocol/retcode_inner/aie_retcode_inner.h"
26 #include "utils/constants/constants.h"
27 #include "utils/log/aie_log.h"
28 
29 namespace OHOS {
30 namespace AI {
31 namespace {
32 SvcIdentity g_sid;
33 IpcObjectStub g_objStub;
34 
35 struct Notify {
36     int retCode;
37 };
38 
39 typedef struct {
40     int clientId;
41     uid_t serverUid;
42 } NotificationInitServer;
43 
44 struct NotifyBuff {
45     int ipcRetCode;
46     int retCode;
47     int outLen;
48     unsigned char *outBuff;
49 };
50 
Callback(void * owner,int code,IpcIo * reply)51 int Callback(void *owner, int code, IpcIo *reply)
52 {
53     HILOGI("[SaClientProxy]Callback start.");
54     if (owner == nullptr) {
55         HILOGE("[SaClientProxy]Callback owner is nullptr.");
56         return RETCODE_NULL_PARAM;
57     }
58     auto notify = reinterpret_cast<NotificationInitServer *>(owner);
59     ReadInt32(reply, &(notify->clientId));
60     ReadUint32(reply, &(notify->serverUid));
61     return RETCODE_SUCCESS;
62 }
63 
CallbackBuff(void * owner,int code,IpcIo * reply)64 int CallbackBuff(void *owner, int code, IpcIo *reply)
65 {
66     HILOGI("[SaClientProxy]CallbackBuff start.");
67     if (owner == nullptr) {
68         HILOGE("[SaClientProxy]SyncCallback owner is nullptr.");
69         return RETCODE_NULL_PARAM;
70     }
71     auto notify = reinterpret_cast<struct NotifyBuff *>(owner);
72     ReadInt32(reply, &(notify->retCode));
73 
74     DataInfo dataInfo {};
75     notify->ipcRetCode = UnParcelDataInfo(reply, &dataInfo);
76     if (notify->ipcRetCode == RETCODE_SUCCESS) {
77         notify->outLen = dataInfo.length;
78         notify->outBuff = dataInfo.data;
79     } else {
80         notify->outLen = 0;
81         notify->outBuff = nullptr;
82     }
83 
84     return notify->ipcRetCode;
85 }
86 
ParcelClientInfo(IpcIo * request,const ClientInfo & clientInfo)87 void ParcelClientInfo(IpcIo *request, const ClientInfo &clientInfo)
88 {
89     WriteInt64(request, clientInfo.clientVersion);
90     WriteInt32(request, clientInfo.clientId);
91     WriteInt32(request, clientInfo.sessionId);
92     WriteUint32(request, clientInfo.serverUid);
93     WriteUint32(request, clientInfo.clientUid);
94 
95     DataInfo dataInfo {clientInfo.extendMsg, clientInfo.extendLen};
96     ParcelDataInfo(request, &dataInfo, clientInfo.serverUid);
97 }
98 
ParcelAlgorithmInfo(IpcIo * request,const AlgorithmInfo & algorithmInfo,const uid_t serverUid)99 void ParcelAlgorithmInfo(IpcIo *request, const AlgorithmInfo &algorithmInfo, const uid_t serverUid)
100 {
101     WriteInt64(request, algorithmInfo.clientVersion);
102     WriteBool(request, algorithmInfo.isAsync);
103     WriteInt32(request, algorithmInfo.algorithmType);
104     WriteInt64(request, algorithmInfo.algorithmVersion);
105     WriteBool(request, algorithmInfo.isCloud);
106     WriteInt32(request, algorithmInfo.operateId);
107     WriteInt32(request, algorithmInfo.requestId);
108 
109     DataInfo dataInfo {algorithmInfo.extendMsg, algorithmInfo.extendLen};
110     ParcelDataInfo(request, &dataInfo, serverUid);
111 }
112 } // anonymous namespace
113 
HOS_SystemInit(void)114 extern "C" void __attribute__((weak)) HOS_SystemInit(void)
115 {
116 };
117 
HosInit()118 void HosInit()
119 {
120     HOS_SystemInit();
121 }
122 
GetRemoteIUnknown(void)123 IClientProxy *GetRemoteIUnknown(void)
124 {
125     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(AI_SERVICE);
126     if (iUnknown == nullptr) {
127         HILOGE("[SaClientProxy][TID:0x%lx][GetDefaultFeatureApi S:%s]: error is null.",
128             pthread_self(), AI_SERVICE);
129         return nullptr;
130     }
131     IClientProxy *proxy = nullptr;
132     (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&proxy);
133     return proxy;
134 }
135 
InitSaEngine(IClientProxy & proxy,const ConfigInfo & configInfo,ClientInfo & clientInfo)136 int InitSaEngine(IClientProxy &proxy, const ConfigInfo &configInfo, ClientInfo &clientInfo)
137 {
138     HILOGI("[SaClientProxy]Begin to call InitSaEngine.");
139 
140     IpcIo request;
141     char data[MAX_IO_SIZE];
142     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
143     WriteString(&request, configInfo.description);
144 
145     NotificationInitServer owner = {
146         .clientId = INVALID_CLIENT_ID,
147         .serverUid = 0
148     };
149     if (proxy.Invoke == nullptr) {
150         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
151         return RETCODE_NULL_PARAM;
152     }
153     proxy.Invoke(&proxy, ID_INIT_ENGINE, &request, &owner, Callback);
154     clientInfo.clientId = owner.clientId;
155     clientInfo.serverUid = owner.serverUid;
156     return RETCODE_SUCCESS;
157 }
158 
DestroyEngineProxy(IClientProxy & proxy,const ClientInfo & clientInfo)159 int DestroyEngineProxy(IClientProxy &proxy, const ClientInfo &clientInfo)
160 {
161     HILOGI("[SaClientProxy]Begin to call DestroyEngineProxy.");
162 
163     struct Notify owner = {.retCode = RETCODE_FAILURE};
164     IpcIo request;
165     char data[MAX_IO_SIZE];
166     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
167     ParcelClientInfo(&request, clientInfo);
168 
169     if (proxy.Invoke == nullptr) {
170         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
171         return RETCODE_NULL_PARAM;
172     }
173     proxy.Invoke(&proxy, ID_DESTROY_ENGINE, &request, &owner, Callback);
174     if (owner.retCode != RETCODE_SUCCESS) {
175         HILOGE("[SaClientProxy]IPC data processing failed, error code is [%d].", owner.retCode);
176     }
177     return owner.retCode;
178 }
179 
ReleaseIUnknown(IUnknown & proxy)180 void ReleaseIUnknown(IUnknown &proxy)
181 {
182     HILOGI("[SaClientProxy]Begin to call ReleaseIUnknown.");
183     if (proxy.Release == nullptr) {
184         HILOGE("[SaClientProxy]Function pointer proxy.Release is nullptr.");
185         return;
186     }
187     proxy.Release(&proxy);
188 }
189 
SyncExecAlgorithmProxy(IClientProxy & proxy,const ClientInfo & clientInfo,const AlgorithmInfo & algoInfo,const DataInfo & inputInfo,DataInfo & outputInfo)190 int SyncExecAlgorithmProxy(IClientProxy &proxy, const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo,
191     const DataInfo &inputInfo, DataInfo &outputInfo)
192 {
193     HILOGI("[SaClientProxy]Begin to call SyncExecAlgorithmProxy.");
194 
195     IpcIo request;
196     char data[MAX_IO_SIZE];
197     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
198 
199     ParcelClientInfo(&request, clientInfo);
200     ParcelAlgorithmInfo(&request, algoInfo, clientInfo.serverUid);
201     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
202 
203     struct NotifyBuff owner = {
204         .ipcRetCode = RETCODE_SUCCESS,
205         .retCode = RETCODE_FAILURE,
206         .outLen = 0,
207         .outBuff = nullptr,
208     };
209     if (proxy.Invoke == nullptr) {
210         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
211         return RETCODE_NULL_PARAM;
212     }
213     proxy.Invoke(&proxy, ID_SYNC_EXECUTE_ALGORITHM, &request, &owner, CallbackBuff);
214 
215     if (owner.ipcRetCode != RETCODE_SUCCESS) {
216         HILOGE("[SaClientProxy]IPC data processing failed, error code is [%d].", owner.ipcRetCode);
217         return owner.ipcRetCode;
218     }
219     outputInfo.data = owner.outBuff;
220     outputInfo.length = owner.outLen;
221     return owner.retCode;
222 }
223 
AsyncExecuteAlgorithmProxy(IClientProxy & proxy,const ClientInfo & clientInfo,const AlgorithmInfo & algoInfo,const DataInfo & inputInfo)224 int AsyncExecuteAlgorithmProxy(IClientProxy &proxy, const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo,
225     const DataInfo &inputInfo)
226 {
227     HILOGI("[SaClientProxy]Begin to call AsyncExecuteAlgorithmProxy.");
228 
229     IpcIo request;
230     char data[MAX_IO_SIZE];
231     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
232     ParcelClientInfo(&request, clientInfo);
233     ParcelAlgorithmInfo(&request, algoInfo, clientInfo.serverUid);
234     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
235 
236     struct Notify owner = {.retCode = RETCODE_FAILURE};
237     if (proxy.Invoke == nullptr) {
238         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
239         return RETCODE_NULL_PARAM;
240     }
241     proxy.Invoke(&proxy, ID_ASYNC_EXECUTE_ALGORITHM, &request, &owner, Callback);
242     return owner.retCode;
243 }
244 
LoadAlgorithmProxy(IClientProxy & proxy,const ClientInfo & clientInfo,const AlgorithmInfo & algoInfo,const DataInfo & inputInfo,DataInfo & outputInfo)245 int LoadAlgorithmProxy(IClientProxy &proxy, const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo,
246     const DataInfo &inputInfo, DataInfo &outputInfo)
247 {
248     HILOGI("[SaClientProxy]Begin to call LoadAlgorithmProxy.");
249     IpcIo request;
250     char data[MAX_IO_SIZE];
251     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
252 
253     ParcelClientInfo(&request, clientInfo);
254     ParcelAlgorithmInfo(&request, algoInfo, clientInfo.serverUid);
255     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
256     struct NotifyBuff owner = {
257         .ipcRetCode = RETCODE_SUCCESS,
258         .retCode = RETCODE_FAILURE,
259         .outLen = 0,
260         .outBuff = nullptr,
261     };
262     if (proxy.Invoke == nullptr) {
263         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
264         return RETCODE_NULL_PARAM;
265     }
266     proxy.Invoke(&proxy, ID_LOAD_ALGORITHM, &request, &owner, CallbackBuff);
267     if (owner.ipcRetCode != RETCODE_SUCCESS) {
268         HILOGE("[SaClientProxy]IPC data processing failed, error code is [%d].", owner.ipcRetCode);
269         return owner.ipcRetCode;
270     }
271     outputInfo.data = owner.outBuff;
272     outputInfo.length = owner.outLen;
273     return owner.retCode;
274 }
275 
UnloadAlgorithmProxy(IClientProxy & proxy,const ClientInfo & clientInfo,const AlgorithmInfo & algoInfo,const DataInfo & inputInfo)276 int UnloadAlgorithmProxy(IClientProxy &proxy, const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo,
277     const DataInfo &inputInfo)
278 {
279     HILOGI("[SaClientProxy]Begin to call UnloadAlgorithmProxy.");
280 
281     IpcIo request;
282     char data[MAX_IO_SIZE];
283     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
284 
285     ParcelClientInfo(&request, clientInfo);
286     ParcelAlgorithmInfo(&request, algoInfo, clientInfo.serverUid);
287     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
288 
289     struct Notify owner = {.retCode = RETCODE_FAILURE};
290     if (proxy.Invoke == nullptr) {
291         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
292         return RETCODE_NULL_PARAM;
293     }
294     proxy.Invoke(&proxy, ID_UNLOAD_ALGORITHM, &request, &owner, Callback);
295     return owner.retCode;
296 }
297 
SetOptionProxy(IClientProxy & proxy,const ClientInfo & clientInfo,int optionType,const DataInfo & inputInfo)298 int SetOptionProxy(IClientProxy &proxy, const ClientInfo &clientInfo, int optionType, const DataInfo &inputInfo)
299 {
300     HILOGI("[SaClientProxy]Begin to call SetOptionProxy.");
301 
302     IpcIo request;
303     char data[MAX_IO_SIZE];
304     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
305 
306     ParcelClientInfo(&request, clientInfo);
307     WriteInt32(&request, optionType);
308     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
309 
310     struct Notify owner = {.retCode = RETCODE_FAILURE};
311     if (proxy.Invoke == nullptr) {
312         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
313         return RETCODE_NULL_PARAM;
314     }
315     proxy.Invoke(&proxy, ID_SET_OPTION, &request, &owner, Callback);
316     return owner.retCode;
317 }
318 
GetOptionProxy(IClientProxy & proxy,const ClientInfo & clientInfo,int optionType,const DataInfo & inputInfo,DataInfo & outputInfo)319 int GetOptionProxy(IClientProxy &proxy, const ClientInfo &clientInfo, int optionType,
320     const DataInfo &inputInfo, DataInfo &outputInfo)
321 {
322     HILOGI("[SaClientProxy]Begin to call GetOptionProxy.");
323 
324     IpcIo request;
325     char data[MAX_IO_SIZE];
326     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
327 
328     ParcelClientInfo(&request, clientInfo);
329     WriteInt32(&request, optionType);
330     ParcelDataInfo(&request, &inputInfo, clientInfo.serverUid);
331 
332     struct NotifyBuff owner = {
333         .ipcRetCode = RETCODE_SUCCESS,
334         .retCode = RETCODE_FAILURE,
335         .outLen = 0,
336         .outBuff = nullptr,
337     };
338     if (proxy.Invoke == nullptr) {
339         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
340         return RETCODE_NULL_PARAM;
341     }
342     proxy.Invoke(&proxy, ID_GET_OPTION, &request, &owner, CallbackBuff);
343 
344     if (owner.ipcRetCode != RETCODE_SUCCESS) {
345         HILOGE("[SaClientProxy]IPC data processing failed, error code is [%d].", owner.ipcRetCode);
346         return owner.ipcRetCode;
347     }
348     outputInfo.data = owner.outBuff;
349     outputInfo.length = owner.outLen;
350     return owner.retCode;
351 }
352 
RegisterCallbackProxy(IClientProxy & proxy,const ClientInfo & clientInfo,OnRemoteRequest asyncCallback)353 int RegisterCallbackProxy(IClientProxy &proxy, const ClientInfo &clientInfo, OnRemoteRequest asyncCallback)
354 {
355     HILOGI("[SaClientProxy]Begin to call RegisterCallbackProxy.");
356 
357     g_objStub.func = asyncCallback;
358     g_objStub.args = nullptr;
359     g_objStub.isRemote = false;
360 
361     g_sid.handle = IPC_INVALID_HANDLE;
362     g_sid.token = SERVICE_TYPE_ANONYMOUS;
363     g_sid.cookie = (uintptr_t)&g_objStub;
364 
365     struct Notify owner = {.retCode = RETCODE_FAILURE};
366     IpcIo request;
367     char data[MAX_IO_SIZE];
368     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
369     bool writeRemote = WriteRemoteObject(&request, &g_sid);
370     if (!writeRemote) {
371         HILOGE("WriteRemoteObject failed.");
372         return RETCODE_FAILURE;
373     }
374     ParcelClientInfo(&request, clientInfo);
375     if (proxy.Invoke == nullptr) {
376         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
377         return RETCODE_NULL_PARAM;
378     }
379     proxy.Invoke(&proxy, ID_REGISTER_CALLBACK, &request, &owner, Callback);
380     return owner.retCode;
381 }
382 
UnregisterCallbackProxy(IClientProxy & proxy,const ClientInfo & clientInfo)383 int UnregisterCallbackProxy(IClientProxy &proxy, const ClientInfo &clientInfo)
384 {
385     HILOGI("[SaClientProxy]Begin to call UnregisterSaCallbackProxy.");
386 
387     IpcIo request;
388     char data[MAX_IO_SIZE];
389     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_OBJECT_COUNTS);
390 
391     ParcelClientInfo(&request, clientInfo);
392     struct Notify owner = {.retCode = RETCODE_FAILURE};
393     if (proxy.Invoke == nullptr) {
394         HILOGE("[SaClientProxy]Function pointer proxy.Invoke is nullptr.");
395         return RETCODE_NULL_PARAM;
396     }
397     proxy.Invoke(&proxy, ID_UNREGISTER_CALLBACK, &request, &owner, Callback);
398     return owner.retCode;
399 }
400 } // namespace AI
401 } // namespace OHOS
402