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 #include <sys/select.h>
29 #include <avahi-client/client.h>
30 #include <avahi-client/lookup.h>
31 #include <avahi-client/publish.h>
32 #include <avahi-common/domain.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/watch.h>
36 #include "lib/mdns/platform/Mdns.h"
40 int mFd; ///< The file descriptor to watch.
41 AvahiWatchEvent mWatchEvents; ///< The interested events.
42 int mHappenedEvents; ///< The events happened.
43 AvahiWatchCallback mCallback; ///< The function to be called when interested events happened on mFd.
44 void * mContext; ///< A pointer to application-specific context.
45 void * mPoller; ///< The poller created this watch.
50 std::chrono::steady_clock::time_point mAbsTimeout; ///< Absolute time when this timer timeout.
51 AvahiTimeoutCallback mCallback; ///< The function to be called when timeout.
52 bool mEnabled; ///< Whether the timeout is enabled.
53 void * mContext; ///< The pointer to application-specific context.
54 void * mPoller; ///< The poller created this timer.
65 void UpdateFdSet(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet, int & maxFd, timeval & timeout);
67 void Process(const fd_set & readFdSet, const fd_set & writeFdSet, const fd_set & errorFdSet);
69 const AvahiPoll * GetAvahiPoll(void) const { return &mAvahiPoller; }
72 static AvahiWatch * WatchNew(const struct AvahiPoll * poller, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
74 AvahiWatch * WatchNew(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void * context);
76 static void WatchUpdate(AvahiWatch * watch, AvahiWatchEvent event);
78 static AvahiWatchEvent WatchGetEvents(AvahiWatch * watch);
80 static void WatchFree(AvahiWatch * watch);
81 void WatchFree(AvahiWatch & watch);
83 static AvahiTimeout * TimeoutNew(const AvahiPoll * poller, const struct timeval * timeout, AvahiTimeoutCallback callback,
85 AvahiTimeout * TimeoutNew(const struct timeval * timeout, AvahiTimeoutCallback callback, void * context);
87 static void TimeoutUpdate(AvahiTimeout * timer, const struct timeval * timeout);
89 static void TimeoutFree(AvahiTimeout * timer);
90 void TimeoutFree(AvahiTimeout & timer);
92 std::vector<std::unique_ptr<AvahiWatch>> mWatches;
93 std::vector<std::unique_ptr<AvahiTimeout>> mTimers;
94 AvahiPoll mAvahiPoller;
100 MdnsAvahi(const MdnsAvahi &) = delete;
101 MdnsAvahi & operator=(const MdnsAvahi &) = delete;
103 CHIP_ERROR Init(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCallback errorCallback, void * context);
104 CHIP_ERROR SetHostname(const char * hostname);
105 CHIP_ERROR PublishService(const MdnsService & service);
106 CHIP_ERROR StopPublish();
107 CHIP_ERROR Browse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
108 chip::Inet::InterfaceId interface, MdnsBrowseCallback callback, void * context);
109 CHIP_ERROR Resolve(const char * name, const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
110 chip::Inet::InterfaceId interface, MdnsResolveCallback callback, void * context);
112 Poller & GetPoller() { return mPoller; }
114 static MdnsAvahi & GetInstance() { return sInstance; }
121 MdnsAvahi * mInstance;
122 MdnsBrowseCallback mCallback;
124 std::vector<MdnsService> mServices;
127 struct ResolveContext
129 MdnsAvahi * mInstance;
130 MdnsResolveCallback mCallback;
134 MdnsAvahi() : mClient(nullptr), mGroup(nullptr) {}
135 static MdnsAvahi sInstance;
137 static void HandleClientState(AvahiClient * client, AvahiClientState state, void * context);
138 void HandleClientState(AvahiClient * client, AvahiClientState state);
140 static void HandleGroupState(AvahiEntryGroup * group, AvahiEntryGroupState state, void * context);
141 void HandleGroupState(AvahiEntryGroup * group, AvahiEntryGroupState state);
143 static void HandleBrowse(AvahiServiceBrowser * broswer, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event,
144 const char * name, const char * type, const char * domain, AvahiLookupResultFlags flags,
146 static void HandleResolve(AvahiServiceResolver * resolver, AvahiIfIndex interface, AvahiProtocol protocol,
147 AvahiResolverEvent event, const char * name, const char * type, const char * domain,
148 const char * host_name, const AvahiAddress * address, uint16_t port, AvahiStringList * txt,
149 AvahiLookupResultFlags flags, void * userdata);
151 MdnsAsyncReturnCallback mInitCallback;
152 MdnsAsyncReturnCallback mErrorCallback;
153 void * mAsyncReturnContext;
155 std::set<std::string> mPublishedServices;
156 AvahiClient * mClient;
157 AvahiEntryGroup * mGroup;