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 implements the OpenThread TCP API.
32 */
33
34 #include "openthread-core-config.h"
35
36 #if OPENTHREAD_CONFIG_TCP_ENABLE
37
38 #include "instance/instance.hpp"
39
40 using namespace ot;
41
otTcpEndpointInitialize(otInstance * aInstance,otTcpEndpoint * aEndpoint,const otTcpEndpointInitializeArgs * aArgs)42 otError otTcpEndpointInitialize(otInstance *aInstance,
43 otTcpEndpoint *aEndpoint,
44 const otTcpEndpointInitializeArgs *aArgs)
45 {
46 return AsCoreType(aEndpoint).Initialize(AsCoreType(aInstance), *aArgs);
47 }
48
otTcpEndpointGetInstance(otTcpEndpoint * aEndpoint)49 otInstance *otTcpEndpointGetInstance(otTcpEndpoint *aEndpoint) { return &AsCoreType(aEndpoint).GetInstance(); }
50
otTcpEndpointGetContext(otTcpEndpoint * aEndpoint)51 void *otTcpEndpointGetContext(otTcpEndpoint *aEndpoint) { return AsCoreType(aEndpoint).GetContext(); }
52
otTcpGetLocalAddress(const otTcpEndpoint * aEndpoint)53 const otSockAddr *otTcpGetLocalAddress(const otTcpEndpoint *aEndpoint)
54 {
55 return &AsCoreType(aEndpoint).GetLocalAddress();
56 }
57
otTcpGetPeerAddress(const otTcpEndpoint * aEndpoint)58 const otSockAddr *otTcpGetPeerAddress(const otTcpEndpoint *aEndpoint)
59 {
60 return &AsCoreType(aEndpoint).GetPeerAddress();
61 }
62
otTcpBind(otTcpEndpoint * aEndpoint,const otSockAddr * aSockName)63 otError otTcpBind(otTcpEndpoint *aEndpoint, const otSockAddr *aSockName)
64 {
65 return AsCoreType(aEndpoint).Bind(AsCoreType(aSockName));
66 }
67
otTcpConnect(otTcpEndpoint * aEndpoint,const otSockAddr * aSockName,uint32_t aFlags)68 otError otTcpConnect(otTcpEndpoint *aEndpoint, const otSockAddr *aSockName, uint32_t aFlags)
69 {
70 return AsCoreType(aEndpoint).Connect(AsCoreType(aSockName), aFlags);
71 }
72
otTcpSendByReference(otTcpEndpoint * aEndpoint,otLinkedBuffer * aBuffer,uint32_t aFlags)73 otError otTcpSendByReference(otTcpEndpoint *aEndpoint, otLinkedBuffer *aBuffer, uint32_t aFlags)
74 {
75 return AsCoreType(aEndpoint).SendByReference(*aBuffer, aFlags);
76 }
77
otTcpSendByExtension(otTcpEndpoint * aEndpoint,size_t aNumBytes,uint32_t aFlags)78 otError otTcpSendByExtension(otTcpEndpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)
79 {
80 return AsCoreType(aEndpoint).SendByExtension(aNumBytes, aFlags);
81 }
82
otTcpReceiveByReference(otTcpEndpoint * aEndpoint,const otLinkedBuffer ** aBuffer)83 otError otTcpReceiveByReference(otTcpEndpoint *aEndpoint, const otLinkedBuffer **aBuffer)
84 {
85 return AsCoreType(aEndpoint).ReceiveByReference(*aBuffer);
86 }
87
otTcpReceiveContiguify(otTcpEndpoint * aEndpoint)88 otError otTcpReceiveContiguify(otTcpEndpoint *aEndpoint) { return AsCoreType(aEndpoint).ReceiveContiguify(); }
89
otTcpCommitReceive(otTcpEndpoint * aEndpoint,size_t aNumBytes,uint32_t aFlags)90 otError otTcpCommitReceive(otTcpEndpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)
91 {
92 return AsCoreType(aEndpoint).CommitReceive(aNumBytes, aFlags);
93 }
94
otTcpSendEndOfStream(otTcpEndpoint * aEndpoint)95 otError otTcpSendEndOfStream(otTcpEndpoint *aEndpoint) { return AsCoreType(aEndpoint).SendEndOfStream(); }
96
otTcpAbort(otTcpEndpoint * aEndpoint)97 otError otTcpAbort(otTcpEndpoint *aEndpoint) { return AsCoreType(aEndpoint).Abort(); }
98
otTcpEndpointDeinitialize(otTcpEndpoint * aEndpoint)99 otError otTcpEndpointDeinitialize(otTcpEndpoint *aEndpoint) { return AsCoreType(aEndpoint).Deinitialize(); }
100
otTcpListenerInitialize(otInstance * aInstance,otTcpListener * aListener,const otTcpListenerInitializeArgs * aArgs)101 otError otTcpListenerInitialize(otInstance *aInstance,
102 otTcpListener *aListener,
103 const otTcpListenerInitializeArgs *aArgs)
104 {
105 return AsCoreType(aListener).Initialize(AsCoreType(aInstance), *aArgs);
106 }
107
otTcpListenerGetInstance(otTcpListener * aListener)108 otInstance *otTcpListenerGetInstance(otTcpListener *aListener) { return &AsCoreType(aListener).GetInstance(); }
109
otTcpListenerGetContext(otTcpListener * aListener)110 void *otTcpListenerGetContext(otTcpListener *aListener) { return AsCoreType(aListener).GetContext(); }
111
otTcpListen(otTcpListener * aListener,const otSockAddr * aSockName)112 otError otTcpListen(otTcpListener *aListener, const otSockAddr *aSockName)
113 {
114 return AsCoreType(aListener).Listen(AsCoreType(aSockName));
115 }
116
otTcpStopListening(otTcpListener * aListener)117 otError otTcpStopListening(otTcpListener *aListener) { return AsCoreType(aListener).StopListening(); }
118
otTcpListenerDeinitialize(otTcpListener * aListener)119 otError otTcpListenerDeinitialize(otTcpListener *aListener) { return AsCoreType(aListener).Deinitialize(); }
120
121 #endif // OPENTHREAD_CONFIG_TCP_ENABLE
122