1*4a64e381SAndroid Build Coastguard Worker /* 2*4a64e381SAndroid Build Coastguard Worker * Copyright (c) 2020, The OpenThread Authors. 3*4a64e381SAndroid Build Coastguard Worker * All rights reserved. 4*4a64e381SAndroid Build Coastguard Worker * 5*4a64e381SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*4a64e381SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 7*4a64e381SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 8*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 9*4a64e381SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 10*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 11*4a64e381SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 12*4a64e381SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the 13*4a64e381SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products 14*4a64e381SAndroid Build Coastguard Worker * derived from this software without specific prior written permission. 15*4a64e381SAndroid Build Coastguard Worker * 16*4a64e381SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17*4a64e381SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*4a64e381SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*4a64e381SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20*4a64e381SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21*4a64e381SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22*4a64e381SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23*4a64e381SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24*4a64e381SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25*4a64e381SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26*4a64e381SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 27*4a64e381SAndroid Build Coastguard Worker */ 28*4a64e381SAndroid Build Coastguard Worker 29*4a64e381SAndroid Build Coastguard Worker /** 30*4a64e381SAndroid Build Coastguard Worker * @file 31*4a64e381SAndroid Build Coastguard Worker * This file includes Handler definition for RESTful HTTP server. 32*4a64e381SAndroid Build Coastguard Worker */ 33*4a64e381SAndroid Build Coastguard Worker 34*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_REST_RESOURCE_HPP_ 35*4a64e381SAndroid Build Coastguard Worker #define OTBR_REST_RESOURCE_HPP_ 36*4a64e381SAndroid Build Coastguard Worker 37*4a64e381SAndroid Build Coastguard Worker #include "openthread-br/config.h" 38*4a64e381SAndroid Build Coastguard Worker 39*4a64e381SAndroid Build Coastguard Worker #include <unordered_map> 40*4a64e381SAndroid Build Coastguard Worker 41*4a64e381SAndroid Build Coastguard Worker #include <openthread/border_agent.h> 42*4a64e381SAndroid Build Coastguard Worker #include <openthread/border_router.h> 43*4a64e381SAndroid Build Coastguard Worker 44*4a64e381SAndroid Build Coastguard Worker #include "common/api_strings.hpp" 45*4a64e381SAndroid Build Coastguard Worker #include "ncp/rcp_host.hpp" 46*4a64e381SAndroid Build Coastguard Worker #include "openthread/dataset.h" 47*4a64e381SAndroid Build Coastguard Worker #include "openthread/dataset_ftd.h" 48*4a64e381SAndroid Build Coastguard Worker #include "rest/json.hpp" 49*4a64e381SAndroid Build Coastguard Worker #include "rest/request.hpp" 50*4a64e381SAndroid Build Coastguard Worker #include "rest/response.hpp" 51*4a64e381SAndroid Build Coastguard Worker #include "utils/thread_helper.hpp" 52*4a64e381SAndroid Build Coastguard Worker 53*4a64e381SAndroid Build Coastguard Worker using otbr::Ncp::RcpHost; 54*4a64e381SAndroid Build Coastguard Worker using std::chrono::steady_clock; 55*4a64e381SAndroid Build Coastguard Worker 56*4a64e381SAndroid Build Coastguard Worker namespace otbr { 57*4a64e381SAndroid Build Coastguard Worker namespace rest { 58*4a64e381SAndroid Build Coastguard Worker 59*4a64e381SAndroid Build Coastguard Worker /** 60*4a64e381SAndroid Build Coastguard Worker * This class implements the Resource handler for OTBR-REST. 61*4a64e381SAndroid Build Coastguard Worker */ 62*4a64e381SAndroid Build Coastguard Worker class Resource 63*4a64e381SAndroid Build Coastguard Worker { 64*4a64e381SAndroid Build Coastguard Worker public: 65*4a64e381SAndroid Build Coastguard Worker /** 66*4a64e381SAndroid Build Coastguard Worker * The constructor initializes the resource handler instance. 67*4a64e381SAndroid Build Coastguard Worker * 68*4a64e381SAndroid Build Coastguard Worker * @param[in] aHost A pointer to the Thread controller. 69*4a64e381SAndroid Build Coastguard Worker */ 70*4a64e381SAndroid Build Coastguard Worker Resource(RcpHost *aHost); 71*4a64e381SAndroid Build Coastguard Worker 72*4a64e381SAndroid Build Coastguard Worker /** 73*4a64e381SAndroid Build Coastguard Worker * This method initialize the Resource handler. 74*4a64e381SAndroid Build Coastguard Worker */ 75*4a64e381SAndroid Build Coastguard Worker void Init(void); 76*4a64e381SAndroid Build Coastguard Worker 77*4a64e381SAndroid Build Coastguard Worker /** 78*4a64e381SAndroid Build Coastguard Worker * This method is the main entry of resource handler, which find corresponding handler according to request url 79*4a64e381SAndroid Build Coastguard Worker * find the resource and set the content of response. 80*4a64e381SAndroid Build Coastguard Worker * 81*4a64e381SAndroid Build Coastguard Worker * @param[in] aRequest A request instance referred by the Resource handler. 82*4a64e381SAndroid Build Coastguard Worker * @param[in,out] aResponse A response instance will be set by the Resource handler. 83*4a64e381SAndroid Build Coastguard Worker */ 84*4a64e381SAndroid Build Coastguard Worker void Handle(Request &aRequest, Response &aResponse) const; 85*4a64e381SAndroid Build Coastguard Worker 86*4a64e381SAndroid Build Coastguard Worker /** 87*4a64e381SAndroid Build Coastguard Worker * This method distributes a callback handler for each connection needs a callback. 88*4a64e381SAndroid Build Coastguard Worker * 89*4a64e381SAndroid Build Coastguard Worker * @param[in] aRequest A request instance referred by the Resource handler. 90*4a64e381SAndroid Build Coastguard Worker * @param[in,out] aResponse A response instance will be set by the Resource handler. 91*4a64e381SAndroid Build Coastguard Worker */ 92*4a64e381SAndroid Build Coastguard Worker void HandleCallback(Request &aRequest, Response &aResponse); 93*4a64e381SAndroid Build Coastguard Worker 94*4a64e381SAndroid Build Coastguard Worker /** 95*4a64e381SAndroid Build Coastguard Worker * This method provides a quick handler, which could directly set response code of a response and set error code and 96*4a64e381SAndroid Build Coastguard Worker * error message to the request body. 97*4a64e381SAndroid Build Coastguard Worker * 98*4a64e381SAndroid Build Coastguard Worker * @param[in] aRequest A request instance referred by the Resource handler. 99*4a64e381SAndroid Build Coastguard Worker * @param[in,out] aErrorCode An enum class represents the status code. 100*4a64e381SAndroid Build Coastguard Worker */ 101*4a64e381SAndroid Build Coastguard Worker void ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) const; 102*4a64e381SAndroid Build Coastguard Worker 103*4a64e381SAndroid Build Coastguard Worker private: 104*4a64e381SAndroid Build Coastguard Worker /** 105*4a64e381SAndroid Build Coastguard Worker * This enumeration represents the Dataset type (active or pending). 106*4a64e381SAndroid Build Coastguard Worker */ 107*4a64e381SAndroid Build Coastguard Worker enum class DatasetType : uint8_t 108*4a64e381SAndroid Build Coastguard Worker { 109*4a64e381SAndroid Build Coastguard Worker kActive, ///< Active Dataset 110*4a64e381SAndroid Build Coastguard Worker kPending, ///< Pending Dataset 111*4a64e381SAndroid Build Coastguard Worker }; 112*4a64e381SAndroid Build Coastguard Worker 113*4a64e381SAndroid Build Coastguard Worker typedef void (Resource::*ResourceHandler)(const Request &aRequest, Response &aResponse) const; 114*4a64e381SAndroid Build Coastguard Worker typedef void (Resource::*ResourceCallbackHandler)(const Request &aRequest, Response &aResponse); 115*4a64e381SAndroid Build Coastguard Worker void NodeInfo(const Request &aRequest, Response &aResponse) const; 116*4a64e381SAndroid Build Coastguard Worker void BaId(const Request &aRequest, Response &aResponse) const; 117*4a64e381SAndroid Build Coastguard Worker void ExtendedAddr(const Request &aRequest, Response &aResponse) const; 118*4a64e381SAndroid Build Coastguard Worker void State(const Request &aRequest, Response &aResponse) const; 119*4a64e381SAndroid Build Coastguard Worker void NetworkName(const Request &aRequest, Response &aResponse) const; 120*4a64e381SAndroid Build Coastguard Worker void LeaderData(const Request &aRequest, Response &aResponse) const; 121*4a64e381SAndroid Build Coastguard Worker void NumOfRoute(const Request &aRequest, Response &aResponse) const; 122*4a64e381SAndroid Build Coastguard Worker void Rloc16(const Request &aRequest, Response &aResponse) const; 123*4a64e381SAndroid Build Coastguard Worker void ExtendedPanId(const Request &aRequest, Response &aResponse) const; 124*4a64e381SAndroid Build Coastguard Worker void Rloc(const Request &aRequest, Response &aResponse) const; 125*4a64e381SAndroid Build Coastguard Worker void Dataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 126*4a64e381SAndroid Build Coastguard Worker void DatasetActive(const Request &aRequest, Response &aResponse) const; 127*4a64e381SAndroid Build Coastguard Worker void DatasetPending(const Request &aRequest, Response &aResponse) const; 128*4a64e381SAndroid Build Coastguard Worker void Diagnostic(const Request &aRequest, Response &aResponse) const; 129*4a64e381SAndroid Build Coastguard Worker void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse); 130*4a64e381SAndroid Build Coastguard Worker 131*4a64e381SAndroid Build Coastguard Worker void GetNodeInfo(Response &aResponse) const; 132*4a64e381SAndroid Build Coastguard Worker void DeleteNodeInfo(Response &aResponse) const; 133*4a64e381SAndroid Build Coastguard Worker void GetDataBaId(Response &aResponse) const; 134*4a64e381SAndroid Build Coastguard Worker void GetDataExtendedAddr(Response &aResponse) const; 135*4a64e381SAndroid Build Coastguard Worker void GetDataState(Response &aResponse) const; 136*4a64e381SAndroid Build Coastguard Worker void SetDataState(const Request &aRequest, Response &aResponse) const; 137*4a64e381SAndroid Build Coastguard Worker void GetDataNetworkName(Response &aResponse) const; 138*4a64e381SAndroid Build Coastguard Worker void GetDataLeaderData(Response &aResponse) const; 139*4a64e381SAndroid Build Coastguard Worker void GetDataNumOfRoute(Response &aResponse) const; 140*4a64e381SAndroid Build Coastguard Worker void GetDataRloc16(Response &aResponse) const; 141*4a64e381SAndroid Build Coastguard Worker void GetDataExtendedPanId(Response &aResponse) const; 142*4a64e381SAndroid Build Coastguard Worker void GetDataRloc(Response &aResponse) const; 143*4a64e381SAndroid Build Coastguard Worker void GetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 144*4a64e381SAndroid Build Coastguard Worker void SetDataset(DatasetType aDatasetType, const Request &aRequest, Response &aResponse) const; 145*4a64e381SAndroid Build Coastguard Worker 146*4a64e381SAndroid Build Coastguard Worker void DeleteOutDatedDiagnostic(void); 147*4a64e381SAndroid Build Coastguard Worker void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag); 148*4a64e381SAndroid Build Coastguard Worker 149*4a64e381SAndroid Build Coastguard Worker static void DiagnosticResponseHandler(otError aError, 150*4a64e381SAndroid Build Coastguard Worker otMessage *aMessage, 151*4a64e381SAndroid Build Coastguard Worker const otMessageInfo *aMessageInfo, 152*4a64e381SAndroid Build Coastguard Worker void *aContext); 153*4a64e381SAndroid Build Coastguard Worker void DiagnosticResponseHandler(otError aError, const otMessage *aMessage, const otMessageInfo *aMessageInfo); 154*4a64e381SAndroid Build Coastguard Worker 155*4a64e381SAndroid Build Coastguard Worker otInstance *mInstance; 156*4a64e381SAndroid Build Coastguard Worker RcpHost *mHost; 157*4a64e381SAndroid Build Coastguard Worker 158*4a64e381SAndroid Build Coastguard Worker std::unordered_map<std::string, ResourceHandler> mResourceMap; 159*4a64e381SAndroid Build Coastguard Worker std::unordered_map<std::string, ResourceCallbackHandler> mResourceCallbackMap; 160*4a64e381SAndroid Build Coastguard Worker 161*4a64e381SAndroid Build Coastguard Worker std::unordered_map<std::string, DiagInfo> mDiagSet; 162*4a64e381SAndroid Build Coastguard Worker }; 163*4a64e381SAndroid Build Coastguard Worker 164*4a64e381SAndroid Build Coastguard Worker } // namespace rest 165*4a64e381SAndroid Build Coastguard Worker } // namespace otbr 166*4a64e381SAndroid Build Coastguard Worker 167*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_REST_RESOURCE_HPP_ 168