• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "remote_register.h"
16 #include <ohos_errno.h>
17 #include <ohos_init.h>
18 #include <log.h>
19 #ifdef MINI_SAMGR_LITE_RPC
20 #include "dbinder_service.h"
21 #include "samgr_server.h"
22 #endif
23 #include "default_client.h"
24 #include "iproxy_client.h"
25 #include "memory_adapter.h"
26 #include "policy_define.h"
27 #include "pthread.h"
28 #include "samgr_lite.h"
29 #include "thread_adapter.h"
30 #include "unistd.h"
31 
32 #undef LOG_TAG
33 #undef LOG_DOMAIN
34 #define LOG_TAG "Samgr"
35 #define LOG_DOMAIN 0xD001800
36 
37 #define RETRY_INTERVAL 2
38 #define MAX_RETRY_TIMES 10
39 #define ABILITY_UID_START 100
40 #define SA_NAME_NUM 2
41 static void ClientInitializeRegistry(void);
42 RemoteRegister g_remoteRegister;
43 static BOOL g_isAbilityInited = FALSE;
44 
SAMGR_RegisterServiceApi(const char * service,const char * feature,const Identity * identity,IUnknown * iUnknown)45 int SAMGR_RegisterServiceApi(const char *service, const char *feature, const Identity *identity, IUnknown *iUnknown)
46 {
47     if (service == NULL) {
48         return EC_INVALID;
49     }
50     ClientInitializeRegistry();
51     MUTEX_Lock(g_remoteRegister.mtx);
52     SaName saName = {service, feature};
53     int32 token = SAMGR_AddRouter(g_remoteRegister.endpoint, &saName, identity, iUnknown);
54 #ifdef MINI_SAMGR_LITE_RPC
55     char saNameStr[SA_NAME_NUM * MAX_NAME_LEN + SA_NAME_NUM];
56     (void)sprintf_s(saNameStr, SA_NAME_NUM * MAX_NAME_LEN + SA_NAME_NUM, "%s#%s", service, feature?feature:"");
57     HILOG_INFO(HILOG_MODULE_SAMGR, "register saname: %s index: %d\n", saNameStr, token);
58     SaNode *saNode = GetSaNodeBySaName(service, feature);
59     if (saNode != NULL) {
60         RegisterRemoteProxy(saNameStr, strlen(saNameStr), saNode->saId);
61     }
62 #endif
63     MUTEX_Unlock(g_remoteRegister.mtx);
64     if (token < 0 || !g_remoteRegister.endpoint->running) {
65         return token;
66     }
67 #ifndef MINI_SAMGR_LITE_RPC
68     SAMGR_ProcPolicy(g_remoteRegister.endpoint, &saName, token);
69 #endif
70     return EC_SUCCESS;
71 }
72 
SAMGR_FindServiceApi(const char * service,const char * feature)73 IUnknown *SAMGR_FindServiceApi(const char *service, const char *feature)
74 {
75     if (service == NULL) {
76         return NULL;
77     }
78     ClientInitializeRegistry();
79     SaName key = {service, feature};
80     int index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
81     if (index != INVALID_INDEX) {
82         IUnknown *proxy = VECTOR_At(&g_remoteRegister.clients, index);
83         if (SAMGR_IsProxyValid(proxy)) {
84             return proxy;
85         }
86     }
87     IUnknown *proxy = SAMGR_CreateIProxy(service, feature);
88     if (proxy == NULL) {
89         return NULL;
90     }
91     MUTEX_Lock(g_remoteRegister.mtx);
92     index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
93     if (index != INVALID_INDEX) {
94         IUnknown *oldProxy = VECTOR_Swap(&g_remoteRegister.clients, index, proxy);
95         MUTEX_Unlock(g_remoteRegister.mtx);
96         if (oldProxy != NULL) {
97             oldProxy->Release(oldProxy);
98         }
99         return proxy;
100     }
101     VECTOR_Add(&g_remoteRegister.clients, proxy);
102     MUTEX_Unlock(g_remoteRegister.mtx);
103     HILOG_INFO(HILOG_MODULE_SAMGR, "Create remote sa proxy<%s, %s>!", service, feature);
104     return proxy;
105 }
106 
SAMGR_RegisterSystemCapabilityApi(const char * sysCap,BOOL isReg)107 int32 SAMGR_RegisterSystemCapabilityApi(const char *sysCap, BOOL isReg)
108 {
109     ClientInitializeRegistry();
110     return SAMGR_AddSysCap(g_remoteRegister.endpoint, sysCap, isReg);
111 }
112 
SAMGR_QuerySystemCapabilityApi(const char * sysCap)113 BOOL SAMGR_QuerySystemCapabilityApi(const char *sysCap)
114 {
115     ClientInitializeRegistry();
116     BOOL isReg = FALSE;
117     if (SAMGR_GetSysCap(g_remoteRegister.endpoint, sysCap, &isReg) != EC_SUCCESS) {
118         return FALSE;
119     }
120     return isReg;
121 }
122 
SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN],int32 * size)123 int32 SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN], int32 *size)
124 {
125     ClientInitializeRegistry();
126     return SAMGR_GetSystemCapabilities(g_remoteRegister.endpoint, sysCaps, size);
127 }
128 
ClearRegistry(void)129 static void ClearRegistry(void)
130 {
131     if (g_remoteRegister.endpoint == NULL) {
132         return;
133     }
134     HILOG_INFO(HILOG_MODULE_SAMGR, "Clear Client Registry!");
135     SAMGR_Free(g_remoteRegister.mtx);
136     g_remoteRegister.mtx = NULL;
137     VECTOR_Clear(&(g_remoteRegister.clients));
138     VECTOR_Clear(&(g_remoteRegister.endpoint->routers));
139     SAMGR_Free(g_remoteRegister.endpoint);
140     g_remoteRegister.endpoint = NULL;
141 }
142 
ClientInitializeRegistry(void)143 static void ClientInitializeRegistry(void)
144 {
145 #ifndef MINI_SAMGR_LITE_RPC
146     if (getuid() >= ABILITY_UID_START && !g_isAbilityInited) {
147         ClearRegistry();
148         g_isAbilityInited = TRUE;
149     }
150 #endif
151 
152     if (g_remoteRegister.endpoint != NULL) {
153         return;
154     }
155     HILOG_INFO(HILOG_MODULE_SAMGR, "Initialize Client Registry!");
156     MUTEX_GlobalLock();
157     if (g_remoteRegister.endpoint == NULL) {
158         g_remoteRegister.mtx = MUTEX_InitValue();
159         g_remoteRegister.clients = VECTOR_Make((VECTOR_Key)SAMGR_GetSAName, (VECTOR_Compare)SAMGR_CompareSAName);
160         g_remoteRegister.endpoint = SAMGR_CreateEndpoint("ipc client", NULL);
161     }
162     MUTEX_GlobalUnlock();
163 }
164