1 /* 2 * Copyright (c) 2020, 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 transmitting SVR_DATA.ntf messages. 32 */ 33 34 #ifndef NETWORK_DATA_NOTIFIER_HPP_ 35 #define NETWORK_DATA_NOTIFIER_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE 40 41 #include <openthread/border_router.h> 42 43 #include "coap/coap.hpp" 44 #include "common/message.hpp" 45 #include "common/non_copyable.hpp" 46 #include "common/notifier.hpp" 47 #include "common/tasklet.hpp" 48 #include "common/time_ticker.hpp" 49 50 namespace ot { 51 namespace NetworkData { 52 53 class NetworkData; 54 55 /** 56 * Implements the SVR_DATA.ntf transmission logic. 57 */ 58 class Notifier : public InstanceLocator, private NonCopyable 59 { 60 friend class ot::Notifier; 61 friend class ot::TimeTicker; 62 63 public: 64 /** 65 * Constructor. 66 * 67 * @param[in] aInstance The OpenThread instance. 68 */ 69 explicit Notifier(Instance &aInstance); 70 71 /** 72 * Call this method to inform the notifier that new server data is available. 73 * 74 * Posts a tasklet to sync new server data with leader so if there are multiple changes within the same 75 * flow of execution (multiple calls to this method) they are all synchronized together and included in the same 76 * message to the leader. 77 */ 78 void HandleServerDataUpdated(void); 79 80 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL 81 typedef otBorderRouterNetDataFullCallback NetDataCallback; ///< Network Data full callback. 82 83 /** 84 * Sets the callback to indicate when Network Data gets full. 85 * 86 * @param[in] aCallback The callback. 87 * @param[in] aContext The context to use with @p aCallback. 88 */ 89 void SetNetDataFullCallback(NetDataCallback aCallback, void *aContext); 90 91 /** 92 * Signals that network data (local or leader) is getting full. 93 */ SignalNetworkDataFull(void)94 void SignalNetworkDataFull(void) { mNetDataFullTask.Post(); } 95 #endif 96 97 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE && OPENTHREAD_CONFIG_BORDER_ROUTER_REQUEST_ROUTER_ROLE 98 /** 99 * Indicates whether the device as border router is eligible for router role upgrade request using the 100 * status reason `kBorderRouterRequest`. 101 * 102 * Checks whether device is providing IP connectivity and that there are fewer than two border routers 103 * in network data acting as router. Device is considered to provide external IP connectivity if at least one of 104 * the below conditions hold: 105 * 106 * - It has added at least one external route entry. 107 * - It has added at least one prefix entry with default-route and on-mesh flags set. 108 * - It has added at least one domain prefix (domain and on-mesh flags set). 109 * 110 * Does not check the current role of device. 111 * 112 * @retval TRUE Device is eligible to request router role upgrade as a border router. 113 * @retval FALSE Device is not eligible to request router role upgrade as a border router. 114 */ 115 bool IsEligibleForRouterRoleUpgradeAsBorderRouter(void) const; 116 #endif 117 118 private: 119 static constexpr uint32_t kDelayNoBufs = 1000; // in msec 120 static constexpr uint32_t kDelayRemoveStaleChildren = 5000; // in msec 121 static constexpr uint32_t kDelaySynchronizeServerData = 300000; // in msec 122 static constexpr uint8_t kRouterRoleUpgradeMaxTimeout = 10; // in sec 123 124 void SynchronizeServerData(void); 125 Error SendServerDataNotification(uint16_t aOldRloc16, const NetworkData *aNetworkData = nullptr); 126 #if OPENTHREAD_FTD 127 Error RemoveStaleChildEntries(void); 128 #endif 129 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE 130 Error UpdateInconsistentData(void); 131 #endif 132 133 void HandleNotifierEvents(Events aEvents); 134 void HandleTimer(void); 135 static void HandleCoapResponse(void *aContext, 136 otMessage *aMessage, 137 const otMessageInfo *aMessageInfo, 138 otError aResult); 139 void HandleCoapResponse(Error aResult); 140 141 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL 142 void HandleNetDataFull(void); 143 #endif 144 145 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE && OPENTHREAD_CONFIG_BORDER_ROUTER_REQUEST_ROUTER_ROLE 146 void ScheduleRouterRoleUpgradeIfEligible(void); 147 void HandleTimeTick(void); 148 #endif 149 150 using SynchronizeDataTask = TaskletIn<Notifier, &Notifier::SynchronizeServerData>; 151 using DelayTimer = TimerMilliIn<Notifier, &Notifier::HandleTimer>; 152 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL 153 using NetDataFullTask = TaskletIn<Notifier, &Notifier::HandleNetDataFull>; 154 #endif 155 156 DelayTimer mTimer; 157 SynchronizeDataTask mSynchronizeDataTask; 158 #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL 159 NetDataFullTask mNetDataFullTask; 160 Callback<NetDataCallback> mNetDataFullCallback; 161 #endif 162 uint32_t mNextDelay; 163 uint16_t mOldRloc; 164 bool mWaitingForResponse : 1; 165 166 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE && OPENTHREAD_CONFIG_BORDER_ROUTER_REQUEST_ROUTER_ROLE 167 bool mDidRequestRouterRoleUpgrade : 1; 168 uint8_t mRouterRoleUpgradeTimeout; 169 #endif 170 }; 171 172 } // namespace NetworkData 173 } // namespace ot 174 175 #endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE 176 177 #endif // NETWORK_DATA_NOTIFIER_HPP_ 178