Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,83 @@ jobs:
cd build/Release
cmake --build . -- -j

- name: Upload compile commands
uses: actions/upload-artifact@v4
with:
name: compile-commands
path: up-zenoh-example-cpp/build/Release/compile_commands.json


- name: Save conan cache to archive
shell: bash
run: |
conan cache save --file ./conan-cache.tgz '*'

- name: Upload conan cache for linting
uses: actions/upload-artifact@v4
with:
name: conan-cache
path: ./conan-cache.tgz

lint:
name: Lint C++ sources
runs-on: ubuntu-22.04
needs: build
permissions:
contents: write
pull-requests: read
steps:
- name: Fetch up-zenoh-example-cpp
uses: actions/checkout@v4
with:
path: up-zenoh-example-cpp

- name: Get build commands
uses: actions/download-artifact@v4
with:
name: compile-commands

- name: Install Conan
id: conan
uses: turtlebrowser/get-conan@main
with:
version: 2.3.2

- name: Install conan CI profile
shell: bash
run: |
conan profile detect
cp up-zenoh-example-cpp/.github/workflows/ci_conan_profile "$(conan profile path default)"
conan profile show

- name: Get conan cache
uses: actions/download-artifact@v4
with:
name: conan-cache

- name: Restore conan cache from archive
shell: bash
run: |
conan cache restore conan-cache.tgz

- name: Run linters on source
id: source-linter
uses: cpp-linter/cpp-linter-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
repo-root: up-zenoh-example-cpp
style: 'file' # read .clang-format for configuration
tidy-checks: '' # Read .clang-tidy for configuration
database: compile_commands.json
version: 13

- name: Report lint failure
if:
steps.source-linter.outputs.checks-failed > 0
run: |
exit 1


# NOTE: In GitHub repository settings, the "Require status checks to pass
# before merging" branch protection rule ensures that commits are only merged
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/ci_conan_profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux
42 changes: 22 additions & 20 deletions pubsub/src/main_pub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,39 @@
#include <chrono>
#include <csignal>
#include <iostream>
#include <random>

#include "common.h"

using namespace uprotocol::datamodel::builder;
using namespace uprotocol::communication;
using namespace uprotocol::v1;

using Payload = uprotocol::datamodel::builder::Payload;
using Publisher = uprotocol::communication::Publisher;
using UPayloadFormat = uprotocol::v1::UPayloadFormat;
using UCode = uprotocol::v1::UCode;
using ZenohUTransport = uprotocol::transport::ZenohUTransport;

bool gTerminate = false;
bool g_terminate = false;

void signalHandler(int signal) {
if (signal == SIGINT) {
std::cout << "Ctrl+C received. Exiting..." << std::endl;
gTerminate = true;
g_terminate = true;
}
}

int64_t getTime() {
auto currentTime = std::chrono::system_clock::now();
auto duration = currentTime.time_since_epoch();
int64_t timeMilli =
auto current_time = std::chrono::system_clock::now();
auto duration = current_time.time_since_epoch();
int64_t time_milli =
std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();

return timeMilli;
return time_milli;
}

int32_t getRandom() {
int32_t val = std::rand();
return val;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int32_t> distribution(0, INT32_MAX);
return distribution(gen);
}

uint8_t getCounter() {
Expand All @@ -59,34 +62,33 @@ uint8_t getCounter() {
/* The sample pub applications demonstrates how to send data using uTransport -
* There are three topics that are published - random number, current time and a
* counter */
int main(int argc, char** argv) {
(void)argc;
(void)argv;
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

if (argc < 2) {
std::cout << "No Zenoh config has been provided" << std::endl;
std::cout << "Usage: pub <config_file>" << std::endl;
return 1;
}

signal(SIGINT, signalHandler);
signal(SIGPIPE, signalHandler);
(void)signal(SIGINT, signalHandler);
(void)signal(SIGPIPE, signalHandler);

UStatus status;
uprotocol::v1::UStatus status;

auto source = getUUri(0);
auto topic_time = getTimeUUri();
auto topic_random = getRandomUUri();
auto topic_counter = getCounterUUri();
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));
Publisher publish_time(transport, std::move(topic_time),
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
Publisher publish_random(transport, std::move(topic_random),
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
Publisher publish_counter(transport, std::move(topic_counter),
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);

while (!gTerminate) {
while (!g_terminate) {
// send a string with a time value (ie "15665489")
uint64_t time_val = getTime();
spdlog::info("sending time = {}", time_val);
Expand Down
44 changes: 22 additions & 22 deletions pubsub/src/main_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@
#include <unistd.h>
#include <up-cpp/communication/Subscriber.h>
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
#include <uprotocol/v1/umessage.pb.h>

#include <csignal>
#include <iostream>

#include "common.h"

using namespace uprotocol::communication;
using namespace uprotocol::v1;
using Subscriber = uprotocol::communication::Subscriber;
using UMessage = uprotocol::v1::UMessage;
using UPayloadFormat = uprotocol::v1::UPayloadFormat;

using ZenohUTransport = uprotocol::transport::ZenohUTransport;

bool gTerminate = false;
bool g_terminate = false;

void signalHandler(int signal) {
if (signal == SIGINT) {
std::cout << "Ctrl+C received. Exiting..." << std::endl;
gTerminate = true;
g_terminate = true;
}
}

Expand Down Expand Up @@ -64,34 +66,32 @@ void onReceiveCounter(const uprotocol::v1::UMessage& message) {
* -
* There are three topics that are received - random number, current time and a
* counter */
int main(int argc, char** argv) {
(void)argc;
(void)argv;
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

if (argc < 2) {
std::cout << "No Zenoh config has been provided" << std::endl;
std::cout << "Usage: sub <config_file>" << std::endl;
return 1;
}

signal(SIGINT, signalHandler);
signal(SIGPIPE, signalHandler);
(void)signal(SIGINT, signalHandler);
(void)signal(SIGPIPE, signalHandler);

UStatus status;
UUri source = getUUri(0);
auto topic_time = getTimeUUri();
auto topic_random = getRandomUUri();
auto topic_counter = getCounterUUri();
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
uprotocol::v1::UStatus status;
uprotocol::v1::UUri source = getUUri(0);
const auto& topic_time = getTimeUUri();
const auto& topic_random = getRandomUUri();
const auto& topic_counter = getCounterUUri();
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));

auto resTime =
Subscriber::subscribe(transport, std::move(topic_time), onReceiveTime);
auto resRandom = Subscriber::subscribe(transport, std::move(topic_random),
onReceiveRandom);
auto resCounter = Subscriber::subscribe(transport, std::move(topic_counter),
onReceiveCounter);
auto res_time = Subscriber::subscribe(transport, topic_time, onReceiveTime);
auto res_random =
Subscriber::subscribe(transport, topic_random, onReceiveRandom);
auto res_counter =
Subscriber::subscribe(transport, topic_counter, onReceiveCounter);

while (!gTerminate) {
while (!g_terminate) {
sleep(1);
}

Expand Down
5 changes: 3 additions & 2 deletions rpc/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
#define RPC_COMMON_H

#include <uprotocol/v1/uri.pb.h>
constexpr uint32_t RPC_UE_ID = 0x10001;

uprotocol::v1::UUri getRpcUUri(const int resource_id) {
inline uprotocol::v1::UUri getRpcUUri(const int resource_id) {
uprotocol::v1::UUri uuri;
uuri.set_authority_name("test_rpc.app");
uuri.set_ue_id(0x10001);
uuri.set_ue_id(RPC_UE_ID);
uuri.set_ue_version_major(1);
uuri.set_resource_id(resource_id);
return uuri;
Expand Down
41 changes: 24 additions & 17 deletions rpc/src/main_rpc_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,36 @@
#include <unistd.h>
#include <up-cpp/communication/RpcClient.h>
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
#include <uprotocol/v1/ustatus.pb.h>

#include <chrono>
#include <csignal>
#include <iostream>

#include "common.h"

using namespace uprotocol::v1;
using namespace uprotocol::communication;
using namespace uprotocol::datamodel::builder;
constexpr uint32_t METHOD_RPC_RESOURCE_ID = 12;
constexpr std::chrono::milliseconds RPCCLIENT_TTL(500);

using UMessage = uprotocol::v1::UMessage;
using UStatus = uprotocol::v1::UStatus;
using UPayloadFormat = uprotocol::v1::UPayloadFormat;
using RpcClient = uprotocol::communication::RpcClient;
using ZenohUTransport = uprotocol::transport::ZenohUTransport;
using UUri = uprotocol::v1::UUri;

bool gTerminate = false;
bool g_terminate = false;

void signalHandler(int signal) {
if (signal == SIGINT) {
std::cout << "Ctrl+C received. Exiting..." << std::endl;
gTerminate = true;
g_terminate = true;
}
}

void OnReceive(RpcClient::MessageOrStatus expected) {
if (!expected.has_value()) {
UStatus status = expected.error();
const UStatus& status = expected.error();
spdlog::error("Expected value not found. -- Status: {}",
status.DebugString());
return;
Expand All @@ -62,34 +68,35 @@ void OnReceive(RpcClient::MessageOrStatus expected) {
// sequence number, current time, and random value
spdlog::debug("(Client) Received message: {}", message.DebugString());

const uint64_t* pdata = (uint64_t*)message.payload().data();
const size_t num_bytes = message.payload().size();
std::vector<uint64_t> pdata(num_bytes / sizeof(uint64_t));
memcpy(pdata.data(), message.payload().data(), num_bytes);
spdlog::info("Received payload: {} - {}, {}", pdata[0], pdata[1], pdata[2]);
}

/* The sample RPC client applications demonstrates how to send RPC requests and
* wait for the response
*/
int main(int argc, char** argv) {
(void)argc;
(void)argv;
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

if (argc < 2) {
std::cout << "No Zenoh config has been provided" << std::endl;
std::cout << "Usage: rpc_client <config_file>" << std::endl;
return 1;
}

signal(SIGINT, signalHandler);
(void)signal(SIGINT, signalHandler);

UUri source = getRpcUUri(0);
UUri method = getRpcUUri(12);
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
auto client =
RpcClient(transport, std::move(method), UPriority::UPRIORITY_CS4,
std::chrono::milliseconds(500));
UUri method = getRpcUUri(METHOD_RPC_RESOURCE_ID);
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));
auto client = RpcClient(transport, std::move(method),
uprotocol::v1::UPriority::UPRIORITY_CS4,
std::chrono::milliseconds(RPCCLIENT_TTL));
RpcClient::InvokeHandle handle;

while (!gTerminate) {
while (!g_terminate) {
handle = client.invokeMethod(OnReceive);
sleep(1);
}
Expand Down
Loading