Refactored RP, WP, RPM to reduce coupling with objects for Linux and Win32 ports.
This commit is contained in:
@@ -38,65 +38,13 @@
|
||||
#include "abort.h"
|
||||
#include "rp.h"
|
||||
|
||||
static uint8_t Temp_Buf[MAX_APDU] = { 0 };
|
||||
/* function that handles the reading of properties from objects */
|
||||
static read_property_function Read_Property_Function;
|
||||
|
||||
static read_property_function Read_Property[MAX_BACNET_OBJECT_TYPE];
|
||||
|
||||
static object_valid_instance_function Valid_Instance[MAX_BACNET_OBJECT_TYPE];
|
||||
|
||||
void handler_read_property_object_set(
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
read_property_function pFunction1,
|
||||
object_valid_instance_function pFunction2)
|
||||
void handler_read_property_function_set(
|
||||
read_property_function pFunction)
|
||||
{
|
||||
if (object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
Read_Property[object_type] = pFunction1;
|
||||
Valid_Instance[object_type] = pFunction2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Encodes the property APDU and returns the length,
|
||||
or sets the error, and returns -1 */
|
||||
int Encode_Property_APDU(
|
||||
uint8_t * apdu,
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
uint32_t object_instance,
|
||||
BACNET_PROPERTY_ID property,
|
||||
int32_t array_index,
|
||||
BACNET_ERROR_CLASS * error_class,
|
||||
BACNET_ERROR_CODE * error_code)
|
||||
{
|
||||
int apdu_len = -1;
|
||||
read_property_function object_rp = NULL;
|
||||
object_valid_instance_function object_valid = NULL;
|
||||
|
||||
/* initialize the default return values */
|
||||
*error_class = ERROR_CLASS_OBJECT;
|
||||
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
/* handle each object type */
|
||||
if (object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
object_rp = Read_Property[object_type];
|
||||
object_valid = Valid_Instance[object_type];
|
||||
}
|
||||
if (object_rp && object_valid) {
|
||||
/* There are handlers for this type of object */
|
||||
if(object_valid(object_instance)) {
|
||||
apdu_len =
|
||||
object_rp(&apdu[0], object_instance, property, array_index,
|
||||
error_class, error_code);
|
||||
} else {
|
||||
/* The specified object instance is not valid */
|
||||
*error_class = ERROR_CLASS_OBJECT;
|
||||
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* We don't support this object type */
|
||||
*error_class = ERROR_CLASS_OBJECT;
|
||||
*error_code = ERROR_CODE_UNSUPPORTED_OBJECT_TYPE;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
Read_Property_Function = pFunction;
|
||||
}
|
||||
|
||||
void handler_read_property(
|
||||
@@ -105,26 +53,26 @@ void handler_read_property(
|
||||
BACNET_ADDRESS * src,
|
||||
BACNET_CONFIRMED_SERVICE_DATA * service_data)
|
||||
{
|
||||
BACNET_READ_PROPERTY_DATA data;
|
||||
BACNET_READ_PROPERTY_DATA rpdata;
|
||||
int len = 0;
|
||||
int pdu_len = 0;
|
||||
int apdu_len = -1;
|
||||
int npdu_len = -1;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
bool error = false;
|
||||
int bytes_sent = 0;
|
||||
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
|
||||
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
BACNET_ADDRESS my_address;
|
||||
|
||||
/* encode the NPDU portion of the packet */
|
||||
datalink_get_my_address(&my_address);
|
||||
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
|
||||
pdu_len =
|
||||
npdu_len =
|
||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address,
|
||||
&npdu_data);
|
||||
if (service_data->segmented_message) {
|
||||
/* we don't support segmentation - send an abort */
|
||||
len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
apdu_len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
|
||||
true);
|
||||
#if PRINT_ENABLED
|
||||
@@ -132,78 +80,81 @@ void handler_read_property(
|
||||
#endif
|
||||
goto RP_ABORT;
|
||||
}
|
||||
|
||||
len = rp_decode_service_request(service_request, service_len, &data);
|
||||
len = rp_decode_service_request(service_request, service_len, &rpdata);
|
||||
#if PRINT_ENABLED
|
||||
if (len <= 0)
|
||||
if (len <= 0) {
|
||||
fprintf(stderr, "RP: Unable to decode Request!\n");
|
||||
}
|
||||
#endif
|
||||
if (len < 0) {
|
||||
/* bad decoding - send an abort */
|
||||
len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
apdu_len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id, ABORT_REASON_OTHER, true);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "RP: Bad Encoding. Sending Abort!\n");
|
||||
#endif
|
||||
goto RP_ABORT;
|
||||
}
|
||||
|
||||
/* assume that there is an error */
|
||||
error = true;
|
||||
len =
|
||||
Encode_Property_APDU(&Temp_Buf[0], data.object_type,
|
||||
data.object_instance, data.object_property, data.array_index,
|
||||
&error_class, &error_code);
|
||||
if (len >= 0) {
|
||||
/* encode the APDU portion of the packet */
|
||||
data.application_data = &Temp_Buf[0];
|
||||
data.application_data_len = len;
|
||||
/* FIXME: probably need a length limitation sent with encode */
|
||||
len =
|
||||
rp_ack_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, &data);
|
||||
if (len > service_data->max_resp) {
|
||||
/* too big for the sender - send an abort */
|
||||
len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
|
||||
true);
|
||||
if (Read_Property_Function) {
|
||||
apdu_len = rp_ack_encode_apdu_init(
|
||||
&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id,
|
||||
&rpdata);
|
||||
/* configure our storage */
|
||||
rpdata.application_data = &Handler_Transmit_Buffer[npdu_len+apdu_len];
|
||||
rpdata.application_data_len =
|
||||
sizeof(Handler_Transmit_Buffer) - (npdu_len + apdu_len);
|
||||
len = Read_Property_Function(&rpdata);
|
||||
if (len >= 0) {
|
||||
apdu_len += len;
|
||||
len = rp_ack_encode_apdu_object_property_end(
|
||||
&Handler_Transmit_Buffer[npdu_len+apdu_len]);
|
||||
apdu_len += len;
|
||||
if (apdu_len > service_data->max_resp) {
|
||||
/* too big for the sender - send an abort */
|
||||
apdu_len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
|
||||
true);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "RP: Message too large. Sending Abort!\n");
|
||||
fprintf(stderr, "RP: Message too large. Sending Abort!\n");
|
||||
#endif
|
||||
goto RP_ABORT;
|
||||
} else {
|
||||
goto RP_ABORT;
|
||||
} else {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "RP: Sending Ack!\n");
|
||||
fprintf(stderr, "RP: Sending Ack!\n");
|
||||
#endif
|
||||
error = false;
|
||||
error = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (error) {
|
||||
if (len == -2) {
|
||||
/* BACnet APDU too small to fit data, so proper response is Abort */
|
||||
len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
apdu_len =
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id,
|
||||
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "RP: Reply too big to fit into APDU!\n");
|
||||
#endif
|
||||
} else {
|
||||
len =
|
||||
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
apdu_len =
|
||||
bacerror_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
|
||||
service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
error_class, error_code);
|
||||
rpdata.error_class, rpdata.error_code);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "RP: Sending Error!\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
RP_ABORT:
|
||||
pdu_len += len;
|
||||
pdu_len = npdu_len + apdu_len;
|
||||
bytes_sent =
|
||||
datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
pdu_len);
|
||||
#if PRINT_ENABLED
|
||||
if (bytes_sent <= 0)
|
||||
@@ -212,29 +163,3 @@ void handler_read_property(
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int local_read_property(
|
||||
uint8_t * value,
|
||||
uint8_t * status,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *Source,
|
||||
BACNET_ERROR_CLASS * error_class,
|
||||
BACNET_ERROR_CODE * error_code)
|
||||
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
/* Try to fetch the required property */
|
||||
len = Encode_Property_APDU(value, Source->objectIdentifier.type,
|
||||
Source->objectIdentifier.instance, Source->propertyIdentifier,
|
||||
Source->arrayIndex, error_class, error_code);
|
||||
|
||||
if((len >= 0) && (status != NULL)){
|
||||
/* Fetch the status flags if required */
|
||||
Encode_Property_APDU(status, Source->objectIdentifier.type,
|
||||
Source->objectIdentifier.instance, PROP_STATUS_FLAGS,
|
||||
0, error_class, error_code);
|
||||
}
|
||||
|
||||
return(len);
|
||||
}
|
||||
|
||||
@@ -39,85 +39,24 @@
|
||||
#include "rpm.h"
|
||||
#include "handlers.h"
|
||||
|
||||
/* function that handles the reading of properties from objects */
|
||||
static read_property_function Read_Property_Function;
|
||||
static rpm_object_property_lists_function RPM_Property_List;
|
||||
|
||||
void handler_rpm_function_set(
|
||||
read_property_function pFunction)
|
||||
{
|
||||
Read_Property_Function = pFunction;
|
||||
}
|
||||
|
||||
void handler_rpm_list_set(
|
||||
rpm_object_property_lists_function pFunction)
|
||||
{
|
||||
RPM_Property_List = pFunction;
|
||||
}
|
||||
|
||||
static uint8_t Temp_Buf[MAX_APDU] = { 0 };
|
||||
|
||||
static rpm_property_lists_function RPM_Lists[MAX_BACNET_OBJECT_TYPE];
|
||||
|
||||
struct property_list_t {
|
||||
const int *pList;
|
||||
unsigned count;
|
||||
};
|
||||
|
||||
struct special_property_list_t {
|
||||
struct property_list_t Required;
|
||||
struct property_list_t Optional;
|
||||
struct property_list_t Proprietary;
|
||||
};
|
||||
|
||||
static unsigned property_list_count(
|
||||
const int *pList)
|
||||
{
|
||||
unsigned property_count = 0;
|
||||
|
||||
if (pList) {
|
||||
while (*pList != -1) {
|
||||
property_count++;
|
||||
pList++;
|
||||
}
|
||||
}
|
||||
|
||||
return property_count;
|
||||
}
|
||||
|
||||
void handler_read_property_multiple_list_set(
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
rpm_property_lists_function pFunction)
|
||||
{
|
||||
if (object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
RPM_Lists[object_type] = pFunction;
|
||||
}
|
||||
}
|
||||
|
||||
/* for a given object type, returns the special property list */
|
||||
static void RPM_Property_List(
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
struct special_property_list_t *pPropertyList)
|
||||
{
|
||||
rpm_property_lists_function object_property_list = NULL;
|
||||
pPropertyList->Required.pList = NULL;
|
||||
pPropertyList->Optional.pList = NULL;
|
||||
pPropertyList->Proprietary.pList = NULL;
|
||||
|
||||
if (object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
object_property_list = RPM_Lists[object_type];
|
||||
}
|
||||
if (object_property_list) {
|
||||
object_property_list(&pPropertyList->Required.pList,
|
||||
&pPropertyList->Optional.pList, &pPropertyList->Proprietary.pList);
|
||||
}
|
||||
/* fill the count */
|
||||
if (pPropertyList->Required.pList) {
|
||||
pPropertyList->Required.count =
|
||||
property_list_count(pPropertyList->Required.pList);
|
||||
} else {
|
||||
pPropertyList->Required.count = 0;
|
||||
}
|
||||
if (pPropertyList->Optional.pList) {
|
||||
pPropertyList->Optional.count =
|
||||
property_list_count(pPropertyList->Optional.pList);
|
||||
} else {
|
||||
pPropertyList->Optional.count = 0;
|
||||
}
|
||||
if (pPropertyList->Proprietary.pList) {
|
||||
pPropertyList->Proprietary.count =
|
||||
property_list_count(pPropertyList->Proprietary.pList);
|
||||
} else {
|
||||
pPropertyList->Proprietary.count = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int RPM_Object_Property(
|
||||
struct special_property_list_t *pPropertyList,
|
||||
BACNET_PROPERTY_ID special_property,
|
||||
@@ -185,8 +124,7 @@ static int RPM_Encode_Property(
|
||||
int len = 0;
|
||||
size_t copy_len = 0;
|
||||
int apdu_len = 0;
|
||||
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
|
||||
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
BACNET_READ_PROPERTY_DATA rpdata;
|
||||
|
||||
len =
|
||||
rpm_ack_encode_apdu_object_property(&Temp_Buf[0], object_property,
|
||||
@@ -196,14 +134,23 @@ static int RPM_Encode_Property(
|
||||
return 0;
|
||||
}
|
||||
apdu_len += len;
|
||||
len =
|
||||
Encode_Property_APDU(&Temp_Buf[0], object_type, object_instance,
|
||||
object_property, array_index, &error_class, &error_code);
|
||||
len = 0;
|
||||
if (Read_Property_Function) {
|
||||
rpdata.error_class = ERROR_CLASS_OBJECT;
|
||||
rpdata.error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
rpdata.object_type = object_type;
|
||||
rpdata.object_instance = object_instance;
|
||||
rpdata.object_property = object_property;
|
||||
rpdata.array_index = array_index;
|
||||
rpdata.application_data = &Temp_Buf[0];
|
||||
rpdata.application_data_len = sizeof(Temp_Buf);
|
||||
len = Read_Property_Function(&rpdata);
|
||||
}
|
||||
if (len < 0) {
|
||||
/* error was returned - encode that for the response */
|
||||
len =
|
||||
rpm_ack_encode_apdu_object_property_error(&Temp_Buf[0],
|
||||
error_class, error_code);
|
||||
rpdata.error_class, rpdata.error_code);
|
||||
copy_len =
|
||||
memcopy(&apdu[0], &Temp_Buf[0], offset + apdu_len, len, max_apdu);
|
||||
if (copy_len == 0) {
|
||||
|
||||
@@ -37,15 +37,12 @@
|
||||
#include "abort.h"
|
||||
#include "wp.h"
|
||||
|
||||
static write_property_function Write_Property[MAX_BACNET_OBJECT_TYPE];
|
||||
static write_property_function Write_Property;
|
||||
|
||||
void handler_write_property_object_set(
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
void handler_write_property_function_set(
|
||||
write_property_function pFunction)
|
||||
{
|
||||
if (object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
Write_Property[object_type] = pFunction;
|
||||
}
|
||||
Write_Property = pFunction;
|
||||
}
|
||||
|
||||
void handler_write_property(
|
||||
@@ -58,11 +55,8 @@ void handler_write_property(
|
||||
int len = 0;
|
||||
int pdu_len = 0;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
|
||||
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
int bytes_sent = 0;
|
||||
BACNET_ADDRESS my_address;
|
||||
write_property_function wp_function = NULL;
|
||||
|
||||
/* encode the NPDU portion of the packet */
|
||||
datalink_get_my_address(&my_address);
|
||||
@@ -103,11 +97,8 @@ void handler_write_property(
|
||||
#endif
|
||||
goto WP_ABORT;
|
||||
}
|
||||
if (wp_data.object_type < MAX_BACNET_OBJECT_TYPE) {
|
||||
wp_function = Write_Property[wp_data.object_type];
|
||||
}
|
||||
if (wp_function) {
|
||||
if (wp_function(&wp_data, &error_class, &error_code)) {
|
||||
if (Write_Property) {
|
||||
if (Write_Property(&wp_data) >= 0) {
|
||||
len =
|
||||
encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY);
|
||||
@@ -118,19 +109,19 @@ void handler_write_property(
|
||||
len =
|
||||
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
error_class, error_code);
|
||||
wp_data.error_class, wp_data.error_code);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "WP: Sending Error!\n");
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
len =
|
||||
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
error_class, error_code);
|
||||
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
|
||||
service_data->invoke_id, ABORT_REASON_OTHER, true);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "WP: Sending Unknown Object Error!\n");
|
||||
fprintf(stderr, "WP: No WP Function Set. Sending Abort!\n");
|
||||
#endif
|
||||
goto WP_ABORT;
|
||||
}
|
||||
WP_ABORT:
|
||||
pdu_len += len;
|
||||
|
||||
Reference in New Issue
Block a user