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