• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cli/cli_output.hpp"
43 #include "common/time.hpp"
44 
45 namespace ot {
46 namespace Cli {
47 
48 /**
49  * This class implements a CLI-based TCP example.
50  *
51  */
52 class TcpExample : private OutputWrapper
53 {
54 public:
55     using Arg = Utils::CmdLineParser::Arg;
56 
57     /**
58      * Constructor
59      *
60      * @param[in]  aOutput  The CLI console output context.
61      *
62      */
63     explicit TcpExample(Output &aOutput);
64 
65     /**
66      * This method interprets a list of CLI arguments.
67      *
68      * @param[in]  aArgs   An array of command line arguments.
69      *
70      */
71     otError Process(Arg aArgs[]);
72 
73 private:
74     using Command = CommandEntry<TcpExample>;
75 
76     otError ProcessHelp(Arg aArgs[]);
77     otError ProcessInit(Arg aArgs[]);
78     otError ProcessDeinit(Arg aArgs[]);
79     otError ProcessBind(Arg aArgs[]);
80     otError ProcessConnect(Arg aArgs[]);
81     otError ProcessSend(Arg aArgs[]);
82     otError ProcessBenchmark(Arg aArgs[]);
83     otError ProcessSendEnd(Arg aArgs[]);
84     otError ProcessAbort(Arg aArgs[]);
85     otError ProcessListen(Arg aArgs[]);
86     otError ProcessStopListening(Arg aArgs[]);
87 
88     static void HandleTcpEstablishedCallback(otTcpEndpoint *aEndpoint);
89     static void HandleTcpSendDoneCallback(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData);
90     static void HandleTcpReceiveAvailableCallback(otTcpEndpoint *aEndpoint,
91                                                   size_t         aBytesAvailable,
92                                                   bool           aEndOfStream,
93                                                   size_t         aBytesRemaining);
94     static void HandleTcpDisconnectedCallback(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason);
95     static otTcpIncomingConnectionAction HandleTcpAcceptReadyCallback(otTcpListener *   aListener,
96                                                                       const otSockAddr *aPeer,
97                                                                       otTcpEndpoint **  aAcceptInto);
98     static void                          HandleTcpAcceptDoneCallback(otTcpListener *   aListener,
99                                                                      otTcpEndpoint *   aEndpoint,
100                                                                      const otSockAddr *aPeer);
101 
102     void                          HandleTcpEstablished(otTcpEndpoint *aEndpoint);
103     void                          HandleTcpSendDone(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData);
104     void                          HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint,
105                                                             size_t         aBytesAvailable,
106                                                             bool           aEndOfStream,
107                                                             size_t         aBytesRemaining);
108     void                          HandleTcpDisconnected(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason);
109     otTcpIncomingConnectionAction HandleTcpAcceptReady(otTcpListener *   aListener,
110                                                        const otSockAddr *aPeer,
111                                                        otTcpEndpoint **  aAcceptInto);
112     void HandleTcpAcceptDone(otTcpListener *aListener, otTcpEndpoint *aEndpoint, const otSockAddr *aPeer);
113 
114     static constexpr Command sCommands[] = {
115         {"abort", &TcpExample::ProcessAbort},
116         {"benchmark", &TcpExample::ProcessBenchmark},
117         {"bind", &TcpExample::ProcessBind},
118         {"connect", &TcpExample::ProcessConnect},
119         {"deinit", &TcpExample::ProcessDeinit},
120         {"help", &TcpExample::ProcessHelp},
121         {"init", &TcpExample::ProcessInit},
122         {"listen", &TcpExample::ProcessListen},
123         {"send", &TcpExample::ProcessSend},
124         {"sendend", &TcpExample::ProcessSendEnd},
125         {"stoplistening", &TcpExample::ProcessStopListening},
126     };
127 
128     static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
129 
130     otTcpEndpoint mEndpoint;
131     otTcpListener mListener;
132 
133     bool mInitialized;
134     bool mEndpointConnected;
135     bool mSendBusy;
136 
137     otLinkedBuffer mSendLink;
138     uint8_t        mSendBuffer[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
139     uint8_t        mReceiveBuffer[OPENTHREAD_CONFIG_CLI_TCP_RECEIVE_BUFFER_SIZE];
140 
141     otLinkedBuffer mBenchmarkLinks[(sizeof(mReceiveBuffer) + sizeof(mSendBuffer) - 1) / sizeof(mSendBuffer)];
142     uint32_t       mBenchmarkBytesTotal;
143     uint32_t       mBenchmarkLinksLeft;
144     TimeMilli      mBenchmarkStart;
145 };
146 
147 } // namespace Cli
148 } // namespace ot
149 
150 #endif // CLI_TCP_EXAMPLE_HPP_
151