Secure ReadProperty decoding and BACnetActionCommand (#702)

* Refactored and secured BACnetActionCommand codec into bacaction.c module for command object and added to bacapp module encode/decode with define for enabling and pseudo application tag for internal use.

* Simplified bacapp_data_len() and moved into bacdcode module as bacnet_enclosed_data_len() function.

* Secured ReadProperty-REQUEST and -ACK decoding.

* Removed deprecated Keylist_Key() functions from usage.

* Removed pseudo application datatypes from bacapp_data_decode() which only uses primitive application tag encoded values.

* Defined INT_MAX when it is not already defined by compiler or libc.

* Deprecated bacapp_decode_application_data_len() and bacapp_decode_context_data_len() as they are no longer used in any code in the library.

* Added BACnetScale to bacapp module. Improved complex property value decoding. Refactored bacapp_decode_known_property() function.

* Refactored and improved the bacapp_snprintf() function for printing EPICS.

* Fixed Lighting Output WriteProperty to handle known property decoding.
This commit is contained in:
Steve Karg
2024-07-25 17:12:08 -05:00
committed by GitHub
parent 923eaf2313
commit 4326128e72
191 changed files with 3856 additions and 2099 deletions
+345 -108
View File
@@ -188,33 +188,6 @@ unsigned Lighting_Output_Instance_To_Index(uint32_t object_instance)
return Keylist_Index(Object_List, object_instance);
}
/**
* For a given object instance-number, determines the present-value
*
* @param object_instance - object-instance number of the object
*
* @return present-value of the object
*/
float Lighting_Output_Present_Value(uint32_t object_instance)
{
float value = 0.0;
unsigned p = 0;
struct object_data *pObject;
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
value = pObject->Relinquish_Default;
for (p = 0; p < BACNET_MAX_PRIORITY; p++) {
if (BIT_CHECK(pObject->Priority_Active_Bits, p)) {
value = pObject->Priority_Array[p];
break;
}
}
}
return value;
}
/**
* @brief Get the priority-array active status for the specific priority
* @param object [in] BACnet object instance
@@ -236,6 +209,27 @@ Priority_Array_Active(struct object_data *pObject, BACNET_ARRAY_INDEX priority)
return active;
}
/**
* @brief Get the priority-array value for the specific priority
* @param object [in] BACnet object instance
* @param priority [in] array index requested:
* 0 to N for individual array members
* @return The priority-array value for the specific priority
*/
static float
Priority_Array_Value(struct object_data *pObject, BACNET_ARRAY_INDEX priority)
{
float real_value = 0.0;
if (priority < BACNET_MAX_PRIORITY) {
if (BIT_CHECK(pObject->Priority_Active_Bits, priority)) {
real_value = pObject->Priority_Array[priority];
}
}
return real_value;
}
/**
* @brief Get the value of the next highest non-NULL priority, including
* Relinquish_Default
@@ -262,24 +256,23 @@ static float Priority_Array_Next_Value(
}
/**
* @brief Get the priority-array value for the specific priority
* @param object [in] BACnet object instance
* @param priority [in] array index requested:
* 0 to N for individual array members
* @return The priority-array value for the specific priority
* For a given object instance-number, determines the present-value
*
* @param object_instance - object-instance number of the object
*
* @return present-value of the object
*/
static float
Priority_Array_Value(struct object_data *pObject, BACNET_ARRAY_INDEX priority)
float Lighting_Output_Present_Value(uint32_t object_instance)
{
float real_value = 0.0;
float value = 0.0;
struct object_data *pObject;
if (priority < BACNET_MAX_PRIORITY) {
if (BIT_CHECK(pObject->Priority_Active_Bits, priority)) {
real_value = pObject->Priority_Array[priority];
}
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
value = Priority_Array_Next_Value(pObject, 0);
}
return real_value;
return value;
}
/**
@@ -437,6 +430,181 @@ bool Lighting_Output_Present_Value_Set(
return status;
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
*/
static void
Lighting_Command_Warn(struct object_data *pObject, unsigned priority)
{
unsigned current_priority;
if (!pObject) {
return;
}
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(Priority_Array_Value(pObject, priority - 1), 0.0))) {
/* The blink-warn notification shall not occur
if any of the following conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The value at the specified priority is 0.0%, or
(c) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation = BACNET_LIGHTS_WARN;
}
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
*/
static void
Lighting_Command_Warn_Off(struct object_data *pObject, unsigned priority)
{
unsigned current_priority;
if (!pObject) {
return;
}
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(Priority_Array_Value(pObject, priority - 1), 0.0)) &&
(isgreater(Priority_Array_Next_Value(pObject, priority - 1), 0.0))) {
/* The blink-warn notification shall not occur and
the value 0.0% written at the specified
priority immediately if any of the following
conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The Present_Value is 0.0%, or
(c) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation = BACNET_LIGHTS_WARN_OFF;
} else {
Present_Value_Set(pObject, 0.0, priority);
}
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
*/
static void
Lighting_Command_Warn_Relinquish(struct object_data *pObject, unsigned priority)
{
unsigned current_priority;
if (!pObject) {
return;
}
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(Priority_Array_Value(pObject, priority - 1), 0.0)) &&
(isgreater(Priority_Array_Next_Value(pObject, priority - 1), 0.0))) {
/* The blink-warn notification shall not occur,
and the value at the specified priority shall be
relinquished immediately if any of the following
conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The value at the specified priority
is 0.0% or NULL, or
(c) The value of the next highest non-NULL
priority, including Relinquish_Default,
is greater than 0.0%, or
(d) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation = BACNET_LIGHTS_WARN_RELINQUISH;
} else {
Present_Value_Relinquish(pObject, priority);
}
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
* @param value [in] BACnet lighting value
* @param fade_time [in] BACnet lighting fade time
*/
static void
Lighting_Command_Fade_To(struct object_data *pObject,
unsigned priority, float value, uint32_t fade_time)
{
unsigned current_priority;
if (!pObject) {
return;
}
Present_Value_Set(pObject, value, priority);
current_priority = Present_Value_Priority(pObject);
if (priority <= current_priority) {
/* we have priority - configure the Lighting Command */
pObject->Lighting_Command.fade_time = fade_time;
pObject->Lighting_Command.operation = BACNET_LIGHTS_FADE_TO;
pObject->Lighting_Command.target_level = value;
}
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
* @param value [in] BACnet lighting value
* @param ramp_rate [in] BACnet lighting ramp rate
*/
static void
Lighting_Command_Ramp_To(struct object_data *pObject,
unsigned priority, float value, float ramp_rate)
{
unsigned current_priority;
if (!pObject) {
return;
}
Present_Value_Set(pObject, value, priority);
current_priority = Present_Value_Priority(pObject);
if (priority <= current_priority) {
/* we have priority - configure the Lighting Command */
pObject->Lighting_Command.ramp_rate = ramp_rate;
pObject->Lighting_Command.operation = BACNET_LIGHTS_RAMP_TO;
pObject->Lighting_Command.target_level = value;
}
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
* @param operation [in] BACnet lighting operation
* @param step_increment [in] BACnet lighting step increment value
*/
static void Lighting_Command_Step(
struct object_data *pObject,
unsigned priority,
BACNET_LIGHTING_OPERATION operation,
float step_increment)
{
unsigned current_priority;
if (!pObject) {
return;
}
current_priority = Present_Value_Priority(pObject);
if (priority <= current_priority) {
/* we have priority - configure the Lighting Command */
pObject->Lighting_Command.operation = operation;
pObject->Lighting_Command.fade_time = 0;
pObject->Lighting_Command.ramp_rate = 0.0f;
pObject->Lighting_Command.step_increment = step_increment;
}
}
/**
* For a given object instance-number, writes the present-value
*
@@ -475,73 +643,17 @@ static bool Lighting_Output_Present_Value_Write(
if (!islessgreater(value, -1.0)) {
/* Provides the same functionality as the
WARN lighting command. */
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(
Priority_Array_Value(pObject, priority - 1), 0.0))) {
/* The blink-warn notification shall not occur
if any of the following conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The value at the specified priority is 0.0%, or
(c) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation = BACNET_LIGHTS_WARN;
}
Lighting_Command_Warn(pObject, priority);
status = true;
} else if (!islessgreater(value, -2.0)) {
/* Provides the same functionality as the
WARN_RELINQUISH lighting command. */
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(
Priority_Array_Value(pObject, priority - 1), 0.0)) &&
(isgreater(
Priority_Array_Next_Value(pObject, priority - 1),
0.0))) {
/* The blink-warn notification shall not occur,
and the value at the specified priority shall be
relinquished immediately if any of the following
conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The value at the specified priority
is 0.0% or NULL, or
(c) The value of the next highest non-NULL
priority, including Relinquish_Default,
is greater than 0.0%, or
(d) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation =
BACNET_LIGHTS_WARN_RELINQUISH;
} else {
Present_Value_Relinquish(pObject, priority);
}
Lighting_Command_Warn_Relinquish(pObject, priority);
status = true;
} else if (!islessgreater(value, -3.0)) {
/* Provides the same functionality as the
WARN_OFF lighting command. */
current_priority = Present_Value_Priority(pObject);
if ((priority <= current_priority) &&
(Priority_Array_Active(pObject, priority - 1)) &&
(isgreater(
Priority_Array_Value(pObject, priority - 1), 0.0)) &&
(isgreater(
Priority_Array_Next_Value(pObject, priority - 1),
0.0))) {
/* The blink-warn notification shall not occur and
the value 0.0% written at the specified
priority immediately if any of the following
conditions occur:
(a) The specified priority is not the highest
active priority, or
(b) The Present_Value is 0.0%, or
(c) Blink_Warn_Enable is FALSE. */
pObject->Lighting_Command.operation =
BACNET_LIGHTS_WARN_OFF;
} else {
Present_Value_Set(pObject, 0.0, priority);
}
Lighting_Command_Warn_Off(pObject, priority);
status = true;
} else if (
isgreaterequal(value, 0.0) && islessequal(value, 100.0)) {
@@ -799,9 +911,131 @@ bool Lighting_Output_Lighting_Command_Set(
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
/* FIXME: check lighting command member values */
status = lighting_command_copy(&pObject->Lighting_Command, value);
/* FIXME: set all the other values, and get the light levels moving */
}
return status;
}
/**
* @brief Set the lighting command if the priority is active
* @param object [in] BACnet object instance
* @param priority [in] BACnet priority array value 1..16
*/
static void
Lighting_Command_Stop(struct object_data *pObject, unsigned priority)
{
unsigned current_priority;
if (!pObject) {
return;
}
current_priority = Present_Value_Priority(pObject);
if (priority <= current_priority) {
/* we have priority - configure the Lighting Command */
pObject->Lighting_Command.operation = BACNET_LIGHTS_STOP;
}
}
/**
* For a given object instance-number, writes the present-value
*
* @param object_instance - object-instance number of the object
* @param value - property value to write
* @param priority - priority-array index value 1..16
* @param error_class - the BACnet error class
* @param error_code - BACnet Error code
*
* @return true if values are within range and present-value is set.
*/
static bool Lighting_Output_Lighting_Command_Write(
uint32_t object_instance,
BACNET_LIGHTING_COMMAND *value,
uint8_t priority,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
bool status = false;
struct object_data *pObject;
if (!value) {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
return status;
}
if (priority == 6) {
/* Command priority 6 is reserved for use by Minimum On/Off
algorithm and may not be used for other purposes in any
object. */
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
return status;
}
if ((priority < BACNET_MIN_PRIORITY) ||
(priority > BACNET_MAX_PRIORITY)) {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
return status;
}
if (value->operation >= MAX_BACNET_LIGHTING_OPERATION) {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
return status;
}
pObject = Keylist_Data(Object_List, object_instance);
if (pObject) {
switch (value->operation) {
case BACNET_LIGHTS_NONE:
status = true;
break;
case BACNET_LIGHTS_FADE_TO:
Lighting_Command_Fade_To(pObject, priority,
value->target_level, value->fade_time);
status = true;
break;
case BACNET_LIGHTS_RAMP_TO:
Lighting_Command_Ramp_To(pObject, priority,
value->target_level, value->ramp_rate);
status = true;
break;
case BACNET_LIGHTS_STEP_UP:
case BACNET_LIGHTS_STEP_DOWN:
case BACNET_LIGHTS_STEP_ON:
case BACNET_LIGHTS_STEP_OFF:
Lighting_Command_Step(pObject,priority,
value->operation,value->step_increment);
status = true;
break;
case BACNET_LIGHTS_WARN:
/* Provides the same functionality as the
WARN lighting command. */
Lighting_Command_Warn(pObject, priority);
status = true;
break;
case BACNET_LIGHTS_WARN_OFF:
/* Provides the same functionality as the
WARN_OFF lighting command. */
Lighting_Command_Warn_Off(pObject, priority);
status = true;
break;
case BACNET_LIGHTS_WARN_RELINQUISH:
/* Provides the same functionality as the
WARN_RELINQUISH lighting command. */
Lighting_Command_Warn_Relinquish(pObject, priority);
status = true;
break;
case BACNET_LIGHTS_STOP:
Lighting_Command_Stop(pObject, priority);
status = true;
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
break;
}
} else {
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
}
return status;
@@ -1866,9 +2100,11 @@ bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
BACNET_APPLICATION_DATA_VALUE value;
/* decode the some of the request */
len = bacapp_decode_application_data(
wp_data->application_data, wp_data->application_data_len, &value);
/* FIXME: len < application_data_len: more data? */
len = bacapp_decode_known_property(
wp_data->application_data, wp_data->application_data_len, &value,
wp_data->object_type, wp_data->object_property);
/* note: len < application_data_len means there is more data,
such as an array or list */
if (len < 0) {
/* error while decoding - a value larger than we can handle */
wp_data->error_class = ERROR_CLASS_PROPERTY;
@@ -1905,8 +2141,10 @@ bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
status = write_property_type_valid(
wp_data, &value, BACNET_APPLICATION_TAG_LIGHTING_COMMAND);
if (status) {
status = Lighting_Output_Lighting_Command_Set(
wp_data->object_instance, &value.type.Lighting_Command);
status = Lighting_Output_Lighting_Command_Write(
wp_data->object_instance, &value.type.Lighting_Command,
wp_data->priority,
&wp_data->error_class, &wp_data->error_code);
if (!status) {
wp_data->error_class = ERROR_CLASS_PROPERTY;
wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
@@ -1997,8 +2235,7 @@ bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
/**
* Handles the timing for a single Lighting Output object Fade
*
* @param pLight - Lighting Output object
* @param pCommand - BACNET_LIGHTING_COMMAND of the Lighting Output object
* @param object_instance - Lighting Output object
* @param milliseconds - number of milliseconds elapsed since previously
* called. Works best when called about every 10 milliseconds.
*/