Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add echo example apps to conduct end-to-end connectivity sanity check… #3836

Merged
merged 10 commits into from
Dec 8, 2020
23 changes: 23 additions & 0 deletions src/messaging/tests/echo/.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2020 Project CHIP Authors
yufengwangca marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.

# The location of the build configuration file.
buildconfig = "//build/config/BUILDCONFIG.gn"

# CHIP uses angle bracket includes.
check_system_includes = true

default_args = {
import("//args.gni")
}
72 changes: 72 additions & 0 deletions src/messaging/tests/echo/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (c) 2020 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.

import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/tools.gni")

assert(chip_build_tools)

executable("chip-echo-requester") {
sources = [
"common.cpp",
"echo_requester.cpp",
]

public_deps = [
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/platform",
"${chip_root}/src/protocols",
"${chip_root}/src/system",
]

if (is_debug) {
mspang marked this conversation as resolved.
Show resolved Hide resolved
defines = [ "BUILD_RELEASE=0" ]
} else {
defines = [ "BUILD_RELEASE=1" ]
}

output_dir = root_out_dir
}

executable("chip-echo-responder") {
sources = [
"common.cpp",
"echo_responder.cpp",
]

public_deps = [
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/platform",
"${chip_root}/src/protocols",
"${chip_root}/src/system",
]

if (is_debug) {
defines = [ "BUILD_RELEASE=0" ]
} else {
defines = [ "BUILD_RELEASE=1" ]
}

output_dir = root_out_dir
}

group("echo") {
deps = [
":chip-echo-requester",
":chip-echo-responder",
]
}
53 changes: 53 additions & 0 deletions src/messaging/tests/echo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# CHIP Example Application Tutorial

## Introduction

The CHIP Echo example application shows you how to implement a CHIP application
program using one of the supported CHIP protocols, namely the very simple CHIP
Echo protocol. This protocol allows you to send a CHIP message to a peer and
expect a CHIP response (similar to the ICMP Echo Request/Echo Response
messages).

CHIP Protocols are, essentially, implementations of specific protocols over the
CHIP transport. Furthermore, when two CHIP nodes are exchanging messages of a
particular CHIP protocol, they do so over a construct called a CHIP Exchange
which is a description of a CHIP-based conversation over a CHIP protocol. A CHIP
Exchange is characterised by the ExchangeContext object, and every CHIP node
must create an ExchangeContext object before initiating a CHIP conversation.

After constructing a CHIP ExchangeContext, CHIP messages are sent and received
using the ChipMessageLayer class which sends the CHIP message over a chosen
transport (TCP, UDP, or CRMP).

## Building

```
cd echo
git submodule update --init
source third_party/connectedhomeip/scripts/activate.sh
gn gen out/debug
ninja -C out/debug
```

- After the applications are built, it can be found in the build directory as
`out/debug/chip-echo-requester and out/debug/chip-echo-responder`

## Example Applications Walk Through

As part of this example, we have a ChipEchoRequester program that acts as the
client and sends echo requests to a ChipEchoResponder program that receives
EchoRequests and sends back EchoResponse messages.

### Ping a device over IP

To start the Server in echo mode, run the built executable.

$ ./chip-echo-responder

To start the Client in echo mode, run the built executable and pass it the IP
address of the server to talk to.

$ ./chip-echo-requester <Server's IPv4 address>

If valid values are supplied, it will begin to periodically send messages to the
server address provided for three times.
17 changes: 17 additions & 0 deletions src/messaging/tests/echo/args.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2020 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.

import("//build_overrides/chip.gni")

import("${chip_root}/config/standalone/args.gni")
1 change: 1 addition & 0 deletions src/messaging/tests/echo/build
1 change: 1 addition & 0 deletions src/messaging/tests/echo/build_overrides
97 changes: 97 additions & 0 deletions src/messaging/tests/echo/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
*
* Copyright (c) 2020 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 constants, globals and interfaces common
* to and used by CHIP example applications.
*
*/

#include <errno.h>

#include "common.h"
#include <support/ErrorStr.h>

// The ExchangeManager global object.
ExchangeManager gExchangeManager;

void InitializeChip(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;

printf("Init CHIP Stack\r\n");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the carriage return + line feed is in this function but not other places.


// Initialize System memory and resources
err = chip::Platform::MemoryInit();
SuccessOrExit(err);

// Initialize the CHIP stack.
err = chip::DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

exit:
if (err != CHIP_NO_ERROR)
{
printf("Failed to init CHIP Stack with err: %s\r\n", chip::ErrorStr(err));
exit(EXIT_FAILURE);
}
}

void ShutdownChip(void)
{
gExchangeManager.Shutdown();
chip::DeviceLayer::SystemLayer.Shutdown();
}

void DriveIO(void)
{
struct timeval sleepTime;
fd_set readFDs, writeFDs, exceptFDs;
int numFDs = 0;
int selectRes;

sleepTime.tv_sec = 0;
sleepTime.tv_usec = NETWORK_SLEEP_TIME_MSECS;

FD_ZERO(&readFDs);
FD_ZERO(&writeFDs);
FD_ZERO(&exceptFDs);

if (chip::DeviceLayer::SystemLayer.State() == chip::System::kLayerState_Initialized)
chip::DeviceLayer::SystemLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, sleepTime);

if (chip::DeviceLayer::InetLayer.State == chip::Inet::InetLayer::kState_Initialized)
chip::DeviceLayer::InetLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, sleepTime);

selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &sleepTime);
if (selectRes < 0)
{
printf("select failed: %s\n", chip::ErrorStr(chip::System::MapErrorPOSIX(errno)));
return;
}

if (chip::DeviceLayer::SystemLayer.State() == chip::System::kLayerState_Initialized)
{
chip::DeviceLayer::SystemLayer.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}

if (chip::DeviceLayer::InetLayer.State == chip::Inet::InetLayer::kState_Initialized)
{
chip::DeviceLayer::InetLayer.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}
}
49 changes: 49 additions & 0 deletions src/messaging/tests/echo/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright (c) 2020 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 some of the common constants, globals and interfaces
* that are common to and used by CHIP example applications.
*
*/

#pragma once

#include <core/CHIPCore.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this header doesn't appear to need all of these include directives, they should be moved to the source files that need 'em

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acked

#include <messaging/ExchangeMgr.h>
#include <platform/CHIPDeviceLayer.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/UDP.h>

using namespace chip;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use using in headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acked


#define MAX_MESSAGE_SOURCE_STR_LENGTH (100)
#define NETWORK_SLEEP_TIME_MSECS (100 * 1000)

constexpr chip::NodeId kClientDeviceId = 12344321;
constexpr chip::NodeId kServerDeviceId = 12344322;

extern ExchangeManager gExchangeManager;

void InitializeChip(void);
void ShutdownChip(void);
void DriveIO(void);
Loading