|
| 1 | +.. _bluetooth_mesh_access: |
| 2 | + |
| 3 | +Access |
| 4 | +###### |
| 5 | + |
| 6 | +The Bluetooth Mesh access layer is the application's interface to the mesh |
| 7 | +network. The access layer provides mechanisms for compartmentalizing the node |
| 8 | +behavior into elements and models, which are implemented by the application. |
| 9 | + |
| 10 | +Mesh models |
| 11 | +*********** |
| 12 | + |
| 13 | +The functionality of a mesh node is represented by models. A model implements |
| 14 | +a single behavior the node supports, like being a light, a sensor or a |
| 15 | +thermostat. The mesh models are grouped into *elements*. Each element is |
| 16 | +assigned its own unicast address, and may only contain one of each type of |
| 17 | +model. Conventionally, each element represents a single aspect of the mesh |
| 18 | +node behavior. For instance, a node that contains a sensor, two lights and a |
| 19 | +power outlet would spread this functionality across four elements, with each |
| 20 | +element instantiating all the models required for a single aspect of the |
| 21 | +supported behavior. |
| 22 | + |
| 23 | +The node's element and model structure is specified in the node composition |
| 24 | +data, which is passed to :cpp:func:`bt_mesh_init()` during initialization. The |
| 25 | +Bluetooth SIG have defined a set of foundation models (see |
| 26 | +:ref:`bluetooth_mesh_models`) and a set of models for implementing common |
| 27 | +behavior in the `Bluetooth Mesh Model Specification |
| 28 | +<https://www.bluetooth.com/specifications/mesh-specifications/>`_. All models |
| 29 | +not specified by the Bluetooth SIG are vendor models, and must be tied to a |
| 30 | +Company ID. |
| 31 | + |
| 32 | +Mesh Models have several parameters that can be configured either through |
| 33 | +initialization of the mesh stack or with the |
| 34 | +:ref:`bluetooth_mesh_models_cfg_srv`: |
| 35 | + |
| 36 | +Opcode list |
| 37 | +=========== |
| 38 | + |
| 39 | +The opcode list contains all message opcodes the model can receive, as well as |
| 40 | +the minimum acceptable payload length and the callback to pass them to. Models |
| 41 | +can support any number of opcodes, but each opcode can only be listed by one |
| 42 | +model in each element. |
| 43 | + |
| 44 | +The full opcode list must be passed to the model structure in the composition |
| 45 | +data, and cannot be changed at runtime. The end of the opcode list is |
| 46 | +determined by the special :c:macro:`BT_MESH_MODEL_OP_END` entry. This entry |
| 47 | +must always be present in the opcode list, unless the list is empty. In that |
| 48 | +case, :c:macro:`BT_MESH_MODEL_NO_OPS` should be used in place of a proper |
| 49 | +opcode list definition. |
| 50 | + |
| 51 | +AppKey list |
| 52 | +=========== |
| 53 | + |
| 54 | +The AppKey list contains all the application keys the model can receive |
| 55 | +messages on. Only messages encrypted with application keys in the AppKey list |
| 56 | +will be passed to the model. |
| 57 | + |
| 58 | +The maximum number of supported application keys each model can hold is |
| 59 | +configured with the :option:`CONFIG_BT_MESH_MODEL_KEY_COUNT` configuration |
| 60 | +option. The contents of the AppKey list is managed by the |
| 61 | +:ref:`bluetooth_mesh_models_cfg_srv`. |
| 62 | + |
| 63 | +Subscription list |
| 64 | +================= |
| 65 | + |
| 66 | +A model will process all messages addressed to the unicast address of their |
| 67 | +element (given that the utilized application key is present in the AppKey |
| 68 | +list). Additionally, the model will process packets addressed to any group or |
| 69 | +virtual address in its subscription list. This allows nodes to address |
| 70 | +multiple nodes throughout the mesh network with a single message. |
| 71 | + |
| 72 | +The maximum number of supported addresses in the Subscription list each model |
| 73 | +can hold is configured with the :option:`CONFIG_BT_MESH_MODEL_GROUP_COUNT` |
| 74 | +configuration option. The contents of the subscription list is managed by the |
| 75 | +:ref:`bluetooth_mesh_models_cfg_srv`. |
| 76 | + |
| 77 | +Model publication |
| 78 | +================= |
| 79 | + |
| 80 | +The models may send messages in two ways: |
| 81 | + |
| 82 | +* By specifying a set of message parameters in a :cpp:type:`bt_mesh_msg_ctx`, |
| 83 | + and calling :cpp:func:`bt_mesh_model_send()`. |
| 84 | +* By setting up a :cpp:type:`bt_mesh_model_pub` structure and calling |
| 85 | + :cpp:func:`bt_mesh_model_publish()`. |
| 86 | + |
| 87 | +When publishing messages with :cpp:func:`bt_mesh_model_publish()`, the model |
| 88 | +will use the publication parameters configured by the |
| 89 | +:ref:`bluetooth_mesh_models_cfg_srv`. This is the recommended way to send |
| 90 | +unprompted model messages, as it passes the responsibility of selecting |
| 91 | +message parameters to the network administrator, which likely knows more about |
| 92 | +the mesh network than the individual nodes will. |
| 93 | + |
| 94 | +To support publishing with the publication parameters, the model must allocate |
| 95 | +a packet buffer for publishing, and pass it to |
| 96 | +:cpp:var:`bt_mesh_model_pub::msg`. The Config Server may also set up period |
| 97 | +publication for the publication message. To support this, the model must |
| 98 | +populate the :cpp:var:`bt_mesh_model_pub::update` callback. The |
| 99 | +:cpp:var:`bt_mesh_model_pub::update` callback will be called right before the |
| 100 | +message is published, allowing the model to change the payload to reflect its |
| 101 | +current state. |
| 102 | + |
| 103 | +Extended models |
| 104 | +=============== |
| 105 | + |
| 106 | +The Bluetooth Mesh specification allows the Mesh models to extend each other. |
| 107 | +When a model extends another, it inherits that model's functionality, and |
| 108 | +extension can be used to construct complex models out of simple ones, |
| 109 | +leveraging the existing model functionality to avoid defining new opcodes. |
| 110 | +Models may extend any number of models, from any element. When one model |
| 111 | +extends another in the same element, the two models will share subscription |
| 112 | +lists. The mesh stack implements this by merging the subscription lists of the |
| 113 | +two models into one, combining the number of subscriptions the models can have |
| 114 | +in total. Models may extend models that extend others, creating an "extension |
| 115 | +tree". All models in an extension tree share a single subscription list per |
| 116 | +element it spans. |
| 117 | + |
| 118 | +Model extensions are done by calling :cpp:func:`bt_mesh_model_extend()` during |
| 119 | +initialization. A model can only be extended by one other model, and |
| 120 | +extensions cannot be circular. Note that binding of node states and other |
| 121 | +relationships between the models must be defined by the model implementations. |
| 122 | + |
| 123 | +The model extension concept adds some overhead in the access layer packet |
| 124 | +processing, and must be explicitly enabled with |
| 125 | +:option:`CONFIG_BT_MESH_MODEL_EXTENSIONS` to have any effect. |
| 126 | + |
| 127 | +Model data storage |
| 128 | +================== |
| 129 | + |
| 130 | +Mesh models may have data associated with each model instance that needs to be |
| 131 | +stored persistently. The access API provides a mechanism for storing this |
| 132 | +data, leveraging the internal model instance encoding scheme. Models can store |
| 133 | +one user defined data entry per instance by calling |
| 134 | +:cpp:func:`bt_mesh_model_data_store()`. To be able to read out the data the |
| 135 | +next time the device reboots, the model's |
| 136 | +:cpp:var:`bt_mesh_model_cb::settings_set()` callback must be populated. This |
| 137 | +callback gets called when model specific data is found in the persistent |
| 138 | +storage. The model can retrieve the data by calling the ``read_cb`` passed as |
| 139 | +a parameter to the callback. See the :ref:`settings` module documentation for |
| 140 | +details. |
| 141 | + |
| 142 | +API reference |
| 143 | +************* |
| 144 | + |
| 145 | +.. doxygengroup:: bt_mesh_access |
| 146 | + :project: Zephyr |
| 147 | + :members: |
0 commit comments