feat():initial version
This commit is contained in:
5
source/example/.vscode/settings.json
vendored
Normal file
5
source/example/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"*.tcc": "cpp"
|
||||
}
|
||||
}
|
||||
22
source/example/CMakeLists.txt
Normal file
22
source/example/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
cmake_minimum_required (VERSION 2.8.7)
|
||||
|
||||
project (example)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
include_directories(${BOOST_INCLUDE_DIR})
|
||||
include_directories(${VSOMEIP_INCLUDE_DIR})
|
||||
|
||||
link_directories(${BOOST_LIBRARY_DIR})
|
||||
link_directories(${VSOMEIP_LIBRARY_DIR})
|
||||
|
||||
# add_executable(someip_service ../someip_service.cpp)
|
||||
# target_link_libraries(someip_service vsomeip3 vsomeip3-cfg vsomeip3-sd pthread boost_filesystem boost_system)
|
||||
|
||||
# add_executable (someip_client ../someip_client.cpp)
|
||||
# target_link_libraries(someip_client vsomeip3 vsomeip3-cfg vsomeip3-sd pthread boost_filesystem boost_system)
|
||||
|
||||
add_executable(your_app ../your_app.cpp ../someip_server.cpp ../someip_client.cpp)
|
||||
target_link_libraries(your_app vsomeip3 vsomeip3-cfg vsomeip3-sd pthread boost_filesystem boost_system)
|
||||
85
source/example/someip_client.cpp
Normal file
85
source/example/someip_client.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
//#include "someip_client.hpp"
|
||||
#include "someip_client_cfg.hpp"
|
||||
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
client *my_client_ptr(nullptr);
|
||||
void client_handle_signal(int _signal) {
|
||||
if (my_client_ptr != nullptr &&
|
||||
(_signal == SIGINT || _signal == SIGTERM
|
||||
|| _signal == SIGKILL))
|
||||
my_client_ptr->stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
void my_someip_client_thread_func(void* arg) {
|
||||
bool use_tcp = false;
|
||||
bool be_quiet = false;
|
||||
uint32_t cycle = 1000; // Default: 1s
|
||||
|
||||
std::string tcp_enable("--tcp");
|
||||
std::string udp_enable("--udp");
|
||||
std::string quiet_enable("--quiet");
|
||||
std::string cycle_arg("--cycle");
|
||||
|
||||
/*
|
||||
int i = 1;
|
||||
while (i < argc) {
|
||||
if (tcp_enable == argv[i]) {
|
||||
use_tcp = true;
|
||||
} else if (udp_enable == argv[i]) {
|
||||
use_tcp = false;
|
||||
} else if (quiet_enable == argv[i]) {
|
||||
be_quiet = true;
|
||||
} else if (cycle_arg == argv[i] && i+1 < argc) {
|
||||
i++;
|
||||
std::stringstream converter;
|
||||
converter << argv[i];
|
||||
converter >> cycle;
|
||||
}
|
||||
i++;
|
||||
}*/
|
||||
|
||||
client my_client(use_tcp, be_quiet, cycle);
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
my_client_ptr = &my_client;
|
||||
signal(SIGINT, client_handle_signal);
|
||||
signal(SIGTERM, client_handle_signal);
|
||||
signal(SIGKILL, client_handle_signal);
|
||||
#endif
|
||||
|
||||
|
||||
if (my_client.init()) {
|
||||
|
||||
for (auto const &server : request_services){
|
||||
my_client.start_request_service(server._service, server._instance);
|
||||
}
|
||||
for (auto const &method : request_methods){
|
||||
my_client.start_request_method(method._service, method._instance, method._method,
|
||||
method._request_method_func, method._message_type, method._massage_handle_func);
|
||||
}
|
||||
for (auto const &event : request_events){
|
||||
my_client.start_request_event(event._service, event._instance, event._eventgroup,
|
||||
event._event, event._type, event._massage_handle_func);
|
||||
}
|
||||
my_client.start(); //block function
|
||||
|
||||
/* below to Stop Client */
|
||||
// for (auto const &method : request_methods){
|
||||
// my_client.stop_request_method(method._service, method._instance, method._method);
|
||||
// }
|
||||
for (auto const &event : request_events){
|
||||
my_client.stop_request_event(event._service, event._instance, event._event);
|
||||
}
|
||||
for (auto const &event : request_events){
|
||||
my_client.stop_request_event_group(event._service, event._instance, event._eventgroup);
|
||||
}
|
||||
for (auto const &server : request_services){
|
||||
my_client.stop_request_service(server._service, server._instance);
|
||||
}
|
||||
my_client.stop();
|
||||
return;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
436
source/example/someip_client.hpp
Normal file
436
source/example/someip_client.hpp
Normal file
@@ -0,0 +1,436 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2022).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_client.hpp
|
||||
* @brief head file of someip client
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial Version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef AG_VSOMEIP_CLIENT_HPP
|
||||
#define AG_VSOMEIP_CLIENT_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
#include <csignal>
|
||||
#endif
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
|
||||
#include <vsomeip/vsomeip.hpp>
|
||||
|
||||
#include "someip_server_cfg.hpp"
|
||||
|
||||
using namespace vsomeip;
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
#ifdef USE_LOG_MODULE
|
||||
#include "log_mgt_api.h"
|
||||
#ifdef LOG_TAG_SOMEIP
|
||||
#undef LOG_TAG_SOMEIP
|
||||
#endif
|
||||
#define LOG_TAG_SOMEIP "someip_client"
|
||||
|
||||
#define someip_log_d(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.d(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_i(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.i(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_w(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.w(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_e(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.e(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#ifdef LOG_TAG_SOMEIP
|
||||
#undef LOG_TAG_SOMEIP
|
||||
#endif
|
||||
#define LOG_TAG_SOMEIP "someip_client"
|
||||
#define someip_log_d(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("D %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_i(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("I %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_w(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("W %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_e(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("E %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
typedef std::function<std::vector<byte_t>(void)> request_method_func_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
method_t _method;
|
||||
request_method_func_t _request_method_func;
|
||||
message_type_e _message_type;
|
||||
message_handler_t _massage_handle_func;
|
||||
} request_method_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
eventgroup_t _eventgroup;
|
||||
event_t _event;
|
||||
event_type_e _type;
|
||||
message_handler_t _massage_handle_func;
|
||||
} request_event_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
} request_service_t;
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
extern void my_someip_client_thread_func(void* arg);
|
||||
|
||||
/*=======[L O C A L D A T A]================================================*/
|
||||
|
||||
/*=======[C L A S S I M P L E M E N T A T I O N S]==========================*/
|
||||
|
||||
class client {
|
||||
public:
|
||||
client(bool _use_tcp, bool _be_quiet, uint32_t _cycle) :
|
||||
app_(runtime::get()->create_application("your_client")),
|
||||
request_(runtime::get()->create_request()),
|
||||
use_tcp_(_use_tcp), //TCP OR UDP
|
||||
be_quiet_(_be_quiet),
|
||||
cycle_(_cycle),
|
||||
running_(true),
|
||||
blocked_(false),
|
||||
is_available_(false),
|
||||
sender_(std::bind(&client::run, this)) {
|
||||
}
|
||||
|
||||
bool init() {
|
||||
if (!app_->init()) {
|
||||
someip_log_e("Couldn't initialize application");
|
||||
return false;
|
||||
}
|
||||
|
||||
someip_log_i("Client settings [protocol=%s:quiet=%s:cycle=%d]",
|
||||
(use_tcp_ ? "TCP" : "UDP"),(be_quiet_ ? "true" : "false"), cycle_);
|
||||
|
||||
app_->register_state_handler(
|
||||
std::bind(&client::on_state, this,
|
||||
std::placeholders::_1));
|
||||
|
||||
//any feed back message
|
||||
app_->register_message_handler(
|
||||
ANY_SERVICE, ANY_INSTANCE, ANY_METHOD,
|
||||
std::bind(&client::on_message, this,
|
||||
std::placeholders::_1));
|
||||
|
||||
someip_log_i("Client app init done.");
|
||||
return true;
|
||||
}
|
||||
|
||||
void start_request_method(service_t _service, instance_t _instance, method_t _method,
|
||||
const request_method_func_t &_request_method_func, message_type_e _message_type,
|
||||
const message_handler_t &_massage_handle_func){
|
||||
|
||||
app_->register_availability_handler(_service, _instance,
|
||||
std::bind(&client::on_availability, this,
|
||||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
|
||||
someip_log_i("Send a requset register to method [0x%04x/0x%04x/0x%04x]",
|
||||
_service, _instance, _method);
|
||||
|
||||
if ((message_type_e::MT_REQUEST_NO_RETURN != _message_type) && (nullptr != _massage_handle_func))
|
||||
{
|
||||
/* register response massage handler */
|
||||
app_->register_message_handler(_service, _instance, _method, _massage_handle_func);
|
||||
}
|
||||
|
||||
/* request thread */
|
||||
std::thread req_method(std::bind(&client::request_method_func, this,
|
||||
_service, _instance, _method, _message_type, _request_method_func));
|
||||
req_method.detach();
|
||||
|
||||
}
|
||||
|
||||
void request_method_func(service_t _service, instance_t _instance, method_t _method,
|
||||
message_type_e _message_type, const request_method_func_t &_request_method_func)
|
||||
{
|
||||
if (is_available_){ /* TODO :this available should be one server available, change it bind to server */
|
||||
if (nullptr != _request_method_func)
|
||||
{
|
||||
std::vector<byte_t> data = _request_method_func();
|
||||
if (data[0] != byte_t{0}) /* data[0] is request flag */
|
||||
{
|
||||
std::shared_ptr< message > request = runtime::get()->create_request();
|
||||
request->set_service(_service);
|
||||
request->set_instance(_instance);
|
||||
request->set_method(_method);
|
||||
|
||||
request->set_message_type(_message_type);
|
||||
|
||||
std::shared_ptr<payload> payload;
|
||||
std::vector<byte_t> req_data;
|
||||
req_data.assign(data[1],data.size()-1); /* request data: data[1]~... */
|
||||
payload->set_data(req_data);
|
||||
request->set_payload(payload);
|
||||
|
||||
app_->send(request);
|
||||
|
||||
someip_log_i("Client/session [0x%04x/0x%04x] sent a request to Service/instance/method [0x%04x/0x%04x/0x%04x]",
|
||||
request->get_client(),request->get_session(),
|
||||
request->get_service(),request->get_instance(),request->get_method());
|
||||
}
|
||||
else
|
||||
{
|
||||
/* code */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void start_request_event(service_t _service, instance_t _instance,
|
||||
eventgroup_t _eventgroup, event_t _event, event_type_e _type,
|
||||
const message_handler_t &_massage_handle_func, major_version_t _major = DEFAULT_MAJOR){
|
||||
|
||||
app_->register_availability_handler(_service, _instance,
|
||||
std::bind(&client::on_availability, this,
|
||||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
|
||||
if (nullptr != _massage_handle_func)
|
||||
{
|
||||
/* register response massage handler */
|
||||
app_->register_message_handler(_service, _instance, _event, _massage_handle_func);
|
||||
}
|
||||
else
|
||||
{
|
||||
someip_log_w("Event [0x%04x/0x%04x/0x%04x/0x%04x] handle function is null",
|
||||
_service, _instance, _eventgroup, _event);
|
||||
}
|
||||
|
||||
std::set<eventgroup_t> its_groups;
|
||||
its_groups.insert(_eventgroup);
|
||||
app_->request_event(_service, _instance, _event,its_groups,_type);
|
||||
app_->subscribe(_service, _instance, _eventgroup, _major);
|
||||
|
||||
someip_log_i("Send a request to event [0x%04x/0x%04x/0x%04x/0x%04x]",
|
||||
_service, _instance, _eventgroup, _event);
|
||||
}
|
||||
|
||||
|
||||
void stop_request_event(service_t _service, instance_t _instance, event_t _event){
|
||||
app_->release_event(_service, _instance, _event);
|
||||
someip_log_i("Stop request event [0x%04x/0x%04x/0x%04x]",
|
||||
_service, _instance, _event);
|
||||
}
|
||||
|
||||
void stop_request_event_group(service_t _service, instance_t _instance,
|
||||
eventgroup_t _eventgroup){
|
||||
app_->unsubscribe( _service, _instance, _eventgroup);
|
||||
someip_log_i("Stop request event-group [0x%04x/0x%04x/0x%04x]",
|
||||
_service, _instance, _eventgroup);
|
||||
}
|
||||
|
||||
void start_request_service(service_t _service, instance_t _instance,
|
||||
major_version_t _major = DEFAULT_MAJOR, minor_version_t _minor = 0U){
|
||||
app_->request_service(_service, _instance, _major, _minor); // find_service (SD)
|
||||
//msg_threads_.push_back(std::thread(&client::on_message, this));
|
||||
// app_->register_message_handler(_service, _instance, ANY_METHOD,
|
||||
// std::bind(&client::on_message, this, std::placeholders::_1));
|
||||
// app_->register_message_handler(
|
||||
// ANY_SERVICE, _instance, ANY_METHOD,
|
||||
// std::bind(&client::on_message, this,
|
||||
// std::placeholders::_1));
|
||||
someip_log_i("Start request service [0x%04x/0x%04x].",_service, _instance);
|
||||
}
|
||||
|
||||
void stop_request_service(service_t _service, instance_t _instance)
|
||||
{
|
||||
app_->release_service(_service, _instance);
|
||||
//is_offered_ = false;
|
||||
someip_log_w("Stop request service [0x%04x/0x%04x].",_service, _instance);
|
||||
}
|
||||
|
||||
void start() {
|
||||
someip_log_i("Client app[%s] start.",app_->get_name().c_str());
|
||||
app_->start();
|
||||
}
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
/*
|
||||
* Handle signal to shutdown
|
||||
*/
|
||||
void stop() {
|
||||
app_->clear_all_handler();
|
||||
//app_->unsubscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENTGROUP_ID);
|
||||
//app_->release_event(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID);
|
||||
//app_->release_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
|
||||
condition_.notify_one();
|
||||
sender_.join();
|
||||
//for (auto &th: msg_threads_)th.join();
|
||||
app_->stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
void on_state(state_type_e _state) {
|
||||
someip_log_i("Application [%s] is %s.",app_->get_name().c_str(),
|
||||
(_state == state_type_e::ST_REGISTERED ? "registered" : "deregistered"));
|
||||
app_state_ = _state;
|
||||
if (_state == state_type_e::ST_REGISTERED) {
|
||||
// TODO : ...
|
||||
}
|
||||
}
|
||||
|
||||
void on_availability(service_t _service, instance_t _instance, bool _is_available){
|
||||
someip_log_i("Service [0x%04x.0x%04x] is %s", _service, _instance,
|
||||
(_is_available ? "available." : "NOT available."));
|
||||
|
||||
if (is_available_ && !_is_available){
|
||||
is_available_ = false;
|
||||
}
|
||||
else if (_is_available && !is_available_){
|
||||
is_available_ = true;
|
||||
// send();
|
||||
}
|
||||
else{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(const std::shared_ptr<message> &_response) {
|
||||
|
||||
std::shared_ptr<payload> its_payload =_response->get_payload();
|
||||
//someip_log_i("Received a message [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
// _response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
// _response->get_client(), _response->get_session());
|
||||
|
||||
switch (_response->get_method())
|
||||
{
|
||||
//case 0x8001:
|
||||
// someip_log_i("------------0x8001---------");
|
||||
// break;
|
||||
case 0x8002:
|
||||
someip_log_i("------------0x8002---------");
|
||||
break;
|
||||
case 0x8003:
|
||||
someip_log_i("------------0x8003---------");
|
||||
break;
|
||||
case 0x8004:
|
||||
someip_log_i("------------0x8004---------");
|
||||
break;
|
||||
case 0x8005:
|
||||
someip_log_i("------------0x8005---------");
|
||||
break;
|
||||
case 0x8006:
|
||||
someip_log_i("------------0x8006---------");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// // TODO : here is received_data
|
||||
// std::vector<byte_t> received_data;
|
||||
// received_data.assign(its_payload->get_data(),its_payload->get_data()+its_payload->get_length());
|
||||
// std::string data_to_str;
|
||||
// data_to_str.assign(received_data.begin(), received_data.end());
|
||||
// someip_log_i("Received message lenght[%d] [%s].",its_payload->get_length(), data_to_str.c_str());
|
||||
|
||||
}
|
||||
|
||||
void send() {
|
||||
if (!be_quiet_)
|
||||
{
|
||||
std::lock_guard< std::mutex > its_lock(mutex_);
|
||||
blocked_ = true;
|
||||
condition_.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
//bind with std::thread sender_, below function is send requset to sevice
|
||||
void run() {
|
||||
while (running_) {
|
||||
{
|
||||
std::unique_lock<std::mutex> its_lock(mutex_);
|
||||
while (!blocked_) condition_.wait(its_lock);
|
||||
if (is_available_) {
|
||||
app_->send(request_);
|
||||
|
||||
someip_log_i("Client/Session [0x%04x/0x%04x] sent a request to Service [0x%04x/0x%04x]",
|
||||
request_->get_client(),request_->get_session(),
|
||||
request_->get_service(),request_->get_instance());
|
||||
blocked_ = false;
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(cycle_));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr< application > app_;
|
||||
std::shared_ptr< message > request_;
|
||||
bool use_tcp_;
|
||||
bool be_quiet_;
|
||||
uint32_t cycle_;
|
||||
session_t session_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable condition_;
|
||||
bool running_;
|
||||
bool blocked_;
|
||||
bool is_available_;
|
||||
state_type_e app_state_;
|
||||
|
||||
std::thread sender_;
|
||||
//std::vector<std::thread> msg_threads_;
|
||||
};
|
||||
|
||||
#endif // AG_VSOMEIP_CLIENT_HPP
|
||||
113
source/example/someip_client_cfg.hpp
Normal file
113
source/example/someip_client_cfg.hpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2026).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_client_cfg.hpp
|
||||
* @brief head file of someip ids
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial Version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef SOMEIP_CLIENT_CFG_HPP
|
||||
#define SOMEIP_CLIENT_CFG_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include "someip_client.hpp"
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*--- TOOLS GENERATE BEGIN ---*/
|
||||
|
||||
#if 0 /* Not used */
|
||||
#define VSOMEIP_CONFIGURATION "/etc/vsomeip/tbox_service.json"
|
||||
#define APP_NAME "my_app"
|
||||
#define USE_TCP (1u)
|
||||
#define USE_UDP (2u)
|
||||
#define USE_PROTOCAL USE_TCP
|
||||
#define USE_STATIC_ROUTING false
|
||||
#define OFFER_F_F_METHOD false
|
||||
#define OFFER_R_R_METHOD true
|
||||
#define OFFER_SETTER true
|
||||
#define OFFER_GETTER true
|
||||
#define OFFER_EVENT true
|
||||
#endif
|
||||
|
||||
/* These IDs is just an example, please refer to SomeIp Matrix */
|
||||
/* ServiceID */
|
||||
#define S_SYSTEM_SERVER_ID 0x0001
|
||||
#define S_SYSTEM_INSTANCE_ID 0x0001
|
||||
#define S_AC_SERVER_ID 0x0002
|
||||
#define S_AC_INSTANCE_ID 0x0001
|
||||
|
||||
#define R_SYSTEM_SERVER_ID 0x8001
|
||||
#define R_SYSTEM_INSTANCE_ID 0x0001
|
||||
#define R_AC_SERVER_ID 0x8002
|
||||
#define R_AC_INSTANCE_ID 0x0001
|
||||
|
||||
/* MethodID */
|
||||
#define RR_METHOD_EXAMPLE1 0x0001
|
||||
#define RR_METHOD_EXAMPLE2 0x0002
|
||||
#define FF_METHOD_EXAMPLE1 0x1001
|
||||
#define FF_METHOD_EXAMPLE2 0x1002
|
||||
#define GETTER_EXAMPLE1 0x2001
|
||||
#define GETTER_EXAMPLE2 0x2002
|
||||
#define SETTER_EXAMPLE1 0x3001
|
||||
#define SETTER_EXAMPLE2 0x3002
|
||||
#define EVENT_EXAMPLE1 0x4001
|
||||
#define EVENT_EXAMPLE2 0x4002
|
||||
#define EVENTGROUP_EXAMPLE1 0x0001
|
||||
#define NOTIFIER_EXAMPLE1 0x5001
|
||||
#define NOTIFIER_EXAMPLE2 0x5002
|
||||
#define EVENTGROUP_EXAMPLE2 0x0002
|
||||
|
||||
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
|
||||
|
||||
/* CLIENT */
|
||||
extern std::vector<byte_t> req_rr_method_example1(void);
|
||||
extern std::vector<byte_t> req_ff_method_example1(void);
|
||||
extern std::vector<byte_t> req_getter_example1(void);
|
||||
extern std::vector<byte_t> req_setter_example1(void);
|
||||
|
||||
extern void msg_rr_method_example1(const std::shared_ptr<message> &_response);
|
||||
extern void msg_ff_method_example1(const std::shared_ptr<message> &_response);
|
||||
extern void msg_getter_example1(const std::shared_ptr<message> &_response);
|
||||
extern void msg_setter_example1(const std::shared_ptr<message> &_response);
|
||||
extern void msg_event_example1(const std::shared_ptr<message> &_response);
|
||||
extern void msg_notifier_example1(const std::shared_ptr<message> &_response);
|
||||
|
||||
|
||||
|
||||
inline std::vector<request_method_t> request_methods = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID,RR_METHOD_EXAMPLE1,req_rr_method_example1,message_type_e::MT_REQUEST,msg_rr_method_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID,FF_METHOD_EXAMPLE1,req_ff_method_example1,message_type_e::MT_REQUEST_NO_RETURN,nullptr},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID,GETTER_EXAMPLE1,req_getter_example1,message_type_e::MT_REQUEST,msg_getter_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID,SETTER_EXAMPLE2,req_setter_example1,message_type_e::MT_REQUEST,msg_setter_example1},
|
||||
|
||||
};
|
||||
inline std::vector<request_event_t> request_events = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE1, EVENT_EXAMPLE1, event_type_e::ET_EVENT,msg_event_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE2, NOTIFIER_EXAMPLE1, event_type_e::ET_FIELD,msg_notifier_example1},
|
||||
};
|
||||
inline std::vector<request_service_t> request_services = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID},
|
||||
// {S_AC_SERVER_ID, S_AC_INSTANCE_ID}
|
||||
};
|
||||
|
||||
/*--- TOOLS GENERATE END ---*/
|
||||
|
||||
#endif // SOMEIP_CLIENT_CFG_HPP
|
||||
122
source/example/someip_server.cpp
Normal file
122
source/example/someip_server.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2026).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_server.cpp
|
||||
* @brief main entry of someip_server
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include "someip_server_cfg.hpp"
|
||||
//#include "someip_server.hpp"
|
||||
#include <list>
|
||||
//#include "location_mgt_api.h"
|
||||
|
||||
using namespace vsomeip;
|
||||
/*=======[G L O B A L D A T A]==============================================*/
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*=======[I N T E R N A L D A T A]==========================================*/
|
||||
|
||||
/*=======[L O C A L F U N C T I O N D E C L A R A T I O N S]==============*/
|
||||
|
||||
/*=======[L O C A L F U N C T I O N I M P L E M E N T A T I O N S]========*/
|
||||
|
||||
/*=======[G L O B A L F U N C T I O N I M P L E M E N T A T I O N S]======*/
|
||||
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
service *server_ptr(nullptr);
|
||||
void server_handle_signal(int _signal)
|
||||
{
|
||||
if (server_ptr != nullptr &&
|
||||
(_signal == SIGINT || _signal == SIGTERM
|
||||
|| _signal == SIGKILL))
|
||||
server_ptr->stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
void my_someip_server_thread_func(void* arg)
|
||||
{
|
||||
service server;
|
||||
someip_log_i("start someip server");
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
server_ptr = &server;
|
||||
signal(SIGINT, server_handle_signal);
|
||||
signal(SIGTERM, server_handle_signal);
|
||||
signal(SIGKILL, server_handle_signal);
|
||||
#endif
|
||||
someip_log_i("start initial.");
|
||||
if (server.init())
|
||||
{
|
||||
someip_log_i("start offer method/events.");
|
||||
/* register/offer server */
|
||||
for (auto const&service: my_servers){
|
||||
server.start_offer_service(service._service, service._instance,
|
||||
service._major, service._minor);
|
||||
}
|
||||
|
||||
/* register R/R methods */
|
||||
for (auto const&method: offer_rr_methods){
|
||||
server.register_method(method._service,method._instance,method._method,method._method_type,method._get_response_data_func);
|
||||
}
|
||||
|
||||
/* register F/F methods */
|
||||
for (auto const&method: offer_ff_methods){
|
||||
server.register_method(method._service,method._instance,method._method,method._method_type,method._get_response_data_func);
|
||||
}
|
||||
|
||||
/* register Events */
|
||||
for (auto const&event: offer_events){
|
||||
server.register_event(event._service, event._instance, event._eventgroup,
|
||||
event._event, event._type, event._cycle, event._change_resets_cycle,
|
||||
event._update_on_change, event._epsilon_change_func,
|
||||
event._reliability, event._notify_data_func);
|
||||
}
|
||||
|
||||
/* register Notifiers */
|
||||
for (auto const&event: offer_notifiers){
|
||||
server.register_event(event._service, event._instance, event._eventgroup,
|
||||
event._event, event._type, event._cycle, event._change_resets_cycle,
|
||||
event._update_on_change, event._epsilon_change_func,
|
||||
event._reliability, event._notify_data_func);
|
||||
}
|
||||
|
||||
/* register setters */
|
||||
for (auto const&method: offer_setters){
|
||||
server.register_method(method._service,method._instance,method._method,method._method_type,method._get_response_data_func);
|
||||
}
|
||||
|
||||
/* register getters */
|
||||
for (auto const&method: offer_getters){
|
||||
server.register_method(method._service,method._instance,method._method,method._method_type,method._get_response_data_func);
|
||||
}
|
||||
|
||||
/* To start server, after method/event is registered */
|
||||
server.start(); //block function
|
||||
|
||||
/* stop server */
|
||||
for (auto const&service: my_servers){
|
||||
server.stop_offer_service(service._service, service._instance,
|
||||
service._major, service._minor);
|
||||
}
|
||||
server.stop();
|
||||
return;
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
420
source/example/someip_server.hpp
Normal file
420
source/example/someip_server.hpp
Normal file
@@ -0,0 +1,420 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2022).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_server.hpp
|
||||
* @brief head file of someip service
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial Version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef SOMEIP_SERVER_HPP
|
||||
#define SOMEIP_SERVER_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
#include <csignal>
|
||||
#endif
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
#include <vsomeip/vsomeip.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace vsomeip;
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
#ifdef USE_LOG_MODULE
|
||||
#include "log_mgt_api.h"
|
||||
#ifdef LOG_TAG_SOMEIP
|
||||
#undef LOG_TAG_SOMEIP
|
||||
#endif
|
||||
#define LOG_TAG_SOMEIP "someip_server"
|
||||
|
||||
#define someip_log_d(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.d(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_i(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.i(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_w(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.w(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define someip_log_e(...) \
|
||||
do \
|
||||
{ \
|
||||
Log.e(LOG_TAG_SOMEIP, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#ifdef LOG_TAG_SOMEIP
|
||||
#undef LOG_TAG_SOMEIP
|
||||
#endif
|
||||
#define LOG_TAG_SOMEIP "someip_server"
|
||||
#define someip_log_d(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("D %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_i(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("I %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_w(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("W %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
#define someip_log_e(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
printf("E %s ", LOG_TAG_SOMEIP); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
typedef std::function<std::vector<byte_t>(void)> get_response_data_func_t;
|
||||
// typedef std::function<std::vector<byte_t>(const std::shared_ptr<bool> &)> get_notify_data_func_t;
|
||||
typedef std::function<std::vector<byte_t>(void)> get_notify_data_func_t;
|
||||
// typedef std::function<
|
||||
// bool (const std::shared_ptr<payload> &,
|
||||
// const std::shared_ptr<payload> &) > epsilon_change_func_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
method_t _method;
|
||||
message_type_e _method_type;
|
||||
get_response_data_func_t _get_response_data_func;
|
||||
} offer_method_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
method_t _method;
|
||||
} stop_method_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
eventgroup_t _eventgroup;
|
||||
event_t _event;
|
||||
event_type_e _type;
|
||||
uint32_t _cycle; /*ms*/
|
||||
bool _change_resets_cycle;
|
||||
bool _update_on_change;
|
||||
epsilon_change_func_t _epsilon_change_func;
|
||||
reliability_type_e _reliability;
|
||||
get_notify_data_func_t _notify_data_func;
|
||||
} offer_event_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
event_t _event;
|
||||
} stop_event_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
service_t _service;
|
||||
instance_t _instance;
|
||||
major_version_t _major;
|
||||
minor_version_t _minor;
|
||||
} offer_service_t;
|
||||
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
extern void my_someip_server_thread_func(void* arg);
|
||||
/*=======[L O C A L D A T A]================================================*/
|
||||
|
||||
/*=======[C L A S S I M P L E M E N T A T I O N S]==========================*/
|
||||
|
||||
class service
|
||||
{
|
||||
public:
|
||||
service() :
|
||||
app_(vsomeip::runtime::get()->create_application("your_server"))
|
||||
, is_registered_(false)
|
||||
, running_(true)
|
||||
, service_is_offered_(false){
|
||||
}
|
||||
|
||||
bool init()
|
||||
{
|
||||
// std::lock_guard<std::mutex> its_lock(mutex_);
|
||||
|
||||
if (!app_->init())
|
||||
{
|
||||
someip_log_e("Couldn't initialize application");
|
||||
return false;
|
||||
}
|
||||
app_->register_state_handler(
|
||||
std::bind(&service::on_state, this, std::placeholders::_1));
|
||||
|
||||
// {
|
||||
// std::lock_guard<std::mutex> its_lock(payload_mutex_);
|
||||
// payload_ = runtime::get()->create_payload();
|
||||
// }
|
||||
|
||||
// condition_.notify_one();
|
||||
someip_log_i("Server app init done.");
|
||||
return true;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
someip_log_i("Server app[%s] start.",app_->get_name().c_str());
|
||||
app_->start();
|
||||
}
|
||||
|
||||
void register_method(service_t _service, instance_t _instance, method_t _method,
|
||||
message_type_e _method_type, const get_response_data_func_t &_get_response_data_func)
|
||||
{
|
||||
someip_log_i("Start a method [0x%04x/0x%04x/0x%04x]",_service, _instance, _method);
|
||||
app_->register_message_handler(_service, _instance, _method,
|
||||
std::bind(&service::on_method_receive, this, std::placeholders::_1, _method_type, _get_response_data_func));
|
||||
}
|
||||
|
||||
void on_method_receive(const std::shared_ptr<message> &_request, message_type_e _method_type,
|
||||
const get_response_data_func_t &_get_response_data_func)
|
||||
{
|
||||
someip_log_i("Received a request from client[0x%04x/0x%04x] to service[0x%04x/0x%04x]",
|
||||
_request->get_client(), _request->get_session(),
|
||||
_request->get_service(), _request->get_instance());
|
||||
if ((nullptr != _get_response_data_func) && (_method_type != message_type_e::MT_REQUEST_NO_RETURN))
|
||||
{
|
||||
std::shared_ptr<message> _response = runtime::get()->create_response(_request);
|
||||
std::shared_ptr<payload> _payload = runtime::get()->create_payload();
|
||||
std::vector<byte_t> data = _get_response_data_func();
|
||||
/* _response->set_message_type(message_type_e::MT_RESPONSE);*/
|
||||
_payload->set_data(data);
|
||||
_response->set_payload(_payload);
|
||||
app_->send(_response);
|
||||
someip_log_i("Send response from [0x%04x/0x%04x] to [0x%04x/0x%04x] length[%d]",
|
||||
_request->get_service(), _request->get_instance(),
|
||||
_request->get_client(), _request->get_session(),_payload->get_length());
|
||||
}
|
||||
else
|
||||
{
|
||||
/* to execute F/F-method callback function */
|
||||
someip_log_i("Execcute F/F-method request from [0x%04x/0x%04x]", _request->get_service(), _request->get_instance());
|
||||
_get_response_data_func();
|
||||
}
|
||||
}
|
||||
void unregister_method(service_t _service, instance_t _instance,
|
||||
method_t _method){
|
||||
someip_log_w("Stop a method [0x%04x/0x%04x/0x%04x]",_service, _instance, _method);
|
||||
app_->unregister_message_handler(_service, _instance, _method);
|
||||
}
|
||||
|
||||
// TODO : _type, _cycle, _reliability define by config file
|
||||
void register_event(service_t _service, instance_t _instance,
|
||||
eventgroup_t _eventgroup, event_t _event, event_type_e _type,
|
||||
uint32_t _cycle, bool _change_resets_cycle,
|
||||
bool _update_on_change, epsilon_change_func_t _epsilon_change_func,
|
||||
reliability_type_e _reliability, get_notify_data_func_t _notify_data_func)
|
||||
{
|
||||
std::set<eventgroup_t> groups;
|
||||
groups.insert(_eventgroup);
|
||||
|
||||
if (!_epsilon_change_func) {
|
||||
std::cerr << "epsilon_change_func is null!" << std::endl;
|
||||
}
|
||||
if (!_notify_data_func)
|
||||
{
|
||||
std::cerr << "_notify_data_func is null!" << std::endl;
|
||||
}
|
||||
|
||||
app_->offer_event(_service, _instance, _event, groups, _type,
|
||||
std::chrono::milliseconds(_cycle),
|
||||
_change_resets_cycle, _update_on_change,
|
||||
_epsilon_change_func, _reliability);
|
||||
|
||||
someip_log_i("Start a event [0x%04x/0x%04x/0x%04x/0x%04x], type[%d], cycle[%dms]",
|
||||
_service, _instance, _eventgroup, _event, _type, _cycle);
|
||||
|
||||
std::thread _notify_event(std::bind(&service::notify_event, this,
|
||||
_service, _instance, _event, _cycle, _update_on_change, _notify_data_func));
|
||||
_notify_event.detach();
|
||||
}
|
||||
|
||||
void notify_event(service_t _service, instance_t _instance, event_t _event,
|
||||
uint32_t _cycle, bool _update_on_change, get_notify_data_func_t _notify_data_func)
|
||||
{
|
||||
|
||||
someip_log_i("Start event [0x%04x] notify thread.",_event);
|
||||
|
||||
while (running_)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
/* wait for service offer */
|
||||
while (!service_is_offered_ && running_)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
if (service_is_offered_ && running_)
|
||||
{
|
||||
|
||||
if (_update_on_change || (0U == _cycle))
|
||||
{
|
||||
// for update on change
|
||||
/* If data is not empty and changed, to notify */
|
||||
static std::vector<byte_t> lastData;
|
||||
std::vector<byte_t> notify_data;
|
||||
notify_data.clear();
|
||||
notify_data = _notify_data_func();
|
||||
|
||||
if ((nullptr != _notify_data_func) && (lastData != notify_data))
|
||||
{
|
||||
lastData = notify_data;
|
||||
std::shared_ptr<payload> _payload = runtime::get()->create_payload();
|
||||
_payload->set_data(notify_data);
|
||||
|
||||
someip_log_d("Notify event[0x%04x] with data size[%d] payload length[%d].", _event,notify_data.size(), _payload->get_length());
|
||||
|
||||
app_->notify(_service, _instance, _event, _payload);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else // for cycle update
|
||||
{
|
||||
|
||||
if (nullptr != _notify_data_func)
|
||||
{
|
||||
std::vector<byte_t> notify_data;
|
||||
std::shared_ptr<payload> _payload = runtime::get()->create_payload();
|
||||
notify_data.clear();
|
||||
notify_data = _notify_data_func();
|
||||
_payload->set_data(notify_data);
|
||||
|
||||
someip_log_d("Notify cycle event[0x%04x] with data size[%d] payload length[%d].", _event,notify_data.size(), _payload->get_length());
|
||||
app_->notify(_service, _instance, _event, _payload);
|
||||
|
||||
}
|
||||
else{
|
||||
someip_log_e("event[0x%04x]_notify_data_func is nullptr!!", _event);
|
||||
return;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(_cycle - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
someip_log_w("Stop event [0x%04x] notify thread.",_event);
|
||||
return;
|
||||
}
|
||||
|
||||
void unregister_event(service_t _service, instance_t _instance, event_t _event)
|
||||
{
|
||||
app_->stop_offer_event(_service, _instance, _event);
|
||||
someip_log_i("Stop a event [0x%04x/0x%04x/0x%04x].",_service, _instance, _event);
|
||||
}
|
||||
|
||||
void start_offer_service(service_t _service, instance_t _instance,
|
||||
major_version_t _major = DEFAULT_MAJOR, minor_version_t _minor = DEFAULT_MINOR)
|
||||
{
|
||||
// std::lock_guard<std::mutex> its_lock(notify_mutex_);
|
||||
app_->offer_service(_service, _instance, _major, _minor); // offer service (SD)
|
||||
service_is_offered_ = true;
|
||||
// notify_condition_.notify_one();
|
||||
someip_log_i("Start offer service [0x%04x/0x%04x].",_service, _instance);
|
||||
}
|
||||
|
||||
void stop_offer_service(service_t _service, instance_t _instance,
|
||||
major_version_t _major = DEFAULT_MAJOR, minor_version_t _minor = DEFAULT_MINOR)
|
||||
{
|
||||
app_->stop_offer_service(_service, _instance, _major, _minor);
|
||||
service_is_offered_ = false;
|
||||
someip_log_w("Stop offer service [0x%04x/0x%04x].",_service, _instance);
|
||||
}
|
||||
|
||||
void on_state(state_type_e _state)
|
||||
{
|
||||
someip_log_i("Application [%s] is %s.",app_->get_name().c_str(),
|
||||
(_state == state_type_e::ST_REGISTERED ? "registered" : "deregistered"));
|
||||
|
||||
if (_state == state_type_e::ST_REGISTERED)
|
||||
{
|
||||
if (!is_registered_)
|
||||
{
|
||||
is_registered_ = true;
|
||||
//condition_.notify_one();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
is_registered_ = false;
|
||||
}
|
||||
}
|
||||
#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
/*
|
||||
* Handle signal to shutdown
|
||||
*/
|
||||
void stop()
|
||||
{
|
||||
running_ = false;
|
||||
//condition_.notify_one();
|
||||
//notify_condition_.notify_one();
|
||||
app_->clear_all_handler();
|
||||
//notify_thread_.join();
|
||||
for (auto &th: notify_threads_)th.join();
|
||||
app_->stop();
|
||||
someip_log_w("Service app stopped.");
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
std::shared_ptr<application> app_;
|
||||
bool is_registered_;
|
||||
|
||||
//std::mutex mutex_;
|
||||
//std::condition_variable condition_;
|
||||
bool running_;
|
||||
|
||||
//std::mutex notify_mutex_;
|
||||
//std::condition_variable notify_condition_;
|
||||
bool service_is_offered_;
|
||||
|
||||
//std::mutex payload_mutex_;
|
||||
//std::shared_ptr<payload> payload_;
|
||||
|
||||
// blocked_ / service_is_offered_ must be initialized before starting the threads!
|
||||
// std::thread offer_thread_;
|
||||
// std::thread notify_thread_;
|
||||
std::vector<std::thread> notify_threads_;
|
||||
};
|
||||
|
||||
#endif // SOMEIP_SERVER_HPP
|
||||
144
source/example/someip_server_cfg.hpp
Normal file
144
source/example/someip_server_cfg.hpp
Normal file
@@ -0,0 +1,144 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2026).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file someip_server_cfg.hpp
|
||||
* @brief head file of someip ids
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial Version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
#ifndef SOMEIP_SERVER_CFG_HPP
|
||||
#define SOMEIP_SERVER_CFG_HPP
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include "someip_server.hpp"
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*--- TOOLS GENERATE BEGIN ---*/
|
||||
|
||||
#define VSOMEIP_CONFIGURATION "/etc/vsomeip/tbox_service.json"
|
||||
#define APP_NAME "my_app"
|
||||
#define USE_TCP (1u)
|
||||
#define USE_UDP (2u)
|
||||
#define USE_PROTOCAL USE_TCP
|
||||
#define USE_STATIC_ROUTING false
|
||||
#define OFFER_F_F_METHOD false
|
||||
#define OFFER_R_R_METHOD true
|
||||
#define OFFER_SETTER true
|
||||
#define OFFER_GETTER true
|
||||
#define OFFER_EVENT true
|
||||
|
||||
/* These IDs is just an example, please refer to SomeIp Matrix */
|
||||
/* ServiceID */
|
||||
#define S_SYSTEM_SERVER_ID 0x0001
|
||||
#define S_SYSTEM_INSTANCE_ID 0x0001
|
||||
#define S_AC_SERVER_ID 0x0002
|
||||
#define S_AC_INSTANCE_ID 0x0001
|
||||
|
||||
#define R_SYSTEM_SERVER_ID 0x8001
|
||||
#define R_SYSTEM_INSTANCE_ID 0x0001
|
||||
#define R_AC_SERVER_ID 0x8002
|
||||
#define R_AC_INSTANCE_ID 0x0001
|
||||
|
||||
/* MethodID */
|
||||
#define RR_METHOD_EXAMPLE1 0x0001
|
||||
#define RR_METHOD_EXAMPLE2 0x0002
|
||||
#define FF_METHOD_EXAMPLE1 0x1001
|
||||
#define FF_METHOD_EXAMPLE2 0x1002
|
||||
#define GETTER_EXAMPLE1 0x2001
|
||||
#define GETTER_EXAMPLE2 0x2002
|
||||
#define SETTER_EXAMPLE1 0x3001
|
||||
#define SETTER_EXAMPLE2 0x3002
|
||||
#define EVENT_EXAMPLE1 0x4001
|
||||
#define EVENT_EXAMPLE2 0x4002
|
||||
#define EVENTGROUP_EXAMPLE1 0x0001
|
||||
#define NOTIFIER_EXAMPLE1 0x5001
|
||||
#define NOTIFIER_EXAMPLE2 0x5002
|
||||
#define EVENTGROUP_EXAMPLE2 0x0002
|
||||
|
||||
|
||||
/*=======[P A R A M T Y P E D E F]==========================================*/
|
||||
|
||||
/*=======[E X P O R T D A T A]==============================================*/
|
||||
|
||||
/*=======[E X P O R T F U N C T I O N D E C L A R A T I O N S]============*/
|
||||
|
||||
|
||||
extern std::vector<byte_t> rr_method_example1(void);
|
||||
extern std::vector<byte_t> rr_method_example2(void);
|
||||
extern std::vector<byte_t> ff_method_example1(void);
|
||||
extern std::vector<byte_t> ff_method_example2(void);
|
||||
extern std::vector<byte_t> getter_example1(void);
|
||||
extern std::vector<byte_t> getter_example2(void);
|
||||
extern std::vector<byte_t> setter_example1(void);
|
||||
extern std::vector<byte_t> setter_example2(void);
|
||||
extern std::vector<byte_t> event_example1(void);
|
||||
extern std::vector<byte_t> event_example2(void);
|
||||
extern std::vector<byte_t> notifier_example1(void);
|
||||
extern std::vector<byte_t> notifier_example2(void);
|
||||
extern bool epsilon_func(std::shared_ptr<vsomeip::payload> _old, std::shared_ptr<vsomeip::payload> _new);
|
||||
|
||||
// my_servers
|
||||
inline std::vector<offer_service_t> my_servers = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, DEFAULT_MAJOR, DEFAULT_MINOR},
|
||||
{S_AC_SERVER_ID, S_AC_INSTANCE_ID, DEFAULT_MAJOR, DEFAULT_MINOR}
|
||||
};
|
||||
|
||||
// R/R Methods /* usage eg: receive action */
|
||||
inline std::vector<offer_method_t> offer_rr_methods = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, RR_METHOD_EXAMPLE1, message_type_e::MT_REQUEST, &rr_method_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, RR_METHOD_EXAMPLE2, message_type_e::MT_REQUEST, &rr_method_example2}
|
||||
};
|
||||
|
||||
// F/F Methods /* usage eg: receive action without respons */
|
||||
inline std::vector<offer_method_t> offer_ff_methods = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, FF_METHOD_EXAMPLE1, message_type_e::MT_REQUEST_NO_RETURN, &ff_method_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, FF_METHOD_EXAMPLE2, message_type_e::MT_REQUEST_NO_RETURN, &ff_method_example2}
|
||||
};
|
||||
|
||||
// Getter /* usage eg: for client get infomation */
|
||||
inline std::vector<offer_method_t> offer_getters = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, GETTER_EXAMPLE1, message_type_e::MT_REQUEST, &getter_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, GETTER_EXAMPLE2, message_type_e::MT_REQUEST, &getter_example2}
|
||||
};
|
||||
|
||||
// Setter /* usage eg: for client set infomation */
|
||||
inline std::vector<offer_method_t> offer_setters = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, SETTER_EXAMPLE1, message_type_e::MT_REQUEST, &setter_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, SETTER_EXAMPLE2, message_type_e::MT_REQUEST, &setter_example2}
|
||||
};
|
||||
|
||||
// Event /* usage eg: offer event massage */
|
||||
inline std::vector<offer_event_t> offer_events = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE1,
|
||||
EVENT_EXAMPLE1, event_type_e::ET_EVENT, 0U, false, false, /* active on change */
|
||||
&epsilon_func, reliability_type_e::RT_UNRELIABLE, &event_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE1,
|
||||
EVENT_EXAMPLE2, event_type_e::ET_EVENT, 1000U, false, false, /* cycle */
|
||||
&epsilon_func, reliability_type_e::RT_UNRELIABLE, &event_example2}
|
||||
};
|
||||
|
||||
// Notifier /* usage eg: offer event massage (with initial state))*/
|
||||
inline std::vector<offer_event_t> offer_notifiers = {
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE2,
|
||||
NOTIFIER_EXAMPLE1, event_type_e::ET_FIELD, 1000U, false, false, /* active on change */
|
||||
&epsilon_func, reliability_type_e::RT_UNRELIABLE, ¬ifier_example1},
|
||||
{S_SYSTEM_SERVER_ID, S_SYSTEM_INSTANCE_ID, EVENTGROUP_EXAMPLE2,
|
||||
NOTIFIER_EXAMPLE2, event_type_e::ET_FIELD, 0U, false, false, /* cycle */
|
||||
&epsilon_func, reliability_type_e::RT_RELIABLE, ¬ifier_example2}
|
||||
};
|
||||
|
||||
/*--- TOOLS GENERATE END ---*/
|
||||
|
||||
#endif // SOMEIP_SERVER_CFG_HPP
|
||||
369
source/example/your_app.cpp
Normal file
369
source/example/your_app.cpp
Normal file
@@ -0,0 +1,369 @@
|
||||
/*============================================================================*/
|
||||
/* Copyright (C) huaxu (2026).
|
||||
*
|
||||
* All rights reserved. This software is huaxu property. Duplication
|
||||
* or disclosure without huaxu written authorization is prohibited.
|
||||
*
|
||||
* @file your_app.cpp
|
||||
* @brief main entry of your_app
|
||||
* @author LiuZhimin
|
||||
* @date 2026-02-24 15:20:00
|
||||
*/
|
||||
/*============================================================================*/
|
||||
/*=======[R E V I S I O N H I S T O R Y]====================================*/
|
||||
/* <VERSION> <DATE> <AUTHOR> <REVISION LOG>
|
||||
* V0.0.1 20260606 Liuzhimin Initial version
|
||||
*/
|
||||
/*============================================================================*/
|
||||
|
||||
/*=======[I N C L U D E S]====================================================*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "someip_server_cfg.hpp"
|
||||
#include "someip_client_cfg.hpp"
|
||||
//#include "someip_server.hpp"
|
||||
#include <list>
|
||||
//#include "location_mgt_api.h"
|
||||
|
||||
using namespace vsomeip;
|
||||
/*=======[G L O B A L D A T A]==============================================*/
|
||||
|
||||
/*=======[M A C R O S]========================================================*/
|
||||
|
||||
/*=======[I N T E R N A L D A T A]==========================================*/
|
||||
|
||||
/*=======[L O C A L F U N C T I O N D E C L A R A T I O N S]==============*/
|
||||
|
||||
/*=======[L O C A L F U N C T I O N I M P L E M E N T A T I O N S]========*/
|
||||
|
||||
/*=======[G L O B A L F U N C T I O N I M P L E M E N T A T I O N S]======*/
|
||||
// methods' data functions
|
||||
std::vector<byte_t> rr_method_example1(void){
|
||||
/* 1.receive this example, and add your code do your action here */
|
||||
//...
|
||||
|
||||
/* 2.And than give response data */
|
||||
std::string example_string = "Hello China.";
|
||||
std::vector<byte_t> data;
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
// events' data functions
|
||||
std::vector<byte_t> rr_method_example2(void){
|
||||
/* 1.receive this example, and add your code do your action here */
|
||||
//...
|
||||
|
||||
/* 2.And than give response data */
|
||||
std::string example_string = "Hello GuangZhou.";
|
||||
std::vector<byte_t> data;
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> ff_method_example1(void){
|
||||
/* 1.Add your code do your action here */
|
||||
//...
|
||||
|
||||
/* reserve,no need data to reponse*/
|
||||
std::vector<byte_t> data;
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> ff_method_example2(void){
|
||||
/* 1.Add your code do your action here */
|
||||
//...
|
||||
|
||||
/* reserve,no need data to reponse*/
|
||||
std::vector<byte_t> data;
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> getter_example1(void){
|
||||
std::vector<byte_t> data;
|
||||
/* 1.Add your code do get a feature value */
|
||||
//...
|
||||
|
||||
/* 2.And than response value to data */
|
||||
std::string example_string = "This is an getter example.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> getter_example2(void){
|
||||
std::vector<byte_t> data;
|
||||
/* 1.Add your code do get a feature value */
|
||||
//...
|
||||
|
||||
/* 2.And than response value to data */
|
||||
std::string example_string = "This is an getter example2.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> setter_example1(void){
|
||||
std::vector<byte_t> data;
|
||||
/* 1.Add your code do set a feature value */
|
||||
//...
|
||||
|
||||
/* 2.And than response state to client */
|
||||
std::string example_string = "Setter xxx is success.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
|
||||
/* 3. If need, you can add your Notify response here */
|
||||
// ...
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> setter_example2(void){
|
||||
std::vector<byte_t> data;
|
||||
/* 1.Add your code do set a feature value */
|
||||
//...
|
||||
|
||||
/* 2.And than response state to client */
|
||||
std::string example_string = "Setter xxx is success2.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
|
||||
/* 3. If need, you can add your Notify response here */
|
||||
// ...
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> event_example1(void){
|
||||
|
||||
std::vector<byte_t> data;
|
||||
/* Add your handle logic here for data to notify */
|
||||
// ...
|
||||
|
||||
std::string example_string = "info is empty.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> event_example2(void){
|
||||
|
||||
std::vector<byte_t> data;
|
||||
/* Add your handle logic here for data to notify */
|
||||
// ...
|
||||
|
||||
std::string example_string = "info is empty.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> notifier_example1(void){
|
||||
|
||||
std::vector<byte_t> data;
|
||||
/* Add your handle logic here for data to notify */
|
||||
// ...
|
||||
|
||||
std::string example_string = "info is empty.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<byte_t> notifier_example2(void){
|
||||
|
||||
std::vector<byte_t> data;
|
||||
/* Add your handle logic here for data to notify */
|
||||
// ...
|
||||
|
||||
std::string example_string = "info is empty.";
|
||||
data.assign(example_string.begin(),example_string.end());
|
||||
return data;
|
||||
}
|
||||
|
||||
bool epsilon_func(std::shared_ptr<vsomeip::payload> _old, std::shared_ptr<vsomeip::payload> _new)
|
||||
{
|
||||
// TODO: add your logic here to compare _old and _new
|
||||
return false;
|
||||
};
|
||||
|
||||
/* CLENT */
|
||||
std::vector<byte_t> req_rr_method_example1(void)
|
||||
{
|
||||
static bool trrigger_method = false;
|
||||
/* 1.Add your code here to give request data */
|
||||
/* 1.1: data[0] is request flag : true/false */
|
||||
// ...
|
||||
|
||||
std::vector<byte_t> data;
|
||||
data.push_back(trrigger_method); /* data[0]*/
|
||||
if (trrigger_method){
|
||||
/* reset trrigger_method */
|
||||
trrigger_method = false;
|
||||
}
|
||||
/* 1.2. Add your other request data to data[1]~... */
|
||||
std::string example_string = "reqeust data.";
|
||||
data.insert(data.end(),example_string.begin(),example_string.end());
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
std::vector<byte_t> req_ff_method_example1(void)
|
||||
{
|
||||
static bool trrigger_method = false;
|
||||
/* 1.Add your code here to give request data */
|
||||
/* 1.1: data[0] is request flag : true/false */
|
||||
// ...
|
||||
|
||||
std::vector<byte_t> data;
|
||||
data.push_back(trrigger_method); /* data[0]*/
|
||||
if (trrigger_method){
|
||||
/* reset trrigger_method */
|
||||
trrigger_method = false;
|
||||
}
|
||||
/* 1.2. Add your other request data to data[1]~... */
|
||||
std::string example_string = "reqeust data.";
|
||||
data.insert(data.end(),example_string.begin(),example_string.end());
|
||||
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
std::vector<byte_t> req_getter_example1(void)
|
||||
{
|
||||
static bool trrigger_method = false;
|
||||
/* 1.Add your code here to give request data */
|
||||
/* 1.1: data[0] is request flag : true/false */
|
||||
// ...
|
||||
|
||||
std::vector<byte_t> data;
|
||||
data.push_back(trrigger_method); /* data[0]*/
|
||||
if (trrigger_method){
|
||||
/* reset trrigger_method */
|
||||
trrigger_method = false;
|
||||
}
|
||||
/* 1.2. Add your other request data to data[1]~... */
|
||||
std::string example_string = "reqeust data.";
|
||||
data.insert(data.end(),example_string.begin(),example_string.end());
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
std::vector<byte_t> req_setter_example1(void)
|
||||
{
|
||||
static bool trrigger_method = false;
|
||||
/* 1.Add your code here to give request data */
|
||||
/* 1.1: data[0] is request flag : true/false */
|
||||
// ...
|
||||
|
||||
std::vector<byte_t> data;
|
||||
data.push_back(trrigger_method); /* data[0]*/
|
||||
if (trrigger_method){
|
||||
/* reset trrigger_method */
|
||||
trrigger_method = false;
|
||||
}
|
||||
/* 1.2. Add your other request data to data[1]~... */
|
||||
std::string example_string = "reqeust data.";
|
||||
data.insert(data.end(),example_string.begin(),example_string.end());
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void msg_rr_method_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
std::shared_ptr<payload> res_payload =_response->get_payload();
|
||||
someip_log_i("Received a message from Server/instance/method [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
_response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
_response->get_client(), _response->get_session());
|
||||
/* 1. Add your logic here to handle this response payload */
|
||||
// res_payload ...
|
||||
|
||||
}
|
||||
#if 0 /* FF-method no need massage response handler */
|
||||
void msg_ff_method_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
/* nothing*/
|
||||
}
|
||||
#endif
|
||||
void msg_getter_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
std::shared_ptr<payload> res_payload =_response->get_payload();
|
||||
someip_log_i("Received a message from Server/instance/method [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
_response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
_response->get_client(), _response->get_session());
|
||||
/* 1. Add your logic here to handle this response payload */
|
||||
// res_payload ...
|
||||
|
||||
|
||||
}
|
||||
void msg_setter_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
std::shared_ptr<payload> res_payload =_response->get_payload();
|
||||
someip_log_i("Received a message from Server/instance/method [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
_response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
_response->get_client(), _response->get_session());
|
||||
/* 1. Add your logic here to handle this response payload */
|
||||
// res_payload ...
|
||||
|
||||
}
|
||||
void msg_event_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
std::shared_ptr<payload> res_payload =_response->get_payload();
|
||||
someip_log_i("Received a message from Server/instance/event [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
_response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
_response->get_client(), _response->get_session());
|
||||
/* 1. Add your logic here to handle this response payload */
|
||||
// res_payload ...
|
||||
|
||||
}
|
||||
void msg_notifier_example1(const std::shared_ptr<message> &_response)
|
||||
{
|
||||
std::shared_ptr<payload> res_payload =_response->get_payload();
|
||||
someip_log_i("Received a message from Server/instance/event [0x%04x.0x%04x.0x%04x] to Client/Session [0x%04x.0x%04x].",
|
||||
_response->get_service(), _response->get_instance(), _response->get_method(),
|
||||
_response->get_client(), _response->get_session());
|
||||
/* 1. Add your logic here to handle this response payload */
|
||||
// res_payload ...
|
||||
|
||||
}
|
||||
|
||||
// #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
// //service *server_ptr(nullptr);
|
||||
// void handle_signal(int _signal)
|
||||
// {
|
||||
// if (server_ptr != nullptr &&
|
||||
// (_signal == SIGINT || _signal == SIGTERM
|
||||
// || _signal == SIGKILL))
|
||||
// server_ptr->stop();
|
||||
// }
|
||||
// #endif
|
||||
|
||||
|
||||
// service server;
|
||||
// someip_log_i("start someip server");
|
||||
|
||||
// #ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
|
||||
// server_ptr = &server;
|
||||
// signal(SIGINT, handle_signal);
|
||||
// signal(SIGTERM, handle_signal);
|
||||
// signal(SIGKILL, handle_signal);
|
||||
// #endif
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
printf("start your_app\r\n");
|
||||
try {
|
||||
std::thread server_thread(my_someip_server_thread_func, nullptr);
|
||||
std::thread client_thread(my_someip_client_thread_func, nullptr);
|
||||
|
||||
// wait for threads stop
|
||||
server_thread.join();
|
||||
std::cout << "server_thread stopped." << std::endl;
|
||||
|
||||
client_thread.join();
|
||||
std::cout << "client_thread stopped." << std::endl;
|
||||
|
||||
} catch (const std::system_error& e) {
|
||||
std::cerr << "Thread creation failed: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
28
source/example/your_app.json
Normal file
28
source/example/your_app.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"unicast": "127.0.0.1",
|
||||
"logging": {
|
||||
"level": "debug",
|
||||
"console": "true"
|
||||
},
|
||||
"applications": [
|
||||
{
|
||||
"name": "your_server",
|
||||
"id": "0x1234"
|
||||
},
|
||||
{
|
||||
"name" : "your_client",
|
||||
"id" : "0x4321"
|
||||
}
|
||||
],
|
||||
"services": [
|
||||
{
|
||||
"service": "0x0001",
|
||||
"instance": "0x0001",
|
||||
"reliable": "30509"
|
||||
}
|
||||
],
|
||||
"routing": "your_server",
|
||||
"service-discovery": {
|
||||
"enable": "false"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user