Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / messaging / tests / echo / echo_responder.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *      This file implements a chip-echo-responder, for the
21  *      CHIP Echo Protocol.
22  *
23  *      The CHIP Echo Protocol implements two simple methods, in the
24  *      style of ICMP ECHO REQUEST and ECHO REPLY, in which a sent
25  *      payload is turned around by the responder and echoed back to
26  *      the originator.
27  *
28  */
29
30 #include "common.h"
31
32 #include <core/CHIPCore.h>
33 #include <platform/CHIPDeviceLayer.h>
34 #include <protocols/echo/Echo.h>
35 #include <support/ErrorStr.h>
36 #include <system/SystemPacketBuffer.h>
37 #include <transport/PASESession.h>
38 #include <transport/SecureSessionMgr.h>
39 #include <transport/raw/TCP.h>
40 #include <transport/raw/UDP.h>
41
42 namespace {
43
44 // The EchoServer object.
45 chip::Protocols::Echo::EchoServer gEchoServer;
46 chip::TransportMgr<chip::Transport::UDP> gUDPManager;
47 chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>> gTCPManager;
48 chip::SecureSessionMgr gSessionManager;
49 chip::SecurePairingUsingTestSecret gTestPairing;
50
51 // Callback handler when a CHIP EchoRequest is received.
52 void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle payload)
53 {
54     printf("Echo Request, len=%u ... sending response.\n", payload->DataLength());
55 }
56
57 } // namespace
58
59 int main(int argc, char * argv[])
60 {
61     CHIP_ERROR err = CHIP_NO_ERROR;
62     chip::Optional<chip::Transport::PeerAddress> peer(chip::Transport::Type::kUndefined);
63     bool useTCP      = false;
64     bool disableEcho = false;
65
66     chip::Transport::AdminPairingTable admins;
67     chip::Transport::AdminPairingInfo * adminInfo = nullptr;
68
69     const chip::Transport::AdminId gAdminId = 0;
70
71     if (argc > 2)
72     {
73         printf("Too many arguments specified!\n");
74         ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
75     }
76
77     if ((argc == 2) && (strcmp(argv[1], "--tcp") == 0))
78     {
79         useTCP = true;
80     }
81
82     if ((argc == 2) && (strcmp(argv[1], "--disable") == 0))
83     {
84         disableEcho = true;
85     }
86
87     InitializeChip();
88
89     adminInfo = admins.AssignAdminId(gAdminId, chip::kTestDeviceNodeId);
90     VerifyOrExit(adminInfo != nullptr, err = CHIP_ERROR_NO_MEMORY);
91
92     if (useTCP)
93     {
94         err = gTCPManager.Init(
95             chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
96         SuccessOrExit(err);
97
98         err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gTCPManager, &admins);
99         SuccessOrExit(err);
100     }
101     else
102     {
103         err = gUDPManager.Init(
104             chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
105         SuccessOrExit(err);
106
107         err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gUDPManager, &admins);
108         SuccessOrExit(err);
109     }
110
111     err = gExchangeManager.Init(&gSessionManager);
112     SuccessOrExit(err);
113
114     if (!disableEcho)
115     {
116         err = gEchoServer.Init(&gExchangeManager);
117         SuccessOrExit(err);
118     }
119
120     err = gSessionManager.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing,
121                                      chip::SecureSessionMgr::PairingDirection::kResponder, gAdminId);
122     SuccessOrExit(err);
123
124     if (!disableEcho)
125     {
126         // Arrange to get a callback whenever an Echo Request is received.
127         gEchoServer.SetEchoRequestReceived(HandleEchoRequestReceived);
128     }
129
130     printf("Listening for Echo requests...\n");
131
132     chip::DeviceLayer::PlatformMgr().RunEventLoop();
133
134 exit:
135     if (err != CHIP_NO_ERROR)
136     {
137         printf("EchoServer failed, err:%s\n", chip::ErrorStr(err));
138         exit(EXIT_FAILURE);
139     }
140
141     if (!disableEcho)
142     {
143         gEchoServer.Shutdown();
144     }
145
146     ShutdownChip();
147
148     return EXIT_SUCCESS;
149 }