• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "softbus_client_frame_manager.h"
17 
18 #include <securec.h>
19 #include <string.h>
20 
21 #include "client_bus_center_manager.h"
22 #include "client_disc_manager.h"
23 #include "client_trans_session_manager.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_client_event_manager.h"
26 #include "softbus_client_stub_interface.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29 #include "softbus_feature_config.h"
30 #include "softbus_log.h"
31 #include "softbus_utils.h"
32 
33 static bool g_isInited = false;
34 static pthread_mutex_t g_isInitedLock = PTHREAD_MUTEX_INITIALIZER;
35 
36 typedef struct PkgNameInfo {
37     ListNode node;
38     char pkgName[PKG_NAME_SIZE_MAX];
39 } PkgNameInfo;
40 
41 static pthread_mutex_t g_pkgNameLock = PTHREAD_MUTEX_INITIALIZER;
42 static LIST_HEAD(g_pkgNameList);
43 
CheckPkgNameInfo(const char * pkgName)44 static bool CheckPkgNameInfo(const char *pkgName)
45 {
46     ListNode *item = NULL;
47     PkgNameInfo *info = NULL;
48     uint32_t totalNum = 0;
49     LIST_FOR_EACH(item, &g_pkgNameList) {
50         totalNum++;
51         info = LIST_ENTRY(item, PkgNameInfo, node);
52         if (strcmp(info->pkgName, pkgName) == 0) {
53             SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "exist same pkg name");
54             return false;
55         }
56     }
57     if (totalNum > SOFTBUS_PKGNAME_MAX_NUM) {
58         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "number of pkgName exceeds maximum");
59         return false;
60     }
61     return true;
62 }
63 
AddClientPkgName(const char * pkgName,bool isInit)64 static int32_t AddClientPkgName(const char *pkgName, bool isInit)
65 {
66     if (pthread_mutex_lock(&g_pkgNameLock) != SOFTBUS_OK) {
67         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock init failed");
68         return SOFTBUS_LOCK_ERR;
69     }
70     if (CheckPkgNameInfo(pkgName) == false) {
71         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Add CheckPkgNameInfo failed.");
72         (void)pthread_mutex_unlock(&g_pkgNameLock);
73         return SOFTBUS_ERR;
74     }
75     PkgNameInfo *info = (PkgNameInfo *)SoftBusCalloc(sizeof(PkgNameInfo));
76     if (info == NULL) {
77         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Create PkgNameInfo malloc fail.");
78         pthread_mutex_unlock(&g_pkgNameLock);
79         return SOFTBUS_MEM_ERR;
80     }
81     if (strcpy_s(info->pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
82         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Add strcpy_s failed.");
83         SoftBusFree(info);
84         (void)pthread_mutex_unlock(&g_pkgNameLock);
85         return SOFTBUS_MEM_ERR;
86     }
87     ListAdd(&g_pkgNameList, &info->node);
88     if (!isInit) {
89         (void)pthread_mutex_unlock(&g_pkgNameLock);
90         return SOFTBUS_OK;
91     }
92     int32_t ret = ClientRegisterService(info->pkgName);
93     if (ret != SOFTBUS_OK) {
94         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ClientRegisterService failed. ret = %d", ret);
95         ListDelete(&info->node);
96         SoftBusFree(info);
97         (void)pthread_mutex_unlock(&g_pkgNameLock);
98         return ret;
99     }
100     (void)pthread_mutex_unlock(&g_pkgNameLock);
101     SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ClientRegisterService success");
102     return SOFTBUS_OK;
103 }
104 
FreeClientPkgName(void)105 static void FreeClientPkgName(void)
106 {
107     if (pthread_mutex_lock(&g_pkgNameLock) != SOFTBUS_OK) {
108         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock init failed");
109         return;
110     }
111     ListNode *item = NULL;
112     ListNode *nextItem = NULL;
113     PkgNameInfo *info = NULL;
114     LIST_FOR_EACH_SAFE(item, nextItem, &g_pkgNameList) {
115         info = LIST_ENTRY(item, PkgNameInfo, node);
116         ListDelete(&info->node);
117         SoftBusFree(info);
118     }
119     (void)pthread_mutex_unlock(&g_pkgNameLock);
120 }
121 
ClientModuleDeinit(void)122 static void ClientModuleDeinit(void)
123 {
124     EventClientDeinit();
125     BusCenterClientDeinit();
126     TransClientDeinit();
127     DiscClientDeinit();
128 }
129 
ClientModuleInit(void)130 static int32_t ClientModuleInit(void)
131 {
132     SoftbusConfigInit();
133     if (EventClientInit() == SOFTBUS_ERR) {
134         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init event manager failed");
135         goto ERR_EXIT;
136     }
137 
138     if (BusCenterClientInit() == SOFTBUS_ERR) {
139         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init bus center failed");
140         goto ERR_EXIT;
141     }
142 
143     if (DiscClientInit() == SOFTBUS_ERR) {
144         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init service manager failed");
145         goto ERR_EXIT;
146     }
147 
148     if (TransClientInit() == SOFTBUS_ERR) {
149         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init connect manager failed");
150         goto ERR_EXIT;
151     }
152 
153     return SOFTBUS_OK;
154 
155 ERR_EXIT:
156     SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus sdk frame init failed.");
157     ClientModuleDeinit();
158     return SOFTBUS_ERR;
159 }
160 
InitSoftBus(const char * pkgName)161 int32_t InitSoftBus(const char *pkgName)
162 {
163     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX)) {
164         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init softbus sdk fail.");
165         return SOFTBUS_ERR;
166     }
167 
168     if (g_isInited == true) {
169         (void)pthread_mutex_lock(&g_isInitedLock);
170         (void)AddClientPkgName(pkgName, g_isInited);
171         pthread_mutex_unlock(&g_isInitedLock);
172         return SOFTBUS_OK;
173     }
174 
175     if (pthread_mutex_lock(&g_isInitedLock) != SOFTBUS_OK) {
176         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock init failed");
177         return SOFTBUS_LOCK_ERR;
178     }
179 
180     if (g_isInited == true) {
181         (void)AddClientPkgName(pkgName, g_isInited);
182         pthread_mutex_unlock(&g_isInitedLock);
183         return SOFTBUS_OK;
184     }
185 
186     if (AddClientPkgName(pkgName, g_isInited) != SOFTBUS_OK) {
187         pthread_mutex_unlock(&g_isInitedLock);
188         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "AddClientPkgName failed.");
189         return SOFTBUS_MEM_ERR;
190     }
191     if (SoftBusTimerInit() != SOFTBUS_OK) {
192         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client timer init fail");
193         FreeClientPkgName();
194         pthread_mutex_unlock(&g_isInitedLock);
195         return SOFTBUS_ERR;
196     }
197 
198     if (ClientModuleInit() != SOFTBUS_OK) {
199         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ctx init fail");
200         FreeClientPkgName();
201         pthread_mutex_unlock(&g_isInitedLock);
202         return SOFTBUS_ERR;
203     }
204 
205     if (ClientStubInit() != SOFTBUS_OK) {
206         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "service init fail");
207         FreeClientPkgName();
208         pthread_mutex_unlock(&g_isInitedLock);
209         return SOFTBUS_ERR;
210     }
211 
212     g_isInited = true;
213     pthread_mutex_unlock(&g_isInitedLock);
214     SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "softbus sdk frame init success.");
215     return SOFTBUS_OK;
216 }
217 
GetSoftBusClientNameList(char * pkgList[],uint32_t len)218 uint32_t GetSoftBusClientNameList(char *pkgList[], uint32_t len)
219 {
220     if (pkgList == NULL || len == 0) {
221         return 0;
222     }
223     if (pthread_mutex_lock(&g_pkgNameLock) != SOFTBUS_OK) {
224         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock init failed");
225         return 0;
226     }
227     ListNode *item = NULL;
228     uint32_t subscript = 0;
229     LIST_FOR_EACH(item, &g_pkgNameList) {
230         PkgNameInfo *info = LIST_ENTRY(item, PkgNameInfo, node);
231         char *pkgName = (char *)SoftBusCalloc(PKG_NAME_SIZE_MAX);
232         if (pkgName == NULL) {
233             SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get client name malloc fail");
234             goto EXIT;
235         }
236         if (strcpy_s(pkgName, PKG_NAME_SIZE_MAX, info->pkgName) != EOK) {
237             SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get client name strcpy_s failed");
238             SoftBusFree(pkgName);
239             goto EXIT;
240         }
241         pkgList[subscript] = pkgName;
242         subscript++;
243         if (subscript >= len) {
244             break;
245         }
246     }
247     (void)pthread_mutex_unlock(&g_pkgNameLock);
248     return subscript;
249 
250 EXIT:
251     for (uint32_t i = 0; i < subscript; i++) {
252         SoftBusFree(pkgList[i]);
253     }
254     (void)pthread_mutex_unlock(&g_pkgNameLock);
255     return 0;
256 }
257 
CheckPackageName(const char * pkgName)258 int32_t CheckPackageName(const char *pkgName)
259 {
260 #ifdef __LITEOS_M__
261     return SOFTBUS_OK;
262 #endif
263     if (pthread_mutex_lock(&g_pkgNameLock) != SOFTBUS_OK) {
264         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock init failed");
265         return SOFTBUS_INVALID_PKGNAME;
266     }
267     ListNode *item = NULL;
268     PkgNameInfo *info = NULL;
269     LIST_FOR_EACH(item, &g_pkgNameList) {
270         info = LIST_ENTRY(item, PkgNameInfo, node);
271         if (strcmp(info->pkgName, pkgName) == 0) {
272             (void)pthread_mutex_unlock(&g_pkgNameLock);
273             return SOFTBUS_OK;
274         }
275     }
276     (void)pthread_mutex_unlock(&g_pkgNameLock);
277     return SOFTBUS_INVALID_PKGNAME;
278 }
279 
280