1 /*
2 * Copyright (c) 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 "client_disc_manager.h"
17
18 #include "disc_log.h"
19 #include "disc_server_proxy.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_errcode.h"
22
23 typedef struct {
24 IPublishCallback publishCb;
25 IDiscoveryCallback subscribeCb;
26 } DiscInfo;
27
28 static DiscInfo *g_discInfo = NULL;
29
PublishServiceInner(const char * packageName,const PublishInfo * info,const IPublishCallback * cb)30 int32_t PublishServiceInner(const char *packageName, const PublishInfo *info, const IPublishCallback *cb)
31 {
32 g_discInfo->publishCb = *cb;
33
34 int32_t ret = ServerIpcPublishService(packageName, info);
35 if (ret != SOFTBUS_OK) {
36 DISC_LOGE(DISC_SDK, "Server PublishService failed, ret=%{public}d", ret);
37 return ret;
38 }
39
40 return SOFTBUS_OK;
41 }
42
UnpublishServiceInner(const char * packageName,int32_t publishId)43 int32_t UnpublishServiceInner(const char *packageName, int32_t publishId)
44 {
45 int32_t ret = ServerIpcUnPublishService(packageName, publishId);
46 if (ret != SOFTBUS_OK) {
47 DISC_LOGE(DISC_SDK, "Server UnPublishService failed, ret=%{public}d", ret);
48 return ret;
49 }
50
51 return SOFTBUS_OK;
52 }
53
StartDiscoveryInner(const char * packageName,const SubscribeInfo * info,const IDiscoveryCallback * cb)54 int32_t StartDiscoveryInner(const char *packageName, const SubscribeInfo *info, const IDiscoveryCallback *cb)
55 {
56 if (packageName == NULL || info == NULL || cb == NULL) {
57 DISC_LOGE(DISC_SDK, "invalid parameter:null");
58 return SOFTBUS_INVALID_PARAM;
59 }
60 g_discInfo->subscribeCb = *cb;
61 int32_t ret = ServerIpcStartDiscovery(packageName, info);
62 if (ret != SOFTBUS_OK) {
63 DISC_LOGE(DISC_SDK, "Server StartDiscovery failed, ret=%{public}d", ret);
64 return ret;
65 }
66
67 return SOFTBUS_OK;
68 }
69
StopDiscoveryInner(const char * packageName,int32_t subscribeId)70 int32_t StopDiscoveryInner(const char *packageName, int32_t subscribeId)
71 {
72 int32_t ret = ServerIpcStopDiscovery(packageName, subscribeId);
73 if (ret != SOFTBUS_OK) {
74 DISC_LOGE(DISC_SDK, "Server StopDiscovery failed, ret=%{public}d", ret);
75 return ret;
76 }
77
78 return SOFTBUS_OK;
79 }
80
DiscClientInit(void)81 int32_t DiscClientInit(void)
82 {
83 if (g_discInfo != NULL) {
84 SoftBusFree(g_discInfo);
85 }
86 g_discInfo = (DiscInfo *)SoftBusCalloc(sizeof(DiscInfo));
87 if (g_discInfo == NULL) {
88 DISC_LOGE(DISC_INIT, "Calloc failed");
89 return SOFTBUS_MALLOC_ERR;
90 }
91 if (DiscServerProxyInit() != SOFTBUS_OK) {
92 DISC_LOGE(DISC_INIT, "disc server proxy init failed.");
93 SoftBusFree(g_discInfo);
94 return SOFTBUS_ERR;
95 }
96 DISC_LOGI(DISC_INIT, "Init success as client side");
97 return SOFTBUS_OK;
98 }
99
DiscClientDeinit(void)100 void DiscClientDeinit(void)
101 {
102 if (g_discInfo == NULL) {
103 return;
104 }
105 SoftBusFree(g_discInfo);
106 g_discInfo = NULL;
107 DiscServerProxyDeInit();
108 DISC_LOGI(DISC_CONTROL, "DeInit success");
109 }
110
DiscClientOnDeviceFound(const DeviceInfo * device)111 void DiscClientOnDeviceFound(const DeviceInfo *device)
112 {
113 if (device == NULL) {
114 DISC_LOGE(DISC_SDK, "invalid parameter:null");
115 return;
116 }
117 DISC_LOGI(DISC_SDK, "Sdk OnDeviceFound, capabilityBitmap=%{public}d",
118 device->capabilityBitmap[0]);
119 if (g_discInfo == NULL) {
120 DISC_LOGE(DISC_SDK, "OnDeviceFound callback failed!");
121 return;
122 }
123 g_discInfo->subscribeCb.OnDeviceFound(device);
124 }
125
DiscClientOnDiscoverySuccess(int32_t subscribeId)126 void DiscClientOnDiscoverySuccess(int32_t subscribeId)
127 {
128 DISC_LOGI(DISC_SDK, "Sdk OnDiscoverySuccess, subscribeId=%{public}d", subscribeId);
129 if (g_discInfo == NULL) {
130 DISC_LOGE(DISC_SDK, "OnDiscoverySuccess callback failed!");
131 return;
132 }
133 g_discInfo->subscribeCb.OnDiscoverySuccess(subscribeId);
134 }
135
DiscClientOnDiscoverFailed(int32_t subscribeId,DiscoveryFailReason failReason)136 void DiscClientOnDiscoverFailed(int32_t subscribeId, DiscoveryFailReason failReason)
137 {
138 DISC_LOGI(DISC_SDK, "Sdk OnDiscoverFailed, subscribeId=%{public}d", subscribeId);
139 if (g_discInfo == NULL) {
140 DISC_LOGE(DISC_SDK, "OnDiscoverFailed callback failed!");
141 return;
142 }
143 g_discInfo->subscribeCb.OnDiscoverFailed(subscribeId, failReason);
144 }
145
DiscClientOnPublishSuccess(int32_t publishId)146 void DiscClientOnPublishSuccess(int32_t publishId)
147 {
148 DISC_LOGI(DISC_SDK, "Sdk OnPublishSuccess, publishId=%{public}d", publishId);
149 if (g_discInfo == NULL) {
150 DISC_LOGE(DISC_SDK, "OnPublishSuccess callback failed!");
151 return;
152 }
153 g_discInfo->publishCb.OnPublishSuccess(publishId);
154 }
155
DiscClientOnPublishFail(int32_t publishId,PublishFailReason reason)156 void DiscClientOnPublishFail(int32_t publishId, PublishFailReason reason)
157 {
158 DISC_LOGI(DISC_SDK, "Sdk OnPublishFail, publishId=%{public}d", publishId);
159 if (g_discInfo == NULL) {
160 DISC_LOGE(DISC_SDK, "OnPublishFail callback failed!");
161 return;
162 }
163 g_discInfo->publishCb.OnPublishFail(publishId, reason);
164 }