xref: /aosp_15_r20/external/ot-br-posix/src/web/web-service/ot_client.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *  Copyright (c) 2019, 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 definitions for OpenThread daemon client.
32  */
33 
34 #ifndef OTBR_WEB_WEB_SERVICE_OT_CLIENT_HPP_
35 #define OTBR_WEB_WEB_SERVICE_OT_CLIENT_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #include <stdint.h>
40 
41 namespace otbr {
42 namespace Web {
43 
44 #define OT_SCANNED_NET_BUFFER_SIZE 250
45 #define OT_SET_MAX_DATA_SIZE 250
46 #define OT_NETWORK_NAME_MAX_SIZE 17
47 #define OT_HARDWARE_ADDRESS_SIZE 8
48 #define OT_PREFIX_SIZE 8
49 #define OT_ROUTER_ROLE 2
50 
51 struct WpanNetworkInfo
52 {
53     char     mNetworkName[OT_NETWORK_NAME_MAX_SIZE];
54     bool     mAllowingJoin;
55     uint16_t mPanId;
56     uint16_t mChannel;
57     uint64_t mExtPanId;
58     int8_t   mRssi;
59     uint8_t  mHardwareAddress[OT_HARDWARE_ADDRESS_SIZE];
60     uint8_t  mPrefix[OT_PREFIX_SIZE];
61 };
62 
63 /**
64  * This class implements functionality of OpenThread client.
65  */
66 class OpenThreadClient
67 {
68 public:
69     /**
70      * This constructor creates an OpenThread client.
71      *
72      * @param[in] aNetifName  The Thread network interface name.
73      */
74     OpenThreadClient(const char *aNetifName);
75 
76     /**
77      * This destructor destories an OpenThread client.
78      */
79     ~OpenThreadClient(void);
80 
81     /**
82      * This method connects to OpenThread daemon.
83      *
84      * @retval TRUE   Successfully connected to the daemon.
85      * @retval FALSE  Failed to connected to the daemon.
86      */
87     bool Connect(void);
88 
89     /**
90      * This method executes OpenThread CLI.
91      *
92      * @param[in] aFormat  C style format string.
93      * @param[in] ...      C style format arguments.
94      *
95      * @returns A pointer to the output if succeeded, otherwise nullptr.
96      */
97     char *Execute(const char *aFormat, ...);
98 
99     /**
100      * This method reads from OpenThread CLI.
101      *
102      * @param[in] aResponse  The expected read response.
103      * @param[in] aTimeout   Timeout for the read, in ms.
104      *
105      * @returns A pointer to the output if the expected response is found, otherwise nullptr.
106      */
107     char *Read(const char *aResponse, int aTimeout);
108 
109     /**
110      * This method scans Thread network.
111      *
112      * @param[out] aNetworks  A pointer to the buffer to store network information.
113      * @param[in]  aLength    Number of entries in @p aNetworks.
114      *
115      * @returns Number of entries found. 0 if none found.
116      */
117     int Scan(WpanNetworkInfo *aNetworks, int aLength);
118 
119     /**
120      * This method performs factory reset.
121      */
122     bool FactoryReset(void);
123 
124 private:
125     void Disconnect(void);
126     void DiscardRead(void);
127 
128     enum
129     {
130         kBufferSize     = 1024, ///< Maximum command line input and output buffer.
131         kDefaultTimeout = 800,  ///< Default timeout(ms) waiting for a command finish.
132     };
133 
134     const char *mNetifName;
135     char        mBuffer[kBufferSize];
136     int         mTimeout; /// Timeout in milliseconds
137     int         mSocket;
138 };
139 
140 } // namespace Web
141 } // namespace otbr
142 
143 #endif // OTBR_WEB_WEB_SERVICE_OT_CLIENT_HPP_
144