3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * This file defines the platform API to publish and subscribe mDNS
23 * You can find the implementation in src/platform/\<PLATFORM\>/MdnsImpl.cpp.
30 #include "core/CHIPError.h"
31 #include "inet/IPAddress.h"
32 #include "inet/InetInterface.h"
33 #include "lib/core/Optional.h"
38 static constexpr uint8_t kMdnsNameMaxSize = 33;
39 static constexpr uint8_t kMdnsTypeMaxSize = 32;
40 static constexpr uint16_t kMdnsTextMaxSize = 64;
42 enum class MdnsServiceProtocol : uint8_t
46 kMdnsProtocolUnknown = 255,
52 const uint8_t * mData;
58 char mName[kMdnsNameMaxSize + 1];
59 char mType[kMdnsTypeMaxSize + 1];
60 MdnsServiceProtocol mProtocol;
61 Inet::IPAddressType mAddressType;
63 chip::Inet::InterfaceId mInterface;
64 TextEntry * mTextEntries;
65 size_t mTextEntrySize;
66 const char ** mSubTypes;
68 Optional<chip::Inet::IPAddress> mAddress;
72 * The callback function for mDNS resolve.
74 * The callback function SHALL NOT take the ownership of the service pointer or
75 * any pointer inside this structure.
77 * @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
78 * @param[in] result The mdns resolve result, can be nullptr if error happens.
79 * @param[in] error The error code.
82 using MdnsResolveCallback = void (*)(void * context, MdnsService * result, CHIP_ERROR error);
85 * The callback function for mDNS browse.
87 * The callback function SHALL NOT take the ownership of the service pointer or
88 * any pointer inside this structure.
90 * @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
91 * @param[in] services The service list, can be nullptr.
92 * @param[in] servicesSize The size of the service list.
93 * @param[in] error The error code.
96 using MdnsBrowseCallback = void (*)(void * context, MdnsService * services, size_t servicesSize, CHIP_ERROR error);
98 using MdnsAsyncReturnCallback = void (*)(void * context, CHIP_ERROR error);
101 * This function intializes the mdns module
103 * @param[in] initCallback The callback for notifying the initialization result.
104 * @param[in] errorCallback The callback for notifying internal errors.
105 * @param[in] context The context passed to the callbacks.
107 * @retval CHIP_NO_ERROR The initialization succeeds.
108 * @retval Error code The initialization fails
111 CHIP_ERROR ChipMdnsInit(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCallback errorCallback, void * context);
114 * This function sets the host name for services.
116 * @param[in] hostname The hostname.
119 CHIP_ERROR ChipMdnsSetHostname(const char * hostname);
122 * This function publishes an service via mDNS.
124 * Calling the function again with the same service name, type, protocol,
125 * interface and port but different text will update the text published.
126 * This function will NOT take the ownership of service->mTextEntries memory.
128 * @param[in] service The service entry.
130 * @retval CHIP_NO_ERROR The publish succeeds.
131 * @retval CHIP_ERROR_INVALID_ARGUMENT The service is nullptr.
132 * @retval Error code The publish fails.
135 CHIP_ERROR ChipMdnsPublishService(const MdnsService * service);
138 * This function stops publishing service via mDNS.
140 * @retval CHIP_NO_ERROR The publish stops successfully.
141 * @retval Error code Stopping the publish fails.
144 CHIP_ERROR ChipMdnsStopPublish();
147 * This function browses the services published by mdns
149 * @param[in] type The service type.
150 * @param[in] protocol The service protocol.
151 * @param[in] addressType The protocol version of the IP address.
152 * @param[in] interface The interface to send queries.
153 * @param[in] callback The callback for found services.
154 * @param[in] context The user context.
156 * @retval CHIP_NO_ERROR The browse succeeds.
157 * @retval CHIP_ERROR_INVALID_ARGUMENT The type or callback is nullptr.
158 * @retval Error code The browse fails.
161 CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
162 chip::Inet::InterfaceId interface, MdnsBrowseCallback callback, void * context);
165 * This function resolves the services published by mdns
167 * @param[in] browseResult The service entry returned by @ref ChipMdnsBrowse
168 * @param[in] interface The interface to send queries.
169 * @param[in] callback The callback for found services.
170 * @param[in] context The user context.
172 * @retval CHIP_NO_ERROR The resolve succeeds.
173 * @retval CHIP_ERROR_INVALID_ARGUMENT The name, type or callback is nullptr.
174 * @retval Error code The resolve fails.
177 CHIP_ERROR ChipMdnsResolve(MdnsService * browseResult, chip::Inet::InterfaceId interface, MdnsResolveCallback callback,