Fixed Channel object coercion encoding of non-primitive data. (#1181)

* Refactored the object-id to and from 32-bit value so channel-value could re-use the same API.

* Expanded the BACnetChannelValue coercion function to include all the coercions, no coercions, and invalid datatypes described in Table 12-63 of the Channel object.  Expanded unit testing code coverage for BACnetChannelValue.
This commit is contained in:
Steve Karg
2025-12-09 11:02:58 -06:00
committed by GitHub
parent 95cdec459e
commit 2b328266c8
5 changed files with 579 additions and 55 deletions
+37 -11
View File
@@ -1846,6 +1846,40 @@ int encode_context_bitstring(
return apdu_len;
}
/**
* @brief Encode the BACnet Object Identifier Value
* as defined in clause 20.2.14 Encoding of an Object Identifier Value
* @param object_type - object type to be encoded
* @param instance - object instance to be encoded
* @return the 32-bit object identifier value
*/
uint32_t
bacnet_object_id_to_value(BACNET_OBJECT_TYPE object_type, uint32_t instance)
{
return (((uint32_t)object_type & BACNET_MAX_OBJECT)
<< BACNET_INSTANCE_BITS) |
(instance & BACNET_MAX_INSTANCE);
}
/**
* @brief Decode the BACnet Object Identifier Value
* as defined in clause 20.2.14 Encoding of an Object Identifier Value
* @param value - the 32-bit object identifier value to be decoded
* @param object_type - pointer to store decoded object type
* @param instance - object instance to be decoded
*/
void bacnet_object_id_from_value(
uint32_t value, BACNET_OBJECT_TYPE *object_type, uint32_t *instance)
{
if (object_type) {
*object_type = (BACNET_OBJECT_TYPE)((
(value >> BACNET_INSTANCE_BITS) & BACNET_MAX_OBJECT));
}
if (instance) {
*instance = (value & BACNET_MAX_INSTANCE);
}
}
/**
* @brief Decode the BACnet Object Identifier Value
* as defined in clause 20.2.14 Encoding of an Object Identifier Value
@@ -1870,13 +1904,7 @@ int decode_object_id_safe(
if (len_value_type == len) {
if (apdu) {
/* value is meaningless if apdu was NULL */
if (object_type) {
*object_type = (BACNET_OBJECT_TYPE)((
(value >> BACNET_INSTANCE_BITS) & BACNET_MAX_OBJECT));
}
if (instance) {
*instance = (value & BACNET_MAX_INSTANCE);
}
bacnet_object_id_from_value(value, object_type, instance);
}
}
@@ -2123,12 +2151,10 @@ int decode_context_object_id(
int encode_bacnet_object_id(
uint8_t *apdu, BACNET_OBJECT_TYPE object_type, uint32_t instance)
{
uint32_t value = 0;
uint32_t value;
int len;
value =
(((uint32_t)object_type & BACNET_MAX_OBJECT) << BACNET_INSTANCE_BITS) |
(instance & BACNET_MAX_INSTANCE);
value = bacnet_object_id_to_value(object_type, instance);
len = encode_unsigned32(apdu, value);
return len;
+7
View File
@@ -312,6 +312,13 @@ BACNET_STACK_EXPORT
int bacnet_double_application_decode(
const uint8_t *apdu, uint32_t apdu_len_max, double *value);
BACNET_STACK_EXPORT
uint32_t
bacnet_object_id_to_value(BACNET_OBJECT_TYPE object_type, uint32_t instance);
BACNET_STACK_EXPORT
void bacnet_object_id_from_value(
uint32_t value, BACNET_OBJECT_TYPE *object_type, uint32_t *instance);
BACNET_STACK_EXPORT
int encode_bacnet_object_id(
uint8_t *apdu, BACNET_OBJECT_TYPE object_type, uint32_t instance);
+153 -44
View File
@@ -740,73 +740,97 @@ static int channel_value_coerce_data_encode(
uint32_t unsigned_value = 0;
int32_t signed_value = 0;
bool boolean_value = false;
BACNET_OBJECT_TYPE object_type = OBJECT_NONE;
uint32_t instance = 0;
if (!value) {
return BACNET_STATUS_ERROR;
}
if (value->tag == tag) {
/* no coercion */
return bacnet_channel_value_type_encode(apdu, value);
}
switch (value->tag) {
case BACNET_APPLICATION_TAG_NULL:
if ((tag == BACNET_APPLICATION_TAG_LIGHTING_COMMAND) ||
(tag == BACNET_APPLICATION_TAG_COLOR_COMMAND)) {
(tag == BACNET_APPLICATION_TAG_COLOR_COMMAND) ||
(tag == BACNET_APPLICATION_TAG_XY_COLOR)) {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
} else {
/* no coercion */
if (apdu) {
*apdu = value->tag;
}
apdu_len++;
apdu_len = encode_application_null(apdu);
}
break;
#if defined(CHANNEL_BOOLEAN)
case BACNET_APPLICATION_TAG_BOOLEAN:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
apdu_len =
encode_application_boolean(apdu, value->type.Boolean);
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
/* The Boolean value FALSE is mapped to 0
and TRUE is mapped to 1.*/
if (value->type.Boolean) {
unsigned_value = 1;
}
apdu_len = encode_application_unsigned(apdu, unsigned_value);
} else if (tag == BACNET_APPLICATION_TAG_SIGNED_INT) {
/* The Boolean value FALSE is mapped to 0
and TRUE is mapped to 1.*/
if (value->type.Boolean) {
signed_value = 1;
}
apdu_len = encode_application_signed(apdu, signed_value);
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
/* The Boolean value FALSE is mapped to 0
and TRUE is mapped to 1.*/
if (value->type.Boolean) {
float_value = 1;
float_value = 1.0f;
}
apdu_len = encode_application_real(apdu, float_value);
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
/* The Boolean value FALSE is mapped to 0
and TRUE is mapped to 1.*/
if (value->type.Boolean) {
double_value = 1;
double_value = 1.0;
}
apdu_len = encode_application_double(apdu, double_value);
} else if (tag == BACNET_APPLICATION_TAG_ENUMERATED) {
/* The Boolean value FALSE is mapped to 0
and TRUE is mapped to 1.*/
if (value->type.Boolean) {
unsigned_value = 1;
}
apdu_len = encode_application_enumerated(apdu, unsigned_value);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_UNSIGNED)
case BACNET_APPLICATION_TAG_UNSIGNED_INT:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
/* The numeric value 0 maps to FALSE
and anything else is TRUE.*/
if (value->type.Unsigned_Int) {
boolean_value = true;
}
apdu_len = encode_application_boolean(apdu, boolean_value);
} else if (tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
unsigned_value = value->type.Unsigned_Int;
apdu_len = encode_application_unsigned(apdu, unsigned_value);
} else if (tag == BACNET_APPLICATION_TAG_SIGNED_INT) {
if (value->type.Unsigned_Int <= 2147483647) {
signed_value = value->type.Unsigned_Int;
apdu_len = encode_application_signed(apdu, signed_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
@@ -814,6 +838,10 @@ static int channel_value_coerce_data_encode(
float_value = (float)value->type.Unsigned_Int;
apdu_len = encode_application_real(apdu, float_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
@@ -822,14 +850,26 @@ static int channel_value_coerce_data_encode(
} else if (tag == BACNET_APPLICATION_TAG_ENUMERATED) {
unsigned_value = value->type.Unsigned_Int;
apdu_len = encode_application_enumerated(apdu, unsigned_value);
} else if (tag == BACNET_APPLICATION_TAG_OBJECT_ID) {
bacnet_object_id_from_value(
value->type.Unsigned_Int, &object_type, &instance);
apdu_len =
encode_application_object_id(apdu, object_type, instance);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_SIGNED)
case BACNET_APPLICATION_TAG_SIGNED_INT:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
/* The numeric value 0 maps to FALSE
and anything else is TRUE.*/
if (value->type.Signed_Int) {
boolean_value = true;
}
@@ -841,16 +881,21 @@ static int channel_value_coerce_data_encode(
apdu_len =
encode_application_unsigned(apdu, unsigned_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_SIGNED_INT) {
signed_value = value->type.Signed_Int;
apdu_len = encode_application_signed(apdu, signed_value);
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
if (value->type.Signed_Int <= 9999999) {
float_value = (float)value->type.Signed_Int;
apdu_len = encode_application_real(apdu, float_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
@@ -860,13 +905,20 @@ static int channel_value_coerce_data_encode(
unsigned_value = value->type.Signed_Int;
apdu_len = encode_application_enumerated(apdu, unsigned_value);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_REAL)
case BACNET_APPLICATION_TAG_REAL:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
/* The numeric value 0 maps to FALSE
and anything else is TRUE.*/
if (islessgreater(value->type.Real, 0.0F)) {
boolean_value = true;
}
@@ -878,6 +930,10 @@ static int channel_value_coerce_data_encode(
apdu_len =
encode_application_unsigned(apdu, unsigned_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_SIGNED_INT) {
@@ -886,11 +942,12 @@ static int channel_value_coerce_data_encode(
signed_value = (int32_t)value->type.Real;
apdu_len = encode_application_signed(apdu, signed_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
float_value = value->type.Real;
apdu_len = encode_application_real(apdu, float_value);
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
double_value = value->type.Real;
apdu_len = encode_application_double(apdu, double_value);
@@ -901,16 +958,27 @@ static int channel_value_coerce_data_encode(
apdu_len =
encode_application_enumerated(apdu, unsigned_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_DOUBLE)
case BACNET_APPLICATION_TAG_DOUBLE:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
/* The numeric value 0 maps to FALSE
and anything else is TRUE.*/
if (islessgreater(value->type.Double, 0.0)) {
boolean_value = true;
}
@@ -922,6 +990,10 @@ static int channel_value_coerce_data_encode(
apdu_len =
encode_application_unsigned(apdu, unsigned_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_SIGNED_INT) {
@@ -930,6 +1002,10 @@ static int channel_value_coerce_data_encode(
signed_value = (int32_t)value->type.Double;
apdu_len = encode_application_signed(apdu, signed_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
@@ -938,11 +1014,12 @@ static int channel_value_coerce_data_encode(
float_value = (float)value->type.Double;
apdu_len = encode_application_real(apdu, float_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
double_value = value->type.Double;
apdu_len = encode_application_double(apdu, double_value);
} else if (tag == BACNET_APPLICATION_TAG_ENUMERATED) {
if ((value->type.Double >= 0.0) &&
(value->type.Double <= 2147483000.0)) {
@@ -950,16 +1027,27 @@ static int channel_value_coerce_data_encode(
apdu_len =
encode_application_enumerated(apdu, unsigned_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_ENUMERATED)
case BACNET_APPLICATION_TAG_ENUMERATED:
if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_BOOLEAN) {
/* The numeric value 0 maps to FALSE
and anything else is TRUE.*/
if (value->type.Enumerated) {
boolean_value = true;
}
@@ -972,6 +1060,10 @@ static int channel_value_coerce_data_encode(
signed_value = value->type.Enumerated;
apdu_len = encode_application_signed(apdu, signed_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_REAL) {
@@ -979,49 +1071,66 @@ static int channel_value_coerce_data_encode(
float_value = (float)value->type.Enumerated;
apdu_len = encode_application_real(apdu, float_value);
} else {
/* Those cases where coercion of values exceeds
a range specified by an indicated coercion rule
shall be considered as coercion failures and
the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
} else if (tag == BACNET_APPLICATION_TAG_DOUBLE) {
double_value = (double)value->type.Enumerated;
apdu_len = encode_application_double(apdu, double_value);
} else if (tag == BACNET_APPLICATION_TAG_ENUMERATED) {
unsigned_value = value->type.Enumerated;
apdu_len = encode_application_enumerated(apdu, unsigned_value);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_LIGHTING_COMMAND)
case BACNET_APPLICATION_TAG_LIGHTING_COMMAND:
if (tag == BACNET_APPLICATION_TAG_LIGHTING_COMMAND) {
apdu_len = lighting_command_encode(
apdu, &value->type.Lighting_Command);
#if defined(CHANNEL_DATE)
case BACNET_APPLICATION_TAG_DATE:
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_COLOR_COMMAND)
case BACNET_APPLICATION_TAG_COLOR_COMMAND:
if (tag == BACNET_APPLICATION_TAG_COLOR_COMMAND) {
apdu_len =
color_command_encode(apdu, &value->type.Color_Command);
#if defined(CHANNEL_TIME)
case BACNET_APPLICATION_TAG_TIME:
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
#if defined(CHANNEL_XY_COLOR)
case BACNET_APPLICATION_TAG_XY_COLOR:
if (tag == BACNET_APPLICATION_TAG_XY_COLOR) {
apdu_len = xy_color_encode(apdu, &value->type.XY_Color);
#if defined(CHANNEL_OBJECT_ID)
case BACNET_APPLICATION_TAG_OBJECT_ID:
if (tag == BACNET_APPLICATION_TAG_NULL) {
apdu_len = encode_application_null(apdu);
} else if (tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
unsigned_value = bacnet_object_id_to_value(
value->type.Object_Id.type, value->type.Object_Id.instance);
apdu_len = encode_application_unsigned(apdu, unsigned_value);
} else {
/* Those cases where Invalid Datatype (ID) is indicated
in Table 12-63 shall be considered as coercion failures
and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
}
break;
#endif
default:
/* Not defined in Table 12-63 shall be considered as coercion
failures and the write shall not occur. */
apdu_len = BACNET_STATUS_ERROR;
break;
}