1 /*
2  * Copyright (C) 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 "dbinder_ipc_adapter.h"
17 
18 #include <pthread.h>
19 #include <string.h>
20 
21 #include "securec.h"
22 #include "ipc_skeleton.h"
23 #include "rpc_log.h"
24 #include "rpc_errno.h"
25 
IsSameStub(DBinderServiceStub * stub,const char * serviceName,const char * deviceID,uintptr_t binderObject)26 bool IsSameStub(DBinderServiceStub *stub, const char *serviceName,
27     const char *deviceID, uintptr_t binderObject)
28 {
29     if (stub == NULL) {
30         return false;
31     }
32     return (strcmp(stub->serviceName, serviceName) == 0 && strcmp(stub->deviceID, deviceID) == 0
33         && stub->binderObject == binderObject);
34 }
35 
RpcGetSystemAbility(int32_t systemAbility)36 ProxyObject *RpcGetSystemAbility(int32_t systemAbility)
37 {
38     IpcIo data;
39     uint8_t tmpData[RPC_IPC_LENGTH];
40     IpcIoInit(&data, tmpData, RPC_IPC_LENGTH, 0);
41     RPC_LOG_INFO("GetSystemAbility systemAbility %d", systemAbility);
42     WriteInt32(&data, systemAbility);
43 
44     IpcIo reply;
45     MessageOption option = {
46         .flags = TF_OP_SYNC
47     };
48 
49     RPC_LOG_INFO("get system ability from samgr");
50     uintptr_t ptr;
51     int32_t ret = SendRequest(*GetContextObject(), GET_SYSTEM_ABILITY_TRANSACTION, &data, &reply, option, &ptr);
52     if (ret != ERR_NONE) {
53         RPC_LOG_ERROR("GetSystemAbility failed");
54         FreeBuffer((void *)ptr);
55         return NULL;
56     }
57 
58     SvcIdentity svc;
59     ReadRemoteObject(&reply, &svc);
60 
61     ProxyObject *proxyObject = (ProxyObject *)malloc(sizeof(ProxyObject));
62     if (proxyObject == NULL) {
63         FreeBuffer((void *)ptr);
64         return NULL;
65     }
66     proxyObject->proxy = (SvcIdentity *)malloc(sizeof(SvcIdentity));
67     if (proxyObject->proxy == NULL) {
68         free(proxyObject);
69         FreeBuffer((void *)ptr);
70         return NULL;
71     }
72 
73     if (memcpy_s(proxyObject->proxy, sizeof(SvcIdentity), &svc, sizeof(SvcIdentity)) != EOK) {
74         free(proxyObject->proxy);
75         free(proxyObject);
76         FreeBuffer((void *)ptr);
77         return NULL;
78     }
79 
80     FreeBuffer((void *)ptr);
81     return proxyObject;
82 }