資料介紹
軟件簡(jiǎn)介
TinyFrame
TinyFrame is a simple library for building and parsing data frames to be sent over a serial interface (e.g. UART, telnet, socket). The code is written to build with --std=gnu99
and mostly compatible with --std=gnu89
.
The library provides a high level interface for passing messages between the two peers. Multi-message sessions, response listeners, checksums, timeouts are all handled by the library.
TinyFrame is suitable for a wide range of applications, including inter-microcontroller communication, as a protocol for FTDI-based PC applications or for messaging through UDP packets.
The library lets you register listeners (callback functions) to wait for (1) any frame, (2) a particular frame Type, or (3) a specific message ID. This high-level API is general enough to implement most communication patterns.
TinyFrame is re-entrant and supports creating multiple instances with the limitation that their structure (field sizes and checksum type) is the same. There is a support for adding multi-threaded access to a shared instance using a mutex.
TinyFrame also comes with (optional) helper functions for building and parsing message payloads, those are provided in the utils/
folder.
Ports
TinyFrame has been ported to mutiple languages:
- The reference C implementation is in this repo
- Python port - MightyPork/PonyFrame
- Rust port - cpsdqs/tinyframe-rs
- JavaScript port - cpsdqs/tinyframe-js
Please note most of the ports are experimental and may exhibit various bugs or missing features. Testers are welcome :)
Functional overview
The basic functionality of TinyFrame is explained here. For particlars, such as the API functions, it's recommended to read the doc comments in the header file.
Structure of a frame
Each frame consists of a header and a payload. Both parts can be protected by a checksum, ensuring a frame with a malformed header (e.g. with a corrupted length field) or a corrupted payload is rejected.
The frame header contains a frame ID and a message type. Frame ID is incremented with each new message. The highest bit of the ID field is fixed to 1 and 0 for the two peers, avoiding a conflict.
Frame ID can be re-used in a response to tie the two messages together. Values of the type field are user defined.
All fields in the frame have a configurable size. By changing a field in the config file, such as TF_LEN_BYTES
(1, 2 or 4), the library seamlessly switches between uint8_t
, uint16_t
and uint32_t
for all functions working with the field.
,-----+-----+-----+------+------------+- - - -+-------------,
| SOF | ID | LEN | TYPE | HEAD_CKSUM | DATA | DATA_CKSUM |
| 0-1 | 1-4 | 1-4 | 1-4 | 0-4 | ... | 0-4 | <- size (bytes)
'-----+-----+-----+------+------------+- - - -+-------------'
SOF ......... start of frame, usually 0x01 (optional, configurable)
ID ......... the frame ID (MSb is the peer bit)
LEN ......... number of data bytes in the frame
TYPE ........ message type (used to run Type Listeners, pick any values you like)
HEAD_CKSUM .. header checksum
DATA ........ LEN bytes of data
DATA_CKSUM .. data checksum (left out if LEN is 0)
Message listeners
TinyFrame is based on the concept of message listeners. A listener is a callback function waiting for a particular message Type or ID to be received.
There are 3 listener types, in the order of precedence:
- ID listeners - waiting for a response
- Type listeners - waiting for a message of the given Type field
- Generic listeners - fallback
ID listeners can be registered automatically when sending a message. All listeners can also be registered and removed manually.
ID listeners are used to receive the response to a request. When registerign an ID listener, it's possible to attach custom user data to it that will be made available to the listener callback. This data (void *
) can be any kind of application context variable.
ID listeners can be assigned a timeout. When a listener expires, before it's removed, the callback is fired with NULL payload data in order to let the user free()
any attached userdata. This happens only if the userdata is not NULL.
Listener callbacks return values of the TF_Result
enum:
-
TF_CLOSE
- message accepted, remove the listener -
TF_STAY
- message accepted, stay registered -
TF_RENEW
- sameasTF_STAY
, but the ID listener's timeout is renewed -
TF_NEXT
- message NOT accepted, keep the listener and pass the message to the next listener capable of handling it.
Data buffers, multi-part frames
TinyFrame uses two data buffers: a small transmit buffer and a larger receive buffer. The transmit buffer is used to prepare bytes to send, either all at once, or in a circular fashion if the buffer is not large enough. The buffer must only contain the entire frame header, so e.g. 32 bytes should be sufficient for short messages.
Using the *_Multipart()
sending functions, it's further possible to split the frame header and payload to multiple function calls, allowing the applciation to e.g. generate the payload on-the-fly.
In contrast to the transmit buffer, the receive buffer must be large enough to contain an entire frame. This is because the final checksum must be verified before the frame is handled.
If frames larger than the possible receive buffer size are required (e.g. in embedded systems with small RAM), it's recommended to implement a multi-message transport mechanism at a higher level and send the data in chunks.
Usage Hints
-
All TinyFrame functions, typedefs and macros start with the
TF_
prefix. - Both peers must include the library with the same config parameters
-
See
TF_Integration.example.c
andTF_Config.example.c
for reference how to configure and integrate the library. - DO NOT modify the library files, if possible. This makes it easy to upgrade.
-
Start by calling
TF_Init()
withTF_MASTER
orTF_SLAVE
as the argument. This creates a handle. UseTF_InitStatic()
to avoid the use of malloc(). -
If multiple instances are used, you can tag them using the
tf.userdata
/tf.usertag
field. -
Implement
TF_WriteImpl()
- declared at the bottom of the header file asextern
. This function is used byTF_Send()
and others to write bytes to your UART (or other physical layer). A frame can be sent in it's entirety, or in multiple parts, depending on its size. - Use TF_AcceptChar(tf, byte) to give read data to TF. TF_Accept(tf, bytes, count) will accept mulitple bytes.
-
If you wish to use timeouts, periodically call
TF_Tick()
. The calling period determines the length of 1 tick. This is used to time-out the parser in case it gets stuck in a bad state (such as receiving a partial frame) and can also time-out ID listeners. -
Bind Type or Generic listeners using
TF_AddTypeListener()
orTF_AddGenericListener()
. -
Send a message using
TF_Send()
,TF_Query()
,TF_SendSimple()
,TF_QuerySimple()
. Query functions take a listener callback (function pointer) that will be added as an ID listener and wait for a response. -
Use the
*_Multipart()
variant of the above sending functions for payloads generated in multiple function calls. The payload is sent afterwards by callingTF_Multipart_Payload()
and the frame is closed byTF_Multipart_Close()
. -
If custom checksum implementation is needed, select
TF_CKSUM_CUSTOM8
, 16 or 32 and implement the three checksum functions. -
To reply to a message (when your listener gets called), use
TF_Respond()
with the msg object you received, replacing thedata
pointer (andlen
) with a response. -
At any time you can manually reset the message parser using
TF_ResetParser()
. It can also be reset automatically after a timeout configured in the config file.
Gotchas to look out for
-
If any userdata is attached to an ID listener with a timeout, when the listener times out, it will be called with NULL
msg->data
to let the user free the userdata. Therefore it's needed to checkmsg->data
before proceeding to handle the message. - If a multi-part frame is being sent, the Tx part of the library is locked to prevent concurrent access. The frame must be fully sent and closed before attempting to send anything else.
-
If multiple threads are used, don't forget to implement the mutex callbacks to avoid concurrent access to the Tx functions. The default implementation is not entirely thread safe, as it can't rely on platform-specific resources like mutexes or atomic access. Set
TF_USE_MUTEX
to1
in the config file.
Examples
You'll find various examples in the demo/
folder. Each example has it's own Makefile, read it to see what options are available.
The demos are written for Linux, some using sockets and clone()
for background processing. They try to simulate real TinyFrame behavior in an embedded system with asynchronous Rx and Tx. If you can't run the demos, the source files are still good as examples.
- LABVIEW NPOI庫(kù)文件下載 192次下載
- 用于OHOS最簡(jiǎn)單的UI驗(yàn)證庫(kù)教程 1次下載
- altium designer元件庫(kù)下載 703次下載
- STM32f10x官方固件庫(kù)資料 151次下載
- STM32固件庫(kù)使用手冊(cè)的中文版 0次下載
- STM32f10x官方固件庫(kù)資料 65次下載
- AD常用3D封裝庫(kù)(STEP)下載 370次下載
- AD 2D標(biāo)準(zhǔn)封裝庫(kù)下載 22次下載
- 面向NoSQL數(shù)據(jù)庫(kù)的JSON文檔異常檢測(cè)模型 20次下載
- 如何使用Multisim擴(kuò)展PSpice元件庫(kù)詳細(xì)方法說(shuō)明 0次下載
- 如何使用簡(jiǎn)單好用的PubSubClient庫(kù)來(lái)做一個(gè)最簡(jiǎn)單的MQTT客戶端
- protel99建庫(kù)規(guī)則大全 0次下載
- AN1246中文手冊(cè)之如何在Microchip圖形庫(kù)中創(chuàng)建控件
- 最簡(jiǎn)單的觸摸屏接線方法 57次下載
- 通用封裝庫(kù)(protel/AD版本通用)資料下載 0次下載
- 基于Rust的Log日志庫(kù)介紹 3121次閱讀
- nuere-簡(jiǎn)單小巧快速的字符串解析庫(kù) 460次閱讀
- STM32 HAL庫(kù)串口收發(fā)如何使用 5547次閱讀
- Linux中的靜態(tài)庫(kù)和共享庫(kù) 850次閱讀
- 庫(kù)遷移器的高級(jí)模式 751次閱讀
- 四種方式來(lái)讓您以簡(jiǎn)單模式訪問(wèn)遷移器 843次閱讀
- 云數(shù)據(jù)庫(kù)和自建數(shù)據(jù)庫(kù)的區(qū)別及應(yīng)用 4412次閱讀
- 數(shù)據(jù)庫(kù)入門書(shū)籍推薦 3.7w次閱讀
- 多維數(shù)據(jù)庫(kù)有哪些 7031次閱讀
- 用JDBC連接MySQL數(shù)據(jù)庫(kù)并進(jìn)行簡(jiǎn)單的增刪改查操作 6256次閱讀
- 谷歌開(kāi)源TFGAN輕量級(jí)的工具庫(kù) 目的是讓訓(xùn)練和評(píng)估GAN變得更加簡(jiǎn)單 4811次閱讀
- cassandra數(shù)據(jù)庫(kù)遷移與擴(kuò)容 5558次閱讀
- Android中實(shí)現(xiàn)簡(jiǎn)單的新聞列表 3769次閱讀
- STM32標(biāo)準(zhǔn)庫(kù)改為HAL庫(kù)的程序?qū)崿F(xiàn) 3.4w次閱讀
- 盤點(diǎn)幾種深度學(xué)習(xí)庫(kù) 3063次閱讀
下載排行
本周
- 1山景DSP芯片AP8248A2數(shù)據(jù)手冊(cè)
- 1.06 MB | 532次下載 | 免費(fèi)
- 2RK3399完整板原理圖(支持平板,盒子VR)
- 3.28 MB | 339次下載 | 免費(fèi)
- 3TC358743XBG評(píng)估板參考手冊(cè)
- 1.36 MB | 330次下載 | 免費(fèi)
- 4DFM軟件使用教程
- 0.84 MB | 295次下載 | 免費(fèi)
- 5元宇宙深度解析—未來(lái)的未來(lái)-風(fēng)口還是泡沫
- 6.40 MB | 227次下載 | 免費(fèi)
- 6迪文DGUS開(kāi)發(fā)指南
- 31.67 MB | 194次下載 | 免費(fèi)
- 7元宇宙底層硬件系列報(bào)告
- 13.42 MB | 182次下載 | 免費(fèi)
- 8FP5207XR-G1中文應(yīng)用手冊(cè)
- 1.09 MB | 178次下載 | 免費(fèi)
本月
- 1OrCAD10.5下載OrCAD10.5中文版軟件
- 0.00 MB | 234315次下載 | 免費(fèi)
- 2555集成電路應(yīng)用800例(新編版)
- 0.00 MB | 33566次下載 | 免費(fèi)
- 3接口電路圖大全
- 未知 | 30323次下載 | 免費(fèi)
- 4開(kāi)關(guān)電源設(shè)計(jì)實(shí)例指南
- 未知 | 21549次下載 | 免費(fèi)
- 5電氣工程師手冊(cè)免費(fèi)下載(新編第二版pdf電子書(shū))
- 0.00 MB | 15349次下載 | 免費(fèi)
- 6數(shù)字電路基礎(chǔ)pdf(下載)
- 未知 | 13750次下載 | 免費(fèi)
- 7電子制作實(shí)例集錦 下載
- 未知 | 8113次下載 | 免費(fèi)
- 8《LED驅(qū)動(dòng)電路設(shè)計(jì)》 溫德?tīng)栔?/a>
- 0.00 MB | 6656次下載 | 免費(fèi)
總榜
- 1matlab軟件下載入口
- 未知 | 935054次下載 | 免費(fèi)
- 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
- 78.1 MB | 537798次下載 | 免費(fèi)
- 3MATLAB 7.1 下載 (含軟件介紹)
- 未知 | 420027次下載 | 免費(fèi)
- 4OrCAD10.5下載OrCAD10.5中文版軟件
- 0.00 MB | 234315次下載 | 免費(fèi)
- 5Altium DXP2002下載入口
- 未知 | 233046次下載 | 免費(fèi)
- 6電路仿真軟件multisim 10.0免費(fèi)下載
- 340992 | 191187次下載 | 免費(fèi)
- 7十天學(xué)會(huì)AVR單片機(jī)與C語(yǔ)言視頻教程 下載
- 158M | 183279次下載 | 免費(fèi)
- 8proe5.0野火版下載(中文版免費(fèi)下載)
- 未知 | 138040次下載 | 免費(fèi)
評(píng)論
查看更多