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