1 /* 2 * Copyright (c) 2025 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 #ifndef SOFTBUS_SERVICE_DISCOVERY_H 17 #define SOFTBUS_SERVICE_DISCOVERY_H 18 19 #include "softbus_service_type.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /** 26 * @brief Register the service to softbus. 27 * 28 * Registration services will not trigger broadcasts or scans. 29 * Service information is cached to softbus. 30 * 31 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 32 * @param serviceInfo Indicates the pointer to the service info. 33 * 34 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>serviceInfo</b> is invalid. 35 * @return Returns <b>SOFTBUS_SERVICE_ALREADY_EXISTS</b> if the service is already registered. 36 * @return Returns <b>SOFTBUS_OK</b> if the service register is successful. 37 * 38 * @since 6.0 39 * @version 1.0 40 */ 41 int32_t SoftbusRegisterService(const char *pkgName, const ServiceInfo *serviceInfo); 42 43 /** 44 * @brief UnRegister the service to the softbus. 45 * 46 * Service information is removed from the cache. 47 * 48 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 49 * @param serviceId Indicates the service ID. 50 * 51 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>serviceId</b> is invalid. 52 * @return Returns <b>SOFTBUS_OK</b> if the service unregister is successful. If the service is not registered, 53 * <b>SOFTBUS_OK</b> is also returned. 54 * 55 * @since 6.0 56 * @version 1.0 57 */ 58 int32_t SoftbusUnregisterService(const char *pkgName, int64_t serviceId); 59 60 /** 61 * @brief Publish a service. 62 * 63 * Services can only be published after they have been registered. 64 * 65 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 66 * @param serviceId Indicates the service ID. 67 * @param publishParam Indicates the pointer to the publishing parameters. 68 * 69 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>serviceId</b> is invalid. 70 * @return Returns <b>SOFTBUS_SERVICE_NOT_FOUND</b> if the service is not registered. 71 * @return Returns <b>SOFTBUS_OK</b> if the service publishing is successful. 72 * 73 * @since 6.0 74 * @version 1.0 75 */ 76 int32_t SoftbusPublishService(const char *pkgName, int64_t serviceId, const ServiceDiscoveryParam *publishParam); 77 78 /** 79 * @brief UnPublish a service. 80 * 81 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 82 * @param serviceId Indicates the service ID. 83 * 84 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>serviceId</b> is invalid. 85 * @return Returns <b>SOFTBUS_OK</b> if the service publishing is successful. If the service is not published, 86 * <b>SOFTBUS_OK</b> is also returned. 87 * 88 * @since 6.0 89 * @version 1.0 90 */ 91 int32_t SoftbusUnpublishService(const char *pkgName, int64_t serviceId); 92 93 /** 94 * @brief Update the service information and publish it. 95 * 96 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 97 * @param serviceInfo Indicates the pointer to the service info. 98 * 99 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>serviceInfo</b> is invalid. 100 * @return Returns <b>SOFTBUS_OK</b> if the service is successfully updated. 101 * 102 * @since 6.0 103 * @version 1.0 104 */ 105 int32_t SoftbusUpdateService(const char *pkgName, const ServiceInfo *serviceInfo); 106 107 /** 108 * @brief Defines the callbacks for service discovery. 109 * 110 * @since 6.0 111 * @version 1.0 112 */ 113 typedef struct { 114 /** 115 * @brief Callback that is invoked when a service is found. 116 * 117 * @param service Indicates the pointer to the service info. For details, see {@link ServiceInfo}. 118 * 119 * @since 6.0 120 * @version 1.0 121 */ 122 void (*OnServiceFound)(const ServiceInfo *service); 123 124 /** 125 * @brief Callback that is invoked when a service is lost. 126 * 127 * @param serviceId Indicates the service ID. 128 * 129 * @since 6.0 130 * @version 1.0 131 */ 132 void (*OnServiceLost)(int64_t serviceId); 133 } IServiceDiscoveryCb; 134 135 /** 136 * @brief Search for services by service type. 137 * 138 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 139 * @param info Indicates the pointer to the service discovery information. For details, see {@link DiscoveryInfo}. 140 * @param discoveryParam Indicates the pointer to the discovery parameters. 141 * @param cb Indicates the service discovery callback {@link IServiceDiscoveryCb}. 142 * 143 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null or invalid. 144 * @return Returns <b>SOFTBUS_OK</b> if the service discovery is successful. 145 * 146 * @since 6.0 147 * @version 1.0 148 */ 149 int32_t SoftbusStartServiceDiscovery(const char *pkgName, const DiscoveryInfo *info, 150 const ServiceDiscoveryParam *discoveryParam, const IServiceDiscoveryCb *cb); 151 152 /** 153 * @brief Stop searching for services. 154 * 155 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 156 * @param localServiceId Indicates the local service ID. 157 * @param serviceType Indicates the pointer to the service type. 158 * 159 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null or invalid. 160 * @return Returns <b>SOFTBUS_OK</b> if the service discovery is stopped successfully. 161 * 162 * @since 6.0 163 * @version 1.0 164 */ 165 int32_t SoftbusStopServiceDiscovery(const char *pkgName, int64_t localServiceId, const char *serviceType); 166 167 #ifdef __cplusplus 168 } 169 #endif 170 171 #endif 172