• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017, 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 implements the web server of border router
32  */
33 
34 #ifndef OTBR_WEB_WEB_SERVICE_WEB_SERVER_
35 #define OTBR_WEB_WEB_SERVICE_WEB_SERVER_
36 
37 #include "openthread-br/config.h"
38 
39 #include <httplib.h>
40 #include <net/if.h>
41 #include <string>
42 #include <syslog.h>
43 
44 #include "web/web-service/wpan_service.hpp"
45 
46 namespace otbr {
47 namespace Web {
48 
49 /**
50  * This class implements the http server.
51  */
52 class WebServer
53 {
54 public:
55     /**
56      * This method is constructor to initialize the WebServer.
57      */
58     WebServer(void);
59 
60     /**
61      * This method is destructor to free the WebServer.
62      */
63     ~WebServer(void);
64 
65     /**
66      * This method starts the Web Server.
67      *
68      * @param[in] aIfName      The pointer to the Thread interface name.
69      * @param[in] aListenAddr  The http server listen address, can be nullptr for any address.
70      * @param[in] aPort        The port of http server.
71      */
72     void StartWebServer(const char *aIfName, const char *aListenAddr, uint16_t aPort);
73 
74     /**
75      * This method stops the Web Server.
76      */
77     void StopWebServer(void);
78 
79 private:
80     typedef std::string (*HttpRequestCallback)(const std::string &aRequest, void *aUserData);
81     static std::string HandleJoinNetworkRequest(const std::string &aJoinRequest, void *aUserData);
82     static std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest, void *aUserData);
83     static std::string HandleFormNetworkRequest(const std::string &aFormRequest, void *aUserData);
84     static std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest, void *aUserData);
85     static std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest, void *aUserData);
86     static std::string HandleGetStatusRequest(const std::string &aGetStatusRequest, void *aUserData);
87     static std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest,
88                                                          void              *aUserData);
89     static std::string HandleCommission(const std::string &aCommissionRequest, void *aUserData);
90 
91     std::string HandleJoinNetworkRequest(const std::string &aJoinRequest);
92     std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest);
93     std::string HandleFormNetworkRequest(const std::string &aFormRequest);
94     std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest);
95     std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest);
96     std::string HandleGetStatusRequest(const std::string &aGetStatusRequest);
97     std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest);
98     std::string HandleCommission(const std::string &aCommissionRequest);
99 
100     void HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback);
101     void ResponseGetQRCode(void);
102     void ResponseJoinNetwork(void);
103     void ResponseFormNetwork(void);
104     void ResponseAddOnMeshPrefix(void);
105     void ResponseDeleteOnMeshPrefix(void);
106     void ResponseGetStatus(void);
107     void ResponseGetAvailableNetwork(void);
108     void DefaultHttpResponse(void);
109     void ResponseCommission(void);
110 
111     void Init(void);
112 
113     httplib::Server        mServer;
114     otbr::Web::WpanService mWpanService;
115 };
116 
117 } // namespace Web
118 } // namespace otbr
119 
120 #endif // OTBR_WEB_WEB_SERVICE_WEB_SERVER_
121