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