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/ncp_openthread.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::ControllerOpenThread; 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 */ 63 class Resource 64 { 65 public: 66 /** 67 * The constructor initializes the resource handler instance. 68 * 69 * @param[in] aNcp A pointer to the NCP controller. 70 * 71 */ 72 Resource(ControllerOpenThread *aNcp); 73 74 /** 75 * This method initialize the Resource handler. 76 * 77 * 78 */ 79 void Init(void); 80 81 /** 82 * This method is the main entry of resource handler, which find corresponding handler according to request url 83 * find the resource and set the content of response. 84 * 85 * @param[in] aRequest A request instance referred by the Resource handler. 86 * @param[in,out] aResponse A response instance will be set by the Resource handler. 87 * 88 */ 89 void Handle(Request &aRequest, Response &aResponse) const; 90 91 /** 92 * This method distributes a callback handler for each connection needs a callback. 93 * 94 * @param[in] aRequest A request instance referred by the Resource handler. 95 * @param[in,out] aResponse A response instance will be set by the Resource handler. 96 * 97 */ 98 void HandleCallback(Request &aRequest, Response &aResponse); 99 100 /** 101 * This method provides a quick handler, which could directly set response code of a response and set error code and 102 * error message to the request body. 103 * 104 * @param[in] aRequest A request instance referred by the Resource handler. 105 * @param[in,out] aErrorCode An enum class represents the status code. 106 * 107 */ 108 void ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) const; 109 110 private: 111 /** 112 * This enumeration represents the Dataset type (active or pending). 113 * 114 */ 115 enum class DatasetType : uint8_t 116 { 117 kActive, ///< Active Dataset 118 kPending, ///< Pending Dataset 119 }; 120 121 typedef void (Resource::*ResourceHandler)(const Request &aRequest, Response &aResponse) const; 122 typedef void (Resource::*ResourceCallbackHandler)(const Request &aRequest, Response &aResponse); 123 void NodeInfo(const Request &aRequest, Response &aResponse) const; 124 void BaId(const Request &aRequest, Response &aResponse) const; 125 void ExtendedAddr(const Request &aRequest, Response &aResponse) const; 126 void State(const Request &aRequest, Response &aResponse) const; 127 void NetworkName(const Request &aRequest, Response &aResponse) const; 128 void LeaderData(const Request &aRequest, Response &aResponse) const; 129 void NumOfRoute(const Request &aRequest, Response &aResponse) const; 130 void Rloc16(const Request &aRequest, Response &aResponse) const; 131 void ExtendedPanId(const Request &aRequest, Response &aResponse) const; 132 void Rloc(const Request &aRequest, Response &aResponse) const; 133 void Dataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 134 void DatasetActive(const Request &aRequest, Response &aResponse) const; 135 void DatasetPending(const Request &aRequest, Response &aResponse) const; 136 void Diagnostic(const Request &aRequest, Response &aResponse) const; 137 void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse); 138 139 void GetNodeInfo(Response &aResponse) const; 140 void DeleteNodeInfo(Response &aResponse) const; 141 void GetDataBaId(Response &aResponse) const; 142 void GetDataExtendedAddr(Response &aResponse) const; 143 void GetDataState(Response &aResponse) const; 144 void SetDataState(const Request &aRequest, Response &aResponse) const; 145 void GetDataNetworkName(Response &aResponse) const; 146 void GetDataLeaderData(Response &aResponse) const; 147 void GetDataNumOfRoute(Response &aResponse) const; 148 void GetDataRloc16(Response &aResponse) const; 149 void GetDataExtendedPanId(Response &aResponse) const; 150 void GetDataRloc(Response &aResponse) const; 151 void GetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 152 void SetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 153 154 void DeleteOutDatedDiagnostic(void); 155 void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag); 156 157 static void DiagnosticResponseHandler(otError aError, 158 otMessage *aMessage, 159 const otMessageInfo *aMessageInfo, 160 void *aContext); 161 void DiagnosticResponseHandler(otError aError, const otMessage *aMessage, const otMessageInfo *aMessageInfo); 162 163 otInstance *mInstance; 164 ControllerOpenThread *mNcp; 165 166 std::unordered_map<std::string, ResourceHandler> mResourceMap; 167 std::unordered_map<std::string, ResourceCallbackHandler> mResourceCallbackMap; 168 169 std::unordered_map<std::string, DiagInfo> mDiagSet; 170 }; 171 172 } // namespace rest 173 } // namespace otbr 174 175 #endif // OTBR_REST_RESOURCE_HPP_ 176