Skip to content

Commit 1c97d20

Browse files
committed
Updated zephyr sample profiles B-LD B-LS B-SA and B-SS to use BACnet settings subsys.
1 parent 50d43fb commit 1c97d20

File tree

7 files changed

+106
-29
lines changed

7 files changed

+106
-29
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude: build|\.git|external/|BACnet\-Server\.X/|tools/(avstack|check\-stack\-u
1616

1717
repos:
1818
- repo: https://github.com/pre-commit/pre-commit-hooks
19-
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # v4.6.0
19+
rev: v5.0.0
2020
hooks:
2121
- id: check-merge-conflict
2222
args: [--assume-in-merge]
@@ -36,7 +36,7 @@ repos:
3636
- id: trailing-whitespace
3737

3838
- repo: https://github.com/Lucas-C/pre-commit-hooks
39-
rev: a30f0d816e5062a67d87c8de753cfe499672b959 # v1.5.5
39+
rev: v1.5.5
4040
hooks:
4141
- id: remove-tabs
4242
name: Remove tabs (4 spaces)
@@ -48,7 +48,7 @@ repos:
4848
files: '.*\.(yaml|yml|html|htm)|Dockerfile$'
4949

5050
- repo: https://github.com/pre-commit/mirrors-clang-format
51-
rev: 05241dc3def184dba136e62d54ff57f1c8a497a9 # v17.0.6
51+
rev: v20.1.5
5252
hooks:
5353
- id: clang-format
5454

@@ -59,12 +59,12 @@ repos:
5959
# - id: prettier
6060

6161
- repo: https://github.com/pre-commit/pygrep-hooks
62-
rev: v1.10.0 # v1.10.0
62+
rev: v1.10.0
6363
hooks:
6464
- id: text-unicode-replacement-char
6565

6666
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
67-
rev: 3.2.0 #2.6.2
67+
rev: 3.2.0
6868
hooks:
6969
- id: editorconfig-checker
7070
alias: ec

zephyr/include/bacnet_settings/bacnet_settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
extern "C" {
2222
#endif /* __cplusplus */
2323

24+
void bacnet_settings_basic_store(BACNET_OBJECT_TYPE object_type, uint32_t object_instance,
25+
BACNET_PROPERTY_ID object_property, BACNET_ARRAY_INDEX array_index,
26+
uint8_t *application_data, int application_data_len);
2427
bool bacnet_settings_write_property_store(BACNET_WRITE_PROPERTY_DATA *wp_data);
2528
bool bacnet_settings_write_property_restore(uint16_t object_type, uint32_t object_instance,
2629
uint32_t property_id, uint32_t array_index,

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

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
#include "bacnet/basic/server/bacnet_basic.h"
2727
#include "bacnet/basic/server/bacnet_port.h"
2828

29+
/* BACnet Stack Zephyr services */
30+
#include <bacnet_settings/bacnet_settings.h>
2931
/* Logging module registration is already done in ports/zephyr/main.c */
30-
#include "bacnet_osif/bacnet_log.h"
32+
#include <bacnet_osif/bacnet_log.h>
3133
LOG_MODULE_DECLARE(bacnet, CONFIG_BACNETSTACK_LOG_LEVEL);
3234

33-
/* FIXME: get the device instance and name from settings! */
35+
/* Default values before we get the device instance and name from settings */
3436
static const char *Device_Name = "BACnet Lighting Device (B-LD)";
37+
static const uint32_t Device_Instance = 260125;
3538
/* object instances */
3639
static const uint32_t Lighting_Instance = 1;
3740

@@ -68,14 +71,64 @@ void BACnet_Lighting_Output_Tracking_Value_Handler(uint32_t object_instance, flo
6871
*/
6972
static void BACnet_Lighting_Device_Init_Handler(void *context)
7073
{
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+
7199
(void)context;
72100
LOG_INF("BACnet Stack Initialized");
73-
/* initialize objects for this basic sample */
101+
/* initialize objects with default values for this basic sample */
102+
Device_Set_Object_Instance_Number(Device_Instance);
74103
Device_Object_Name_ANSI_Init(Device_Name);
104+
/* lighting output object */
75105
Lighting_Output_Create(Lighting_Instance);
76106
Lighting_Output_Name_Set(Lighting_Instance, "Light-1");
107+
/* 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. */
127+
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
128+
/* lighting output callbacks */
77129
Lighting_Output_Write_Present_Value_Callback_Set(
78130
BACnet_Lighting_Output_Tracking_Value_Handler);
131+
/* done */
79132
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
80133
/* set the BACnet Basic Task device object timer for lighting output use */
81134
bacnet_basic_task_object_timer_set(10UL);
@@ -94,24 +147,22 @@ static void BACnet_Lighting_Device_Task_Handler(void *context)
94147

95148
int main(void)
96149
{
150+
bool port_initialized = false;
151+
97152
LOG_INF("BACnet Device: %s", Device_Name);
98153
LOG_INF("BACnet Stack Version " BACNET_VERSION_TEXT);
99154
LOG_INF("BACnet Stack Max APDU: %d", MAX_APDU);
100155
bacnet_basic_init_callback_set(BACnet_Lighting_Device_Init_Handler, NULL);
101156
bacnet_basic_task_callback_set(BACnet_Lighting_Device_Task_Handler, NULL);
102157
bacnet_basic_init();
103-
for (;;) {
104-
if (bacnet_port_init()) {
105-
break;
106-
} else {
107-
LOG_ERR("Server: port initialization failed");
108-
k_sleep(K_MSEC(1000));
109-
}
110-
}
111158
for (;;) {
112159
k_sleep(K_MSEC(CONFIG_BACNET_BASIC_SERVER_KSLEEP));
113160
bacnet_basic_task();
114-
bacnet_port_task();
161+
if (port_initialized) {
162+
bacnet_port_task();
163+
} else {
164+
port_initialized = bacnet_port_init();
165+
}
115166
}
116167

117168
return 0;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <bacnet_osif/bacnet_log.h>
3737
LOG_MODULE_DECLARE(bacnet, CONFIG_BACNETSTACK_LOG_LEVEL);
3838

39-
/* FIXME: get the device instance and name from settings! */
39+
/* Default values before we get the device instance and name from settings */
4040
static const uint32_t Device_Instance = 260126;
4141
static const char *Device_Name = "BACnet Lighting Supervisor (B-LS)";
4242
/* instances for our objects */
@@ -141,20 +141,15 @@ static void BACnet_Lighting_Device_Init_Handler(void *context)
141141

142142
(void)context;
143143
LOG_INF("BACnet Stack Initialized");
144-
/* initialize objects for this basic sample */
145-
Device_Init(NULL);
144+
/* initialize objects with default values for this basic sample */
146145
Device_Set_Object_Instance_Number(Device_Instance);
147146
Device_Object_Name_ANSI_Init(Device_Name);
148147
/* lighting output object */
149148
Lighting_Output_Create(Lighting_Instance);
150149
Lighting_Output_Name_Set(Lighting_Instance, "Light-1");
151-
Lighting_Output_Write_Present_Value_Callback_Set(Lighting_Output_Tracking_Value_Handler);
152150
/* binary lighting output object */
153151
Binary_Lighting_Output_Create(Binary_Lighting_Instance);
154152
Binary_Lighting_Output_Name_Set(Binary_Lighting_Instance, "Binary-Light-1");
155-
Binary_Lighting_Output_Write_Value_Callback_Set(
156-
Binary_Lighting_Output_Present_Value_Handler);
157-
Binary_Lighting_Output_Blink_Warn_Callback_Set(Binary_Lighting_Output_Blink_Warn_Handler);
158153
/* channel object */
159154
Channel_Create(Channel_Instance);
160155
Channel_Name_Set(Channel_Instance, "Lights");
@@ -196,7 +191,12 @@ static void BACnet_Lighting_Device_Init_Handler(void *context)
196191
}
197192
/* These writable property values are stored WriteProperty.
198193
Set this callback after init to prevent recursion. */
199-
Device_Write_Property_Store_Callback_Set(bacnet_settings_write_property_store);
194+
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
195+
/* lighting output callbacks */
196+
Lighting_Output_Write_Present_Value_Callback_Set(Lighting_Output_Tracking_Value_Handler);
197+
Binary_Lighting_Output_Write_Value_Callback_Set(
198+
Binary_Lighting_Output_Present_Value_Handler);
199+
Binary_Lighting_Output_Blink_Warn_Callback_Set(Binary_Lighting_Output_Blink_Warn_Handler);
200200
/* link WriteGroup service to our channel object */
201201
Write_Group_Notification.callback = Channel_Write_Group;
202202
handler_write_group_notification_add(&Write_Group_Notification);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <bacnet_osif/bacnet_log.h>
3232
LOG_MODULE_DECLARE(bacnet, CONFIG_BACNETSTACK_LOG_LEVEL);
3333

34-
/* FIXME: get the device instance and name from settings! */
34+
/* Default values before we get the device instance and name from settings */
3535
static const uint32_t Device_Instance = 260124;
3636
static const char *Device_Name = "BACnet Smart Actuator (B-SA)";
3737
/* object instances */
@@ -97,7 +97,7 @@ static void BACnet_Smart_Actuator_Init_Handler(void *context)
9797
}
9898
/* These writable property values are stored WriteProperty.
9999
Set this callback after init to prevent recursion. */
100-
Device_Write_Property_Store_Callback_Set(bacnet_settings_write_property_store);
100+
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
101101
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
102102
/* start the seconds cyclic timer */
103103
mstimer_set(&Actuator_Update_Timer, 1000);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <zephyr/random/random.h>
1010
#include <stdint.h>
1111
#include <stdlib.h>
12-
1312
/* BACnet Stack defines - first */
1413
#include "bacnet/bacdef.h"
1514
/* BACnet Stack core API */
@@ -31,7 +30,9 @@
3130
#include <bacnet_osif/bacnet_log.h>
3231
LOG_MODULE_DECLARE(bacnet, CONFIG_BACNETSTACK_LOG_LEVEL);
3332

33+
/* Default values before we get the device instance and name from settings */
3434
static const char *Device_Name = "BACnet Smart Sensor (B-SS)";
35+
static const uint32_t Device_Instance = 260121;
3536
/* object instances */
3637
static const uint32_t Sensor_Instance = 1;
3738
/* timer for Sensor Update Interval */
@@ -64,6 +65,7 @@ static void BACnet_Smart_Sensor_Init_Handler(void *context)
6465
(void)context;
6566
LOG_INF("BACnet Stack Initialized");
6667
/* initialize objects with default values for this basic sample */
68+
Device_Set_Object_Instance_Number(Device_Instance);
6769
Device_Object_Name_ANSI_Init(Device_Name);
6870
Analog_Input_Create(Sensor_Instance);
6971
Analog_Input_Name_Set(Sensor_Instance, "Sensor");
@@ -90,7 +92,7 @@ static void BACnet_Smart_Sensor_Init_Handler(void *context)
9092
}
9193
/* These writable property values are stored WriteProperty.
9294
Set this callback after init to prevent recursion. */
93-
Device_Write_Property_Store_Callback_Set(bacnet_settings_write_property_store);
95+
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
9496
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
9597
/* start the seconds cyclic timer */
9698
mstimer_set(&Sensor_Update_Timer, 1000);

zephyr/subsys/bacnet_settings/bacnet_settings.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
#include "bacnet/proplist.h"
2020
#include "bacnet/wp.h"
2121

22+
/**
23+
* @brief Store the BACnet data after a WriteProperty for object property
24+
* @param object_type - BACnet object type
25+
* @param object_instance - BACnet object instance
26+
* @param object_property - BACnet object property
27+
* @param array_index - BACnet array index
28+
* @param application_data - pointer to the data
29+
* @param application_data_len - length of the data
30+
* @note Used directly with bacnet_basic_store_callback_set() function
31+
*/
32+
void bacnet_settings_basic_store(BACNET_OBJECT_TYPE object_type, uint32_t object_instance,
33+
BACNET_PROPERTY_ID object_property, BACNET_ARRAY_INDEX array_index,
34+
uint8_t *application_data, int application_data_len)
35+
{
36+
BACNET_STORAGE_KEY key = {0};
37+
38+
bacnet_storage_key_init(&key, object_type, object_instance, object_property, array_index);
39+
/* store the data */
40+
(void)bacnet_storage_set(&key, application_data, application_data_len);
41+
}
42+
2243
/**
2344
* @brief Store application data to an object property after a successful
2445
* WriteProperty of the object property

0 commit comments

Comments
 (0)