Skip to content

Commit 345441e

Browse files
authored
Merge pull request #33 from lennartbecker-d/fix_linting_zenoh
Resolve clang-tidy warnings
2 parents 7ea2f51 + 27d243a commit 345441e

7 files changed

Lines changed: 185 additions & 84 deletions

File tree

.github/workflows/ci.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,83 @@ jobs:
6969
cd build/Release
7070
cmake --build . -- -j
7171
72+
- name: Upload compile commands
73+
uses: actions/upload-artifact@v4
74+
with:
75+
name: compile-commands
76+
path: up-zenoh-example-cpp/build/Release/compile_commands.json
77+
78+
79+
- name: Save conan cache to archive
80+
shell: bash
81+
run: |
82+
conan cache save --file ./conan-cache.tgz '*'
83+
84+
- name: Upload conan cache for linting
85+
uses: actions/upload-artifact@v4
86+
with:
87+
name: conan-cache
88+
path: ./conan-cache.tgz
89+
90+
lint:
91+
name: Lint C++ sources
92+
runs-on: ubuntu-22.04
93+
needs: build
94+
permissions:
95+
contents: write
96+
pull-requests: read
97+
steps:
98+
- name: Fetch up-zenoh-example-cpp
99+
uses: actions/checkout@v4
100+
with:
101+
path: up-zenoh-example-cpp
102+
103+
- name: Get build commands
104+
uses: actions/download-artifact@v4
105+
with:
106+
name: compile-commands
107+
108+
- name: Install Conan
109+
id: conan
110+
uses: turtlebrowser/get-conan@main
111+
with:
112+
version: 2.3.2
113+
114+
- name: Install conan CI profile
115+
shell: bash
116+
run: |
117+
conan profile detect
118+
cp up-zenoh-example-cpp/.github/workflows/ci_conan_profile "$(conan profile path default)"
119+
conan profile show
120+
121+
- name: Get conan cache
122+
uses: actions/download-artifact@v4
123+
with:
124+
name: conan-cache
125+
126+
- name: Restore conan cache from archive
127+
shell: bash
128+
run: |
129+
conan cache restore conan-cache.tgz
130+
131+
- name: Run linters on source
132+
id: source-linter
133+
uses: cpp-linter/cpp-linter-action@v2
134+
env:
135+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
with:
137+
repo-root: up-zenoh-example-cpp
138+
style: 'file' # read .clang-format for configuration
139+
tidy-checks: '' # Read .clang-tidy for configuration
140+
database: compile_commands.json
141+
version: 13
142+
143+
- name: Report lint failure
144+
if:
145+
steps.source-linter.outputs.checks-failed > 0
146+
run: |
147+
exit 1
148+
72149
73150
# NOTE: In GitHub repository settings, the "Require status checks to pass
74151
# before merging" branch protection rule ensures that commits are only merged

.github/workflows/ci_conan_profile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[settings]
2+
arch=x86_64
3+
build_type=Release
4+
compiler=gcc
5+
compiler.cppstd=gnu17
6+
compiler.libcxx=libstdc++11
7+
compiler.version=11
8+
os=Linux

pubsub/src/main_pub.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,39 @@
1818
#include <chrono>
1919
#include <csignal>
2020
#include <iostream>
21+
#include <random>
2122

2223
#include "common.h"
2324

24-
using namespace uprotocol::datamodel::builder;
25-
using namespace uprotocol::communication;
26-
using namespace uprotocol::v1;
27-
25+
using Payload = uprotocol::datamodel::builder::Payload;
26+
using Publisher = uprotocol::communication::Publisher;
27+
using UPayloadFormat = uprotocol::v1::UPayloadFormat;
28+
using UCode = uprotocol::v1::UCode;
2829
using ZenohUTransport = uprotocol::transport::ZenohUTransport;
2930

30-
bool gTerminate = false;
31+
bool g_terminate = false;
3132

3233
void signalHandler(int signal) {
3334
if (signal == SIGINT) {
3435
std::cout << "Ctrl+C received. Exiting..." << std::endl;
35-
gTerminate = true;
36+
g_terminate = true;
3637
}
3738
}
3839

3940
int64_t getTime() {
40-
auto currentTime = std::chrono::system_clock::now();
41-
auto duration = currentTime.time_since_epoch();
42-
int64_t timeMilli =
41+
auto current_time = std::chrono::system_clock::now();
42+
auto duration = current_time.time_since_epoch();
43+
int64_t time_milli =
4344
std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
4445

45-
return timeMilli;
46+
return time_milli;
4647
}
4748

4849
int32_t getRandom() {
49-
int32_t val = std::rand();
50-
return val;
50+
std::random_device rd;
51+
std::mt19937 gen(rd());
52+
std::uniform_int_distribution<int32_t> distribution(0, INT32_MAX);
53+
return distribution(gen);
5154
}
5255

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

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

72-
signal(SIGINT, signalHandler);
73-
signal(SIGPIPE, signalHandler);
74+
(void)signal(SIGINT, signalHandler);
75+
(void)signal(SIGPIPE, signalHandler);
7476

75-
UStatus status;
77+
uprotocol::v1::UStatus status;
7678

7779
auto source = getUUri(0);
7880
auto topic_time = getTimeUUri();
7981
auto topic_random = getRandomUUri();
8082
auto topic_counter = getCounterUUri();
81-
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
83+
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));
8284
Publisher publish_time(transport, std::move(topic_time),
8385
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
8486
Publisher publish_random(transport, std::move(topic_random),
8587
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
8688
Publisher publish_counter(transport, std::move(topic_counter),
8789
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
8890

89-
while (!gTerminate) {
91+
while (!g_terminate) {
9092
// send a string with a time value (ie "15665489")
9193
uint64_t time_val = getTime();
9294
spdlog::info("sending time = {}", time_val);

pubsub/src/main_sub.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@
1313
#include <unistd.h>
1414
#include <up-cpp/communication/Subscriber.h>
1515
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
16+
#include <uprotocol/v1/umessage.pb.h>
1617

1718
#include <csignal>
1819
#include <iostream>
1920

2021
#include "common.h"
2122

22-
using namespace uprotocol::communication;
23-
using namespace uprotocol::v1;
23+
using Subscriber = uprotocol::communication::Subscriber;
24+
using UMessage = uprotocol::v1::UMessage;
25+
using UPayloadFormat = uprotocol::v1::UPayloadFormat;
2426

2527
using ZenohUTransport = uprotocol::transport::ZenohUTransport;
2628

27-
bool gTerminate = false;
29+
bool g_terminate = false;
2830

2931
void signalHandler(int signal) {
3032
if (signal == SIGINT) {
3133
std::cout << "Ctrl+C received. Exiting..." << std::endl;
32-
gTerminate = true;
34+
g_terminate = true;
3335
}
3436
}
3537

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

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

77-
signal(SIGINT, signalHandler);
78-
signal(SIGPIPE, signalHandler);
78+
(void)signal(SIGINT, signalHandler);
79+
(void)signal(SIGPIPE, signalHandler);
7980

80-
UStatus status;
81-
UUri source = getUUri(0);
82-
auto topic_time = getTimeUUri();
83-
auto topic_random = getRandomUUri();
84-
auto topic_counter = getCounterUUri();
85-
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
81+
uprotocol::v1::UStatus status;
82+
uprotocol::v1::UUri source = getUUri(0);
83+
const auto& topic_time = getTimeUUri();
84+
const auto& topic_random = getRandomUUri();
85+
const auto& topic_counter = getCounterUUri();
86+
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));
8687

87-
auto resTime =
88-
Subscriber::subscribe(transport, std::move(topic_time), onReceiveTime);
89-
auto resRandom = Subscriber::subscribe(transport, std::move(topic_random),
90-
onReceiveRandom);
91-
auto resCounter = Subscriber::subscribe(transport, std::move(topic_counter),
92-
onReceiveCounter);
88+
auto res_time = Subscriber::subscribe(transport, topic_time, onReceiveTime);
89+
auto res_random =
90+
Subscriber::subscribe(transport, topic_random, onReceiveRandom);
91+
auto res_counter =
92+
Subscriber::subscribe(transport, topic_counter, onReceiveCounter);
9393

94-
while (!gTerminate) {
94+
while (!g_terminate) {
9595
sleep(1);
9696
}
9797

rpc/include/common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
#define RPC_COMMON_H
1414

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

17-
uprotocol::v1::UUri getRpcUUri(const int resource_id) {
18+
inline uprotocol::v1::UUri getRpcUUri(const int resource_id) {
1819
uprotocol::v1::UUri uuri;
1920
uuri.set_authority_name("test_rpc.app");
20-
uuri.set_ue_id(0x10001);
21+
uuri.set_ue_id(RPC_UE_ID);
2122
uuri.set_ue_version_major(1);
2223
uuri.set_resource_id(resource_id);
2324
return uuri;

rpc/src/main_rpc_client.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,36 @@
1414
#include <unistd.h>
1515
#include <up-cpp/communication/RpcClient.h>
1616
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
17+
#include <uprotocol/v1/ustatus.pb.h>
1718

1819
#include <chrono>
1920
#include <csignal>
2021
#include <iostream>
2122

2223
#include "common.h"
2324

24-
using namespace uprotocol::v1;
25-
using namespace uprotocol::communication;
26-
using namespace uprotocol::datamodel::builder;
25+
constexpr uint32_t METHOD_RPC_RESOURCE_ID = 12;
26+
constexpr std::chrono::milliseconds RPCCLIENT_TTL(500);
27+
28+
using UMessage = uprotocol::v1::UMessage;
29+
using UStatus = uprotocol::v1::UStatus;
30+
using UPayloadFormat = uprotocol::v1::UPayloadFormat;
31+
using RpcClient = uprotocol::communication::RpcClient;
2732
using ZenohUTransport = uprotocol::transport::ZenohUTransport;
33+
using UUri = uprotocol::v1::UUri;
2834

29-
bool gTerminate = false;
35+
bool g_terminate = false;
3036

3137
void signalHandler(int signal) {
3238
if (signal == SIGINT) {
3339
std::cout << "Ctrl+C received. Exiting..." << std::endl;
34-
gTerminate = true;
40+
g_terminate = true;
3541
}
3642
}
3743

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

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

6977
/* The sample RPC client applications demonstrates how to send RPC requests and
7078
* wait for the response
7179
*/
72-
int main(int argc, char** argv) {
73-
(void)argc;
74-
(void)argv;
80+
int main(int argc, char* argv[]) {
81+
std::vector<std::string> args(argv, argv + argc);
7582

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

82-
signal(SIGINT, signalHandler);
89+
(void)signal(SIGINT, signalHandler);
8390

8491
UUri source = getRpcUUri(0);
85-
UUri method = getRpcUUri(12);
86-
auto transport = std::make_shared<ZenohUTransport>(source, argv[1]);
87-
auto client =
88-
RpcClient(transport, std::move(method), UPriority::UPRIORITY_CS4,
89-
std::chrono::milliseconds(500));
92+
UUri method = getRpcUUri(METHOD_RPC_RESOURCE_ID);
93+
auto transport = std::make_shared<ZenohUTransport>(source, args.at(1));
94+
auto client = RpcClient(transport, std::move(method),
95+
uprotocol::v1::UPriority::UPRIORITY_CS4,
96+
std::chrono::milliseconds(RPCCLIENT_TTL));
9097
RpcClient::InvokeHandle handle;
9198

92-
while (!gTerminate) {
99+
while (!g_terminate) {
93100
handle = client.invokeMethod(OnReceive);
94101
sleep(1);
95102
}

0 commit comments

Comments
 (0)