xref: /aosp_15_r20/external/pigweed/pw_channel/guides.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_channel-quickstart-guides:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker===================
4*61c4878aSAndroid Build Coastguard WorkerQuickstart & guides
5*61c4878aSAndroid Build Coastguard Worker===================
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_channel
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. _module-pw_channel-guides-faqs:
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker---------------------------------
12*61c4878aSAndroid Build Coastguard WorkerFrequently asked questions (FAQs)
13*61c4878aSAndroid Build Coastguard Worker---------------------------------
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard WorkerMy API reads and writes data. Should I implement the ``Channel`` interface, or should my API accept a ``Channel`` to read from and write into?
16*61c4878aSAndroid Build Coastguard Worker==============================================================================================================================================
17*61c4878aSAndroid Build Coastguard WorkerUsers should typically only implement the ``Channel`` interface themselves if
18*61c4878aSAndroid Build Coastguard Workerthey want to control where buffers for underlying data are stored.
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard WorkerOn the read side, the channel implementation controls buffer allocation by
21*61c4878aSAndroid Build Coastguard Workerchoosing how to allocate the ``MultiBuf`` values that are returned from
22*61c4878aSAndroid Build Coastguard Worker``PendRead``.
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard WorkerOn the write side, the channel implementation controls buffer allocation via
25*61c4878aSAndroid Build Coastguard Workerits implementation of the ``PendAllocateWriteBuffer`` method.
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard WorkerMost commonly, lower layers of the communications stack implement ``Channel`` in
28*61c4878aSAndroid Build Coastguard Workerorder to provide buffers that are customized to the needs of the underlying
29*61c4878aSAndroid Build Coastguard Workertransport. For example, a UART DMA channel might need to allocate buffers
30*61c4878aSAndroid Build Coastguard Workerout of particular DMA-compatible memory regions, with a particular alignment,
31*61c4878aSAndroid Build Coastguard Workeror of a particular MTU size.
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard WorkerThe top-level or application layer should typically work with provided
34*61c4878aSAndroid Build Coastguard Workerchannel implementations in order to allow the lower layers to control
35*61c4878aSAndroid Build Coastguard Workerallocation and minimize copies.
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard WorkerIntermediate layers of the stack should generally give preference to
38*61c4878aSAndroid Build Coastguard Workerallowing lower layers of the stack to allocate, and so should implement
39*61c4878aSAndroid Build Coastguard Workera channel interface for the higher layer by delegating to the lower layer.
40*61c4878aSAndroid Build Coastguard WorkerWhen intermediate layers must copy data (are incompatible with zero-copy),
41*61c4878aSAndroid Build Coastguard Workerthey should prefer to accept channels from both the higher and lower
42*61c4878aSAndroid Build Coastguard Workerlayers so that both the application layer and lower layers can choose
43*61c4878aSAndroid Build Coastguard Workerhow to manage memory allocations.
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard WorkerWhen necessary, two APIs that accept channels can be paired together
46*61c4878aSAndroid Build Coastguard Workerusing a ``ForwardingChannel`` which manages allocations by delegating
47*61c4878aSAndroid Build Coastguard Workerto the provided ``MultiBufAllocator``.
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard WorkerCan different tasks write into and read from the same channel?
50*61c4878aSAndroid Build Coastguard Worker==============================================================
51*61c4878aSAndroid Build Coastguard WorkerNo; it is not possible to read from the channel in one task while
52*61c4878aSAndroid Build Coastguard Workerwriting to it from another task. A single task must own and operate
53*61c4878aSAndroid Build Coastguard Workerthe channel. In the future, a wrapper will be offered which will
54*61c4878aSAndroid Build Coastguard Workerallow the channel to be split into a read half and a write half which
55*61c4878aSAndroid Build Coastguard Workercan be used from independent tasks.
56