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 #ifndef DISCOVERY_SERVICE_H 17 #define DISCOVERY_SERVICE_H 18 19 #include "softbus_common.h" 20 21 #ifdef __cplusplus 22 #if __cplusplus 23 extern "C" { 24 #endif 25 #endif 26 27 /** 28 * @brief Indicates the maximum length of the device address in <b>IDiscoveryCallback</b>. 29 * 30 */ 31 #define CONNECT_ADDR_LEN 46 32 33 /** 34 * @brief Enumerates error codes for service publishing failures. 35 * 36 * The error codes are returned to the caller through <b>IPublishCallback</b>. 37 * 38 */ 39 typedef enum { 40 /* Unsupported medium */ 41 PUBLISH_FAIL_REASON_NOT_SUPPORT_MEDIUM = 1, 42 /* internal error */ 43 PUBLISH_FAIL_REASON_INTERNAL = 2, 44 /* Unknown reason */ 45 PUBLISH_FAIL_REASON_UNKNOWN = 0xFF 46 } PublishFailReason; 47 48 /** 49 * @brief Defines the callbacks for successful and failed service publishing. 50 * 51 */ 52 typedef struct { 53 /** Callback for successful publishing */ 54 void (*OnPublishSuccess)(int publishId); 55 /** Callback for failed publishing */ 56 void (*OnPublishFail)(int publishId, PublishFailReason reason); 57 } IPublishCallback; 58 59 /** 60 * @brief Enumerates error codes for service subscription failures. 61 * 62 * The error codes are returned to the caller through <b>IDiscoveryCallback</b>. 63 * 64 */ 65 typedef enum { 66 /* Unsupported medium */ 67 DISCOVERY_FAIL_REASON_NOT_SUPPORT_MEDIUM = 1, 68 /* internal error */ 69 DISCOVERY_FAIL_REASON_INTERNAL = 2, 70 /* Unknown error */ 71 DISCOVERY_FAIL_REASON_UNKNOWN = 0xFF 72 } DiscoveryFailReason; 73 74 /** 75 * @brief Defines a callback for service subscription. 76 * 77 * Three types of callbacks are available. 78 * 79 */ 80 typedef struct { 81 /** Callback that is invoked when a device is found */ 82 void (*OnDeviceFound)(const DeviceInfo *device); 83 /** Callback for a subscription failure */ 84 void (*OnDiscoverFailed)(int subscribeId, DiscoveryFailReason failReason); 85 /** Callback for a subscription success */ 86 void (*OnDiscoverySuccess)(int subscribeId); 87 } IDiscoveryCallback; 88 89 /** 90 * @brief Publishes a specified service. 91 * 92 * Peer devices in the same LAN as the device that publishes this service can discover this service as needed. 93 * The service is identified by <b>publicId</b> and <b>pkgName</b>. 94 * 95 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 96 * @param info Indicates the pointer to the service publishing information. For details, see {@link PublishInfo}. 97 * @param cb Indicates the pointer to the service publishing callback {@link IPublishCallback}. 98 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null or invalid. 99 * @return Returns <b>SOFTBUS_DISCOVER_NOT_INIT</b> if the Intelligent Soft Bus client fails to be initialized. 100 * @return Returns <b>SOFTBUS_LOCK_ERR</b> if the mutex fails to be locked. 101 * @return Returns <b>SOFTBUS_OK</b> if the service is successfully published. 102 */ 103 int PublishService(const char *pkgName, const PublishInfo *info, const IPublishCallback *cb); 104 105 /** 106 * @brief Unpublishes a specified service. 107 * 108 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 109 * @param publishId Indicates the service ID. 110 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>pkgName</b> is invalid. 111 * @return Returns <b>SOFTBUS_DISCOVER_NOT_INIT</b> if the Intelligent Soft Bus client fails to be initialized. 112 * @return Returns <b>SOFTBUS_OK</b> if the service is successfully unpublished. 113 */ 114 int UnPublishService(const char *pkgName, int publishId); 115 116 /** 117 * @brief Subscribes to a specified service. 118 * 119 * Information about the device that publishes the service will be reported to the device that subscribes to 120 * the service. 121 * The service is identified by <b>subscribeId</b> and <b>pkgName</b>. 122 * 123 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 124 * @param info Indicates the pointer to the service subscription information. For details, see {@link SubscribeInfo}. 125 * @param cb Indicates the service subscription callback {@link IDiscoveryCallback}. 126 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null or invalid. 127 * @return Returns <b>SOFTBUS_DISCOVER_NOT_INIT</b> if the Intelligent Soft Bus client fails to be initialized. 128 * @return Returns <b>SOFTBUS_LOCK_ERR</b> if the mutex fails to be locked. 129 * @return Returns <b>SOFTBUS_OK</b> if the service subscription is successful. 130 */ 131 int StartDiscovery(const char *pkgName, const SubscribeInfo *info, const IDiscoveryCallback *cb); 132 133 /** 134 * @brief Unsubscribes from a specified service. 135 * 136 * @param pkgName Indicates the pointer to the service package name, which can contain a maximum of 64 bytes. 137 * @param subscribeId Indicates the service ID. 138 * @return Returns <b>SOFTBUS_INVALID_PARAM</b> if <b>pkgName</b> is invalid. 139 * @return Returns <b>SOFTBUS_DISCOVER_NOT_INIT</b> if the Intelligent Soft Bus client fails to be initialized. 140 * @return Returns <b>SOFTBUS_OK</b> if the service unsubscription is successful. 141 */ 142 int StopDiscovery(const char *pkgName, int subscribeId); 143 144 #ifdef __cplusplus 145 #if __cplusplus 146 } 147 #endif /* __cplusplus */ 148 #endif /* __cplusplus */ 149 150 #endif /* DISCOVERY_SERVICE_H */ 151