• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_3 3
32 #define IPC_DATA_CACHES_4 4
33 #define REPLAY_CACHE_NUM(caches) (sizeof(caches) / sizeof(IpcDataInfo))
34 #define IPC_APPID_LEN 128
35 
36 #define IS_STRING_VALID(str) (((str) != NULL) && ((str)[0] != 0))
37 #define IS_COMM_DATA_VALID(dPtr, dLen) (((dPtr) != NULL) && ((dLen) > 0) && ((dLen) <= 4096))
38 
39 typedef struct {
40     uintptr_t inst;
41     char appId[IPC_APPID_LEN];
42 } IpcProxyCbInfo;
43 static IpcProxyCbInfo g_ipcProxyCbList = {0};
44 static IpcProxyCbInfo g_ipcListenerCbList = {0};
45 static HcMutex g_ipcMutex;
46 
DelIpcCliCallbackCtx(const char * appId,IpcProxyCbInfo * cbCache)47 static void DelIpcCliCallbackCtx(const char *appId, IpcProxyCbInfo *cbCache)
48 {
49     int32_t ret;
50 
51     if (cbCache->appId[0] == 0) {
52         return;
53     }
54     g_ipcMutex.lock(&g_ipcMutex);
55     ret = memcmp(appId, cbCache->appId, strlen(cbCache->appId) + 1);
56     if (ret == 0) {
57         cbCache->appId[0] = 0;
58     }
59     g_ipcMutex.unlock(&g_ipcMutex);
60     return;
61 }
62 
AddIpcCliCallbackCtx(const char * appId,uintptr_t cbInst,IpcProxyCbInfo * cbCache)63 static void AddIpcCliCallbackCtx(const char *appId, uintptr_t cbInst, IpcProxyCbInfo *cbCache)
64 {
65     errno_t eno;
66 
67     LOGI("starting ...");
68     g_ipcMutex.lock(&g_ipcMutex);
69     eno = memcpy_s(cbCache->appId, IPC_APPID_LEN, appId, strlen(appId) + 1);
70     if (eno != EOK) {
71         g_ipcMutex.unlock(&g_ipcMutex);
72         LOGE("memory copy failed");
73         return;
74     }
75     cbCache->inst = cbInst;
76     g_ipcMutex.unlock(&g_ipcMutex);
77     LOGI("success, appid: %s", appId);
78     return;
79 }
80 
GetIpcReplyByType(const IpcDataInfo * ipcData,int32_t dataNum,int32_t type,uint8_t * outCache,int32_t * cacheLen)81 static void GetIpcReplyByType(const IpcDataInfo *ipcData,
82     int32_t dataNum, int32_t type, uint8_t *outCache, int32_t *cacheLen)
83 {
84     int32_t i;
85     int32_t ret = HC_ERR_IPC_BAD_MSG_TYPE;
86     errno_t eno;
87 
88     LOGI("type %d", type);
89     for (i = 0; i < dataNum; i++) {
90         if (ipcData[i].type != type) {
91             continue;
92         }
93         ret = HC_SUCCESS;
94         switch (type) {
95             case PARAM_TYPE_REG_INFO:
96             case PARAM_TYPE_MGR_APPID:
97             case PARAM_TYPE_FRIEND_APPID:
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                 if (cacheLen != NULL) {
103                     *cacheLen = ipcData[i].valSz;
104                 }
105                 break;
106             case PARAM_TYPE_IPC_RESULT:
107             case PARAM_TYPE_IPC_RESULT_NUM:
108             case PARAM_TYPE_COMM_DATA:
109             case PARAM_TYPE_DATA_NUM:
110                 eno = memcpy_s(outCache, *cacheLen, ipcData[i].val, ipcData[i].valSz);
111                 if (eno != EOK) {
112                     ret = HC_ERR_MEMORY_COPY;
113                     break;
114                 }
115                 *cacheLen = ipcData[i].valSz;
116                 break;
117             default:
118                 ret = HC_ERR_IPC_BAD_MSG_TYPE;
119                 LOGE("un-expectation type case");
120                 break;
121         }
122     }
123     LOGI("process done, type %d, result %d", type, ret);
124     return;
125 }
126 
IpcGmRegCallback(const char * appId,const DeviceAuthCallback * callback)127 static int32_t IpcGmRegCallback(const char *appId, const DeviceAuthCallback *callback)
128 {
129     uintptr_t callCtx = 0x0;
130     int32_t ret;
131 
132     LOGI("starting ...");
133     if (!IS_STRING_VALID(appId)) {
134         LOGE("invalid params");
135         return HC_ERR_INVALID_PARAMS;
136     }
137     if (!IsServiceRunning()) {
138         LOGE("service is not activity");
139         return HC_ERROR;
140     }
141     ret = CreateCallCtx(&callCtx, NULL);
142     if (ret != HC_SUCCESS) {
143         LOGE("CreateCallCtx failed, ret %d", ret);
144         return HC_ERR_IPC_INIT;
145     }
146 
147     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
148     if (ret != HC_SUCCESS) {
149         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_APPID);
150         DestroyCallCtx(&callCtx, NULL);
151         return HC_ERR_IPC_BUILD_PARAM;
152     }
153     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
154     if (ret != HC_SUCCESS) {
155         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_DEV_AUTH_CB);
156         DestroyCallCtx(&callCtx, NULL);
157         return HC_ERR_IPC_BUILD_PARAM;
158     }
159     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_BIND_ID);
160     ret = DoBinderCall(callCtx, IPC_CALL_ID_REG_CB, true);
161     if (ret == HC_SUCCESS) {
162         AddIpcCliCallbackCtx(appId, 0, &g_ipcProxyCbList);
163     }
164     DestroyCallCtx(&callCtx, NULL);
165     LOGI("process done, ret %d", ret);
166     return (ret == HC_SUCCESS) ? HC_SUCCESS : HC_ERR_IPC_PROC_FAILED;
167 }
168 
IpcGmUnRegCallback(const char * appId)169 static int32_t IpcGmUnRegCallback(const char *appId)
170 {
171     uintptr_t callCtx = 0x0;
172     int32_t ret;
173 
174     LOGI("starting ...");
175     if (!IS_STRING_VALID(appId)) {
176         return HC_ERR_INVALID_PARAMS;
177     }
178     if (!IsServiceRunning()) {
179         LOGE("service is not activity");
180         return HC_ERROR;
181     }
182     ret = CreateCallCtx(&callCtx, NULL);
183     if (ret != HC_SUCCESS) {
184         LOGE("CreateCallCtx failed, ret %d", ret);
185         return HC_ERR_IPC_INIT;
186     }
187 
188     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
189     if (ret != HC_SUCCESS) {
190         LOGE("set request param failed, ret %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 %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 (!IS_STRING_VALID(appId) || (listener == NULL)) {
210         LOGE("invalid params");
211         return HC_ERR_INVALID_PARAMS;
212     }
213     if (!IsServiceRunning()) {
214         LOGE("service is not activity");
215         return HC_ERROR;
216     }
217     ret = CreateCallCtx(&callCtx, NULL);
218     if (ret != HC_SUCCESS) {
219         LOGE("CreateCallCtx failed, ret %d", ret);
220         return HC_ERR_IPC_INIT;
221     }
222 
223     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
224     if (ret != HC_SUCCESS) {
225         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_APPID);
226         DestroyCallCtx(&callCtx, NULL);
227         return HC_ERR_IPC_BUILD_PARAM;
228     }
229     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_LISTERNER, (const uint8_t *)listener, sizeof(*listener));
230     if (ret != HC_SUCCESS) {
231         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_LISTERNER);
232         DestroyCallCtx(&callCtx, NULL);
233         return HC_ERR_IPC_BUILD_PARAM;
234     }
235     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_BIND_ID);
236     ret = DoBinderCall(callCtx, IPC_CALL_ID_REG_LISTENER, true);
237     if (ret == HC_SUCCESS) {
238         AddIpcCliCallbackCtx(appId, 0, &g_ipcListenerCbList);
239     }
240     DestroyCallCtx(&callCtx, NULL);
241     LOGI("process done, ret %d", ret);
242     return (ret == HC_SUCCESS) ? HC_SUCCESS : HC_ERR_IPC_PROC_FAILED;
243 }
244 
IpcGmUnRegDataChangeListener(const char * appId)245 static int32_t IpcGmUnRegDataChangeListener(const char *appId)
246 {
247     uintptr_t callCtx = 0x0;
248     int32_t ret;
249 
250     LOGI("starting ...");
251     if (!IS_STRING_VALID(appId)) {
252         return HC_ERR_INVALID_PARAMS;
253     }
254     if (!IsServiceRunning()) {
255         LOGE("service is not activity");
256         return HC_ERROR;
257     }
258     ret = CreateCallCtx(&callCtx, NULL);
259     if (ret != HC_SUCCESS) {
260         LOGE("CreateCallCtx failed, ret %d", ret);
261         return HC_ERR_IPC_INIT;
262     }
263 
264     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
265     if (ret != HC_SUCCESS) {
266         LOGE("set request param failed, ret %d", ret);
267         DestroyCallCtx(&callCtx, NULL);
268         return HC_ERR_IPC_BUILD_PARAM;
269     }
270     ret = DoBinderCall(callCtx, IPC_CALL_ID_UNREG_LISTENER, true);
271     if (ret == HC_SUCCESS) {
272         DelIpcCliCallbackCtx(appId, &g_ipcListenerCbList);
273     }
274     DestroyCallCtx(&callCtx, NULL);
275     LOGI("process done");
276     return HC_SUCCESS;
277 }
278 
IpcGmCreateGroup(int32_t osAccountId,int64_t requestId,const char * appid,const char * createParams)279 static int32_t IpcGmCreateGroup(int32_t osAccountId, int64_t requestId, const char *appid, const char *createParams)
280 {
281     uintptr_t callCtx = 0x0;
282     int32_t ret;
283     int32_t inOutLen;
284     IpcDataInfo replyCache = {0};
285 
286     LOGI("starting ...");
287     if (!IS_STRING_VALID(createParams) || !IS_STRING_VALID(appid)) {
288         return HC_ERR_INVALID_PARAMS;
289     }
290     if (!IsServiceRunning()) {
291         LOGE("service is not activity");
292         return HC_ERROR;
293     }
294     ret = CreateCallCtx(&callCtx, NULL);
295     if (ret != HC_SUCCESS) {
296         LOGE("CreateCallCtx failed, ret %d", ret);
297         return HC_ERR_IPC_INIT;
298     }
299     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
300         sizeof(osAccountId));
301     if (ret != HC_SUCCESS) {
302         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
303         DestroyCallCtx(&callCtx, NULL);
304         return HC_ERR_IPC_BUILD_PARAM;
305     }
306     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
307     if (ret != HC_SUCCESS) {
308         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_REQID);
309         DestroyCallCtx(&callCtx, NULL);
310         return HC_ERR_IPC_BUILD_PARAM;
311     }
312     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appid, strlen(appid) + 1);
313     if (ret != HC_SUCCESS) {
314         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
315         DestroyCallCtx(&callCtx, NULL);
316         return HC_ERR_IPC_BUILD_PARAM;
317     }
318     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CREATE_PARAMS,
319                                   (const uint8_t *)createParams, strlen(createParams) + 1);
320     if (ret != HC_SUCCESS) {
321         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_CREATE_PARAMS);
322         DestroyCallCtx(&callCtx, NULL);
323         return HC_ERR_IPC_BUILD_PARAM;
324     }
325     ret = DoBinderCall(callCtx, IPC_CALL_ID_CREATE_GROUP, true);
326     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
327         LOGE("ipc call failed");
328         DestroyCallCtx(&callCtx, NULL);
329         return HC_ERR_IPC_PROC_FAILED;
330     }
331     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
332     ret = HC_ERR_IPC_UNKNOW_REPLY;
333     inOutLen = sizeof(int32_t);
334     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
335     DestroyCallCtx(&callCtx, NULL);
336     LOGI("process done, ret %d", ret);
337     return ret;
338 }
339 
IpcGmDelGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)340 static int32_t IpcGmDelGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *delParams)
341 {
342     uintptr_t callCtx = 0x0;
343     int32_t ret;
344     int32_t inOutLen;
345     IpcDataInfo replyCache = {0};
346 
347     LOGI("starting ...");
348     if (!IS_STRING_VALID(delParams) || !IS_STRING_VALID(appId)) {
349         return HC_ERR_INVALID_PARAMS;
350     }
351     if (!IsServiceRunning()) {
352         LOGE("service is not activity");
353         return HC_ERROR;
354     }
355     ret = CreateCallCtx(&callCtx, NULL);
356     if (ret != HC_SUCCESS) {
357         LOGE("CreateCallCtx failed, ret %d", ret);
358         return HC_ERR_IPC_INIT;
359     }
360 
361     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
362         sizeof(osAccountId));
363     if (ret != HC_SUCCESS) {
364         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
365         DestroyCallCtx(&callCtx, NULL);
366         return HC_ERR_IPC_BUILD_PARAM;
367     }
368     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
369     if (ret != HC_SUCCESS) {
370         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_REQID);
371         DestroyCallCtx(&callCtx, NULL);
372         return HC_ERR_IPC_BUILD_PARAM;
373     }
374     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEL_PARAMS, (const uint8_t *)delParams, strlen(delParams) + 1);
375     if (ret != HC_SUCCESS) {
376         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_DEL_PARAMS);
377         DestroyCallCtx(&callCtx, NULL);
378         return HC_ERR_IPC_BUILD_PARAM;
379     }
380     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
381     if (ret != HC_SUCCESS) {
382         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
383         DestroyCallCtx(&callCtx, NULL);
384         return HC_ERR_IPC_BUILD_PARAM;
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 %d", ret);
398     return ret;
399 }
400 
IpcGmAddMemberToGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * addParams)401 static int32_t IpcGmAddMemberToGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams)
402 {
403     uintptr_t callCtx = 0x0;
404     int32_t ret;
405     int32_t inOutLen;
406     IpcDataInfo replyCache = {0};
407 
408     LOGI("starting ...");
409     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(addParams)) {
410         return HC_ERR_INVALID_PARAMS;
411     }
412     if (!IsServiceRunning()) {
413         LOGE("service is not activity");
414         return HC_ERROR;
415     }
416     ret = CreateCallCtx(&callCtx, NULL);
417     if (ret != HC_SUCCESS) {
418         LOGE("CreateCallCtx failed, ret %d", ret);
419         return HC_ERR_IPC_INIT;
420     }
421     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
422         sizeof(osAccountId));
423     if (ret != HC_SUCCESS) {
424         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
425         DestroyCallCtx(&callCtx, NULL);
426         return HC_ERR_IPC_BUILD_PARAM;
427     }
428     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
429     if (ret != HC_SUCCESS) {
430         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_REQID);
431         DestroyCallCtx(&callCtx, NULL);
432         return HC_ERR_IPC_BUILD_PARAM;
433     }
434     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
435     if (ret != HC_SUCCESS) {
436         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
437         DestroyCallCtx(&callCtx, NULL);
438         return HC_ERR_IPC_BUILD_PARAM;
439     }
440     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_ADD_PARAMS, (const uint8_t *)addParams, strlen(addParams) + 1);
441     if (ret != HC_SUCCESS) {
442         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_ADD_PARAMS);
443         DestroyCallCtx(&callCtx, NULL);
444         return HC_ERR_IPC_BUILD_PARAM;
445     }
446     ret = DoBinderCall(callCtx, IPC_CALL_ID_ADD_GROUP_MEMBER, true);
447     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
448         LOGE("ipc call failed");
449         DestroyCallCtx(&callCtx, NULL);
450         return HC_ERR_IPC_PROC_FAILED;
451     }
452     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
453     ret = HC_ERR_IPC_UNKNOW_REPLY;
454     inOutLen = sizeof(int32_t);
455     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
456     DestroyCallCtx(&callCtx, NULL);
457     LOGI("process done, ret %d", ret);
458     return ret;
459 }
460 
IpcGmDelMemberFromGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * delParams)461 static int32_t IpcGmDelMemberFromGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *delParams)
462 {
463     uintptr_t callCtx = 0x0;
464     int32_t ret;
465     int32_t inOutLen;
466     IpcDataInfo replyCache = {0};
467 
468     LOGI("starting ...");
469     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(delParams)) {
470         return HC_ERR_INVALID_PARAMS;
471     }
472     if (!IsServiceRunning()) {
473         LOGE("service is not activity");
474         return HC_ERROR;
475     }
476     ret = CreateCallCtx(&callCtx, NULL);
477     if (ret != HC_SUCCESS) {
478         LOGE("CreateCallCtx failed, ret %d", ret);
479         return HC_ERR_IPC_INIT;
480     }
481     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
482         sizeof(osAccountId));
483     if (ret != HC_SUCCESS) {
484         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
485         DestroyCallCtx(&callCtx, NULL);
486         return HC_ERR_IPC_BUILD_PARAM;
487     }
488     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
489     if (ret != HC_SUCCESS) {
490         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_REQID);
491         DestroyCallCtx(&callCtx, NULL);
492         return HC_ERR_IPC_BUILD_PARAM;
493     }
494     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
495     if (ret != HC_SUCCESS) {
496         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
497         DestroyCallCtx(&callCtx, NULL);
498         return HC_ERR_IPC_BUILD_PARAM;
499     }
500     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEL_PARAMS, (const uint8_t *)delParams, strlen(delParams) + 1);
501     if (ret != HC_SUCCESS) {
502         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_DEL_PARAMS);
503         DestroyCallCtx(&callCtx, NULL);
504         return HC_ERR_IPC_BUILD_PARAM;
505     }
506     ret = DoBinderCall(callCtx, IPC_CALL_ID_DEL_GROUP_MEMBER, true);
507     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
508         LOGE("ipc call failed");
509         DestroyCallCtx(&callCtx, NULL);
510         return HC_ERR_IPC_PROC_FAILED;
511     }
512     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
513     ret = HC_ERR_IPC_UNKNOW_REPLY;
514     inOutLen = sizeof(int32_t);
515     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
516     DestroyCallCtx(&callCtx, NULL);
517     LOGI("process done, ret %d", ret);
518     return ret;
519 }
520 
IpcGmProcessData(int64_t requestId,const uint8_t * data,uint32_t dataLen)521 static int32_t IpcGmProcessData(int64_t requestId, const uint8_t *data, uint32_t dataLen)
522 {
523     uintptr_t callCtx = 0x0;
524     int32_t ret;
525     int32_t inOutLen;
526     IpcDataInfo replyCache = {0};
527 
528     LOGI("starting ...");
529     if (!IS_COMM_DATA_VALID(data, dataLen)) {
530         return HC_ERR_INVALID_PARAMS;
531     }
532     if (!IsServiceRunning()) {
533         LOGE("service is not activity");
534         return HC_ERROR;
535     }
536     ret = CreateCallCtx(&callCtx, NULL);
537     if (ret != HC_SUCCESS) {
538         LOGE("CreateCallCtx failed, ret %d", ret);
539         return HC_ERR_IPC_INIT;
540     }
541     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)&requestId, sizeof(requestId));
542     if (ret != HC_SUCCESS) {
543         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_REQID);
544         DestroyCallCtx(&callCtx, NULL);
545         return HC_ERR_IPC_BUILD_PARAM;
546     }
547     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_COMM_DATA, data, dataLen);
548     if (ret != HC_SUCCESS) {
549         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_COMM_DATA);
550         DestroyCallCtx(&callCtx, NULL);
551         return HC_ERR_IPC_BUILD_PARAM;
552     }
553     ret = DoBinderCall(callCtx, IPC_CALL_ID_GM_PROC_DATA, true);
554     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
555         LOGE("ipc call failed");
556         DestroyCallCtx(&callCtx, NULL);
557         return HC_ERR_IPC_PROC_FAILED;
558     }
559     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
560     ret = HC_ERR_IPC_UNKNOW_REPLY;
561     inOutLen = sizeof(int32_t);
562     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
563     DestroyCallCtx(&callCtx, NULL);
564     LOGI("process done, ret %d", ret);
565     return ret;
566 }
567 
IpcGmProcCredential(int32_t operationCode,const char * credential,char ** returnJsonStr)568 static int32_t IpcGmProcCredential(int32_t operationCode, const char *credential, char **returnJsonStr)
569 {
570     LOGI("starting ...");
571     (void)operationCode;
572     (void)returnJsonStr;
573     if (!IS_STRING_VALID(credential)) {
574         LOGE("The input groupType is invalid!");
575         return HC_ERR_INVALID_PARAMS;
576     }
577 
578     LOGE("IpcGmProcCredential is currently not supported!");
579     return HC_ERR_NOT_SUPPORT;
580 }
581 
IpcGmGetRegisterInfo(char ** registerInfo)582 static int32_t IpcGmGetRegisterInfo(char **registerInfo)
583 {
584     uintptr_t callCtx = 0x0;
585     int32_t ret;
586     int32_t inOutLen;
587     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = {{0}};
588     char *outInfo = NULL;
589 
590     LOGI("starting ...");
591     if (registerInfo == NULL) {
592         return HC_ERR_INVALID_PARAMS;
593     }
594     if (!IsServiceRunning()) {
595         LOGE("service is not activity");
596         return HC_ERROR;
597     }
598     ret = CreateCallCtx(&callCtx, NULL);
599     if (ret != HC_SUCCESS) {
600         LOGE("CreateCallCtx failed, ret %d", ret);
601         return HC_ERR_IPC_INIT;
602     }
603     ret = DoBinderCall(callCtx, IPC_CALL_ID_APPLY_REG_INFO, true);
604     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
605         LOGE("ipc call failed");
606         DestroyCallCtx(&callCtx, NULL);
607         return HC_ERR_IPC_PROC_FAILED;
608     }
609     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
610     ret = HC_ERR_IPC_UNKNOW_REPLY;
611     inOutLen = sizeof(int32_t);
612     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
613     LOGI("process done, ret %d", ret);
614     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
615         DestroyCallCtx(&callCtx, NULL);
616         return HC_ERR_IPC_BAD_PARAM;
617     }
618     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
619     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
620         LOGE("done, ret %d", HC_ERR_IPC_OUT_DATA_NUM);
621         DestroyCallCtx(&callCtx, NULL);
622         return HC_ERR_IPC_OUT_DATA_NUM;
623     }
624     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_REG_INFO, (uint8_t *)&outInfo, NULL);
625     if ((outInfo == NULL) || (strlen(outInfo) == 0)) {
626         LOGE("done, ret %d", HC_ERR_IPC_OUT_DATA);
627         DestroyCallCtx(&callCtx, NULL);
628         return HC_ERR_IPC_OUT_DATA;
629     }
630     *registerInfo = strdup(outInfo);
631     DestroyCallCtx(&callCtx, NULL);
632     return (*registerInfo != NULL) ? HC_SUCCESS : HC_ERR_NULL_PTR;
633 }
634 
IpcGmCheckAccessToGroup(int32_t osAccountId,const char * appId,const char * groupId)635 static int32_t IpcGmCheckAccessToGroup(int32_t osAccountId, const char *appId, const char *groupId)
636 {
637     uintptr_t callCtx = 0x0;
638     int32_t ret;
639     int32_t inOutLen;
640     IpcDataInfo replyCache = {0};
641 
642     LOGI("starting ...");
643     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(groupId)) {
644         return HC_ERR_INVALID_PARAMS;
645     }
646     if (!IsServiceRunning()) {
647         LOGE("service is not activity");
648         return HC_ERROR;
649     }
650     ret = CreateCallCtx(&callCtx, NULL);
651     if (ret != HC_SUCCESS) {
652         LOGE("CreateCallCtx failed, ret %d", ret);
653         return HC_ERR_IPC_INIT;
654     }
655     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
656         sizeof(osAccountId));
657     if (ret != HC_SUCCESS) {
658         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
659         DestroyCallCtx(&callCtx, NULL);
660         return HC_ERR_IPC_BUILD_PARAM;
661     }
662     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
663     if (ret != HC_SUCCESS) {
664         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
665         DestroyCallCtx(&callCtx, NULL);
666         return HC_ERR_IPC_BUILD_PARAM;
667     }
668     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, strlen(groupId) + 1);
669     if (ret != HC_SUCCESS) {
670         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUPID);
671         DestroyCallCtx(&callCtx, NULL);
672         return HC_ERR_IPC_BUILD_PARAM;
673     }
674     ret = DoBinderCall(callCtx, IPC_CALL_ID_CHECK_ACCESS_TO_GROUP, true);
675     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
676         LOGE("ipc call failed");
677         DestroyCallCtx(&callCtx, NULL);
678         return HC_ERR_IPC_PROC_FAILED;
679     }
680     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
681     ret = HC_ERR_IPC_UNKNOW_REPLY;
682     inOutLen = sizeof(int32_t);
683     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
684     LOGI("process done, ret %d", ret);
685     DestroyCallCtx(&callCtx, NULL);
686     return ret;
687 }
688 
ParseReturnResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData,uint32_t * returnNum)689 static int32_t ParseReturnResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData, uint32_t *returnNum)
690 {
691     int32_t ret;
692     int32_t inOutLen;
693 
694     inOutLen = sizeof(int32_t);
695     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
696     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
697         return HC_ERR_IPC_OUT_DATA_NUM;
698     }
699     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_RETURN_DATA, (uint8_t *)returnData, NULL);
700     if (*returnData == NULL) {
701         return HC_ERR_IPC_OUT_DATA;
702     }
703     *returnData = strdup(*returnData);
704     if (*returnData == NULL) {
705         return HC_ERR_ALLOC_MEMORY;
706     }
707     inOutLen = sizeof(int32_t);
708     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)returnNum, &inOutLen);
709     return HC_SUCCESS;
710 }
711 
IpcGmGetPkInfoList(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnInfoList,uint32_t * returnInfoNum)712 static int32_t IpcGmGetPkInfoList(int32_t osAccountId, const char *appId, const char *queryParams,
713                                   char **returnInfoList, uint32_t *returnInfoNum)
714 {
715     uintptr_t callCtx = 0x0;
716     int32_t ret;
717     int32_t inOutLen;
718     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = {{0}};
719 
720     LOGI("starting ...");
721     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(queryParams) ||
722         (returnInfoList == NULL) || (returnInfoNum == NULL)) {
723         return HC_ERR_INVALID_PARAMS;
724     }
725     if (!IsServiceRunning()) {
726         LOGE("service is not activity");
727         return HC_ERROR;
728     }
729     ret = CreateCallCtx(&callCtx, NULL);
730     if (ret != HC_SUCCESS) {
731         LOGE("CreateCallCtx failed, ret %d", ret);
732         return HC_ERR_IPC_INIT;
733     }
734     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
735                                   sizeof(osAccountId));
736     if (ret != HC_SUCCESS) {
737         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
738         DestroyCallCtx(&callCtx, NULL);
739         return HC_ERR_IPC_BUILD_PARAM;
740     }
741     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
742     if (ret != HC_SUCCESS) {
743         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
744         DestroyCallCtx(&callCtx, NULL);
745         return HC_ERR_IPC_BUILD_PARAM;
746     }
747     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_QUERY_PARAMS,
748                                   (const uint8_t *)queryParams, strlen(queryParams) + 1);
749     if (ret != HC_SUCCESS) {
750         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_QUERY_PARAMS);
751         DestroyCallCtx(&callCtx, NULL);
752         return HC_ERR_IPC_BUILD_PARAM;
753     }
754     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_PK_INFO_LIST, true);
755     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
756         LOGE("ipc call failed");
757         DestroyCallCtx(&callCtx, NULL);
758         return HC_ERR_IPC_PROC_FAILED;
759     }
760     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
761     ret = HC_ERR_IPC_UNKNOW_REPLY;
762     inOutLen = sizeof(int32_t);
763     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
764     LOGI("process done, ret %d", ret);
765     if (ret != HC_SUCCESS) {
766         DestroyCallCtx(&callCtx, NULL);
767         return ret;
768     }
769     ret = ParseReturnResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnInfoList, returnInfoNum);
770     LOGI("proc result done, ret %d", ret);
771     DestroyCallCtx(&callCtx, NULL);
772     return ret;
773 }
774 
GroupInfoIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupInfo)775 static int32_t GroupInfoIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outGroupInfo)
776 {
777     int32_t inOutLen;
778     int32_t ret;
779 
780     inOutLen = sizeof(int32_t);
781     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
782     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
783         return HC_ERR_IPC_OUT_DATA_NUM;
784     }
785     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupInfo, NULL);
786     if (*outGroupInfo == NULL) {
787         return HC_ERR_IPC_OUT_DATA;
788     }
789     *outGroupInfo = strdup(*outGroupInfo);
790     if (*outGroupInfo == NULL) {
791         return HC_ERR_ALLOC_MEMORY;
792     }
793     return HC_SUCCESS;
794 }
795 
IpcGmGetGroupInfoById(int32_t osAccountId,const char * appId,const char * groupId,char ** outGroupInfo)796 static int32_t IpcGmGetGroupInfoById(int32_t osAccountId, const char *appId, const char *groupId, char **outGroupInfo)
797 {
798     uintptr_t callCtx = 0x0;
799     int32_t ret;
800     int32_t inOutLen;
801     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = {{0}};
802 
803     LOGI("starting ...");
804     if (!IS_STRING_VALID(groupId) || !IS_STRING_VALID(appId) || (outGroupInfo == NULL)) {
805         return HC_ERR_INVALID_PARAMS;
806     }
807     if (!IsServiceRunning()) {
808         LOGE("service is not activity");
809         return HC_ERROR;
810     }
811     ret = CreateCallCtx(&callCtx, NULL);
812     if (ret != HC_SUCCESS) {
813         LOGE("CreateCallCtx failed, ret %d", ret);
814         return HC_ERR_IPC_INIT;
815     }
816     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
817         sizeof(osAccountId));
818     if (ret != HC_SUCCESS) {
819         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
820         DestroyCallCtx(&callCtx, NULL);
821         return HC_ERR_IPC_BUILD_PARAM;
822     }
823     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
824     if (ret != HC_SUCCESS) {
825         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
826         DestroyCallCtx(&callCtx, NULL);
827         return HC_ERR_IPC_BUILD_PARAM;
828     }
829     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, strlen(groupId) + 1);
830     if (ret != HC_SUCCESS) {
831         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUPID);
832         DestroyCallCtx(&callCtx, NULL);
833         return HC_ERR_IPC_BUILD_PARAM;
834     }
835     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_GROUP_INFO, true);
836     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
837         LOGE("ipc call failed");
838         DestroyCallCtx(&callCtx, NULL);
839         return HC_ERR_IPC_PROC_FAILED;
840     }
841     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
842     ret = HC_ERR_IPC_UNKNOW_REPLY;
843     inOutLen = sizeof(int32_t);
844     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
845     LOGI("process done, ret %d", ret);
846     if (ret != HC_SUCCESS) {
847         DestroyCallCtx(&callCtx, NULL);
848         return ret;
849     }
850     ret = GroupInfoIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupInfo);
851     DestroyCallCtx(&callCtx, NULL);
852     LOGI("proc result done, ret %d", ret);
853     return ret;
854 }
855 
SearchGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)856 static int32_t SearchGroupsIpcResult(const IpcDataInfo *replies,
857     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
858 {
859     int32_t ret;
860     int32_t inOutLen;
861 
862     inOutLen = sizeof(int32_t);
863     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
864     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
865         return HC_ERR_IPC_OUT_DATA_NUM;
866     }
867     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
868     if (*outGroupVec == NULL) {
869         return HC_ERR_IPC_OUT_DATA;
870     }
871     *outGroupVec = strdup(*outGroupVec);
872     if (*outGroupVec == NULL) {
873         return HC_ERR_ALLOC_MEMORY;
874     }
875     inOutLen = sizeof(int32_t);
876     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
877     return HC_SUCCESS;
878 }
879 
IpcGmGetGroupInfo(int32_t osAccountId,const char * appId,const char * queryParams,char ** outGroupVec,uint32_t * groupNum)880 static int32_t IpcGmGetGroupInfo(int32_t osAccountId, const char *appId, const char *queryParams,
881     char **outGroupVec, uint32_t *groupNum)
882 {
883     uintptr_t callCtx = 0x0;
884     int32_t ret;
885     int32_t inOutLen;
886     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = {{0}};
887 
888     LOGI("starting ...");
889     if (!IS_STRING_VALID(queryParams) || !IS_STRING_VALID(appId) || (outGroupVec == NULL) || (groupNum == NULL)) {
890         return HC_ERR_INVALID_PARAMS;
891     }
892     if (!IsServiceRunning()) {
893         LOGE("service is not activity");
894         return HC_ERROR;
895     }
896     ret = CreateCallCtx(&callCtx, NULL);
897     if (ret != HC_SUCCESS) {
898         LOGE("CreateCallCtx failed, ret %d", ret);
899         return HC_ERR_IPC_INIT;
900     }
901     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
902         sizeof(osAccountId));
903     if (ret != HC_SUCCESS) {
904         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
905         DestroyCallCtx(&callCtx, NULL);
906         return HC_ERR_IPC_BUILD_PARAM;
907     }
908     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
909     if (ret != HC_SUCCESS) {
910         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
911         DestroyCallCtx(&callCtx, NULL);
912         return HC_ERR_IPC_BUILD_PARAM;
913     }
914     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_QUERY_PARAMS,
915                                   (const uint8_t *)queryParams, strlen(queryParams) + 1);
916     if (ret != HC_SUCCESS) {
917         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_QUERY_PARAMS);
918         DestroyCallCtx(&callCtx, NULL);
919         return HC_ERR_IPC_BUILD_PARAM;
920     }
921     ret = DoBinderCall(callCtx, IPC_CALL_ID_SEARCH_GROUPS, true);
922     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
923         LOGE("ipc call failed");
924         DestroyCallCtx(&callCtx, NULL);
925         return HC_ERR_IPC_PROC_FAILED;
926     }
927     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
928     ret = HC_ERR_IPC_UNKNOW_REPLY;
929     inOutLen = sizeof(int32_t);
930     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
931     LOGI("process done, ret %d", ret);
932     if (ret != HC_SUCCESS) {
933         DestroyCallCtx(&callCtx, NULL);
934         return ret;
935     }
936     ret = SearchGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
937     LOGI("proc result done, ret %d", ret);
938     DestroyCallCtx(&callCtx, NULL);
939     return ret;
940 }
941 
JoinedGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)942 static int32_t JoinedGroupsIpcResult(const IpcDataInfo *replies,
943     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
944 {
945     int32_t ret;
946     int32_t inOutLen;
947 
948     inOutLen = sizeof(int32_t);
949     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
950     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
951         return HC_ERR_IPC_OUT_DATA_NUM;
952     }
953     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
954     if (*outGroupVec == NULL) {
955         return HC_ERR_IPC_OUT_DATA;
956     }
957     *outGroupVec = strdup(*outGroupVec);
958     if (*outGroupVec == NULL) {
959         return HC_ERR_ALLOC_MEMORY;
960     }
961     inOutLen = sizeof(int32_t);
962     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
963     return HC_SUCCESS;
964 }
965 
IpcGmGetJoinedGroups(int32_t osAccountId,const char * appId,int32_t groupType,char ** outGroupVec,uint32_t * groupNum)966 static int32_t IpcGmGetJoinedGroups(int32_t osAccountId, const char *appId, int32_t groupType,
967     char **outGroupVec, uint32_t *groupNum)
968 {
969     uintptr_t callCtx = 0x0;
970     int32_t ret;
971     int32_t inOutLen;
972     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = {{0}};
973 
974     LOGI("starting ...");
975     if (!IS_STRING_VALID(appId) || (outGroupVec == NULL) || (groupNum == NULL)) {
976         return HC_ERR_INVALID_PARAMS;
977     }
978     if (!IsServiceRunning()) {
979         LOGE("service is not activity");
980         return HC_ERROR;
981     }
982     ret = CreateCallCtx(&callCtx, NULL);
983     if (ret != HC_SUCCESS) {
984         LOGE("CreateCallCtx failed, ret %d", ret);
985         return HC_ERR_IPC_INIT;
986     }
987     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
988         sizeof(osAccountId));
989     if (ret != HC_SUCCESS) {
990         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
991         DestroyCallCtx(&callCtx, NULL);
992         return HC_ERR_IPC_BUILD_PARAM;
993     }
994     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
995     if (ret != HC_SUCCESS) {
996         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
997         DestroyCallCtx(&callCtx, NULL);
998         return HC_ERR_IPC_BUILD_PARAM;
999     }
1000     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUP_TYPE, (const uint8_t *)&groupType, sizeof(groupType));
1001     if (ret != HC_SUCCESS) {
1002         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUP_TYPE);
1003         DestroyCallCtx(&callCtx, NULL);
1004         return HC_ERR_IPC_BUILD_PARAM;
1005     }
1006     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_JOINED_GROUPS, true);
1007     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1008         LOGE("ipc call failed");
1009         DestroyCallCtx(&callCtx, NULL);
1010         return HC_ERR_IPC_PROC_FAILED;
1011     }
1012     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1013     ret = HC_ERR_IPC_UNKNOW_REPLY;
1014     inOutLen = sizeof(int32_t);
1015     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1016     LOGI("process done, ret %d", ret);
1017     if (ret != HC_SUCCESS) {
1018         DestroyCallCtx(&callCtx, NULL);
1019         return ret;
1020     }
1021     ret = JoinedGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
1022     LOGI("proc result done, ret %d", ret);
1023     DestroyCallCtx(&callCtx, NULL);
1024     return ret;
1025 }
1026 
RelatedGroupsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outGroupVec,uint32_t * groupNum)1027 static int32_t RelatedGroupsIpcResult(const IpcDataInfo *replies,
1028     int32_t cacheNum, char **outGroupVec, uint32_t *groupNum)
1029 {
1030     int32_t ret;
1031     int32_t inOutLen;
1032 
1033     inOutLen = sizeof(int32_t);
1034     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1035     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1036         return HC_ERR_IPC_OUT_DATA_NUM;
1037     }
1038     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_GROUP_INFO, (uint8_t *)outGroupVec, NULL);
1039     if (*outGroupVec == NULL) {
1040         return HC_ERR_IPC_OUT_DATA;
1041     }
1042     *outGroupVec = strdup(*outGroupVec);
1043     if (*outGroupVec == NULL) {
1044         return HC_ERR_ALLOC_MEMORY;
1045     }
1046     inOutLen = sizeof(int32_t);
1047     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)groupNum, &inOutLen);
1048     return HC_SUCCESS;
1049 }
1050 
IpcGmGetRelatedGroups(int32_t osAccountId,const char * appId,const char * peerUdid,char ** outGroupVec,uint32_t * groupNum)1051 static int32_t IpcGmGetRelatedGroups(int32_t osAccountId, const char *appId, const char *peerUdid,
1052     char **outGroupVec, uint32_t *groupNum)
1053 {
1054     uintptr_t callCtx = 0x0;
1055     int32_t ret;
1056     int32_t inOutLen;
1057     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = {{0}};
1058 
1059     LOGI("starting ...");
1060     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(peerUdid) || (outGroupVec == NULL) || (groupNum == NULL)) {
1061         return HC_ERR_INVALID_PARAMS;
1062     }
1063     if (!IsServiceRunning()) {
1064         LOGE("service is not activity");
1065         return HC_ERROR;
1066     }
1067     ret = CreateCallCtx(&callCtx, NULL);
1068     if (ret != HC_SUCCESS) {
1069         LOGE("CreateCallCtx failed, ret %d", ret);
1070         return HC_ERR_IPC_INIT;
1071     }
1072     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1073         sizeof(osAccountId));
1074     if (ret != HC_SUCCESS) {
1075         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1076         DestroyCallCtx(&callCtx, NULL);
1077         return HC_ERR_IPC_BUILD_PARAM;
1078     }
1079     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
1080     if (ret != HC_SUCCESS) {
1081         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
1082         DestroyCallCtx(&callCtx, NULL);
1083         return HC_ERR_IPC_BUILD_PARAM;
1084     }
1085     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)peerUdid, strlen(peerUdid) + 1);
1086     if (ret != HC_SUCCESS) {
1087         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_UDID);
1088         DestroyCallCtx(&callCtx, NULL);
1089         return HC_ERR_IPC_BUILD_PARAM;
1090     }
1091     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_RELATED_GROUPS, true);
1092     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1093         LOGE("ipc call failed");
1094         DestroyCallCtx(&callCtx, NULL);
1095         return HC_ERR_IPC_PROC_FAILED;
1096     }
1097     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1098     ret = HC_ERR_IPC_UNKNOW_REPLY;
1099     inOutLen = sizeof(int32_t);
1100     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1101     LOGI("process done, ret %d", ret);
1102     if (ret != HC_SUCCESS) {
1103         DestroyCallCtx(&callCtx, NULL);
1104         return ret;
1105     }
1106     ret = RelatedGroupsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outGroupVec, groupNum);
1107     LOGI("proc result done, ret %d", ret);
1108     DestroyCallCtx(&callCtx, NULL);
1109     return ret;
1110 }
1111 
DevInfoByIdIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outDevInfo)1112 static int32_t DevInfoByIdIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outDevInfo)
1113 {
1114     int32_t ret;
1115     int32_t inOutLen;
1116 
1117     inOutLen = sizeof(int32_t);
1118     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1119     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
1120         return HC_ERR_IPC_OUT_DATA_NUM;
1121     }
1122     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DEVICE_INFO, (uint8_t *)outDevInfo, NULL);
1123     if (*outDevInfo == NULL) {
1124         return HC_ERR_IPC_OUT_DATA;
1125     }
1126     *outDevInfo = strdup(*outDevInfo);
1127     if (*outDevInfo == NULL) {
1128         return HC_ERR_ALLOC_MEMORY;
1129     }
1130     return HC_SUCCESS;
1131 }
1132 
FormParamsForGettingDeviceInfo(int32_t osAccountId,const char * appId,const char * peerUdid,const char * groupId,uintptr_t callCtx)1133 static int32_t FormParamsForGettingDeviceInfo(int32_t osAccountId, const char *appId,
1134     const char *peerUdid, const char *groupId, uintptr_t callCtx)
1135 {
1136     int32_t ret;
1137     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1138         sizeof(osAccountId));
1139     if (ret != HC_SUCCESS) {
1140         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1141         DestroyCallCtx(&callCtx, NULL);
1142         return HC_ERR_IPC_BUILD_PARAM;
1143     }
1144     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
1145     if (ret != HC_SUCCESS) {
1146         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
1147         return HC_ERR_IPC_BUILD_PARAM;
1148     }
1149     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)peerUdid, strlen(peerUdid) + 1);
1150     if (ret != HC_SUCCESS) {
1151         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_UDID);
1152         return HC_ERR_IPC_BUILD_PARAM;
1153     }
1154     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, strlen(groupId) + 1);
1155     if (ret != HC_SUCCESS) {
1156         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUPID);
1157         return HC_ERR_IPC_BUILD_PARAM;
1158     }
1159     return HC_SUCCESS;
1160 }
1161 
IpcGmGetDeviceInfoById(int32_t osAccountId,const char * appId,const char * peerUdid,const char * groupId,char ** outDevInfo)1162 static int32_t IpcGmGetDeviceInfoById(int32_t osAccountId, const char *appId, const char *peerUdid, const char *groupId,
1163     char **outDevInfo)
1164 {
1165     uintptr_t callCtx = 0x0;
1166     int32_t ret;
1167     int32_t inOutLen;
1168     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = {{0}};
1169 
1170     LOGI("starting ...");
1171     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(peerUdid) || !IS_STRING_VALID(groupId) || (outDevInfo == NULL)) {
1172         return HC_ERR_INVALID_PARAMS;
1173     }
1174     if (!IsServiceRunning()) {
1175         LOGE("service is not activity");
1176         return HC_ERROR;
1177     }
1178     ret = CreateCallCtx(&callCtx, NULL);
1179     if (ret != HC_SUCCESS) {
1180         LOGE("CreateCallCtx failed, ret %d", ret);
1181         return HC_ERR_IPC_INIT;
1182     }
1183     ret = FormParamsForGettingDeviceInfo(osAccountId, appId, peerUdid, groupId, callCtx);
1184     if (ret != HC_SUCCESS) {
1185         DestroyCallCtx(&callCtx, NULL);
1186         return ret;
1187     }
1188     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_DEV_INFO_BY_ID, true);
1189     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1190         LOGE("ipc call failed");
1191         DestroyCallCtx(&callCtx, NULL);
1192         return HC_ERR_IPC_PROC_FAILED;
1193     }
1194     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1195     ret = HC_ERR_IPC_UNKNOW_REPLY;
1196     inOutLen = sizeof(int32_t);
1197     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1198     LOGI("process done, ret %d", ret);
1199     if (ret != HC_SUCCESS) {
1200         DestroyCallCtx(&callCtx, NULL);
1201         return ret;
1202     }
1203     ret = DevInfoByIdIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outDevInfo);
1204     LOGI("proc result done, ret %d", ret);
1205     DestroyCallCtx(&callCtx, NULL);
1206     return ret;
1207 }
1208 
TrustedDevIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** outDevInfoVec,uint32_t * devNum)1209 static int32_t TrustedDevIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **outDevInfoVec, uint32_t *devNum)
1210 {
1211     int32_t ret;
1212     int32_t inOutLen;
1213 
1214     inOutLen = sizeof(int32_t);
1215     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
1216     if ((ret < IPC_RESULT_NUM_2) || (inOutLen != sizeof(int32_t))) {
1217         return HC_ERR_IPC_OUT_DATA_NUM;
1218     }
1219     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DEVICE_INFO, (uint8_t *)outDevInfoVec, NULL);
1220     if (*outDevInfoVec == NULL) {
1221         return HC_ERR_IPC_OUT_DATA;
1222     }
1223     *outDevInfoVec = strdup(*outDevInfoVec);
1224     if (*outDevInfoVec == NULL) {
1225         return HC_ERR_ALLOC_MEMORY;
1226     }
1227     inOutLen = sizeof(int32_t);
1228     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_DATA_NUM, (uint8_t *)devNum, &inOutLen);
1229     return HC_SUCCESS;
1230 }
1231 
IpcGmGetTrustedDevices(int32_t osAccountId,const char * appId,const char * groupId,char ** outDevInfoVec,uint32_t * deviceNum)1232 static int32_t IpcGmGetTrustedDevices(int32_t osAccountId, const char *appId,
1233     const char *groupId, char **outDevInfoVec, uint32_t *deviceNum)
1234 {
1235     uintptr_t callCtx = 0x0;
1236     int32_t ret;
1237     int32_t inOutLen;
1238     IpcDataInfo replyCache[IPC_DATA_CACHES_4] = {{0}};
1239 
1240     LOGI("starting ...");
1241     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(groupId) ||
1242         (outDevInfoVec == NULL) || (deviceNum == NULL)) {
1243         return HC_ERR_INVALID_PARAMS;
1244     }
1245     if (!IsServiceRunning()) {
1246         LOGE("service is not activity");
1247         return HC_ERROR;
1248     }
1249     ret = CreateCallCtx(&callCtx, NULL);
1250     if (ret != HC_SUCCESS) {
1251         LOGE("CreateCallCtx failed, ret %d", ret);
1252         return HC_ERR_IPC_INIT;
1253     }
1254     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1255         sizeof(osAccountId));
1256     if (ret != HC_SUCCESS) {
1257         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1258         DestroyCallCtx(&callCtx, NULL);
1259         return HC_ERR_IPC_BUILD_PARAM;
1260     }
1261     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
1262     if (ret != HC_SUCCESS) {
1263         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
1264         DestroyCallCtx(&callCtx, NULL);
1265         return HC_ERR_IPC_BUILD_PARAM;
1266     }
1267     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, strlen(groupId) + 1);
1268     if (ret != HC_SUCCESS) {
1269         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUPID);
1270         DestroyCallCtx(&callCtx, NULL);
1271         return HC_ERR_IPC_BUILD_PARAM;
1272     }
1273     ret = DoBinderCall(callCtx, IPC_CALL_ID_GET_TRUST_DEVICES, true);
1274     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1275         LOGE("ipc call failed");
1276         DestroyCallCtx(&callCtx, NULL);
1277         return HC_ERR_IPC_PROC_FAILED;
1278     }
1279     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
1280     ret = HC_ERR_IPC_UNKNOW_REPLY;
1281     inOutLen = sizeof(int32_t);
1282     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1283     LOGI("process done, ret %d", ret);
1284     if (ret != HC_SUCCESS) {
1285         DestroyCallCtx(&callCtx, NULL);
1286         return ret;
1287     }
1288     ret = TrustedDevIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), outDevInfoVec, deviceNum);
1289     LOGI("proc result done, ret %d", ret);
1290     DestroyCallCtx(&callCtx, NULL);
1291     return ret;
1292 }
1293 
IpcGmIsDeviceInGroup(int32_t osAccountId,const char * appId,const char * groupId,const char * udid)1294 static bool IpcGmIsDeviceInGroup(int32_t osAccountId, const char *appId, const char *groupId, const char *udid)
1295 {
1296     uintptr_t callCtx = 0x0;
1297     int32_t ret;
1298     int32_t inOutLen;
1299     IpcDataInfo replyCache = {0};
1300 
1301     LOGI("starting ...");
1302     if (!IS_STRING_VALID(appId) || !IS_STRING_VALID(groupId) || !IS_STRING_VALID(udid)) {
1303         return false;
1304     }
1305     if (!IsServiceRunning()) {
1306         LOGE("service is not activity");
1307         return false;
1308     }
1309     ret = CreateCallCtx(&callCtx, NULL);
1310     if (ret != HC_SUCCESS) {
1311         LOGE("CreateCallCtx failed, ret %d", ret);
1312         return false;
1313     }
1314     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1315         sizeof(osAccountId));
1316     if (ret != HC_SUCCESS) {
1317         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1318         DestroyCallCtx(&callCtx, NULL);
1319         return false;
1320     }
1321     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId, strlen(appId) + 1);
1322     if (ret != HC_SUCCESS) {
1323         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_APPID);
1324         DestroyCallCtx(&callCtx, NULL);
1325         return false;
1326     }
1327     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_GROUPID, (const uint8_t *)groupId, strlen(groupId) + 1);
1328     if (ret != HC_SUCCESS) {
1329         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_GROUPID);
1330         DestroyCallCtx(&callCtx, NULL);
1331         return false;
1332     }
1333     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_UDID, (const uint8_t *)udid, strlen(udid) + 1);
1334     if (ret != HC_SUCCESS) {
1335         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_UDID);
1336         DestroyCallCtx(&callCtx, NULL);
1337         return false;
1338     }
1339     ret = DoBinderCall(callCtx, IPC_CALL_ID_IS_DEV_IN_GROUP, true);
1340     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1341         LOGE("ipc call failed");
1342         DestroyCallCtx(&callCtx, NULL);
1343         return false;
1344     }
1345     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1346     ret = HC_ERR_IPC_UNKNOW_REPLY;
1347     inOutLen = sizeof(int32_t);
1348     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1349     LOGI("process done, ret %d", ret);
1350     DestroyCallCtx(&callCtx, NULL);
1351     return (ret == HC_SUCCESS) ? true : false;
1352 }
1353 
IpcGmDestroyInfo(char ** returnInfo)1354 static void IpcGmDestroyInfo(char **returnInfo)
1355 {
1356     if ((returnInfo == NULL) || (*returnInfo == NULL)) {
1357         return;
1358     }
1359     FreeJsonString(*returnInfo);
1360     *returnInfo = NULL;
1361 }
1362 
InitIpcGmMethods(DeviceGroupManager * gmMethodObj)1363 static void InitIpcGmMethods(DeviceGroupManager *gmMethodObj)
1364 {
1365     LOGI("starting ...");
1366     gmMethodObj->regCallback = IpcGmRegCallback;
1367     gmMethodObj->unRegCallback = IpcGmUnRegCallback;
1368     gmMethodObj->regDataChangeListener = IpcGmRegDataChangeListener;
1369     gmMethodObj->unRegDataChangeListener = IpcGmUnRegDataChangeListener;
1370     gmMethodObj->createGroup = IpcGmCreateGroup;
1371     gmMethodObj->deleteGroup = IpcGmDelGroup;
1372     gmMethodObj->addMemberToGroup = IpcGmAddMemberToGroup;
1373     gmMethodObj->deleteMemberFromGroup = IpcGmDelMemberFromGroup;
1374     gmMethodObj->processData = IpcGmProcessData;
1375     gmMethodObj->processCredential = IpcGmProcCredential;
1376     gmMethodObj->getRegisterInfo = IpcGmGetRegisterInfo;
1377     gmMethodObj->checkAccessToGroup = IpcGmCheckAccessToGroup;
1378     gmMethodObj->getPkInfoList = IpcGmGetPkInfoList;
1379     gmMethodObj->getGroupInfoById = IpcGmGetGroupInfoById;
1380     gmMethodObj->getGroupInfo = IpcGmGetGroupInfo;
1381     gmMethodObj->getJoinedGroups = IpcGmGetJoinedGroups;
1382     gmMethodObj->getRelatedGroups = IpcGmGetRelatedGroups;
1383     gmMethodObj->getDeviceInfoById = IpcGmGetDeviceInfoById;
1384     gmMethodObj->getTrustedDevices = IpcGmGetTrustedDevices;
1385     gmMethodObj->isDeviceInGroup = IpcGmIsDeviceInGroup;
1386     gmMethodObj->destroyInfo = IpcGmDestroyInfo;
1387     LOGI("process done");
1388     return;
1389 }
1390 
IpcGaProcessData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)1391 static int32_t IpcGaProcessData(int64_t authReqId,
1392     const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *callback)
1393 {
1394     uintptr_t callCtx = 0x0;
1395     int32_t ret;
1396     int32_t inOutLen;
1397     IpcDataInfo replyCache = {0};
1398 
1399     LOGI("starting ...");
1400     if (!IS_COMM_DATA_VALID(data, dataLen) || (callback == NULL)) {
1401         LOGE("invalid params");
1402         return HC_ERR_INVALID_PARAMS;
1403     }
1404     if (!IsServiceRunning()) {
1405         LOGE("service is not activity");
1406         return HC_ERROR;
1407     }
1408     ret = CreateCallCtx(&callCtx, NULL);
1409     if (ret != HC_SUCCESS) {
1410         LOGE("CreateCallCtx failed, ret %d", ret);
1411         return HC_ERR_IPC_INIT;
1412     }
1413 
1414     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
1415     if (ret != HC_SUCCESS) {
1416         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_REQID);
1417         DestroyCallCtx(&callCtx, NULL);
1418         return HC_ERR_IPC_BUILD_PARAM;
1419     }
1420     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_COMM_DATA, (const uint8_t *)data, dataLen);
1421     if (ret != HC_SUCCESS) {
1422         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_COMM_DATA);
1423         DestroyCallCtx(&callCtx, NULL);
1424         return HC_ERR_IPC_BUILD_PARAM;
1425     }
1426     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
1427     if (ret != HC_SUCCESS) {
1428         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_DEV_AUTH_CB);
1429         DestroyCallCtx(&callCtx, NULL);
1430         return HC_ERR_IPC_BUILD_PARAM;
1431     }
1432     SetCbCtxToDataCtx(callCtx, 0x0);
1433     ret = DoBinderCall(callCtx, IPC_CALL_ID_GA_PROC_DATA, true);
1434     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1435         LOGE("ipc call failed");
1436         DestroyCallCtx(&callCtx, NULL);
1437         return HC_ERR_IPC_PROC_FAILED;
1438     }
1439     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1440     ret = HC_ERR_IPC_UNKNOW_REPLY;
1441     inOutLen = sizeof(int32_t);
1442     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1443     LOGI("process done, ret %d", ret);
1444     DestroyCallCtx(&callCtx, NULL);
1445     return ret;
1446 }
1447 
IpcGaAuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)1448 static int32_t IpcGaAuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
1449     const DeviceAuthCallback *callback)
1450 {
1451     uintptr_t callCtx = 0x0;
1452     int32_t ret;
1453     int32_t inOutLen;
1454     IpcDataInfo replyCache = {0};
1455 
1456     LOGI("starting ...");
1457     if (!IS_STRING_VALID(authParams) || (callback == NULL)) {
1458         LOGE("invalid params");
1459         return HC_ERR_INVALID_PARAMS;
1460     }
1461     if (!IsServiceRunning()) {
1462         LOGE("service is not activity");
1463         return HC_ERROR;
1464     }
1465     ret = CreateCallCtx(&callCtx, NULL);
1466     if (ret != HC_SUCCESS) {
1467         LOGE("CreateCallCtx failed, ret %d", ret);
1468         return HC_ERR_IPC_INIT;
1469     }
1470     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
1471         sizeof(osAccountId));
1472     if (ret != HC_SUCCESS) {
1473         LOGE("set request param failed, ret %d, param id %d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
1474         DestroyCallCtx(&callCtx, NULL);
1475         return HC_ERR_IPC_BUILD_PARAM;
1476     }
1477     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
1478     if (ret != HC_SUCCESS) {
1479         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_REQID);
1480         DestroyCallCtx(&callCtx, NULL);
1481         return HC_ERR_IPC_BUILD_PARAM;
1482     }
1483     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams, strlen(authParams) + 1);
1484     if (ret != HC_SUCCESS) {
1485         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_AUTH_PARAMS);
1486         DestroyCallCtx(&callCtx, NULL);
1487         return HC_ERR_IPC_BUILD_PARAM;
1488     }
1489     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
1490     if (ret != HC_SUCCESS) {
1491         LOGE("set request param failed, ret %d, type %d", ret, PARAM_TYPE_DEV_AUTH_CB);
1492         DestroyCallCtx(&callCtx, NULL);
1493         return HC_ERR_IPC_BUILD_PARAM;
1494     }
1495     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_AUTH_ID);
1496     ret = DoBinderCall(callCtx, IPC_CALL_ID_AUTH_DEVICE, true);
1497     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1498         LOGE("ipc call failed");
1499         DestroyCallCtx(&callCtx, NULL);
1500         return HC_ERR_IPC_PROC_FAILED;
1501     }
1502     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1503     ret = HC_ERR_IPC_UNKNOW_REPLY;
1504     inOutLen = sizeof(int32_t);
1505     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1506     LOGI("process done, ret %d", ret);
1507     DestroyCallCtx(&callCtx, NULL);
1508     return ret;
1509 }
1510 
InitIpcGaMethods(GroupAuthManager * gaMethodObj)1511 static void InitIpcGaMethods(GroupAuthManager *gaMethodObj)
1512 {
1513     LOGI("entering...");
1514     gaMethodObj->processData = IpcGaProcessData;
1515     gaMethodObj->authDevice = IpcGaAuthDevice;
1516     LOGI("process done");
1517     return;
1518 }
1519 
InitDeviceAuthService(void)1520 DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void)
1521 {
1522     InitHcMutex(&g_ipcMutex);
1523     return InitProxyAdapt();
1524 }
1525 
DestroyDeviceAuthService(void)1526 DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void)
1527 {
1528     UnInitProxyAdapt();
1529     DestroyHcMutex(&g_ipcMutex);
1530 }
1531 
GetGaInstance(void)1532 DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void)
1533 {
1534     static GroupAuthManager gaInstCtx = {NULL};
1535     static GroupAuthManager *gaInstPtr = NULL;
1536 
1537     LOGI("Enter InitIpcMethods...");
1538     if (gaInstPtr == NULL) {
1539         InitIpcGaMethods(&gaInstCtx);
1540         gaInstPtr = &gaInstCtx;
1541     }
1542     LOGI("InitIpcMethods done");
1543     return (const GroupAuthManager *)(gaInstPtr);
1544 }
1545 
GetGmInstance(void)1546 DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void)
1547 {
1548     static DeviceGroupManager gmInstCtx = {NULL};
1549     static DeviceGroupManager *gmInstPtr = NULL;
1550 
1551     LOGI("Enter InitIpcMethods...");
1552     if (gmInstPtr == NULL) {
1553         InitIpcGmMethods(&gmInstCtx);
1554         gmInstPtr = &gmInstCtx;
1555     }
1556     LOGI("InitIpcMethods done");
1557     return (const DeviceGroupManager *)(gmInstPtr);
1558 }
1559 
1560 #ifdef __cplusplus
1561 }
1562 #endif
1563