• 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 static void ClientInitializeRegistry(void);
41 RemoteRegister g_remoteRegister;
42 static BOOL g_isAbilityInited = FALSE;
43 
SAMGR_RegisterServiceApi(const char * service,const char * feature,const Identity * identity,IUnknown * iUnknown)44 int SAMGR_RegisterServiceApi(const char *service, const char *feature, const Identity *identity, IUnknown *iUnknown)
45 {
46     if (service == NULL) {
47         return EC_INVALID;
48     }
49     ClientInitializeRegistry();
50     MUTEX_Lock(g_remoteRegister.mtx);
51     SaName saName = {service, feature};
52     int32 token = SAMGR_AddRouter(g_remoteRegister.endpoint, &saName, identity, iUnknown);
53 #ifdef MINI_SAMGR_LITE_RPC
54     char saNameStr[2 * MAX_NAME_LEN + 2];
55     (void)sprintf_s(saNameStr, 2 * MAX_NAME_LEN + 2, "%s#%s", service, feature?feature:"");
56     HILOG_INFO(HILOG_MODULE_SAMGR, "register saname: %s index: %d\n", saNameStr, token);
57     SaNode *saNode = GetSaNodeBySaName(service, feature);
58     if (saNode != NULL) {
59         RegisterRemoteProxy(saNameStr, strlen(saNameStr), saNode->saId);
60     }
61 #endif
62     MUTEX_Unlock(g_remoteRegister.mtx);
63     if (token < 0 || !g_remoteRegister.endpoint->running) {
64         return token;
65     }
66 #ifndef MINI_SAMGR_LITE_RPC
67     SAMGR_ProcPolicy(g_remoteRegister.endpoint, &saName, token);
68 #endif
69     return EC_SUCCESS;
70 }
71 
SAMGR_FindServiceApi(const char * service,const char * feature)72 IUnknown *SAMGR_FindServiceApi(const char *service, const char *feature)
73 {
74     if (service == NULL) {
75         return NULL;
76     }
77     ClientInitializeRegistry();
78     SaName key = {service, feature};
79     int index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
80     if (index != INVALID_INDEX) {
81         return VECTOR_At(&g_remoteRegister.clients, index);
82     }
83     IUnknown *proxy = SAMGR_CreateIProxy(service, feature);
84     if (proxy == NULL) {
85         return NULL;
86     }
87     MUTEX_Lock(g_remoteRegister.mtx);
88     index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
89     if (index != INVALID_INDEX) {
90         MUTEX_Unlock(g_remoteRegister.mtx);
91         proxy->Release(proxy);
92         return VECTOR_At(&g_remoteRegister.clients, index);
93     }
94     VECTOR_Add(&g_remoteRegister.clients, proxy);
95     MUTEX_Unlock(g_remoteRegister.mtx);
96     HILOG_INFO(HILOG_MODULE_SAMGR, "Create remote sa proxy<%s, %s>!", service, feature);
97     return proxy;
98 }
99 
SAMGR_RegisterSystemCapabilityApi(const char * sysCap,BOOL isReg)100 int32 SAMGR_RegisterSystemCapabilityApi(const char *sysCap, BOOL isReg)
101 {
102     ClientInitializeRegistry();
103     return SAMGR_AddSysCap(g_remoteRegister.endpoint, sysCap, isReg);
104 }
105 
SAMGR_QuerySystemCapabilityApi(const char * sysCap)106 BOOL SAMGR_QuerySystemCapabilityApi(const char *sysCap)
107 {
108     ClientInitializeRegistry();
109     BOOL isReg = FALSE;
110     if (SAMGR_GetSysCap(g_remoteRegister.endpoint, sysCap, &isReg) != EC_SUCCESS) {
111         return FALSE;
112     }
113     return isReg;
114 }
115 
SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN],int32 * size)116 int32 SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN], int32 *size)
117 {
118     ClientInitializeRegistry();
119     return SAMGR_GetSystemCapabilities(g_remoteRegister.endpoint, sysCaps, size);
120 }
121 
ClearRegistry(void)122 static void ClearRegistry(void)
123 {
124     if (g_remoteRegister.endpoint == NULL) {
125         return;
126     }
127     HILOG_INFO(HILOG_MODULE_SAMGR, "Clear Client Registry!");
128     SAMGR_Free(g_remoteRegister.mtx);
129     g_remoteRegister.mtx = NULL;
130     VECTOR_Clear(&(g_remoteRegister.clients));
131     VECTOR_Clear(&(g_remoteRegister.endpoint->routers));
132     SAMGR_Free(g_remoteRegister.endpoint);
133     g_remoteRegister.endpoint = NULL;
134 }
135 
ClientInitializeRegistry(void)136 static void ClientInitializeRegistry(void)
137 {
138 #ifndef MINI_SAMGR_LITE_RPC
139     if (getuid() >= ABILITY_UID_START && !g_isAbilityInited) {
140         ClearRegistry();
141         g_isAbilityInited = TRUE;
142     }
143 #endif
144 
145     if (g_remoteRegister.endpoint != NULL) {
146         return;
147     }
148     HILOG_INFO(HILOG_MODULE_SAMGR, "Initialize Client Registry!");
149     MUTEX_GlobalLock();
150     if (g_remoteRegister.endpoint == NULL) {
151         g_remoteRegister.mtx = MUTEX_InitValue();
152         g_remoteRegister.clients = VECTOR_Make((VECTOR_Key)SAMGR_GetSAName, (VECTOR_Compare)SAMGR_CompareSAName);
153         g_remoteRegister.endpoint = SAMGR_CreateEndpoint("ipc client", NULL);
154     }
155     MUTEX_GlobalUnlock();
156 }
157