xref: /btstack/doc/manual/docs-template/architecture.md (revision 99238babf8b2881a820b41c13008f1726022e12f)
1503a627eSMilanka Ringwald#
2503a627eSMilanka Ringwald
3503a627eSMilanka RingwaldAs well as any other communication stack, BTstack is a collection of
4503a627eSMilanka Ringwaldstate machines that interact with each other. There is one or more state
5503a627eSMilanka Ringwaldmachines for each protocol and service that it implements. The rest of
6503a627eSMilanka Ringwaldthe architecture follows these fundamental design guidelines:
7503a627eSMilanka Ringwald
8503a627eSMilanka Ringwald-   *Single threaded design* - BTstack does not use or require
9503a627eSMilanka Ringwald    multi-threading to handle data sources and timers. Instead, it uses
10503a627eSMilanka Ringwald    a single run loop.
11503a627eSMilanka Ringwald
12503a627eSMilanka Ringwald-   *No blocking anywhere* - If Bluetooth processing is required, its
13503a627eSMilanka Ringwald    result will be delivered as an event via registered packet handlers.
14503a627eSMilanka Ringwald
15503a627eSMilanka Ringwald-   *No artificially limited buffers/pools* - Incoming and outgoing data
16503a627eSMilanka Ringwald    packets are not queued.
17503a627eSMilanka Ringwald
18503a627eSMilanka Ringwald-   *Statically bounded memory (optionally)* - The number of maximum
19503a627eSMilanka Ringwald    connections/channels/services can be configured.
20503a627eSMilanka Ringwald
21503a627eSMilanka RingwaldFigure {@fig:BTstackArchitecture} shows the general architecture of a
22503a627eSMilanka RingwaldBTstack-based single-threaded application that includes the BTstack run loop.
23503a627eSMilanka RingwaldThe Main Application contains the application logic, e.g., reading a sensor value and
24503a627eSMilanka Ringwaldproviding it via the Communication Logic as a SPP Server. The
25503a627eSMilanka RingwaldCommunication Logic is often modeled as a finite state machine with
26503a627eSMilanka Ringwaldevents and data coming from either the Main Application or from BTstack
27503a627eSMilanka Ringwaldvia registered packet handlers (PH). BTstack’s Run Loop is responsible
28503a627eSMilanka Ringwaldfor providing timers and processing incoming data.
29503a627eSMilanka Ringwald
307029d61cSMilanka Ringwald![Architecture of a BTstack-based application.](picts/btstack-architecture.png) {#fig:BTstackArchitecture}
31503a627eSMilanka Ringwald
32503a627eSMilanka Ringwald## Single threaded design
33503a627eSMilanka Ringwald
34503a627eSMilanka RingwaldBTstack does not use or require multi-threading. It uses a single run
35503a627eSMilanka Ringwaldloop to handle data sources and timers. Data sources represent
36503a627eSMilanka Ringwaldcommunication interfaces like an UART or an USB driver. Timers are used
37503a627eSMilanka Ringwaldby BTstack to implement various Bluetooth-related timeouts. For example,
38503a627eSMilanka Ringwaldto disconnect a Bluetooth baseband channel without an active L2CAP
39503a627eSMilanka Ringwaldchannel after 20 seconds. They can also be used to handle periodic
40503a627eSMilanka Ringwaldevents. During a run loop cycle, the callback functions of all
41503a627eSMilanka Ringwaldregistered data sources are called. Then, the callback functions of
42503a627eSMilanka Ringwaldtimers that are ready are executed.
43503a627eSMilanka Ringwald
44*99238babSMilanka RingwaldFor adapting BTstack to multi-threaded environments check [here](../integration/#sec:multithreadingIntegration).
45503a627eSMilanka Ringwald
46503a627eSMilanka Ringwald## No blocking anywhere
47503a627eSMilanka Ringwald
48503a627eSMilanka RingwaldBluetooth logic is event-driven. Therefore, all BTstack functions are
49503a627eSMilanka Ringwaldnon-blocking, i.e., all functions that cannot return immediately
50503a627eSMilanka Ringwaldimplement an asynchronous pattern. If the arguments of a function are
51503a627eSMilanka Ringwaldvalid, the necessary commands are sent to the Bluetooth chipset and the
52503a627eSMilanka Ringwaldfunction returns with a success value. The actual result is delivered
53503a627eSMilanka Ringwaldlater as an asynchronous event via registered packet handlers.
54503a627eSMilanka Ringwald
55503a627eSMilanka RingwaldIf a Bluetooth event triggers longer processing by the application, the
56503a627eSMilanka Ringwaldprocessing should be split into smaller chunks. The packet handler could
57503a627eSMilanka Ringwaldthen schedule a timer that manages the sequential execution of the
58503a627eSMilanka Ringwaldchunks.
59503a627eSMilanka Ringwald
60503a627eSMilanka Ringwald## No artificially limited buffers/pools
61503a627eSMilanka Ringwald
62503a627eSMilanka RingwaldIncoming and outgoing data packets are not queued. BTstack delivers an
63503a627eSMilanka Ringwaldincoming data packet to the application before it receives the next one
64503a627eSMilanka Ringwaldfrom the Bluetooth chipset. Therefore, it relies on the link layer of
65503a627eSMilanka Ringwaldthe Bluetooth chipset to slow down the remote sender when needed.
66503a627eSMilanka Ringwald
67503a627eSMilanka RingwaldSimilarly, the application has to adapt its packet generation to the
68503a627eSMilanka Ringwaldremote receiver for outgoing data. L2CAP relies on ACL flow control
69503a627eSMilanka Ringwaldbetween sender and receiver. If there are no free ACL buffers in the
70503a627eSMilanka RingwaldBluetooth module, the application cannot send. For RFCOMM, the mandatory
71503a627eSMilanka Ringwaldcredit-based flow-control limits the data sending rate additionally. The
72503a627eSMilanka Ringwaldapplication can only send an RFCOMM packet if it has RFCOMM credits.
73503a627eSMilanka Ringwald
74503a627eSMilanka Ringwald## Statically bounded memory
75503a627eSMilanka Ringwald
76503a627eSMilanka RingwaldBTstack has to keep track of services and active connections on the
77503a627eSMilanka Ringwaldvarious protocol layers. The number of maximum
78503a627eSMilanka Ringwaldconnections/channels/services can be configured. In addition, the
79503a627eSMilanka Ringwaldnon-persistent database for remote device names and link keys needs
80503a627eSMilanka Ringwaldmemory and can be be configured, too. These numbers determine the amount
81503a627eSMilanka Ringwaldof static memory allocation.
82