简体   繁体   English

使用protobuf和STM32F7设计协议

[英]Designing a protocol using protobuf and STM32F7

I'm trying to do the same protocol that is defined and described here in that wiki https://wiki.trezor.io/Developers_guide-Message_Workflows 我正在尝试执行与该Wiki https://wiki.trezor.io/Developers_guide-Message_Workflows中此处定义和描述的协议相同的协议

My toolset is Protobuf for embedded systems: Nanopb. 我的工具集是用于嵌入式系统的Protobuf:Nanopb。 STM32F7 using Serial port. STM32F7使用串行端口。

I'm trying to communicate right now between PC and STM32F7, and usually the communication will be done between two STM32F7 boards. 我现在正在尝试在PC和STM32F7之间进行通信,通常该通信将在两个STM32F7板之间进行。

My questions: 我的问题:

  1. What kind of protocol that is sufficient for a request and answer like the one in trezor ? 什么样的协议足以满足请求和响应的需求?

  2. I googled and I found I have to use something like HLDC, is it necessary for that purpose or it's an overhead ? 我用谷歌搜索,发现我必须使用类似HLDC的工具,这是否有必要,还是开销很大?

  3. Coding and design issue: I will fire a serial interrupt that always polling for the data that is communicated between two boards, now there will a very big state machine to decode each message type and do the event based on the message type. 编码和设计问题:我将触发一个串行中断,该中断总是轮询两块板之间通信的数据,现在将有一个非常大的状态机来解码每种消息类型并根据消息类型执行事件。 Is there an alternative design ? 有替代设计吗?

Firstly, AFAIK, nanopb doesn't support the full range of possibilities in the Protobuf schema language. 首先,AFAIK,nanopb不支持Protobuf模式语言的全部功能。 So you'll need a schema that works for nanopb, and hopefully that'll be good enough for the needs. 因此,您需要一个适用于nanopb的架构,并希望该架构足以满足需求。 However it can be very annoying as (so far as I know) the very useful oneof doesn't work. 然而,它可以是非常恼人的,因为(据我所知)非常有用的oneof不起作用。

Secondly, protobuf wireformat is not self delimiting. 其次,protobuf线格式不是自定界的。 So you'll be squirting data down the serial cable, but it's not possible to reliably (if at all) tell where one message ends and another starts. 因此,您将通过串行电缆发送数据,但是不可能(如果有的话)可靠地告知一条消息在哪里结束而另一条消息在哪里开始。 So you'll need to transmit some sort of inter-message sync pattern of bytes, chosen to be unlikely to be encountered in a message. 因此,您将需要传输某种消息间的字节同步模式,该模式在消息中不太可能遇到。 You'll have to read in the bytes inbetween sync patterns, place them in a buffer, and parse from that. 您必须读取同步模式之间的字节,将它们放置在缓冲区中,然后从中进行解析。

Thirdly, if you're sending a variety of different messages and you can't use oneof , then you'll need some other way of identifying what type of message has arrived so that you can parse it to the right type of object. 第三,如果您要发送各种不同的消息,并且不能使用oneof ,则需要使用其他方法来识别已到达的消息类型,以便将其解析为正确的对象类型。 That "way" could simply be a fixed sequence of message types, or a byte whose value identifies the message type, or a field that does the same thing in all the messages. 该“方式”可以简单地是消息类型的固定序列,或者是一个字节,其值标识消息类型,或者是在所有消息中都执行相同操作的字段。 oneof is attractive (though not available to you) because it can be used as a carrier for a variety of different message types; oneof很吸引人(尽管您无法使用),因为它可以用作各种不同消息类型的载体; you simply parse the received data using the oneof 's parser. 您只需使用oneof的解析器来解析接收到的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM