The Bluetooth mesh model specification specifies a Generic OnOff Model to be used in real applications with the Bluetooth mesh. This vendor-specific model is a simplified version of the Generic OnOff Model. It is an introductory example for Creating new models, but you can also use it in your applications.
See the following sections for information about how to implement a vendor-specific Simple OnOff model that turns something On or Off, for example a light bulb, a heater, or a washing machine.
Table of contents
You can check the complete model implementation and its layout in the models/vendor/simple_on_off
directory.
If you want to see how this model can be integrated into a complete application, take a look at the Light switch example and at the examples/light_switch
directory.
A Bluetooth mesh application is specified using a client-server architecture, where client and server models use publish and subscribe mechanism to communicate with each other. For this reason, the intended functionality of this model will be realized using two parts:
When the server model receives a GET or a (reliable) SET message from a client model, it sends the current value of the OnOff state as response. This keeps the client up-to-date about the server state.
For more details about setting up publication and subscription, see Creating new models.
The following table shows the opcodes that are supported by this model.
Name | Definition | Opcode | Description | Parameter | Parameter size |
---|---|---|---|---|---|
SET | SIMPLE_ON_OFF_OPCODE_SET | 0xc1 | Sets the current on/off state | New state | 1 byte |
GET | SIMPLE_ON_OFF_OPCODE_GET | 0xc2 | Gets the current on/off state | N/A | No parameter |
SET UNRELIABLE | SIMPLE_ON_OFF_OPCODE_SET_UNRELIABLE | 0xc3 | Sets the current on/off state | New state | 1 byte |
Status | SIMPLE_ON_OFF_OPCODE_STATUS | 0xc4 | Contains the current state | Current state | 1 byte |
The opcodes sent on-air are three bytes for the vendor-specific models. The complete opcode is the combination of the vendor-specific opcode and the company identifier. For more information, see the access_opcode_t
documentation.
For this model, the following identifiers are used.
Description | Value |
---|---|
Company identifier | 0x0059 |
Server identifier | 0x0000 |
Client identifier | 0x0001 |
The company identifier used in this table is Nordic Semiconductor's assigned Bluetooth company ID. In a real application, use your own company's assigned ID.
As described earlier, a model comprises of two entities that together implement the complete behavior:
Implement both the server and the client models for the Simple OnOff model to work.
The behavior of the simple OnOff server is illustrated by the following message sequence chart.
When the OnOff server receives SET and GET messages:
To implement the server model:
simple_on_off_server_t
) and associated callbacks: SIMPLE_ON_OFF_OPCODE_GET
, SIMPLE_ON_OFF_OPCODE_SET
, and SIMPLE_ON_OFF_OPCODE_SET_UNRELIABLE
messages. Each of these opcode handlers calls the corresponding user callback function from the context structure. This context structure gets passed to the opcode handlers via the p_args
parameter.reply_status()
function.set_cb()
callback to fetch the current OnOff state value from the user application and sends this value with the reply_status()
function. The server model also publishes its state in response to any received message, using the simple_on_off_server_status_publish()
function, if its publish address is set by the provisioner.reply_status()
function sends the value of the current state in an SIMPLE_ON_OFF_OPCODE_STATUS
message as a reply to the client using the access_model_reply()
API. This API requires certain parameters to send the message correctly, which is why it has been wrapped in reply_status()
.reply_status()
function: simple_on_off_server_status_publish()
function.simple_on_off_server_status_publish()
function is very similar to the reply_status()
function, except it uses the access_model_publish()
API to publish the response message. If the publish address of the client model is not configured by the provisioner, the access_model_publish()
will not publish the given message.access_opcode_handler_t
type and consists of the opcode, vendor ID, and an opcode handler function pointer.You now have the basic skeleton of a simple OnOff server model, which can be expanded or tweaked to produce more complex server models. See models/vendor/simple_on_off/
for the complete code of this model.
The client model is used to interact with the corresponding server model. It sends SET and GET messages and processes incoming status replies. The client model sends messages using a publish mechanism. It uses assigned publication address as the destination for outgoing messages.
Just as in the server implementation, the client needs a context structure to keep information about callbacks and its model handle. In addition, a boolean variable is used to keep track of whether a transaction is currently active and to prevent running multiple simultaneous transactions.
The client model uses a callback function to provide information about the state of the server to the user application. If the server does not reply within a given time frame, it will notify the user application with the error code SIMPLE_ON_OFF_STATUS_ERROR_NO_REPLY
.
The following code snippet shows the status codes (simple_on_off_status_t
), the context structure (simple_on_off_client_t
), and associated callbacks required for this model:
To implement the client model:
access_model_publish()
API to send an unreliable message.access_model_reliable_publish()
API to send a reliable message. This API guarantees delivery of a message by retransmitting it until a reply is received from the destination node or the transaction times out.reliable_status_cb()
callback and the send_reliable_message()
function for the client model: SIMPLE_ON_OFF_OPCODE_STATUS
opcode to process the reply message.publish_timeout_cb
. This callback will be called by the publish mechanism if the provisioner configures periodic publishing.The client model is now implemented. You can now use it to turn something On or Off by communicating with the server node.