1 /*
2  *  Copyright (c) 2021, 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 contains definitions for a TCP CLI tool.
32  */
33 
34 #ifndef CLI_TCP_EXAMPLE_HPP_
35 #define CLI_TCP_EXAMPLE_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <openthread/tcp.h>
40 
41 #include "cli/cli_config.h"
42 #include "common/time.hpp"
43 #include "utils/lookup_table.hpp"
44 #include "utils/parse_cmdline.hpp"
45 
46 namespace ot {
47 namespace Cli {
48 
49 class Interpreter;
50 
51 /**
52  * This class implements a CLI-based TCP example.
53  *
54  */
55 class TcpExample
56 {
57 public:
58     using Arg = Utils::CmdLineParser::Arg;
59 
60     /**
61      * Constructor
62      *
63      * @param[in]  aInterpreter  The CLI interpreter.
64      *
65      */
66     explicit TcpExample(Interpreter &aInterpreter);
67 
68     /**
69      * This mehtod interprets a list of CLI arguments.
70      *
71      * @param[in]  aArgs   An array of command line arguments.
72      *
73      */
74     otError Process(Arg aArgs[]);
75 
76 private:
77     struct Command
78     {
79         const char *mName;
80         otError (TcpExample::*mHandler)(Arg aArgs[]);
81     };
82 
83     otError ProcessHelp(Arg aArgs[]);
84     otError ProcessInit(Arg aArgs[]);
85     otError ProcessDeinit(Arg aArgs[]);
86     otError ProcessBind(Arg aArgs[]);
87     otError ProcessConnect(Arg aArgs[]);
88     otError ProcessSend(Arg aArgs[]);
89     otError ProcessBenchmark(Arg aArgs[]);
90     otError ProcessSendEnd(Arg aArgs[]);
91     otError ProcessAbort(Arg aArgs[]);
92     otError ProcessListen(Arg aArgs[]);
93     otError ProcessStopListening(Arg aArgs[]);
94 
95     static void HandleTcpEstablishedCallback(otTcpEndpoint *aEndpoint);
96     static void HandleTcpSendDoneCallback(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData);
97     static void HandleTcpReceiveAvailableCallback(otTcpEndpoint *aEndpoint,
98                                                   size_t         aBytesAvailable,
99                                                   bool           aEndOfStream,
100                                                   size_t         aBytesRemaining);
101     static void HandleTcpDisconnectedCallback(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason);
102     static otTcpIncomingConnectionAction HandleTcpAcceptReadyCallback(otTcpListener *   aListener,
103                                                                       const otSockAddr *aPeer,
104                                                                       otTcpEndpoint **  aAcceptInto);
105     static void                          HandleTcpAcceptDoneCallback(otTcpListener *   aListener,
106                                                                      otTcpEndpoint *   aEndpoint,
107                                                                      const otSockAddr *aPeer);
108 
109     void                          HandleTcpEstablished(otTcpEndpoint *aEndpoint);
110     void                          HandleTcpSendDone(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData);
111     void                          HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint,
112                                                             size_t         aBytesAvailable,
113                                                             bool           aEndOfStream,
114                                                             size_t         aBytesRemaining);
115     void                          HandleTcpDisconnected(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason);
116     otTcpIncomingConnectionAction HandleTcpAcceptReady(otTcpListener *   aListener,
117                                                        const otSockAddr *aPeer,
118                                                        otTcpEndpoint **  aAcceptInto);
119     void HandleTcpAcceptDone(otTcpListener *aListener, otTcpEndpoint *aEndpoint, const otSockAddr *aPeer);
120 
121     static constexpr Command sCommands[] = {
122         {"abort", &TcpExample::ProcessAbort},
123         {"benchmark", &TcpExample::ProcessBenchmark},
124         {"bind", &TcpExample::ProcessBind},
125         {"connect", &TcpExample::ProcessConnect},
126         {"deinit", &TcpExample::ProcessDeinit},
127         {"help", &TcpExample::ProcessHelp},
128         {"init", &TcpExample::ProcessInit},
129         {"listen", &TcpExample::ProcessListen},
130         {"send", &TcpExample::ProcessSend},
131         {"sendend", &TcpExample::ProcessSendEnd},
132         {"stoplistening", &TcpExample::ProcessStopListening},
133     };
134 
135     static_assert(Utils::LookupTable::IsSorted(sCommands), "Command Table is not sorted");
136 
137     Interpreter &mInterpreter;
138 
139     otTcpEndpoint mEndpoint;
140     otTcpListener mListener;
141 
142     bool mInitialized;
143     bool mEndpointConnected;
144     bool mSendBusy;
145 
146     otLinkedBuffer mSendLink;
147     uint8_t        mSendBuffer[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
148     uint8_t        mReceiveBuffer[OPENTHREAD_CONFIG_CLI_TCP_RECEIVE_BUFFER_SIZE];
149 
150     otLinkedBuffer mBenchmarkLinks[(sizeof(mReceiveBuffer) + sizeof(mSendBuffer) - 1) / sizeof(mSendBuffer)];
151     uint32_t       mBenchmarkBytesTotal;
152     uint32_t       mBenchmarkLinksLeft;
153     TimeMilli      mBenchmarkStart;
154 };
155 
156 } // namespace Cli
157 } // namespace ot
158 
159 #endif // CLI_TCP_EXAMPLE_HPP_
160