Feature/comments and buffer checks (#73)

* Added comments

* Replaced Goto construct

* Added apdu_len check

* Added comments

* Added string limit and reworked printable check

* Mainly comments

* Just comments

* Just comments

* decode service request returns now non zero on success

* eliminated warnings

* Added character string init with length check.

* Paranoic length check

* Comments and object index checking on read/write.

* Check name/desc strings before returning.

* Eliminated Goto
This commit is contained in:
Roy Schneider
2020-04-16 17:38:49 +02:00
committed by GitHub
parent 1ead6acea5
commit 8f13d59629
16 changed files with 987 additions and 360 deletions
+113 -24
View File
@@ -63,6 +63,13 @@ static const int Analog_Value_Properties_Optional[] = { PROP_DESCRIPTION,
static const int Analog_Value_Properties_Proprietary[] = { -1 };
/**
* Initialize the pointers for the required, the optional and the properitary value properties.
*
* @param pRequired - Pointer to the pointer of required values.
* @param pOptional - Pointer to the pointer of optional values.
* @param pProprietary - Pointer to the pointer of properitary values.
*/
void Analog_Value_Property_Lists(
const int **pRequired, const int **pOptional, const int **pProprietary)
{
@@ -79,6 +86,9 @@ void Analog_Value_Property_Lists(
return;
}
/**
* Initialize the analog values.
*/
void Analog_Value_Init(void)
{
unsigned i;
@@ -116,9 +126,15 @@ void Analog_Value_Init(void)
}
}
/* we simply have 0-n object instances. Yours might be */
/* more complex, and then you need validate that the */
/* given instance exists */
/**
* We simply have 0-n object instances. Yours might be
* more complex, and then you need validate that the
* given instance exists.
*
* @param object_instance Object instance
*
* @return true/false
*/
bool Analog_Value_Valid_Instance(uint32_t object_instance)
{
if (object_instance < MAX_ANALOG_VALUES) {
@@ -128,24 +144,39 @@ bool Analog_Value_Valid_Instance(uint32_t object_instance)
return false;
}
/* we simply have 0-n object instances. Yours might be */
/* more complex, and then count how many you have */
/**
* Return the count of analog values.
*
* @return Count of analog values.
*/
unsigned Analog_Value_Count(void)
{
return MAX_ANALOG_VALUES;
}
/* we simply have 0-n object instances. Yours might be */
/* more complex, and then you need to return the instance */
/* that correlates to the correct index */
/**
* We simply have 0-n object instances. Yours might be
* more complex, and then you need to return the instance
* that correlates to the correct index.
*
* @param index Index
*
* @return Object instance
*/
uint32_t Analog_Value_Index_To_Instance(unsigned index)
{
return index;
}
/* we simply have 0-n object instances. Yours might be */
/* more complex, and then you need to return the index */
/* that correlates to the correct instance number */
/**
* We simply have 0-n object instances. Yours might be
* more complex, and then you need to return the index
* that correlates to the correct instance number
*
* @param object_instance Object instance
*
* @return Index in the object table.
*/
unsigned Analog_Value_Instance_To_Index(uint32_t object_instance)
{
unsigned index = MAX_ANALOG_VALUES;
@@ -157,6 +188,16 @@ unsigned Analog_Value_Instance_To_Index(uint32_t object_instance)
return index;
}
/**
* This function is used to detect a value change,
* using the new value compared against the prior
* value, using a delta as threshold.
*
* This method will update the COV-changed attribute.
*
* @param index Object index
* @param value Given present value.
*/
static void Analog_Value_COV_Detect(unsigned int index, float value)
{
float prior_value = 0.0;
@@ -203,6 +244,13 @@ bool Analog_Value_Present_Value_Set(
return status;
}
/**
* For a given object instance-number, return the present value.
*
* @param object_instance - object-instance number of the object
*
* @return Present value
*/
float Analog_Value_Present_Value(uint32_t object_instance)
{
float value = 0;
@@ -216,7 +264,16 @@ float Analog_Value_Present_Value(uint32_t object_instance)
return value;
}
/* note: the object name must be unique within this device */
/**
* For a given object instance-number, return the name.
*
* Note: the object name must be unique within this device
*
* @param object_instance - object-instance number of the object
* @param object_name - object name/string pointer
*
* @return true/false
*/
bool Analog_Value_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
@@ -370,7 +427,13 @@ void Analog_Value_Out_Of_Service_Set(uint32_t object_instance, bool value)
}
}
/* return apdu len, or BACNET_STATUS_ERROR on error */
/**
* Return the requested property of the analog value.
*
* @param rpdata Property requested, see for BACNET_READ_PROPERTY_DATA details.
*
* @return apdu len, or BACNET_STATUS_ERROR on error
*/
int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
{
int apdu_len = 0; /* return value */
@@ -386,7 +449,11 @@ int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
unsigned i = 0;
#endif
if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
/* Valid data? */
if (rpdata == NULL) {
return 0;
}
if ((rpdata->application_data == NULL) ||
(rpdata->application_data_len == 0)) {
return 0;
}
@@ -394,12 +461,14 @@ int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
apdu = rpdata->application_data;
object_index = Analog_Value_Instance_To_Index(rpdata->object_instance);
if (object_index < MAX_ANALOG_VALUES) {
CurrentAV = &AV_Descr[object_index];
} else {
if (object_index >= MAX_ANALOG_VALUES) {
rpdata->error_class = ERROR_CLASS_OBJECT;
rpdata->error_code = ERROR_CODE_UNKNOWN_OBJECT;
return BACNET_STATUS_ERROR;
}
CurrentAV = &AV_Descr[object_index];
switch (rpdata->object_property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_application_object_id(
@@ -408,9 +477,10 @@ int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
Analog_Value_Object_Name(rpdata->object_instance, &char_string);
apdu_len =
encode_application_character_string(&apdu[0], &char_string);
if (Analog_Value_Object_Name(rpdata->object_instance, &char_string)) {
apdu_len =
encode_application_character_string(&apdu[0], &char_string);
}
break;
case PROP_OBJECT_TYPE:
@@ -594,7 +664,13 @@ int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
return apdu_len;
}
/* returns true if successful */
/**
* Set the requested property of the analog value.
*
* @param wp_data Property requested, see for BACNET_WRITE_PROPERTY_DATA details.
*
* @return true if successful
*/
bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
{
bool status = false; /* return value */
@@ -603,6 +679,15 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
BACNET_APPLICATION_DATA_VALUE value;
ANALOG_VALUE_DESCR *CurrentAV;
/* Valid data? */
if (wp_data == NULL) {
return false;
}
if ((wp_data->application_data == NULL) ||
(wp_data->application_data_len == 0)) {
return false;
}
/* decode the some of the request */
len = bacapp_decode_application_data(
wp_data->application_data, wp_data->application_data_len, &value);
@@ -621,13 +706,17 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
return false;
}
/* Valid object? */
object_index = Analog_Value_Instance_To_Index(wp_data->object_instance);
if (object_index < MAX_ANALOG_VALUES) {
CurrentAV = &AV_Descr[object_index];
} else {
if (object_index >= MAX_ANALOG_VALUES) {
wp_data->error_class = ERROR_CLASS_OBJECT;
wp_data->error_code = ERROR_CODE_UNKNOWN_OBJECT;
return false;
}
CurrentAV = &AV_Descr[object_index];
switch (wp_data->object_property) {
case PROP_PRESENT_VALUE:
if (value.tag == BACNET_APPLICATION_TAG_REAL) {