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_table.hpp" 53 #include "thread/topology.hpp" 54 55 namespace ot { 56 namespace Utils { 57 58 /** 59 * This class implements the OTNS Stub that interacts with OTNS. 60 * 61 */ 62 class Otns : public InstanceLocator, private NonCopyable 63 { 64 friend class ot::Notifier; 65 66 public: 67 /** 68 * This constructor initializes the object. 69 * 70 * @param[in] aInstance A reference to the OpenThread instance. 71 * 72 */ Otns(Instance & aInstance)73 explicit Otns(Instance &aInstance) 74 : InstanceLocator(aInstance) 75 { 76 } 77 78 /** 79 * This function emits radio short address to OTNS when changed. 80 * 81 * @param[in] aShortAddress The new short address. 82 * 83 */ 84 static void EmitShortAddress(uint16_t aShortAddress); 85 86 /** 87 * This function emits radio extended address to OTNS when changed. 88 * 89 * @param[in] aExtAddress The new extended address. 90 * 91 */ 92 static void EmitExtendedAddress(const Mac::ExtAddress &aExtAddress); 93 94 /** 95 * This function emits ping request information to OTNS when sending. 96 * 97 * @param[in] aPeerAddress The peer address of the ping request. 98 * @param[in] aPingLength The data length of the ping request. 99 * @param[in] aTimestamp The timestamp of the ping request. 100 * @param[in] aHopLimit The hop limit of the ping request. 101 * 102 */ 103 static void EmitPingRequest(const Ip6::Address &aPeerAddress, 104 uint16_t aPingLength, 105 uint32_t aTimestamp, 106 uint8_t aHopLimit); 107 108 /** 109 * This function emits ping reply information to OTNS when received. 110 * 111 * @param[in] aPeerAddress The peer address of the ping request. 112 * @param[in] aPingLength The data length of the ping reply. 113 * @param[in] aTimestamp The timestamp of the ping reply. 114 * @param[in] aHopLimit The hop limit of the ping reply. 115 * 116 */ 117 static void EmitPingReply(const Ip6::Address &aPeerAddress, 118 uint16_t aPingLength, 119 uint32_t aTimestamp, 120 uint8_t aHopLimit); 121 122 /** 123 * This function emits a neighbor table event to OTNS when a neighbor is added or removed. 124 * 125 * @param[in] aEvent The event type. 126 * @param[in] aNeighbor The neighbor that is added or removed. 127 * 128 */ 129 static void EmitNeighborChange(NeighborTable::Event aEvent, const Neighbor &aNeighbor); 130 131 /** 132 * This function emits a transmit event to OTNS. 133 * 134 * @param[in] aFrame The frame of the transmission. 135 * 136 */ 137 static void EmitTransmit(const Mac::TxFrame &aFrame); 138 139 /** 140 * This function emits the device mode to OTNS. 141 * 142 * @param[in] aMode The device mode. 143 * 144 */ 145 static void EmitDeviceMode(Mle::DeviceMode aMode); 146 147 /** 148 * This function emits the sending COAP message info to OTNS. 149 * 150 * @param[in] aMessage The sending COAP message. 151 * @param[in] aMessageInfo The message info. 152 * 153 */ 154 static void EmitCoapSend(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 155 156 /** 157 * This function emits the COAP message sending failure to OTNS. 158 * 159 * @param[in] aError The error in sending the COAP message. 160 * @param[in] aMessage The COAP message failed to send. 161 * @param[in] aMessageInfo The message info. 162 * 163 */ 164 static void EmitCoapSendFailure(Error aError, Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 165 166 /** 167 * This function emits the received COAP message info to OTNS. 168 * 169 * @param[in] aMessage The received COAP message. 170 * @param[in] aMessageInfo The message info. 171 * 172 */ 173 static void EmitCoapReceive(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 174 175 private: 176 static void EmitStatus(const char *aFmt, ...); 177 void HandleNotifierEvents(Events aEvents); 178 }; 179 180 } // namespace Utils 181 } // namespace ot 182 183 #endif //(OPENTHREAD_MTD || OPENTHREAD_FTD) && OPENTHREAD_CONFIG_OTNS_ENABLE 184 185 #endif // UTILS_OTNS_HPP_ 186