Astarte device API for Zephyr 0.11.1
Astarte device SDK for Zephyr RTOS
Loading...
Searching...
No Matches
Astarte device API for Zephyr

Astarte Device SDK Zephyr

Astarte device library for the Zephyr framework.

Getting started

Before getting started, make sure you have a proper Zephyr development environment. Follow the official Zephyr Getting Started Guide.

Using the library as a dependency in an application

To add this module as a dependency to an application the application's west.yml manifest file should be modified in the following way. First, a new remote should be added:

remotes:
# ... other remotes ...
- name: astarte-platform
url-base: https://github.com/astarte-platform

Second, a new entry should be added to the projects list:

projects:
# ... other projects ...
- name: astarte-device-sdk-zephyr
remote: astarte-platform
revision: v0.11.1
import: true

Remember to run west update after performing changes to the manifest file.

In addition, the Astarte device library Kconfig should be added to the Kconfig file of the application.

rsource "../../astarte-device-sdk-zephyr/Kconfig"

This will add the configuration options of the example module to the application.

Using the library as a standalone application

For development or evaluation purposes the library can be used as a stand alone application in its own workspace. This will make it possible to build and execute the module samples without having to set up an extra application.

Creating a new workspace, venv and install west

Start by creating a new workspace folder and a venv where west will reside.

mkdir ~/astarte-zephyrproject && cd ~/astarte-zephyrproject
python3 -m venv .venv
source .venv/bin/activate
pip install west

Initializing the workspace

The west workspace should be then intialized, using the Astarte libary repository as the manifest repository.

west init -m git@github.com:astarte-platform/astarte-device-sdk-zephyr --mr v0.11.1
west update

Exporting zephyr environment and installing python dependencies

Lastly some dependencies will need to be installed for all west extensions to work correctly.

west zephyr-export
west packages pip --install
west packages pip --install -- -r ./astarte-device-sdk-zephyr/scripts/requirements.txt

Fetching proprietary binary blobs

If building for boards requiring proprietary binary blobs an additional fetch step is required.

west blobs fetch hal_espressif
west blobs fetch hal_nxp

Note: this command should be re-run when updating Zephyr to a newer version

Building and running a sample application

To build one of the samples applications, run the following command:

west build -p always -b <BOARD> ./astarte-device-sdk-zephyr/samples/<SAMPLE>

Where <BOARD> is one of the boards supported by Zephyr and <SAMPLE> is one of the samples contained in the samples folder.

Once you have built the application, run the following command to flash it on the ESP board:

west flash

Or the following command to run on an emulated board or the native simulator:

west build -t run

One time configuration

While usually the configuration setting are set in a prj.conf file, during development it might be useful to set them manually. To do so run the following command:

west build -t menuconfig

This command will require the application to have already been build to function correctly. After changing the configuration, the application can be flashed directly using west flash.

Reading serial monitor

The standard configuration for all the samples include settings for printing the logs through the serial port connected to the host.

Unit testing

A set of unit test is present under the tests directory of this project. They can be run using twister.

west twister -c -T ./astarte-device-sdk-zephyr/tests

West extension commands

Interface definitions generation

The standard format of an Astarte interface is a JSON file as defined in the Astarte documentation. However, an Astarte device on Zephyr uses C structures to define Astarte interfaces. Translating an interface from a JSON definition to a C definition for Zephyr can be a repetitive and error-prone process.

For this reason, an extension command for west has been created to facilitate this procedure. The astarte-interfaces extension command accepts as input one or more JSON interface definitions and automatically generates the corresponding C source code. Run west astarte-interfaces --help to learn about the generation options.

Build time interface definitions generation

It's also possible to automatically generate the interface definition structs. Generated files will be located in the build directory: build/zephyr/include/generated/astarte_generated_interfaces.*.

This allows an easier management of generated files that won't need to be committed to the vcs. Enabling the feature is straight forward and requires setting two configuration values in the project prj.conf file:

CONFIG_ASTARTE_DEVICE_SDK_CODE_GENERATION=y
CONFIG_ASTARTE_DEVICE_SDK_CODE_GENERATION_INTERFACE_DIRECTORY="<interface jsons directory>"

Replace <interface jsons directory> with the relative path to the interfaces directory.

After adding the two properties you can run a build targeting astarte_generate_interfaces.

west build -b native_sim -t astarte_generate_interfaces .

or simply launch a build of the project.

After this you'll be able to include the generated header file:

#include "astarte_generated_interfaces.h"

For a complete example you can take a look at e2e/prj.conf and the interfaces/ directory. The generated interfaces are included in e2e/src/runner.c.

Code formatting

The code in this module is formatted using clang-format. An extension command for west has been created to facilitate formatting. Run west astarte-format --help to learn about the formatting options.

Static code analysis

CodeChecker is the one of the many natively supported static analysis tools in west. It can be configured to run different static checkers, such as clang-tidy and cppcheck.

Due to dependency conflicts with the zephyr venv it's suggested to install CodeChecker using pipx. The following command should suffice:

pipx install codechecker

An extension command for west has been created to facilitate running static analysis with clang-tidy. Run west astarte-static --help to read what this command performs.

Build doxygen documentation

The dependencies for generating the doxygen documentation are:

  • doxygen <= 1.9.6
  • graphviz

An extension command for west has been created to facilitate generating the documentation. Run west astarte-docs --help for more information on how to generate the documentation.

Architectural documentation

Some additional documentation for the library architecture is available.

  • Astarte MQTT client is a soft wrapper around the MQTT client library from Zephyr. It extends the functionality of the standard MQTT client.
  • Astarte device is the main entry point for any interaction with Astarte.
  • Astarte key-value storage is an overlay on the ZMS library that greatly extends the allowed keys length.