1 /* 2 * Copyright (c) 2021, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for Anycast Locator functionality. 32 */ 33 34 #ifndef ANYCAST_LOCATOR_HPP_ 35 #define ANYCAST_LOCATOR_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE 40 41 #include "common/callback.hpp" 42 #include "common/locator.hpp" 43 #include "common/non_copyable.hpp" 44 #include "net/ip6_address.hpp" 45 #include "thread/tmf.hpp" 46 47 namespace ot { 48 49 /** 50 * Implements Anycast Locator functionality which allows caller to determine the mesh local EID and RLOC16 51 * of the closest destination of an anycast address (if any). 52 * 53 * The closest destination is determined based on the current routing table and path costs within the Thread mesh. 54 * 55 */ 56 class AnycastLocator : public InstanceLocator, private NonCopyable 57 { 58 friend class Tmf::Agent; 59 60 public: 61 /** 62 * Pointer type defines the callback to notify the outcome of a request. 63 * 64 */ 65 typedef otThreadAnycastLocatorCallback LocatorCallback; 66 67 /** 68 * Initializes the `AnycastLocator` object. 69 * 70 * @param[in] aInstance A reference to the OpenThread instance. 71 * 72 */ 73 explicit AnycastLocator(Instance &aInstance); 74 75 /** 76 * Requests the closest destination of a given anycast address to be located. 77 * 78 * If a previous `Locate()` request is ongoing, a subsequent call to this method will cancel and replace the 79 * earlier request. 80 * 81 * @param[in] aAnycastAddress The anycast address to locate the closest destination of. 82 * @param[in] aCallback The callback handler to report the result. 83 * @param[in] aContext An arbitrary context used with @p aCallback. 84 * 85 * @retval kErrorNone The request started successfully. @p aCallback will be invoked to report the result. 86 * @retval kErrorNoBufs Out of buffers to prepare and send the request message. 87 * @retval kErrorInvalidArgs The @p aAnycastAddress is not a valid anycast address or @p aCallback is `nullptr`. 88 * 89 */ 90 Error Locate(const Ip6::Address &aAnycastAddress, LocatorCallback aCallback, void *aContext); 91 92 /** 93 * Indicates whether an earlier request is in progress. 94 * 95 * @returns TRUE if an earlier request is in progress, FALSE otherwise. 96 * 97 */ IsInProgress(void) const98 bool IsInProgress(void) const { return mCallback.IsSet(); } 99 100 private: 101 static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, Error aError); 102 103 void HandleResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aError); 104 105 template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 106 107 Callback<LocatorCallback> mCallback; 108 }; 109 110 #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_SEND_RESPONSE 111 DeclareTmfHandler(AnycastLocator, kUriAnycastLocate); 112 #endif 113 114 } // namespace ot 115 116 #endif // OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE 117 118 #endif // ANYCAST_LOCATOR_HPP_ 119