• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 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 "ipc_sdk.h"
17 
18 #include "common_defs.h"
19 #include "device_auth_defines.h"
20 #include "identity_service_ipc_sdk.h"
21 #include "ipc_sdk_defines.h"
22 #include "device_auth.h"
23 #include "hc_log.h"
24 #include "hc_mutex.h"
25 #include "hc_types.h"
26 
27 #include "ipc_adapt.h"
28 #include "securec.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define IPC_DATA_CACHES_1 1
35 #define IPC_DATA_CACHES_3 3
36 #define IPC_DATA_CACHES_4 4
37 #define IPC_DATA_CACHES_6 6
38 #define REPLAY_CACHE_NUM(caches) (sizeof(caches) / sizeof(IpcDataInfo))
39 #define IPC_APPID_LEN 128
40 
41 #define IS_COMM_DATA_VALID(dPtr, dLen) (((dPtr) != NULL) && ((dLen) > 0) && ((dLen) <= 4096))
42 
43 static const int32_t IPC_RESULT_NUM_1 = 1;
44 static const int32_t IPC_RESULT_NUM_2 = 2;
45 static const int32_t IPC_RESULT_NUM_4 = 4;
46 
47 typedef struct {
48     uintptr_t inst;
49     char appId[IPC_APPID_LEN];
50 } IpcProxyCbInfo;
51 static IpcProxyCbInfo g_ipcProxyCbList = { 0 };
52 static IpcProxyCbInfo g_ipcListenerCbList = { 0 };
53 static HcMutex g_ipcMutex;
54 
IsStrInvalid(const char * str)55 static bool IsStrInvalid(const char *str)
56 {
57     return (str == NULL || str[0] == 0);
58 }
59 
DelIpcCliCallbackCtx(const char * appId,IpcProxyCbInfo * cbCache)60 static void DelIpcCliCallbackCtx(const char *appId, IpcProxyCbInfo *cbCache)
61 {
62     int32_t ret;
63 
64     if (cbCache->appId[0] == 0) {
65         return;
66     }
67     (void)LockHcMutex(&g_ipcMutex);
68     ret = memcmp(appId, cbCache->appId, HcStrlen(cbCache->appId) + 1);
69     if (ret == 0) {
70         cbCache->appId[0] = 0;
71     }
72     UnlockHcMutex(&g_ipcMutex);
73     return;
74 }
75 
AddIpcCliCallbackCtx(const char * appId,uintptr_t cbInst,IpcProxyCbInfo * cbCache)76 static void AddIpcCliCallbackCtx(const char *appId, uintptr_t cbInst, IpcProxyCbInfo *cbCache)
77 {
78     errno_t eno;
79 
80     (void)LockHcMutex(&g_ipcMutex);
81     eno = memcpy_s(cbCache->appId, IPC_APPID_LEN, appId, HcStrlen(appId) + 1);
82     if (eno != EOK) {
83         UnlockHcMutex(&g_ipcMutex);
84         LOGE("memory copy failed");
85         return;
86     }
87     cbCache->inst = cbInst;
88     UnlockHcMutex(&g_ipcMutex);
89 }
90 
GetIpcReplyByType(const IpcDataInfo * ipcData,int32_t dataNum,int32_t type,uint8_t * outCache,int32_t * cacheLen)91 static void GetIpcReplyByType(const IpcDataInfo *ipcData,
92     int32_t dataNum, int32_t type, uint8_t *outCache, int32_t *cacheLen)
93 {
94     int32_t i;
95     errno_t eno;
96 
97     for (i = 0; i < dataNum; i++) {
98         if (ipcData[i].type != type) {
99             continue;
100         }
101         switch (type) {
102             case PARAM_TYPE_REG_INFO:
103             case PARAM_TYPE_DEVICE_INFO:
104             case PARAM_TYPE_GROUP_INFO:
105             case PARAM_TYPE_CRED_ID:
106             case PARAM_TYPE_CRED_INFO:
107             case PARAM_TYPE_CRED_INFO_LIST:
108             case PARAM_TYPE_CRED_VAL:
109             case PARAM_TYPE_RETURN_DATA:
110             case PARAM_TYPE_SHARED_KEY_VAL:
111             case PARAM_TYPE_RANDOM_VAL:
112                 *(uint8_t **)outCache = ipcData[i].val;
113                 break;
114             case PARAM_TYPE_IPC_RESULT:
115             case PARAM_TYPE_IPC_RESULT_NUM:
116             case PARAM_TYPE_COMM_DATA:
117             case PARAM_TYPE_DATA_NUM:
118             case PARAM_TYPE_SHARED_KEY_LEN:
119             case PARAM_TYPE_RANDOM_LEN:
120                 eno = memcpy_s(outCache, *cacheLen, ipcData[i].val, ipcData[i].valSz);
121                 if (eno != EOK) {
122                     break;
123                 }
124                 *cacheLen = ipcData[i].valSz;
125                 break;
126             default:
127                 LOGE("un-expectation type case");
128                 break;
129         }
130     }
131     return;
132 }
133 
IpcGmRegCallback(const char * appId,const DeviceAuthCallback * callback)134 static int32_t IpcGmRegCallback(const char *appId, const DeviceAuthCallback *callback)
135 {
136     uintptr_t callCtx = 0x0;
137     int32_t ret;
138 
139     LOGI("starting ...");
140     if (IsStrInvalid(appId) || callback == NULL) {
141         LOGE("invalid params");
142         return HC_ERR_INVALID_PARAMS;
143     }
144     ret = CreateCallCtx(&callCtx, NULL);
145     if (ret != HC_SUCCESS) {
146         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
147         return HC_ERR_IPC_INIT;
148     }
149 
150     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
151     if (ret != HC_SUCCESS) {
152         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
153         DestroyCallCtx(&callCtx, NULL);
154         return HC_ERR_IPC_BUILD_PARAM;
155     }
156     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
157     if (ret != HC_SUCCESS) {
158         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
159         DestroyCallCtx(&callCtx, NULL);
160         return HC_ERR_IPC_BUILD_PARAM;
161     }
162     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_BIND_ID);
163     ret = DoBinderCall(callCtx, IPC_CALL_ID_REG_CB, true);
164     if (ret == HC_SUCCESS) {
165         AddIpcCliCallbackCtx(appId, 0, &g_ipcProxyCbList);
166     }
167     DestroyCallCtx(&callCtx, NULL);
168     LOGI("process done, ret %" LOG_PUB "d", ret);
169     return (ret == HC_SUCCESS) ? HC_SUCCESS : HC_ERR_IPC_PROC_FAILED;
170 }
171 
IpcGmUnRegCallback(const char * appId)172 static int32_t IpcGmUnRegCallback(const char *appId)
173 {
174     uintptr_t callCtx = 0x0;
175     int32_t ret;
176 
177     LOGI("starting ...");
178     if (IsStrInvalid(appId)) {
179         LOGE("invalid params");
180         return HC_ERR_INVALID_PARAMS;
181     }
182     ret = CreateCallCtx(&callCtx, NULL);
183     if (ret != HC_SUCCESS) {
184         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
185         return HC_ERR_IPC_INIT;
186     }
187 
188     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
189     if (ret != HC_SUCCESS) {
190         LOGE("set request param failed, ret %" LOG_PUB "d", ret);
191         DestroyCallCtx(&callCtx, NULL);
192         return HC_ERR_IPC_BUILD_PARAM;
193     }
194     ret = DoBinderCall(callCtx, IPC_CALL_ID_UNREG_CB, true);
195     if (ret == HC_SUCCESS) {
196         DelIpcCliCallbackCtx(appId, &g_ipcProxyCbList);
197     }
198     DestroyCallCtx(&callCtx, NULL);
199     LOGI("process done, ret %" LOG_PUB "d", HC_SUCCESS);
200     return HC_SUCCESS;
201 }
202 
IpcGmRegDataChangeListener(const char * appId,const DataChangeListener * listener)203 static int32_t IpcGmRegDataChangeListener(const char *appId, const DataChangeListener *listener)
204 {
205     uintptr_t callCtx = 0x0;
206     int32_t ret;
207 
208     LOGI("starting ...");
209     if (IsStrInvalid(appId) || (listener == NULL)) {
210         LOGE("invalid params");
211         return HC_ERR_INVALID_PARAMS;
212     }
213     ret = CreateCallCtx(&callCtx, NULL);
214     if (ret != HC_SUCCESS) {
215         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
216         return HC_ERR_IPC_INIT;
217     }
218 
219     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
220     if (ret != HC_SUCCESS) {
221         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
222         DestroyCallCtx(&callCtx, NULL);
223         return HC_ERR_IPC_BUILD_PARAM;
224     }
225     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_LISTERNER, (const uint8_t *)listener, sizeof(*listener));
226     if (ret != HC_SUCCESS) {
227         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_LISTERNER);
228         DestroyCallCtx(&callCtx, NULL);
229         return HC_ERR_IPC_BUILD_PARAM;
230     }
231     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_BIND_ID);
232     ret = DoBinderCall(callCtx, IPC_CALL_ID_REG_LISTENER, true);
233     if (ret == HC_SUCCESS) {
234         AddIpcCliCallbackCtx(appId, 0, &g_ipcListenerCbList);
235     }
236     DestroyCallCtx(&callCtx, NULL);
237     LOGI("process done, ret %" LOG_PUB "d", ret);
238     return (ret == HC_SUCCESS) ? HC_SUCCESS : HC_ERR_IPC_PROC_FAILED;
239 }
240 
IpcGmUnRegDataChangeListener(const char * appId)241 static int32_t IpcGmUnRegDataChangeListener(const char *appId)
242 {
243     uintptr_t callCtx = 0x0;
244     int32_t ret;
245 
246     LOGI("starting ...");
247     if (IsStrInvalid(appId)) {
248         LOGE("invalid params");
249         return HC_ERR_INVALID_PARAMS;
250     }
251     ret = CreateCallCtx(&callCtx, NULL);
252     if (ret != HC_SUCCESS) {
253         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
254         return HC_ERR_IPC_INIT;
255     }
256 
257     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
258     if (ret != HC_SUCCESS) {
259         LOGE("set request param failed, ret %" LOG_PUB "d", ret);
260         DestroyCallCtx(&callCtx, NULL);
261         return HC_ERR_IPC_BUILD_PARAM;
262     }
263     ret = DoBinderCall(callCtx, IPC_CALL_ID_UNREG_LISTENER, true);
264     if (ret == HC_SUCCESS) {
265         DelIpcCliCallbackCtx(appId, &g_ipcListenerCbList);
266     }
267     DestroyCallCtx(&callCtx, NULL);
268     LOGI("process done");
269     return HC_SUCCESS;
270 }
271 
EncodeCreateGroupParams(uintptr_t callCtx,int32_t osAccountId,int64_t requestId,const char * appId,const char * createParams)272 static int32_t EncodeCreateGroupParams(uintptr_t callCtx, int32_t osAccountId, int64_t requestId,
273     const char *appId, const char *createParams)
274 {
275     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
276         sizeof(osAccountId));
277     if (ret != HC_SUCCESS) {
278         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
279         return HC_ERR_IPC_BUILD_PARAM;
280     }
281     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
282     if (ret != HC_SUCCESS) {
283         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
284         return HC_ERR_IPC_BUILD_PARAM;
285     }
286     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
287     if (ret != HC_SUCCESS) {
288         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
289         return HC_ERR_IPC_BUILD_PARAM;
290     }
291     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CREATE_PARAMS,
292                                   (const uint8_t *)createParams, HcStrlen(createParams) + 1);
293     if (ret != HC_SUCCESS) {
294         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_CREATE_PARAMS);
295         return HC_ERR_IPC_BUILD_PARAM;
296     }
297     return HC_SUCCESS;
298 }
299 
IpcGmCreateGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * createParams)300 static int32_t IpcGmCreateGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *createParams)
301 {
302     uintptr_t callCtx = 0x0;
303     int32_t ret;
304     int32_t inOutLen;
305     IpcDataInfo replyCache = { 0 };
306 
307     LOGI("starting ...");
308     if (IsStrInvalid(createParams) || IsStrInvalid(appId)) {
309         LOGE("invalid params");
310         return HC_ERR_INVALID_PARAMS;
311     }
312     ret = CreateCallCtx(&callCtx, NULL);
313     if (ret != HC_SUCCESS) {
314         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
315         return HC_ERR_IPC_INIT;
316     }
317     ret = EncodeCreateGroupParams(callCtx, osAccountId, requestId, appId, createParams);
318     if (ret != HC_SUCCESS) {
319         DestroyCallCtx(&callCtx, NULL);
320         return ret;
321     }
322     ret = DoBinderCall(callCtx, IPC_CALL_ID_CREATE_GROUP, true);
323     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
324         LOGE("ipc call failed");
325         DestroyCallCtx(&callCtx, NULL);
326         return HC_ERR_IPC_PROC_FAILED;
327     }
328     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
329     ret = HC_ERR_IPC_UNKNOW_REPLY;
330     inOutLen = sizeof(int32_t);
331     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
332     DestroyCallCtx(&callCtx, NULL);
333     LOGI("process done, ret %" LOG_PUB "d", ret);
334     return ret;
335 }
336 
EncodeDeleteGroupParams(uintptr_t callCtx,int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)337 static int32_t EncodeDeleteGroupParams(uintptr_t callCtx, int32_t osAccountId, int64_t requestId,
338     const char *appId, const char *delParams)
339 {
340     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
341         sizeof(osAccountId));
342     if (ret != HC_SUCCESS) {
343         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
344         return HC_ERR_IPC_BUILD_PARAM;
345     }
346     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
347     if (ret != HC_SUCCESS) {
348         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
349         return HC_ERR_IPC_BUILD_PARAM;
350     }
351     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEL_PARAMS, (const uint8_t *)delParams, HcStrlen(delParams) + 1);
352     if (ret != HC_SUCCESS) {
353         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_DEL_PARAMS);
354         return HC_ERR_IPC_BUILD_PARAM;
355     }
356     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
357     if (ret != HC_SUCCESS) {
358         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
359         return HC_ERR_IPC_BUILD_PARAM;
360     }
361     return HC_SUCCESS;
362 }
363 
IpcGmDeleteGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)364 static int32_t IpcGmDeleteGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *delParams)
365 {
366     uintptr_t callCtx = 0x0;
367     int32_t ret;
368     int32_t inOutLen;
369     IpcDataInfo replyCache = { 0 };
370 
371     LOGI("starting ...");
372     if (IsStrInvalid(delParams) || IsStrInvalid(appId)) {
373         LOGE("invalid params");
374         return HC_ERR_INVALID_PARAMS;
375     }
376     ret = CreateCallCtx(&callCtx, NULL);
377     if (ret != HC_SUCCESS) {
378         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
379         return HC_ERR_IPC_INIT;
380     }
381     ret = EncodeDeleteGroupParams(callCtx, osAccountId, requestId, appId, delParams);
382     if (ret != HC_SUCCESS) {
383         DestroyCallCtx(&callCtx, NULL);
384         return ret;
385     }
386     ret = DoBinderCall(callCtx, IPC_CALL_ID_DEL_GROUP, true);
387     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
388         LOGE("ipc call failed");
389         DestroyCallCtx(&callCtx, NULL);
390         return HC_ERR_IPC_PROC_FAILED;
391     }
392     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
393     ret = HC_ERR_IPC_UNKNOW_REPLY;
394     inOutLen = sizeof(int32_t);
395     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
396     DestroyCallCtx(&callCtx, NULL);
397     LOGI("process done, ret %" LOG_PUB "d", ret);
398     return ret;
399 }
400 
EncodeAddMemberParams(uintptr_t callCtx,int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)401 static int32_t EncodeAddMemberParams(uintptr_t callCtx, int32_t osAccountId, int64_t requestId,
402     const char *appId, const char *addParams)
403 {
404     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
405         sizeof(osAccountId));
406     if (ret != HC_SUCCESS) {
407         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
408         return HC_ERR_IPC_BUILD_PARAM;
409     }
410     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
411     if (ret != HC_SUCCESS) {
412         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
413         return HC_ERR_IPC_BUILD_PARAM;
414     }
415     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
416     if (ret != HC_SUCCESS) {
417         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
418         return HC_ERR_IPC_BUILD_PARAM;
419     }
420     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_ADD_PARAMS, (const uint8_t *)addParams, HcStrlen(addParams) + 1);
421     if (ret != HC_SUCCESS) {
422         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_ADD_PARAMS);
423         return HC_ERR_IPC_BUILD_PARAM;
424     }
425     return HC_SUCCESS;
426 }
427 
IpcGmAddMemberToGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)428 static int32_t IpcGmAddMemberToGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams)
429 {
430     uintptr_t callCtx = 0x0;
431     int32_t ret;
432     int32_t inOutLen;
433     IpcDataInfo replyCache = { 0 };
434 
435     LOGI("starting ...");
436     if (IsStrInvalid(appId) || IsStrInvalid(addParams)) {
437         LOGE("invalid params");
438         return HC_ERR_INVALID_PARAMS;
439     }
440     ret = CreateCallCtx(&callCtx, NULL);
441     if (ret != HC_SUCCESS) {
442         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
443         return HC_ERR_IPC_INIT;
444     }
445     ret = EncodeAddMemberParams(callCtx, osAccountId, requestId, appId, addParams);
446     if (ret != HC_SUCCESS) {
447         DestroyCallCtx(&callCtx, NULL);
448         return ret;
449     }
450     ret = DoBinderCall(callCtx, IPC_CALL_ID_ADD_GROUP_MEMBER, true);
451     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
452         LOGE("ipc call failed");
453         DestroyCallCtx(&callCtx, NULL);
454         return HC_ERR_IPC_PROC_FAILED;
455     }
456     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
457     ret = HC_ERR_IPC_UNKNOW_REPLY;
458     inOutLen = sizeof(int32_t);
459     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
460     DestroyCallCtx(&callCtx, NULL);
461     LOGI("process done, ret %" LOG_PUB "d", ret);
462     return ret;
463 }
464 
EncodeDeleteMemberParams(uintptr_t callCtx,int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)465 static int32_t EncodeDeleteMemberParams(uintptr_t callCtx, int32_t osAccountId, int64_t requestId,
466     const char *appId, const char *delParams)
467 {
468     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
469         sizeof(osAccountId));
470     if (ret != HC_SUCCESS) {
471         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
472         return HC_ERR_IPC_BUILD_PARAM;
473     }
474     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
475     if (ret != HC_SUCCESS) {
476         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
477         return HC_ERR_IPC_BUILD_PARAM;
478     }
479     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
480     if (ret != HC_SUCCESS) {
481         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
482         return HC_ERR_IPC_BUILD_PARAM;
483     }
484     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEL_PARAMS, (const uint8_t *)delParams, HcStrlen(delParams) + 1);
485     if (ret != HC_SUCCESS) {
486         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_DEL_PARAMS);
487         return HC_ERR_IPC_BUILD_PARAM;
488     }
489     return HC_SUCCESS;
490 }
491 
IpcGmDelMemberFromGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)492 static int32_t IpcGmDelMemberFromGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *delParams)
493 {
494     uintptr_t callCtx = 0x0;
495     int32_t ret;
496     int32_t inOutLen;
497     IpcDataInfo replyCache = { 0 };
498 
499     LOGI("starting ...");
500     if (IsStrInvalid(appId) || IsStrInvalid(delParams)) {
501         LOGE("invalid params");
502         return HC_ERR_INVALID_PARAMS;
503     }
504     ret = CreateCallCtx(&callCtx, NULL);
505     if (ret != HC_SUCCESS) {
506         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
507         return HC_ERR_IPC_INIT;
508     }
509     ret = EncodeDeleteMemberParams(callCtx, osAccountId, requestId, appId, delParams);
510     if (ret != HC_SUCCESS) {
511         DestroyCallCtx(&callCtx, NULL);
512         return ret;
513     }
514     ret = DoBinderCall(callCtx, IPC_CALL_ID_DEL_GROUP_MEMBER, true);
515     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
516         LOGE("ipc call failed");
517         DestroyCallCtx(&callCtx, NULL);
518         return HC_ERR_IPC_PROC_FAILED;
519     }
520     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
521     ret = HC_ERR_IPC_UNKNOW_REPLY;
522     inOutLen = sizeof(int32_t);
523     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
524     DestroyCallCtx(&callCtx, NULL);
525     LOGI("process done, ret %" LOG_PUB "d", ret);
526     return ret;
527 }
528 
IpcGmAddMultiMembersToGroup(int32_t osAccountId,const char * appId,const char * addParams)529 static int32_t IpcGmAddMultiMembersToGroup(int32_t osAccountId, const char *appId, const char *addParams)
530 {
531     LOGI("starting ...");
532     if (IsStrInvalid(appId) || IsStrInvalid(addParams)) {
533         LOGE("Invalid params");
534         return HC_ERR_INVALID_PARAMS;
535     }
536     uintptr_t callCtx = 0x0;
537     int32_t ret = CreateCallCtx(&callCtx, NULL);
538     if (ret != HC_SUCCESS) {
539         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
540         return HC_ERR_IPC_INIT;
541     }
542     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
543         sizeof(osAccountId));
544     if (ret != HC_SUCCESS) {
545         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
546         DestroyCallCtx(&callCtx, NULL);
547         return HC_ERR_IPC_BUILD_PARAM;
548     }
549     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
550     if (ret != HC_SUCCESS) {
551         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
552         DestroyCallCtx(&callCtx, NULL);
553         return HC_ERR_IPC_BUILD_PARAM;
554     }
555     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_ADD_PARAMS, (const uint8_t *)addParams, HcStrlen(addParams) + 1);
556     if (ret != HC_SUCCESS) {
557         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_ADD_PARAMS);
558         DestroyCallCtx(&callCtx, NULL);
559         return HC_ERR_IPC_BUILD_PARAM;
560     }
561     ret = DoBinderCall(callCtx, IPC_CALL_ID_ADD_MULTI_GROUP_MEMBERS, true);
562     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
563         LOGE("ipc call failed");
564         DestroyCallCtx(&callCtx, NULL);
565         return HC_ERR_IPC_PROC_FAILED;
566     }
567     IpcDataInfo replyCache = { 0 };
568     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
569     ret = HC_ERR_IPC_UNKNOW_REPLY;
570     int32_t inOutLen = sizeof(int32_t);
571     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
572     DestroyCallCtx(&callCtx, NULL);
573     LOGI("process done, ret %" LOG_PUB "d", ret);
574     return ret;
575 }
576 
IpcGmDelMultiMembersFromGroup(int32_t osAccountId,const char * appId,const char * delParams)577 static int32_t IpcGmDelMultiMembersFromGroup(int32_t osAccountId, const char *appId, const char *delParams)
578 {
579     LOGI("starting ...");
580     if (IsStrInvalid(appId) || IsStrInvalid(delParams)) {
581         LOGE("Invalid params");
582         return HC_ERR_INVALID_PARAMS;
583     }
584     uintptr_t callCtx = 0x0;
585     int32_t ret = CreateCallCtx(&callCtx, NULL);
586     if (ret != HC_SUCCESS) {
587         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
588         return HC_ERR_IPC_INIT;
589     }
590     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
591         sizeof(osAccountId));
592     if (ret != HC_SUCCESS) {
593         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
594         DestroyCallCtx(&callCtx, NULL);
595         return HC_ERR_IPC_BUILD_PARAM;
596     }
597     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
598     if (ret != HC_SUCCESS) {
599         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
600         DestroyCallCtx(&callCtx, NULL);
601         return HC_ERR_IPC_BUILD_PARAM;
602     }
603     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEL_PARAMS, (const uint8_t *)delParams, HcStrlen(delParams) + 1);
604     if (ret != HC_SUCCESS) {
605         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_DEL_PARAMS);
606         DestroyCallCtx(&callCtx, NULL);
607         return HC_ERR_IPC_BUILD_PARAM;
608     }
609     ret = DoBinderCall(callCtx, IPC_CALL_ID_DEL_MULTI_GROUP_MEMBERS, true);
610     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
611         LOGE("ipc call failed");
612         DestroyCallCtx(&callCtx, NULL);
613         return HC_ERR_IPC_PROC_FAILED;
614     }
615     IpcDataInfo replyCache = { 0 };
616     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
617     ret = HC_ERR_IPC_UNKNOW_REPLY;
618     int32_t inOutLen = sizeof(int32_t);
619     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
620     DestroyCallCtx(&callCtx, NULL);
621     LOGI("process done, ret %" LOG_PUB "d", ret);
622     return ret;
623 }
624 
IpcGmProcessData(int64_t requestId,const uint8_t * data,uint32_t dataLen)625 static int32_t IpcGmProcessData(int64_t requestId, const uint8_t *data, uint32_t dataLen)
626 {
627     uintptr_t callCtx = 0x0;
628     int32_t ret;
629     int32_t inOutLen;
630     IpcDataInfo replyCache = { 0 };
631 
632     LOGI("starting ...");
633     if (!IS_COMM_DATA_VALID(data, dataLen)) {
634         LOGE("invalid params");
635         return HC_ERR_INVALID_PARAMS;
636     }
637     ret = CreateCallCtx(&callCtx, NULL);
638     if (ret != HC_SUCCESS) {
639         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
640         return HC_ERR_IPC_INIT;
641     }
642     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
643     if (ret != HC_SUCCESS) {
644         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
645         DestroyCallCtx(&callCtx, NULL);
646         return HC_ERR_IPC_BUILD_PARAM;
647     }
648     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_COMM_DATA, data, dataLen);
649     if (ret != HC_SUCCESS) {
650         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_COMM_DATA);
651         DestroyCallCtx(&callCtx, NULL);
652         return HC_ERR_IPC_BUILD_PARAM;
653     }
654     ret = DoBinderCall(callCtx, IPC_CALL_ID_GM_PROC_DATA, true);
655     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
656         LOGE("ipc call failed");
657         DestroyCallCtx(&callCtx, NULL);
658         return HC_ERR_IPC_PROC_FAILED;
659     }
660     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
661     ret = HC_ERR_IPC_UNKNOW_REPLY;
662     inOutLen = sizeof(int32_t);
663     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
664     DestroyCallCtx(&callCtx, NULL);
665     LOGI("process done, ret %" LOG_PUB "d", ret);
666     return ret;
667 }
668 
IpcGmGetRegisterInfo(const char * reqJsonStr,char ** registerInfo)669 static int32_t IpcGmGetRegisterInfo(const char *reqJsonStr, char **registerInfo)
670 {
671     uintptr_t callCtx = 0x0;
672     int32_t inOutLen;
673     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
674     char *outInfo = NULL;
675 
676     if (IsStrInvalid(reqJsonStr) || (registerInfo == NULL)) {
677         LOGE("Invalid params.");
678         return HC_ERR_INVALID_PARAMS;
679     }
680     int32_t ret = CreateCallCtx(&callCtx, NULL);
681     if (ret != HC_SUCCESS) {
682         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
683         return HC_ERR_IPC_INIT;
684     }
685     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQ_JSON, (const uint8_t *)reqJsonStr, HcStrlen(reqJsonStr) + 1);
686     if (ret != HC_SUCCESS) {
687         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQ_JSON);
688         DestroyCallCtx(&callCtx, NULL);
689         return HC_ERR_IPC_BUILD_PARAM;
690     }
691     ret = DoBinderCall(callCtx, IPC_CALL_ID_APPLY_REG_INFO, true);
692     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
693         DestroyCallCtx(&callCtx, NULL);
694         return HC_ERR_IPC_PROC_FAILED;
695     }
696     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
697     ret = HC_ERR_IPC_UNKNOW_REPLY;
698     inOutLen = sizeof(int32_t);
699     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
700     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
701         DestroyCallCtx(&callCtx, NULL);
702         return HC_ERR_IPC_BAD_PARAM;
703     }
704     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
705     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
706         LOGE("done, ret %" LOG_PUB "d", HC_ERR_IPC_OUT_DATA_NUM);
707         DestroyCallCtx(&callCtx, NULL);
708         return HC_ERR_IPC_OUT_DATA_NUM;
709     }
710     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_REG_INFO, (uint8_t *)&outInfo, NULL);
711     if ((outInfo == NULL) || (HcStrlen(outInfo) == 0)) {
712         LOGE("done, ret %" LOG_PUB "d", HC_ERR_IPC_OUT_DATA);
713         DestroyCallCtx(&callCtx, NULL);
714         return HC_ERR_IPC_OUT_DATA;
715     }
716     *registerInfo = strdup(outInfo);
717     DestroyCallCtx(&callCtx, NULL);
718     return (*registerInfo != NULL) ? HC_SUCCESS : HC_ERR_NULL_PTR;
719 }
720 
IpcGmCheckAccessToGroup(int32_t osAccountId,const char * appId,const char * groupId)721 static int32_t IpcGmCheckAccessToGroup(int32_t osAccountId, const char *appId, const char *groupId)
722 {
723     uintptr_t callCtx = 0x0;
724     int32_t ret;
725     int32_t inOutLen;
726     IpcDataInfo replyCache = { 0 };
727 
728     LOGI("starting ...");
729     if (IsStrInvalid(appId) || IsStrInvalid(groupId)) {
730         LOGE("Invalid params.");
731         return HC_ERR_INVALID_PARAMS;
732     }
733     ret = CreateCallCtx(&callCtx, NULL);
734     if (ret != HC_SUCCESS) {
735         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
736         return HC_ERR_IPC_INIT;
737     }
738     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
739         sizeof(osAccountId));
740     if (ret != HC_SUCCESS) {
741         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
742         DestroyCallCtx(&callCtx, NULL);
743         return HC_ERR_IPC_BUILD_PARAM;
744     }
745     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
746     if (ret != HC_SUCCESS) {
747         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
748         DestroyCallCtx(&callCtx, NULL);
749         return HC_ERR_IPC_BUILD_PARAM;
750     }
751     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, HcStrlen(groupId) + 1);
752     if (ret != HC_SUCCESS) {
753         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUPID);
754         DestroyCallCtx(&callCtx, NULL);
755         return HC_ERR_IPC_BUILD_PARAM;
756     }
757     ret = DoBinderCall(callCtx, IPC_CALL_ID_CHECK_ACCESS_TO_GROUP, true);
758     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
759         LOGE("ipc call failed");
760         DestroyCallCtx(&callCtx, NULL);
761         return HC_ERR_IPC_PROC_FAILED;
762     }
763     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
764     ret = HC_ERR_IPC_UNKNOW_REPLY;
765     inOutLen = sizeof(int32_t);
766     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
767     LOGI("process done, ret %" LOG_PUB "d", ret);
768     DestroyCallCtx(&callCtx, NULL);
769     return ret;
770 }
771 
ParseReturnResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData,uint32_t * returnNum)772 static int32_t ParseReturnResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData, uint32_t *returnNum)
773 {
774     int32_t ret;
775     int32_t inOutLen;
776 
777     inOutLen = sizeof(int32_t);
778     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
779     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
780         return HC_ERR_IPC_OUT_DATA_NUM;
781     }
782     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_RETURN_DATA, (uint8_t *)returnData, NULL);
783     if (*returnData == NULL) {
784         return HC_ERR_IPC_OUT_DATA;
785     }
786     *returnData = strdup(*returnData);
787     if (*returnData == NULL) {
788         return HC_ERR_ALLOC_MEMORY;
789     }
790     inOutLen = sizeof(int32_t);
791     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)returnNum, &inOutLen);
792     return HC_SUCCESS;
793 }
794 
IpcGmGetPkInfoList(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnInfoList,uint32_t * returnInfoNum)795 static int32_t IpcGmGetPkInfoList(int32_t osAccountId, const char *appId, const char *queryParams,
796                                   char **returnInfoList, uint32_t *returnInfoNum)
797 {
798     uintptr_t callCtx = 0x0;
799     int32_t ret;
800     int32_t inOutLen;
801     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
802 
803     if (IsStrInvalid(appId) || IsStrInvalid(queryParams) || (returnInfoList == NULL) || (returnInfoNum == NULL)) {
804         LOGE("Invalid params.");
805         return HC_ERR_INVALID_PARAMS;
806     }
807     ret = CreateCallCtx(&callCtx, NULL);
808     if (ret != HC_SUCCESS) {
809         return HC_ERR_IPC_INIT;
810     }
811     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID,
812         (const uint8_t *)&osAccountId, sizeof(osAccountId));
813     if (ret != HC_SUCCESS) {
814         DestroyCallCtx(&callCtx, NULL);
815         return HC_ERR_IPC_BUILD_PARAM;
816     }
817     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
818     if (ret != HC_SUCCESS) {
819         DestroyCallCtx(&callCtx, NULL);
820         return HC_ERR_IPC_BUILD_PARAM;
821     }
822     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_QUERY_PARAMS,
823         (const uint8_t *)queryParams, HcStrlen(queryParams) + 1);
824     if (ret != HC_SUCCESS) {
825         DestroyCallCtx(&callCtx, NULL);
826         return HC_ERR_IPC_BUILD_PARAM;
827     }
828     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_PK_INFO_LIST, true);
829     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
830         DestroyCallCtx(&callCtx, NULL);
831         return HC_ERR_IPC_PROC_FAILED;
832     }
833     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
834     ret = HC_ERR_IPC_UNKNOW_REPLY;
835     inOutLen = sizeof(int32_t);
836     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
837     if (ret != HC_SUCCESS) {
838         DestroyCallCtx(&callCtx, NULL);
839         return ret;
840     }
841     ret = ParseReturnResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnInfoList, returnInfoNum);
842     DestroyCallCtx(&callCtx, NULL);
843     return ret;
844 }
845 
GroupInfoIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupInfo)846 static int32_t GroupInfoIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outGroupInfo)
847 {
848     int32_t inOutLen;
849     int32_t ret;
850 
851     inOutLen = sizeof(int32_t);
852     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
853     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
854         return HC_ERR_IPC_OUT_DATA_NUM;
855     }
856     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupInfo, NULL);
857     if (*outGroupInfo == NULL) {
858         return HC_ERR_IPC_OUT_DATA;
859     }
860     *outGroupInfo = strdup(*outGroupInfo);
861     if (*outGroupInfo == NULL) {
862         return HC_ERR_ALLOC_MEMORY;
863     }
864     return HC_SUCCESS;
865 }
866 
IpcGmGetGroupInfoById(int32_t osAccountId,const char * appId,const char * groupId,char ** outGroupInfo)867 static int32_t IpcGmGetGroupInfoById(int32_t osAccountId, const char *appId, const char *groupId, char **outGroupInfo)
868 {
869     uintptr_t callCtx = 0x0;
870     int32_t inOutLen;
871     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
872 
873     if (IsStrInvalid(groupId) || IsStrInvalid(appId) || (outGroupInfo == NULL)) {
874         LOGE("Invalid params.");
875         return HC_ERR_INVALID_PARAMS;
876     }
877     int32_t ret = CreateCallCtx(&callCtx, NULL);
878     if (ret != HC_SUCCESS) {
879         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
880         return HC_ERR_IPC_INIT;
881     }
882     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
883         sizeof(osAccountId));
884     if (ret != HC_SUCCESS) {
885         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
886         DestroyCallCtx(&callCtx, NULL);
887         return HC_ERR_IPC_BUILD_PARAM;
888     }
889     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
890     if (ret != HC_SUCCESS) {
891         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
892         DestroyCallCtx(&callCtx, NULL);
893         return HC_ERR_IPC_BUILD_PARAM;
894     }
895     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, HcStrlen(groupId) + 1);
896     if (ret != HC_SUCCESS) {
897         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUPID);
898         DestroyCallCtx(&callCtx, NULL);
899         return HC_ERR_IPC_BUILD_PARAM;
900     }
901     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_GROUP_INFO, true);
902     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
903         DestroyCallCtx(&callCtx, NULL);
904         return HC_ERR_IPC_PROC_FAILED;
905     }
906     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
907     ret = HC_ERR_IPC_UNKNOW_REPLY;
908     inOutLen = sizeof(int32_t);
909     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
910     if (ret != HC_SUCCESS) {
911         DestroyCallCtx(&callCtx, NULL);
912         return ret;
913     }
914     ret = GroupInfoIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupInfo);
915     DestroyCallCtx(&callCtx, NULL);
916     return ret;
917 }
918 
SearchGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)919 static int32_t SearchGroupsIpcResult(const IpcDataInfo *replies,
920     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
921 {
922     int32_t ret;
923     int32_t inOutLen;
924 
925     inOutLen = sizeof(int32_t);
926     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
927     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
928         return HC_ERR_IPC_OUT_DATA_NUM;
929     }
930     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
931     if (*outGroupVec == NULL) {
932         return HC_ERR_IPC_OUT_DATA;
933     }
934     *outGroupVec = strdup(*outGroupVec);
935     if (*outGroupVec == NULL) {
936         return HC_ERR_ALLOC_MEMORY;
937     }
938     inOutLen = sizeof(int32_t);
939     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
940     return HC_SUCCESS;
941 }
942 
IpcGmGetGroupInfo(int32_t osAccountId,const char * appId,const char * queryParams,char ** outGroupVec,uint32_t * groupNum)943 static int32_t IpcGmGetGroupInfo(int32_t osAccountId, const char *appId, const char *queryParams,
944     char **outGroupVec, uint32_t *groupNum)
945 {
946     uintptr_t callCtx = 0x0;
947     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
948 
949     if (IsStrInvalid(queryParams) || IsStrInvalid(appId) || (outGroupVec == NULL) || (groupNum == NULL)) {
950         LOGE("Invalid params.");
951         return HC_ERR_INVALID_PARAMS;
952     }
953     int32_t ret = CreateCallCtx(&callCtx, NULL);
954     if (ret != HC_SUCCESS) {
955         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
956         return HC_ERR_IPC_INIT;
957     }
958     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
959         sizeof(osAccountId));
960     if (ret != HC_SUCCESS) {
961         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
962         DestroyCallCtx(&callCtx, NULL);
963         return HC_ERR_IPC_BUILD_PARAM;
964     }
965     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
966     if (ret != HC_SUCCESS) {
967         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
968         DestroyCallCtx(&callCtx, NULL);
969         return HC_ERR_IPC_BUILD_PARAM;
970     }
971     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_QUERY_PARAMS,
972         (const uint8_t *)queryParams, HcStrlen(queryParams) + 1);
973     if (ret != HC_SUCCESS) {
974         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_QUERY_PARAMS);
975         DestroyCallCtx(&callCtx, NULL);
976         return HC_ERR_IPC_BUILD_PARAM;
977     }
978     ret = DoBinderCall(callCtx, IPC_CALL_ID_SEARCH_GROUPS, true);
979     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
980         DestroyCallCtx(&callCtx, NULL);
981         return HC_ERR_IPC_PROC_FAILED;
982     }
983     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
984     ret = HC_ERR_IPC_UNKNOW_REPLY;
985     int32_t inOutLen = sizeof(int32_t);
986     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
987     if (ret != HC_SUCCESS) {
988         DestroyCallCtx(&callCtx, NULL);
989         return ret;
990     }
991     ret = SearchGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
992     DestroyCallCtx(&callCtx, NULL);
993     return ret;
994 }
995 
JoinedGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)996 static int32_t JoinedGroupsIpcResult(const IpcDataInfo *replies,
997     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
998 {
999     int32_t ret;
1000     int32_t inOutLen;
1001 
1002     inOutLen = sizeof(int32_t);
1003     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1004     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1005         return HC_ERR_IPC_OUT_DATA_NUM;
1006     }
1007     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
1008     if (*outGroupVec == NULL) {
1009         return HC_ERR_IPC_OUT_DATA;
1010     }
1011     *outGroupVec = strdup(*outGroupVec);
1012     if (*outGroupVec == NULL) {
1013         return HC_ERR_ALLOC_MEMORY;
1014     }
1015     inOutLen = sizeof(int32_t);
1016     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
1017     return HC_SUCCESS;
1018 }
1019 
IpcGmGetJoinedGroups(int32_t osAccountId,const char * appId,int32_t groupType,char ** outGroupVec,uint32_t * groupNum)1020 static int32_t IpcGmGetJoinedGroups(int32_t osAccountId, const char *appId, int32_t groupType,
1021     char **outGroupVec, uint32_t *groupNum)
1022 {
1023     uintptr_t callCtx = 0x0;
1024     int32_t inOutLen;
1025     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
1026 
1027     if (IsStrInvalid(appId) || (outGroupVec == NULL) || (groupNum == NULL)) {
1028         LOGE("Invalid params.");
1029         return HC_ERR_INVALID_PARAMS;
1030     }
1031     int32_t ret = CreateCallCtx(&callCtx, NULL);
1032     if (ret != HC_SUCCESS) {
1033         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1034         return HC_ERR_IPC_INIT;
1035     }
1036     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1037         sizeof(osAccountId));
1038     if (ret != HC_SUCCESS) {
1039         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1040         DestroyCallCtx(&callCtx, NULL);
1041         return HC_ERR_IPC_BUILD_PARAM;
1042     }
1043     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1044     if (ret != HC_SUCCESS) {
1045         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1046         DestroyCallCtx(&callCtx, NULL);
1047         return HC_ERR_IPC_BUILD_PARAM;
1048     }
1049     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUP_TYPE, (const uint8_t *)&groupType, sizeof(groupType));
1050     if (ret != HC_SUCCESS) {
1051         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUP_TYPE);
1052         DestroyCallCtx(&callCtx, NULL);
1053         return HC_ERR_IPC_BUILD_PARAM;
1054     }
1055     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_JOINED_GROUPS, true);
1056     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1057         DestroyCallCtx(&callCtx, NULL);
1058         return HC_ERR_IPC_PROC_FAILED;
1059     }
1060     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1061     ret = HC_ERR_IPC_UNKNOW_REPLY;
1062     inOutLen = sizeof(int32_t);
1063     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1064     if (ret != HC_SUCCESS) {
1065         DestroyCallCtx(&callCtx, NULL);
1066         return ret;
1067     }
1068     ret = JoinedGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
1069     DestroyCallCtx(&callCtx, NULL);
1070     return ret;
1071 }
1072 
RelatedGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)1073 static int32_t RelatedGroupsIpcResult(const IpcDataInfo *replies,
1074     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
1075 {
1076     int32_t ret;
1077     int32_t inOutLen;
1078 
1079     inOutLen = sizeof(int32_t);
1080     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1081     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1082         return HC_ERR_IPC_OUT_DATA_NUM;
1083     }
1084     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
1085     if (*outGroupVec == NULL) {
1086         return HC_ERR_IPC_OUT_DATA;
1087     }
1088     *outGroupVec = strdup(*outGroupVec);
1089     if (*outGroupVec == NULL) {
1090         return HC_ERR_ALLOC_MEMORY;
1091     }
1092     inOutLen = sizeof(int32_t);
1093     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
1094     return HC_SUCCESS;
1095 }
1096 
IpcGmGetRelatedGroups(int32_t osAccountId,const char * appId,const char * peerUdid,char ** outGroupVec,uint32_t * groupNum)1097 static int32_t IpcGmGetRelatedGroups(int32_t osAccountId, const char *appId, const char *peerUdid,
1098     char **outGroupVec, uint32_t *groupNum)
1099 {
1100     uintptr_t callCtx = 0x0;
1101     int32_t ret;
1102     int32_t inOutLen;
1103     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
1104 
1105     if (IsStrInvalid(appId) || IsStrInvalid(peerUdid) || (outGroupVec == NULL) || (groupNum == NULL)) {
1106         LOGE("Invalid params.");
1107         return HC_ERR_INVALID_PARAMS;
1108     }
1109     ret = CreateCallCtx(&callCtx, NULL);
1110     if (ret != HC_SUCCESS) {
1111         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1112         return HC_ERR_IPC_INIT;
1113     }
1114     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId, sizeof(int32_t));
1115     if (ret != HC_SUCCESS) {
1116         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1117         DestroyCallCtx(&callCtx, NULL);
1118         return HC_ERR_IPC_BUILD_PARAM;
1119     }
1120     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1121     if (ret != HC_SUCCESS) {
1122         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1123         DestroyCallCtx(&callCtx, NULL);
1124         return HC_ERR_IPC_BUILD_PARAM;
1125     }
1126     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)peerUdid, HcStrlen(peerUdid) + 1);
1127     if (ret != HC_SUCCESS) {
1128         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_UDID);
1129         DestroyCallCtx(&callCtx, NULL);
1130         return HC_ERR_IPC_BUILD_PARAM;
1131     }
1132     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_RELATED_GROUPS, true);
1133     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1134         DestroyCallCtx(&callCtx, NULL);
1135         return HC_ERR_IPC_PROC_FAILED;
1136     }
1137     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1138     ret = HC_ERR_IPC_UNKNOW_REPLY;
1139     inOutLen = sizeof(int32_t);
1140     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1141     if (ret != HC_SUCCESS) {
1142         LOGE("Service return exception, ret: %" LOG_PUB "d", ret);
1143         DestroyCallCtx(&callCtx, NULL);
1144         return ret;
1145     }
1146     ret = RelatedGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
1147     DestroyCallCtx(&callCtx, NULL);
1148     return ret;
1149 }
1150 
DevInfoByIdIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outDevInfo)1151 static int32_t DevInfoByIdIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outDevInfo)
1152 {
1153     int32_t ret;
1154     int32_t inOutLen;
1155 
1156     inOutLen = sizeof(int32_t);
1157     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1158     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
1159         return HC_ERR_IPC_OUT_DATA_NUM;
1160     }
1161     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DEVICE_INFO, (uint8_t *)outDevInfo, NULL);
1162     if (*outDevInfo == NULL) {
1163         return HC_ERR_IPC_OUT_DATA;
1164     }
1165     *outDevInfo = strdup(*outDevInfo);
1166     if (*outDevInfo == NULL) {
1167         return HC_ERR_ALLOC_MEMORY;
1168     }
1169     return HC_SUCCESS;
1170 }
1171 
FormParamsForGettingDeviceInfo(int32_t osAccountId,const char * appId,const char * peerUdid,const char * groupId,uintptr_t callCtx)1172 static int32_t FormParamsForGettingDeviceInfo(int32_t osAccountId, const char *appId,
1173     const char *peerUdid, const char *groupId, uintptr_t callCtx)
1174 {
1175     int32_t ret;
1176     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1177         sizeof(osAccountId));
1178     if (ret != HC_SUCCESS) {
1179         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1180         DestroyCallCtx(&callCtx, NULL);
1181         return HC_ERR_IPC_BUILD_PARAM;
1182     }
1183     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1184     if (ret != HC_SUCCESS) {
1185         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1186         return HC_ERR_IPC_BUILD_PARAM;
1187     }
1188     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)peerUdid, HcStrlen(peerUdid) + 1);
1189     if (ret != HC_SUCCESS) {
1190         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_UDID);
1191         return HC_ERR_IPC_BUILD_PARAM;
1192     }
1193     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, HcStrlen(groupId) + 1);
1194     if (ret != HC_SUCCESS) {
1195         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUPID);
1196         return HC_ERR_IPC_BUILD_PARAM;
1197     }
1198     return HC_SUCCESS;
1199 }
1200 
IpcGmGetDeviceInfoById(int32_t osAccountId,const char * appId,const char * peerUdid,const char * groupId,char ** outDevInfo)1201 static int32_t IpcGmGetDeviceInfoById(int32_t osAccountId, const char *appId, const char *peerUdid, const char *groupId,
1202     char **outDevInfo)
1203 {
1204     uintptr_t callCtx = 0x0;
1205     int32_t ret;
1206     int32_t inOutLen;
1207     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
1208 
1209     LOGI("starting ...");
1210     if (IsStrInvalid(appId) || IsStrInvalid(peerUdid) || IsStrInvalid(groupId) || (outDevInfo == NULL)) {
1211         LOGE("Invalid params.");
1212         return HC_ERR_INVALID_PARAMS;
1213     }
1214     ret = CreateCallCtx(&callCtx, NULL);
1215     if (ret != HC_SUCCESS) {
1216         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1217         return HC_ERR_IPC_INIT;
1218     }
1219     ret = FormParamsForGettingDeviceInfo(osAccountId, appId, peerUdid, groupId, callCtx);
1220     if (ret != HC_SUCCESS) {
1221         DestroyCallCtx(&callCtx, NULL);
1222         return ret;
1223     }
1224     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_DEV_INFO_BY_ID, true);
1225     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1226         LOGE("ipc call failed");
1227         DestroyCallCtx(&callCtx, NULL);
1228         return HC_ERR_IPC_PROC_FAILED;
1229     }
1230     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1231     ret = HC_ERR_IPC_UNKNOW_REPLY;
1232     inOutLen = sizeof(int32_t);
1233     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1234     if (ret != HC_SUCCESS) {
1235         LOGE("Service return exception, ret: %" LOG_PUB "d", ret);
1236         DestroyCallCtx(&callCtx, NULL);
1237         return ret;
1238     }
1239     ret = DevInfoByIdIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outDevInfo);
1240     LOGI("proc result done, ret %" LOG_PUB "d", ret);
1241     DestroyCallCtx(&callCtx, NULL);
1242     return ret;
1243 }
1244 
TrustedDevIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outDevInfoVec,uint32_t * devNum)1245 static int32_t TrustedDevIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outDevInfoVec, uint32_t *devNum)
1246 {
1247     int32_t ret;
1248     int32_t inOutLen;
1249 
1250     inOutLen = sizeof(int32_t);
1251     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1252     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1253         return HC_ERR_IPC_OUT_DATA_NUM;
1254     }
1255     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DEVICE_INFO, (uint8_t *)outDevInfoVec, NULL);
1256     if (*outDevInfoVec == NULL) {
1257         return HC_ERR_IPC_OUT_DATA;
1258     }
1259     *outDevInfoVec = strdup(*outDevInfoVec);
1260     if (*outDevInfoVec == NULL) {
1261         return HC_ERR_ALLOC_MEMORY;
1262     }
1263     inOutLen = sizeof(int32_t);
1264     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)devNum, &inOutLen);
1265     return HC_SUCCESS;
1266 }
1267 
IpcGmGetTrustedDevices(int32_t osAccountId,const char * appId,const char * groupId,char ** outDevInfoVec,uint32_t * deviceNum)1268 static int32_t IpcGmGetTrustedDevices(int32_t osAccountId, const char *appId,
1269     const char *groupId, char **outDevInfoVec, uint32_t *deviceNum)
1270 {
1271     uintptr_t callCtx = 0x0;
1272     int32_t inOutLen;
1273     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
1274 
1275     if (IsStrInvalid(appId) || IsStrInvalid(groupId) || (outDevInfoVec == NULL) || (deviceNum == NULL)) {
1276         LOGE("Invalid params.");
1277         return HC_ERR_INVALID_PARAMS;
1278     }
1279     int32_t ret = CreateCallCtx(&callCtx, NULL);
1280     if (ret != HC_SUCCESS) {
1281         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1282         return HC_ERR_IPC_INIT;
1283     }
1284     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1285         sizeof(osAccountId));
1286     if (ret != HC_SUCCESS) {
1287         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1288         DestroyCallCtx(&callCtx, NULL);
1289         return HC_ERR_IPC_BUILD_PARAM;
1290     }
1291     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1292     if (ret != HC_SUCCESS) {
1293         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1294         DestroyCallCtx(&callCtx, NULL);
1295         return HC_ERR_IPC_BUILD_PARAM;
1296     }
1297     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, HcStrlen(groupId) + 1);
1298     if (ret != HC_SUCCESS) {
1299         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUPID);
1300         DestroyCallCtx(&callCtx, NULL);
1301         return HC_ERR_IPC_BUILD_PARAM;
1302     }
1303     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_TRUST_DEVICES, true);
1304     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1305         DestroyCallCtx(&callCtx, NULL);
1306         return HC_ERR_IPC_PROC_FAILED;
1307     }
1308     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1309     ret = HC_ERR_IPC_UNKNOW_REPLY;
1310     inOutLen = sizeof(int32_t);
1311     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1312     if (ret != HC_SUCCESS) {
1313         DestroyCallCtx(&callCtx, NULL);
1314         return ret;
1315     }
1316     ret = TrustedDevIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outDevInfoVec, deviceNum);
1317     DestroyCallCtx(&callCtx, NULL);
1318     return ret;
1319 }
1320 
IpcGmIsDeviceInGroup(int32_t osAccountId,const char * appId,const char * groupId,const char * udid)1321 static bool IpcGmIsDeviceInGroup(int32_t osAccountId, const char *appId, const char *groupId, const char *udid)
1322 {
1323     uintptr_t callCtx = 0x0;
1324     int32_t inOutLen;
1325     IpcDataInfo replyCache = { 0 };
1326 
1327     if (IsStrInvalid(appId) || IsStrInvalid(groupId) || IsStrInvalid(udid)) {
1328         LOGE("Invalid params.");
1329         return false;
1330     }
1331     int32_t ret = CreateCallCtx(&callCtx, NULL);
1332     if (ret != HC_SUCCESS) {
1333         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1334         return false;
1335     }
1336     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1337         sizeof(osAccountId));
1338     if (ret != HC_SUCCESS) {
1339         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1340         DestroyCallCtx(&callCtx, NULL);
1341         return false;
1342     }
1343     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1344     if (ret != HC_SUCCESS) {
1345         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1346         DestroyCallCtx(&callCtx, NULL);
1347         return false;
1348     }
1349     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, HcStrlen(groupId) + 1);
1350     if (ret != HC_SUCCESS) {
1351         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_GROUPID);
1352         DestroyCallCtx(&callCtx, NULL);
1353         return false;
1354     }
1355     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)udid, HcStrlen(udid) + 1);
1356     if (ret != HC_SUCCESS) {
1357         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_UDID);
1358         DestroyCallCtx(&callCtx, NULL);
1359         return false;
1360     }
1361     ret = DoBinderCall(callCtx, IPC_CALL_ID_IS_DEV_IN_GROUP, true);
1362     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1363         DestroyCallCtx(&callCtx, NULL);
1364         return false;
1365     }
1366     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1367     ret = HC_ERR_IPC_UNKNOW_REPLY;
1368     inOutLen = sizeof(int32_t);
1369     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1370     DestroyCallCtx(&callCtx, NULL);
1371     return (ret == HC_SUCCESS) ? true : false;
1372 }
1373 
IpcGmDestroyInfo(char ** returnInfo)1374 static void IpcGmDestroyInfo(char **returnInfo)
1375 {
1376     if ((returnInfo == NULL) || (*returnInfo == NULL)) {
1377         return;
1378     }
1379     FreeJsonString(*returnInfo);
1380     *returnInfo = NULL;
1381 }
1382 
IpcGmCancelRequest(int64_t requestId,const char * appId)1383 static void IpcGmCancelRequest(int64_t requestId, const char *appId)
1384 {
1385     uintptr_t callCtx = 0x0;
1386     int32_t ret;
1387 
1388     LOGI("starting ...");
1389     if (IsStrInvalid(appId)) {
1390         LOGE("Invalid params.");
1391         return;
1392     }
1393     ret = CreateCallCtx(&callCtx, NULL);
1394     if (ret != HC_SUCCESS) {
1395         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1396         return;
1397     }
1398     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&requestId), sizeof(requestId));
1399     if (ret != HC_SUCCESS) {
1400         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
1401         DestroyCallCtx(&callCtx, NULL);
1402         return;
1403     }
1404     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1405     if (ret != HC_SUCCESS) {
1406         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1407         DestroyCallCtx(&callCtx, NULL);
1408         return;
1409     }
1410     ret = DoBinderCall(callCtx, IPC_CALL_GM_CANCEL_REQUEST, true);
1411     DestroyCallCtx(&callCtx, NULL);
1412     if (ret != HC_SUCCESS) {
1413         LOGE("ipc call failed");
1414     } else {
1415         LOGI("process done, ret %" LOG_PUB "d", ret);
1416     }
1417 }
1418 
InitIpcGmMethods(DeviceGroupManager * gmMethodObj)1419 static void InitIpcGmMethods(DeviceGroupManager *gmMethodObj)
1420 {
1421     gmMethodObj->regCallback = IpcGmRegCallback;
1422     gmMethodObj->unRegCallback = IpcGmUnRegCallback;
1423     gmMethodObj->regDataChangeListener = IpcGmRegDataChangeListener;
1424     gmMethodObj->unRegDataChangeListener = IpcGmUnRegDataChangeListener;
1425     gmMethodObj->createGroup = IpcGmCreateGroup;
1426     gmMethodObj->deleteGroup = IpcGmDeleteGroup;
1427     gmMethodObj->addMemberToGroup = IpcGmAddMemberToGroup;
1428     gmMethodObj->deleteMemberFromGroup = IpcGmDelMemberFromGroup;
1429     gmMethodObj->addMultiMembersToGroup = IpcGmAddMultiMembersToGroup;
1430     gmMethodObj->delMultiMembersFromGroup = IpcGmDelMultiMembersFromGroup;
1431     gmMethodObj->processData = IpcGmProcessData;
1432     gmMethodObj->getRegisterInfo = IpcGmGetRegisterInfo;
1433     gmMethodObj->checkAccessToGroup = IpcGmCheckAccessToGroup;
1434     gmMethodObj->getPkInfoList = IpcGmGetPkInfoList;
1435     gmMethodObj->getGroupInfoById = IpcGmGetGroupInfoById;
1436     gmMethodObj->getGroupInfo = IpcGmGetGroupInfo;
1437     gmMethodObj->getJoinedGroups = IpcGmGetJoinedGroups;
1438     gmMethodObj->getRelatedGroups = IpcGmGetRelatedGroups;
1439     gmMethodObj->getDeviceInfoById = IpcGmGetDeviceInfoById;
1440     gmMethodObj->getTrustedDevices = IpcGmGetTrustedDevices;
1441     gmMethodObj->isDeviceInGroup = IpcGmIsDeviceInGroup;
1442     gmMethodObj->cancelRequest = IpcGmCancelRequest;
1443     gmMethodObj->destroyInfo = IpcGmDestroyInfo;
1444     return;
1445 }
1446 
EncodeProcessDataParams(uintptr_t callCtx,int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)1447 static int32_t EncodeProcessDataParams(uintptr_t callCtx, int64_t authReqId,
1448     const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *callback)
1449 {
1450     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
1451     if (ret != HC_SUCCESS) {
1452         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
1453         return HC_ERR_IPC_BUILD_PARAM;
1454     }
1455     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_COMM_DATA, (const uint8_t *)data, dataLen);
1456     if (ret != HC_SUCCESS) {
1457         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_COMM_DATA);
1458         return HC_ERR_IPC_BUILD_PARAM;
1459     }
1460     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
1461     if (ret != HC_SUCCESS) {
1462         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
1463         return HC_ERR_IPC_BUILD_PARAM;
1464     }
1465     SetCbCtxToDataCtx(callCtx, 0x0);
1466     return HC_SUCCESS;
1467 }
1468 
IpcGaProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)1469 static int32_t IpcGaProcessData(int64_t authReqId,
1470     const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *callback)
1471 {
1472     uintptr_t callCtx = 0x0;
1473     int32_t ret;
1474     int32_t inOutLen;
1475     IpcDataInfo replyCache = { 0 };
1476 
1477     LOGI("starting ...");
1478     if (!IS_COMM_DATA_VALID(data, dataLen) || (callback == NULL)) {
1479         LOGE("invalid params");
1480         return HC_ERR_INVALID_PARAMS;
1481     }
1482     ret = CreateCallCtx(&callCtx, NULL);
1483     if (ret != HC_SUCCESS) {
1484         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1485         return HC_ERR_IPC_INIT;
1486     }
1487     ret = EncodeProcessDataParams(callCtx, authReqId, data, dataLen, callback);
1488     if (ret != HC_SUCCESS) {
1489         DestroyCallCtx(&callCtx, NULL);
1490         return ret;
1491     }
1492     ret = DoBinderCall(callCtx, IPC_CALL_ID_GA_PROC_DATA, true);
1493     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1494         LOGE("ipc call failed");
1495         DestroyCallCtx(&callCtx, NULL);
1496         return HC_ERR_IPC_PROC_FAILED;
1497     }
1498     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1499     ret = HC_ERR_IPC_UNKNOW_REPLY;
1500     inOutLen = sizeof(int32_t);
1501     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1502     LOGI("process done, ret %" LOG_PUB "d", ret);
1503     DestroyCallCtx(&callCtx, NULL);
1504     return ret;
1505 }
1506 
EncodeAuthDeviceParams(uintptr_t callCtx,int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1507 static int32_t EncodeAuthDeviceParams(uintptr_t callCtx, int32_t osAccountId, int64_t authReqId,
1508     const char *authParams, const DeviceAuthCallback *callback)
1509 {
1510     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1511         sizeof(osAccountId));
1512     if (ret != HC_SUCCESS) {
1513         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1514         return HC_ERR_IPC_BUILD_PARAM;
1515     }
1516     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
1517     if (ret != HC_SUCCESS) {
1518         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
1519         return HC_ERR_IPC_BUILD_PARAM;
1520     }
1521     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams,
1522         HcStrlen(authParams) + 1);
1523     if (ret != HC_SUCCESS) {
1524         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_AUTH_PARAMS);
1525         return HC_ERR_IPC_BUILD_PARAM;
1526     }
1527     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
1528     if (ret != HC_SUCCESS) {
1529         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
1530         return HC_ERR_IPC_BUILD_PARAM;
1531     }
1532     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_AUTH_ID);
1533     return HC_SUCCESS;
1534 }
1535 
IpcGaAuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1536 static int32_t IpcGaAuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
1537     const DeviceAuthCallback *callback)
1538 {
1539     uintptr_t callCtx = 0x0;
1540     int32_t ret;
1541     int32_t inOutLen;
1542     IpcDataInfo replyCache = { 0 };
1543 
1544     LOGI("starting ...");
1545     if (IsStrInvalid(authParams) || (callback == NULL)) {
1546         LOGE("invalid params");
1547         return HC_ERR_INVALID_PARAMS;
1548     }
1549     ret = CreateCallCtx(&callCtx, NULL);
1550     if (ret != HC_SUCCESS) {
1551         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1552         return HC_ERR_IPC_INIT;
1553     }
1554     ret = EncodeAuthDeviceParams(callCtx, osAccountId, authReqId, authParams, callback);
1555     if (ret != HC_SUCCESS) {
1556         DestroyCallCtx(&callCtx, NULL);
1557         return ret;
1558     }
1559     ret = DoBinderCall(callCtx, IPC_CALL_ID_AUTH_DEVICE, true);
1560     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1561         LOGE("ipc call failed");
1562         DestroyCallCtx(&callCtx, NULL);
1563         return HC_ERR_IPC_PROC_FAILED;
1564     }
1565     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1566     ret = HC_ERR_IPC_UNKNOW_REPLY;
1567     inOutLen = sizeof(int32_t);
1568     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1569     LOGI("process done, ret %" LOG_PUB "d", ret);
1570     DestroyCallCtx(&callCtx, NULL);
1571     return ret;
1572 }
1573 
IpcGaCancelRequest(int64_t requestId,const char * appId)1574 static void IpcGaCancelRequest(int64_t requestId, const char *appId)
1575 {
1576     uintptr_t callCtx = 0x0;
1577     int32_t ret;
1578 
1579     LOGI("starting ...");
1580     if (IsStrInvalid(appId)) {
1581         LOGE("Invalid params.");
1582         return;
1583     }
1584     ret = CreateCallCtx(&callCtx, NULL);
1585     if (ret != HC_SUCCESS) {
1586         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1587         return;
1588     }
1589     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&requestId), sizeof(requestId));
1590     if (ret != HC_SUCCESS) {
1591         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
1592         DestroyCallCtx(&callCtx, NULL);
1593         return;
1594     }
1595     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, HcStrlen(appId) + 1);
1596     if (ret != HC_SUCCESS) {
1597         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
1598         DestroyCallCtx(&callCtx, NULL);
1599         return;
1600     }
1601     ret = DoBinderCall(callCtx, IPC_CALL_GA_CANCEL_REQUEST, true);
1602     DestroyCallCtx(&callCtx, NULL);
1603     if (ret != HC_SUCCESS) {
1604         LOGE("ipc call failed");
1605     } else {
1606         LOGI("process done, ret %" LOG_PUB "d", ret);
1607     }
1608 }
1609 
GetIpcReplyByTypeInner(const IpcDataInfo * replies,int32_t cacheNum,char ** outInfo)1610 static int32_t GetIpcReplyByTypeInner(const IpcDataInfo *replies, int32_t cacheNum, char **outInfo)
1611 {
1612     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_RETURN_DATA, (uint8_t *)outInfo, NULL);
1613     if (*outInfo == NULL) {
1614         return HC_ERR_IPC_OUT_DATA;
1615     }
1616     *outInfo = strdup(*outInfo);
1617     if (*outInfo == NULL) {
1618         return HC_ERR_ALLOC_MEMORY;
1619     }
1620     return HC_SUCCESS;
1621 }
1622 
IpcGaGetRealInfo(int32_t osAccountId,const char * pseudonymId,char ** realInfo)1623 static int32_t IpcGaGetRealInfo(int32_t osAccountId, const char *pseudonymId, char **realInfo)
1624 {
1625     uintptr_t callCtx = 0x0;
1626     int32_t ret;
1627     IpcDataInfo replyCache[IPC_DATA_CACHES_1] = { { 0 } };
1628 
1629     LOGI("starting ...");
1630     if (IsStrInvalid(pseudonymId) || (realInfo == NULL)) {
1631         LOGE("Invalid params.");
1632         return HC_ERR_INVALID_PARAMS;
1633     }
1634     ret = CreateCallCtx(&callCtx, NULL);
1635     if (ret != HC_SUCCESS) {
1636         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1637         return HC_ERR_IPC_INIT;
1638     }
1639     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1640         sizeof(osAccountId));
1641     if (ret != HC_SUCCESS) {
1642         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1643         DestroyCallCtx(&callCtx, NULL);
1644         return HC_ERR_IPC_BUILD_PARAM;
1645     }
1646     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_PSEUDONYM_ID, (const uint8_t *)pseudonymId,
1647         HcStrlen(pseudonymId) + 1);
1648     if (ret != HC_SUCCESS) {
1649         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_PSEUDONYM_ID);
1650         DestroyCallCtx(&callCtx, NULL);
1651         return HC_ERR_IPC_BUILD_PARAM;
1652     }
1653     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_REAL_INFO, true);
1654     if (ret != HC_SUCCESS) {
1655         LOGE("ipc call failed");
1656         DestroyCallCtx(&callCtx, NULL);
1657         return HC_ERR_IPC_PROC_FAILED;
1658     }
1659     LOGI("process done, ret %" LOG_PUB "d", ret);
1660     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1661     ret = GetIpcReplyByTypeInner(replyCache, REPLAY_CACHE_NUM(replyCache), realInfo);
1662     if (ret != HC_SUCCESS) {
1663         LOGE("GetIpcReplyByType failed, ret %" LOG_PUB "d", ret);
1664     }
1665     DestroyCallCtx(&callCtx, NULL);
1666     return ret;
1667 }
1668 
IpcGaGetPseudonymId(int32_t osAccountId,const char * indexKey,char ** pseudonymId)1669 static int32_t IpcGaGetPseudonymId(int32_t osAccountId, const char *indexKey, char **pseudonymId)
1670 {
1671     uintptr_t callCtx = 0x0;
1672     int32_t ret;
1673     IpcDataInfo replyCache[IPC_DATA_CACHES_1] = { { 0 } };
1674 
1675     LOGI("starting ...");
1676     if (IsStrInvalid(indexKey) || (pseudonymId == NULL)) {
1677         LOGE("Invalid params.");
1678         return HC_ERR_INVALID_PARAMS;
1679     }
1680     ret = CreateCallCtx(&callCtx, NULL);
1681     if (ret != HC_SUCCESS) {
1682         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1683         return HC_ERR_IPC_INIT;
1684     }
1685     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1686         sizeof(osAccountId));
1687     if (ret != HC_SUCCESS) {
1688         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1689         DestroyCallCtx(&callCtx, NULL);
1690         return HC_ERR_IPC_BUILD_PARAM;
1691     }
1692     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_INDEX_KEY, (const uint8_t *)indexKey, HcStrlen(indexKey) + 1);
1693     if (ret != HC_SUCCESS) {
1694         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_INDEX_KEY);
1695         DestroyCallCtx(&callCtx, NULL);
1696         return HC_ERR_IPC_BUILD_PARAM;
1697     }
1698     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_PSEUDONYM_ID, true);
1699     if (ret != HC_SUCCESS) {
1700         LOGE("ipc call failed");
1701         DestroyCallCtx(&callCtx, NULL);
1702         return HC_ERR_IPC_PROC_FAILED;
1703     }
1704     LOGI("process done, ret %" LOG_PUB "d", ret);
1705     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1706     ret = GetIpcReplyByTypeInner(replyCache, REPLAY_CACHE_NUM(replyCache), pseudonymId);
1707     if (ret != HC_SUCCESS) {
1708         LOGE("GetIpcReplyByType failed, ret %" LOG_PUB "d", ret);
1709     }
1710     DestroyCallCtx(&callCtx, NULL);
1711     return ret;
1712 }
1713 
InitIpcGaMethods(GroupAuthManager * gaMethodObj)1714 static void InitIpcGaMethods(GroupAuthManager *gaMethodObj)
1715 {
1716     gaMethodObj->processData = IpcGaProcessData;
1717     gaMethodObj->authDevice = IpcGaAuthDevice;
1718     gaMethodObj->cancelRequest = IpcGaCancelRequest;
1719     gaMethodObj->getRealInfo = IpcGaGetRealInfo;
1720     gaMethodObj->getPseudonymId = IpcGaGetPseudonymId;
1721     return;
1722 }
1723 
SetReturnSharedKey(const uint8_t * sharedKeyVal,uint32_t sharedKeyLen,DataBuff * returnSharedKey)1724 static int32_t SetReturnSharedKey(const uint8_t *sharedKeyVal, uint32_t sharedKeyLen, DataBuff *returnSharedKey)
1725 {
1726     uint8_t *tmpSharedKeyVal = (uint8_t *)HcMalloc(sharedKeyLen, 0);
1727     if (tmpSharedKeyVal == NULL) {
1728         LOGE("malloc temp shared key failed.");
1729         return HC_ERR_ALLOC_MEMORY;
1730     }
1731     if (memcpy_s(tmpSharedKeyVal, sharedKeyLen, sharedKeyVal, sharedKeyLen) != EOK) {
1732         LOGE("memcpy_s temp shared key failed.");
1733         HcFree(tmpSharedKeyVal);
1734         return HC_ERR_MEMORY_COPY;
1735     }
1736     returnSharedKey->data = tmpSharedKeyVal;
1737     returnSharedKey->length = sharedKeyLen;
1738     return HC_SUCCESS;
1739 }
1740 
SetReturnRandom(const uint8_t * randomVal,uint32_t randomLen,DataBuff * returnRandom)1741 static int32_t SetReturnRandom(const uint8_t *randomVal, uint32_t randomLen, DataBuff *returnRandom)
1742 {
1743     uint8_t *tmpRandomVal = (uint8_t *)HcMalloc(randomLen, 0);
1744     if (tmpRandomVal == NULL) {
1745         LOGE("malloc temp random failed.");
1746         return HC_ERR_ALLOC_MEMORY;
1747     }
1748     if (memcpy_s(tmpRandomVal, randomLen, randomVal, randomLen) != EOK) {
1749         LOGE("memcpy_s temp random failed.");
1750         HcFree(tmpRandomVal);
1751         return HC_ERR_MEMORY_COPY;
1752     }
1753     returnRandom->data = tmpRandomVal;
1754     returnRandom->length = randomLen;
1755     return HC_SUCCESS;
1756 }
1757 
IpcAvDestroyDataBuff(DataBuff * dataBuff)1758 static void IpcAvDestroyDataBuff(DataBuff *dataBuff)
1759 {
1760     if (dataBuff == NULL || dataBuff->data == NULL) {
1761         return;
1762     }
1763     HcFree(dataBuff->data);
1764     dataBuff->data = NULL;
1765     dataBuff->length = 0;
1766 }
1767 
GetSharedKeyAndRandom(const IpcDataInfo * replies,int32_t cacheNum,DataBuff * returnSharedKey,DataBuff * returnRandom)1768 static int32_t GetSharedKeyAndRandom(const IpcDataInfo *replies, int32_t cacheNum, DataBuff *returnSharedKey,
1769     DataBuff *returnRandom)
1770 {
1771     int32_t resultNum;
1772     int32_t inOutLen = sizeof(int32_t);
1773     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&resultNum, &inOutLen);
1774     if ((resultNum < IPC_RESULT_NUM_4) || (inOutLen != sizeof(int32_t))) {
1775         return HC_ERR_IPC_OUT_DATA_NUM;
1776     }
1777 
1778     uint8_t *sharedKeyVal = NULL;
1779     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_SHARED_KEY_VAL, (uint8_t *)&sharedKeyVal, NULL);
1780     if (sharedKeyVal == NULL) {
1781         return HC_ERR_IPC_OUT_DATA;
1782     }
1783     inOutLen = sizeof(int32_t);
1784     uint32_t sharedKeyLen = 0;
1785     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_SHARED_KEY_LEN, (uint8_t *)&sharedKeyLen, &inOutLen);
1786     if (sharedKeyLen == 0) {
1787         return HC_ERR_IPC_OUT_DATA;
1788     }
1789 
1790     uint8_t *randomVal = NULL;
1791     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_RANDOM_VAL, (uint8_t *)&randomVal, NULL);
1792     if (randomVal == NULL) {
1793         return HC_ERR_IPC_OUT_DATA;
1794     }
1795     inOutLen = sizeof(int32_t);
1796     uint32_t randomLen = 0;
1797     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_RANDOM_LEN, (uint8_t *)&randomLen, &inOutLen);
1798     if (randomLen == 0) {
1799         return HC_ERR_IPC_OUT_DATA;
1800     }
1801     int32_t ret = SetReturnSharedKey(sharedKeyVal, sharedKeyLen, returnSharedKey);
1802     if (ret != HC_SUCCESS) {
1803         return ret;
1804     }
1805     ret = SetReturnRandom(randomVal, randomLen, returnRandom);
1806     if (ret != HC_SUCCESS) {
1807         IpcAvDestroyDataBuff(returnSharedKey);
1808     }
1809     return ret;
1810 }
1811 
IpcAvGetClientSharedKey(const char * peerPk,const char * serviceId,DataBuff * returnSharedKey,DataBuff * returnRandom)1812 static int32_t IpcAvGetClientSharedKey(const char *peerPk, const char *serviceId, DataBuff *returnSharedKey,
1813     DataBuff *returnRandom)
1814 {
1815     if ((peerPk == NULL) || (serviceId == NULL) || (returnSharedKey == NULL) || (returnRandom == NULL)) {
1816         LOGE("Invalid params.");
1817         return HC_ERR_INVALID_PARAMS;
1818     }
1819     uintptr_t callCtx = 0x0;
1820     int32_t ret = CreateCallCtx(&callCtx, NULL);
1821     if (ret != HC_SUCCESS) {
1822         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1823         return HC_ERR_IPC_INIT;
1824     }
1825     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_PUB_KEY, (const uint8_t *)peerPk, HcStrlen(peerPk) + 1);
1826     if (ret != HC_SUCCESS) {
1827         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_PUB_KEY);
1828         DestroyCallCtx(&callCtx, NULL);
1829         return HC_ERR_IPC_BUILD_PARAM;
1830     }
1831     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_SERVICE_ID, (const uint8_t *)serviceId, HcStrlen(serviceId) + 1);
1832     if (ret != HC_SUCCESS) {
1833         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_SERVICE_ID);
1834         DestroyCallCtx(&callCtx, NULL);
1835         return HC_ERR_IPC_BUILD_PARAM;
1836     }
1837     ret = DoBinderCall(callCtx, IPC_CALL_ID_AV_GET_CLIENT_SHARED_KEY, true);
1838     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1839         DestroyCallCtx(&callCtx, NULL);
1840         return HC_ERR_IPC_PROC_FAILED;
1841     }
1842     IpcDataInfo replyCache[IPC_DATA_CACHES_6] = { { 0 } };
1843     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1844     ret = HC_ERR_IPC_UNKNOW_REPLY;
1845     int32_t inOutLen = sizeof(int32_t);
1846     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1847     if (ret != HC_SUCCESS) {
1848         DestroyCallCtx(&callCtx, NULL);
1849         return ret;
1850     }
1851     ret = GetSharedKeyAndRandom(replyCache, REPLAY_CACHE_NUM(replyCache), returnSharedKey, returnRandom);
1852     DestroyCallCtx(&callCtx, NULL);
1853     return ret;
1854 }
1855 
GetSharedKey(const IpcDataInfo * replies,int32_t cacheNum,DataBuff * returnSharedKey)1856 static int32_t GetSharedKey(const IpcDataInfo *replies, int32_t cacheNum, DataBuff *returnSharedKey)
1857 {
1858     int32_t resultNum;
1859     int32_t inOutLen = sizeof(int32_t);
1860     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&resultNum, &inOutLen);
1861     if ((resultNum < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1862         return HC_ERR_IPC_OUT_DATA_NUM;
1863     }
1864 
1865     uint8_t *sharedKeyVal = NULL;
1866     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_SHARED_KEY_VAL, (uint8_t *)&sharedKeyVal, NULL);
1867     if (sharedKeyVal == NULL) {
1868         return HC_ERR_IPC_OUT_DATA;
1869     }
1870     inOutLen = sizeof(int32_t);
1871     uint32_t sharedKeyLen = 0;
1872     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_SHARED_KEY_LEN, (uint8_t *)&sharedKeyLen, &inOutLen);
1873     if (sharedKeyLen == 0) {
1874         return HC_ERR_IPC_OUT_DATA;
1875     }
1876     return SetReturnSharedKey(sharedKeyVal, sharedKeyLen, returnSharedKey);
1877 }
1878 
IpcAvGetServerSharedKey(const char * peerPk,const char * serviceId,const DataBuff * random,DataBuff * returnSharedKey)1879 static int32_t IpcAvGetServerSharedKey(const char *peerPk, const char *serviceId, const DataBuff *random,
1880     DataBuff *returnSharedKey)
1881 {
1882     if ((peerPk == NULL) || (serviceId == NULL) || (random == NULL) || (random->data == NULL) ||
1883         (returnSharedKey == NULL)) {
1884         LOGE("Invalid params.");
1885         return HC_ERR_INVALID_PARAMS;
1886     }
1887     uintptr_t callCtx = 0x0;
1888     int32_t ret = CreateCallCtx(&callCtx, NULL);
1889     if (ret != HC_SUCCESS) {
1890         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1891         return HC_ERR_IPC_INIT;
1892     }
1893     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_PUB_KEY, (const uint8_t *)peerPk, HcStrlen(peerPk) + 1);
1894     if (ret != HC_SUCCESS) {
1895         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_PUB_KEY);
1896         DestroyCallCtx(&callCtx, NULL);
1897         return HC_ERR_IPC_BUILD_PARAM;
1898     }
1899     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_SERVICE_ID, (const uint8_t *)serviceId, HcStrlen(serviceId) + 1);
1900     if (ret != HC_SUCCESS) {
1901         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_SERVICE_ID);
1902         DestroyCallCtx(&callCtx, NULL);
1903         return HC_ERR_IPC_BUILD_PARAM;
1904     }
1905     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_RANDOM, (const uint8_t *)random->data, random->length);
1906     if (ret != HC_SUCCESS) {
1907         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_RANDOM);
1908         DestroyCallCtx(&callCtx, NULL);
1909         return HC_ERR_IPC_BUILD_PARAM;
1910     }
1911     ret = DoBinderCall(callCtx, IPC_CALL_ID_AV_GET_SERVER_SHARED_KEY, true);
1912     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1913         DestroyCallCtx(&callCtx, NULL);
1914         return HC_ERR_IPC_PROC_FAILED;
1915     }
1916     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = { { 0 } };
1917     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1918     ret = HC_ERR_IPC_UNKNOW_REPLY;
1919     int32_t inOutLen = sizeof(int32_t);
1920     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1921     if (ret != HC_SUCCESS) {
1922         DestroyCallCtx(&callCtx, NULL);
1923         return ret;
1924     }
1925     ret = GetSharedKey(replyCache, REPLAY_CACHE_NUM(replyCache), returnSharedKey);
1926     DestroyCallCtx(&callCtx, NULL);
1927     return ret;
1928 }
1929 
InitIpcAccountVerifierMethods(AccountVerifier * accountVerifier)1930 static void InitIpcAccountVerifierMethods(AccountVerifier *accountVerifier)
1931 {
1932     accountVerifier->getClientSharedKey = IpcAvGetClientSharedKey;
1933     accountVerifier->getServerSharedKey = IpcAvGetServerSharedKey;
1934     accountVerifier->destroyDataBuff = IpcAvDestroyDataBuff;
1935 }
1936 
ProcessCredential(int32_t operationCode,const char * reqJsonStr,char ** returnData)1937 DEVICE_AUTH_API_PUBLIC int32_t ProcessCredential(int32_t operationCode, const char *reqJsonStr, char **returnData)
1938 {
1939     uintptr_t callCtx = IPC_CALL_CONTEXT_INIT;
1940     int32_t ret;
1941     IpcDataInfo replyCache = { 0 };
1942 
1943     LOGI("starting ...");
1944     if (IsStrInvalid(reqJsonStr) || (returnData == NULL)) {
1945         LOGE("Invalid params.");
1946         return HC_ERR_INVALID_PARAMS;
1947     }
1948     ret = CreateCallCtx(&callCtx, NULL);
1949     if (ret != HC_SUCCESS) {
1950         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1951         return HC_ERR_IPC_INIT;
1952     }
1953     ret = SetCallRequestParamInfo(
1954         callCtx, PARAM_TYPE_OPCODE, (const uint8_t *)&operationCode, sizeof(operationCode));
1955     if (ret != HC_SUCCESS) {
1956         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OPCODE);
1957         DestroyCallCtx(&callCtx, NULL);
1958         return HC_ERR_IPC_BUILD_PARAM;
1959     }
1960     ret =
1961         SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQ_JSON, (const uint8_t *)reqJsonStr, HcStrlen(reqJsonStr) + 1);
1962     if (ret != HC_SUCCESS) {
1963         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQ_JSON);
1964         DestroyCallCtx(&callCtx, NULL);
1965         return HC_ERR_IPC_BUILD_PARAM;
1966     }
1967     ret = DoBinderCall(callCtx, IPC_CALL_ID_PROCESS_CREDENTIAL, true);
1968     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1969         LOGE("ipc call failed");
1970         DestroyCallCtx(&callCtx, NULL);
1971         return HC_ERR_IPC_PROC_FAILED;
1972     }
1973     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1974     ret = GetIpcReplyByTypeInner(&replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
1975     if (ret != HC_SUCCESS) {
1976         LOGE("GetIpcReplyByType failed, ret %" LOG_PUB "d", ret);
1977     }
1978     DestroyCallCtx(&callCtx, NULL);
1979     return ret;
1980 }
1981 
ProcessAuthDevice(int64_t requestId,const char * authParams,const DeviceAuthCallback * callback)1982 DEVICE_AUTH_API_PUBLIC int32_t ProcessAuthDevice(
1983     int64_t requestId, const char *authParams, const DeviceAuthCallback *callback)
1984 {
1985     uintptr_t callCtx = IPC_CALL_CONTEXT_INIT;
1986     int32_t ret;
1987     int32_t inOutLen;
1988     IpcDataInfo replyCache = { 0 };
1989 
1990     LOGI("starting ...");
1991     if (IsStrInvalid(authParams) || (callback == NULL)) {
1992         LOGE("invalid params");
1993         return HC_ERR_INVALID_PARAMS;
1994     }
1995     ret = CreateCallCtx(&callCtx, NULL);
1996     if (ret != HC_SUCCESS) {
1997         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
1998         return HC_ERR_IPC_INIT;
1999     }
2000     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&requestId), sizeof(requestId));
2001     if (ret != HC_SUCCESS) {
2002         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
2003         DestroyCallCtx(&callCtx, NULL);
2004         return HC_ERR_IPC_BUILD_PARAM;
2005     }
2006     ret = SetCallRequestParamInfo(
2007         callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams, HcStrlen(authParams) + 1);
2008     if (ret != HC_SUCCESS) {
2009         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_AUTH_PARAMS);
2010         DestroyCallCtx(&callCtx, NULL);
2011         return HC_ERR_IPC_BUILD_PARAM;
2012     }
2013     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
2014     if (ret != HC_SUCCESS) {
2015         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
2016         DestroyCallCtx(&callCtx, NULL);
2017         return HC_ERR_IPC_BUILD_PARAM;
2018     }
2019     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_DIRECT_AUTH_ID);
2020     ret = DoBinderCall(callCtx, IPC_CALL_ID_DA_PROC_DATA, true);
2021     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
2022         LOGE("ipc call failed");
2023         DestroyCallCtx(&callCtx, NULL);
2024         return HC_ERR_IPC_PROC_FAILED;
2025     }
2026     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
2027     ret = HC_ERR_IPC_UNKNOW_REPLY;
2028     inOutLen = sizeof(int32_t);
2029     GetIpcReplyByType(
2030         &replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
2031     LOGI("process done, ret %" LOG_PUB "d", ret);
2032     DestroyCallCtx(&callCtx, NULL);
2033     return ret;
2034 }
2035 
StartAuthDevice(int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)2036 DEVICE_AUTH_API_PUBLIC int32_t StartAuthDevice(
2037     int64_t authReqId, const char *authParams, const DeviceAuthCallback *callback)
2038 {
2039     uintptr_t callCtx = IPC_CALL_CONTEXT_INIT;
2040     int32_t ret;
2041     int32_t inOutLen;
2042     IpcDataInfo replyCache = { 0 };
2043 
2044     LOGI("starting ...");
2045     if (IsStrInvalid(authParams) || (callback == NULL)) {
2046         LOGE("invalid params");
2047         return HC_ERR_INVALID_PARAMS;
2048     }
2049     ret = CreateCallCtx(&callCtx, NULL);
2050     if (ret != HC_SUCCESS) {
2051         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
2052         return HC_ERR_IPC_INIT;
2053     }
2054     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
2055     if (ret != HC_SUCCESS) {
2056         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
2057         DestroyCallCtx(&callCtx, NULL);
2058         return HC_ERR_IPC_BUILD_PARAM;
2059     }
2060     ret = SetCallRequestParamInfo(
2061         callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams, HcStrlen(authParams) + 1);
2062     if (ret != HC_SUCCESS) {
2063         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_AUTH_PARAMS);
2064         DestroyCallCtx(&callCtx, NULL);
2065         return HC_ERR_IPC_BUILD_PARAM;
2066     }
2067     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
2068     if (ret != HC_SUCCESS) {
2069         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
2070         DestroyCallCtx(&callCtx, NULL);
2071         return HC_ERR_IPC_BUILD_PARAM;
2072     }
2073     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_DIRECT_AUTH_ID);
2074     ret = DoBinderCall(callCtx, IPC_CALL_ID_DA_AUTH_DEVICE, true);
2075     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
2076         LOGE("ipc call failed");
2077         DestroyCallCtx(&callCtx, NULL);
2078         return HC_ERR_IPC_PROC_FAILED;
2079     }
2080     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
2081     ret = HC_ERR_IPC_UNKNOW_REPLY;
2082     inOutLen = sizeof(int32_t);
2083     GetIpcReplyByType(
2084         &replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
2085     LOGI("process done, ret %" LOG_PUB "d", ret);
2086     DestroyCallCtx(&callCtx, NULL);
2087     return ret;
2088 }
2089 
CancelAuthRequest(int64_t requestId,const char * authParams)2090 DEVICE_AUTH_API_PUBLIC int32_t CancelAuthRequest(int64_t requestId, const char *authParams)
2091 {
2092     uintptr_t callCtx = IPC_CALL_CONTEXT_INIT;
2093     int32_t ret;
2094     int32_t inOutLen;
2095     IpcDataInfo replyCache = { 0 };
2096 
2097     LOGI("starting ...");
2098     if (IsStrInvalid(authParams)) {
2099         LOGE("Invalid params.");
2100         return HC_ERR_INVALID_PARAMS;
2101     }
2102     ret = CreateCallCtx(&callCtx, NULL);
2103     if (ret != HC_SUCCESS) {
2104         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
2105         return HC_ERR_NULL_PTR;
2106     }
2107     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&requestId), sizeof(requestId));
2108     if (ret != HC_SUCCESS) {
2109         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
2110         DestroyCallCtx(&callCtx, NULL);
2111         return HC_ERR_NULL_PTR;
2112     }
2113     ret = SetCallRequestParamInfo(
2114         callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams, HcStrlen(authParams) + 1);
2115     if (ret != HC_SUCCESS) {
2116         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_AUTH_PARAMS);
2117         DestroyCallCtx(&callCtx, NULL);
2118         return HC_ERR_NULL_PTR;
2119     }
2120     ret = DoBinderCall(callCtx, IPC_CALL_ID_DA_CANCEL_REQUEST, true);
2121     if (ret != HC_SUCCESS) {
2122         LOGE("ipc call failed");
2123         DestroyCallCtx(&callCtx, NULL);
2124         return HC_ERR_IPC_PROC_FAILED;
2125     }
2126     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
2127     ret = HC_ERR_IPC_UNKNOW_REPLY;
2128     inOutLen = sizeof(int32_t);
2129     GetIpcReplyByType(
2130         &replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
2131     LOGI("process done, ret %" LOG_PUB "d", ret);
2132     DestroyCallCtx(&callCtx, NULL);
2133     return ret;
2134 }
2135 
InitDeviceAuthService(void)2136 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
2137 {
2138     InitHcMutex(&g_ipcMutex, false);
2139 #ifdef DEV_AUTH_IS_ENABLE
2140     InitISIpc();
2141 #endif
2142     return InitProxyAdapt();
2143 }
2144 
DestroyDeviceAuthService(void)2145 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
2146 {
2147     UnInitProxyAdapt();
2148     DestroyHcMutex(&g_ipcMutex);
2149 #ifdef DEV_AUTH_IS_ENABLE
2150     DeInitISIpc();
2151 #endif
2152 }
2153 
GetGaInstance(void)2154 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
2155 {
2156     static GroupAuthManager gaInstCtx;
2157     static GroupAuthManager *gaInstPtr = NULL;
2158 
2159     if (gaInstPtr == NULL) {
2160         InitIpcGaMethods(&gaInstCtx);
2161         gaInstPtr = &gaInstCtx;
2162     }
2163     return (const GroupAuthManager *)(gaInstPtr);
2164 }
2165 
GetGmInstance(void)2166 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
2167 {
2168     static DeviceGroupManager gmInstCtx;
2169     static DeviceGroupManager *gmInstPtr = NULL;
2170 
2171     if (gmInstPtr == NULL) {
2172         InitIpcGmMethods(&gmInstCtx);
2173         gmInstPtr = &gmInstCtx;
2174     }
2175     return (const DeviceGroupManager *)(gmInstPtr);
2176 }
2177 
GetAccountVerifierInstance(void)2178 DEVICE_AUTH_API_PUBLIC const AccountVerifier *GetAccountVerifierInstance(void)
2179 {
2180     static AccountVerifier avInstCtx;
2181     static AccountVerifier *avInstPtr = NULL;
2182     InitIpcAccountVerifierMethods(&avInstCtx);
2183     avInstPtr = &avInstCtx;
2184     return (const AccountVerifier *)(avInstPtr);
2185 }
2186 
2187 #ifdef __cplusplus
2188 }
2189 #endif
2190