xref: /aosp_15_r20/external/ot-br-posix/src/web/web-service/wpan_service.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker /*
2*4a64e381SAndroid Build Coastguard Worker  *  Copyright (c) 2017, 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 implements the wpan controller service
32*4a64e381SAndroid Build Coastguard Worker  */
33*4a64e381SAndroid Build Coastguard Worker 
34*4a64e381SAndroid Build Coastguard Worker #ifndef OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_
35*4a64e381SAndroid Build Coastguard Worker #define OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_
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 <net/if.h>
40*4a64e381SAndroid Build Coastguard Worker #include <stdint.h>
41*4a64e381SAndroid Build Coastguard Worker #include <stdio.h>
42*4a64e381SAndroid Build Coastguard Worker #include <stdlib.h>
43*4a64e381SAndroid Build Coastguard Worker #include <string.h>
44*4a64e381SAndroid Build Coastguard Worker 
45*4a64e381SAndroid Build Coastguard Worker #include <json/json.h>
46*4a64e381SAndroid Build Coastguard Worker #include <json/writer.h>
47*4a64e381SAndroid Build Coastguard Worker 
48*4a64e381SAndroid Build Coastguard Worker #include "common/logging.hpp"
49*4a64e381SAndroid Build Coastguard Worker #include "utils/hex.hpp"
50*4a64e381SAndroid Build Coastguard Worker #include "utils/pskc.hpp"
51*4a64e381SAndroid Build Coastguard Worker #include "web/web-service/ot_client.hpp"
52*4a64e381SAndroid Build Coastguard Worker 
53*4a64e381SAndroid Build Coastguard Worker /**
54*4a64e381SAndroid Build Coastguard Worker  * WPAN parameter constants
55*4a64e381SAndroid Build Coastguard Worker  */
56*4a64e381SAndroid Build Coastguard Worker 
57*4a64e381SAndroid Build Coastguard Worker #define OT_EXTENDED_PANID_LENGTH 8
58*4a64e381SAndroid Build Coastguard Worker #define OT_HARDWARE_ADDRESS_LENGTH 8
59*4a64e381SAndroid Build Coastguard Worker #define OT_NETWORK_NAME_LENGTH 16
60*4a64e381SAndroid Build Coastguard Worker #define OT_PANID_LENGTH 2
61*4a64e381SAndroid Build Coastguard Worker #define OT_PSKC_MAX_LENGTH 16
62*4a64e381SAndroid Build Coastguard Worker #define OT_HEX_PREFIX_LENGTH 2
63*4a64e381SAndroid Build Coastguard Worker #define OT_PUBLISH_SERVICE_INTERVAL 20
64*4a64e381SAndroid Build Coastguard Worker 
65*4a64e381SAndroid Build Coastguard Worker namespace otbr {
66*4a64e381SAndroid Build Coastguard Worker namespace Web {
67*4a64e381SAndroid Build Coastguard Worker 
68*4a64e381SAndroid Build Coastguard Worker /**
69*4a64e381SAndroid Build Coastguard Worker  * This class provides web service to manage WPAN.
70*4a64e381SAndroid Build Coastguard Worker  */
71*4a64e381SAndroid Build Coastguard Worker class WpanService
72*4a64e381SAndroid Build Coastguard Worker {
73*4a64e381SAndroid Build Coastguard Worker public:
74*4a64e381SAndroid Build Coastguard Worker     /**
75*4a64e381SAndroid Build Coastguard Worker      * This method handles http request to get information to generate QR code.
76*4a64e381SAndroid Build Coastguard Worker      *
77*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of getting QR code.
78*4a64e381SAndroid Build Coastguard Worker      */
79*4a64e381SAndroid Build Coastguard Worker     std::string HandleGetQRCodeRequest(void);
80*4a64e381SAndroid Build Coastguard Worker 
81*4a64e381SAndroid Build Coastguard Worker     /**
82*4a64e381SAndroid Build Coastguard Worker      * This method handles the http request to join network.
83*4a64e381SAndroid Build Coastguard Worker      *
84*4a64e381SAndroid Build Coastguard Worker      * @param[in]  aJoinRequest  A reference to the http request of joining network.
85*4a64e381SAndroid Build Coastguard Worker      *
86*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of joining network.
87*4a64e381SAndroid Build Coastguard Worker      */
88*4a64e381SAndroid Build Coastguard Worker     std::string HandleJoinNetworkRequest(const std::string &aJoinRequest);
89*4a64e381SAndroid Build Coastguard Worker 
90*4a64e381SAndroid Build Coastguard Worker     /**
91*4a64e381SAndroid Build Coastguard Worker      * This method handles the http request to form network.
92*4a64e381SAndroid Build Coastguard Worker      *
93*4a64e381SAndroid Build Coastguard Worker      * @param[in]  aFormRequest  A reference to the http request of forming network.
94*4a64e381SAndroid Build Coastguard Worker      *
95*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of forming network.
96*4a64e381SAndroid Build Coastguard Worker      */
97*4a64e381SAndroid Build Coastguard Worker     std::string HandleFormNetworkRequest(const std::string &aFormRequest);
98*4a64e381SAndroid Build Coastguard Worker 
99*4a64e381SAndroid Build Coastguard Worker     /**
100*4a64e381SAndroid Build Coastguard Worker      * This method handles the http request to add on-mesh prefix.
101*4a64e381SAndroid Build Coastguard Worker      *
102*4a64e381SAndroid Build Coastguard Worker      * @param[in]  aAddPrefixRequest  A reference to the http request of adding on-mesh prefix.
103*4a64e381SAndroid Build Coastguard Worker      *
104*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of adding on-mesh prefix.
105*4a64e381SAndroid Build Coastguard Worker      */
106*4a64e381SAndroid Build Coastguard Worker     std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest);
107*4a64e381SAndroid Build Coastguard Worker 
108*4a64e381SAndroid Build Coastguard Worker     /**
109*4a64e381SAndroid Build Coastguard Worker      * This method handles the http request to delete on-mesh prefix http request.
110*4a64e381SAndroid Build Coastguard Worker      *
111*4a64e381SAndroid Build Coastguard Worker      * @param[in]  aDeleteRequest  A reference to the http request of deleting on-mesh prefix.
112*4a64e381SAndroid Build Coastguard Worker      *
113*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of deleting on-mesh prefix.
114*4a64e381SAndroid Build Coastguard Worker      */
115*4a64e381SAndroid Build Coastguard Worker     std::string HandleDeletePrefixRequest(const std::string &aDeleteRequest);
116*4a64e381SAndroid Build Coastguard Worker 
117*4a64e381SAndroid Build Coastguard Worker     /**
118*4a64e381SAndroid Build Coastguard Worker      * This method handles http request to get netowrk status.
119*4a64e381SAndroid Build Coastguard Worker      *
120*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of getting status.
121*4a64e381SAndroid Build Coastguard Worker      */
122*4a64e381SAndroid Build Coastguard Worker     std::string HandleStatusRequest(void);
123*4a64e381SAndroid Build Coastguard Worker 
124*4a64e381SAndroid Build Coastguard Worker     /**
125*4a64e381SAndroid Build Coastguard Worker      * This method handles http request to get available networks.
126*4a64e381SAndroid Build Coastguard Worker      *
127*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of getting available networks.
128*4a64e381SAndroid Build Coastguard Worker      */
129*4a64e381SAndroid Build Coastguard Worker     std::string HandleAvailableNetworkRequest(void);
130*4a64e381SAndroid Build Coastguard Worker 
131*4a64e381SAndroid Build Coastguard Worker     /**
132*4a64e381SAndroid Build Coastguard Worker      * This method handles http request to commission device
133*4a64e381SAndroid Build Coastguard Worker      *
134*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of commissioning
135*4a64e381SAndroid Build Coastguard Worker      */
136*4a64e381SAndroid Build Coastguard Worker     std::string HandleCommission(const std::string &aCommissionRequest);
137*4a64e381SAndroid Build Coastguard Worker 
138*4a64e381SAndroid Build Coastguard Worker     /**
139*4a64e381SAndroid Build Coastguard Worker      * This method sets the Thread interface name.
140*4a64e381SAndroid Build Coastguard Worker      *
141*4a64e381SAndroid Build Coastguard Worker      * @param[in] aIfName  The pointer to the Thread interface name.
142*4a64e381SAndroid Build Coastguard Worker      */
SetInterfaceName(const char * aIfName)143*4a64e381SAndroid Build Coastguard Worker     void SetInterfaceName(const char *aIfName)
144*4a64e381SAndroid Build Coastguard Worker     {
145*4a64e381SAndroid Build Coastguard Worker         strncpy(mIfName, aIfName, sizeof(mIfName) - 1);
146*4a64e381SAndroid Build Coastguard Worker         mIfName[sizeof(mIfName) - 1] = '\0';
147*4a64e381SAndroid Build Coastguard Worker     }
148*4a64e381SAndroid Build Coastguard Worker 
149*4a64e381SAndroid Build Coastguard Worker     /**
150*4a64e381SAndroid Build Coastguard Worker      * This method gets status of wpan service.
151*4a64e381SAndroid Build Coastguard Worker      *
152*4a64e381SAndroid Build Coastguard Worker      * @param[in,out] aNetworkName  The pointer to the network name.
153*4a64e381SAndroid Build Coastguard Worker      * @param[in,out] aIfName       The pointer to the extended PAN ID.
154*4a64e381SAndroid Build Coastguard Worker      *
155*4a64e381SAndroid Build Coastguard Worker      * @retval kWpanStatus_OK       Successfully started the Thread service.
156*4a64e381SAndroid Build Coastguard Worker      * @retval kWpanStatus_Offline  Not started the Thread service.
157*4a64e381SAndroid Build Coastguard Worker      * @retval kWpanStatus_Down     The Thread service was down.
158*4a64e381SAndroid Build Coastguard Worker      */
159*4a64e381SAndroid Build Coastguard Worker     int GetWpanServiceStatus(std::string &aNetworkName, std::string &aExtPanId) const;
160*4a64e381SAndroid Build Coastguard Worker 
161*4a64e381SAndroid Build Coastguard Worker     /**
162*4a64e381SAndroid Build Coastguard Worker      * This method starts commissioner and wait for a device to join
163*4a64e381SAndroid Build Coastguard Worker      *
164*4a64e381SAndroid Build Coastguard Worker      * @param[in] aPskd             Joiner pskd
165*4a64e381SAndroid Build Coastguard Worker      * @param[in] aNetworkPassword  Network password
166*4a64e381SAndroid Build Coastguard Worker      *
167*4a64e381SAndroid Build Coastguard Worker      * @returns The string to the http response of getting available networks.
168*4a64e381SAndroid Build Coastguard Worker      */
169*4a64e381SAndroid Build Coastguard Worker     std::string CommissionDevice(const char *aPskd, const char *aNetworkPassword);
170*4a64e381SAndroid Build Coastguard Worker 
171*4a64e381SAndroid Build Coastguard Worker private:
172*4a64e381SAndroid Build Coastguard Worker     int                formActiveDataset(otbr::Web::OpenThreadClient &aClient,
173*4a64e381SAndroid Build Coastguard Worker                                          const std::string           &aNetworkKey,
174*4a64e381SAndroid Build Coastguard Worker                                          const std::string           &aNetworkName,
175*4a64e381SAndroid Build Coastguard Worker                                          const std::string           &aPskc,
176*4a64e381SAndroid Build Coastguard Worker                                          uint16_t                     aChannel,
177*4a64e381SAndroid Build Coastguard Worker                                          uint64_t                     aExtPanId,
178*4a64e381SAndroid Build Coastguard Worker                                          uint16_t                     aPanId);
179*4a64e381SAndroid Build Coastguard Worker     int                joinActiveDataset(otbr::Web::OpenThreadClient &aClient,
180*4a64e381SAndroid Build Coastguard Worker                                          const std::string           &aNetworkKey,
181*4a64e381SAndroid Build Coastguard Worker                                          uint16_t                     aChannel,
182*4a64e381SAndroid Build Coastguard Worker                                          uint16_t                     aPanId);
183*4a64e381SAndroid Build Coastguard Worker     static std::string escapeOtCliEscapable(const std::string &aArg);
184*4a64e381SAndroid Build Coastguard Worker 
185*4a64e381SAndroid Build Coastguard Worker     WpanNetworkInfo mNetworks[OT_SCANNED_NET_BUFFER_SIZE];
186*4a64e381SAndroid Build Coastguard Worker     int             mNetworksCount;
187*4a64e381SAndroid Build Coastguard Worker     char            mIfName[IFNAMSIZ];
188*4a64e381SAndroid Build Coastguard Worker     std::string     mNetworkName;
189*4a64e381SAndroid Build Coastguard Worker     std::string     mExtPanId;
190*4a64e381SAndroid Build Coastguard Worker 
191*4a64e381SAndroid Build Coastguard Worker     enum
192*4a64e381SAndroid Build Coastguard Worker     {
193*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_Ok = 0,
194*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_Associating,
195*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_Down,
196*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_FormFailed,
197*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_GetPropertyFailed,
198*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_JoinFailed,
199*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_JoinFailed_NotFound,
200*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_JoinFailed_Security,
201*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_LeaveFailed,
202*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_NetworkNotFound,
203*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_Offline,
204*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_ParseRequestFailed,
205*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_ScanFailed,
206*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_SetFailed,
207*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_SetGatewayFailed,
208*4a64e381SAndroid Build Coastguard Worker         kWpanStatus_Uninitialized,
209*4a64e381SAndroid Build Coastguard Worker     };
210*4a64e381SAndroid Build Coastguard Worker 
211*4a64e381SAndroid Build Coastguard Worker     enum
212*4a64e381SAndroid Build Coastguard Worker     {
213*4a64e381SAndroid Build Coastguard Worker         kPropertyType_String = 0,
214*4a64e381SAndroid Build Coastguard Worker         kPropertyType_Data,
215*4a64e381SAndroid Build Coastguard Worker     };
216*4a64e381SAndroid Build Coastguard Worker };
217*4a64e381SAndroid Build Coastguard Worker 
218*4a64e381SAndroid Build Coastguard Worker } // namespace Web
219*4a64e381SAndroid Build Coastguard Worker } // namespace otbr
220*4a64e381SAndroid Build Coastguard Worker 
221*4a64e381SAndroid Build Coastguard Worker #endif // OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_
222