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 #include "default_client_adapter.h"
16 #include "samgr_server.h"
17 #include "dbinder_service.h"
18 #include "ipc_process_skeleton.h"
19 #define MAX_COUNT_NUM 2
20 static pthread_mutex_t g_handleMutex = PTHREAD_MUTEX_INITIALIZER;
21 static int32_t g_handle = 0;
22 
GetNextHandle(void)23 static int32_t GetNextHandle(void)
24 {
25     pthread_mutex_lock(&g_handleMutex);
26     int32_t handle = ++g_handle;
27     pthread_mutex_unlock(&g_handleMutex);
28     return handle;
29 }
30 
GetRemoteSaIdInner(const char * service,const char * feature)31 uintptr_t GetRemoteSaIdInner(const char *service, const char *feature)
32 {
33     SaNode *saNode = GetSaNodeBySaName(service, feature);
34     if (saNode != NULL) {
35         return saNode->saId;
36     }
37     return 0;
38 }
39 
ProxyInvokeArgInner(IpcIo * reply,IClientHeader * header)40 void ProxyInvokeArgInner(IpcIo *reply, IClientHeader *header)
41 {
42     WriteInt32(reply, (int32_t)header->saId);
43 }
44 
QueryRemoteIdentityInner(const char * deviceId,const char * service,const char * feature)45 SvcIdentity QueryRemoteIdentityInner(const char *deviceId, const char *service, const char *feature)
46 {
47     char saName[MAX_COUNT_NUM * MAX_NAME_LEN + MAX_COUNT_NUM];
48     int count = sprintf_s(saName, MAX_COUNT_NUM * MAX_NAME_LEN + MAX_COUNT_NUM,
49         "%s#%s", service?service:"", feature?feature:"");
50     HILOG_INFO(HILOG_MODULE_SAMGR, "saName %s, make remote binder start", saName);
51     SvcIdentity target = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX};
52     if (count < 0) {
53         HILOG_ERROR(HILOG_MODULE_SAMGR, "sprintf_s failed");
54         return target;
55     }
56     SaNode *saNode = GetSaNodeBySaName(service, feature);
57     if (saNode == NULL) {
58         HILOG_ERROR(HILOG_MODULE_SAMGR, "service: %s feature: %s have no saId in sa map", service, feature);
59         return target;
60     }
61     int32_t ret = MakeRemoteBinder(saName, strlen(saName), deviceId, strlen(deviceId), saNode->saId, 0,
62                                    &target);
63     if (ret != EC_SUCCESS) {
64         HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder failed");
65         return target;
66     }
67     target.handle = GetNextHandle();
68     WaitForProxyInit(&target);
69     HILOG_ERROR(HILOG_MODULE_SAMGR, "MakeRemoteBinder sid handle=%d", target.handle);
70     return target;
71 }