Skip to content

Commit c4c963f

Browse files
committed
Refactored bacnet settings and storage and their shells.
1 parent e665332 commit c4c963f

File tree

12 files changed

+1880
-1432
lines changed

12 files changed

+1880
-1432
lines changed

zephyr/include/bacnet_settings/bacnet_settings.h

Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,114 @@
2323
* @param context The context to pass to the WriteProperty function
2424
* @return true if the WriteProperty succeeded
2525
*/
26-
typedef bool (*bacnet_settings_restore_callback)(BACNET_WRITE_PROPERTY_DATA *wp_data,
27-
void *context);
26+
typedef bool (*bacnet_settings_restore_callback)(
27+
BACNET_WRITE_PROPERTY_DATA *wp_data, void *context);
2828

2929
#ifdef __cplusplus
3030
extern "C" {
3131
#endif /* __cplusplus */
3232

33-
void bacnet_settings_basic_store(BACNET_OBJECT_TYPE object_type, uint32_t object_instance,
34-
BACNET_PROPERTY_ID object_property, BACNET_ARRAY_INDEX array_index,
35-
uint8_t *application_data, int application_data_len);
33+
void bacnet_settings_basic_store(
34+
BACNET_OBJECT_TYPE object_type,
35+
uint32_t object_instance,
36+
BACNET_PROPERTY_ID object_property,
37+
BACNET_ARRAY_INDEX array_index,
38+
uint8_t *application_data,
39+
int application_data_len);
3640
bool bacnet_settings_write_property_store(BACNET_WRITE_PROPERTY_DATA *wp_data);
37-
bool bacnet_settings_write_property_restore(bacnet_settings_restore_callback cb, void *context);
41+
bool bacnet_settings_write_property_restore(
42+
bacnet_settings_restore_callback cb, void *context);
3843

39-
int bacnet_settings_value_get(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
40-
uint32_t array_index, BACNET_APPLICATION_DATA_VALUE *value);
41-
bool bacnet_settings_value_set(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
42-
uint32_t array_index, BACNET_APPLICATION_DATA_VALUE *value);
44+
int bacnet_settings_value_get(
45+
uint16_t object_type,
46+
uint32_t object_instance,
47+
uint32_t property_id,
48+
uint32_t array_index,
49+
BACNET_APPLICATION_DATA_VALUE *value);
50+
bool bacnet_settings_value_set(
51+
uint16_t object_type,
52+
uint32_t object_instance,
53+
uint32_t property_id,
54+
uint32_t array_index,
55+
BACNET_APPLICATION_DATA_VALUE *value);
56+
bool bacnet_settings_value_parse(
57+
const char *value_string,
58+
uint16_t object_type,
59+
uint32_t property_id,
60+
BACNET_APPLICATION_DATA_VALUE *value);
4361

44-
int bacnet_settings_real_get(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
45-
uint32_t array_index, float default_value, float *value);
46-
bool bacnet_settings_real_set(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
47-
uint32_t array_index, float value);
62+
int bacnet_settings_real_get(
63+
uint16_t object_type,
64+
uint32_t object_instance,
65+
uint32_t property_id,
66+
uint32_t array_index,
67+
float default_value,
68+
float *value);
69+
bool bacnet_settings_real_set(
70+
uint16_t object_type,
71+
uint32_t object_instance,
72+
uint32_t property_id,
73+
uint32_t array_index,
74+
float value);
4875

49-
int bacnet_settings_unsigned_get(uint16_t object_type, uint32_t object_instance,
50-
uint32_t property_id, uint32_t array_index,
51-
BACNET_UNSIGNED_INTEGER default_value,
52-
BACNET_UNSIGNED_INTEGER *value);
53-
bool bacnet_settings_unsigned_set(uint16_t object_type, uint32_t object_instance,
54-
uint32_t property_id, uint32_t array_index,
55-
BACNET_UNSIGNED_INTEGER value);
76+
int bacnet_settings_unsigned_get(
77+
uint16_t object_type,
78+
uint32_t object_instance,
79+
uint32_t property_id,
80+
uint32_t array_index,
81+
BACNET_UNSIGNED_INTEGER default_value,
82+
BACNET_UNSIGNED_INTEGER *value);
83+
bool bacnet_settings_unsigned_set(
84+
uint16_t object_type,
85+
uint32_t object_instance,
86+
uint32_t property_id,
87+
uint32_t array_index,
88+
BACNET_UNSIGNED_INTEGER value);
5689

57-
int bacnet_settings_signed_get(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
58-
uint32_t array_index, int32_t default_value, int32_t *value);
59-
bool bacnet_settings_signed_set(uint16_t object_type, uint32_t object_instance,
60-
uint32_t property_id, uint32_t array_index, int32_t value);
90+
int bacnet_settings_signed_get(
91+
uint16_t object_type,
92+
uint32_t object_instance,
93+
uint32_t property_id,
94+
uint32_t array_index,
95+
int32_t default_value,
96+
int32_t *value);
97+
bool bacnet_settings_signed_set(
98+
uint16_t object_type,
99+
uint32_t object_instance,
100+
uint32_t property_id,
101+
uint32_t array_index,
102+
int32_t value);
61103

62-
int bacnet_settings_characterstring_get(uint16_t object_type, uint32_t object_instance,
63-
uint32_t property_id, uint32_t array_index,
64-
const char *default_value, BACNET_CHARACTER_STRING *value);
104+
int bacnet_settings_characterstring_get(
105+
uint16_t object_type,
106+
uint32_t object_instance,
107+
uint32_t property_id,
108+
uint32_t array_index,
109+
const char *default_value,
110+
BACNET_CHARACTER_STRING *value);
65111

66-
bool bacnet_settings_characterstring_ansi_set(uint16_t object_type, uint32_t object_instance,
67-
uint32_t property_id, uint32_t array_index,
68-
const char *cstring);
112+
bool bacnet_settings_characterstring_ansi_set(
113+
uint16_t object_type,
114+
uint32_t object_instance,
115+
uint32_t property_id,
116+
uint32_t array_index,
117+
const char *cstring);
69118

70-
int bacnet_settings_string_get(uint16_t object_type, uint32_t object_instance, uint32_t property_id,
71-
uint32_t array_index, const char *default_value, char *value,
72-
size_t value_size);
119+
int bacnet_settings_string_get(
120+
uint16_t object_type,
121+
uint32_t object_instance,
122+
uint32_t property_id,
123+
uint32_t array_index,
124+
const char *default_value,
125+
char *value,
126+
size_t value_size);
73127

74-
bool bacnet_settings_string_set(uint16_t object_type, uint32_t object_instance,
75-
uint32_t property_id, uint32_t array_index, const char *value);
128+
bool bacnet_settings_string_set(
129+
uint16_t object_type,
130+
uint32_t object_instance,
131+
uint32_t property_id,
132+
uint32_t array_index,
133+
const char *value);
76134

77135
bool bacnet_settings_init(void);
78136

zephyr/include/bacnet_settings/bacnet_storage.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,53 @@
1515
#include <errno.h>
1616
#include <zephyr/settings/settings.h>
1717

18-
#define BACNET_STORAGE_VALUE_SIZE_MAX SETTINGS_MAX_VAL_LEN
19-
#define BACNET_STORAGE_KEY_SIZE_MAX SETTINGS_MAX_NAME_LEN
18+
#define BACNET_STORAGE_VALUE_SIZE_MAX SETTINGS_MAX_VAL_LEN
19+
#define BACNET_STORAGE_KEY_SIZE_MAX SETTINGS_MAX_NAME_LEN
2020
#define BACNET_STORAGE_ARRAY_INDEX_NONE UINT32_MAX
2121

2222
typedef struct bacnet_storage_key_t {
23-
uint16_t object_type;
24-
uint32_t object_instance;
25-
uint32_t property_id;
26-
uint32_t array_index;
23+
uint16_t object_type;
24+
uint32_t object_instance;
25+
uint32_t property_id;
26+
uint32_t array_index;
2727
} BACNET_STORAGE_KEY;
2828

29-
typedef int (*bacnet_storage_restore_callback)(BACNET_STORAGE_KEY *key, const void *data,
30-
size_t data_size, void *context);
29+
typedef int (*bacnet_storage_restore_callback)(
30+
BACNET_STORAGE_KEY *key, const void *data, size_t data_size, void *context);
3131

3232
#ifdef __cplusplus
3333
extern "C" {
3434
#endif /* __cplusplus */
3535

3636
int bacnet_storage_init(void);
3737

38-
int bacnet_storage_load_callback_set(bacnet_storage_restore_callback cb, void *context);
38+
int bacnet_storage_load_callback_set(
39+
bacnet_storage_restore_callback cb, void *context);
3940
int bacnet_storage_load(void);
4041

41-
int bacnet_storage_handler_set(const char *name, size_t len, settings_read_cb read_cb,
42-
void *cb_arg);
42+
int bacnet_storage_handler_set(
43+
const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
4344
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+
int bacnet_storage_handler_export(
46+
int (*cb)(const char *name, const void *value, size_t val_len));
4547

4648
bool bacnet_storage_strtoul(const char *search_name, unsigned long *long_value);
4749
bool bacnet_storage_strtol(const char *search_name, long *long_value);
4850
bool bacnet_storage_strtof(const char *search_name, float *float_value);
4951

50-
void bacnet_storage_key_init(BACNET_STORAGE_KEY *key, uint16_t object_type,
51-
uint32_t object_instance, uint32_t property_id, uint32_t array_index);
52+
void bacnet_storage_key_init(
53+
BACNET_STORAGE_KEY *key,
54+
uint16_t object_type,
55+
uint32_t object_instance,
56+
uint32_t property_id,
57+
uint32_t array_index);
58+
int bacnet_storage_key_parse(BACNET_STORAGE_KEY *key, size_t argc, char **argv);
5259
int bacnet_storage_key_decode(const char *path, BACNET_STORAGE_KEY *key);
53-
int bacnet_storage_key_encode(char *buffer, size_t buffer_size, BACNET_STORAGE_KEY *key);
60+
int bacnet_storage_key_encode(
61+
char *buffer, size_t buffer_size, BACNET_STORAGE_KEY *key);
5462

55-
int bacnet_storage_set(BACNET_STORAGE_KEY *key, const void *data, size_t data_size);
63+
int bacnet_storage_set(
64+
BACNET_STORAGE_KEY *key, const void *data, size_t data_size);
5665
int bacnet_storage_get(BACNET_STORAGE_KEY *key, void *data, size_t data_size);
5766
int bacnet_storage_delete(BACNET_STORAGE_KEY *key);
5867

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

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,27 @@ static const uint32_t Lighting_Instance = 1;
4343
* @param object-instance [in] The object-instance number of the object
4444
* @param old_value [in] The value to track
4545
*/
46-
void BACnet_Lighting_Output_Tracking_Value_Handler(uint32_t object_instance, float old_value,
47-
float value)
46+
void BACnet_Lighting_Output_Tracking_Value_Handler(
47+
uint32_t object_instance, float old_value, float value)
4848
{
49-
uint16_t steps = 0;
49+
uint16_t steps = 0;
5050

51-
(void)old_value;
52-
if (object_instance != Lighting_Instance) {
53-
return;
54-
}
55-
/* Tracking value are 0.0 and 1.0-100.0 normalized */
56-
if (isgreaterequal(value, 1.0) && islessequal(value, 100.0)) {
57-
steps = linear_interpolate(1.0, value, 100.0, 1, UINT16_MAX);
58-
} else if (isgreater(value, 100.0)) {
59-
steps = UINT16_MAX;
60-
} else {
61-
steps = 0;
62-
}
63-
LOG_INF("Lighting Output[%lu]: value=%f step=%u/%u", (unsigned long)object_instance,
64-
(double)value, (unsigned)steps, (unsigned)UINT16_MAX);
51+
(void)old_value;
52+
if (object_instance != Lighting_Instance) {
53+
return;
54+
}
55+
/* Tracking value are 0.0 and 1.0-100.0 normalized */
56+
if (isgreaterequal(value, 1.0) && islessequal(value, 100.0)) {
57+
steps = linear_interpolate(1.0, value, 100.0, 1, UINT16_MAX);
58+
} else if (isgreater(value, 100.0)) {
59+
steps = UINT16_MAX;
60+
} else {
61+
steps = 0;
62+
}
63+
LOG_INF(
64+
"Lighting Output[%lu]: value=%f step=%u/%u",
65+
(unsigned long)object_instance, (double)value, (unsigned)steps,
66+
(unsigned)UINT16_MAX);
6567
}
6668

6769
/**
@@ -70,18 +72,19 @@ void BACnet_Lighting_Output_Tracking_Value_Handler(uint32_t object_instance, flo
7072
* @param context The context to pass to the WriteProperty function
7173
* @return true if the WriteProperty succeeded
7274
*/
73-
static bool Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void *context)
75+
static bool
76+
Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void *context)
7477
{
75-
(void)context;
76-
if (wp_data == NULL) {
77-
return false;
78-
}
79-
if ((wp_data->object_type == OBJECT_DEVICE) &&
80-
(wp_data->object_instance == BACNET_MAX_INSTANCE)) {
81-
wp_data->object_instance = Device_Object_Instance_Number();
82-
}
78+
(void)context;
79+
if (wp_data == NULL) {
80+
return false;
81+
}
82+
if ((wp_data->object_type == OBJECT_DEVICE) &&
83+
(wp_data->object_instance == BACNET_MAX_INSTANCE)) {
84+
wp_data->object_instance = Device_Object_Instance_Number();
85+
}
8386

84-
return Device_Write_Property(wp_data);
87+
return Device_Write_Property(wp_data);
8588
}
8689

8790
/**
@@ -91,28 +94,28 @@ static bool Settings_Restore_Callback(BACNET_WRITE_PROPERTY_DATA *wp_data, void
9194
*/
9295
static void BACnet_Lighting_Device_Init_Handler(void *context)
9396
{
94-
(void)context;
95-
LOG_INF("BACnet Stack Initialized");
96-
/* initialize objects with default values for this basic sample */
97-
Device_Set_Object_Instance_Number(Device_Instance);
98-
Device_Object_Name_ANSI_Init(Device_Name);
99-
/* lighting output object */
100-
Lighting_Output_Create(Lighting_Instance);
101-
Lighting_Output_Name_Set(Lighting_Instance, "Light-1");
102-
/* restore any property values previously stored via WriteProperty */
103-
bacnet_settings_init();
104-
bacnet_settings_write_property_restore(&Settings_Restore_Callback, NULL);
105-
/* writable property values are stored with WriteProperty.
106-
Set this callback after restore to prevent recursion. */
107-
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
108-
/* lighting output callbacks */
109-
Lighting_Output_Write_Present_Value_Callback_Set(
110-
BACnet_Lighting_Output_Tracking_Value_Handler);
111-
/* done */
112-
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
113-
/* set the BACnet Basic Task device object timer for lighting output use */
114-
bacnet_basic_task_object_timer_set(10UL);
115-
srand(sys_rand32_get());
97+
(void)context;
98+
LOG_INF("BACnet Stack Initialized");
99+
/* initialize objects with default values for this basic sample */
100+
Device_Set_Object_Instance_Number(Device_Instance);
101+
Device_Object_Name_ANSI_Init(Device_Name);
102+
/* lighting output object */
103+
Lighting_Output_Create(Lighting_Instance);
104+
Lighting_Output_Name_Set(Lighting_Instance, "Light-1");
105+
/* restore any property values previously stored via WriteProperty */
106+
bacnet_settings_init();
107+
bacnet_settings_write_property_restore(&Settings_Restore_Callback, NULL);
108+
/* writable property values are stored with WriteProperty.
109+
Set this callback after restore to prevent recursion. */
110+
bacnet_basic_store_callback_set(bacnet_settings_basic_store);
111+
/* lighting output callbacks */
112+
Lighting_Output_Write_Present_Value_Callback_Set(
113+
BACnet_Lighting_Output_Tracking_Value_Handler);
114+
/* done */
115+
LOG_INF("BACnet Device ID: %u", Device_Object_Instance_Number());
116+
/* set the BACnet Basic Task device object timer for lighting output use */
117+
bacnet_basic_task_object_timer_set(10UL);
118+
srand(sys_rand32_get());
116119
}
117120

118121
/**
@@ -122,28 +125,28 @@ static void BACnet_Lighting_Device_Init_Handler(void *context)
122125
*/
123126
static void BACnet_Lighting_Device_Task_Handler(void *context)
124127
{
125-
(void)context;
128+
(void)context;
126129
}
127130

128131
int main(void)
129132
{
130-
bool port_initialized = false;
133+
bool port_initialized = false;
131134

132-
LOG_INF("BACnet Device: %s", Device_Name);
133-
LOG_INF("BACnet Stack Version " BACNET_VERSION_TEXT);
134-
LOG_INF("BACnet Stack Max APDU: %d", MAX_APDU);
135-
bacnet_basic_init_callback_set(BACnet_Lighting_Device_Init_Handler, NULL);
136-
bacnet_basic_task_callback_set(BACnet_Lighting_Device_Task_Handler, NULL);
137-
bacnet_basic_init();
138-
for (;;) {
139-
k_sleep(K_MSEC(CONFIG_BACNET_BASIC_SERVER_KSLEEP));
140-
bacnet_basic_task();
141-
if (port_initialized) {
142-
bacnet_port_task();
143-
} else {
144-
port_initialized = bacnet_port_init();
145-
}
146-
}
135+
LOG_INF("BACnet Device: %s", Device_Name);
136+
LOG_INF("BACnet Stack Version " BACNET_VERSION_TEXT);
137+
LOG_INF("BACnet Stack Max APDU: %d", MAX_APDU);
138+
bacnet_basic_init_callback_set(BACnet_Lighting_Device_Init_Handler, NULL);
139+
bacnet_basic_task_callback_set(BACnet_Lighting_Device_Task_Handler, NULL);
140+
bacnet_basic_init();
141+
for (;;) {
142+
k_sleep(K_MSEC(CONFIG_BACNET_BASIC_SERVER_KSLEEP));
143+
bacnet_basic_task();
144+
if (port_initialized) {
145+
bacnet_port_task();
146+
} else {
147+
port_initialized = bacnet_port_init();
148+
}
149+
}
147150

148-
return 0;
151+
return 0;
149152
}

0 commit comments

Comments
 (0)