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