本文主要介绍车载以太网的 CommonAPI 的应用
user guide links
GitHub source code repo links the step 3 depends on the step 1
the step 1 need to install boost library first like following
1 $ sudo apt-get install libboost-all-dev
vsomeip
capicxx-core-runtime
capicxx-someip-runtime
this code generate tools depend on Java environment
1 $ sudo apt-get install openjdk-17-jdk
CommonAPI core header generate
CommonAPI source code generate
run helloworld big three
json config file
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 { "unicast" : "local" , "logging" : { "level" : "debug" , "console" : "true" } , "applications" : [ { "name" : "HelloWorldService" , "id" : "0x4444" } , { "name" : "HelloWorldClient" , "id" : "0x5555" } ] , "services" : [ { "service" : "0x1111" , "instance" : "0x2222" , "unreliable" : "30509" } ] , "routing" : "HelloWorldService" , "service-discovery" : { "enable" : "false" } }
HelloWorld.fidl
1 2 3 4 5 6 7 8 9 10 11 12 package commonapi interface HelloWorld { version {major 1 minor 0} method sayHello { in { String name } out { String message } } }
HelloWorld.fdepl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import "platform:/plugin/org.genivi.commonapi.someip/deployment/CommonAPI-SOMEIP_deployment_spec.fdepl" import "HelloWorld.fidl" define org.genivi.commonapi.someip.deployment for interface commonapi.HelloWorld { SomeIpServiceID = 4660 method sayHello { SomeIpMethodID = 3300 } } define org.genivi.commonapi.someip.deployment for provider as MyService { instance commonapi.HelloWorld { InstanceId = "test" SomeIpInstanceID = 2200 } }
then generate code
1 2 ➜ helloworld ../commonapi_someip_generator/commonapi-someip-generator-linux-x86_64 -ll verbos ./fidl/HelloWorld.fdepl
write your code
HelloWorldStubImpl.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #ifndef HELLOWORLDSTUBIMPL_H_ #define HELLOWORLDSTUBIMPL_H_ #include <CommonAPI/CommonAPI.hpp> #include <v1/commonapi/HelloWorldStubDefault.hpp> class HelloWorldStubImpl : public v1::commonapi::HelloWorldStubDefault {public : HelloWorldStubImpl (); virtual ~HelloWorldStubImpl (); virtual void sayHello (const std::shared_ptr<CommonAPI::ClientId> _client, std::string _name, sayHelloReply_t _return) ; }; #endif
HelloWorldStubImpl.cpp
1 2 3 4 5 6 7 8 9 10 11 12 #include "HelloWorldStubImpl.hpp" HelloWorldStubImpl::HelloWorldStubImpl () { } HelloWorldStubImpl::~HelloWorldStubImpl () { } void HelloWorldStubImpl::sayHello (const std::shared_ptr<CommonAPI::ClientId> _client, std::string _name, sayHelloReply_t _reply) { std::stringstream messageStream; messageStream << "Hello " << _name << "!" ; std::cout << "sayHello('" << _name << "'): '" << messageStream.str () << "'\n" ; _reply(messageStream.str ()); };
HelloWorldService.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> #include <thread> #include <CommonAPI/CommonAPI.hpp> #include "HelloWorldStubImpl.hpp" using namespace std;int main () { std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::get (); std::shared_ptr<HelloWorldStubImpl> myService = std::make_shared <HelloWorldStubImpl>(); bool res = 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 ; }
HelloWorldClient.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 #include <iostream> #include <string> #include <unistd.h> #include <CommonAPI/CommonAPI.hpp> #include <v1/commonapi/HelloWorldProxy.hpp> using namespace v1::commonapi;int main () { std::shared_ptr < CommonAPI::Runtime > runtime = CommonAPI::Runtime::get (); std::shared_ptr<HelloWorldProxy<>> myProxy = runtime->buildProxy <HelloWorldProxy>("local" , "test" ); std::cout << "Checking availability!" << std::endl; while (!myProxy->isAvailable ()) usleep (10 ); std::cout << "Available..." << std::endl; CommonAPI::CallStatus callStatus; std::string returnMessage; while (true ) { std::this_thread::sleep_for (std::chrono::seconds (1 )); myProxy->sayHello ("xiepeng" , callStatus, returnMessage); std::cout << "Got message: '" << returnMessage << "'\n" ; } return 0 ; }
cmake
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 cmake_minimum_required (VERSION 2.8 )project (helloworld)set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++0x" )include_directories ( /home/helianthus/dev_tools/helloworld/src-gen /home/helianthus/dev_tools/capicxx-core-runtime-3.2 .0 /include /home/helianthus/dev_tools/capicxx-someip-runtime-3.2 .0 /include /home/helianthus/dev_tools/vsomeip-3.1 .20.3 /interface ) link_directories ( /home/helianthus/dev_tools/capicxx-core-runtime-3.2 .0 /build /home/helianthus/dev_tools/capicxx-someip-runtime-3.2 .0 /build /home/helianthus/dev_tools/vsomeip-3.1 .20.3 /build ) add_executable (HelloWorldClient /home/helianthus/dev_tools/helloworld/HelloWorldClient.cpp /home/helianthus/dev_tools/helloworld/src-gen/v1/commonapi/HelloWorldSomeIPProxy.cpp /home/helianthus/dev_tools/helloworld/src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp ) target_link_libraries (HelloWorldClient CommonAPI CommonAPI-SomeIP vsomeip3)add_executable (HelloWorldService /home/helianthus/dev_tools/helloworld/HelloWorldService.cpp /home/helianthus/dev_tools/helloworld/HelloWorldStubImpl.cpp /home/helianthus/dev_tools/helloworld/src-gen/v1/commonapi/HelloWorldSomeIPStubAdapter.cpp /home/helianthus/dev_tools/helloworld/src-gen/v1/commonapi/HelloWorldStubDefault.hpp /home/helianthus/dev_tools/helloworld/src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp ) target_link_libraries (HelloWorldService CommonAPI CommonAPI-SomeIP vsomeip3)
踩的几个坑
fidl 文件高版本有变动 ~~define org.genivi.commonapi.someip.deployment for provider MyService {~~应该为 define org.genivi.commonapi.someip.deployment for provider as MyService {
sudo ldconfig 会报 Configuration module could not be loaded! 其实是动态库的问题
vsomeip ld 找不到,高版本 cmake 要链接 vsomeip3