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