summaryrefslogtreecommitdiff
path: root/examples/subscribe-sample.cpp
diff options
context:
space:
mode:
authorLutz Bichler <Lutz.Bichler@bmw.de>2014-08-05 14:16:27 +0200
committerLutz Bichler <Lutz.Bichler@bmw.de>2014-08-05 14:16:27 +0200
commite339029b33ad8415469794691d70016cac6701b0 (patch)
treeb12d6c67c58865e19bf7887c4ffafbeb3ba471af /examples/subscribe-sample.cpp
parentd3644b3edc8b15ace244724bc50e4fbfbf9601ec (diff)
downloadvSomeIP-e339029b33ad8415469794691d70016cac6701b0.tar.gz
Implemented get/set of fields. Only currently this works for remote
events/fields. TODO: enable the mechanism for local events/fields. The simple approach (sending locally) would require serialization/deserialization which I'd like to avoid...
Diffstat (limited to 'examples/subscribe-sample.cpp')
-rw-r--r--examples/subscribe-sample.cpp151
1 files changed, 83 insertions, 68 deletions
diff --git a/examples/subscribe-sample.cpp b/examples/subscribe-sample.cpp
index bb1beaf..2c6312b 100644
--- a/examples/subscribe-sample.cpp
+++ b/examples/subscribe-sample.cpp
@@ -16,77 +16,92 @@
#include "sample-ids.hpp"
class client_sample {
- public:
- client_sample(bool _use_tcp)
- : app_(vsomeip::runtime::get()->create_application()),
- use_tcp_(_use_tcp) {
- }
-
- void init() {
- app_->init();
-
- VSOMEIP_INFO<< "Client settings [protocol="
- << (use_tcp_ ? "TCP" : "UDP")
- << "]";
-
- app_->register_message_handler(
- vsomeip::ANY_SERVICE, SAMPLE_INSTANCE_ID, vsomeip::ANY_METHOD,
- std::bind(&client_sample::on_message, this, std::placeholders::_1));
-
- app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID,
- SAMPLE_EVENTGROUP_ID);
- }
-
- void start() {
- app_->start();
- }
-
- void on_message(std::shared_ptr<vsomeip::message> &_response) {
- std::stringstream its_message;
- its_message << "Received a notification from Service [" << std::setw(4)
- << std::setfill('0') << std::hex << _response->get_service()
- << "." << std::setw(4) << std::setfill('0') << std::hex
- << _response->get_instance() << "] to Client/Session ["
- << std::setw(4) << std::setfill('0') << std::hex
- << _response->get_client() << "/" << std::setw(4)
- << std::setfill('0') << std::hex << _response->get_session()
- << "] = ";
- std::shared_ptr<vsomeip::payload> its_payload = _response->get_payload();
- its_message << "(" << std::dec << its_payload->get_length() << ")";
-#if 0
- for (uint32_t i = 0; i < its_payload->get_length(); ++i)
- its_message << std::hex << std::setw(2) << std::setfill('0')
- << (int) its_payload->get_data()[i] << " ";
-#endif
- VSOMEIP_INFO << its_message.str();
- }
+public:
+ client_sample(bool _use_tcp) :
+ app_(vsomeip::runtime::get()->create_application()), use_tcp_(
+ _use_tcp) {
+ }
+
+ void init() {
+ app_->init();
+
+ VSOMEIP_INFO<< "Client settings [protocol="
+ << (use_tcp_ ? "TCP" : "UDP")
+ << "]";
+
+ app_->register_message_handler(vsomeip::ANY_SERVICE, SAMPLE_INSTANCE_ID,
+ vsomeip::ANY_METHOD,
+ std::bind(&client_sample::on_message, this,
+ std::placeholders::_1));
+
+ app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID,
+ SAMPLE_EVENTGROUP_ID);
+ }
+
+ void start() {
+ app_->start();
+ }
+
+ void on_message(std::shared_ptr<vsomeip::message> &_response) {
+ std::stringstream its_message;
+ its_message << "Received a notification for Event [" << std::setw(4)
+ << std::setfill('0') << std::hex << _response->get_service()
+ << "." << _response->get_instance() << "."
+ << _response->get_method() << "] to Client/Session ["
+ << std::setw(4) << std::setfill('0') << std::hex
+ << _response->get_client() << "/" << std::setw(4)
+ << std::setfill('0') << std::hex << _response->get_session()
+ << "] = ";
+ std::shared_ptr<vsomeip::payload> its_payload =
+ _response->get_payload();
+ its_message << "(" << std::dec << its_payload->get_length() << ") ";
+ for (uint32_t i = 0; i < its_payload->get_length(); ++i)
+ its_message << std::hex << std::setw(2) << std::setfill('0')
+ << (int) its_payload->get_data()[i] << " ";
+ VSOMEIP_INFO << its_message.str();
+
+ if (_response->get_client() == 0) {
+ if ((its_payload->get_length() % 5) == 0)
+ app_->get(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID);
+
+ if ((its_payload->get_length() % 8) == 0) {
+ const vsomeip::byte_t its_data[]
+ = { 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ 0x48, 0x49, 0x50, 0x51, 0x52 };
+ std::shared_ptr<vsomeip::payload> its_new_payload
+ = vsomeip::runtime::get()->create_payload();
+ its_new_payload->set_data(its_data, sizeof(its_data));
+ app_->set(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID, its_new_payload);
+ }
+ }
+ }
private:
- std::shared_ptr< vsomeip::application > app_;
- bool use_tcp_;
- bool be_quiet_;
+ std::shared_ptr< vsomeip::application > app_;
+ bool use_tcp_;
+ bool be_quiet_;
};
int main(int argc, char **argv) {
- bool use_tcp = false;
- bool be_quiet = false;
- uint32_t cycle = 1000; // Default: 1s
-
- std::string tcp_enable("--tcp");
- std::string udp_enable("--udp");
-
- int i = 1;
- while (i < argc) {
- if (tcp_enable == argv[i]) {
- use_tcp = true;
- } else if (udp_enable == argv[i]) {
- use_tcp = false;
- }
- i++;
- }
-
- client_sample its_sample(use_tcp);
- its_sample.init();
- its_sample.start();
- return 0;
+ bool use_tcp = false;
+ bool be_quiet = false;
+ uint32_t cycle = 1000; // Default: 1s
+
+ std::string tcp_enable("--tcp");
+ std::string udp_enable("--udp");
+
+ int i = 1;
+ while (i < argc) {
+ if (tcp_enable == argv[i]) {
+ use_tcp = true;
+ } else if (udp_enable == argv[i]) {
+ use_tcp = false;
+ }
+ i++;
+ }
+
+ client_sample its_sample(use_tcp);
+ its_sample.init();
+ its_sample.start();
+ return 0;
}