forked from cfanatic/vsomeip-fuzzing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.cpp
103 lines (90 loc) · 3.07 KB
/
response.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <csignal>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vsomeip/vsomeip.hpp>
#include <vsomeip/internal/logger.hpp>
#define SERVICE_ID 0x1234
#define SERVICE_METHOD_ID 0x0421
#define SERVICE_INSTANCE_ID 0x5678
class Response
{
public:
Response() : app_(vsomeip::runtime::get()->create_application("!!SERVICE!!"))
{
}
void init()
{
app_->init();
app_->register_state_handler(std::bind(&Response::on_state_cbk,
this,
std::placeholders::_1));
app_->register_message_handler(Response::service_id__,
Response::service_instance_id__,
Response::service_method_id__,
std::bind(&Response::on_message_cbk,
this,
std::placeholders::_1));
}
void start()
{
app_->start();
}
void stop()
{
app_->stop();
}
void on_state_cbk(vsomeip::state_type_e _state)
{
if (_state == vsomeip::state_type_e::ST_REGISTERED)
{
app_->offer_service(Response::service_id__, Response::service_instance_id__);
}
}
void on_message_cbk(const std::shared_ptr<vsomeip::message> &_request)
{
std::string str_payload;
str_payload.append(reinterpret_cast<const char *>(_request->get_payload()->get_data()), 0, _request->get_payload()->get_length());
VSOMEIP_INFO << "--RESPONSE-- Received request with Client/Session ["
<< std::setw(4) << std::setfill('0') << std::hex << _request->get_client() << "/"
<< std::setw(4) << std::setfill('0') << std::hex << _request->get_session() << "] "
<< str_payload;
std::shared_ptr<vsomeip::message> its_response = vsomeip::runtime::get()->create_response(_request);
std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()->create_payload();
std::string str;
if (str_payload != "ping")
{
str = "Hello Client!";
}
else
{
str = "pong";
}
std::vector<vsomeip::byte_t> its_payload_data(std::begin(str), std::end(str));
its_payload->set_data(its_payload_data);
its_response->set_payload(its_payload);
app_->send(its_response);
}
private:
std::shared_ptr<vsomeip::application> app_;
static const vsomeip::service_t service_id__ = SERVICE_ID;
static const vsomeip::method_t service_method_id__ = SERVICE_METHOD_ID;
static const vsomeip::instance_t service_instance_id__ = SERVICE_INSTANCE_ID;
};
Response *res_ptr(nullptr);
void terminate(int _signal)
{
if (res_ptr != nullptr && (_signal == SIGINT || _signal == SIGTERM))
{
res_ptr->stop();
}
}
int main()
{
Response res;
res_ptr = &res;
signal(SIGINT, terminate);
signal(SIGTERM, terminate);
res.init();
res.start();
}