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