• 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 "client_disc_service.h"
17 
18 #include <string.h>
19 #include "softbus_client_frame_manager.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "softbus_log.h"
23 #include "softbus_type_def.h"
24 #include "stdbool.h"
25 
PublishInfoCheck(const PublishInfo * info)26 static int32_t PublishInfoCheck(const PublishInfo *info)
27 {
28     if ((info->mode != DISCOVER_MODE_PASSIVE) && (info->mode != DISCOVER_MODE_ACTIVE)) {
29         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "mode is invalid");
30         return SOFTBUS_INVALID_PARAM;
31     }
32 
33     if ((info->medium < AUTO) || (info->medium > COAP)) {
34         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "medium is invalid");
35         return SOFTBUS_INVALID_PARAM;
36     }
37 
38     if ((info->freq < LOW) || (info->freq > SUPER_HIGH)) {
39         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "freq is invalid");
40         return SOFTBUS_INVALID_PARAM;
41     }
42 
43     if ((info->capabilityData == NULL) && (info->dataLen != 0)) {
44         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data is invalid");
45         return SOFTBUS_INVALID_PARAM;
46     }
47 
48     if (info->dataLen == 0) {
49         return SOFTBUS_OK;
50     }
51 
52     if ((info->dataLen > MAX_CAPABILITYDATA_LEN) ||
53         (strlen((char *)(info->capabilityData)) >= MAX_CAPABILITYDATA_LEN)) {
54         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data exceeds the maximum length");
55         return SOFTBUS_INVALID_PARAM;
56     }
57 
58     return SOFTBUS_OK;
59 }
60 
SubscribeInfoCheck(const SubscribeInfo * info)61 static int32_t SubscribeInfoCheck(const SubscribeInfo *info)
62 {
63     if ((info->mode != DISCOVER_MODE_PASSIVE) && (info->mode != DISCOVER_MODE_ACTIVE)) {
64         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "mode is invalid");
65         return SOFTBUS_INVALID_PARAM;
66     }
67 
68     if ((info->medium < AUTO) || (info->medium > COAP)) {
69         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "medium is invalid");
70         return SOFTBUS_INVALID_PARAM;
71     }
72 
73     if ((info->freq < LOW) || (info->freq > SUPER_HIGH)) {
74         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "freq is invalid");
75         return SOFTBUS_INVALID_PARAM;
76     }
77 
78     if ((info->capabilityData == NULL) && (info->dataLen != 0)) {
79         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data is invalid");
80         return SOFTBUS_INVALID_PARAM;
81     }
82 
83     if (info->dataLen == 0) {
84         return SOFTBUS_OK;
85     }
86 
87     if ((info->dataLen > MAX_CAPABILITYDATA_LEN) ||
88         (strlen((char *)(info->capabilityData)) >= MAX_CAPABILITYDATA_LEN)) {
89         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "data exceeds the maximum length");
90         return SOFTBUS_INVALID_PARAM;
91     }
92 
93     return SOFTBUS_OK;
94 }
95 
PublishService(const char * packageName,const PublishInfo * info,const IPublishCallback * cb)96 int PublishService(const char *packageName, const PublishInfo *info, const IPublishCallback *cb)
97 {
98     if ((packageName == NULL) || (strlen(packageName) >= PKG_NAME_SIZE_MAX) || (info == NULL) || (cb == NULL)) {
99         return SOFTBUS_INVALID_PARAM;
100     }
101 
102     if (InitSoftBus(packageName) != SOFTBUS_OK) {
103         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "init softbus err");
104         return SOFTBUS_DISCOVER_NOT_INIT;
105     }
106 
107     if (CheckPackageName(packageName) != SOFTBUS_OK) {
108         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "check packageName failed");
109         return SOFTBUS_INVALID_PARAM;
110     }
111 
112     if (PublishInfoCheck(info) != SOFTBUS_OK) {
113         return SOFTBUS_INVALID_PARAM;
114     }
115 
116     return PublishServiceInner(packageName, info, cb);
117 }
118 
UnPublishService(const char * packageName,int publishId)119 int UnPublishService(const char *packageName, int publishId)
120 {
121     if ((packageName == NULL) || (strlen(packageName) >= PKG_NAME_SIZE_MAX)) {
122         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "invalid packageName");
123         return SOFTBUS_INVALID_PARAM;
124     }
125 
126     if (CheckPackageName(packageName) != SOFTBUS_OK) {
127         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "check packageName failed");
128         return SOFTBUS_INVALID_PARAM;
129     }
130 
131     return UnPublishServiceInner(packageName, publishId);
132 }
133 
StartDiscovery(const char * packageName,const SubscribeInfo * info,const IDiscoveryCallback * cb)134 int StartDiscovery(const char *packageName, const SubscribeInfo *info, const IDiscoveryCallback *cb)
135 {
136     if ((packageName == NULL) || (strlen(packageName) >= PKG_NAME_SIZE_MAX) || (info == NULL) || (cb == NULL)) {
137         return SOFTBUS_INVALID_PARAM;
138     }
139     if (InitSoftBus(packageName) != SOFTBUS_OK) {
140         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "init softbus err");
141         return SOFTBUS_DISCOVER_NOT_INIT;
142     }
143     if (CheckPackageName(packageName) != SOFTBUS_OK) {
144         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "check packageName failed");
145         return SOFTBUS_INVALID_PARAM;
146     }
147     if (SubscribeInfoCheck(info) != SOFTBUS_OK) {
148         return SOFTBUS_INVALID_PARAM;
149     }
150     return StartDiscoveryInner(packageName, info, cb);
151 }
152 
StopDiscovery(const char * packageName,int subscribeId)153 int StopDiscovery(const char *packageName, int subscribeId)
154 {
155     if ((packageName == NULL) || (strlen(packageName) >= PKG_NAME_SIZE_MAX)) {
156         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "invalid packageName");
157         return SOFTBUS_INVALID_PARAM;
158     }
159 
160     if (CheckPackageName(packageName) != SOFTBUS_OK) {
161         SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "check packageName failed");
162         return SOFTBUS_INVALID_PARAM;
163     }
164 
165     return StopDiscoveryInner(packageName, subscribeId);
166 }
167