• 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_service.h"
17 
18 #include "common_defs.h"
19 #include "device_auth_defines.h"
20 #include "device_auth.h"
21 #include "hc_condition.h"
22 #include "hc_log.h"
23 #include "hc_thread.h"
24 #include "ipc_adapt.h"
25 #include "ipc_sdk.h"
26 #include "securec.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 static const int32_t IPC_RESULT_NUM_1 = 1;
33 static const int32_t IPC_RESULT_NUM_2 = 2;
34 static const char *g_serviceAppId = "deviceauth_service";
35 static DeviceGroupManager g_devGroupMgrMethod = {NULL};
36 static GroupAuthManager g_groupAuthMgrMethod = {NULL};
37 static DeviceAuthCallback g_bindCbAdt = {NULL};
38 static DeviceAuthCallback g_authCbAdt = {NULL};
39 static DataChangeListener g_listenCbAdt = {NULL};
40 
BindRequestIdWithAppId(const char * data)41 static int32_t BindRequestIdWithAppId(const char *data)
42 {
43     const char *appId = NULL;
44     int32_t ret;
45     int64_t requestId = -1;
46     CJson *dataJson = CreateJsonFromString(data);
47     if (dataJson == NULL) {
48         LOGE("failed to create json from string!");
49         return HC_ERROR;
50     }
51 
52     appId = GetStringFromJson(dataJson, FIELD_APP_ID);
53     if (appId == NULL) {
54         LOGE("failed to get appId from json object!");
55         FreeJson(dataJson);
56         return HC_ERROR;
57     }
58     (void)GetInt64FromJson(dataJson, FIELD_REQUEST_ID, &requestId);
59     ret = AddReqIdByAppId(appId, requestId);
60     FreeJson(dataJson);
61     return ret;
62 }
63 
IpcServiceGmRegCallback(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)64 static int32_t IpcServiceGmRegCallback(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
65 {
66     int32_t callRet;
67     int32_t ret;
68     const char *appId = NULL;
69     const DeviceAuthCallback *callback = NULL;
70     int32_t cbObjIdx = -1;
71     int32_t idxLen;
72 
73     LOGI("starting ...");
74     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
75     if (ret != HC_SUCCESS) {
76         LOGE("get param error, type %d", PARAM_TYPE_APPID);
77         return ret;
78     }
79 
80     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEV_AUTH_CB, (uint8_t *)&callback, NULL);
81     if (ret != HC_SUCCESS) {
82         LOGE("get param error, type %d", PARAM_TYPE_DEV_AUTH_CB);
83         return ret;
84     }
85     ret = AddIpcCallBackByAppId(appId, (const uint8_t *)callback, sizeof(DeviceAuthCallback), CB_TYPE_DEV_AUTH);
86     if (ret != HC_SUCCESS) {
87         LOGE("add ipc callback failed");
88         return HC_ERROR;
89     }
90 
91     idxLen = sizeof(cbObjIdx);
92     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_CB_OBJECT, (uint8_t *)&cbObjIdx, &idxLen);
93     if (ret != HC_SUCCESS) {
94         LOGE("get param error, type %d", PARAM_TYPE_CB_OBJECT);
95         DelIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
96         return ret;
97     }
98     AddIpcCbObjByAppId(appId, cbObjIdx, CB_TYPE_DEV_AUTH);
99     InitDeviceAuthCbCtx(&g_bindCbAdt, CB_TYPE_DEV_AUTH);
100     callRet = g_devGroupMgrMethod.regCallback(appId, &g_bindCbAdt);
101     if (callRet != HC_SUCCESS) {
102         DelIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
103     }
104     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
105     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
106     return ret;
107 }
108 
IpcServiceGmUnRegCallback(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)109 static int32_t IpcServiceGmUnRegCallback(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
110 {
111     int32_t callRet = HC_SUCCESS;
112     int32_t ret;
113     const char *appId = NULL;
114 
115     LOGI("starting ...");
116     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
117     if (ret != HC_SUCCESS) {
118         LOGE("get param error, type %d", PARAM_TYPE_APPID);
119         return ret;
120     }
121 
122     DelIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
123     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
124     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
125     return ret;
126 }
127 
IpcServiceGmRegDataChangeListener(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)128 static int32_t IpcServiceGmRegDataChangeListener(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
129 {
130     int32_t callRet;
131     int32_t ret;
132     const char *appId = NULL;
133     const DataChangeListener *callback = NULL;
134     static int32_t registered = 0;
135     int32_t cbObjIdx = -1;
136     int32_t idxLen;
137 
138     LOGI("starting ...");
139     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
140     if (ret != HC_SUCCESS) {
141         LOGE("get param error, type %d", PARAM_TYPE_APPID);
142         return ret;
143     }
144 
145     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_LISTERNER, (uint8_t *)&callback, NULL);
146     if (ret != HC_SUCCESS) {
147         LOGE("get param error, type %d", PARAM_TYPE_LISTERNER);
148         return ret;
149     }
150 
151     ret = AddIpcCallBackByAppId(appId, (const uint8_t *)callback, sizeof(DataChangeListener), CB_TYPE_LISTENER);
152     if (ret != HC_SUCCESS) {
153         LOGE("add ipc callback failed");
154         return HC_ERROR;
155     }
156 
157     idxLen = sizeof(cbObjIdx);
158     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_CB_OBJECT, (uint8_t *)&cbObjIdx, &idxLen);
159     if (ret != HC_SUCCESS) {
160         LOGE("get param error, type %d", PARAM_TYPE_CB_OBJECT);
161         DelIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
162         return ret;
163     }
164     AddIpcCbObjByAppId(appId, cbObjIdx, CB_TYPE_LISTENER);
165 
166     callRet = HC_SUCCESS;
167     if (registered == 0) {
168         InitDevAuthListenerCbCtx(&g_listenCbAdt);
169         callRet = g_devGroupMgrMethod.regDataChangeListener(g_serviceAppId, &g_listenCbAdt);
170         if (callRet == HC_SUCCESS) {
171             registered = 1;
172         } else {
173             DelIpcCallBackByAppId(appId, CB_TYPE_LISTENER);
174         }
175     }
176     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
177     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
178     return ret;
179 }
180 
IpcServiceGmUnRegDataChangeListener(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)181 static int32_t IpcServiceGmUnRegDataChangeListener(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
182 {
183     int32_t callRet = HC_SUCCESS;
184     int32_t ret;
185     const char *appId = NULL;
186 
187     LOGI("starting ...");
188     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
189     if (ret != HC_SUCCESS) {
190         LOGE("get param error, type %d", PARAM_TYPE_APPID);
191         return ret;
192     }
193     DelIpcCallBackByAppId(appId, CB_TYPE_LISTENER);
194     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
195     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
196     return ret;
197 }
198 
IpcServiceGmCreateGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)199 static int32_t IpcServiceGmCreateGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
200 {
201     int32_t callRet;
202     int32_t ret;
203     int32_t osAccountId;
204     int64_t requestId = 0;
205     int32_t inOutLen;
206     const char *createParams = NULL;
207     const char *appId = NULL;
208 
209     LOGI("starting ...");
210     inOutLen = sizeof(int32_t);
211     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
212     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
213         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
214         return HC_ERR_IPC_BAD_PARAM;
215     }
216     inOutLen = sizeof(int64_t);
217     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&requestId, &inOutLen);
218     if ((inOutLen != sizeof(int64_t)) || (ret != HC_SUCCESS)) {
219         LOGE("get param error, type %d", PARAM_TYPE_REQID);
220         return HC_ERR_IPC_BAD_PARAM;
221     }
222     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
223     if (ret != HC_SUCCESS) {
224         LOGE("get param error, type %d", PARAM_TYPE_APPID);
225         return ret;
226     }
227     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_CREATE_PARAMS, (uint8_t *)&createParams, NULL);
228     if (ret != HC_SUCCESS) {
229         LOGE("get param error, type %d", PARAM_TYPE_CREATE_PARAMS);
230         return ret;
231     }
232     ret = AddReqIdByAppId(appId, requestId);
233     if (ret != 0) {
234         LOGE("bind request id by app id failed");
235         return ret;
236     }
237     callRet = g_devGroupMgrMethod.createGroup(osAccountId, requestId, appId, createParams);
238     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
239     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
240     return ret;
241 }
242 
IpcServiceGmDelGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)243 static int32_t IpcServiceGmDelGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
244 {
245     int32_t callRet;
246     int32_t ret;
247     int32_t osAccountId;
248     int64_t requestId = 0;
249     int32_t inOutLen;
250     const char *appId = NULL;
251     const char *delParams = NULL;
252 
253     LOGI("starting ...");
254     inOutLen = sizeof(int32_t);
255     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
256     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
257         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
258         return HC_ERR_IPC_BAD_PARAM;
259     }
260     inOutLen = sizeof(int64_t);
261     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&requestId, &inOutLen);
262     if ((inOutLen != sizeof(int64_t)) || (ret != HC_SUCCESS)) {
263         LOGE("get param error, type %d", PARAM_TYPE_REQID);
264         return HC_ERR_IPC_BAD_PARAM;
265     }
266     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
267     if (ret != HC_SUCCESS) {
268         LOGE("get param error, type %d", PARAM_TYPE_APPID);
269         return ret;
270     }
271     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEL_PARAMS, (uint8_t *)&delParams, NULL);
272     if (ret != HC_SUCCESS) {
273         LOGE("get param error, type %d", PARAM_TYPE_DEL_PARAMS);
274         return ret;
275     }
276     ret = AddReqIdByAppId(appId, requestId);
277     if (ret != 0) {
278         LOGE("bind request id by app id failed");
279         return ret;
280     }
281     callRet = g_devGroupMgrMethod.deleteGroup(osAccountId, requestId, appId, delParams);
282     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
283     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
284     return ret;
285 }
286 
IpcServiceGmAddMemberToGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)287 static int32_t IpcServiceGmAddMemberToGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
288 {
289     int32_t callRet;
290     int32_t ret;
291     int32_t inOutLen;
292     int32_t osAccountId;
293     int64_t requestId = 0;
294     const char *addParams = NULL;
295     const char *appId = NULL;
296 
297     LOGI("starting ...");
298     inOutLen = sizeof(int32_t);
299     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
300     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
301         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
302         return HC_ERR_IPC_BAD_PARAM;
303     }
304     inOutLen = sizeof(int64_t);
305     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&requestId, &inOutLen);
306     if ((inOutLen != sizeof(int64_t)) || (ret != HC_SUCCESS)) {
307         LOGE("get param error, type %d", PARAM_TYPE_REQID);
308         return HC_ERR_IPC_BAD_PARAM;
309     }
310     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_ADD_PARAMS, (uint8_t *)&addParams, NULL);
311     if (ret != HC_SUCCESS) {
312         LOGE("get param error, type %d", PARAM_TYPE_ADD_PARAMS);
313         return ret;
314     }
315     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
316     if (ret != HC_SUCCESS) {
317         LOGE("get param error, type %d", PARAM_TYPE_APPID);
318         return ret;
319     }
320     ret = AddReqIdByAppId(appId, requestId);
321     if (ret != HC_SUCCESS) {
322         return ret;
323     }
324     callRet = g_devGroupMgrMethod.addMemberToGroup(osAccountId, requestId, appId, addParams);
325     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
326     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
327     return ret;
328 }
329 
IpcServiceGmDelMemberFromGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)330 static int32_t IpcServiceGmDelMemberFromGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
331 {
332     int32_t callRet;
333     int32_t ret;
334     int32_t inOutLen;
335     int32_t osAccountId;
336     int64_t requestId = 0;
337     const char *delParams = NULL;
338     const char *appId = NULL;
339 
340     LOGI("starting ...");
341     inOutLen = sizeof(int32_t);
342     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
343     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
344         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
345         return HC_ERR_IPC_BAD_PARAM;
346     }
347     inOutLen = sizeof(int64_t);
348     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&requestId, &inOutLen);
349     if ((inOutLen != sizeof(int64_t)) || (ret != HC_SUCCESS)) {
350         LOGE("get param error, type %d", PARAM_TYPE_REQID);
351         return HC_ERR_IPC_BAD_PARAM;
352     }
353     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
354     if (ret != HC_SUCCESS) {
355         LOGE("get param error, type %d", PARAM_TYPE_APPID);
356         return ret;
357     }
358     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEL_PARAMS, (uint8_t *)&delParams, NULL);
359     if (ret != HC_SUCCESS) {
360         LOGE("get param error, type %d", PARAM_TYPE_DEL_PARAMS);
361         return ret;
362     }
363     ret = AddReqIdByAppId(appId, requestId);
364     if (ret != 0) {
365         LOGE("bind request id by app id failed");
366         return ret;
367     }
368     callRet = g_devGroupMgrMethod.deleteMemberFromGroup(osAccountId, requestId, appId, delParams);
369     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
370     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
371     return ret;
372 }
373 
IpcServiceGmAddMultiMembersToGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)374 static int32_t IpcServiceGmAddMultiMembersToGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
375 {
376     int32_t callRet;
377     int32_t ret;
378     int32_t inOutLen;
379     int32_t osAccountId;
380     const char *addParams = NULL;
381     const char *appId = NULL;
382 
383     LOGI("starting ...");
384     inOutLen = sizeof(int32_t);
385     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
386     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
387         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
388         return HC_ERR_IPC_BAD_PARAM;
389     }
390     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_ADD_PARAMS, (uint8_t *)&addParams, NULL);
391     if (ret != HC_SUCCESS) {
392         LOGE("get param error, type %d", PARAM_TYPE_ADD_PARAMS);
393         return ret;
394     }
395     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
396     if (ret != HC_SUCCESS) {
397         LOGE("get param error, type %d", PARAM_TYPE_APPID);
398         return ret;
399     }
400     callRet = g_devGroupMgrMethod.addMultiMembersToGroup(osAccountId, appId, addParams);
401     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
402     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
403     return ret;
404 }
405 
IpcServiceGmDelMultiMembersFromGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)406 static int32_t IpcServiceGmDelMultiMembersFromGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
407 {
408     int32_t callRet;
409     int32_t ret;
410     int32_t inOutLen;
411     int32_t osAccountId;
412     const char *delParams = NULL;
413     const char *appId = NULL;
414 
415     LOGI("starting ...");
416     inOutLen = sizeof(int32_t);
417     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
418     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
419         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
420         return HC_ERR_IPC_BAD_PARAM;
421     }
422     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
423     if (ret != HC_SUCCESS) {
424         LOGE("get param error, type %d", PARAM_TYPE_APPID);
425         return ret;
426     }
427     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEL_PARAMS, (uint8_t *)&delParams, NULL);
428     if (ret != HC_SUCCESS) {
429         LOGE("get param error, type %d", PARAM_TYPE_DEL_PARAMS);
430         return ret;
431     }
432     callRet = g_devGroupMgrMethod.delMultiMembersFromGroup(osAccountId, appId, delParams);
433     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
434     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
435     return ret;
436 }
437 
IpcServiceGmProcessData(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)438 static int32_t IpcServiceGmProcessData(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
439 {
440     int32_t callRet;
441     int32_t ret;
442     int32_t dataLen;
443     int32_t inOutLen;
444     int64_t requestId = 0;
445     const uint8_t *data = NULL;
446 
447     LOGI("starting ...");
448     inOutLen = sizeof(int64_t);
449     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&requestId, &inOutLen);
450     if ((inOutLen != sizeof(int64_t)) || (ret != HC_SUCCESS)) {
451         LOGE("get param error, type %d", PARAM_TYPE_REQID);
452         return HC_ERR_IPC_BAD_PARAM;
453     }
454 
455     dataLen = 0;
456     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_COMM_DATA, (uint8_t *)&data, &dataLen);
457     if ((dataLen <= 0) || (ret != HC_SUCCESS)) {
458         LOGE("get param error, type %d, data length %d", PARAM_TYPE_COMM_DATA, dataLen);
459         return HC_ERR_IPC_BAD_PARAM;
460     }
461     ret = BindRequestIdWithAppId((const char *)data);
462     if (ret != HC_SUCCESS) {
463         return ret;
464     }
465     callRet = g_devGroupMgrMethod.processData(requestId, data, dataLen);
466     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
467     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
468     return ret;
469 }
470 
IpcServiceGmApplyRegisterInfo(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)471 static int32_t IpcServiceGmApplyRegisterInfo(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
472 {
473     int32_t callRet;
474     int32_t ret;
475     const char *reqJsonStr = NULL;
476     char *registerInfo = NULL;
477 
478     LOGI("starting ...");
479     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQ_JSON, (uint8_t *)&reqJsonStr, NULL);
480     if ((reqJsonStr == NULL) || (ret != HC_SUCCESS)) {
481         LOGE("get param error, type %d", PARAM_TYPE_REQ_JSON);
482         return HC_ERR_IPC_BAD_PARAM;
483     }
484     callRet = g_devGroupMgrMethod.getRegisterInfo(reqJsonStr, &registerInfo);
485     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
486     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
487                                (const uint8_t *)&IPC_RESULT_NUM_1, sizeof(int32_t));
488     if (registerInfo != NULL) {
489         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_REG_INFO,
490             (const uint8_t *)registerInfo, strlen(registerInfo) + 1);
491         g_devGroupMgrMethod.destroyInfo(&registerInfo);
492     } else {
493         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_REG_INFO, NULL, 0);
494     }
495     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
496     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
497 }
498 
IpcServiceGmCheckAccessToGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)499 static int32_t IpcServiceGmCheckAccessToGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
500 {
501     int32_t callRet;
502     int32_t ret;
503     int32_t inOutLen;
504     int32_t osAccountId;
505     const char *appId = NULL;
506     const char *groupId = NULL;
507 
508     LOGI("starting ...");
509     inOutLen = sizeof(int32_t);
510     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
511     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
512         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
513         return HC_ERR_IPC_BAD_PARAM;
514     }
515     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
516     if ((appId == NULL) || (ret != HC_SUCCESS)) {
517         LOGE("get param error, type %d", PARAM_TYPE_APPID);
518         return HC_ERR_IPC_BAD_PARAM;
519     }
520     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUPID, (uint8_t *)&groupId, NULL);
521     if ((groupId == NULL) || (ret != HC_SUCCESS)) {
522         LOGE("get param error, type %d", PARAM_TYPE_GROUPID);
523         return HC_ERR_IPC_BAD_PARAM;
524     }
525 
526     callRet = g_devGroupMgrMethod.checkAccessToGroup(osAccountId, appId, groupId);
527     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
528     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
529     return ret;
530 }
531 
IpcServiceGmGetPkInfoList(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)532 static int32_t IpcServiceGmGetPkInfoList(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
533 {
534     int32_t callRet;
535     int32_t ret;
536     int32_t inOutLen;
537     int32_t osAccountId;
538     const char *appId = NULL;
539     const char *queryParams = NULL;
540     char *returnInfoList = NULL;
541     uint32_t returnInfoNum = 0;
542 
543     LOGI("starting ...");
544     inOutLen = sizeof(int32_t);
545     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
546     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
547         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
548         return HC_ERR_IPC_BAD_PARAM;
549     }
550     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
551     if ((appId == NULL) || (ret != HC_SUCCESS)) {
552         LOGE("get param error, type %d", PARAM_TYPE_APPID);
553         return HC_ERR_IPC_BAD_PARAM;
554     }
555     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_QUERY_PARAMS, (uint8_t *)&queryParams, NULL);
556     if ((queryParams == NULL) || (ret != HC_SUCCESS)) {
557         LOGE("get param error, type %d", PARAM_TYPE_QUERY_PARAMS);
558         return HC_ERR_IPC_BAD_PARAM;
559     }
560 
561     callRet = g_devGroupMgrMethod.getPkInfoList(osAccountId, appId, queryParams, &returnInfoList, &returnInfoNum);
562     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
563     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
564                                (const uint8_t *)&IPC_RESULT_NUM_2, sizeof(int32_t));
565     if (returnInfoList != NULL) {
566         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_RETURN_DATA, (const uint8_t *)returnInfoList,
567                                    strlen(returnInfoList) + 1);
568     } else {
569         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_RETURN_DATA, NULL, 0);
570     }
571     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DATA_NUM, (const uint8_t *)&returnInfoNum, sizeof(int32_t));
572     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
573     g_devGroupMgrMethod.destroyInfo(&returnInfoList);
574     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
575 }
576 
IpcServiceGmGetGroupInfoById(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)577 static int32_t IpcServiceGmGetGroupInfoById(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
578 {
579     int32_t callRet;
580     int32_t ret;
581     int32_t inOutLen;
582     int32_t osAccountId;
583     const char *appId = NULL;
584     const char *groupId = NULL;
585     char *groupInfo = NULL;
586 
587     LOGI("starting ...");
588     inOutLen = sizeof(int32_t);
589     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
590     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
591         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
592         return HC_ERR_IPC_BAD_PARAM;
593     }
594     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
595     if ((appId == NULL) || (ret != HC_SUCCESS)) {
596         LOGE("get param error, type %d", PARAM_TYPE_APPID);
597         return HC_ERR_IPC_BAD_PARAM;
598     }
599     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUPID, (uint8_t *)&groupId, NULL);
600     if ((groupId == NULL) || (ret != HC_SUCCESS)) {
601         LOGE("get param error, type %d", PARAM_TYPE_GROUPID);
602         return HC_ERR_IPC_BAD_PARAM;
603     }
604 
605     callRet = g_devGroupMgrMethod.getGroupInfoById(osAccountId, appId, groupId, &groupInfo);
606     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
607     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
608                                (const uint8_t *)&IPC_RESULT_NUM_1, sizeof(int32_t));
609     if (groupInfo != NULL) {
610         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, (const uint8_t *)groupInfo, strlen(groupInfo) + 1);
611         g_devGroupMgrMethod.destroyInfo(&groupInfo);
612     } else {
613         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, NULL, 0);
614     }
615     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
616     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
617 }
618 
IpcServiceGmGetGroupInfo(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)619 static int32_t IpcServiceGmGetGroupInfo(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
620 {
621     int32_t callRet;
622     int32_t ret;
623     int32_t inOutLen;
624     int32_t osAccountId;
625     const char *appId = NULL;
626     const char *queryParams = NULL;
627     char *outGroups = NULL;
628     uint32_t groupNum = 0;
629 
630     LOGI("starting ...");
631     inOutLen = sizeof(int32_t);
632     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
633     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
634         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
635         return HC_ERR_IPC_BAD_PARAM;
636     }
637     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
638     if ((appId == NULL) || (ret != HC_SUCCESS)) {
639         LOGE("get param error, type %d", PARAM_TYPE_APPID);
640         return HC_ERR_IPC_BAD_PARAM;
641     }
642     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_QUERY_PARAMS, (uint8_t *)&queryParams, NULL);
643     if ((queryParams == NULL) || (ret != HC_SUCCESS)) {
644         LOGE("get param error, type %d", PARAM_TYPE_QUERY_PARAMS);
645         return HC_ERR_IPC_BAD_PARAM;
646     }
647 
648     callRet = g_devGroupMgrMethod.getGroupInfo(osAccountId, appId, queryParams, &outGroups, &groupNum);
649     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
650     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
651                                (const uint8_t *)&IPC_RESULT_NUM_2, sizeof(int32_t));
652     if (outGroups != NULL) {
653         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, (const uint8_t *)outGroups, strlen(outGroups) + 1);
654     } else {
655         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, NULL, 0);
656     }
657     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DATA_NUM, (const uint8_t *)&groupNum, sizeof(int32_t));
658     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
659     g_devGroupMgrMethod.destroyInfo(&outGroups);
660     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
661 }
662 
IpcServiceGmGetJoinedGroups(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)663 static int32_t IpcServiceGmGetJoinedGroups(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
664 {
665     int32_t callRet;
666     int32_t ret;
667     int32_t groupType = 0;
668     int32_t inOutLen;
669     int32_t osAccountId;
670     const char *appId = NULL;
671     char *outGroups = NULL;
672     uint32_t groupNum = 0;
673 
674     LOGI("starting ...");
675     inOutLen = sizeof(int32_t);
676     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
677     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
678         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
679         return HC_ERR_IPC_BAD_PARAM;
680     }
681     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
682     if ((appId == NULL) || (ret != HC_SUCCESS)) {
683         LOGE("get param error, type %d", PARAM_TYPE_APPID);
684         return HC_ERR_IPC_BAD_PARAM;
685     }
686     inOutLen = sizeof(groupType);
687     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUP_TYPE, (uint8_t *)&groupType, &inOutLen);
688     if ((inOutLen != sizeof(groupType)) || (ret != HC_SUCCESS)) {
689         LOGE("get param error, type %d", PARAM_TYPE_GROUP_TYPE);
690         return HC_ERR_IPC_BAD_PARAM;
691     }
692 
693     callRet = g_devGroupMgrMethod.getJoinedGroups(osAccountId, appId, groupType, &outGroups, &groupNum);
694     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
695     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
696                                (const uint8_t *)&IPC_RESULT_NUM_2, sizeof(int32_t));
697     if (outGroups != NULL) {
698         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, (const uint8_t *)outGroups, strlen(outGroups) + 1);
699         g_devGroupMgrMethod.destroyInfo(&outGroups);
700     } else {
701         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, NULL, 0);
702     }
703     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DATA_NUM, (const uint8_t *)&groupNum, sizeof(int32_t));
704     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
705     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
706 }
707 
IpcServiceGmGetRelatedGroups(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)708 static int32_t IpcServiceGmGetRelatedGroups(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
709 {
710     int32_t callRet;
711     int32_t ret;
712     int32_t inOutLen;
713     int32_t osAccountId;
714     const char *appId = NULL;
715     const char *peerUdid = NULL;
716     char *outGroups = NULL;
717     uint32_t groupNum = 0;
718 
719     LOGI("starting ...");
720     inOutLen = sizeof(int32_t);
721     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
722     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
723         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
724         return HC_ERR_IPC_BAD_PARAM;
725     }
726     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
727     if ((appId == NULL) || (ret != HC_SUCCESS)) {
728         LOGE("get param error, type %d", PARAM_TYPE_APPID);
729         return HC_ERR_IPC_BAD_PARAM;
730     }
731     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_UDID, (uint8_t *)&peerUdid, NULL);
732     if ((peerUdid == NULL) || (ret != HC_SUCCESS)) {
733         LOGE("get param error, type %d", PARAM_TYPE_UDID);
734         return HC_ERR_IPC_BAD_PARAM;
735     }
736 
737     callRet = g_devGroupMgrMethod.getRelatedGroups(osAccountId, appId, peerUdid, &outGroups, &groupNum);
738     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
739     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
740                                (const uint8_t *)&IPC_RESULT_NUM_2, sizeof(int32_t));
741     if (outGroups != NULL) {
742         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, (const uint8_t *)outGroups, strlen(outGroups) + 1);
743     } else {
744         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_GROUP_INFO, NULL, 0);
745     }
746     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DATA_NUM, (const uint8_t *)&groupNum, sizeof(int32_t));
747     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
748     g_devGroupMgrMethod.destroyInfo(&outGroups);
749     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
750 }
751 
IpcServiceGmGetDeviceInfoById(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)752 static int32_t IpcServiceGmGetDeviceInfoById(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
753 {
754     int32_t callRet;
755     int32_t ret;
756     int32_t inOutLen;
757     int32_t osAccountId;
758     const char *appId = NULL;
759     const char *peerUdid = NULL;
760     const char *groupId = NULL;
761     char *outDevInfo = NULL;
762 
763     LOGI("starting ...");
764     inOutLen = sizeof(int32_t);
765     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
766     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
767         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
768         return HC_ERR_IPC_BAD_PARAM;
769     }
770     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
771     if ((appId == NULL) || (ret != HC_SUCCESS)) {
772         LOGE("get param error, type %d", PARAM_TYPE_APPID);
773         return HC_ERR_IPC_BAD_PARAM;
774     }
775     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_UDID, (uint8_t *)&peerUdid, NULL);
776     if ((peerUdid == NULL) || (ret != HC_SUCCESS)) {
777         LOGE("get param error, type %d", PARAM_TYPE_UDID);
778         return HC_ERR_IPC_BAD_PARAM;
779     }
780     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUPID, (uint8_t *)&groupId, NULL);
781     if ((groupId == NULL) || (ret != HC_SUCCESS)) {
782         LOGE("get param error, type %d", PARAM_TYPE_GROUPID);
783         return HC_ERR_IPC_BAD_PARAM;
784     }
785 
786     callRet = g_devGroupMgrMethod.getDeviceInfoById(osAccountId, appId, peerUdid, groupId, &outDevInfo);
787     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
788     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
789                                (const uint8_t *)&IPC_RESULT_NUM_1, sizeof(int32_t));
790     if (outDevInfo != NULL) {
791         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DEVICE_INFO,
792             (const uint8_t *)outDevInfo, strlen(outDevInfo) + 1);
793         g_devGroupMgrMethod.destroyInfo(&outDevInfo);
794     } else {
795         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DEVICE_INFO, NULL, 0);
796     }
797     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
798     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
799 }
800 
IpcServiceGmGetTrustedDevices(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)801 static int32_t IpcServiceGmGetTrustedDevices(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
802 {
803     int32_t callRet;
804     int32_t ret;
805     int32_t inOutLen;
806     int32_t osAccountId;
807     const char *appId = NULL;
808     const char *groupId = NULL;
809     char *outDevInfo = NULL;
810     uint32_t outDevNum = 0;
811 
812     LOGI("starting ...");
813     inOutLen = sizeof(int32_t);
814     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
815     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
816         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
817         return HC_ERR_IPC_BAD_PARAM;
818     }
819     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
820     if ((appId == NULL) || (ret != HC_SUCCESS)) {
821         LOGE("get param error, type %d", PARAM_TYPE_APPID);
822         return HC_ERR_IPC_BAD_PARAM;
823     }
824     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUPID, (uint8_t *)&groupId, NULL);
825     if ((groupId == NULL) || (ret != HC_SUCCESS)) {
826         LOGE("get param error, type %d", PARAM_TYPE_GROUPID);
827         return HC_ERR_IPC_BAD_PARAM;
828     }
829 
830     callRet = g_devGroupMgrMethod.getTrustedDevices(osAccountId, appId, groupId, &outDevInfo, &outDevNum);
831     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
832     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT_NUM,
833                                (const uint8_t *)&IPC_RESULT_NUM_2, sizeof(int32_t));
834     if (outDevInfo != NULL) {
835         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DEVICE_INFO,
836             (const uint8_t *)outDevInfo, strlen(outDevInfo) + 1);
837     } else {
838         ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DEVICE_INFO, NULL, 0);
839     }
840     ret += IpcEncodeCallReplay(outCache, PARAM_TYPE_DATA_NUM, (const uint8_t *)&outDevNum, sizeof(int32_t));
841     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
842     g_devGroupMgrMethod.destroyInfo(&outDevInfo);
843     return (ret == HC_SUCCESS) ? ret : HC_ERROR;
844 }
845 
IpcServiceGmIsDeviceInGroup(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)846 static int32_t IpcServiceGmIsDeviceInGroup(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
847 {
848     int32_t callRet;
849     int32_t ret;
850     int32_t inOutLen;
851     int32_t osAccountId;
852     bool bRet = false;
853     const char *appId = NULL;
854     const char *udid = NULL;
855     const char *groupId = NULL;
856 
857     LOGI("starting ...");
858     inOutLen = sizeof(int32_t);
859     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
860     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
861         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
862         return HC_ERR_IPC_BAD_PARAM;
863     }
864     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_APPID, (uint8_t *)&appId, NULL);
865     if ((appId == NULL) || (ret != HC_SUCCESS)) {
866         LOGE("get param error, type %d", PARAM_TYPE_APPID);
867         return HC_ERR_IPC_BAD_PARAM;
868     }
869     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_UDID, (uint8_t *)&udid, NULL);
870     if ((udid == NULL) || (ret != HC_SUCCESS)) {
871         LOGE("get param error, type %d", PARAM_TYPE_UDID);
872         return HC_ERR_IPC_BAD_PARAM;
873     }
874     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_GROUPID, (uint8_t *)&groupId, NULL);
875     if ((groupId == NULL) || (ret != HC_SUCCESS)) {
876         LOGE("get param error, type %d", PARAM_TYPE_GROUPID);
877         return HC_ERR_IPC_BAD_PARAM;
878     }
879 
880     bRet = g_devGroupMgrMethod.isDeviceInGroup(osAccountId, appId, groupId, udid);
881     callRet = ((bRet == true) ? HC_SUCCESS : HC_ERROR);
882     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
883     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
884     return ret;
885 }
886 
IpcServiceGaProcessData(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)887 static int32_t IpcServiceGaProcessData(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
888 {
889     int32_t callRet;
890     int32_t ret;
891     const DeviceAuthCallback *gaCallback = NULL;
892     int64_t authReqId = 0;
893     uint8_t *data = NULL;
894     uint32_t dataLen = 0;
895     int32_t inOutLen;
896     int32_t cbObjIdx = -1;
897 
898     LOGI("starting ...");
899     inOutLen = sizeof(authReqId);
900     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&authReqId, &inOutLen);
901     if ((inOutLen != sizeof(authReqId)) || (ret != HC_SUCCESS)) {
902         LOGE("get param error, type %d", PARAM_TYPE_REQID);
903         return HC_ERR_IPC_BAD_PARAM;
904     }
905     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_COMM_DATA, (uint8_t *)&data, (int32_t *)&dataLen);
906     if ((data == NULL) || (dataLen == 0) || (ret != HC_SUCCESS)) {
907         LOGE("get param error, type %d", PARAM_TYPE_COMM_DATA);
908         return HC_ERR_IPC_BAD_PARAM;
909     }
910     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEV_AUTH_CB, (uint8_t *)&gaCallback, NULL);
911     if (ret != HC_SUCCESS) {
912         LOGE("get param error, type %d", PARAM_TYPE_DEV_AUTH_CB);
913         return ret;
914     }
915     /* add call back */
916     ret = AddIpcCallBackByReqId(authReqId, (const uint8_t *)gaCallback,
917         sizeof(DeviceAuthCallback), CB_TYPE_TMP_DEV_AUTH);
918     if (ret != HC_SUCCESS) {
919         LOGE("add ipc callback failed");
920         return HC_ERROR;
921     }
922     inOutLen = sizeof(cbObjIdx);
923     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_CB_OBJECT, (uint8_t *)&cbObjIdx, &inOutLen);
924     if (ret != HC_SUCCESS) {
925         LOGE("get param error, type %d", PARAM_TYPE_CB_OBJECT);
926         DelIpcCallBackByReqId(authReqId, CB_TYPE_TMP_DEV_AUTH, true);
927         return ret;
928     }
929     AddIpcCbObjByReqId(authReqId, cbObjIdx, CB_TYPE_TMP_DEV_AUTH);
930     InitDeviceAuthCbCtx(&g_authCbAdt, CB_TYPE_TMP_DEV_AUTH);
931     callRet = g_groupAuthMgrMethod.processData(authReqId, data, dataLen, &g_authCbAdt);
932     if (callRet != HC_SUCCESS) {
933         DelIpcCallBackByReqId(authReqId, CB_TYPE_TMP_DEV_AUTH, true);
934     }
935     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
936     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
937     return ret;
938 }
939 
IpcServiceGaAuthDevice(const IpcDataInfo * ipcParams,int32_t paramNum,uintptr_t outCache)940 static int32_t IpcServiceGaAuthDevice(const IpcDataInfo *ipcParams, int32_t paramNum, uintptr_t outCache)
941 {
942     int32_t callRet;
943     int32_t ret;
944     DeviceAuthCallback *gaCallback = NULL;
945     int32_t osAccountId;
946     int64_t authReqId = 0;
947     const char *authParams = NULL;
948     int32_t inOutLen;
949     int32_t cbObjIdx = -1;
950 
951     LOGI("starting ...");
952     inOutLen = sizeof(int32_t);
953     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_OS_ACCOUNT_ID, (uint8_t *)&osAccountId, &inOutLen);
954     if ((inOutLen != sizeof(int32_t)) || (ret != HC_SUCCESS)) {
955         LOGE("get param error, type %d", PARAM_TYPE_OS_ACCOUNT_ID);
956         return HC_ERR_IPC_BAD_PARAM;
957     }
958     inOutLen = sizeof(authReqId);
959     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_REQID, (uint8_t *)&authReqId, &inOutLen);
960     if ((inOutLen != sizeof(authReqId)) || (ret != HC_SUCCESS)) {
961         LOGE("get param error, type %d", PARAM_TYPE_REQID);
962         return HC_ERR_IPC_BAD_PARAM;
963     }
964     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_AUTH_PARAMS, (uint8_t *)&authParams, NULL);
965     if ((authParams == NULL) || (ret != HC_SUCCESS)) {
966         LOGE("get param error, type %d", PARAM_TYPE_AUTH_PARAMS);
967         return HC_ERR_IPC_BAD_PARAM;
968     }
969     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_DEV_AUTH_CB, (uint8_t *)&gaCallback, NULL);
970     if (ret != HC_SUCCESS) {
971         LOGE("get param error, type %d", PARAM_TYPE_DEV_AUTH_CB);
972         return ret;
973     }
974 
975     /* add call back */
976     ret = AddIpcCallBackByReqId(authReqId, (const uint8_t *)gaCallback,
977         sizeof(DeviceAuthCallback), CB_TYPE_TMP_DEV_AUTH);
978     if (ret != HC_SUCCESS) {
979         LOGE("add ipc callback failed");
980         return HC_ERROR;
981     }
982     inOutLen = sizeof(cbObjIdx);
983     ret = GetIpcRequestParamByType(ipcParams, paramNum, PARAM_TYPE_CB_OBJECT, (uint8_t *)&cbObjIdx, &inOutLen);
984     if (ret != HC_SUCCESS) {
985         LOGE("get param error, type %d", PARAM_TYPE_CB_OBJECT);
986         DelIpcCallBackByReqId(authReqId, CB_TYPE_TMP_DEV_AUTH, true);
987         return ret;
988     }
989     AddIpcCbObjByReqId(authReqId, cbObjIdx, CB_TYPE_TMP_DEV_AUTH);
990     InitDeviceAuthCbCtx(&g_authCbAdt, CB_TYPE_TMP_DEV_AUTH);
991     callRet = g_groupAuthMgrMethod.authDevice(osAccountId, authReqId, authParams, &g_authCbAdt);
992     if (callRet != HC_SUCCESS) {
993         DelIpcCallBackByReqId(authReqId, CB_TYPE_TMP_DEV_AUTH, true);
994     }
995     ret = IpcEncodeCallReplay(outCache, PARAM_TYPE_IPC_RESULT, (const uint8_t *)&callRet, sizeof(int32_t));
996     LOGI("process done, call ret %d, ipc ret %d", callRet, ret);
997     return ret;
998 }
999 
AddMethodMap(uintptr_t ipcInstance)1000 static int32_t AddMethodMap(uintptr_t ipcInstance)
1001 {
1002     uint32_t ret;
1003 
1004     // Group Manager Interfaces
1005     ret = SetIpcCallMap(ipcInstance, IpcServiceGmRegCallback, IPC_CALL_ID_REG_CB);
1006     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmUnRegCallback, IPC_CALL_ID_UNREG_CB);
1007     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmRegDataChangeListener, IPC_CALL_ID_REG_LISTENER);
1008     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmUnRegDataChangeListener, IPC_CALL_ID_UNREG_LISTENER);
1009     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmCreateGroup, IPC_CALL_ID_CREATE_GROUP);
1010     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmDelGroup, IPC_CALL_ID_DEL_GROUP);
1011     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmAddMemberToGroup, IPC_CALL_ID_ADD_GROUP_MEMBER);
1012     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmDelMemberFromGroup, IPC_CALL_ID_DEL_GROUP_MEMBER);
1013     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmAddMultiMembersToGroup, IPC_CALL_ID_ADD_MULTI_GROUP_MEMBERS);
1014     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmDelMultiMembersFromGroup, IPC_CALL_ID_DEL_MULTI_GROUP_MEMBERS);
1015     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmProcessData, IPC_CALL_ID_GM_PROC_DATA);
1016     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmApplyRegisterInfo, IPC_CALL_ID_APPLY_REG_INFO);
1017     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmCheckAccessToGroup, IPC_CALL_ID_CHECK_ACCESS_TO_GROUP);
1018     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetPkInfoList, IPC_CALL_ID_GET_PK_INFO_LIST);
1019     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetGroupInfoById, IPC_CALL_ID_GET_GROUP_INFO);
1020     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetGroupInfo, IPC_CALL_ID_SEARCH_GROUPS);
1021     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetJoinedGroups, IPC_CALL_ID_GET_JOINED_GROUPS);
1022     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetRelatedGroups, IPC_CALL_ID_GET_RELATED_GROUPS);
1023     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetDeviceInfoById, IPC_CALL_ID_GET_DEV_INFO_BY_ID);
1024     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmGetTrustedDevices, IPC_CALL_ID_GET_TRUST_DEVICES);
1025     ret &= SetIpcCallMap(ipcInstance, IpcServiceGmIsDeviceInGroup, IPC_CALL_ID_IS_DEV_IN_GROUP);
1026 
1027     // Group Auth Interfaces
1028     ret &= SetIpcCallMap(ipcInstance, IpcServiceGaProcessData, IPC_CALL_ID_GA_PROC_DATA);
1029     ret &= SetIpcCallMap(ipcInstance, IpcServiceGaAuthDevice, IPC_CALL_ID_AUTH_DEVICE);
1030     LOGI("process done, ret %u", ret);
1031     return ret;
1032 }
1033 
DeMainRescInit(uintptr_t * serviceCtx)1034 void DeMainRescInit(uintptr_t *serviceCtx)
1035 {
1036     if (g_devGroupMgrMethod.unRegDataChangeListener != NULL) {
1037         (void)g_devGroupMgrMethod.unRegDataChangeListener(g_serviceAppId);
1038     }
1039     DeInitIpcCallBackList();
1040     DestroyServiceInstance(serviceCtx);
1041     return;
1042 }
1043 
MainRescInit(void)1044 int32_t MainRescInit(void)
1045 {
1046     int32_t ret;
1047     const DeviceGroupManager *gmInst = NULL;
1048     const GroupAuthManager *gaInst = NULL;
1049 
1050     LOGI("starting ...");
1051     ret = InitIpcCallBackList();
1052     if (ret != HC_SUCCESS) {
1053         return ret;
1054     }
1055     gmInst = GetGmInstance();
1056     gaInst = GetGaInstance();
1057     if ((gmInst == NULL) || (gaInst == NULL)) {
1058         DeInitIpcCallBackList();
1059         LOGE("MainInit, GetGmInstance failed");
1060         return HC_ERROR;
1061     }
1062     g_devGroupMgrMethod = (DeviceGroupManager)(*gmInst);
1063     g_groupAuthMgrMethod = (GroupAuthManager)(*gaInst);
1064     InitDevAuthListenerCbCtx(&g_listenCbAdt);
1065     ret = gmInst->regDataChangeListener(g_serviceAppId, &g_listenCbAdt);
1066     if (ret != HC_SUCCESS) {
1067         DeInitIpcCallBackList();
1068         LOGE("MainInit, register ipc listener failed, ret %d", ret);
1069         return HC_ERROR;
1070     }
1071 
1072     LOGI("process done");
1073     return HC_SUCCESS;
1074 }
1075 
main(int32_t argc,char const * argv[])1076 int32_t main(int32_t argc, char const *argv[])
1077 {
1078     uintptr_t serviceCtx = 0x0;
1079     int32_t ret;
1080     HcCondition cond;
1081 
1082     (void)argc;
1083     (void)argv;
1084     LOGI("device authentication service starting ...");
1085     ret = InitDeviceAuthService();
1086     if (ret != HC_SUCCESS) {
1087         LOGE("device auth service main, InitDeviceAuthService failed, ret %d", ret);
1088         return 1;
1089     }
1090 
1091     ret = MainRescInit();
1092     if (ret != HC_SUCCESS) {
1093         DestroyDeviceAuthService();
1094         LOGE("device auth service main, init work failed");
1095         return 1;
1096     }
1097 
1098     ret = AddDevAuthServiceToManager(&serviceCtx);
1099     if (ret != HC_SUCCESS) {
1100         DeMainRescInit(&serviceCtx);
1101         DestroyDeviceAuthService();
1102         serviceCtx = 0x0;
1103         LOGE("device auth service main, AddDevAuthServiceToManager failed, ret %d", ret);
1104         return 1;
1105     }
1106     (void)AddMethodMap(serviceCtx);
1107     LOGI("device authentication service register to IPC manager done, service running...");
1108     (void)memset_s(&cond, sizeof(cond), 0, sizeof(cond));
1109     InitHcCond(&cond, NULL);
1110     cond.wait(&cond);
1111     DestroyHcCond(&cond);
1112     return 0;
1113 }
1114 
1115 #ifdef __cplusplus
1116 }
1117 #endif
1118