Refer to COVESA wiki:
CommonAPI C SomeIP in 10 minutes · COVESA/capicxx-someip-tools Wiki · GitHub
Step 1: Preparation
Ubuntu 20.04.6
git, cmake, jre ...
DLT
https://github.com/COVESA/dlt-daemon
googletest...
Step 2: Build CommonAPI Runtime
$ git clone https://github.com/GENIVI/capicxx-core-runtime.git
$ cd capicxx-core-runtime/
<.>/capicxx-core-runtime$ mkdir build
<.>/capicxx-core-runtime$ cd build
<.>/capicxx-core-runtime/build$ cmake ..
<.>/capicxx-core-runtime/build$ make
Step 3: Build CommonAPI SOME/IP Runtime Library
$ git clone https://github.com/GENIVI/capicxx-someip-runtime.git
$ git clone https://github.com/GENIVI/vsomeip.git
Build vsomeip
$ cd vsomeip
<.>/vsomeip$ mkdir build
<.>/vsomeip$ cd build
<.>/vsomeip/build$ cmake -DENABLE_SIGNAL_HANDLING=1 -DDIAGNOSIS_ADDRESS=0x10 ..
<.>/vsomeip/build$ make
当 ENABLE_SIGNAL_HANDLING=1 时,启用 vsomeip (SIGINT/SIGTERM) 的信号处理;可以通过 ctrl-c 中止 vsomeip 应用程序。
Build CommonAPI C++ SOME/IP runtime library
$ cd capicxx-someip-runtime
<.>/capicxx-someip-runtime$ mkdir build
<.>/capicxx-someip-runtime$ cd build
<.>/capicxx-someip-runtime/build$ cmake -DUSE_INSTALLED_COMMONAPI=OFF ..
<.>/capicxx-someip-runtime/build$ make
Step 4: Write the Franca file and generate code
$ mkdir project
$ cd project/
<.>/project$ mkdir fidl src build
<.>/project$ cd fidl
<.>/project/fidl$ vi HelloWorld.fidl HelloWorld.fdepl
Interface: HelloWorld.fidl
package commonapi
interface HelloWorld {
version {major 1 minor 0}
method sayHello {
in {
String name
}
out {
String message
}
}
}
SOME/IP deployment file HelloWorld.fdepl
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 = 123
}
}
define org.genivi.commonapi.someip.deployment for provider as MyService {
instance commonapi.HelloWorld {
InstanceId = "test"
SomeIpInstanceID = 22136
}
}
Generate code
https://github.com/COVESA/capicxx-core-tools/releases/download/3.2.14/commonapi_core_generator.zip
<.>/project$ ../../cgen/commonapi_core_generator/commonapi-core-generator-linux-x86_64 -d src-gen/core -sk ./fidl/HelloWorld.fidl
<.>/project$ ../../cgen/commonapi_someip_generator/commonapi-someip-generator-linux-x86_64 -d src-gen/someip ./fidl/HelloWorld.fdepl
Step 5: Write the client and the service application
<.>/project$ cd src
<.>/project/src$ vi HelloWorldClient.cpp HelloWorldService.cpp HelloWorldStubImpl.cpp HelloWorldStubImpl.hpp
HelloWorldClient.cpp
// HelloWorldClient.cpp
#include <iostream>
#include <string>
#include <unistd.h>
#include <CommonAPI/CommonAPI.hpp>
#include <v1/commonapi/HelloWorldProxy.hpp>
using namespace v1_0::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;
myProxy->sayHello("Bob", callStatus, returnMessage);
std::cout << "Got message: '" << returnMessage << "'\n";
return 0;
}
HelloWorldService.cpp
// HelloWorldService.cpp
#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>();
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;
}
HelloWorldStubImpl.hpp
// HelloWorldStubImpl.hpp
#ifndef HELLOWORLDSTUBIMPL_H_
#define HELLOWORLDSTUBIMPL_H_
#include <CommonAPI/CommonAPI.hpp>
#include <v1/commonapi/HelloWorldStubDefault.hpp>
class HelloWorldStubImpl: public v1_0::commonapi::HelloWorldStubDefault {
public:
HelloWorldStubImpl();
virtual ~HelloWorldStubImpl();
virtual void sayHello(const std::shared_ptr<CommonAPI::ClientId> _client,
std::string _name, sayHelloReply_t _return);
};
#endif /* HELLOWORLDSTUBIMPL_H_ */
HelloWorldStubImpl.cpp
// HelloWorldStubImpl.cpp
#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());
};
Step 6: Build
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Commonapi_Someip)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++0x")
set(RUNTIME_PATH /xxx)
include_directories(
src-gen
$ENV{RUNTIME_PATH}/capicxx-core-runtime/include
$ENV{RUNTIME_PATH}/capicxx-someip-runtime/include
$ENV{RUNTIME_PATH}/vsomeip/interface
)
link_directories(
$ENV{RUNTIME_PATH}/capicxx-core-runtime/build
$ENV{RUNTIME_PATH}/capicxx-someip-runtime/build
$ENV{RUNTIME_PATH}/vsomeip/build
)
add_executable(HelloWorldClient
src/HelloWorldClient.cpp
src-gen/v1/commonapi/HelloWorldSomeIPProxy.cpp
src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp
)
target_link_libraries(HelloWorldClient CommonAPI CommonAPI-SomeIP vsomeip3)
add_executable(HelloWorldService
src/HelloWorldService.cpp
src/HelloWorldStubImpl.cpp
src-gen/v1/commonapi/HelloWorldSomeIPStubAdapter.cpp
#src-gen/v1/commonapi/HelloWorldStubDefault.cpp
src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp
)
target_link_libraries(HelloWorldService CommonAPI CommonAPI-SomeIP vsomeip3)
make
<.>/build$ cmake ..
<.>/build$ make
Step 7: Run
Run dlt-daemon
Copy vsomeip-local.json to project directory and run.
VSOMEIP_CONFIGURATION=../vsomeip-local.json \
VSOMEIP_APPLICATION_NAME=service-sample \
./HelloWorldService
VSOMEIP_CONFIGURATION=../vsomeip-local.json \
VSOMEIP_APPLICATION_NAME=client-sample \
./HelloWorldClient