Replaced gotos in handler code (#109)

This commit is contained in:
Roy Schneider
2020-08-13 17:18:50 +02:00
committed by GitHub
parent f41b5377cc
commit 993acb494a
2 changed files with 137 additions and 112 deletions
+21 -16
View File
@@ -86,28 +86,32 @@ void handler_read_property(uint8_t *service_request,
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
npdu_len = npdu_encode_pdu( npdu_len = npdu_encode_pdu(
&Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); &Handler_Transmit_Buffer[0], src, &my_address, &npdu_data);
if (service_data->segmented_message) { if (npdu_len <= 0) {
/* If 0 or negative, there were problems with the data or encoding. */
len = BACNET_STATUS_ABORT;
#if PRINT_ENABLED
fprintf(stderr, "RP: npdu_encode_pdu error. Sending Abort!\n");
#endif
} else if (service_data->segmented_message) {
/* we don't support segmentation - send an abort */ /* we don't support segmentation - send an abort */
len = BACNET_STATUS_ABORT; len = BACNET_STATUS_ABORT;
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RP: Segmented message. Sending Abort!\n"); fprintf(stderr, "RP: Segmented message. Sending Abort!\n");
#endif #endif
goto RP_FAILURE; } else {
}
len = rp_decode_service_request(service_request, service_len, &rpdata); len = rp_decode_service_request(service_request, service_len, &rpdata);
#if PRINT_ENABLED #if PRINT_ENABLED
if (len <= 0) { if (len <= 0) {
fprintf(stderr, "RP: Unable to decode Request!\n"); fprintf(stderr, "RP: Unable to decode Request!\n");
} }
#endif #endif
if (len < 0) { if (len < 0) {
/* bad decoding - skip to error/reject/abort handling */ /* bad decoding - skip to error/reject/abort handling */
error = true; error = true;
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RP: Bad Encoding.\n"); fprintf(stderr, "RP: Bad Encoding.\n");
#endif #endif
goto RP_FAILURE; } else {
}
/* Test for case of indefinite Device object instance */ /* Test for case of indefinite Device object instance */
if ((rpdata.object_type == OBJECT_DEVICE) && if ((rpdata.object_type == OBJECT_DEVICE) &&
(rpdata.object_instance == BACNET_MAX_INSTANCE)) { (rpdata.object_instance == BACNET_MAX_INSTANCE)) {
@@ -132,17 +136,17 @@ void handler_read_property(uint8_t *service_request,
* have overriden the default set at start */ * have overriden the default set at start */
rpdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; rpdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
len = BACNET_STATUS_ABORT; len = BACNET_STATUS_ABORT;
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RP: Message too large.\n"); fprintf(stderr, "RP: Message too large.\n");
#endif #endif
} else { } else {
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RP: Sending Ack!\n"); fprintf(stderr, "RP: Sending Ack!\n");
#endif #endif
error = false; error = false;
} }
} else { } else {
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RP: Device_Read_Property: "); fprintf(stderr, "RP: Device_Read_Property: ");
if (len == BACNET_STATUS_ABORT) { if (len == BACNET_STATUS_ABORT) {
fprintf(stderr, "Abort!\n"); fprintf(stderr, "Abort!\n");
@@ -153,10 +157,11 @@ void handler_read_property(uint8_t *service_request,
} else { } else {
fprintf(stderr, "Unknown Len=%d\n", len); fprintf(stderr, "Unknown Len=%d\n", len);
} }
#endif #endif
}
}
} }
RP_FAILURE:
if (error) { if (error) {
if (len == BACNET_STATUS_ABORT) { if (len == BACNET_STATUS_ABORT) {
apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len],
+38 -18
View File
@@ -45,8 +45,20 @@
static uint8_t Temp_Buf[MAX_APDU] = { 0 }; static uint8_t Temp_Buf[MAX_APDU] = { 0 };
/* Encodes the property APDU and returns the length, /**
or sets the error, and returns -1 */ * Encodes the property APDU and returns the length,
* or sets the error, and returns -1.
*
* @param apdu Pointer to the APDU buffer.
* @param pRequest Pointer to the request to encode.
*
* @return Bytes encoded or -1 on an error. The error code
* may also be a negative return value from the called
* handler of 'PropInfo.Handler' type from within
* this function. The error code might be -2, if an
* abort message has been encoded, because the APDU
* was too small to fit the data.
*/
static int Encode_RR_payload(uint8_t *apdu, BACNET_READ_RANGE_DATA *pRequest) static int Encode_RR_payload(uint8_t *apdu, BACNET_READ_RANGE_DATA *pRequest)
{ {
int apdu_len = -1; int apdu_len = -1;
@@ -99,6 +111,15 @@ static int Encode_RR_payload(uint8_t *apdu, BACNET_READ_RANGE_DATA *pRequest)
return apdu_len; return apdu_len;
} }
/**
* Handle the received ReadRange request and encode a response.
*
* @param service_request Pointer to the service request.
* @param service_len Bytes valid in the service request.
* @param src Pointer to the BACnet addresss.
* @param service_data Pointer to the service data,
* taken from the request.
*/
void handler_read_range(uint8_t *service_request, void handler_read_range(uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS *src,
@@ -129,24 +150,21 @@ void handler_read_range(uint8_t *service_request,
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RR: Segmented message. Sending Abort!\n"); fprintf(stderr, "RR: Segmented message. Sending Abort!\n");
#endif #endif
goto RR_ABORT; } else {
}
memset(&data, 0, sizeof(data)); /* start with blank canvas */ memset(&data, 0, sizeof(data)); /* start with blank canvas */
len = rr_decode_service_request(service_request, service_len, &data); len = rr_decode_service_request(service_request, service_len, &data);
#if PRINT_ENABLED #if PRINT_ENABLED
if (len <= 0) if (len <= 0)
fprintf(stderr, "RR: Unable to decode Request!\n"); fprintf(stderr, "RR: Unable to decode Request!\n");
#endif #endif
if (len < 0) { if (len < 0) {
/* bad decoding - send an abort */ /* bad decoding - send an abort */
len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER, true); service_data->invoke_id, ABORT_REASON_OTHER, true);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RR: Bad Encoding. Sending Abort!\n"); fprintf(stderr, "RR: Bad Encoding. Sending Abort!\n");
#endif #endif
goto RR_ABORT; } else {
}
/* assume that there is an error */ /* assume that there is an error */
error = true; error = true;
len = Encode_RR_payload(&Temp_Buf[0], &data); len = Encode_RR_payload(&Temp_Buf[0], &data);
@@ -157,9 +175,9 @@ void handler_read_range(uint8_t *service_request,
/* FIXME: probably need a length limitation sent with encode */ /* FIXME: probably need a length limitation sent with encode */
len = rr_ack_encode_apdu( len = rr_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); &Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RR: Sending Ack!\n"); fprintf(stderr, "RR: Sending Ack!\n");
#endif #endif
error = false; error = false;
} }
if (error) { if (error) {
@@ -168,19 +186,21 @@ void handler_read_range(uint8_t *service_request,
len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RR: Reply too big to fit into APDU!\n"); fprintf(stderr, "RR: Reply too big to fit into APDU!\n");
#endif #endif
} else { } else {
len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, SERVICE_CONFIRMED_READ_RANGE, service_data->invoke_id, SERVICE_CONFIRMED_READ_RANGE,
data.error_class, data.error_code); data.error_class, data.error_code);
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "RR: Sending Error!\n"); fprintf(stderr, "RR: Sending Error!\n");
#endif #endif
} }
} }
RR_ABORT: }
}
pdu_len += len; pdu_len += len;
#if PRINT_ENABLED #if PRINT_ENABLED
bytes_sent = bytes_sent =