1 /*
2 * Copyright (c) 2021-2023 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_info_manager.h"
17
18 #include "comm_log.h"
19 #include "securec.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_errcode.h"
22 #include "softbus_utils.h"
23
24 typedef struct {
25 ListNode node;
26 char name[PKG_NAME_SIZE_MAX]; /* softbus client name */
27 unsigned int handle; /* use for small system device */
28 unsigned int token; /* use for small system device */
29 unsigned int cookie; /* use for small system device */
30 } SoftBusClientInfoNode;
31
32 static SoftBusList *g_clientInfoList = NULL;
33
SERVER_InitClient(void)34 int SERVER_InitClient(void)
35 {
36 if (g_clientInfoList != NULL) {
37 COMM_LOGI(COMM_SVC, "has inited");
38 return SOFTBUS_ERR;
39 }
40
41 g_clientInfoList = CreateSoftBusList();
42 if (g_clientInfoList == NULL) {
43 COMM_LOGE(COMM_SVC, "init client info list failed");
44 return SOFTBUS_MALLOC_ERR;
45 }
46
47 return SOFTBUS_OK;
48 }
49
SERVER_RegisterService(const char * name,const struct CommonScvId * svcId)50 int SERVER_RegisterService(const char *name, const struct CommonScvId *svcId)
51 {
52 if (name == NULL || svcId == NULL) {
53 COMM_LOGE(COMM_SVC, "invalid param");
54 return SOFTBUS_ERR;
55 }
56 COMM_LOGI(COMM_SVC, "new client register=%{public}s", name);
57
58 if (g_clientInfoList == NULL) {
59 COMM_LOGE(COMM_SVC, "not init");
60 return SOFTBUS_ERR;
61 }
62
63 SoftBusClientInfoNode *clientInfo = SoftBusCalloc(sizeof(SoftBusClientInfoNode));
64 if (clientInfo == NULL) {
65 COMM_LOGE(COMM_SVC, "malloc failed");
66 return SOFTBUS_ERR;
67 }
68
69 if (strcpy_s(clientInfo->name, sizeof(clientInfo->name), name) != EOK) {
70 COMM_LOGE(COMM_SVC, "strcpy failed");
71 SoftBusFree(clientInfo);
72 return SOFTBUS_ERR;
73 }
74
75 clientInfo->handle = svcId->handle;
76 clientInfo->token = svcId->token;
77 clientInfo->cookie = svcId->cookie;
78 ListInit(&clientInfo->node);
79
80 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
81 COMM_LOGE(COMM_SVC, "lock failed");
82 SoftBusFree(clientInfo);
83 return SOFTBUS_ERR;
84 }
85
86 ListAdd(&(g_clientInfoList->list), &(clientInfo->node));
87 g_clientInfoList->cnt++;
88
89 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
90 return SOFTBUS_OK;
91 }
92
SERVER_GetIdentityByPkgName(const char * name,struct CommonScvId * svcId)93 int SERVER_GetIdentityByPkgName(const char *name, struct CommonScvId *svcId)
94 {
95 if (name == NULL || svcId == NULL) {
96 COMM_LOGE(COMM_SVC, "invalid param");
97 return SOFTBUS_ERR;
98 }
99
100 if (g_clientInfoList == NULL) {
101 COMM_LOGE(COMM_SVC, "not init");
102 return SOFTBUS_ERR;
103 }
104
105 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
106 COMM_LOGE(COMM_SVC, "lock failed");
107 return SOFTBUS_ERR;
108 }
109
110 SoftBusClientInfoNode *clientInfo = NULL;
111 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
112 if (strcmp(clientInfo->name, name) == 0) {
113 svcId->handle = clientInfo->handle;
114 svcId->token = clientInfo->token;
115 svcId->cookie = clientInfo->cookie;
116 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
117 return SOFTBUS_OK;
118 }
119 }
120
121 COMM_LOGE(COMM_SVC, "not found");
122 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
123 return SOFTBUS_ERR;
124 }
125
SERVER_GetClientInfoNodeNum(int * num)126 int SERVER_GetClientInfoNodeNum(int *num)
127 {
128 if (num == NULL) {
129 COMM_LOGE(COMM_SVC, "num is null");
130 return SOFTBUS_INVALID_PARAM;
131 }
132 *num = 0;
133 if (g_clientInfoList == NULL) {
134 COMM_LOGE(COMM_SVC, "not init");
135 return SOFTBUS_ERR;
136 }
137 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
138 COMM_LOGE(COMM_SVC, "lock failed");
139 return SOFTBUS_ERR;
140 }
141 *num = g_clientInfoList->cnt;
142 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
143 return SOFTBUS_OK;
144 }
145
SERVER_GetAllClientIdentity(struct CommonScvId * svcId,int num)146 int SERVER_GetAllClientIdentity(struct CommonScvId *svcId, int num)
147 {
148 if (svcId == NULL || num == 0) {
149 COMM_LOGE(COMM_SVC, "invalid parameters");
150 return SOFTBUS_ERR;
151 }
152 int32_t i = 0;
153 if (g_clientInfoList == NULL) {
154 COMM_LOGE(COMM_SVC, "not init");
155 return SOFTBUS_ERR;
156 }
157 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
158 COMM_LOGE(COMM_SVC, "lock failed");
159 return SOFTBUS_ERR;
160 }
161 SoftBusClientInfoNode *clientInfo = NULL;
162 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
163 if (i < num) {
164 svcId[i].handle = clientInfo->handle;
165 svcId[i].token = clientInfo->token;
166 svcId[i].cookie = clientInfo->cookie;
167 i++;
168 } else {
169 break;
170 }
171 }
172 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
173 return SOFTBUS_OK;
174 }
175
SERVER_UnregisterService(const char * name)176 void SERVER_UnregisterService(const char *name)
177 {
178 if (name == NULL) {
179 COMM_LOGE(COMM_SVC, "invalid parameters");
180 return;
181 }
182 if (g_clientInfoList == NULL) {
183 COMM_LOGE(COMM_SVC, "server info list not init");
184 return;
185 }
186 if (SoftBusMutexLock(&g_clientInfoList->lock) != SOFTBUS_OK) {
187 COMM_LOGE(COMM_SVC, "lock failed");
188 return;
189 }
190 COMM_LOGE(COMM_SVC, "client service died, remove it from softbus server. name=%{public}s", name);
191 SoftBusClientInfoNode *clientInfo = NULL;
192 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
193 if (strcmp(clientInfo->name, name) == 0) {
194 ListDelete(&(clientInfo->node));
195 SoftBusFree(clientInfo);
196 g_clientInfoList->cnt--;
197 break;
198 }
199 }
200 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
201 }