xref: /aosp_15_r20/external/ot-br-posix/src/rest/resource.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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 "openthread-br/config.h"
38 
39 #include <unordered_map>
40 
41 #include <openthread/border_agent.h>
42 #include <openthread/border_router.h>
43 
44 #include "common/api_strings.hpp"
45 #include "ncp/rcp_host.hpp"
46 #include "openthread/dataset.h"
47 #include "openthread/dataset_ftd.h"
48 #include "rest/json.hpp"
49 #include "rest/request.hpp"
50 #include "rest/response.hpp"
51 #include "utils/thread_helper.hpp"
52 
53 using otbr::Ncp::RcpHost;
54 using std::chrono::steady_clock;
55 
56 namespace otbr {
57 namespace rest {
58 
59 /**
60  * This class implements the Resource handler for OTBR-REST.
61  */
62 class Resource
63 {
64 public:
65     /**
66      * The constructor initializes the resource handler instance.
67      *
68      * @param[in] aHost  A pointer to the Thread controller.
69      */
70     Resource(RcpHost *aHost);
71 
72     /**
73      * This method initialize the Resource handler.
74      */
75     void Init(void);
76 
77     /**
78      * This method is the main entry of resource handler, which find corresponding handler according to request url
79      * find the resource and set the content of response.
80      *
81      * @param[in]     aRequest  A request instance referred by the Resource handler.
82      * @param[in,out] aResponse  A response instance will be set by the Resource handler.
83      */
84     void Handle(Request &aRequest, Response &aResponse) const;
85 
86     /**
87      * This method distributes a callback handler for each connection needs a callback.
88      *
89      * @param[in]     aRequest   A request instance referred by the Resource handler.
90      * @param[in,out] aResponse  A response instance will be set by the Resource handler.
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     void ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) const;
102 
103 private:
104     /**
105      * This enumeration represents the Dataset type (active or pending).
106      */
107     enum class DatasetType : uint8_t
108     {
109         kActive,  ///< Active Dataset
110         kPending, ///< Pending Dataset
111     };
112 
113     typedef void (Resource::*ResourceHandler)(const Request &aRequest, Response &aResponse) const;
114     typedef void (Resource::*ResourceCallbackHandler)(const Request &aRequest, Response &aResponse);
115     void NodeInfo(const Request &aRequest, Response &aResponse) const;
116     void BaId(const Request &aRequest, Response &aResponse) const;
117     void ExtendedAddr(const Request &aRequest, Response &aResponse) const;
118     void State(const Request &aRequest, Response &aResponse) const;
119     void NetworkName(const Request &aRequest, Response &aResponse) const;
120     void LeaderData(const Request &aRequest, Response &aResponse) const;
121     void NumOfRoute(const Request &aRequest, Response &aResponse) const;
122     void Rloc16(const Request &aRequest, Response &aResponse) const;
123     void ExtendedPanId(const Request &aRequest, Response &aResponse) const;
124     void Rloc(const Request &aRequest, Response &aResponse) const;
125     void Dataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const;
126     void DatasetActive(const Request &aRequest, Response &aResponse) const;
127     void DatasetPending(const Request &aRequest, Response &aResponse) const;
128     void Diagnostic(const Request &aRequest, Response &aResponse) const;
129     void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse);
130 
131     void GetNodeInfo(Response &aResponse) const;
132     void DeleteNodeInfo(Response &aResponse) const;
133     void GetDataBaId(Response &aResponse) const;
134     void GetDataExtendedAddr(Response &aResponse) const;
135     void GetDataState(Response &aResponse) const;
136     void SetDataState(const Request &aRequest, Response &aResponse) const;
137     void GetDataNetworkName(Response &aResponse) const;
138     void GetDataLeaderData(Response &aResponse) const;
139     void GetDataNumOfRoute(Response &aResponse) const;
140     void GetDataRloc16(Response &aResponse) const;
141     void GetDataExtendedPanId(Response &aResponse) const;
142     void GetDataRloc(Response &aResponse) const;
143     void GetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const;
144     void SetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const;
145 
146     void DeleteOutDatedDiagnostic(void);
147     void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag);
148 
149     static void DiagnosticResponseHandler(otError              aError,
150                                           otMessage           *aMessage,
151                                           const otMessageInfo *aMessageInfo,
152                                           void                *aContext);
153     void        DiagnosticResponseHandler(otError aError, const otMessage *aMessage, const otMessageInfo *aMessageInfo);
154 
155     otInstance *mInstance;
156     RcpHost    *mHost;
157 
158     std::unordered_map<std::string, ResourceHandler>         mResourceMap;
159     std::unordered_map<std::string, ResourceCallbackHandler> mResourceCallbackMap;
160 
161     std::unordered_map<std::string, DiagInfo> mDiagSet;
162 };
163 
164 } // namespace rest
165 } // namespace otbr
166 
167 #endif // OTBR_REST_RESOURCE_HPP_
168