openthread-br  0.3.0-72c0388
resource.hpp
Go to the documentation of this file.
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 
34 #ifndef OTBR_REST_RESOURCE_HPP_
35 #define OTBR_REST_RESOURCE_HPP_
36 
37 #include <unordered_map>
38 
39 #include <openthread/border_router.h>
40 
41 #include "ncp/ncp_openthread.hpp"
42 #include "rest/json.hpp"
43 #include "rest/request.hpp"
44 #include "rest/response.hpp"
45 #include "utils/thread_helper.hpp"
46 
48 using std::chrono::steady_clock;
49 
50 namespace otbr {
51 namespace rest {
52 
57 class Resource
58 {
59 public:
67 
73  void Init(void);
74 
83  void Handle(Request &aRequest, Response &aResponse) const;
84 
92  void HandleCallback(Request &aRequest, Response &aResponse);
93 
102  void ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) const;
103 
104 private:
105  typedef void (Resource::*ResourceHandler)(const Request &aRequest, Response &aResponse) const;
106  typedef void (Resource::*ResourceCallbackHandler)(const Request &aRequest, Response &aResponse);
107  void NodeInfo(const Request &aRequest, Response &aResponse) const;
108  void ExtendedAddr(const Request &aRequest, Response &aResponse) const;
109  void State(const Request &aRequest, Response &aResponse) const;
110  void NetworkName(const Request &aRequest, Response &aResponse) const;
111  void LeaderData(const Request &aRequest, Response &aResponse) const;
112  void NumOfRoute(const Request &aRequest, Response &aResponse) const;
113  void Rloc16(const Request &aRequest, Response &aResponse) const;
114  void ExtendedPanId(const Request &aRequest, Response &aResponse) const;
115  void Rloc(const Request &aRequest, Response &aResponse) const;
116  void Diagnostic(const Request &aRequest, Response &aResponse) const;
117  void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse);
118 
119  void GetNodeInfo(Response &aResponse) const;
120  void GetDataExtendedAddr(Response &aResponse) const;
121  void GetDataState(Response &aResponse) const;
122  void GetDataNetworkName(Response &aResponse) const;
123  void GetDataLeaderData(Response &aResponse) const;
124  void GetDataNumOfRoute(Response &aResponse) const;
125  void GetDataRloc16(Response &aResponse) const;
126  void GetDataExtendedPanId(Response &aResponse) const;
127  void GetDataRloc(Response &aResponse) const;
128 
129  void DeleteOutDatedDiagnostic(void);
130  void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag);
131 
132  static void DiagnosticResponseHandler(otError aError,
133  otMessage * aMessage,
134  const otMessageInfo *aMessageInfo,
135  void * aContext);
136  void DiagnosticResponseHandler(otError aError, const otMessage *aMessage, const otMessageInfo *aMessageInfo);
137 
138  otInstance * mInstance;
139  ControllerOpenThread *mNcp;
140 
141  std::unordered_map<std::string, ResourceHandler> mResourceMap;
142  std::unordered_map<std::string, ResourceCallbackHandler> mResourceCallbackMap;
143 
144  std::unordered_map<std::string, DiagInfo> mDiagSet;
145 };
146 
147 } // namespace rest
148 } // namespace otbr
149 
150 #endif // OTBR_REST_RESOURCE_HPP_
otbr::rest::Resource
Definition: resource.hpp:57
otbr::rest::Response
Definition: response.hpp:56
otbr::rest::Resource::HandleCallback
void HandleCallback(Request &aRequest, Response &aResponse)
Definition: resource.cpp:148
otbr::rest::Resource::Init
void Init(void)
Definition: resource.cpp:127
ncp_openthread.hpp
response.hpp
otbr::rest::Request
Definition: request.hpp:50
otbr::rest::Resource::Handle
void Handle(Request &aRequest, Response &aResponse) const
Definition: resource.cpp:132
json.hpp
otbr::rest::NodeInfo
Definition: types.hpp:87
otbr::rest::Resource::ErrorHandler
void ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) const
Definition: resource.cpp:185
thread_helper.hpp
otbr::Ncp::ControllerOpenThread
Definition: ncp_openthread.hpp:64
otbr::rest::Resource::Resource
Resource(ControllerOpenThread *aNcp)
Definition: resource.cpp:107
request.hpp