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_info_manager.h"
17
18 #include "securec.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_errcode.h"
21 #include "softbus_log.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 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "has inited");
38 return SOFTBUS_ERR;
39 }
40
41 g_clientInfoList = CreateSoftBusList();
42 if (g_clientInfoList == NULL) {
43 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init service 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 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
54 return SOFTBUS_ERR;
55 }
56 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "new client register:%s", name);
57
58 if (g_clientInfoList == NULL) {
59 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not init");
60 return SOFTBUS_ERR;
61 }
62
63 SoftBusClientInfoNode *clientInfo = SoftBusMalloc(sizeof(SoftBusClientInfoNode));
64 if (clientInfo == NULL) {
65 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "malloc failed");
66 return SOFTBUS_ERR;
67 }
68 (void)memset_s(clientInfo, sizeof(SoftBusClientInfoNode), 0, sizeof(SoftBusClientInfoNode));
69
70 if (strcpy_s(clientInfo->name, sizeof(clientInfo->name), name) != EOK) {
71 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "strcpy failed");
72 SoftBusFree(clientInfo);
73 return SOFTBUS_ERR;
74 }
75
76 clientInfo->handle = svcId->handle;
77 clientInfo->token = svcId->token;
78 clientInfo->cookie = svcId->cookie;
79 ListInit(&clientInfo->node);
80
81 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
82 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock failed");
83 SoftBusFree(clientInfo);
84 return SOFTBUS_ERR;
85 }
86
87 ListAdd(&(g_clientInfoList->list), &(clientInfo->node));
88 g_clientInfoList->cnt++;
89
90 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
91 return SOFTBUS_OK;
92 }
93
SERVER_GetIdentityByPkgName(const char * name,struct CommonScvId * svcId)94 int SERVER_GetIdentityByPkgName(const char *name, struct CommonScvId *svcId)
95 {
96 if (name == NULL || svcId == NULL) {
97 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
98 return SOFTBUS_ERR;
99 }
100
101 if (g_clientInfoList == NULL) {
102 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not init");
103 return SOFTBUS_ERR;
104 }
105
106 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
107 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock failed");
108 return SOFTBUS_ERR;
109 }
110
111 SoftBusClientInfoNode *clientInfo = NULL;
112 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
113 if (strcmp(clientInfo->name, name) == 0) {
114 svcId->handle = clientInfo->handle;
115 svcId->token = clientInfo->token;
116 svcId->cookie = clientInfo->cookie;
117 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
118 return SOFTBUS_OK;
119 }
120 }
121
122 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not found");
123 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
124 return SOFTBUS_ERR;
125 }
126
SERVER_GetClientInfoNodeNum(int * num)127 int SERVER_GetClientInfoNodeNum(int *num)
128 {
129 *num = 0;
130 if (g_clientInfoList == NULL) {
131 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not init");
132 return SOFTBUS_ERR;
133 }
134 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
135 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock failed");
136 return SOFTBUS_ERR;
137 }
138 *num = g_clientInfoList->cnt;
139 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
140 return SOFTBUS_OK;
141 }
142
SERVER_GetAllClientIdentity(struct CommonScvId * svcId,int num)143 int SERVER_GetAllClientIdentity(struct CommonScvId *svcId, int num)
144 {
145 if (svcId == NULL || num == 0) {
146 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid parameters");
147 return SOFTBUS_ERR;
148 }
149 int32_t i = 0;
150 if (g_clientInfoList == NULL) {
151 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not init");
152 return SOFTBUS_ERR;
153 }
154 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
155 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock failed");
156 return SOFTBUS_ERR;
157 }
158 SoftBusClientInfoNode *clientInfo = NULL;
159 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
160 if (i < num) {
161 svcId[i].handle = clientInfo->handle;
162 svcId[i].token = clientInfo->token;
163 svcId[i].cookie = clientInfo->cookie;
164 i++;
165 }
166 }
167 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
168 return SOFTBUS_OK;
169 }
170
SERVER_UnregisterService(const char * name)171 void SERVER_UnregisterService(const char *name)
172 {
173 if (g_clientInfoList == NULL) {
174 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "server info list not init");
175 return;
176 }
177 if (SoftBusMutexLock(&g_clientInfoList->lock) != 0) {
178 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "lock failed");
179 return;
180 }
181 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client service %s died, remove it from softbus server", name);
182 SoftBusClientInfoNode *clientInfo = NULL;
183 LIST_FOR_EACH_ENTRY(clientInfo, &g_clientInfoList->list, SoftBusClientInfoNode, node) {
184 if (strcmp(clientInfo->name, name) == 0) {
185 ListDelete(&(clientInfo->node));
186 SoftBusFree(clientInfo);
187 g_clientInfoList->cnt--;
188 break;
189 }
190 }
191 (void)SoftBusMutexUnlock(&g_clientInfoList->lock);
192 }