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 kMdnsProtocolTextMaxSize = 8;
40 static constexpr uint8_t kMdnsTypeMaxSize = 32;
41 static constexpr uint16_t kMdnsTextMaxSize = 64;
43 enum class MdnsServiceProtocol : uint8_t
47 kMdnsProtocolUnknown = 255,
53 const uint8_t * mData;
59 char mName[kMdnsNameMaxSize + 1];
60 char mType[kMdnsTypeMaxSize + 1];
61 MdnsServiceProtocol mProtocol;
62 Inet::IPAddressType mAddressType;
64 chip::Inet::InterfaceId mInterface;
65 TextEntry * mTextEntries;
66 size_t mTextEntrySize;
67 const char ** mSubTypes;
69 Optional<chip::Inet::IPAddress> mAddress;
73 * The callback function for mDNS resolve.
75 * The callback function SHALL NOT take the ownership of the service pointer or
76 * any pointer inside this structure.
78 * @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
79 * @param[in] result The mdns resolve result, can be nullptr if error happens.
80 * @param[in] error The error code.
83 using MdnsResolveCallback = void (*)(void * context, MdnsService * result, CHIP_ERROR error);
86 * The callback function for mDNS browse.
88 * The callback function SHALL NOT take the ownership of the service pointer or
89 * any pointer inside this structure.
91 * @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
92 * @param[in] services The service list, can be nullptr.
93 * @param[in] servicesSize The size of the service list.
94 * @param[in] error The error code.
97 using MdnsBrowseCallback = void (*)(void * context, MdnsService * services, size_t servicesSize, CHIP_ERROR error);
99 using MdnsAsyncReturnCallback = void (*)(void * context, CHIP_ERROR error);
102 * This function intializes the mdns module
104 * @param[in] initCallback The callback for notifying the initialization result.
105 * @param[in] errorCallback The callback for notifying internal errors.
106 * @param[in] context The context passed to the callbacks.
108 * @retval CHIP_NO_ERROR The initialization succeeds.
109 * @retval Error code The initialization fails
112 CHIP_ERROR ChipMdnsInit(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCallback errorCallback, void * context);
115 * This function sets the host name for services.
117 * @param[in] hostname The hostname.
120 CHIP_ERROR ChipMdnsSetHostname(const char * hostname);
123 * This function publishes an service via mDNS.
125 * Calling the function again with the same service name, type, protocol,
126 * interface and port but different text will update the text published.
127 * This function will NOT take the ownership of service->mTextEntries memory.
129 * @param[in] service The service entry.
131 * @retval CHIP_NO_ERROR The publish succeeds.
132 * @retval CHIP_ERROR_INVALID_ARGUMENT The service is nullptr.
133 * @retval Error code The publish fails.
136 CHIP_ERROR ChipMdnsPublishService(const MdnsService * service);
139 * This function stops publishing service via mDNS.
141 * @retval CHIP_NO_ERROR The publish stops successfully.
142 * @retval Error code Stopping the publish fails.
145 CHIP_ERROR ChipMdnsStopPublish();
148 * This function browses the services published by mdns
150 * @param[in] type The service type.
151 * @param[in] protocol The service protocol.
152 * @param[in] addressType The protocol version of the IP address.
153 * @param[in] interface The interface to send queries.
154 * @param[in] callback The callback for found services.
155 * @param[in] context The user context.
157 * @retval CHIP_NO_ERROR The browse succeeds.
158 * @retval CHIP_ERROR_INVALID_ARGUMENT The type or callback is nullptr.
159 * @retval Error code The browse fails.
162 CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
163 chip::Inet::InterfaceId interface, MdnsBrowseCallback callback, void * context);
166 * This function resolves the services published by mdns
168 * @param[in] browseResult The service entry returned by @ref ChipMdnsBrowse
169 * @param[in] interface The interface to send queries.
170 * @param[in] callback The callback for found services.
171 * @param[in] context The user context.
173 * @retval CHIP_NO_ERROR The resolve succeeds.
174 * @retval CHIP_ERROR_INVALID_ARGUMENT The name, type or callback is nullptr.
175 * @retval Error code The resolve fails.
178 CHIP_ERROR ChipMdnsResolve(MdnsService * browseResult, chip::Inet::InterfaceId interface, MdnsResolveCallback callback,