linux 安装和使用CommonAPI
1、编译 CommonAPI C++ Core Runtime
下载commonAPI 的源代码,这是 CommonAPI 的核心运行时库。然后通过编译生成为CommonAPI运行时库:
下载:github下载、csdn下载
cd capicxx-core-runtime/
mkdir build
cd build/
cmake -D CMAKE_INSTALL_PREFIX=/home/gui/gui/build ..
make -j$(nproc)
make install
若报错误:
cc1plus: error: -Werror=extra-semi: no option -Wextra-semi
删除CMakeLists.txt文件的 -Werror=extra-semi 。
2、编译vsomeip
编译步骤:点击跳转
3、安装 CommonAPI C++ SOMEIP Runtime
这是 CommonAPI 用于 SOME/IP 绑定的运行时库。
下载:github下载、csdn下载
cmake -D USE_INSTALLED_COMMONAPI=ON -D CMAKE_INSTALL_PREFIX=/home/gui/gui/build ..
make -j$(nproc)
sudo make install
4、安装代码生成工具
这些工具用于根据你的接口定义文件(.fidl
和 .fdepl
)自动生成 C++ 代码。
commonapi-core-generator下载:csdn下载、github下载
commonapi-someip-generator下载:github下载、csdn下载
使用 CommonAPI
使用 CommonAPI 开发应用通常包含以下步骤:
1)创建 Franca IDL (.fidl
)和 Franca 部署 (.fdepl
)文件。
例如 HelloWorld.fidl
:
package commonapiinterface HelloWorld {version {major 1 minor 0}method sayHello {in {String name}out {String message}}
}
例如 HelloWorld.fdepl
(SOMEIP 绑定 specific 的部署信息):
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 = 4660method sayHello {SomeIpMethodID = 123}
}define org.genivi.commonapi.someip.deployment for provider as MyService {instance commonapi.HelloWorld {InstanceId = "test"SomeIpInstanceID = 22136}
}
2)生成代码
使用之前下载的代码生成器处理接口文件。
commonapi-core-generator-linux-x86_64 -sk ./fidl/HelloWorld.fidl
# 这会生成 CommonAPI 核心的代理和存根类(如 HelloWorldProxy.hpp, HelloWorldStub.hpp)commonapi-someip-generator-linux-x86_64 ./fidl/HelloWorld.fdepl
# 这会生成 SOME/IP 绑定的特定代码(如 HelloWorldSomeIPProxy.hpp, HelloWorldSomeIPStubAdapter.hpp)
生成的代码通常会放在一个 src-gen
目录中。你需要将这些生成的文件包含到你的项目中。
3)实现服务端和客户端
服务端需要实现生成的存根类(Stub)中的虚拟方法。
// 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
中需要实现 sayHello
方法:
// 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
#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());
};
客户端使用生成的代理类(Proxy)调用远程方法。
// 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;
}
g++ -o HelloWorldClient \src/HelloWorldClient.cpp \src-gen/v1/commonapi/HelloWorldSomeIPProxy.cpp \src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp \-pthread -std=c++0x \-I src-gen \-I /home/gui/gui/build/include/CommonAPI-3.2 \-I /home/gui/gui/build/include/vsomeip \-I /home/gui/gui/build/include \-L /home/gui/gui/build/lib \-lCommonAPI -lCommonAPI-SomeIP -lvsomeip3g++ -o HelloWorldService \src/HelloWorldService.cpp \src/HelloWorldStubImpl.cpp \src-gen/v1/commonapi/HelloWorldSomeIPStubAdapter.cpp \src-gen/v1/commonapi/HelloWorldSomeIPDeployment.cpp \-pthread -std=c++0x \-I src-gen \-I /home/gui/gui/build/include/CommonAPI-3.2 \-I /home/gui/gui/build/include/vsomeip \-I /home/gui/gui/build/include \-L /home/gui/gui/build/lib \-lCommonAPI -lCommonAPI-SomeIP -lvsomeip3
觉得有帮助的话,打赏一下呗。。
需要商务合作(定制程序)的欢迎私信!!