Skip to content

Commit

Permalink
feat: support C++ building (#77)
Browse files Browse the repository at this point in the history
* chore: minor templates cleanup

* feat: support C++ building

Increase minimum supported version to properly support this change.

This will allow to provide more flexibility in how code is implemented.
Also this will reduce number of required templates to define.
  • Loading branch information
ffenix113 authored Sep 27, 2024
1 parent 54f141e commit 044e5ef
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 43 deletions.
13 changes: 12 additions & 1 deletion config/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Board struct {
}

func ParseFromFile(configPath string) (*Device, error) {
var minimumNCSVersion = types.NewSemver(2, 6, 0)

cfg := &Device{
General: General{
RunEvery: time.Minute,
Expand All @@ -68,7 +70,7 @@ func ParseFromFile(configPath string) (*Device, error) {
}
return "~/ncs"
}(),
NCSVersion: "v2.6.1",
NCSVersion: minimumNCSVersion.String(),
Manufacturer: "FFexix113",
DeviceName: "dongle",
},
Expand All @@ -86,6 +88,15 @@ func ParseFromFile(configPath string) (*Device, error) {
return nil, fmt.Errorf("unmarshal config file: %w", err)
}

selectedNCSVersion, err := types.ParseSemver(cfg.General.NCSVersion)
if err != nil {
return nil, fmt.Errorf("could not parse selected NCS version: %w", err)
}

if minimumNCSVersion.Compare(selectedNCSVersion) == 1 {
return nil, fmt.Errorf("selected NCS version is lower than minimum supported version: selected %q, minimum supported %q", cfg.General.NCSVersion, minimumNCSVersion)
}

return cfg, nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/set_toolchain_version/zigbee.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
general:
board: nrf52840dongle_nrf52840
# We can force specific version of toolchain (if it is available).
ncs_version: v2.5.0
ncs_version: v2.6.2
10 changes: 5 additions & 5 deletions templates/extenders/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (Sensor) AppConfig() []appconfig.ConfigValue {

// Includes implements templates.Extender.
func (Sensor) Includes() []string {
return []string{"zephyr/drivers/sensor.h", "zbhome_sensor.h"}
return []string{"zephyr/drivers/sensor.h", "zbhome_sensor.hpp"}
}

// Template implements templates.Extender.
Expand All @@ -35,12 +35,12 @@ func (Sensor) Template() string {
func (Sensor) WriteFiles() []generator.WriteFile {
return []generator.WriteFile{
{
FileName: "zbhome_sensor.h",
TemplateName: "zbhome_sensor.h",
FileName: "zbhome_sensor.hpp",
TemplateName: "zbhome_sensor.hpp",
},
{
FileName: "zbhome_sensor.c",
TemplateName: "zbhome_sensor.c",
FileName: "zbhome_sensor.cpp",
TemplateName: "zbhome_sensor.cpp",
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion templates/src/CMakeLists.txt.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ project(zigbee_common)
################################################################################

# NORDIC SDK APP START
FILE(GLOB app_sources_cpp src/*.cpp src/**/*.cpp)
FILE(GLOB app_sources src/*.c src/**/*.c)
target_sources(app PRIVATE ${app_sources})
target_sources(app
PRIVATE ${app_sources_cpp}
PRIVATE ${app_sources}
)

# NORDIC SDK APP END
3 changes: 2 additions & 1 deletion templates/src/device.h.tpl → templates/src/device.hpp.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "clusters.h"
#include <zboss_api_addons.h>
#include "clusters.hpp"

/* Delay for console initialization */
#define WAIT_FOR_CONSOLE_MSEC 100
Expand Down
4 changes: 4 additions & 0 deletions templates/src/extenders/sensors/device_temperature.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ if (err != NRFX_SUCCESS)
}
{{ end}}

{{ define "top_level" }}
{{/* No need to define a device, as it is handled by NRFX lib */}}
{{- end}}

{{ define "loop"}}
int res = nrfx_temp_measure();
if (res != NRFX_SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <zboss_api.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/sensor.h>

#include "zbhome_sensor.h"
#include "clusters.h"
#include "zbhome_sensor.hpp"
#include "clusters.hpp"

LOG_MODULE_DECLARE(app, LOG_LEVEL_INF);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef ZBHOME_SENSOR_H
#define ZBHOME_SENSOR_H
#pragma once

#define GENERATE_ATTR_VAL_SETTER(cluster_name, cluster) \
zb_zcl_status_t zbhome_set_attr_val_for_##cluster_name(int endpoint, zb_uint8_t * data_ptr) { \
Expand Down Expand Up @@ -51,5 +50,3 @@ NAME_GENERATE_SENSOR_FULL_FOR_ATTR(pressure);
#ifdef ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE
NAME_GENERATE_SENSOR_FULL_FOR_ATTR(carbon_dioxide);
#endif

#endif
26 changes: 17 additions & 9 deletions templates/src/main.c.tpl → templates/src/main.cpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@

// Generated by zigbee_home on {{ .GeneratedOn }}, version {{ .Version }}

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <dk_buttons_and_leds.h>
// #include <zephyr/drivers/uart.h>
#include <zephyr/logging/log.h>
#include <ram_pwrdn.h>
#include <dk_buttons_and_leds.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <zb_nrf_platform.h>
#include <zboss_api.h>
#include <zboss_api_addons.h>
#include <zephyr/kernel.h>
#ifdef __cplusplus
}
#endif

#include <zigbee/zigbee_app_utils.h>
#include <zigbee/zigbee_error_handler.h>

// Header only, why not?
#include "device.h"
#include "device.hpp"

// Extender includes
{{- range .Extenders}}
Expand Down Expand Up @@ -192,7 +198,7 @@ static void zcl_device_cb(zb_bufid_t bufid)
ZB_FALSE);
switch (cluster_id) {
case ZB_ZCL_CLUSTER_ID_ON_OFF:
case ZB_ZCL_CLUSTER_ID_ON_OFF: {
uint8_t value =
device_cb_param->cb_param.set_attr_value_param
.values.data8;
Expand All @@ -211,13 +217,15 @@ static void zcl_device_cb(zb_bufid_t bufid)
{{- end}}
}
break;
default:
}
default: {
/* Other clusters can be processed here */
LOG_INF("Unhandled cluster attribute id: %d",
cluster_id);
device_cb_param->status = RET_NOT_IMPLEMENTED;
break;
}
}

break;

Expand Down
10 changes: 1 addition & 9 deletions templates/src/zigbee/carbon_dioxide.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void zb_zcl_carbon_dioxide_init_server()
}

#define ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_SERVER_ROLE_INIT zb_zcl_carbon_dioxide_init_server
#define ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_CLIENT_ROLE_INIT (NULL)
#define ZB_ZCL_CLUSTER_ID_CARBON_DIOXIDE_CLIENT_ROLE_INIT ((zb_zcl_cluster_init_t)NULL)


typedef void * zb_voidp_t;
Expand All @@ -36,9 +36,7 @@ typedef void * zb_voidp_t;
ZB_ZCL_ATTR_CARBON_DIOXIDE_VALUE_ID, \
ZB_ZCL_ATTR_TYPE_SINGLE, \
ZB_ZCL_ATTR_ACCESS_READ_ONLY | ZB_ZCL_ATTR_ACCESS_REPORTING, \
{{ if not ncsVersionIs_2_5 -}}
(ZB_UINT16_MAX), \
{{ end -}}
(zb_voidp_t) data_ptr \
}

Expand All @@ -47,9 +45,7 @@ typedef void * zb_voidp_t;
ZB_ZCL_ATTR_CARBON_DIOXIDE_MIN_VALUE_ID, \
ZB_ZCL_ATTR_TYPE_SINGLE, \
ZB_ZCL_ATTR_ACCESS_READ_ONLY, \
{{ if not ncsVersionIs_2_5 -}}
(ZB_UINT16_MAX), \
{{ end -}}
(zb_voidp_t) data_ptr \
}

Expand All @@ -58,9 +54,7 @@ typedef void * zb_voidp_t;
ZB_ZCL_ATTR_CARBON_DIOXIDE_MAX_VALUE_ID, \
ZB_ZCL_ATTR_TYPE_SINGLE, \
ZB_ZCL_ATTR_ACCESS_READ_ONLY, \
{{ if not ncsVersionIs_2_5 -}}
(ZB_UINT16_MAX), \
{{ end -}}
(zb_voidp_t) data_ptr \
}

Expand All @@ -69,9 +63,7 @@ typedef void * zb_voidp_t;
ZB_ZCL_ATTR_CARBON_DIOXIDE_TOLERANCE_ID, \
ZB_ZCL_ATTR_TYPE_SINGLE, \
ZB_ZCL_ATTR_ACCESS_READ_ONLY, \
{{ if not ncsVersionIs_2_5 -}}
(ZB_UINT16_MAX), \
{{ end -}}
(zb_voidp_t) data_ptr \
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif
#include <zboss_api.h>
#ifdef __cplusplus
}
#endif

#define MANUFACTURER_CODE ZB_ZCL_MANUF_CODE_INVALID

/* Temperature sensor device version */
Expand Down
4 changes: 1 addition & 3 deletions templates/src/zigbee/device_temperature.c.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void zb_zcl_device_temp_config_init_server()
}

#define ZB_ZCL_CLUSTER_ID_DEVICE_TEMP_CONFIG_SERVER_ROLE_INIT zb_zcl_device_temp_config_init_server
#define ZB_ZCL_CLUSTER_ID_DEVICE_TEMP_CONFIG_CLIENT_ROLE_INIT (NULL)
#define ZB_ZCL_CLUSTER_ID_DEVICE_TEMP_CONFIG_CLIENT_ROLE_INIT ((zb_zcl_cluster_init_t)NULL)


typedef void * zb_voidp_t;
Expand All @@ -26,9 +26,7 @@ typedef void * zb_voidp_t;
ZB_ZCL_ATTR_DEVICE_TEMP_CONFIG_CURRENT_TEMPERATURE_ID, \
ZB_ZCL_ATTR_TYPE_U16, \
ZB_ZCL_ATTR_ACCESS_READ_ONLY, \
{{ if not ncsVersionIs_2_5 -}}
(ZB_UINT16_MAX), \
{{ end -}}
(zb_voidp_t) data_ptr \
}

Expand Down
2 changes: 1 addition & 1 deletion templates/src/zigbee/water_content.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void zb_zcl_soil_moisture_init_server()
}

#define ZB_ZCL_CLUSTER_ID_SOIL_MOISTURE_SERVER_ROLE_INIT zb_zcl_soil_moisture_init_server
#define ZB_ZCL_CLUSTER_ID_SOIL_MOISTURE_CLIENT_ROLE_INIT (NULL)
#define ZB_ZCL_CLUSTER_ID_SOIL_MOISTURE_CLIENT_ROLE_INIT ((zb_zcl_cluster_init_t)NULL)
{{end}}
{{end}}

Expand Down
7 changes: 3 additions & 4 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ var knownClusterTemplates = map[cluster.ID]string{
var sourceFiles = [][2]string{
{path.Join("..", "CMakeLists.txt"), "CMakeLists.txt.tpl"},
{path.Join("..", "Kconfig"), "Kconfig.tpl"},
{"main.c", "main.c.tpl"},
{"device.h", "device.h.tpl"},
{"clusters.h", "clusters.h.tpl"},
{"main.cpp", "main.cpp.tpl"},
{"device.hpp", "device.hpp.tpl"},
{"clusters.hpp", "clusters.hpp.tpl"},
}

var knownExtenders = [...]string{
Expand Down Expand Up @@ -122,7 +122,6 @@ func NewTemplates(templateFS fs.FS, ncsVersion types.Semver) *Templates {
// Specific functions to check exact version
// so we would know where each one is used,
// and what we can deprecate.
"ncsVersionIs_2_5": ncsVersionIs(ncsVersion, types.Semver{2, 5, 0}),
"ncsVersionIs_2_6": ncsVersionIs(ncsVersion, types.Semver{2, 6, 0}),
})

Expand Down
3 changes: 3 additions & 0 deletions types/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type DefaultAppConfigOptions struct {

func NewDefaultAppConfig(opts DefaultAppConfigOptions) (*AppConfig, error) {
appConfig := NewEmptyAppConfig().AddValue(
CONFIG_CPP,
CONFIG_DK_LIBRARY,
CONFIG_ZIGBEE,
CONFIG_ZIGBEE_APP_UTILS,
Expand All @@ -117,6 +118,7 @@ func NewDefaultAppConfig(opts DefaultAppConfigOptions) (*AppConfig, error) {

if len(opts.ZigbeeChannels) != 0 {
channel := int32(0)

for _, chann := range opts.ZigbeeChannels {
if chann < 11 || chann > 26 {
return nil, fmt.Errorf("zigbee channels must be in range [11, 26], but have %d", chann)
Expand Down Expand Up @@ -148,6 +150,7 @@ func (c *AppConfig) AddValue(configValues ...ConfigValue) *AppConfig {
}

c.values[dep.Name] = dep

continue
}
}
Expand Down
3 changes: 3 additions & 0 deletions types/appconfig/known.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package appconfig
// As such they do not represent good/best configurations,
// but mostly the ones that work for this project.
var (
// Zephyr config
CONFIG_CPP = NewValue("CONFIG_CPP").Default(Yes)

// Logging
CONFIG_LOG = NewValue("CONFIG_LOG").Default(No)
CONFIG_SERIAL = NewValue("CONFIG_SERIAL").Default(No)
Expand Down
2 changes: 1 addition & 1 deletion types/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Semver [3]uint8

var versionRegx = regexp.MustCompile(`^v?(\d+)\.(\d+)(?:\.(\d+))?$`)
var versionRegx = regexp.MustCompile(`^v?(\d+)\.(\d+)(?:\.(\d+))?(?:-.*)?$`)

func NewSemver(major, minor, patch uint8) Semver {
return Semver{major, minor, patch}
Expand Down

0 comments on commit 044e5ef

Please sign in to comment.