当前位置: 首页 > news >正文

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

觉得有帮助的话,打赏一下呗。。

           

需要商务合作(定制程序)的欢迎私信!! 

http://www.dtcms.com/a/407895.html

相关文章:

  • Linux指令和Windows的有啥不一样?咋用的?
  • 操作系统(二) :操作系统运行机制(中断和异常、系统调用)
  • 高精度组合惯导技术与IMU传感器价格及供应商分析
  • 网站文字优化方案网络规划与设计报告总结
  • C++ 拓扑排序
  • Ubuntu 24.04.3搭建redis哨兵模式
  • Swift 入门(一 - 基础语法)
  • GCMSCNN 模块:气相色谱 - 质谱数据的分子特征提取方案
  • 简单网页制作模板图片福州seo兼职
  • 新德通:深耕光通信领域,打造全场景网络连接解决方案
  • 汽车网站模板免费下载做网站的注意点
  • COMSOL建立Voronoi泰森多边形二维模型
  • springboot - 邮箱验证码登录
  • 百度收录什么网站京东网站建设的策划书
  • 进程的概念(上)
  • 高仿id97网站模板涞水县建设局网站
  • 网站数据库结构被删了怎么办一键优化为什么不能100
  • 余姚建设网站的公司网站new图标
  • 重庆网站推广流程php5mysql网站开发实例精讲
  • 小型商城网站没技术怎么做网站
  • 网络公司网站模版视频网站做app开发的
  • 北京建站管理系统开发中企动力中山分公司网站
  • 手机端企业网站模板网页怎么设计与制作
  • 代码随想录算法训练营第21天 -- 回溯4 || 491.非递减子序列 / 46.全排列 /47.全排列 II
  • 绍兴公司网站建设 中企动力绍兴wordpress edc
  • 怎么找到做网站的客户岳阳网站建设团队
  • Android匿名共享内存突破Binder传递大小限制
  • 网络网站推广首荐乐云seowordpress关键字内链
  • 元气森林宇宙大赛——第五届高校创新挑战赛
  • 51的烧录与调试