converted COV to use new error, reject, abort method.

This commit is contained in:
skarg
2010-09-11 19:24:20 +00:00
parent 914a063681
commit 9e13bd0ebb
3 changed files with 88 additions and 41 deletions
+48 -22
View File
@@ -35,6 +35,7 @@
#include "apdu.h" #include "apdu.h"
#include "npdu.h" #include "npdu.h"
#include "abort.h" #include "abort.h"
#include "reject.h"
#include "cov.h" #include "cov.h"
#include "tsm.h" #include "tsm.h"
/* demo objects */ /* demo objects */
@@ -507,28 +508,29 @@ void handler_cov_subscribe(
BACNET_SUBSCRIBE_COV_DATA cov_data; BACNET_SUBSCRIBE_COV_DATA cov_data;
int len = 0; int len = 0;
int pdu_len = 0; int pdu_len = 0;
int npdu_len = 0;
int apdu_len = 0;
BACNET_NPDU_DATA npdu_data; BACNET_NPDU_DATA npdu_data;
bool success = false; bool success = false;
int bytes_sent = 0; 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; BACNET_ADDRESS my_address;
bool error = false;
/* initialize a common abort code */
cov_data.error_code = ABORT_REASON_SEGMENTATION_NOT_SUPPORTED;
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); 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_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address,
&npdu_data); &npdu_data);
if (service_data->segmented_message) { if (service_data->segmented_message) {
/* we don't support segmentation - send an abort */ /* we don't support segmentation - send an abort */
len = len = BACNET_STATUS_ABORT;
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
true);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Segmented message. Sending Abort!\n"); fprintf(stderr, "SubscribeCOV: Segmented message. Sending Abort!\n");
#endif #endif
error = true;
goto COV_ABORT; goto COV_ABORT;
} }
len = len =
@@ -539,34 +541,56 @@ void handler_cov_subscribe(
fprintf(stderr, "SubscribeCOV: Unable to decode Request!\n"); fprintf(stderr, "SubscribeCOV: Unable to decode Request!\n");
#endif #endif
if (len < 0) { if (len < 0) {
/* bad decoding - send an abort */ error = true;
len =
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER, true);
#if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Bad decoding. Sending Abort!\n");
#endif
goto COV_ABORT; goto COV_ABORT;
} }
success = cov_subscribe(src, &cov_data, &error_class, &error_code); cov_data.error_class = ERROR_CLASS_OBJECT;
cov_data.error_code = ERROR_CODE_UNKNOWN_OBJECT;
success = cov_subscribe(src, &cov_data,
&cov_data.error_class, &cov_data.error_code);
if (success) { if (success) {
len = apdu_len =
encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], encode_simple_ack(&Handler_Transmit_Buffer[npdu_len],
service_data->invoke_id, SERVICE_CONFIRMED_SUBSCRIBE_COV); service_data->invoke_id, SERVICE_CONFIRMED_SUBSCRIBE_COV);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Sending Simple Ack!\n"); fprintf(stderr, "SubscribeCOV: Sending Simple Ack!\n");
#endif #endif
} else { } else {
len = len = BACNET_STATUS_ERROR;
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], error = true;
service_data->invoke_id, SERVICE_CONFIRMED_SUBSCRIBE_COV,
error_class, error_code);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Sending Error!\n"); fprintf(stderr, "SubscribeCOV: Sending Error!\n");
#endif #endif
} }
COV_ABORT: COV_ABORT:
pdu_len += len; if (error) {
if (len == BACNET_STATUS_ABORT) {
apdu_len =
abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
service_data->invoke_id,
abort_convert_error_code(cov_data.error_code), true);
#if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Sending Abort!\n");
#endif
} else if (len == BACNET_STATUS_ERROR) {
apdu_len =
bacerror_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
service_data->invoke_id, SERVICE_CONFIRMED_SUBSCRIBE_COV,
cov_data.error_class, cov_data.error_code);
#if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Sending Error!\n");
#endif
} else if (len == BACNET_STATUS_REJECT) {
apdu_len =
reject_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
service_data->invoke_id,
reject_convert_error_code(cov_data.error_code));
#if PRINT_ENABLED
fprintf(stderr, "SubscribeCOV: Sending Reject!\n");
#endif
}
}
pdu_len = npdu_len + apdu_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],
pdu_len); pdu_len);
@@ -574,6 +598,8 @@ void handler_cov_subscribe(
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr, "SubscribeCOV: Failed to send PDU (%s)!\n", fprintf(stderr, "SubscribeCOV: Failed to send PDU (%s)!\n",
strerror(errno)); strerror(errno));
#else
bytes_sent = bytes_sent;
#endif #endif
return; return;
+2
View File
@@ -56,6 +56,8 @@ typedef struct BACnet_Subscribe_COV_Data {
BACNET_PROPERTY_REFERENCE monitoredProperty; BACNET_PROPERTY_REFERENCE monitoredProperty;
bool covIncrementPresent; /* true if present */ bool covIncrementPresent; /* true if present */
float covIncrement; /* optional */ float covIncrement; /* optional */
BACNET_ERROR_CLASS error_class;
BACNET_ERROR_CODE error_code;
} BACNET_SUBSCRIBE_COV_DATA; } BACNET_SUBSCRIBE_COV_DATA;
#ifdef __cplusplus #ifdef __cplusplus
+38 -19
View File
@@ -381,8 +381,10 @@ int cov_subscribe_decode_service_request(
&len_value); &len_value);
len += decode_unsigned(&apdu[len], len_value, &decoded_value); len += decode_unsigned(&apdu[len], len_value, &decoded_value);
data->subscriberProcessIdentifier = decoded_value; data->subscriberProcessIdentifier = decoded_value;
} else } else {
return -1; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* tag 1 - monitoredObjectIdentifier */ /* tag 1 - monitoredObjectIdentifier */
if (decode_is_context_tag(&apdu[len], 1)) { if (decode_is_context_tag(&apdu[len], 1)) {
len += len +=
@@ -392,8 +394,10 @@ int cov_subscribe_decode_service_request(
decode_object_id(&apdu[len], &decoded_type, decode_object_id(&apdu[len], &decoded_type,
&data->monitoredObjectIdentifier.instance); &data->monitoredObjectIdentifier.instance);
data->monitoredObjectIdentifier.type = decoded_type; data->monitoredObjectIdentifier.type = decoded_type;
} else } else {
return -1; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* optional parameters - if missing, means cancellation */ /* optional parameters - if missing, means cancellation */
if ((unsigned) len < apdu_len) { if ((unsigned) len < apdu_len) {
/* tag 2 - issueConfirmedNotifications - optional */ /* tag 2 - issueConfirmedNotifications - optional */
@@ -415,8 +419,9 @@ int cov_subscribe_decode_service_request(
&len_value); &len_value);
len += decode_unsigned(&apdu[len], len_value, &decoded_value); len += decode_unsigned(&apdu[len], len_value, &decoded_value);
data->lifetime = decoded_value; data->lifetime = decoded_value;
} else } else {
data->lifetime = 0; data->lifetime = 0;
}
} else { } else {
data->cancellationRequest = true; data->cancellationRequest = true;
} }
@@ -528,8 +533,10 @@ int cov_subscribe_property_decode_service_request(
&len_value); &len_value);
len += decode_unsigned(&apdu[len], len_value, &decoded_value); len += decode_unsigned(&apdu[len], len_value, &decoded_value);
data->subscriberProcessIdentifier = decoded_value; data->subscriberProcessIdentifier = decoded_value;
} else } else {
return -1; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* tag 1 - monitoredObjectIdentifier */ /* tag 1 - monitoredObjectIdentifier */
if (decode_is_context_tag(&apdu[len], 1)) { if (decode_is_context_tag(&apdu[len], 1)) {
len += len +=
@@ -539,8 +546,10 @@ int cov_subscribe_property_decode_service_request(
decode_object_id(&apdu[len], &decoded_type, decode_object_id(&apdu[len], &decoded_type,
&data->monitoredObjectIdentifier.instance); &data->monitoredObjectIdentifier.instance);
data->monitoredObjectIdentifier.type = decoded_type; data->monitoredObjectIdentifier.type = decoded_type;
} else } else {
return -2; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* tag 2 - issueConfirmedNotifications - optional */ /* tag 2 - issueConfirmedNotifications - optional */
if (decode_is_context_tag(&apdu[len], 2)) { if (decode_is_context_tag(&apdu[len], 2)) {
data->cancellationRequest = false; data->cancellationRequest = false;
@@ -550,8 +559,9 @@ int cov_subscribe_property_decode_service_request(
data->issueConfirmedNotifications = data->issueConfirmedNotifications =
decode_context_boolean(&apdu[len]); decode_context_boolean(&apdu[len]);
len++; len++;
} else } else {
data->cancellationRequest = true; data->cancellationRequest = true;
}
/* tag 3 - lifetime - optional */ /* tag 3 - lifetime - optional */
if (decode_is_context_tag(&apdu[len], 3)) { if (decode_is_context_tag(&apdu[len], 3)) {
len += len +=
@@ -559,11 +569,14 @@ int cov_subscribe_property_decode_service_request(
&len_value); &len_value);
len += decode_unsigned(&apdu[len], len_value, &decoded_value); len += decode_unsigned(&apdu[len], len_value, &decoded_value);
data->lifetime = decoded_value; data->lifetime = decoded_value;
} else } else {
data->lifetime = 0; data->lifetime = 0;
}
/* tag 4 - monitoredPropertyIdentifier */ /* tag 4 - monitoredPropertyIdentifier */
if (!decode_is_opening_tag_number(&apdu[len], 4)) if (!decode_is_opening_tag_number(&apdu[len], 4)) {
return -3; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* a tag number of 4 is not extended so only one octet */ /* a tag number of 4 is not extended so only one octet */
len++; len++;
/* the propertyIdentifier is tag 0 */ /* the propertyIdentifier is tag 0 */
@@ -574,8 +587,10 @@ int cov_subscribe_property_decode_service_request(
len += decode_enumerated(&apdu[len], len_value, &property); len += decode_enumerated(&apdu[len], len_value, &property);
data->monitoredProperty.propertyIdentifier = data->monitoredProperty.propertyIdentifier =
(BACNET_PROPERTY_ID) property; (BACNET_PROPERTY_ID) property;
} else } else {
return -4; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* the optional array index is tag 1 */ /* the optional array index is tag 1 */
if (decode_is_context_tag(&apdu[len], 1)) { if (decode_is_context_tag(&apdu[len], 1)) {
len += len +=
@@ -587,8 +602,10 @@ int cov_subscribe_property_decode_service_request(
data->monitoredProperty.propertyArrayIndex = BACNET_ARRAY_ALL; data->monitoredProperty.propertyArrayIndex = BACNET_ARRAY_ALL;
} }
if (!decode_is_closing_tag_number(&apdu[len], 4)) if (!decode_is_closing_tag_number(&apdu[len], 4)) {
return -5; data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
return BACNET_STATUS_REJECT;
}
/* a tag number of 4 is not extended so only one octet */ /* a tag number of 4 is not extended so only one octet */
len++; len++;
/* tag 5 - covIncrement - optional */ /* tag 5 - covIncrement - optional */
@@ -598,8 +615,9 @@ int cov_subscribe_property_decode_service_request(
decode_tag_number_and_value(&apdu[len], &tag_number, decode_tag_number_and_value(&apdu[len], &tag_number,
&len_value); &len_value);
len += decode_real(&apdu[len], &data->covIncrement); len += decode_real(&apdu[len], &data->covIncrement);
} else } else {
data->covIncrementPresent = false; data->covIncrementPresent = false;
}
} }
return len; return len;
@@ -620,8 +638,9 @@ int ccov_notify_decode_apdu(
int len = 0; int len = 0;
unsigned offset = 0; unsigned offset = 0;
if (!apdu) if (!apdu) {
return -1; return -1;
}
/* optional checking - most likely was already done prior to this call */ /* optional checking - most likely was already done prior to this call */
if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST)
return -2; return -2;