• 在20.04.4 LTS (Focal Fossa)学习CommonAPI-C---D-Bus


    Ubuntu环境

    $ cat /etc/os-release
    NAME="Ubuntu"
    VERSION="20.04.4 LTS (Focal Fossa)"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 20.04.4 LTS"
    VERSION_ID="20.04"
    HOME_URL="https://www.ubuntu.com/"
    SUPPORT_URL="https://help.ubuntu.com/"
    BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
    PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
    VERSION_CODENAME=focal
    UBUNTU_CODENAME=focal
     

     步骤1 下载依赖工具

    axel -n 10 https://github.com/COVESA/capicxx-core-tools/releases/download/3.2.0.1/commonapi_core_generator.zip
    axel -n 10 https://github.com/COVESA/capicxx-dbus-tools/releases/download/3.2.0/commonapi_dbus_generator.zip

    解压依赖工具并把依赖工具添加到PATH环境变量

    mkdir  commonapi_core_generator

    mkdir commonapi_dbus_generator

    unzip commonapi_core_generator.zip

    unzip commonapi_dbus_generator.zip

    export PATH=`realpath commonapi_core_generator`:`realpath commonapi_dbus_generator`:$PATH

     步骤2 Build the CommonAPI Runtime Library

    git clone https://github.com/GENIVI/capicxx-core-runtime.git
    cd capicxx-core-runtime/
    git checkout -b 3.2.0 3.2.0
    mkdir -p build
    mkdir -p build/_install
    cd build
    cmake -D CMAKE_INSTALL_PREFIX=${PWD}/_install ..
    make -j 8
    make install -j8

    步骤3  Build the CommonAPI D-Bus Runtime Library

    git clone https://github.com/GENIVI/capicxx-dbus-runtime.git

    axel -n 10 http://dbus.freedesktop.org/releases/dbus/dbus-1.4.10.tar.gz
    tar -xzf dbus-1.4.10.tar.gz
    cd dbus-1.4.10/
    for i in ../capicxx-dbus-runtime/src/dbus-patches/*.patch; do patch -p1 < $i; done 这个一步有个patch失败,需要手动加这个patch
    ./configure --prefix=${PWD}/_install
    make -C dbus -j 8
    make -C dbus install -j 8
    make install-pkgconfigDATA
     

    cd capicxx-dbus-runtime

    git checkout -b 3.2.0 3.2.0
    mkdir build
    cd build
    export PKG_CONFIG_PATH=`realpath ../dbus-1.4.10/_install/lib/pkgconfig`
    cmake -DCMAKE_CXX_FLAGS=-DDBUS_TIMEOUT_INFINITE=5000 -DUSE_INSTALLED_COMMONAPI=OFF -DUSE_INSTALLED_DBUS=ON ..
    make -j 8

     步骤4 Write the Franca file and generate code

    mkdir project
    cd project
    mkdir fidl
    cat > fidl/HelloWorld.fidl << EOF
    package commonapi

    interface HelloWorld {
      version {major 1 minor 0}
      method sayHello {
        in {
          String name
        }
        out {
          String message
        }
      }
    }
    EOF

    步骤5  Write the client and the service application

    mkdir src
    cat > src/HelloWorldClient.cpp << EOF
    // HelloWorldClient.cpp
    #include
    #include
    #include
    #include
    #include

    using namespace v1_0::commonapi;

    int main() {
        std::shared_ptr < CommonAPI::Runtime > runtime = CommonAPI::Runtime::get();
        std::shared_ptr> myProxy =
            runtime->buildProxy("local", "test");

        std::cout << "Checking availability!" << std::endl;
        while (!myProxy->isAvailable())
            usleep(10);
        std::cout << "Available..." << std::endl;

        CommonAPI::CallStatus callStatus;
        std::string returnMessage;
        myProxy->sayHello("Bob", callStatus, returnMessage);
        std::cout << "Got message: '" << returnMessage << "'\n";
        return 0;
    }
    EOF

    cat > src/HelloWorldService.cpp << EOF
    // HelloWorldService.cpp
    #include
    #include
    #include
    #include "HelloWorldStubImpl.hpp"

    using namespace std;

    int main() {
        std::shared_ptr runtime = CommonAPI::Runtime::get();
        std::shared_ptr myService =
            std::make_shared();
        runtime->registerService("local", "test", myService);
        std::cout << "Successfully Registered Service!" << std::endl;

        while (true) {
            std::cout << "Waiting for calls... (Abort with CTRL+C)" << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(30));
        }
        return 0;
     }
    EOF

    cat > src/HelloWorldStubImpl.hpp << EOF
    // HelloWorldStubImpl.hpp
    #ifndef HELLOWORLDSTUBIMPL_H_
    #define HELLOWORLDSTUBIMPL_H_
    #include
    #include

    class HelloWorldStubImpl: public v1_0::commonapi::HelloWorldStubDefault {
    public:
        HelloWorldStubImpl();
        virtual ~HelloWorldStubImpl();
        virtual void sayHello(const std::shared_ptr _client,
            std::string _name, sayHelloReply_t _return);
    };
    #endif /* HELLOWORLDSTUBIMPL_H_ */
    EOF

    cat > src/HelloWorldStubImpl.cpp << EOF
    // HelloWorldStubImpl.cpp
    #include "HelloWorldStubImpl.hpp"

    HelloWorldStubImpl::HelloWorldStubImpl() { }
    HelloWorldStubImpl::~HelloWorldStubImpl() { }

    void HelloWorldStubImpl::sayHello(const std::shared_ptr _client,
        std::string _name, sayHelloReply_t _reply) {
            std::stringstream messageStream;
            messageStream << "Hello " << _name << "!";
            std::cout << "sayHello('" << _name << "'): '" << messageStream.str() << "'\n";

        _reply(messageStream.str());
    };
    EOF
     

     步骤6 Build and run

    cat > CMakeLists.txt << EOF
    cmake_minimum_required(VERSION 2.8)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++0x")
    include_directories(
        src-gen
        $ENV{RUNTIME_PATH}/capicxx-core-runtime/include
        $ENV{RUNTIME_PATH}/capicxx-dbus-runtime/include
        $ENV{DBUS_PATH}
    )
    link_directories(
        $ENV{RUNTIME_PATH}/capicxx-core-runtime/build
        $ENV{RUNTIME_PATH}/capicxx-dbus-runtime/build
        $ENV{DBUS_PATH}/dbus/.libs
    )
    add_executable(HelloWorldClient
        src/HelloWorldClient.cpp
        src-gen/v1/commonapi/HelloWorldDBusProxy.cpp
        src-gen/v1/commonapi/HelloWorldDBusDeployment
    )
    target_link_libraries(HelloWorldClient CommonAPI CommonAPI-DBus dbus-1)
    add_executable(HelloWorldService
        src/HelloWorldService.cpp
        src/HelloWorldStubImpl.cpp
        src-gen/v1/commonapi/HelloWorldDBusStubAdapter.cpp
        src-gen/v1/commonapi/HelloWorldStubDefault
        src-gen/v1/commonapi/HelloWorldDBusDeployment
    )
    target_link_libraries(HelloWorldService CommonAPI CommonAPI-DBus dbus-1)
    EOF
    mkdir build && cd build
    export RUNTIME_PATH=`realpath ..` DBUS_PATH=`realpath ../dbus-1.4.10/`
    cmake ..
    make


    ./build/HelloWorldService &
    ./build/HelloWorldClient

    目录结构

     $ tree project -L 2
    project
    ├── build
    │   ├── CMakeCache.txt
    │   ├── CMakeFiles
    │   ├── cmake_install.cmake
    │   ├── HelloWorldClient
    │   ├── HelloWorldService
    │   └── Makefile
    ├── CMakeLists.txt
    ├── fidl
    │   └── HelloWorld.fidl
    ├── mk
    ├── src
    │   ├── HelloWorldClient.cpp
    │   ├── HelloWorldService.cpp
    │   ├── HelloWorldStubImpl.cpp
    │   └── HelloWorldStubImpl.hpp
    └── src-gen
        └── v1

    运行效果

     

    $ ./build/HelloWorldService &
    [1] 3233806
    $ [CAPI][INFO] Loading configuration file '/etc/commonapi.ini'
    [CAPI][INFO] Using default binding 'dbus'
    [CAPI][INFO] Using default shared library folder '/usr/local/lib/commonapi'
    Successfully Registered Service!
    Waiting for calls... (Abort with CTRL+C)

    $ ./build/HelloWorldClient
    [CAPI][INFO] Loading configuration file '/etc/commonapi.ini'
    [CAPI][INFO] Using default binding 'dbus'
    [CAPI][INFO] Using default shared library folder '/usr/local/lib/commonapi'
    [CAPI][INFO] subscribeAvailabilityListener service: commonapi.HelloWorld.v1_0_test objectPath: /test interface: commonapi.HelloWorld.v1_0
    Checking availability!
    Available...
    sayHello('Bob'): 'Hello Bob!'
    Got message: 'Hello Bob!'
    [CAPI][INFO] unsubscribeAvailabilityListener service: commonapi.HelloWorld.v1_0_test objectPath: /test interface: commonapi.HelloWorld.v1_0
    $ ./build/HelloWorldClient
    [CAPI][INFO] Loading configuration file '/etc/commonapi.ini'
    [CAPI][INFO] Using default binding 'dbus'
    [CAPI][INFO] Using default shared library folder '/usr/local/lib/commonapi'
    [CAPI][INFO] subscribeAvailabilityListener service: commonapi.HelloWorld.v1_0_test objectPath: /test interface: commonapi.HelloWorld.v1_0
    Checking availability!
    Available...
    sayHello('Bob'): 'Hello Bob!'
    Got message: 'Hello Bob!'
    [CAPI][INFO] unsubscribeAvailabilityListener service: commonapi.HelloWorld.v1_0_test objectPath: /test interface: commonapi.HelloWorld.v1_0
    $ fg
    ./build/HelloWorldService
    ^C
     


     参考:https://github.com/COVESA/capicxx-dbus-tools/wiki/CommonAPI-C---D-Bus-in-10-minutes

  • 相关阅读:
    qtablewidget 设置列宽行高遇到的问题
    ESP32设备驱动-OLED显示DHT11和DHT22传感器数据
    【MySQL】MySQL中的逻辑运算符,位运算符和运算符的优先级
    2022 IDEA大会引领科技创新趋势 沈向洋团队重磅发布低空经济白皮书
    基于 nodejs+vue旅游推荐系统 mysql
    配置高级 --------打包与运行---配置高级---多环境开发---日志
    网络通信错误代码列表 HTTP 、FTP
    java代码:Random和Scanner应用的小例子-猜数字小游戏
    R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
    NAT协议
  • 原文地址:https://blog.csdn.net/m0_37132481/article/details/126391014