vsomeip源码剖析--00环境搭建

环境

Win11 + WSL2 + Ubuntu22.04

安装依赖

sudo apt-get install cmake
sudo apt-get install libboost-system1.71-dev libboost-thread1.71-dev libboost-log1.71-dev

源码编译

获取源码

https://github.com/COVESA/vsomeip.git

编译

cd vsomeip
mkdir build
cd build

// 一般编译
cmake .. && make -j4 && make install

// 自定义安装路径编译
cmake -DCMAKE_INSTALL_PREFIX=$PWD/install .. && make -j4 && make install

其中可以设置一些编译参数:

  • 添加BasePath: base path为生成的unix domain socket文件的路径(默认的base path是/tmp)
    cmake -DBASE_PATH=/your/base_path ..
    
  • 使用unicast and/or diagnosis address进行编译,预定义unicast,unicast是单播地址,默认为127.0.0.1,预定义diagnosis,diagnosis address只占一个字节,默认为0x01
    cmake -DUNICAST_ADDRESS=/your/unicast_ip_addr ..
    cmake -DDIAGNOSIS_ADDRESS=/your/diagnosis_addr ..
    
  • 使用DEFAULT_CONFIGURATION_FOLDER: 默认使用的配置文件夹所在路径, 默认的default configuration folder路径是/etc/vsomeip
    cmake -DDEFAULT_CONFIGURATION_FOLDER=/your/default/configuration_folder ..
    
  • 使用DEFAULT_CONFIGURATION_FILE:默认使用的配置文件路径,默认的配置文件是/etc/vsomeip.json
    cmake -DDEFAULT_CONFIGURATION_FILE=/your/default/configuration_file ..
    
  • 使用ENABLE_SIGNAL_HANDLING:如果没有使用自定义去处理信号,可以使用vsomeip自带的信号处理.该选项会通过定义宏VSOMEIP_ENABLE_SIGNAL_HANDLING,来启用vsomeip自己的信号处理代码
    cmake -DENABLE_SIGNAL_HANDLING=1 ..
    
  • 使用ROUTING_READY_MESSAGE:可以自定义一些log,当IP路由准备好发送和接收message,可以执行cmake
    cmake -DROUTING_READY_MESSAGE=/your/message ..
    
  • 使用ENABLE_CONFIGURATION_OVERLAYS
    cmake -DENABLE_CONFIGURATION_OVERLAYS=1 ..
    
最终编译成果物
cd  build/install
tree            
.
├── etc
│   └── vsomeip
│       ├── vsomeip-local.json
│       ├── vsomeip-tcp-client.json
│       ├── vsomeip-tcp-service.json
│       ├── vsomeip-udp-client.json
│       ├── vsomeip-udp-service.json
│       └── vsomeip.json
├── include
│   ├── compat
│   │   └── vsomeip
│   │       ├── application.hpp
│   │       ├── constants.hpp
│   │       ├── defines.hpp
│   │       ├── enumeration_types.hpp
│   │       ├── error.hpp
│   │       ├── export.hpp
│   │       ├── function_types.hpp
│   │       ├── handler.hpp
│   │       ├── internal
│   │       │   ├── deserializable.hpp
│   │       │   └── serializable.hpp
│   │       ├── message.hpp
│   │       ├── message_base.hpp
│   │       ├── payload.hpp
│   │       ├── plugin.hpp
│   │       ├── plugins
│   │       │   ├── application_plugin.hpp
│   │       │   └── pre_configuration_plugin.hpp
│   │       ├── primitive_types.hpp
│   │       ├── runtime.hpp
│   │       ├── trace.hpp
│   │       └── vsomeip.hpp
│   └── vsomeip
│       ├── application.hpp
│       ├── constants.hpp
│       ├── defines.hpp
│       ├── deprecated.hpp
│       ├── enumeration_types.hpp
│       ├── error.hpp
│       ├── export.hpp
│       ├── function_types.hpp
│       ├── handler.hpp
│       ├── internal
│       │   ├── deserializable.hpp
│       │   ├── logger.hpp
│       │   ├── policy_manager.hpp
│       │   └── serializable.hpp
│       ├── message.hpp
│       ├── message_base.hpp
│       ├── payload.hpp
│       ├── plugin.hpp
│       ├── plugins
│       │   ├── application_plugin.hpp
│       │   └── pre_configuration_plugin.hpp
│       ├── primitive_types.hpp
│       ├── runtime.hpp
│       ├── structured_types.hpp
│       ├── trace.hpp
│       ├── vsomeip.hpp
│       └── vsomeip_sec.h
└── lib
    ├── cmake
    │   └── vsomeip3
    │       ├── vsomeip3Config.cmake
    │       ├── vsomeip3ConfigVersion.cmake
    │       ├── vsomeip3Targets-relwithdebinfo.cmake
    │       └── vsomeip3Targets.cmake
    ├── libvsomeip3-cfg.so -> libvsomeip3-cfg.so.3
    ├── libvsomeip3-cfg.so.3 -> libvsomeip3-cfg.so.3.4.10
    ├── libvsomeip3-cfg.so.3.4.10
    ├── libvsomeip3-e2e.so -> libvsomeip3-e2e.so.3
    ├── libvsomeip3-e2e.so.3 -> libvsomeip3-e2e.so.3.4.10
    ├── libvsomeip3-e2e.so.3.4.10
    ├── libvsomeip3-sd.so -> libvsomeip3-sd.so.3
    ├── libvsomeip3-sd.so.3 -> libvsomeip3-sd.so.3.4.10
    ├── libvsomeip3-sd.so.3.4.10
    ├── libvsomeip3.so -> libvsomeip3.so.3
    ├── libvsomeip3.so.3 -> libvsomeip3.so.3.4.10
    ├── libvsomeip3.so.3.4.10
    └── pkgconfig
        └── vsomeip3.pc

运行demo程序(Request/Response)

  • 工程目录组织
    ├── 3rdparty
    │   └── vsomeip
    │       ├── etc
    │       │   └── vsomeip
    │       │       ├── vsomeip-local.json
    │       │       ├── vsomeip-tcp-client.json
    │       │       ├── vsomeip-tcp-service.json
    │       │       ├── vsomeip-udp-client.json
    │       │       ├── vsomeip-udp-service.json
    │       │       └── vsomeip.json
    │       ├── include
    │       │   ├── compat
    │       │   │   └── vsomeip
    │       │   │       ├── application.hpp
    │       │   │       ├── constants.hpp
    │       │   │       ├── defines.hpp
    │       │   │       ├── enumeration_types.hpp
    │       │   │       ├── error.hpp
    │       │   │       ├── export.hpp
    │       │   │       ├── function_types.hpp
    │       │   │       ├── handler.hpp
    │       │   │       ├── internal
    │       │   │       │   ├── deserializable.hpp
    │       │   │       │   └── serializable.hpp
    │       │   │       ├── message.hpp
    │       │   │       ├── message_base.hpp
    │       │   │       ├── payload.hpp
    │       │   │       ├── plugin.hpp
    │       │   │       ├── plugins
    │       │   │       │   ├── application_plugin.hpp
    │       │   │       │   └── pre_configuration_plugin.hpp
    │       │   │       ├── primitive_types.hpp
    │       │   │       ├── runtime.hpp
    │       │   │       ├── trace.hpp
    │       │   │       └── vsomeip.hpp
    │       │   └── vsomeip
    │       │       ├── application.hpp
    │       │       ├── constants.hpp
    │       │       ├── defines.hpp
    │       │       ├── deprecated.hpp
    │       │       ├── enumeration_types.hpp
    │       │       ├── error.hpp
    │       │       ├── export.hpp
    │       │       ├── function_types.hpp
    │       │       ├── handler.hpp
    │       │       ├── internal
    │       │       │   ├── deserializable.hpp
    │       │       │   ├── logger.hpp
    │       │       │   ├── policy_manager.hpp
    │       │       │   └── serializable.hpp
    │       │       ├── message.hpp
    │       │       ├── message_base.hpp
    │       │       ├── payload.hpp
    │       │       ├── plugin.hpp
    │       │       ├── plugins
    │       │       │   ├── application_plugin.hpp
    │       │       │   └── pre_configuration_plugin.hpp
    │       │       ├── primitive_types.hpp
    │       │       ├── runtime.hpp
    │       │       ├── structured_types.hpp
    │       │       ├── trace.hpp
    │       │       ├── vsomeip.hpp
    │       │       └── vsomeip_sec.h
    │       └── lib
    │           ├── libvsomeip3-cfg.so -> libvsomeip3-cfg.so.3
    │           ├── libvsomeip3-cfg.so.3 -> libvsomeip3-cfg.so.3.4.10
    │           ├── libvsomeip3-cfg.so.3.4.10
    │           ├── libvsomeip3-e2e.so -> libvsomeip3-e2e.so.3
    │           ├── libvsomeip3-e2e.so.3 -> libvsomeip3-e2e.so.3.4.10
    │           ├── libvsomeip3-e2e.so.3.4.10
    │           ├── libvsomeip3-sd.so -> libvsomeip3-sd.so.3
    │           ├── libvsomeip3-sd.so.3 -> libvsomeip3-sd.so.3.4.10
    │           ├── libvsomeip3-sd.so.3.4.10
    │           ├── libvsomeip3.so -> libvsomeip3.so.3
    │           ├── libvsomeip3.so.3 -> libvsomeip3.so.3.4.10
    │           └── libvsomeip3.so.3.4.10
    ├── CMakeLists.txt
    ├── build.sh
    ├── client_example.cpp
    └── service_example.cpp
    
  • client_example.cpp
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <condition_variable>
    #include <thread>
    #include <vsomeip/vsomeip.hpp>
    
    #define SAMPLE_SERVICE_ID  0x1234
    #define SAMPLE_INSTANCE_ID 0x5678
    #define SAMPLE_METHOD_ID   0x0421
    
    std::shared_ptr<vsomeip::application> app;
    std::mutex                            mutex;
    std::condition_variable               condition;
    
    void run()
    {
        std::unique_lock<std::mutex> its_lock(mutex);
    
        condition.wait(its_lock);
        std::shared_ptr<vsomeip::message> request;
    
        request = vsomeip::runtime::get()->create_request();
        request->set_service(SAMPLE_SERVICE_ID);
        request->set_instance(SAMPLE_INSTANCE_ID);
        request->set_method(SAMPLE_METHOD_ID);
    
        std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()->create_payload();
        std::vector<vsomeip::byte_t>      its_payload_data;
    
        for (vsomeip::byte_t i = 0; i < 10; i++) {
            its_payload_data.push_back(i % 256);
        }
    
        its_payload->set_data(its_payload_data);
        request->set_payload(its_payload);
        app->send(request);
    }
    
    void on_message(const std::shared_ptr<vsomeip::message>& _response)
    {
        std::shared_ptr<vsomeip::payload> its_payload = _response->get_payload();
        vsomeip::length_t                 l           = its_payload->get_length();
    
        // Get payload
        std::stringstream ss;
    
        for (vsomeip::length_t i = 0; i < l; i++) {
            ss << std::setw(2) << std::setfill('0') << std::hex << (int)*(its_payload->get_data() + i) << " ";
        }
    
        std::cout << "CLIENT: Received message with Client/Session [" << std::setw(4) << std::setfill('0') << std::hex
                  << _response->get_client() << "/" << std::setw(4) << std::setfill('0') << std::hex
                  << _response->get_session() << "] " << ss.str() << std::endl;
    }
    
    void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available)
    {
        std::cout << "CLIENT: Service [" << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance
                  << "] is " << (_is_available ? "available." : "NOT available.") << std::endl;
        condition.notify_one();
    }
    
    int main(int argc, char** argv)
    {
        app = vsomeip::runtime::get()->create_application("Hello");
        app->init();
        app->register_availability_handler(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, on_availability);
        app->request_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
        app->register_message_handler(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_METHOD_ID, on_message);
        std::thread sender(run);
        app->start();
    }
    
  • service_example.cpp
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <vsomeip/vsomeip.hpp>
    
    #define SAMPLE_SERVICE_ID  0x1234
    #define SAMPLE_INSTANCE_ID 0x5678
    #define SAMPLE_METHOD_ID   0x0421
    
    std::shared_ptr<vsomeip::application> app;
    
    void on_message(const std::shared_ptr<vsomeip::message>& _request)
    {
        std::shared_ptr<vsomeip::payload> its_payload = _request->get_payload();
        vsomeip::length_t                 l           = its_payload->get_length();
    
        // Get payload
        std::stringstream ss;
        for (vsomeip::length_t i = 0; i < l; i++) {
            ss << std::setw(2) << std::setfill('0') << std::hex << (int)*(its_payload->get_data() + i) << " ";
        }
    
        std::cout << "SERVICE: Received message 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() << "] " << ss.str() << std::endl;
    
        // Create response
        std::shared_ptr<vsomeip::message> its_response = vsomeip::runtime::get()->create_response(_request);
        its_payload                                    = vsomeip::runtime::get()->create_payload();
        std::vector<vsomeip::byte_t> its_payload_data;
        for (int i = 9; i >= 0; i--) {
            its_payload_data.push_back(i % 256);
        }
        its_payload->set_data(its_payload_data);
        its_response->set_payload(its_payload);
        app->send(its_response);
    }
    
    int main(int argc, char** argv)
    {
        app = vsomeip::runtime::get()->create_application("World");
        app->init();
        app->register_message_handler(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_METHOD_ID, on_message);
        app->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
        app->start();
    
        return 0;
    }
    
  • build.sh
    #!/bin/bash
    
    BUILD_DIR=build
    if [ ! -d "${BUILD_DIR}" ]; then
      mkdir "${BUILD_DIR}"
      echo "creating build dir: ${BUILD_DIR} ..."
    else
      echo "build dir: ${BUILD_DIR} directory exist! ..."
    fi
    
    cd "${BUILD_DIR}" && pwd && cmake ..  && make -j4
    
  • 运行程序
    // 打开一个终端
    cd /your/vsomeip/demo/project/3rdparty/vsomeip/lib
    export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    // 启动程序
    ./client-example 
    // or
    env VSOMEIP_CONFIGURATION=../3rdparty/vsomeip/etc/vsomeip/vsomeip-local.json \
    VSOMEIP_APPLICATION_NAME=client-example \
    ./client-example
    
    // 打开另一个终端
    cd /your/vsomeip/demo/project/3rdparty/vsomeip/lib
    export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    // 启动程序
    ./service-example
    // or
    env VSOMEIP_CONFIGURATION=../3rdparty/vsomeip/etc/vsomeip/vsomeip-local.json \
    VSOMEIP_APPLICATION_NAME=service-example \
    ./service-example
    
  • 运行结果如下所示
    请添加图片描述

vsomeip应用和使用环境变量

vsomeip应用启动时以下的环境变量会被读取:

  • VSOMEIP_APPLICATION_NAME
    赋予当前程序在vsomeip中使用的名字。vsomeip会通过该名字在配置文件中进行匹配查找。该名字与二进制可执行文件的名字是不一样的。
  • VSOMEIP_CONFIGURATION
    vsomeip默认会使用配置文件/etc/vsomeip.json或者包含配置文件的文件夹/etc/vsomeip。可以通过该变量使vsomeip使用自定义的配置文件
  • VSOMEIP_MANDATORY_CONFIGURATION_FILES
    vsomeip允许使用mandatory配置文件来加快应用的启动速度(此时,除负责连接某些外部设别的程序之外,其他所有程序运行时都需要按照mandatory配置文件工作)。默认mandatory配置文件是:vsomeip_std.json, vsomeip_app.json和vsomeip_plc.json
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

血_影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值