Refactored rpm decoding to return proper reject/abort status for malformed requests.
Note: the test code in rpm.c has been changed to accommodate the changes made to the decoding functions but I have not tested the test code as I have not run the test suite.
This commit is contained in:
+125
-127
@@ -36,6 +36,8 @@
|
|||||||
#include "apdu.h"
|
#include "apdu.h"
|
||||||
#include "npdu.h"
|
#include "npdu.h"
|
||||||
#include "abort.h"
|
#include "abort.h"
|
||||||
|
#include "reject.h"
|
||||||
|
#include "bacerror.h"
|
||||||
#include "rpm.h"
|
#include "rpm.h"
|
||||||
#include "handlers.h"
|
#include "handlers.h"
|
||||||
/* device object has custom handler for all objects */
|
/* device object has custom handler for all objects */
|
||||||
@@ -104,10 +106,7 @@ static int RPM_Encode_Property(
|
|||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
uint16_t offset,
|
uint16_t offset,
|
||||||
uint16_t max_apdu,
|
uint16_t max_apdu,
|
||||||
BACNET_OBJECT_TYPE object_type,
|
BACNET_RPM_DATA *rpmdata)
|
||||||
uint32_t object_instance,
|
|
||||||
BACNET_PROPERTY_ID object_property,
|
|
||||||
int32_t array_index)
|
|
||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
size_t copy_len = 0;
|
size_t copy_len = 0;
|
||||||
@@ -115,26 +114,28 @@ static int RPM_Encode_Property(
|
|||||||
BACNET_READ_PROPERTY_DATA rpdata;
|
BACNET_READ_PROPERTY_DATA rpdata;
|
||||||
|
|
||||||
len =
|
len =
|
||||||
rpm_ack_encode_apdu_object_property(&Temp_Buf[0], object_property,
|
rpm_ack_encode_apdu_object_property(&Temp_Buf[0], rpmdata->object_property,
|
||||||
array_index);
|
rpmdata->array_index);
|
||||||
copy_len = memcopy(&apdu[0], &Temp_Buf[0], offset, len, max_apdu);
|
copy_len = memcopy(&apdu[0], &Temp_Buf[0], offset, len, max_apdu);
|
||||||
if (copy_len == 0) {
|
if (copy_len == 0) {
|
||||||
return 0;
|
rpmdata->error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
|
return BACNET_STATUS_ABORT;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
len = 0;
|
len = 0;
|
||||||
rpdata.error_class = ERROR_CLASS_OBJECT;
|
rpdata.error_class = ERROR_CLASS_OBJECT;
|
||||||
rpdata.error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
rpdata.error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||||
rpdata.object_type = object_type;
|
rpdata.object_type = rpmdata->object_type;
|
||||||
rpdata.object_instance = object_instance;
|
rpdata.object_instance = rpmdata->object_instance;
|
||||||
rpdata.object_property = object_property;
|
rpdata.object_property = rpmdata->object_property;
|
||||||
rpdata.array_index = array_index;
|
rpdata.array_index = rpmdata->array_index;
|
||||||
rpdata.application_data = &Temp_Buf[0];
|
rpdata.application_data = &Temp_Buf[0];
|
||||||
rpdata.application_data_len = sizeof(Temp_Buf);
|
rpdata.application_data_len = sizeof(Temp_Buf);
|
||||||
len = Device_Read_Property(&rpdata);
|
len = Device_Read_Property(&rpdata);
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
if (len == -2) {
|
if ((len == BACNET_STATUS_ABORT) || (len == BACNET_STATUS_REJECT)){
|
||||||
return 0; /* Ie, Abort */
|
/* pass on aborts and rejects for now */
|
||||||
|
return len; /* Ie, Abort */
|
||||||
}
|
}
|
||||||
/* error was returned - encode that for the response */
|
/* error was returned - encode that for the response */
|
||||||
len =
|
len =
|
||||||
@@ -142,8 +143,10 @@ static int RPM_Encode_Property(
|
|||||||
rpdata.error_class, rpdata.error_code);
|
rpdata.error_class, rpdata.error_code);
|
||||||
copy_len =
|
copy_len =
|
||||||
memcopy(&apdu[0], &Temp_Buf[0], offset + apdu_len, len, max_apdu);
|
memcopy(&apdu[0], &Temp_Buf[0], offset + apdu_len, len, max_apdu);
|
||||||
|
|
||||||
if (copy_len == 0) {
|
if (copy_len == 0) {
|
||||||
return 0;
|
rpmdata->error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
|
return BACNET_STATUS_ABORT;
|
||||||
}
|
}
|
||||||
} else if ((offset + apdu_len + 1 + len + 1) < max_apdu) {
|
} else if ((offset + apdu_len + 1 + len + 1) < max_apdu) {
|
||||||
/* enough room to fit the property value and tags */
|
/* enough room to fit the property value and tags */
|
||||||
@@ -152,7 +155,8 @@ static int RPM_Encode_Property(
|
|||||||
&Temp_Buf[0], len);
|
&Temp_Buf[0], len);
|
||||||
} else {
|
} else {
|
||||||
/* not enough room - abort! */
|
/* not enough room - abort! */
|
||||||
return 0;
|
rpmdata->error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
|
return BACNET_STATUS_ABORT;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
|
|
||||||
@@ -192,12 +196,10 @@ void handler_read_property_multiple(
|
|||||||
BACNET_NPDU_DATA npdu_data;
|
BACNET_NPDU_DATA npdu_data;
|
||||||
int bytes_sent;
|
int bytes_sent;
|
||||||
BACNET_ADDRESS my_address;
|
BACNET_ADDRESS my_address;
|
||||||
BACNET_OBJECT_TYPE object_type;
|
BACNET_RPM_DATA rpmdata;
|
||||||
uint32_t object_instance = 0;
|
|
||||||
int apdu_len = 0;
|
int apdu_len = 0;
|
||||||
int npdu_len = 0;
|
int npdu_len = 0;
|
||||||
BACNET_PROPERTY_ID object_property;
|
int error = 0;
|
||||||
int32_t array_index = 0;
|
|
||||||
|
|
||||||
/* jps_debug - see if we are utilizing all the buffer */
|
/* jps_debug - see if we are utilizing all the buffer */
|
||||||
/* memset(&Handler_Transmit_Buffer[0], 0xff, sizeof(Handler_Transmit_Buffer)); */
|
/* memset(&Handler_Transmit_Buffer[0], 0xff, sizeof(Handler_Transmit_Buffer)); */
|
||||||
@@ -208,14 +210,12 @@ void handler_read_property_multiple(
|
|||||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address,
|
npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address,
|
||||||
&npdu_data);
|
&npdu_data);
|
||||||
if (service_data->segmented_message) {
|
if (service_data->segmented_message) {
|
||||||
apdu_len =
|
rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
error = BACNET_STATUS_ABORT;
|
||||||
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
|
|
||||||
true);
|
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
printf("RPM: Segmented message. Sending Abort!\r\n");
|
printf("RPM: Segmented message. Sending Abort!\r\n");
|
||||||
#endif
|
#endif
|
||||||
goto RPM_ABORT;
|
goto RPM_FAILURE;
|
||||||
}
|
}
|
||||||
/* decode apdu request & encode apdu reply
|
/* decode apdu request & encode apdu reply
|
||||||
encode complex ack, invoke id, service choice */
|
encode complex ack, invoke id, service choice */
|
||||||
@@ -223,102 +223,61 @@ void handler_read_property_multiple(
|
|||||||
rpm_ack_encode_apdu_init(&Handler_Transmit_Buffer[npdu_len],
|
rpm_ack_encode_apdu_init(&Handler_Transmit_Buffer[npdu_len],
|
||||||
service_data->invoke_id);
|
service_data->invoke_id);
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
/* Start by looking for an object ID */
|
||||||
len =
|
len =
|
||||||
rpm_decode_object_id(&service_request[decode_len],
|
rpm_decode_object_id(&service_request[decode_len],
|
||||||
service_len - decode_len, &object_type, &object_instance);
|
service_len - decode_len, &rpmdata);
|
||||||
/* end of object? */
|
if (len >= 0) { /* Got one so skip to next stage */
|
||||||
if (len > 0) {
|
|
||||||
decode_len += len;
|
decode_len += len;
|
||||||
} else {
|
} else { /* bad encoding - skip to error/reject/abort handling */
|
||||||
len =
|
#if PRINT_ENABLED
|
||||||
rpm_decode_object_end(&service_request[decode_len],
|
fprintf(stderr, "RPM: Bad Encoding.\n");
|
||||||
service_len - decode_len);
|
#endif
|
||||||
if (len == 1) {
|
error = len;
|
||||||
decode_len++;
|
goto RPM_FAILURE;
|
||||||
len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]);
|
|
||||||
copy_len =
|
|
||||||
memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0],
|
|
||||||
apdu_len, len, sizeof(Handler_Transmit_Buffer));
|
|
||||||
if (!copy_len) {
|
|
||||||
apdu_len =
|
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
|
||||||
service_data->invoke_id,
|
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
} else {
|
|
||||||
apdu_len += copy_len;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
apdu_len =
|
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
|
||||||
service_data->invoke_id, ABORT_REASON_OTHER, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
len =
|
|
||||||
rpm_ack_encode_apdu_object_begin(&Temp_Buf[0], object_type,
|
/* Stick this object id into the reply - if it will fit */
|
||||||
object_instance);
|
len = rpm_ack_encode_apdu_object_begin(&Temp_Buf[0], &rpmdata);
|
||||||
copy_len =
|
copy_len =
|
||||||
memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len,
|
memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len,
|
||||||
len, sizeof(Handler_Transmit_Buffer));
|
len, sizeof(Handler_Transmit_Buffer));
|
||||||
if (!copy_len) {
|
if (!copy_len) {
|
||||||
apdu_len =
|
#if PRINT_ENABLED
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
printf("RPM: Response too big!\r\n");
|
||||||
service_data->invoke_id,
|
#endif
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
goto RPM_ABORT;
|
error = BACNET_STATUS_ABORT;
|
||||||
} else {
|
goto RPM_FAILURE;
|
||||||
apdu_len += copy_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apdu_len += copy_len;
|
||||||
/* do each property of this object of the RPM request */
|
/* do each property of this object of the RPM request */
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
/* Fetch a property */
|
||||||
len =
|
len =
|
||||||
rpm_decode_object_property(&service_request[decode_len],
|
rpm_decode_object_property(&service_request[decode_len],
|
||||||
service_len - decode_len, &object_property, &array_index);
|
service_len - decode_len, &rpmdata);
|
||||||
/* end of property list? */
|
if (len < 0) { /* bad encoding - skip to error/reject/abort handling */
|
||||||
if (len > 0) {
|
#if PRINT_ENABLED
|
||||||
decode_len += len;
|
fprintf(stderr, "RPM: Bad Encoding.\n");
|
||||||
} else {
|
#endif
|
||||||
len =
|
error = len;
|
||||||
rpm_decode_object_end(&service_request[decode_len],
|
goto RPM_FAILURE;
|
||||||
service_len - decode_len);
|
|
||||||
if (len == 1) {
|
|
||||||
decode_len++;
|
|
||||||
len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]);
|
|
||||||
copy_len =
|
|
||||||
memcopy(&Handler_Transmit_Buffer[npdu_len],
|
|
||||||
&Temp_Buf[0], apdu_len, len,
|
|
||||||
sizeof(Handler_Transmit_Buffer));
|
|
||||||
if (!copy_len) {
|
|
||||||
apdu_len =
|
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer
|
|
||||||
[npdu_len], service_data->invoke_id,
|
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
} else {
|
|
||||||
apdu_len += copy_len;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
apdu_len =
|
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
|
||||||
service_data->invoke_id, ABORT_REASON_OTHER, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
}
|
|
||||||
/* stop decoding properties */
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
decode_len += len;
|
||||||
/* handle the special properties */
|
/* handle the special properties */
|
||||||
if ((object_property == PROP_ALL) ||
|
if ((rpmdata.object_property == PROP_ALL) ||
|
||||||
(object_property == PROP_REQUIRED) ||
|
(rpmdata.object_property == PROP_REQUIRED) ||
|
||||||
(object_property == PROP_OPTIONAL)) {
|
(rpmdata.object_property == PROP_OPTIONAL)) {
|
||||||
struct special_property_list_t property_list;
|
struct special_property_list_t property_list;
|
||||||
unsigned property_count = 0;
|
unsigned property_count = 0;
|
||||||
unsigned index = 0;
|
unsigned index = 0;
|
||||||
BACNET_PROPERTY_ID special_object_property;
|
BACNET_PROPERTY_ID special_object_property;
|
||||||
|
|
||||||
special_object_property = object_property;
|
special_object_property = rpmdata.object_property;
|
||||||
Device_Objects_Property_List(object_type, &property_list);
|
Device_Objects_Property_List(rpmdata.object_type, &property_list);
|
||||||
property_count =
|
property_count =
|
||||||
RPM_Object_Property_Count(&property_list,
|
RPM_Object_Property_Count(&property_list,
|
||||||
special_object_property);
|
special_object_property);
|
||||||
@@ -326,34 +285,26 @@ void handler_read_property_multiple(
|
|||||||
/* handle the error code - but use the special property */
|
/* handle the error code - but use the special property */
|
||||||
len =
|
len =
|
||||||
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
||||||
(uint16_t)(npdu_len + apdu_len), MAX_APDU, object_type,
|
(uint16_t)(npdu_len + apdu_len), MAX_APDU, &rpmdata);
|
||||||
object_instance, object_property, array_index);
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
apdu_len =
|
error = len;
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer
|
goto RPM_FAILURE;
|
||||||
[npdu_len], service_data->invoke_id,
|
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (index = 0; index < property_count; index++) {
|
for (index = 0; index < property_count; index++) {
|
||||||
object_property =
|
rpmdata.object_property =
|
||||||
RPM_Object_Property(&property_list,
|
RPM_Object_Property(&property_list,
|
||||||
special_object_property, index);
|
special_object_property, index);
|
||||||
len =
|
len =
|
||||||
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
||||||
(uint16_t)(npdu_len + apdu_len), MAX_APDU, object_type,
|
(uint16_t)(npdu_len + apdu_len), MAX_APDU, &rpmdata);
|
||||||
object_instance, object_property, array_index);
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
apdu_len =
|
error = len;
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer
|
goto RPM_FAILURE;
|
||||||
[npdu_len], service_data->invoke_id,
|
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -361,24 +312,37 @@ void handler_read_property_multiple(
|
|||||||
/* handle an individual property */
|
/* handle an individual property */
|
||||||
len =
|
len =
|
||||||
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
RPM_Encode_Property(&Handler_Transmit_Buffer[0],
|
||||||
(uint16_t)(npdu_len + apdu_len), sizeof(Handler_Transmit_Buffer),
|
(uint16_t)(npdu_len + apdu_len), sizeof(Handler_Transmit_Buffer), &rpmdata);
|
||||||
object_type, object_instance, object_property,
|
|
||||||
array_index);
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
apdu_len =
|
error = len;
|
||||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
goto RPM_FAILURE;
|
||||||
service_data->invoke_id,
|
|
||||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
|
||||||
goto RPM_ABORT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (decode_is_closing_tag_number(&service_request[decode_len], 1)) {
|
||||||
|
/* Reached end of property list so cap the result list */
|
||||||
|
decode_len++;
|
||||||
|
len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]);
|
||||||
|
copy_len =
|
||||||
|
memcopy(&Handler_Transmit_Buffer[npdu_len],
|
||||||
|
&Temp_Buf[0], apdu_len, len,
|
||||||
|
sizeof(Handler_Transmit_Buffer));
|
||||||
|
if (!copy_len) {
|
||||||
|
rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
|
error = BACNET_STATUS_ABORT;
|
||||||
|
goto RPM_FAILURE;
|
||||||
|
} else {
|
||||||
|
apdu_len += copy_len;
|
||||||
|
}
|
||||||
|
break; /* finished with this property list */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (decode_len >= service_len) {
|
if(service_len == decode_len) /* Reached the end so finish up */
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apdu_len > service_data->max_resp) {
|
if (apdu_len > service_data->max_resp) {
|
||||||
/* too big for the sender - send an abort */
|
/* too big for the sender - send an abort */
|
||||||
apdu_len =
|
apdu_len =
|
||||||
@@ -388,9 +352,43 @@ void handler_read_property_multiple(
|
|||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr, "RPM: Message too large. Sending Abort!\n");
|
fprintf(stderr, "RPM: Message too large. Sending Abort!\n");
|
||||||
#endif
|
#endif
|
||||||
goto RPM_ABORT;
|
goto RPM_FAILURE;
|
||||||
}
|
}
|
||||||
RPM_ABORT:
|
|
||||||
|
RPM_FAILURE:
|
||||||
|
if (error) {
|
||||||
|
if (error == BACNET_STATUS_ABORT) {
|
||||||
|
/* Kludge alert! At the moment we assume any abort is due to
|
||||||
|
* to space issues due to segmentation or lack thereof. I wanted to show the proper
|
||||||
|
* handling via the abort_convert_error_code() so I put the error code
|
||||||
|
* in here, if you are sure all aborts properly set up the error_code then
|
||||||
|
* remove this next line
|
||||||
|
*/
|
||||||
|
rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||||
|
apdu_len =
|
||||||
|
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||||
|
service_data->invoke_id,
|
||||||
|
abort_convert_error_code(rpmdata.error_code), true);
|
||||||
|
#if PRINT_ENABLED
|
||||||
|
fprintf(stderr, "RP: Sending Abort!\n");
|
||||||
|
#endif
|
||||||
|
} else if (error == BACNET_STATUS_ERROR){
|
||||||
|
apdu_len =
|
||||||
|
bacerror_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||||
|
service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY,
|
||||||
|
rpmdata.error_class, rpmdata.error_code);
|
||||||
|
#if PRINT_ENABLED
|
||||||
|
fprintf(stderr, "RP: Sending Error!\n");
|
||||||
|
#endif
|
||||||
|
} else if (error == BACNET_STATUS_REJECT){
|
||||||
|
apdu_len = reject_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||||
|
service_data->invoke_id, reject_convert_error_code(rpmdata.error_code));
|
||||||
|
#if PRINT_ENABLED
|
||||||
|
fprintf(stderr, "RP: Sending Reject!\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pdu_len = apdu_len + npdu_len;
|
pdu_len = apdu_len + npdu_len;
|
||||||
bytes_sent =
|
bytes_sent =
|
||||||
datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0],
|
datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
<Tool
|
<Tool
|
||||||
Name="VCCLCompilerTool"
|
Name="VCCLCompilerTool"
|
||||||
Optimization="0"
|
Optimization="0"
|
||||||
AdditionalIncludeDirectories="..\..\..\include;..\..\..\ports\win32"
|
AdditionalIncludeDirectories="..\..\..\include;..\..\..\ports\win32;..\..\..\test"
|
||||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||||
MinimalRebuild="true"
|
MinimalRebuild="true"
|
||||||
BasicRuntimeChecks="3"
|
BasicRuntimeChecks="3"
|
||||||
|
|||||||
@@ -40,6 +40,20 @@
|
|||||||
#include "bacdef.h"
|
#include "bacdef.h"
|
||||||
#include "bacapp.h"
|
#include "bacapp.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bundle together commonly used data items for convenience when calling
|
||||||
|
* rpm helper functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct BACnet_RPM_Data {
|
||||||
|
BACNET_OBJECT_TYPE object_type;
|
||||||
|
uint32_t object_instance;
|
||||||
|
BACNET_PROPERTY_ID object_property;
|
||||||
|
int32_t array_index;
|
||||||
|
BACNET_ERROR_CLASS error_class;
|
||||||
|
BACNET_ERROR_CODE error_code;
|
||||||
|
} BACNET_RPM_DATA;
|
||||||
|
|
||||||
struct BACnet_Read_Access_Data;
|
struct BACnet_Read_Access_Data;
|
||||||
typedef struct BACnet_Read_Access_Data {
|
typedef struct BACnet_Read_Access_Data {
|
||||||
BACNET_OBJECT_TYPE object_type;
|
BACNET_OBJECT_TYPE object_type;
|
||||||
@@ -119,8 +133,7 @@ extern "C" {
|
|||||||
int rpm_decode_object_id(
|
int rpm_decode_object_id(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
unsigned apdu_len,
|
unsigned apdu_len,
|
||||||
BACNET_OBJECT_TYPE * object_type,
|
BACNET_RPM_DATA *rpmdata);
|
||||||
uint32_t * object_instance);
|
|
||||||
|
|
||||||
/* is this the end of this object property list? */
|
/* is this the end of this object property list? */
|
||||||
int rpm_decode_object_end(
|
int rpm_decode_object_end(
|
||||||
@@ -131,8 +144,7 @@ extern "C" {
|
|||||||
int rpm_decode_object_property(
|
int rpm_decode_object_property(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
unsigned apdu_len,
|
unsigned apdu_len,
|
||||||
BACNET_PROPERTY_ID * object_property,
|
BACNET_RPM_DATA *rpmdata);
|
||||||
int32_t * array_index);
|
|
||||||
|
|
||||||
/* RPM Ack - reply from server */
|
/* RPM Ack - reply from server */
|
||||||
int rpm_ack_encode_apdu_init(
|
int rpm_ack_encode_apdu_init(
|
||||||
@@ -141,8 +153,7 @@ extern "C" {
|
|||||||
|
|
||||||
int rpm_ack_encode_apdu_object_begin(
|
int rpm_ack_encode_apdu_object_begin(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
BACNET_OBJECT_TYPE object_type,
|
BACNET_RPM_DATA *rpmdata);
|
||||||
uint32_t object_instance);
|
|
||||||
|
|
||||||
int rpm_ack_encode_apdu_object_property(
|
int rpm_ack_encode_apdu_object_property(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
|
|||||||
+93
-67
@@ -1,3 +1,4 @@
|
|||||||
|
#define TEST
|
||||||
/*####COPYRIGHTBEGIN####
|
/*####COPYRIGHTBEGIN####
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
Copyright (C) 2005 Steve Karg
|
Copyright (C) 2005 Steve Karg
|
||||||
@@ -191,27 +192,35 @@ int rpm_encode_apdu(
|
|||||||
return apdu_len;
|
return apdu_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* decode the object portion of the service request only */
|
/* decode the object portion of the service request only. Bails out if
|
||||||
|
* tags are wrong or missing/incomplete
|
||||||
|
*/
|
||||||
int rpm_decode_object_id(
|
int rpm_decode_object_id(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
unsigned apdu_len,
|
unsigned apdu_len,
|
||||||
BACNET_OBJECT_TYPE * object_type,
|
BACNET_RPM_DATA *rpmdata)
|
||||||
uint32_t * object_instance)
|
|
||||||
{
|
{
|
||||||
unsigned len = 0;
|
unsigned len = 0;
|
||||||
uint16_t type = 0; /* for decoding */
|
uint16_t type = 0; /* for decoding */
|
||||||
|
|
||||||
/* check for value pointers */
|
/* check for value pointers */
|
||||||
if (apdu && apdu_len && object_type && object_instance) {
|
if (apdu && apdu_len && rpmdata) {
|
||||||
|
if(apdu_len < 5) { /* Must be at least 2 tags and an object id */
|
||||||
|
rpmdata->error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
/* Tag 0: Object ID */
|
/* Tag 0: Object ID */
|
||||||
if (!decode_is_context_tag(&apdu[len++], 0))
|
if (!decode_is_context_tag(&apdu[len++], 0)) {
|
||||||
return -1;
|
rpmdata->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||||
len += decode_object_id(&apdu[len], &type, object_instance);
|
return BACNET_STATUS_REJECT;
|
||||||
if (object_type)
|
}
|
||||||
*object_type = (BACNET_OBJECT_TYPE) type;
|
len += decode_object_id(&apdu[len], &type, &rpmdata->object_instance);
|
||||||
|
rpmdata->object_type = (BACNET_OBJECT_TYPE) type;
|
||||||
/* Tag 1: sequence of ReadAccessSpecification */
|
/* Tag 1: sequence of ReadAccessSpecification */
|
||||||
if (!decode_is_opening_tag_number(&apdu[len], 1))
|
if (!decode_is_opening_tag_number(&apdu[len], 1)) {
|
||||||
return -1;
|
rpmdata->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
len++; /* opening tag is only one octet */
|
len++; /* opening tag is only one octet */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +234,7 @@ int rpm_decode_object_end(
|
|||||||
int len = 0; /* total length of the apdu, return value */
|
int len = 0; /* total length of the apdu, return value */
|
||||||
|
|
||||||
if (apdu && apdu_len) {
|
if (apdu && apdu_len) {
|
||||||
if (decode_is_closing_tag_number(apdu, 1))
|
if (decode_is_closing_tag_number(apdu, 1) == true)
|
||||||
len = 1;
|
len = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +252,7 @@ int rpm_decode_object_end(
|
|||||||
int rpm_decode_object_property(
|
int rpm_decode_object_property(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
unsigned apdu_len,
|
unsigned apdu_len,
|
||||||
BACNET_PROPERTY_ID * object_property,
|
BACNET_RPM_DATA *rpmdata)
|
||||||
int32_t * array_index)
|
|
||||||
{
|
{
|
||||||
unsigned len = 0;
|
unsigned len = 0;
|
||||||
unsigned option_len = 0;
|
unsigned option_len = 0;
|
||||||
@@ -254,33 +262,46 @@ int rpm_decode_object_property(
|
|||||||
uint32_t array_value = 0; /* for decoding */
|
uint32_t array_value = 0; /* for decoding */
|
||||||
|
|
||||||
/* check for valid pointers */
|
/* check for valid pointers */
|
||||||
if (apdu && apdu_len && object_property && array_index) {
|
if (apdu && apdu_len && rpmdata) {
|
||||||
/* Tag 0: propertyIdentifier */
|
/* Tag 0: propertyIdentifier */
|
||||||
if (!IS_CONTEXT_SPECIFIC(apdu[len]))
|
if (!IS_CONTEXT_SPECIFIC(apdu[len])) {
|
||||||
return -1;
|
rpmdata->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
|
|
||||||
len +=
|
len +=
|
||||||
decode_tag_number_and_value(&apdu[len], &tag_number,
|
decode_tag_number_and_value(&apdu[len], &tag_number,
|
||||||
&len_value_type);
|
&len_value_type);
|
||||||
if (tag_number != 0)
|
if (tag_number != 0) {
|
||||||
return -1;
|
rpmdata->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
|
/* Should be at least the unsigned value + 1 tag left */
|
||||||
|
if ((len + len_value_type) >= apdu_len) {
|
||||||
|
rpmdata->error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
len += decode_enumerated(&apdu[len], len_value_type, &property);
|
len += decode_enumerated(&apdu[len], len_value_type, &property);
|
||||||
if (object_property)
|
rpmdata->object_property = (BACNET_PROPERTY_ID) property;
|
||||||
*object_property = (BACNET_PROPERTY_ID) property;
|
|
||||||
/* Tag 1: Optional propertyArrayIndex */
|
/* Tag 1: Optional propertyArrayIndex */
|
||||||
if ((len < apdu_len) && IS_CONTEXT_SPECIFIC(apdu[len]) &&
|
rpmdata->array_index = BACNET_ARRAY_ALL; /* Assume most probable outcome */
|
||||||
(!IS_CLOSING_TAG(apdu[len]))) {
|
if (IS_CONTEXT_SPECIFIC(apdu[len]) && !IS_CLOSING_TAG(apdu[len])) {
|
||||||
option_len =
|
option_len =
|
||||||
(unsigned) decode_tag_number_and_value(&apdu[len], &tag_number,
|
(unsigned) decode_tag_number_and_value(&apdu[len], &tag_number,
|
||||||
&len_value_type);
|
&len_value_type);
|
||||||
if (tag_number == 1) {
|
if (tag_number == 1) {
|
||||||
len += option_len;
|
len += option_len;
|
||||||
|
/* Should be at least the unsigned array index + 1 tag left */
|
||||||
|
if ((len + len_value_type) >= apdu_len) {
|
||||||
|
rpmdata->error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||||
|
return BACNET_STATUS_REJECT;
|
||||||
|
}
|
||||||
len +=
|
len +=
|
||||||
decode_unsigned(&apdu[len], len_value_type, &array_value);
|
decode_unsigned(&apdu[len], len_value_type, &array_value);
|
||||||
*array_index = array_value;
|
rpmdata->array_index = array_value;
|
||||||
} else
|
}
|
||||||
*array_index = BACNET_ARRAY_ALL;
|
}
|
||||||
} else
|
|
||||||
*array_index = BACNET_ARRAY_ALL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int) len;
|
return (int) len;
|
||||||
@@ -304,16 +325,15 @@ int rpm_ack_encode_apdu_init(
|
|||||||
|
|
||||||
int rpm_ack_encode_apdu_object_begin(
|
int rpm_ack_encode_apdu_object_begin(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
BACNET_OBJECT_TYPE object_type,
|
BACNET_RPM_DATA *rpmdata)
|
||||||
uint32_t object_instance)
|
|
||||||
{
|
{
|
||||||
int apdu_len = 0; /* total length of the apdu, return value */
|
int apdu_len = 0; /* total length of the apdu, return value */
|
||||||
|
|
||||||
if (apdu) {
|
if (apdu) {
|
||||||
/* Tag 0: objectIdentifier */
|
/* Tag 0: objectIdentifier */
|
||||||
apdu_len =
|
apdu_len =
|
||||||
encode_context_object_id(&apdu[0], 0, object_type,
|
encode_context_object_id(&apdu[0], 0, rpmdata->object_type,
|
||||||
object_instance);
|
rpmdata->object_instance);
|
||||||
/* Tag 1: listOfResults */
|
/* Tag 1: listOfResults */
|
||||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 1);
|
apdu_len += encode_opening_tag(&apdu[apdu_len], 1);
|
||||||
}
|
}
|
||||||
@@ -555,10 +575,12 @@ void testReadPropertyMultiple(
|
|||||||
uint8_t test_invoke_id = 0;
|
uint8_t test_invoke_id = 0;
|
||||||
uint8_t *service_request = NULL;
|
uint8_t *service_request = NULL;
|
||||||
unsigned service_request_len = 0;
|
unsigned service_request_len = 0;
|
||||||
BACNET_OBJECT_TYPE object_type = OBJECT_DEVICE;
|
BACNET_RPM_DATA rpmdata;
|
||||||
uint32_t object_instance = 0;
|
|
||||||
BACNET_PROPERTY_ID object_property = PROP_OBJECT_IDENTIFIER;
|
rpmdata.object_type = OBJECT_DEVICE;
|
||||||
int32_t array_index = 0;
|
rpmdata.object_instance = 0;
|
||||||
|
rpmdata.object_property = PROP_OBJECT_IDENTIFIER;
|
||||||
|
rpmdata.array_index = 0;
|
||||||
|
|
||||||
/* build the RPM - try to make it easy for the Application Layer development */
|
/* build the RPM - try to make it easy for the Application Layer development */
|
||||||
/* IDEA: similar construction, but pass apdu, apdu_len pointer, size of apdu to
|
/* IDEA: similar construction, but pass apdu, apdu_len pointer, size of apdu to
|
||||||
@@ -600,31 +622,31 @@ void testReadPropertyMultiple(
|
|||||||
|
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_id(service_request, service_request_len,
|
rpm_decode_object_id(service_request, service_request_len,
|
||||||
&object_type, &object_instance);
|
&rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_type == OBJECT_DEVICE);
|
ct_test(pTest, rpmdata.object_type == OBJECT_DEVICE);
|
||||||
ct_test(pTest, object_instance == 123);
|
ct_test(pTest, rpmdata.object_instance == 123);
|
||||||
len = test_len;
|
len = test_len;
|
||||||
/* decode the object property portion of the service request */
|
/* decode the object property portion of the service request */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_property == PROP_OBJECT_IDENTIFIER);
|
ct_test(pTest, rpmdata.object_property == PROP_OBJECT_IDENTIFIER);
|
||||||
ct_test(pTest, array_index == BACNET_ARRAY_ALL);
|
ct_test(pTest, rpmdata.array_index == BACNET_ARRAY_ALL);
|
||||||
len += test_len;
|
len += test_len;
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_property == PROP_OBJECT_NAME);
|
ct_test(pTest, rpmdata.object_property == PROP_OBJECT_NAME);
|
||||||
ct_test(pTest, array_index == BACNET_ARRAY_ALL);
|
ct_test(pTest, rpmdata.array_index == BACNET_ARRAY_ALL);
|
||||||
len += test_len;
|
len += test_len;
|
||||||
/* try again - we should fail */
|
/* try again - we should fail */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len == -1);
|
ct_test(pTest, test_len < 0);
|
||||||
/* is it the end of this object? */
|
/* is it the end of this object? */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_end(&service_request[len],
|
rpm_decode_object_end(&service_request[len],
|
||||||
@@ -634,30 +656,30 @@ void testReadPropertyMultiple(
|
|||||||
/* try to decode an object id */
|
/* try to decode an object id */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_id(&service_request[len], service_request_len - len,
|
rpm_decode_object_id(&service_request[len], service_request_len - len,
|
||||||
&object_type, &object_instance);
|
&rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_type == OBJECT_ANALOG_INPUT);
|
ct_test(pTest, rpmdata.object_type == OBJECT_ANALOG_INPUT);
|
||||||
ct_test(pTest, object_instance == 33);
|
ct_test(pTest, rpmdata.object_instance == 33);
|
||||||
len += test_len;
|
len += test_len;
|
||||||
/* decode the object property portion of the service request only */
|
/* decode the object property portion of the service request only */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_property == PROP_OBJECT_IDENTIFIER);
|
ct_test(pTest, rpmdata.object_property == PROP_OBJECT_IDENTIFIER);
|
||||||
ct_test(pTest, array_index == BACNET_ARRAY_ALL);
|
ct_test(pTest, rpmdata.array_index == BACNET_ARRAY_ALL);
|
||||||
len += test_len;
|
len += test_len;
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len != -1);
|
ct_test(pTest, test_len > 0);
|
||||||
ct_test(pTest, object_property == PROP_ALL);
|
ct_test(pTest, rpmdata.object_property == PROP_ALL);
|
||||||
ct_test(pTest, array_index == BACNET_ARRAY_ALL);
|
ct_test(pTest, rpmdata.array_index == BACNET_ARRAY_ALL);
|
||||||
len += test_len;
|
len += test_len;
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_property(&service_request[len],
|
rpm_decode_object_property(&service_request[len],
|
||||||
service_request_len - len, &object_property, &array_index);
|
service_request_len - len, &rpmdata);
|
||||||
ct_test(pTest, test_len == -1);
|
ct_test(pTest, test_len < 0);
|
||||||
/* got an error -1, is it the end of this object? */
|
/* got an error -1, is it the end of this object? */
|
||||||
test_len =
|
test_len =
|
||||||
rpm_decode_object_end(&service_request[len],
|
rpm_decode_object_end(&service_request[len],
|
||||||
@@ -688,6 +710,7 @@ void testReadPropertyMultipleAck(
|
|||||||
int application_data_buffer_len = 0;
|
int application_data_buffer_len = 0;
|
||||||
BACNET_ERROR_CLASS error_class;
|
BACNET_ERROR_CLASS error_class;
|
||||||
BACNET_ERROR_CODE error_code;
|
BACNET_ERROR_CODE error_code;
|
||||||
|
BACNET_RPM_DATA rpmdata;
|
||||||
|
|
||||||
/* build the RPM - try to make it easy for the
|
/* build the RPM - try to make it easy for the
|
||||||
Application Layer development */
|
Application Layer development */
|
||||||
@@ -701,8 +724,10 @@ void testReadPropertyMultipleAck(
|
|||||||
apdu_len = rpm_ack_encode_apdu_init(&apdu[0], invoke_id);
|
apdu_len = rpm_ack_encode_apdu_init(&apdu[0], invoke_id);
|
||||||
|
|
||||||
/* object beginning */
|
/* object beginning */
|
||||||
|
rpmdata.object_type = OBJECT_DEVICE;
|
||||||
|
rpmdata.object_instance = 123;
|
||||||
apdu_len +=
|
apdu_len +=
|
||||||
rpm_ack_encode_apdu_object_begin(&apdu[apdu_len], OBJECT_DEVICE, 123);
|
rpm_ack_encode_apdu_object_begin(&apdu[apdu_len], &rpmdata);
|
||||||
/* reply property */
|
/* reply property */
|
||||||
apdu_len +=
|
apdu_len +=
|
||||||
rpm_ack_encode_apdu_object_property(&apdu[apdu_len],
|
rpm_ack_encode_apdu_object_property(&apdu[apdu_len],
|
||||||
@@ -734,9 +759,10 @@ void testReadPropertyMultipleAck(
|
|||||||
apdu_len += rpm_ack_encode_apdu_object_end(&apdu[apdu_len]);
|
apdu_len += rpm_ack_encode_apdu_object_end(&apdu[apdu_len]);
|
||||||
|
|
||||||
/* object beginning */
|
/* object beginning */
|
||||||
|
rpmdata.object_type = OBJECT_ANALOG_INPUT;
|
||||||
|
rpmdata.object_instance = 33;
|
||||||
apdu_len +=
|
apdu_len +=
|
||||||
rpm_ack_encode_apdu_object_begin(&apdu[apdu_len], OBJECT_ANALOG_INPUT,
|
rpm_ack_encode_apdu_object_begin(&apdu[apdu_len], &rpmdata);
|
||||||
33);
|
|
||||||
/* reply property */
|
/* reply property */
|
||||||
apdu_len +=
|
apdu_len +=
|
||||||
rpm_ack_encode_apdu_object_property(&apdu[apdu_len],
|
rpm_ack_encode_apdu_object_property(&apdu[apdu_len],
|
||||||
|
|||||||
Reference in New Issue
Block a user