1 /*
2  *  Copyright (c) 2016, 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 handle network diagnostic.
32  */
33 
34 #ifndef NETWORK_DIAGNOSTIC_HPP_
35 #define NETWORK_DIAGNOSTIC_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE
40 
41 #include <openthread/netdiag.h>
42 
43 #include "coap/coap.hpp"
44 #include "common/locator.hpp"
45 #include "common/non_copyable.hpp"
46 #include "net/udp6.hpp"
47 #include "thread/network_diagnostic_tlvs.hpp"
48 
49 namespace ot {
50 
51 namespace NetworkDiagnostic {
52 
53 /**
54  * @addtogroup core-netdiag
55  *
56  * @brief
57  *   This module includes definitions for sending and handling Network Diagnostic Commands.
58  *
59  * @{
60  */
61 
62 /**
63  * This class implements the Network Diagnostic processing.
64  *
65  */
66 class NetworkDiagnostic : public InstanceLocator, private NonCopyable
67 {
68 public:
69     /**
70      * This type represents an iterator used to iterate through Network Diagnostic TLVs from `GetNextDiagTlv()`.
71      *
72      */
73     typedef otNetworkDiagIterator Iterator;
74 
75     static constexpr Iterator kIteratorInit = OT_NETWORK_DIAGNOSTIC_ITERATOR_INIT; ///< Initializer for Iterator.
76 
77     /**
78      * This constructor initializes the object.
79      *
80      */
81     explicit NetworkDiagnostic(Instance &aInstance);
82 
83     /**
84      * This method sends Diagnostic Get request. If the @p aDestination is of multicast type, the DIAG_GET.qry
85      * message is sent or the DIAG_GET.req otherwise.
86      *
87      * @param[in]  aDestination      A reference to the destination address.
88      * @param[in]  aTlvTypes         An array of Network Diagnostic TLV types.
89      * @param[in]  aCount            Number of types in aTlvTypes.
90      * @param[in]  aCallback         A pointer to a function that is called when Network Diagnostic Get response
91      *                               is received or NULL to disable the callback.
92      * @param[in]  aCallbackContext  A pointer to application-specific context.
93      *
94      */
95     Error SendDiagnosticGet(const Ip6::Address &           aDestination,
96                             const uint8_t                  aTlvTypes[],
97                             uint8_t                        aCount,
98                             otReceiveDiagnosticGetCallback aCallback,
99                             void *                         aCallbackContext);
100 
101     /**
102      * This method sends Diagnostic Reset request.
103      *
104      * @param[in] aDestination  A reference to the destination address.
105      * @param[in] aTlvTypes     An array of Network Diagnostic TLV types.
106      * @param[in] aCount        Number of types in aTlvTypes
107      *
108      */
109     Error SendDiagnosticReset(const Ip6::Address &aDestination, const uint8_t aTlvTypes[], uint8_t aCount);
110 
111     /**
112      * This static method gets the next Network Diagnostic TLV in a given message.
113      *
114      * @param[in]     aMessage         A message.
115      * @param[inout]  aIterator        The Network Diagnostic iterator. To get the first TLV set it to `kIteratorInit`.
116      * @param[out]    aNetworkDiagTlv  A reference to a Network Diagnostic TLV to output the next TLV.
117      *
118      * @retval kErrorNone       Successfully found the next Network Diagnostic TLV.
119      * @retval kErrorNotFound   No subsequent Network Diagnostic TLV exists in the message.
120      * @retval kErrorParse      Parsing the next Network Diagnostic failed.
121      *
122      */
123     static Error GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, otNetworkDiagTlv &aNetworkDiagTlv);
124 
125 private:
126     Error AppendIp6AddressList(Message &aMessage);
127     Error AppendChildTable(Message &aMessage);
128     void  FillMacCountersTlv(MacCountersTlv &aMacCountersTlv);
129     Error FillRequestedTlvs(const Message &aRequest, Message &aResponse, NetworkDiagnosticTlv &aNetworkDiagnosticTlv);
130 
131     static void HandleDiagnosticGetRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
132     void        HandleDiagnosticGetRequest(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
133 
134     static void HandleDiagnosticGetQuery(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
135     void        HandleDiagnosticGetQuery(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
136 
137     static void HandleDiagnosticGetResponse(void *               aContext,
138                                             otMessage *          aMessage,
139                                             const otMessageInfo *aMessageInfo,
140                                             Error                aResult);
141     void HandleDiagnosticGetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);
142 
143     static void HandleDiagnosticGetAnswer(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
144     void        HandleDiagnosticGetAnswer(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
145 
146     static void HandleDiagnosticReset(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
147     void        HandleDiagnosticReset(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
148 
149     Coap::Resource mDiagnosticGetRequest;
150     Coap::Resource mDiagnosticGetQuery;
151     Coap::Resource mDiagnosticGetAnswer;
152     Coap::Resource mDiagnosticReset;
153 
154     otReceiveDiagnosticGetCallback mReceiveDiagnosticGetCallback;
155     void *                         mReceiveDiagnosticGetCallbackContext;
156 };
157 
158 /**
159  * @}
160  */
161 } // namespace NetworkDiagnostic
162 
163 } // namespace ot
164 
165 #endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE
166 
167 #endif // NETWORK_DIAGNOSTIC_HPP_
168