Skip to content

Commit 89bfd87

Browse files
committed
Refactor BACnet settings to use settings_load as iterator.
1 parent 6d24901 commit 89bfd87

File tree

10 files changed

+431
-312
lines changed

10 files changed

+431
-312
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ repos:
3838
- repo: https://github.com/Lucas-C/pre-commit-hooks
3939
rev: v1.5.5
4040
hooks:
41-
- id: remove-tabs
42-
name: Remove tabs (4 spaces)
43-
args: ["--whitespaces-count", "4"]
44-
exclude: \.(yaml|yml|html|htm|sln|atsln|layout)$|Makefile|\.(mgw|mak|MAK)$|Dockerfile$
4541
- id: remove-tabs
4642
name: Remove tabs (2 spaces)
4743
args: ["--whitespaces-count", "2"]
@@ -52,12 +48,6 @@ repos:
5248
hooks:
5349
- id: clang-format
5450

55-
# We might enable this in future.
56-
# - repo: https://github.com/pre-commit/mirrors-prettier
57-
# rev: ffb6a759a979008c0e6dff86e39f4745a2d9eac4 # v3.1.0
58-
# hooks:
59-
# - id: prettier
60-
6151
- repo: https://github.com/pre-commit/pygrep-hooks
6252
rev: v1.10.0
6353
hooks:

zephyr/include/bacnet_settings/bacnet_settings.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
#include "bacnet/bacint.h"
1818
#include "bacnet/wp.h"
1919

20+
/**
21+
* @brief Callback data for WriteProperty restore iterator
22+
* @param write_function The WriteProperty function to call
23+
* @param context The context to pass to the WriteProperty function
24+
* @return true if the WriteProperty succeeded
25+
*/
26+
typedef bool (*bacnet_settings_restore_callback)(BACNET_WRITE_PROPERTY_DATA *wp_data,
27+
void *context);
28+
2029
#ifdef __cplusplus
2130
extern "C" {
2231
#endif /* __cplusplus */
@@ -25,13 +34,7 @@ void bacnet_settings_basic_store(BACNET_OBJECT_TYPE object_type, uint32_t object
2534
BACNET_PROPERTY_ID object_property, BACNET_ARRAY_INDEX array_index,
2635
uint8_t *application_data, int application_data_len);
2736
bool bacnet_settings_write_property_store(BACNET_WRITE_PROPERTY_DATA *wp_data);
28-
bool bacnet_settings_restore(uint16_t object_type, uint32_t object_instance,
29-
uint32_t property_id, uint32_t array_index,
30-
const void *data, size_t data_len,
31-
write_property_function write_function);
32-
bool bacnet_settings_write_property_restore(uint16_t object_type, uint32_t object_instance,
33-
uint32_t property_id, uint32_t array_index,
34-
write_property_function write_function);
37+
bool bacnet_settings_write_property_restore(bacnet_settings_restore_callback cb, void *context);
3538

3639
int bacnet_settings_value_get(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
3740
uint32_t array_index, BACNET_APPLICATION_DATA_VALUE *value);
@@ -71,6 +74,8 @@ int bacnet_settings_string_get(uint16_t object_type, uint32_t object_instance, u
7174
bool bacnet_settings_string_set(uint16_t object_type, uint32_t object_instance,
7275
uint32_t property_id, uint32_t array_index, const char *value);
7376

77+
bool bacnet_settings_init(void);
78+
7479
#ifdef __cplusplus
7580
}
7681
#endif /* __cplusplus */

zephyr/include/bacnet_settings/bacnet_storage.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <stddef.h>
1313
#include <stdint.h>
14+
#include <stdbool.h>
15+
#include <errno.h>
1416
#include <zephyr/settings/settings.h>
1517

1618
#define BACNET_STORAGE_VALUE_SIZE_MAX SETTINGS_MAX_VAL_LEN
@@ -25,19 +27,32 @@ typedef struct bacnet_storage_key_t {
2527
} BACNET_STORAGE_KEY;
2628

2729
typedef int (*bacnet_storage_restore_callback)(BACNET_STORAGE_KEY *key, const void *data,
28-
size_t data_size);
30+
size_t data_size, void *context);
2931

3032
#ifdef __cplusplus
3133
extern "C" {
3234
#endif /* __cplusplus */
3335

34-
int bacnet_storage_init(bacnet_storage_restore_callback cb);
36+
int bacnet_storage_init(void);
37+
38+
int bacnet_storage_load_callback_set(bacnet_storage_restore_callback cb, void *context);
39+
int bacnet_storage_load(void);
40+
41+
int bacnet_storage_handler_set(const char *name, size_t len, settings_read_cb read_cb,
42+
void *cb_arg);
43+
int bacnet_storage_handler_commit(void);
44+
int bacnet_storage_handler_export(int (*cb)(const char *name, const void *value, size_t val_len));
45+
46+
bool bacnet_storage_strtoul(const char *search_name, unsigned long *long_value);
47+
3548
void bacnet_storage_key_init(BACNET_STORAGE_KEY *key, uint16_t object_type,
3649
uint32_t object_instance, uint32_t property_id, uint32_t array_index);
3750
int bacnet_storage_key_decode(const char *path, BACNET_STORAGE_KEY *key);
3851
int bacnet_storage_key_encode(char *buffer, size_t buffer_size, BACNET_STORAGE_KEY *key);
52+
3953
int bacnet_storage_set(BACNET_STORAGE_KEY *key, const void *data, size_t data_size);
4054
int bacnet_storage_get(BACNET_STORAGE_KEY *key, void *data, size_t data_size);
55+
int bacnet_storage_delete(BACNET_STORAGE_KEY *key);
4156

4257
#ifdef __cplusplus
4358
}

zephyr/samples/profiles/b-ld/src/main.c

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,25 @@ void BACnet_Lighting_Output_Tracking_Value_Handler(uint32_t object_instance, flo
6464
(double)value, (unsigned)steps, (unsigned)UINT16_MAX);
6565
}
6666

67+
/**
68+
* @brief Callback data for WriteProperty restore iterator
69+
* @param write_function The WriteProperty function to call
70+
* @param context The context to pass to the WriteProperty function
71+
* @return true if the WriteProperty succeeded
72+
*/
73+
static bool Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void *context)
74+
{
75+
(void)context;
76+
return Device_Write_Property(wp_data);
77+
}
78+
6779
/**
6880
* @brief BACnet Project Initialization Handler
6981
* @param context [in] The context to pass to the callback function
7082
* @note This is called from the BACnet task
7183
*/
7284
static void BACnet_Lighting_Device_Init_Handler(void *context)
7385
{
74-
uint32_t array_index = BACNET_ARRAY_ALL;
75-
bool status = false;
76-
int i;
77-
int32_t lighting_output_writeable_property_list[] = {
78-
/* list of properties to set via WriteProperty */
79-
PROP_OUT_OF_SERVICE,
80-
PROP_DEFAULT_FADE_TIME,
81-
PROP_DEFAULT_RAMP_RATE,
82-
PROP_DEFAULT_STEP_INCREMENT,
83-
PROP_TRANSITION,
84-
PROP_PRESENT_VALUE,
85-
PROP_RELINQUISH_DEFAULT,
86-
PROP_BLINK_WARN_ENABLE,
87-
PROP_EGRESS_TIME,
88-
PROP_DEFAULT_FADE_TIME,
89-
PROP_DEFAULT_RAMP_RATE,
90-
PROP_LIGHTING_COMMAND_DEFAULT_PRIORITY,
91-
PROP_DEFAULT_STEP_INCREMENT,
92-
PROP_TRANSITION};
93-
int32_t device_writeable_property_list[] = {
94-
/* list of properties to set via WriteProperty */
95-
PROP_OBJECT_IDENTIFIER,
96-
PROP_OBJECT_NAME,
97-
};
98-
9986
(void)context;
10087
LOG_INF("BACnet Stack Initialized");
10188
/* initialize objects with default values for this basic sample */
@@ -105,25 +92,10 @@ static void BACnet_Lighting_Device_Init_Handler(void *context)
10592
Lighting_Output_Create(Lighting_Instance);
10693
Lighting_Output_Name_Set(Lighting_Instance, "Light-1");
10794
/* restore any property values previously stored via WriteProperty */
108-
for (i = 0; i < ARRAY_SIZE(device_writeable_property_list); i++) {
109-
status = bacnet_settings_write_property_restore(
110-
OBJECT_DEVICE, BACNET_MAX_INSTANCE, device_writeable_property_list[i],
111-
array_index, Device_Write_Property_Local);
112-
if (!status) {
113-
/* no settings stored for this property, use defaults */
114-
}
115-
}
116-
for (i = 0; i < ARRAY_SIZE(lighting_output_writeable_property_list); i++) {
117-
status = bacnet_settings_write_property_restore(
118-
OBJECT_LIGHTING_OUTPUT, Lighting_Instance,
119-
lighting_output_writeable_property_list[i], array_index,
120-
Lighting_Output_Write_Property);
121-
if (!status) {
122-
/* no settings stored for this property, use defaults */
123-
}
124-
}
125-
/* These writable property values are stored WriteProperty.
126-
Set this callback after init to prevent recursion. */
95+
bacnet_settings_init();
96+
bacnet_settings_write_property_restore(&Settings_Restore_Callback, NULL);
97+
/* writable property values are stored with WriteProperty.
98+
Set this callback after restore to prevent recursion. */
12799
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
128100
/* lighting output callbacks */
129101
Lighting_Output_Write_Present_Value_Callback_Set(

zephyr/samples/profiles/b-ls/src/main.c

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ void Binary_Lighting_Output_Blink_Warn_Handler(uint32_t object_instance)
101101
LOG_INF("Binary Lighting Output[%lu]: Blink Warning", (unsigned long)object_instance);
102102
}
103103

104+
/**
105+
* @brief Callback data for WriteProperty restore iterator
106+
* @param write_function The WriteProperty function to call
107+
* @param context The context to pass to the WriteProperty function
108+
* @return true if the WriteProperty succeeded
109+
*/
110+
static bool Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void *context)
111+
{
112+
(void)context;
113+
return Device_Write_Property(wp_data);
114+
}
115+
104116
/**
105117
* @brief BACnet Project Initialization Handler
106118
* @param context [in] The context to pass to the callback function
@@ -109,35 +121,6 @@ void Binary_Lighting_Output_Blink_Warn_Handler(uint32_t object_instance)
109121
static void BACnet_Lighting_Device_Init_Handler(void *context)
110122
{
111123
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE member;
112-
uint32_t array_index = BACNET_ARRAY_ALL;
113-
bool status = false;
114-
int i;
115-
int32_t channel_writeable_property_list[] = {
116-
/* list of properties to set via WriteProperty */
117-
PROP_PRESENT_VALUE, PROP_OUT_OF_SERVICE, PROP_LIST_OF_OBJECT_PROPERTY_REFERENCES,
118-
PROP_CHANNEL_NUMBER, PROP_CONTROL_GROUPS,
119-
};
120-
int32_t lighting_output_writeable_property_list[] = {
121-
/* list of properties to set via WriteProperty */
122-
PROP_OUT_OF_SERVICE,
123-
PROP_DEFAULT_FADE_TIME,
124-
PROP_DEFAULT_RAMP_RATE,
125-
PROP_DEFAULT_STEP_INCREMENT,
126-
PROP_TRANSITION,
127-
PROP_PRESENT_VALUE,
128-
PROP_RELINQUISH_DEFAULT,
129-
PROP_BLINK_WARN_ENABLE,
130-
PROP_EGRESS_TIME,
131-
PROP_DEFAULT_FADE_TIME,
132-
PROP_DEFAULT_RAMP_RATE,
133-
PROP_LIGHTING_COMMAND_DEFAULT_PRIORITY,
134-
PROP_DEFAULT_STEP_INCREMENT,
135-
PROP_TRANSITION};
136-
int32_t device_writeable_property_list[] = {
137-
/* list of properties to set via WriteProperty */
138-
PROP_OBJECT_IDENTIFIER,
139-
PROP_OBJECT_NAME,
140-
};
141124

142125
(void)context;
143126
LOG_INF("BACnet Stack Initialized");
@@ -164,33 +147,10 @@ static void BACnet_Lighting_Device_Init_Handler(void *context)
164147
member.deviceIdentifier.instance = Device_Instance;
165148
Channel_Reference_List_Member_Element_Set(Channel_Instance, 1, &member);
166149
/* restore any property values previously stored via WriteProperty */
167-
for (i = 0; i < ARRAY_SIZE(device_writeable_property_list); i++) {
168-
status = bacnet_settings_write_property_restore(
169-
OBJECT_DEVICE, BACNET_MAX_INSTANCE, device_writeable_property_list[i],
170-
array_index, Device_Write_Property_Local);
171-
if (!status) {
172-
/* no settings stored for this property, use defaults */
173-
}
174-
}
175-
for (i = 0; i < ARRAY_SIZE(lighting_output_writeable_property_list); i++) {
176-
status = bacnet_settings_write_property_restore(
177-
OBJECT_LIGHTING_OUTPUT, Lighting_Instance,
178-
lighting_output_writeable_property_list[i], array_index,
179-
Lighting_Output_Write_Property);
180-
if (!status) {
181-
/* no settings stored for this property, use defaults */
182-
}
183-
}
184-
for (i = 0; i < ARRAY_SIZE(channel_writeable_property_list); i++) {
185-
status = bacnet_settings_write_property_restore(
186-
OBJECT_CHANNEL, Channel_Instance, channel_writeable_property_list[i],
187-
array_index, Channel_Write_Property);
188-
if (!status) {
189-
/* no settings stored for this property, use defaults */
190-
}
191-
}
192-
/* These writable property values are stored WriteProperty.
193-
Set this callback after init to prevent recursion. */
150+
bacnet_settings_init();
151+
bacnet_settings_write_property_restore(&Settings_Restore_Callback, NULL);
152+
/* writable property values are stored with WriteProperty.
153+
Set this callback after restore to prevent recursion. */
194154
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
195155
/* lighting output callbacks */
196156
Lighting_Output_Write_Present_Value_Callback_Set(Lighting_Output_Tracking_Value_Handler);

zephyr/samples/profiles/b-sa/src/main.c

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,25 @@ static void BACnet_Smart_Actuator_Datalink_Init(void)
4444
/* nothing to do */
4545
}
4646

47+
/**
48+
* @brief Callback data for WriteProperty restore iterator
49+
* @param write_function The WriteProperty function to call
50+
* @param context The context to pass to the WriteProperty function
51+
* @return true if the WriteProperty succeeded
52+
*/
53+
static bool Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void *context)
54+
{
55+
(void)context;
56+
return Device_Write_Property(wp_data);
57+
}
58+
4759
/**
4860
* @brief BACnet Project Initialization Handler
4961
* @param context [in] The context to pass to the callback function
5062
* @note This is called from the BACnet task
5163
*/
5264
static void BACnet_Smart_Actuator_Init_Handler(void *context)
5365
{
54-
uint32_t array_index = BACNET_ARRAY_ALL;
55-
bool status = false;
56-
int i;
57-
int32_t analog_output_writeable_property_list[] = {
58-
/* list of properties to set via WriteProperty */
59-
PROP_OUT_OF_SERVICE, PROP_PRESENT_VALUE, PROP_UNITS,
60-
PROP_COV_INCREMENT, PROP_MIN_PRES_VALUE, PROP_MAX_PRES_VALUE,
61-
};
62-
int32_t device_writeable_property_list[] = {
63-
/* list of properties to set via WriteProperty */
64-
PROP_OBJECT_IDENTIFIER,
65-
PROP_OBJECT_NAME,
66-
};
67-
6866
(void)context;
6967
LOG_INF("BACnet Stack Initialized");
7068
BACnet_Smart_Actuator_Datalink_Init();
@@ -78,25 +76,10 @@ static void BACnet_Smart_Actuator_Init_Handler(void *context)
7876
Analog_Output_Min_Pres_Value_Set(Actuator_Instance, 0.0f);
7977
Analog_Output_Max_Pres_Value_Set(Actuator_Instance, 100.0f);
8078
/* restore any property values previously stored via WriteProperty */
81-
for (i = 0; i < ARRAY_SIZE(device_writeable_property_list); i++) {
82-
status = bacnet_settings_write_property_restore(
83-
OBJECT_DEVICE, BACNET_MAX_INSTANCE, device_writeable_property_list[i],
84-
array_index, Device_Write_Property_Local);
85-
if (!status) {
86-
/* no settings stored for this property, use defaults */
87-
}
88-
}
89-
for (i = 0; i < ARRAY_SIZE(analog_output_writeable_property_list); i++) {
90-
status = bacnet_settings_write_property_restore(
91-
OBJECT_ANALOG_OUTPUT, Actuator_Instance,
92-
analog_output_writeable_property_list[i], array_index,
93-
Analog_Output_Write_Property);
94-
if (!status) {
95-
/* no settings stored for this property, use defaults */
96-
}
97-
}
98-
/* These writable property values are stored WriteProperty.
99-
Set this callback after init to prevent recursion. */
79+
bacnet_settings_init();
80+
bacnet_settings_write_property_restore(&Settings_Restore_Callback, NULL);
81+
/* writable property values are stored with WriteProperty.
82+
Set this callback after restore to prevent recursion. */
10083
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
10184
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
10285
/* start the seconds cyclic timer */

0 commit comments

Comments
 (0)