3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * This file implements a chip-echo-responder, for the
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
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>
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;
51 // Callback handler when a CHIP EchoRequest is received.
52 void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle payload)
54 printf("Echo Request, len=%u ... sending response.\n", payload->DataLength());
59 int main(int argc, char * argv[])
61 CHIP_ERROR err = CHIP_NO_ERROR;
62 chip::Optional<chip::Transport::PeerAddress> peer(chip::Transport::Type::kUndefined);
64 bool disableEcho = false;
66 chip::Transport::AdminPairingTable admins;
67 chip::Transport::AdminPairingInfo * adminInfo = nullptr;
69 const chip::Transport::AdminId gAdminId = 0;
73 printf("Too many arguments specified!\n");
74 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
77 if ((argc == 2) && (strcmp(argv[1], "--tcp") == 0))
82 if ((argc == 2) && (strcmp(argv[1], "--disable") == 0))
89 adminInfo = admins.AssignAdminId(gAdminId, chip::kTestDeviceNodeId);
90 VerifyOrExit(adminInfo != nullptr, err = CHIP_ERROR_NO_MEMORY);
94 err = gTCPManager.Init(
95 chip::Transport::TcpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
98 err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gTCPManager, &admins);
103 err = gUDPManager.Init(
104 chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
107 err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gUDPManager, &admins);
111 err = gExchangeManager.Init(&gSessionManager);
116 err = gEchoServer.Init(&gExchangeManager);
120 err = gSessionManager.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing,
121 chip::SecureSessionMgr::PairingDirection::kResponder, gAdminId);
126 // Arrange to get a callback whenever an Echo Request is received.
127 gEchoServer.SetEchoRequestReceived(HandleEchoRequestReceived);
130 printf("Listening for Echo requests...\n");
132 chip::DeviceLayer::PlatformMgr().RunEventLoop();
135 if (err != CHIP_NO_ERROR)
137 printf("EchoServer failed, err:%s\n", chip::ErrorStr(err));
143 gEchoServer.Shutdown();