• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
67 class OpenThreadClient
68 {
69 public:
70     /**
71      * This constructor creates an OpenThread client.
72      *
73      * @param[in] aNetifName  The Thread network interface name.
74      *
75      */
76     OpenThreadClient(const char *aNetifName);
77 
78     /**
79      * This destructor destories an OpenThread client.
80      *
81      */
82     ~OpenThreadClient(void);
83 
84     /**
85      * This method connects to OpenThread daemon.
86      *
87      * @retval TRUE   Successfully connected to the daemon.
88      * @retval FALSE  Failed to connected to the daemon.
89      *
90      */
91     bool Connect(void);
92 
93     /**
94      * This method executes OpenThread CLI.
95      *
96      * @param[in] aFormat  C style format string.
97      * @param[in] ...      C style format arguments.
98      *
99      * @returns A pointer to the output if succeeded, otherwise nullptr.
100      *
101      */
102     char *Execute(const char *aFormat, ...);
103 
104     /**
105      * This method reads from OpenThread CLI.
106      *
107      * @param[in] aResponse  The expected read response.
108      * @param[in] aTimeout   Timeout for the read, in ms.
109      *
110      * @returns A pointer to the output if the expected response is found, otherwise nullptr.
111      *
112      */
113     char *Read(const char *aResponse, int aTimeout);
114 
115     /**
116      * This method scans Thread network.
117      *
118      * @param[out] aNetworks  A pointer to the buffer to store network information.
119      * @param[in]  aLength    Number of entries in @p aNetworks.
120      *
121      * @returns Number of entries found. 0 if none found.
122      *
123      */
124     int Scan(WpanNetworkInfo *aNetworks, int aLength);
125 
126     /**
127      * This method performs factory reset.
128      *
129      */
130     bool FactoryReset(void);
131 
132 private:
133     void Disconnect(void);
134     void DiscardRead(void);
135 
136     enum
137     {
138         kBufferSize     = 1024, ///< Maximum command line input and output buffer.
139         kDefaultTimeout = 800,  ///< Default timeout(ms) waiting for a command finish.
140     };
141 
142     const char *mNetifName;
143     char        mBuffer[kBufferSize];
144     int         mTimeout; /// Timeout in milliseconds
145     int         mSocket;
146 };
147 
148 } // namespace Web
149 } // namespace otbr
150 
151 #endif // OTBR_WEB_WEB_SERVICE_OT_CLIENT_HPP_
152