xref: /aosp_15_r20/external/pigweed/pw_hdlc/design.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_hdlc-design:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker================
4*61c4878aSAndroid Build Coastguard WorkerDesign & roadmap
5*61c4878aSAndroid Build Coastguard Worker================
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_hdlc
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. pw_hdlc-overview-start
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker``pw_hdlc`` implements a subset of the `High-Level Data Link Control
12*61c4878aSAndroid Build Coastguard Worker<https://en.wikipedia.org/wiki/High-Level_Data_Link_Control>`_ (HDLC) protocol.
13*61c4878aSAndroid Build Coastguard WorkerHDLC is a data link layer protocol intended for serial communication between
14*61c4878aSAndroid Build Coastguard Workerdevices and is standardized as `ISO/IEC 13239:2002
15*61c4878aSAndroid Build Coastguard Worker<https://www.iso.org/standard/37010.html>`_.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker.. pw_hdlc-overview-end
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker--------
20*61c4878aSAndroid Build Coastguard WorkerOverview
21*61c4878aSAndroid Build Coastguard Worker--------
22*61c4878aSAndroid Build Coastguard WorkerThe ``pw_hdlc`` module provides a simple, robust frame-oriented transport that
23*61c4878aSAndroid Build Coastguard Workeruses a subset of the HDLC protocol. ``pw_hdlc`` supports sending between
24*61c4878aSAndroid Build Coastguard Workerembedded devices or the host. It can be used with :ref:`module-pw_rpc` to enable
25*61c4878aSAndroid Build Coastguard Workerremote procedure calls (RPCs) on embedded devices.
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker``pw_hdlc`` has two primary functions:
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker* **Encoding** data by constructing a frame with the escaped payload bytes and
30*61c4878aSAndroid Build Coastguard Worker  frame check sequence.
31*61c4878aSAndroid Build Coastguard Worker* **Decoding** data by unescaping the received bytes, verifying the frame
32*61c4878aSAndroid Build Coastguard Worker  check sequence, and returning successfully decoded frames.
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard WorkerDesign considerations
35*61c4878aSAndroid Build Coastguard Worker=====================
36*61c4878aSAndroid Build Coastguard Worker* ``pw_hdlc`` only supports unnumbered information frames.
37*61c4878aSAndroid Build Coastguard Worker* It uses special escaped bytes to mark the beginning and end of a frame.
38*61c4878aSAndroid Build Coastguard Worker* Frame data is stored in a buffer until the end-of-frame delimiter byte is read.
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker--------------------
41*61c4878aSAndroid Build Coastguard WorkerProtocol Description
42*61c4878aSAndroid Build Coastguard Worker--------------------
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard WorkerFrames
45*61c4878aSAndroid Build Coastguard Worker======
46*61c4878aSAndroid Build Coastguard WorkerThe HDLC implementation in ``pw_hdlc`` supports only HDLC unnumbered
47*61c4878aSAndroid Build Coastguard Workerinformation frames. These frames are encoded as follows:
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard Worker   _________________________________________
52*61c4878aSAndroid Build Coastguard Worker   | | | |                          |    | |...
53*61c4878aSAndroid Build Coastguard Worker   | | | |                          |    | |... [More frames]
54*61c4878aSAndroid Build Coastguard Worker   |_|_|_|__________________________|____|_|...
55*61c4878aSAndroid Build Coastguard Worker    F A C       Payload              FCS  F
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker    F = flag byte (0x7e, the ~ character)
58*61c4878aSAndroid Build Coastguard Worker    A = address field
59*61c4878aSAndroid Build Coastguard Worker    C = control field
60*61c4878aSAndroid Build Coastguard Worker    FCS = frame check sequence (CRC-32)
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard WorkerEncoding and sending data
64*61c4878aSAndroid Build Coastguard Worker=========================
65*61c4878aSAndroid Build Coastguard WorkerThis module first writes an initial frame delimiter byte (``0x7E``) to indicate
66*61c4878aSAndroid Build Coastguard Workerthe beginning of the frame. Before sending any of the payload data through
67*61c4878aSAndroid Build Coastguard Workerserial, the special bytes are escaped:
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Worker+-------------------------+-----------------------+
70*61c4878aSAndroid Build Coastguard Worker| Unescaped Special Bytes | Escaped Special Bytes |
71*61c4878aSAndroid Build Coastguard Worker+=========================+=======================+
72*61c4878aSAndroid Build Coastguard Worker|         ``7E``          |      ``7D 5E``        |
73*61c4878aSAndroid Build Coastguard Worker+-------------------------+-----------------------+
74*61c4878aSAndroid Build Coastguard Worker|         ``7D``          |      ``7D 5D``        |
75*61c4878aSAndroid Build Coastguard Worker+-------------------------+-----------------------+
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard WorkerThe bytes of the payload are escaped and written in a single pass. The
78*61c4878aSAndroid Build Coastguard Workerframe check sequence is calculated, escaped, and written after. After this, a
79*61c4878aSAndroid Build Coastguard Workerfinal frame delimiter byte (``0x7E``) is written to mark the end of the frame.
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard WorkerDecoding received bytes
82*61c4878aSAndroid Build Coastguard Worker=======================
83*61c4878aSAndroid Build Coastguard WorkerFrames may be received in multiple parts, so ``pw_hdlc`` needs to store the
84*61c4878aSAndroid Build Coastguard Workerreceived data in a buffer until the ending frame delimiter (``0x7E``) is read.
85*61c4878aSAndroid Build Coastguard WorkerWhen the ``pw_hdlc`` decoder receives data, it unescapes it and adds it to a
86*61c4878aSAndroid Build Coastguard Workerbuffer. When the frame is complete, it calculates and verifies the frame check
87*61c4878aSAndroid Build Coastguard Workersequence and does the following:
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker* If correctly verified, the decoder returns the decoded frame.
90*61c4878aSAndroid Build Coastguard Worker* If the checksum verification fails, the frame is discarded and an error is
91*61c4878aSAndroid Build Coastguard Worker  reported.
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard Worker-------
94*61c4878aSAndroid Build Coastguard WorkerRoadmap
95*61c4878aSAndroid Build Coastguard Worker-------
96*61c4878aSAndroid Build Coastguard Worker- **Expanded protocol support** - ``pw_hdlc`` currently only supports
97*61c4878aSAndroid Build Coastguard Worker  unnumbered information frames. Support for different frame types and
98*61c4878aSAndroid Build Coastguard Worker  extended control fields may be added in the future.
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard Worker-----------------
101*61c4878aSAndroid Build Coastguard WorkerMore pw_hdlc docs
102*61c4878aSAndroid Build Coastguard Worker-----------------
103*61c4878aSAndroid Build Coastguard Worker.. include:: docs.rst
104*61c4878aSAndroid Build Coastguard Worker   :start-after: .. pw_hdlc-nav-start
105*61c4878aSAndroid Build Coastguard Worker   :end-before: .. pw_hdlc-nav-end
106