Envoy 物联网模块开发---串口服务器 (一)

2023-11-22 15:59

本文主要是介绍Envoy 物联网模块开发---串口服务器 (一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、背景

最近业余时间想基于Envoy 开发一个串口网关,主要是想把一些 modbus、bacnet 以及 mqtt 等物联网协议接入Envoy中,当读到串口数据后可以转发成对应的网络协议

二、Envoy的优势

选择Envoy的话主要是因为Envoy的代码已经十分健全了,零信任、连接池、DNS解析、健康检查、集群调度等等Envoy都支持的很完善了,思来想去还是决定在Envoy基础上走二开,Envoy应该是C++里写的最好的网关了,内存小,而且扩展性极强。

三、Envoy在物联网方面的劣势

但是Envoy也有一些缺点

1、Envoy是基于互联网的网关对物联网模块支持不足、对物联网协议支持的不多

2、Envoy 代码巨大,开发难度和成本非常的高,开发起来非常的困难以及复杂,对技术要求十分的高。

3、Envoy代码巨大,变动一个文件就可能要几个小时

4、Envoy当前ListenerManager 并没有很好的扩展性,甚至在Bazel 文件里可见性只有几个模块,而且只支持UDP和TCP两种通信,要加一个串口通信难度并不小。

四、我们该怎么做?

尽量不要自己写基本的串口代码,使用第三方库libserialport

libserial

串口的第三方库

https://github.com/crayzeewulf/libserial

我们需要做的事情是两部:

1、加入新的listener

2、引入第三方串口库

1、加入新的listener

Envoy本身是不支持Listener模块扩展的,只支持Filter,Listener模块如果我们想扩展,就需要动ListenerManager的代码,动刀需要谨慎,所以我拷贝出来一份ListenerMangaer

 

核心代码改动:

修改配置下发格式为:

listeners:- name: listener_0address:socket_address:address: 0.0.0.0port_value: 10001rtu:path: /dev/ttyS0filter_chains:- filters:- name: envoy.filters.network.http_connection_manager

修改proto配置文件

api/envoy/config/core/v3/address.proto

加入串口的Rtu形式:


// Addresses specify either a logical or physical address and port, which are
// used to tell Envoy where to bind/listen, connect to upstream and find
// management servers.
message Address {option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.core.Address";oneof address {option (validate.required) = true;SocketAddress socket_address = 1;Pipe pipe = 2;// Specifies a user-space address handled by :ref:`internal listeners// <envoy_v3_api_field_config.listener.v3.Listener.internal_listener>`.EnvoyInternalAddress envoy_internal_address = 3;Rtu rtu = 4;}
}message Rtu {option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.core.Rtu";// Unix Domain Socket path. On Linux, paths starting with '@' will use the// abstract namespace. The starting '@' is replaced by a null byte by Envoy.// Paths starting with '@' will result in an error in environments other than// Linux.string path = 1 [(validate.rules).string = {min_len: 1}];// The mode for the Rtu. Not applicable for abstract sockets.uint32 mode = 2 [(validate.rules).uint32 = {lte: 511}];
}

source/common/network/utility.cc

适配串口:

    case envoy::config::core::v3::SocketAddress::UDP:return Socket::Type::Datagram;case envoy::config::core::v3::SocketAddress::SERIAL:return Socket::Type::Stream;}}

由于watchermen listener manager 原来Envoy代码不能用了,需要我们扩展,所以注释掉原来Envoy的ListenerManager入口


//REGISTER_FACTORY(DefaultListenerManagerFactoryImpl, ListenerManagerFactory);

加入新的串口实例化方式

source/common/network/utility.cc

    return std::make_shared<Address::EnvoyInternalInstance>(proto_address.envoy_internal_address().server_listener_name(),proto_address.envoy_internal_address().endpoint_id());case envoy::config::core::v3::Address::AddressCase::kRtu:return std::make_shared<Address::RtuInstance>(proto_address.rtu().path(),proto_address.rtu().mode());case envoy::config::core::v3::Address::AddressCase::ADDRESS_NOT_SET:PANIC_DUE_TO_PROTO_UNSET;}

 case envoy::config::core::v3::Address::AddressCase::kEnvoyInternalAddress:// Currently internal address supports stream operation only.return Socket::Type::Stream;case envoy::config::core::v3::Address::AddressCase::kRtu:return Socket::Type::Stream;case envoy::config::core::v3::Address::AddressCase::ADDRESS_NOT_SET:PANIC_DUE_TO_PROTO_UNSET;}
/*** Implementation of a pipe address (unix domain socket on unix).*/
class RtuInstance : public InstanceBase {
public:/*** Construct from an existing unix address.*/explicit RtuInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode = 0,const SocketInterface* sock_interface = nullptr);/*** Construct from a string pipe path.*/explicit RtuInstance(const std::string& pipe_path, mode_t mode = 0,const SocketInterface* sock_interface = nullptr);static absl::Status validateProtocolSupported() { return absl::OkStatus(); }// Network::Address::Instancebool operator==(const Instance& rhs) const override;const Ip* ip() const override { return nullptr; }const Pipe* pipe() const override { return &pipe_; }const EnvoyInternalAddress* envoyInternalAddress() const override { return nullptr; }const sockaddr* sockAddr() const override {return reinterpret_cast<const sockaddr*>(&pipe_.address_);}const sockaddr_un& getSockAddr() const { return pipe_.address_; }socklen_t sockAddrLen() const override {if (pipe_.abstract_namespace_) {return offsetof(struct sockaddr_un, sun_path) + pipe_.address_length_;}return sizeof(pipe_.address_);}absl::string_view addressType() const override { return "default"; }private:/*** Construct from an existing unix address.* Store the error status code in passed in parameter instead of throwing.* It is called by the factory method and the partially constructed instance will be discarded* upon error.*/RtuInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len, mode_t mode = 0,const SocketInterface* sock_interface = nullptr);struct PipeHelper : public Pipe {bool abstractNamespace() const override { return abstract_namespace_; }mode_t mode() const override { return mode_; }sockaddr_un address_;// For abstract namespaces.bool abstract_namespace_{false};uint32_t address_length_{0};mode_t mode_{0};};absl::Status initHelper(const sockaddr_un* address, mode_t mode);PipeHelper pipe_;friend class InstanceFactory;
};
//RTU
RtuInstance::RtuInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode,const SocketInterface* sock_interface): InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {if (address->sun_path[0] == '\0') {
#if !defined(__linux__)throw EnvoyException("Abstract AF_UNIX sockets are only supported on linux.");
#endifRELEASE_ASSERT(static_cast<unsigned int>(ss_len) >= offsetof(struct sockaddr_un, sun_path) + 1,"");pipe_.abstract_namespace_ = true;pipe_.address_length_ = ss_len - offsetof(struct sockaddr_un, sun_path);}absl::Status status = initHelper(address, mode);throwOnError(status);
}RtuInstance::RtuInstance(const std::string& pipe_path, mode_t mode,const SocketInterface* sock_interface): InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {if (pipe_path.size() >= sizeof(pipe_.address_.sun_path)) {throw EnvoyException(fmt::format("Path \"{}\" exceeds maximum UNIX domain socket path size of {}.", pipe_path,sizeof(pipe_.address_.sun_path)));}memset(&pipe_.address_, 0, sizeof(pipe_.address_));pipe_.address_.sun_family = AF_UNIX;if (pipe_path[0] == '@') {// This indicates an abstract namespace.// In this case, null bytes in the name have no special significance, and so we copy all// characters of pipe_path to sun_path, including null bytes in the name. The pathname must also// be null terminated. The friendly name is the address path with embedded nulls replaced with// '@' for consistency with the first character.
#if !defined(__linux__)throw EnvoyException("Abstract AF_UNIX sockets are only supported on linux.");
#endifif (mode != 0) {throw EnvoyException("Cannot set mode for Abstract AF_UNIX sockets");}pipe_.abstract_namespace_ = true;pipe_.address_length_ = pipe_path.size();// The following statement is safe since pipe_path size was checked at the beginning of this// functionmemcpy(&pipe_.address_.sun_path[0], pipe_path.data(), pipe_path.size()); // NOLINT(safe-memcpy)pipe_.address_.sun_path[0] = '\0';pipe_.address_.sun_path[pipe_path.size()] = '\0';friendly_name_ = friendlyNameFromAbstractPath(absl::string_view(pipe_.address_.sun_path, pipe_.address_length_));} else {// Throw an error if the pipe path has an embedded null character.if (pipe_path.size() != strlen(pipe_path.c_str())) {throw EnvoyException("UNIX domain socket pathname contains embedded null characters");}StringUtil::strlcpy(&pipe_.address_.sun_path[0], pipe_path.c_str(),sizeof(pipe_.address_.sun_path));friendly_name_ = pipe_.address_.sun_path;}pipe_.mode_ = mode;
}RtuInstance::RtuInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len,mode_t mode, const SocketInterface* sock_interface): InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {if (address->sun_path[0] == '\0') {
#if !defined(__linux__)error = absl::FailedPreconditionError("Abstract AF_UNIX sockets are only supported on linux.");return;
#endifRELEASE_ASSERT(static_cast<unsigned int>(ss_len) >= offsetof(struct sockaddr_un, sun_path) + 1,"");pipe_.abstract_namespace_ = true;pipe_.address_length_ = ss_len - offsetof(struct sockaddr_un, sun_path);}error = initHelper(address, mode);
}bool RtuInstance::operator==(const Instance& rhs) const { return asString() == rhs.asString(); }absl::Status RtuInstance::initHelper(const sockaddr_un* address, mode_t mode) {pipe_.address_ = *address;if (pipe_.abstract_namespace_) {if (mode != 0) {return absl::FailedPreconditionError("Cannot set mode for Abstract AF_UNIX sockets");}// Replace all null characters with '@' in friendly_name_.friendly_name_ = friendlyNameFromAbstractPath(absl::string_view(pipe_.address_.sun_path, pipe_.address_length_));} else {friendly_name_ = address->sun_path;}pipe_.mode_ = mode;return absl::OkStatus();
}

加入新的常量:

enum class Type { Ip, Pipe, EnvoyInternal, Rtu };

二、引入第三方串口库

第三方串口库我使用的是

GitHub - crayzeewulf/libserial: Serial Port Programming in C++

定义iot.bzl

load("@envoy_api//bazel:envoy_http_archive.bzl", "envoy_http_archive")
load("@envoy_api//bazel:external_deps.bzl", "load_repository_locations")
load("repository_locations.bzl", "WATCHERMEN_REPOSITORY_LOCATIONS_SPEC")# archives, e.g. cares.
def _build_all_content(exclude = []):return """filegroup(name = "all", srcs = glob(["**"], exclude={}), visibility = ["//visibility:public"])""".format(repr(exclude))BUILD_ALL_CONTENT = _build_all_content()WATCHERMEN_REPOSITORY_LOCATIONS = load_repository_locations(WATCHERMEN_REPOSITORY_LOCATIONS_SPEC)# Use this macro to reference any HTTP archive from bazel/repository_locations.bzl.
def external_http_archive(name, **kwargs):envoy_http_archive(name,locations = WATCHERMEN_REPOSITORY_LOCATIONS,**kwargs)def watchermen_iot_dependencies():external_http_archive(name = "com_github_serial",build_file_content = BUILD_ALL_CONTENT,)native.bind(name = "serial",actual = "//bazel/foreign_cc:serial",)

 定义仓库字典:

# This should match the schema defined in external_deps.bzl.WATCHERMEN_REPOSITORY_LOCATIONS_SPEC = dict(com_github_serial = dict(project_name = "serial",project_desc = "C library for serial port",project_url = "https://github.com/crayzeewulf/libserial",version = "master",strip_prefix = "libserial-{version}",# urls = ["https://github.com/crayzeewulf/libserial/archive/refs/tags/v{version}.tar.gz"],urls = ["https://github.com/crayzeewulf/libserial/archive/refs/heads/master.zip"],release_date = "2022-05-29",sha256 = "9f0c6137e56027d496a205072c527d47f552d4c170f24ae5cea2668da54e2a1b",use_category = ["dataplane_core"],cpe = "cpe:2.3:a:c-serial_project:c-serial:*",license = "libserial",license_url = "https://github.com/crayzeewulf/libserial/blob/master/LICENSE.txt",),
)

加入bazel cmake

envoy_cmake(name = "serial",lib_source = "@com_github_serial//:all",cache_entries = {# "CMAKE_INSTALL_LIBDIR": "lib",# "CMAKE_CXX_COMPILER_FORCED": "on","LIBSERIAL_ENABLE_TESTING": "off","LIBSERIAL_BUILD_EXAMPLES": "off",},# linkopts = select({#     # "//bazel:apple": ["-lresolv"],#     "//conditions:default": [],# }),cmake_files_dir = "$BUILD_TMPDIR/",out_static_libs = select({"//conditions:default": ["libserial.a"],}),# postfix_script = select({#     # "//bazel:windows_x86_64": "cp -L $EXT_BUILD_ROOT/external/com_github_libserial/src/lib/ares_nameser.h $INSTALLDIR/include/ares_nameser.h && cp -L $EXT_BUILD_ROOT/external/com_github_c_ares_c_ares/include/ares_dns.h $INSTALLDIR/include/ares_dns.h",#     # "//conditions:default": "rm -f $INSTALLDIR/include/ares_dns.h && cp -L $EXT_BUILD_ROOT/external/com_github_c_ares_c_ares/include/ares_dns.h $INSTALLDIR/include/ares_dns.h",# }),
)

 

在模块中引入Envoy 

envoy_cc_library(name = "watchermen_iot_factory_lib",hdrs = ["watchermen_rtu_socket_handle.h","watchermen_rtu_listener_socket.h",],srcs = ["watchermen_rtu_listener_socket.cc","watchermen_rtu_socket_handle.cc",],repository = "@envoy",external_deps = ["serial"],deps = [# "//external:serial"],
)

这篇关于Envoy 物联网模块开发---串口服务器 (一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/411287

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基