Fixed load control object and unit test. Added recipe to Makefile for Zephyr OS twister unit testing. (#710)

This commit is contained in:
Steve Karg
2024-08-06 08:28:01 -05:00
committed by GitHub
parent ebfaa5eb2c
commit 61730e3d87
63 changed files with 1191 additions and 684 deletions
+321 -13
View File
@@ -81,7 +81,6 @@ static int bacnet_scale_encode(uint8_t *apdu, BACNET_SCALE *value)
static int
bacnet_scale_decode(uint8_t *apdu, size_t apdu_size, BACNET_SCALE *value)
{
int len = 0;
int apdu_len = 0;
BACNET_TAG tag = { 0 };
int32_t signed_value = 0;
@@ -90,28 +89,25 @@ bacnet_scale_decode(uint8_t *apdu, size_t apdu_size, BACNET_SCALE *value)
if (!apdu) {
return BACNET_STATUS_ERROR;
}
len = bacnet_tag_decode(&apdu[apdu_len], apdu_size - apdu_len, &tag);
if (len <= 0) {
apdu_len = bacnet_tag_decode(apdu, apdu_size, &tag);
if (apdu_len <= 0) {
return BACNET_STATUS_ERROR;
}
switch (tag.number) {
case 0:
len = bacnet_real_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, tag.number, &real_value);
if (len > 0) {
apdu_len = bacnet_real_context_decode(
apdu, apdu_size, tag.number, &real_value);
if (apdu_len > 0) {
value->float_scale = true;
value->type.real_scale = real_value;
apdu_len += len;
}
break;
case 1:
len = bacnet_signed_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, tag.number,
&signed_value);
if (len > 0) {
apdu_len = bacnet_signed_context_decode(
apdu, apdu_size, tag.number, &signed_value);
if (apdu_len > 0) {
value->float_scale = false;
value->type.integer_scale = signed_value;
apdu_len += len;
}
break;
default:
@@ -149,6 +145,151 @@ static bool bacnet_scale_same(BACNET_SCALE *value1, BACNET_SCALE *value2)
}
#endif
#if defined(BACAPP_SHED_LEVEL)
/**
* @brief Encode a BACnetScale value.
*
* BACnetScale ::= CHOICE {
* float-scale [0] REAL,
* integer-scale [1] INTEGER
* }
*
* @param apdu - buffer to encode to
* @param value - value to encode
* @return number of bytes encoded
*/
static int bacnet_shed_level_encode(uint8_t *apdu, BACNET_SHED_LEVEL *value)
{
int apdu_len = 0;
if (!value) {
return 0;
}
switch (value->type) {
case BACNET_SHED_TYPE_PERCENT:
apdu_len = encode_context_unsigned(apdu, 0,
value->value.percent);
break;
case BACNET_SHED_TYPE_AMOUNT:
apdu_len = encode_context_real(apdu, 2,
value->value.amount);
break;
case BACNET_SHED_TYPE_LEVEL:
apdu_len = encode_context_unsigned(apdu, 1,
value->value.level);
break;
default:
break;
}
return apdu_len;
}
#endif
#if defined(BACAPP_SHED_LEVEL)
/**
* @brief Decode a BACnetShedLevel value.
*
* BACnetShedLevel ::= CHOICE {
* percent [0] Unsigned,
* level [1] Unsigned,
* amount [2] REAL
* }
*
* @param apdu - buffer to decode to
* @param apdu_size - size of the buffer
* @param value - value to encode
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
*/
static int
bacnet_shed_level_decode(uint8_t *apdu, size_t apdu_size, BACNET_SHED_LEVEL *value)
{
int apdu_len = 0;
BACNET_TAG tag = { 0 };
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
float real_value = 0.0f;
if (!apdu) {
return BACNET_STATUS_ERROR;
}
apdu_len = bacnet_tag_decode(apdu, apdu_size, &tag);
if (apdu_len <= 0) {
return BACNET_STATUS_ERROR;
}
switch (tag.number) {
case 0:
/* percent - Unsigned */
apdu_len = bacnet_unsigned_context_decode(
apdu, apdu_size, tag.number, &unsigned_value);
if (apdu_len > 0) {
value->value.percent = unsigned_value;
value->type = BACNET_SHED_TYPE_PERCENT;
}
break;
case 1:
/* level - Unsigned */
apdu_len = bacnet_unsigned_context_decode(
apdu, apdu_size, tag.number, &unsigned_value);
if (apdu_len > 0) {
value->value.level = unsigned_value;
value->type = BACNET_SHED_TYPE_LEVEL;
}
break;
case 2:
apdu_len = bacnet_real_context_decode(
apdu, apdu_size, tag.number, &real_value);
if (apdu_len > 0) {
value->type = BACNET_SHED_TYPE_AMOUNT;
value->value.amount = real_value;
}
break;
default:
return BACNET_STATUS_ERROR;
}
return apdu_len;
}
#endif
#if defined(BACAPP_SHED_LEVEL)
static bool
bacnet_shed_level_same(BACNET_SHED_LEVEL *value1, BACNET_SHED_LEVEL *value2)
{
bool status = false;
if (value1 && value2) {
status = true;
if (value1->type != value2->type) {
status = false;
} else {
switch (value1->type) {
case BACNET_SHED_TYPE_PERCENT:
if (value1->value.percent != value2->value.percent) {
status = false;
}
break;
case BACNET_SHED_TYPE_AMOUNT:
if (islessgreater(
value1->value.amount, value2->value.amount)) {
status = false;
}
break;
case BACNET_SHED_TYPE_LEVEL:
if (value1->value.level != value2->value.level) {
status = false;
}
break;
default:
break;
}
}
}
return status;
}
#endif
/**
* @brief Encode application data given by a pointer into the APDU.
* @param apdu - Pointer to the buffer to encode to, or NULL for length
@@ -355,6 +496,13 @@ int bacapp_encode_application_data(
/* BACnetScale */
apdu_len = bacnet_scale_encode(apdu, &value->type.Scale);
break;
#endif
#if defined(BACAPP_SHED_LEVEL)
case BACNET_APPLICATION_TAG_SHED_LEVEL:
/* BACnetShedLevel */
apdu_len = bacnet_shed_level_encode(apdu,
&value->type.Shed_Level);
break;
#endif
default:
break;
@@ -769,6 +917,7 @@ int bacapp_encode_context_data_value(
case BACNET_APPLICATION_TAG_FDT_ENTRY:
case BACNET_APPLICATION_TAG_ACTION_COMMAND:
case BACNET_APPLICATION_TAG_SCALE:
case BACNET_APPLICATION_TAG_SHED_LEVEL:
/* complex data is enclosed in open/close tags */
len = encode_opening_tag(apdu, context_tag_number);
apdu_len += len;
@@ -1098,6 +1247,11 @@ int bacapp_known_property_tag(
case PROP_SCALE:
/* BACnetScale */
return BACNET_APPLICATION_TAG_SCALE;
case PROP_ACTUAL_SHED_LEVEL:
case PROP_EXPECTED_SHED_LEVEL:
case PROP_REQUESTED_SHED_LEVEL:
/* BACnetShedLevel */
return BACNET_APPLICATION_TAG_SHED_LEVEL;
case PROP_FD_BBMD_ADDRESS:
case PROP_BACNET_IP_GLOBAL_ADDRESS:
@@ -1399,6 +1553,13 @@ int bacapp_decode_application_tag_value(
/* BACnetScale */
apdu_len = bacnet_scale_decode(apdu, apdu_size, &value->type.Scale);
break;
#endif
#if defined(BACAPP_SHED_LEVEL)
case BACNET_APPLICATION_TAG_SHED_LEVEL:
/* BACnetShedLevel */
apdu_len = bacnet_shed_level_decode(
apdu, apdu_size, &value->type.Shed_Level);
break;
#endif
default:
break;
@@ -1698,6 +1859,43 @@ int bacapp_snprintf_shift(int len, char **buf, size_t *buf_size)
return len;
}
#if defined(BACAPP_SHED_LEVEL)
/**
* @brief Print a value to a string for EPICS
* @param str - destination string, or NULL for length only
* @param str_len - length of the destination string, or 0 for length only
* @param value - value to be printed
* @return number of characters written to the string
*/
int bacapp_snprintf_shed_level(
char *str, size_t str_len, BACNET_SHED_LEVEL *value)
{
int length = 0;
switch (value->type) {
case BACNET_SHED_TYPE_PERCENT:
length = bacapp_snprintf(
str, str_len, "%u%%",(unsigned)
value->value.percent);
break;
case BACNET_SHED_TYPE_LEVEL:
length = bacapp_snprintf(
str, str_len, "%u",(unsigned)
value->value.level);
break;
case BACNET_SHED_TYPE_AMOUNT:
length = bacapp_snprintf(
str, str_len, "%f",
value->value.amount);
break;
default:
break;
}
return length;
}
#endif
/**
* @brief Print a value to a string for EPICS
* @param str - destination string, or NULL for length only
@@ -3018,6 +3216,12 @@ int bacapp_snprintf_value(
(long)value->type.Scale.type.integer_scale);
}
break;
#endif
#if defined(BACAPP_SHED_LEVEL)
case BACNET_APPLICATION_TAG_SHED_LEVEL:
ret_val = bacapp_snprintf_shed_level(
str, str_len, &value->type.Shed_Level);
break;
#endif
default:
ret_val = bacapp_snprintf(
@@ -3295,6 +3499,97 @@ static bool strtod_checked(const char *s, double *out)
}
#endif
#if defined(BACAPP_SCALE)
/**
* @brief Parse a string into a BACnetScale value
* @param value [out] The BACnetScale value
* @param argv [in] The string to parse
* @return True on success, else False
*/
bool bacnet_scale_from_ascii(BACNET_SCALE *value, const char *argv)
{
bool status = false;
int count;
unsigned integer_scale;
float float_scale;
const char *decimal_point;
if (!status) {
decimal_point = strchr(argv, '.');
if (decimal_point) {
count = sscanf(argv, "%f", &float_scale);
if (count == 1) {
value->float_scale = true;
value->type.real_scale = float_scale;
status = true;
}
}
}
if (!status) {
count = sscanf(argv, "%u", &integer_scale);
if (count == 1) {
value->float_scale = false;
value->type.integer_scale = integer_scale;
status = true;
}
}
return status;
}
#endif
#if defined(BACAPP_SHED_LEVEL)
/**
* @brief Parse a string into a BACnet Shed Level value
* @param value [out] The BACnet Shed Level value
* @param argv [in] The string to parse
* @return True on success, else False
*/
bool bacnet_shed_level_from_ascii(BACNET_SHED_LEVEL *value, const char *argv)
{
bool status = false;
int count;
unsigned percent, level;
float amount;
const char *percentage;
const char *decimal_point;
if (!status) {
percentage = strchr(argv, '%');
if (percentage) {
count = sscanf(argv, "%u", &percent);
if (count == 1) {
value->type = BACNET_SHED_TYPE_PERCENT;
value->value.percent = percent;
status = true;
}
}
}
if (!status) {
decimal_point = strchr(argv, '.');
if (decimal_point) {
count = sscanf(argv, "%f", &amount);
if (count == 1) {
value->type = BACNET_SHED_TYPE_AMOUNT;
value->value.amount = amount;
status = true;
}
}
}
if (!status) {
count = sscanf(argv, "%u", &level);
if (count == 1) {
value->type = BACNET_SHED_TYPE_LEVEL;
value->value.level = level;
status = true;
}
}
return status;
}
#endif
/* used to load the app data struct with the proper data
converted from a command line argument.
"argv" is not const to allow using strtok internally. It MAY be modified. */
@@ -3528,7 +3823,14 @@ bool bacapp_parse_application_data(
#endif
#if defined(BACAPP_SCALE)
case BACNET_APPLICATION_TAG_SCALE:
/* BACnetScale - not implement */
status = bacnet_scale_from_ascii(
&value->type.Scale, argv);
break;
#endif
#if defined(BACAPP_SHED_LEVEL)
case BACNET_APPLICATION_TAG_SHED_LEVEL:
status = bacnet_shed_level_from_ascii(
&value->type.Shed_Level, argv);
break;
#endif
default:
@@ -4075,6 +4377,12 @@ bool bacapp_same_value(
status = bacnet_scale_same(
&value->type.Scale, &test_value->type.Scale);
break;
#endif
#if defined(BACAPP_SHED_LEVEL)
case BACNET_APPLICATION_TAG_SHED_LEVEL:
status = bacnet_shed_level_same(
&value->type.Shed_Level, &test_value->type.Shed_Level);
break;
#endif
case BACNET_APPLICATION_TAG_EMPTYLIST:
status = true;
+13
View File
@@ -47,6 +47,16 @@ typedef struct BACnetScale {
} type;
} BACNET_SCALE;
/* The shed levels for the LEVEL choice of BACnetShedLevel. */
typedef struct {
BACNET_SHED_LEVEL_TYPE type;
union {
BACNET_UNSIGNED_INTEGER level;
BACNET_UNSIGNED_INTEGER percent;
float amount;
} value;
} BACNET_SHED_LEVEL;
struct BACnet_Application_Data_Value;
typedef struct BACnet_Application_Data_Value {
bool context_specific; /* true if context specific data */
@@ -146,6 +156,9 @@ typedef struct BACnet_Application_Data_Value {
#endif
#if defined (BACAPP_SCALE)
BACNET_SCALE Scale;
#endif
#if defined(BACAPP_SHED_LEVEL)
BACNET_SHED_LEVEL Shed_Level;
#endif
} type;
/* simple linked list if needed */
+9 -1
View File
@@ -1606,7 +1606,9 @@ typedef enum {
/* BACnetActionCommand */
BACNET_APPLICATION_TAG_ACTION_COMMAND,
/* BACnetScale */
BACNET_APPLICATION_TAG_SCALE
BACNET_APPLICATION_TAG_SCALE,
/* BACnetShedLevel */
BACNET_APPLICATION_TAG_SHED_LEVEL
} BACNET_APPLICATION_TAG;
/* note: these are not the real values, */
@@ -2253,6 +2255,12 @@ typedef enum BACnetShedState {
BACNET_SHED_NON_COMPLIANT = 3
} BACNET_SHED_STATE;
typedef enum BACnetShedLevelType {
BACNET_SHED_TYPE_PERCENT, /* Unsigned */
BACNET_SHED_TYPE_LEVEL, /* Unsigned */
BACNET_SHED_TYPE_AMOUNT /* REAL */
} BACNET_SHED_LEVEL_TYPE;
typedef enum BACnetLightingOperation {
BACNET_LIGHTS_NONE = 0,
BACNET_LIGHTS_FADE_TO = 1,
+1
View File
@@ -144,6 +144,7 @@ INDTEXT_DATA bacnet_application_tag_names[] = { { BACNET_APPLICATION_TAG_NULL,
{ BACNET_APPLICATION_TAG_FDT_ENTRY, "BACnetFDTEntry" },
{ BACNET_APPLICATION_TAG_ACTION_COMMAND, "BACnetActionCommand" },
{ BACNET_APPLICATION_TAG_SCALE, "BACnetScale" },
{ BACNET_APPLICATION_TAG_SHED_LEVEL, "BACnetShedLevel" },
{ 0, NULL } };
const char *bactext_application_tag_name(unsigned index)
+2 -1
View File
@@ -317,7 +317,8 @@ bool Analog_Output_Present_Value_Set(
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
if ((priority >= 1) && (priority <= BACNET_MAX_PRIORITY) &&
value >= pObject->Min_Pres_Value && value <= pObject->Max_Pres_Value) {
(value >= pObject->Min_Pres_Value) &&
(value <= pObject->Max_Pres_Value)) {
pObject->Relinquished[priority - 1] = false;
pObject->Priority_Array[priority - 1] = value;
Analog_Output_Present_Value_COV_Detect(
File diff suppressed because it is too large Load Diff
+34 -2
View File
@@ -16,6 +16,14 @@
#include "bacnet/rp.h"
#include "bacnet/wp.h"
typedef enum bacnet_load_control_state {
SHED_INACTIVE,
SHED_REQUEST_PENDING,
SHED_NON_COMPLIANT,
SHED_COMPLIANT,
MAX_LOAD_CONTROL_STATE
} BACNET_LOAD_CONTROL_STATE;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -50,9 +58,21 @@ extern "C" {
BACNET_STACK_EXPORT
void Load_Control_Init(
void);
BACNET_STACK_EXPORT
void Load_Control_State_Machine(
int object_index);
unsigned Load_Control_Priority_For_Writing(
uint32_t object_instance);
BACNET_STACK_EXPORT
bool Load_Control_Priority_For_Writing_Set(
uint32_t object_instance, unsigned priority);
BACNET_STACK_EXPORT
bool Load_Control_Manipulated_Variable_Reference(
uint32_t object_instance,
BACNET_OBJECT_PROPERTY_REFERENCE * object_property_reference);
BACNET_STACK_EXPORT
bool Load_Control_Manipulated_Variable_Reference_Set(
uint32_t object_instance,
BACNET_OBJECT_PROPERTY_REFERENCE * object_property_reference);
BACNET_STACK_EXPORT
int Load_Control_Read_Property(
@@ -62,6 +82,18 @@ extern "C" {
bool Load_Control_Write_Property(
BACNET_WRITE_PROPERTY_DATA * wp_data);
/* functions used for unit testing */
BACNET_STACK_EXPORT
void Load_Control_State_Machine(
int object_index,
BACNET_DATE_TIME *bdatetime);
BACNET_STACK_EXPORT
BACNET_LOAD_CONTROL_STATE Load_Control_State(
int object_index);
BACNET_STACK_EXPORT
BACNET_OBJECT_ID Load_Control_Object_ID(
int object_index);
#ifdef __cplusplus
}
#endif /* __cplusplus */
+4 -1
View File
@@ -162,6 +162,7 @@
defined(BACAPP_FDT_ENTRY) || \
defined(BACAPP_ACTION_COMMAND) || \
defined(BACAPP_SCALE) || \
defined(BACAPP_SHED_LEVEL) || \
defined(BACAPP_TYPES_EXTRA))
#define BACAPP_ALL
#endif
@@ -206,6 +207,7 @@
#define BACAPP_FDT_ENTRY
#define BACAPP_ACTION_COMMAND
#define BACAPP_SCALE
#define BACAPP_SHED_LEVEL
#endif
#if defined(BACAPP_DOUBLE) || \
@@ -225,7 +227,8 @@
defined(BACAPP_BDT_ENTRY) || \
defined(BACAPP_FDT_ENTRY) || \
defined(BACAPP_ACTION_COMMAND) || \
defined(BACAPP_SCALE)
defined(BACAPP_SCALE) || \
defined(BACAPP_SHED_LEVEL)
#define BACAPP_COMPLEX_TYPES
#endif