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>
3133LOG_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 */
3436static const char * Device_Name = "BACnet Lighting Device (B-LD)" ;
37+ static const uint32_t Device_Instance = 260125 ;
3538/* object instances */
3639static const uint32_t Lighting_Instance = 1 ;
3740
@@ -68,14 +71,64 @@ void BACnet_Lighting_Output_Tracking_Value_Handler(uint32_t object_instance, flo
6871 */
6972static 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
95148int 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 ;
0 commit comments