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 wraps the calls to platform OTNS abstractions. 32 */ 33 34 #ifndef UTILS_OTNS_HPP_ 35 #define UTILS_OTNS_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if (OPENTHREAD_MTD || OPENTHREAD_FTD) && OPENTHREAD_CONFIG_OTNS_ENABLE 40 41 #include <openthread/thread.h> 42 #include <openthread/thread_ftd.h> 43 #include <openthread/platform/otns.h> 44 45 #include "coap/coap_message.hpp" 46 #include "common/locator.hpp" 47 #include "common/non_copyable.hpp" 48 #include "common/notifier.hpp" 49 #include "mac/mac_frame.hpp" 50 #include "mac/mac_types.hpp" 51 #include "net/ip6_address.hpp" 52 #include "thread/neighbor.hpp" 53 #include "thread/neighbor_table.hpp" 54 55 namespace ot { 56 namespace Utils { 57 58 /** 59 * Implements the OTNS Stub that interacts with OTNS. 60 */ 61 class Otns : public InstanceLocator, private NonCopyable 62 { 63 friend class ot::Notifier; 64 65 public: 66 /** 67 * Initializes the object. 68 * 69 * @param[in] aInstance A reference to the OpenThread instance. 70 */ Otns(Instance & aInstance)71 explicit Otns(Instance &aInstance) 72 : InstanceLocator(aInstance) 73 { 74 } 75 76 /** 77 * Emits radio short address to OTNS when changed. 78 * 79 * @param[in] aShortAddress The new short address. 80 */ 81 static void EmitShortAddress(uint16_t aShortAddress); 82 83 /** 84 * Emits radio extended address to OTNS when changed. 85 * 86 * @param[in] aExtAddress The new extended address. 87 */ 88 static void EmitExtendedAddress(const Mac::ExtAddress &aExtAddress); 89 90 /** 91 * Emits ping request information to OTNS when sending. 92 * 93 * @param[in] aPeerAddress The peer address of the ping request. 94 * @param[in] aPingLength The data length of the ping request. 95 * @param[in] aTimestamp The timestamp of the ping request. 96 * @param[in] aHopLimit The hop limit of the ping request. 97 */ 98 static void EmitPingRequest(const Ip6::Address &aPeerAddress, 99 uint16_t aPingLength, 100 uint32_t aTimestamp, 101 uint8_t aHopLimit); 102 103 /** 104 * Emits ping reply information to OTNS when received. 105 * 106 * @param[in] aPeerAddress The peer address of the ping request. 107 * @param[in] aPingLength The data length of the ping reply. 108 * @param[in] aTimestamp The timestamp of the ping reply. 109 * @param[in] aHopLimit The hop limit of the ping reply. 110 */ 111 static void EmitPingReply(const Ip6::Address &aPeerAddress, 112 uint16_t aPingLength, 113 uint32_t aTimestamp, 114 uint8_t aHopLimit); 115 116 /** 117 * Emits a neighbor table event to OTNS when a neighbor is added or removed. 118 * 119 * @param[in] aEvent The event type. 120 * @param[in] aNeighbor The neighbor that is added or removed. 121 */ 122 static void EmitNeighborChange(NeighborTable::Event aEvent, const Neighbor &aNeighbor); 123 124 /** 125 * Emits a transmit event to OTNS. 126 * 127 * @param[in] aFrame The frame of the transmission. 128 */ 129 static void EmitTransmit(const Mac::TxFrame &aFrame); 130 131 /** 132 * Emits the device mode to OTNS. 133 * 134 * @param[in] aMode The device mode. 135 */ 136 static void EmitDeviceMode(Mle::DeviceMode aMode); 137 138 /** 139 * Emits the sending COAP message info to OTNS. 140 * 141 * @param[in] aMessage The sending COAP message. 142 * @param[in] aMessageInfo The message info. 143 */ 144 static void EmitCoapSend(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 145 146 /** 147 * Emits the COAP message sending failure to OTNS. 148 * 149 * @param[in] aError The error in sending the COAP message. 150 * @param[in] aMessage The COAP message failed to send. 151 * @param[in] aMessageInfo The message info. 152 */ 153 static void EmitCoapSendFailure(Error aError, Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 154 155 /** 156 * Emits the received COAP message info to OTNS. 157 * 158 * @param[in] aMessage The received COAP message. 159 * @param[in] aMessageInfo The message info. 160 */ 161 static void EmitCoapReceive(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 162 163 private: 164 static void EmitStatus(const char *aFmt, ...); 165 void HandleNotifierEvents(Events aEvents); 166 }; 167 168 } // namespace Utils 169 } // namespace ot 170 171 #endif //(OPENTHREAD_MTD || OPENTHREAD_FTD) && OPENTHREAD_CONFIG_OTNS_ENABLE 172 173 #endif // UTILS_OTNS_HPP_ 174