Skip to content

Commit

Permalink
Implemented notification events (tested over IP / untested internally).
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzbichler committed Aug 4, 2014
1 parent deebe9f commit d3644b3
Show file tree
Hide file tree
Showing 36 changed files with 1,113 additions and 703 deletions.
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(DL_LIBRARY "")
set(EXPORTSYMBOLS "")
set(NO_DEPRECATED "-Wno-deprecated")
set(OPTIMIZE "-Os")
set(OPTIMIZE "")
endif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")

# Compiler settings
Expand Down Expand Up @@ -83,11 +83,17 @@ add_library(vsomeip-sd SHARED ${vsomeip-sd_SRC})
target_link_libraries(vsomeip-sd vsomeip ${Boost_LIBRARIES} rt ${DL_LIBRARY})

# Examples
add_executable(client-sample examples/client-sample.cpp)
target_link_libraries(client-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})
add_executable(request-sample examples/request-sample.cpp)
target_link_libraries(request-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})

add_executable(service-sample examples/service-sample.cpp)
target_link_libraries(service-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})
add_executable(response-sample examples/response-sample.cpp)
target_link_libraries(response-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})

add_executable(subscribe-sample examples/subscribe-sample.cpp)
target_link_libraries(subscribe-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})

add_executable(notify-sample examples/notify-sample.cpp)
target_link_libraries(notify-sample vsomeip ${Boost_LIBRARIES} ${DL_LIBRARY})

# Test
add_executable(configuration-test test/configuration-test.cpp)
Expand Down
10 changes: 5 additions & 5 deletions config/vsomeip-udp-client.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"unicast" : "192.168.56.104",
"unicast" : "192.168.56.101",
"netmask" : "255.255.255.0",
"logging" :
{
"level" : "trace",
"level" : "debug",
"console" : "true",
"file" : { "enable" : "true", "path" : "/var/log/vsomeip.log" },
"dlt" : "true"
Expand Down Expand Up @@ -31,7 +31,7 @@
[
{
"name" : "remote",
"unicast" : "192.168.56.101",
"unicast" : "192.168.56.104",
"services" :
[
{
Expand Down Expand Up @@ -62,12 +62,12 @@
[
{
"eventgroup" : "0x4455",
"events" : [ "0x777", "0x778" ]
"events" : [ "0x777", "0x778" ]
},
{
"eventgroup" : "0x4465",
"events" : [ "0x778", "0x779" ],
"is_multicast" : "true"
"is_multicast" : "true"
},
{
"eventgroup" : "0x4555",
Expand Down
10 changes: 5 additions & 5 deletions config/vsomeip-udp-service.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"unicast" : "192.168.56.101",
"unicast" : "192.168.56.104",
"logging" :
{
"level" : "trace",
"level" : "debug",
"console" : "true",
"file" : { "enable" : "false", "path" : "/tmp/vsomeip.log" },
"dlt" : "false"
Expand Down Expand Up @@ -63,12 +63,12 @@
[
{
"eventgroup" : "0x4455",
"events" : [ "0x777", "0x778" ]
"events" : [ "0x777", "0x778" ]
},
{
"eventgroup" : "0x4465",
"events" : [ "0x778", "0x779" ],
"is_multicast" : "true"
"events" : [ "0x778", "0x779" ],
"is_multicast" : "true"
},
{
"eventgroup" : "0x4555",
Expand Down
168 changes: 168 additions & 0 deletions examples/notify-sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright (C) 2014 BMW Group
// Author: Lutz Bichler (lutz.bichler@bmw.de)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <chrono>
#include <condition_variable>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>

#include <vsomeip/vsomeip.hpp>

#include "sample-ids.hpp"

class service_sample {
public:
service_sample(bool _use_tcp, uint32_t _cycle)
: app_(vsomeip::runtime::get()->create_application()),
is_registered_(false),
use_tcp_(_use_tcp),
cycle_(_cycle),
offer_thread_(std::bind(&service_sample::run, this)),
notify_thread_(std::bind(&service_sample::notify, this)),
is_offered_(false) {
}

void init() {
std::lock_guard < std::mutex > its_lock(mutex_);

app_->init();
app_->register_event_handler(
std::bind(&service_sample::on_event, this, std::placeholders::_1));

blocked_ = true;
condition_.notify_one();
}

void start() {
app_->start();
}

void offer() {
std::lock_guard < std::mutex > its_lock(notify_mutex_);
app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
is_offered_ = true;
notify_condition_.notify_one();
}

void stop_offer() {
app_->stop_offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
is_offered_ = false;
}

void on_event(vsomeip::event_type_e _event) {
VSOMEIP_INFO << "Application " << app_->get_name() << " is "
<< (_event == vsomeip::event_type_e::REGISTERED ?
"registered." : "deregistered.");

if (_event == vsomeip::event_type_e::REGISTERED) {
if (!is_registered_) {
is_registered_ = true;
}
} else {
is_registered_ = false;
}
}

void run() {
std::unique_lock < std::mutex > its_lock(mutex_);
while (!blocked_)
condition_.wait(its_lock);

bool is_offer(true);
while (true) {
if (is_offer)
offer();
else
stop_offer();
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
is_offer = !is_offer;
}
}

void notify() {
std::shared_ptr<vsomeip::payload> its_payload
= vsomeip::runtime::get()->create_payload();

vsomeip::byte_t its_data[256];
uint32_t its_size = 1;

while (true) {
VSOMEIP_INFO << "notify: started...";
std::unique_lock < std::mutex > its_lock(notify_mutex_);
while (!is_offered_)
notify_condition_.wait(its_lock);

VSOMEIP_INFO << "notify: unblocked ...";
while (is_offered_) {
if (its_size == sizeof(its_data))
its_size = 1;

for (uint32_t i = 0; i < its_size; ++i)
its_data[i] = (i % 256);

its_payload->set_data(its_data, its_size);

VSOMEIP_INFO << "Setting event (Length=" << std::dec << its_size << ").";
app_->set(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID, its_payload);

its_size++;

std::this_thread::sleep_for(std::chrono::milliseconds(cycle_));
}
}
}

private:
std::shared_ptr<vsomeip::application> app_;
bool is_registered_;
bool use_tcp_;
uint32_t cycle_;

std::thread offer_thread_;
std::mutex mutex_;
std::condition_variable condition_;
bool blocked_;

std::thread notify_thread_;
std::mutex notify_mutex_;
std::condition_variable notify_condition_;
bool is_offered_;
};

int main(int argc, char **argv) {
bool use_tcp = false;
uint32_t cycle = 1000; // default 1s

std::string tcp_enable("--tcp");
std::string udp_enable("--udp");
std::string cycle_arg("--cycle");

for (int i = 1; i < argc; i++) {
if (tcp_enable == argv[i]) {
use_tcp = true;
break;
}
if (udp_enable == argv[i]) {
use_tcp = false;
break;
}

if (cycle_arg == argv[i] && i+1 < argc) {
i++;
std::stringstream converter;
converter << argv[i];
converter >> cycle;
}
}

service_sample its_sample(use_tcp, cycle);
its_sample.init();
its_sample.start();

return 0;
}
4 changes: 0 additions & 4 deletions examples/request-sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ class client_sample {
this,
std::placeholders::_1));

app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENTGROUP_ID,
vsomeip::ANY_MAJOR, vsomeip::ANY_TTL);

request_->set_service(SAMPLE_SERVICE_ID);
request_->set_instance(SAMPLE_INSTANCE_ID);
request_->set_method(SAMPLE_METHOD_ID);
Expand All @@ -75,7 +72,6 @@ class client_sample {

void on_event(vsomeip::event_type_e _event) {
if (_event == vsomeip::event_type_e::REGISTERED) {
//app_->offer_service(OTHER_SAMPLE_SERVICE_ID, OTHER_SAMPLE_INSTANCE_ID);
app_->request_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
}
}
Expand Down
47 changes: 2 additions & 45 deletions examples/response-sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class service_sample {
: app_(vsomeip::runtime::get()->create_application()),
is_registered_(false),
use_tcp_(_use_tcp),
offer_thread_(std::bind(&service_sample::run, this)),
notification_thread_(std::bind(&service_sample::notify, this)) {
offer_thread_(std::bind(&service_sample::run, this)) {
}

void init() {
Expand All @@ -45,36 +44,11 @@ class service_sample {
}

void offer() {
VSOMEIP_INFO << "Offering service.";
//std::lock_guard < std::mutex > its_lock(notification_mutex_);
app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);

notification_blocked_ = true;
//condition_.notify_one();
}

void stop_offer() {
VSOMEIP_INFO << "Stop offering service.";
app_->stop_offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
notification_blocked_ = false;
}

void update() {
static std::shared_ptr<vsomeip::payload> its_payload =
vsomeip::runtime::get()->create_payload();

static vsomeip::byte_t its_data[1000];
static uint32_t its_size = 0;

its_size++;
for (uint32_t i = 0; i < its_size; ++i)
its_data[i] = (i % 256);

its_payload->set_data(its_data, its_size);

VSOMEIP_INFO << "Updating event to " << its_size << " bytes.";
app_->set(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID,
its_payload);
}

void on_event(vsomeip::event_type_e _event) {
Expand Down Expand Up @@ -127,19 +101,7 @@ class service_sample {
}
}

void notify() {
while (true) {
//std::unique_lock < std::mutex > its_lock(notification_mutex_);
//while (!notification_blocked_)
// notification_condition_.wait(its_lock);
//while (notification_blocked_) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (notification_blocked_) update();
//}
}
}

private:
private:
std::shared_ptr<vsomeip::application> app_;
bool is_registered_;
bool use_tcp_;
Expand All @@ -148,11 +110,6 @@ class service_sample {
std::mutex mutex_;
std::condition_variable condition_;
bool blocked_;

std::thread notification_thread_;
std::mutex notification_mutex_;
std::condition_variable notification_condition_;
bool notification_blocked_;
};

int main(int argc, char **argv) {
Expand Down
Loading

0 comments on commit d3644b3

Please sign in to comment.