• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Handler definition for RESTful HTTP server.
32  */
33 
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 
47 using otbr::Ncp::ControllerOpenThread;
48 using std::chrono::steady_clock;
49 
50 namespace otbr {
51 namespace rest {
52 
53 /**
54  * This class implements the Resource handler for OTBR-REST.
55  *
56  */
57 class Resource
58 {
59 public:
60     /**
61      * The constructor initializes the resource handler instance.
62      *
63      * @param[in] aNcp  A pointer to the NCP controller.
64      *
65      */
66     Resource(ControllerOpenThread *aNcp);
67 
68     /**
69      * This method initialize the Resource handler.
70      *
71      *
72      */
73     void Init(void);
74 
75     /**
76      * This method is the main entry of resource handler, which find corresponding handler according to request url
77      * find the resource and set the content of response.
78      *
79      * @param[in]     aRequest  A request instance referred by the Resource handler.
80      * @param[in,out] aResponse  A response instance will be set by the Resource handler.
81      *
82      */
83     void Handle(Request &aRequest, Response &aResponse) const;
84 
85     /**
86      * This method distributes a callback handler for each connection needs a callback.
87      *
88      * @param[in]     aRequest   A request instance referred by the Resource handler.
89      * @param[in,out] aResponse  A response instance will be set by the Resource handler.
90      *
91      */
92     void HandleCallback(Request &aRequest, Response &aResponse);
93 
94     /**
95      * This method provides a quick handler, which could directly set response code of a response and set error code and
96      * error message to the request body.
97      *
98      * @param[in]     aRequest    A request instance referred by the Resource handler.
99      * @param[in,out] aErrorCode  An enum class represents the status code.
100      *
101      */
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_
151