Skip to content

Commit

Permalink
Add echo handler in app server for secure channel testing (#5236)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored Mar 9, 2021
1 parent a0db449 commit a47019b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 7 deletions.
6 changes: 6 additions & 0 deletions examples/lighting-app/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Raspberry Pi Desktop 20.10 (aarch64)**
>
> gn gen out/debug --args='chip_bypass_rendezvous=false'
> If you want to test Echo protocol, please disable Rendezvous and enable Echo
> handler
>
> gn gen out/debug --args='chip_bypass_rendezvous=true chip_app_use_echo=true'
> ninja -C out/debug
- Prerequisites

1. A Raspberry Pi 4 board
Expand Down
3 changes: 2 additions & 1 deletion src/app/common_flags.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

declare_args() {
# Temporary flag for new interaction model engine, set it to true to enable
# Temporary flag for interaction model and echo protocols, set it to true to enable
chip_app_use_echo = false
chip_app_use_interaction_model = false
}
7 changes: 7 additions & 0 deletions src/app/server/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ config("server_config") {
defines += [ "CHIP_APP_USE_INTERACTION_MODEL" ]
}

if (chip_app_use_echo) {
defines += [ "CHIP_APP_USE_ECHO" ]
}

include_dirs = [ "." ]
}

static_library("server") {
output_name = "libCHIPAppServer"

sources = [
"EchoHandler.cpp",
"EchoHandler.h",
"Mdns.cpp",
"Mdns.h",
"QRCodeUtil.cpp",
Expand All @@ -46,6 +52,7 @@ static_library("server") {
"${chip_root}/src/lib/mdns",
"${chip_root}/src/messaging",
"${chip_root}/src/platform",
"${chip_root}/src/protocols",
"${chip_root}/src/setup_payload",
"${chip_root}/src/transport",
]
Expand Down
65 changes: 65 additions & 0 deletions src/app/server/EchoHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements the handler for echo messages.
*/

#include <app/server/EchoHandler.h>

#include <protocols/echo/Echo.h>
#include <support/ErrorStr.h>

namespace {

// The EchoServer object.
chip::Protocols::Echo::EchoServer gEchoServer;

/**
* Callback handler when a CHIP EchoRequest is received.
*
* @param [in] ec The exchange context holding the incoming message.
* @param [in] payload The buffer holding the message. This function guarantees
* that it will free the buffer before returning.
*
*/
void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle payload)
{
ChipLogProgress(AppServer, "Echo Request, len=%u ... sending response.\n", payload->DataLength());
}

} // namespace

CHIP_ERROR InitEchoHandler(chip::Messaging::ExchangeManager * exchangeMgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;

err = gEchoServer.Init(exchangeMgr);
SuccessOrExit(err);

// Arrange to get a callback whenever an Echo Request is received.
gEchoServer.SetEchoRequestReceived(HandleEchoRequestReceived);

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "EchoServer failed, err:%s\n", chip::ErrorStr(err));
}

return err;
}
33 changes: 33 additions & 0 deletions src/app/server/EchoHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file defines the API for the handler for echo messages.
*/

#pragma once

#include <messaging/ExchangeMgr.h>
#include <system/SystemPacketBuffer.h>

/**
* Register an unsolicited message handler for Echo protocol to be ready to process
* Echo messages.
*
*/
CHIP_ERROR InitEchoHandler(chip::Messaging::ExchangeManager * exchangeMgr);
21 changes: 15 additions & 6 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <app/InteractionModelEngine.h>
#include <app/server/DataModelHandler.h>
#include <app/server/EchoHandler.h>
#include <app/server/RendezvousServer.h>
#include <app/server/SessionManager.h>

Expand Down Expand Up @@ -410,8 +411,8 @@ class ServerCallback : public SecureSessionMgrDelegate
AppDelegate * mDelegate = nullptr;
};

#ifdef CHIP_APP_USE_INTERACTION_MODEL
Messaging::ExchangeManager gExchange;
#if defined(CHIP_APP_USE_INTERACTION_MODEL) || defined(CHIP_APP_USE_ECHO)
Messaging::ExchangeManager gExchangeMgr;
#endif
ServerCallback gCallbacks;
SecurePairingUsingTestSecret gTestPairing;
Expand Down Expand Up @@ -487,15 +488,23 @@ void InitServer(AppDelegate * delegate)
err = gSessions.Init(chip::kTestDeviceNodeId, &DeviceLayer::SystemLayer, &gTransports, &gAdminPairings);
SuccessOrExit(err);

#ifdef CHIP_APP_USE_INTERACTION_MODEL
err = gExchange.Init(&gSessions);
SuccessOrExit(err);
err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchange);
#if defined(CHIP_APP_USE_INTERACTION_MODEL) || defined(CHIP_APP_USE_ECHO)
err = gExchangeMgr.Init(&gSessions);
SuccessOrExit(err);
#else
gSessions.SetDelegate(&gCallbacks);
#endif

#if defined(CHIP_APP_USE_INTERACTION_MODEL)
err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchangeMgr);
SuccessOrExit(err);
#endif

#if defined(CHIP_APP_USE_ECHO)
err = InitEchoHandler(&gExchangeMgr);
SuccessOrExit(err);
#endif

if (useTestPairing())
{
AdminPairingInfo * adminInfo = gAdminPairings.AssignAdminId(gNextAvailableAdminId);
Expand Down

0 comments on commit a47019b

Please sign in to comment.