Refactored to make the ReadProperty functions less stack heavy. Fixed ReadPropertyAck after running unit test.

This commit is contained in:
skarg
2005-06-24 12:42:49 +00:00
parent f081bf3cf2
commit 5ceddb8c03
5 changed files with 105 additions and 139 deletions
+62 -74
View File
@@ -149,6 +149,7 @@ bool Send_Read_Property_Request(
bool status = false; bool status = false;
int pdu_len = 0; int pdu_len = 0;
int bytes_sent = 0; int bytes_sent = 0;
BACNET_READ_PROPERTY_DATA data;
status = address_get_by_device(device_id, &max_apdu, &dest); status = address_get_by_device(device_id, &max_apdu, &dest);
if (status) if (status)
@@ -162,13 +163,15 @@ bool Send_Read_Property_Request(
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
// load the data for the encoding
data.object_type = object_type;
data.object_instance = object_instance;
data.object_property = object_property;
data.array_index = array_index;
pdu_len += rp_encode_apdu( pdu_len += rp_encode_apdu(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
invoke_id, invoke_id,
object_type, &data);
object_instance,
object_property,
array_index);
if ((unsigned)pdu_len < max_apdu) if ((unsigned)pdu_len < max_apdu)
{ {
tsm_set_confirmed_unsegmented_transaction( tsm_set_confirmed_unsegmented_transaction(
@@ -252,13 +255,10 @@ void ReadPropertyHandler(
BACNET_ADDRESS *src, BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data) BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_READ_PROPERTY_DATA rp_data; BACNET_READ_PROPERTY_DATA data;
int len = 0; int len = 0;
int pdu_len = 0; int pdu_len = 0;
BACNET_OBJECT_TYPE object_type;
uint32_t object_instance; uint32_t object_instance;
BACNET_PROPERTY_ID object_property;
int32_t array_index;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
bool send = false; bool send = false;
bool error = false; bool error = false;
@@ -269,17 +269,14 @@ void ReadPropertyHandler(
len = rp_decode_service_request( len = rp_decode_service_request(
service_request, service_request,
service_len, service_len,
&object_type, &data);
&object_instance,
&object_property,
&array_index);
fprintf(stderr,"Received Read-Property Request!\n"); fprintf(stderr,"Received Read-Property Request!\n");
if (len > 0) if (len > 0)
fprintf(stderr,"type=%u instance=%u property=%u index=%d\n", fprintf(stderr,"type=%u instance=%u property=%u index=%d\n",
object_type, data.object_type,
object_instance, data.object_instance,
object_property, data.object_property,
array_index); data.array_index);
else else
fprintf(stderr,"Unable to decode Read-Property Request!\n"); fprintf(stderr,"Unable to decode Read-Property Request!\n");
// prepare a reply // prepare a reply
@@ -312,32 +309,28 @@ void ReadPropertyHandler(
} }
else else
{ {
switch (object_type) switch (data.object_type)
{ {
case OBJECT_DEVICE: case OBJECT_DEVICE:
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
if (object_instance == Device_Object_Instance_Number()) if (data.object_instance == Device_Object_Instance_Number())
{ {
len = Device_Encode_Property_APDU( len = Device_Encode_Property_APDU(
&Temp_Buf[0], &Temp_Buf[0],
object_property, data.object_property,
array_index, data.array_index,
&error_class, &error_class,
&error_code); &error_code);
if (len > 0) if (len > 0)
{ {
// encode the APDU portion of the packet // encode the APDU portion of the packet
rp_data.object_type = object_type; data.application_data = &Temp_Buf[0];
rp_data.object_instance = object_instance; data.application_data_len = len;
rp_data.object_property = object_property;
rp_data.array_index = array_index;
rp_data.application_data = &Temp_Buf[0];
rp_data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len += rp_ack_encode_apdu(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
&rp_data); &data);
fprintf(stderr,"Sending Read Property Ack!\n"); fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} }
@@ -348,29 +341,25 @@ void ReadPropertyHandler(
error = true; error = true;
break; break;
case OBJECT_ANALOG_INPUT: case OBJECT_ANALOG_INPUT:
if (Analog_Input_Valid_Instance(object_instance)) if (Analog_Input_Valid_Instance(data.object_instance))
{ {
len = Analog_Input_Encode_Property_APDU( len = Analog_Input_Encode_Property_APDU(
&Temp_Buf[0], &Temp_Buf[0],
object_instance, data.object_instance,
object_property, data.object_property,
array_index, data.array_index,
&error_class, &error_class,
&error_code); &error_code);
if (len > 0) if (len > 0)
{ {
// encode the APDU portion of the packet // encode the APDU portion of the packet
rp_data.object_type = object_type; data.application_data = &Temp_Buf[0];
rp_data.object_instance = object_instance; data.application_data_len = len;
rp_data.object_property = object_property;
rp_data.array_index = array_index;
rp_data.application_data = &Temp_Buf[0];
rp_data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len += rp_ack_encode_apdu(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
&rp_data); &data);
fprintf(stderr,"Sending Read Property Ack!\n"); fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} }
@@ -381,29 +370,25 @@ void ReadPropertyHandler(
error = true; error = true;
break; break;
case OBJECT_ANALOG_OUTPUT: case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Valid_Instance(object_instance)) if (Analog_Output_Valid_Instance(data.object_instance))
{ {
len = Analog_Output_Encode_Property_APDU( len = Analog_Output_Encode_Property_APDU(
&Temp_Buf[0], &Temp_Buf[0],
object_instance, data.object_instance,
object_property, data.object_property,
array_index, data.array_index,
&error_class, &error_class,
&error_code); &error_code);
if (len > 0) if (len > 0)
{ {
// encode the APDU portion of the packet // encode the APDU portion of the packet
rp_data.object_type = object_type; data.application_data = &Temp_Buf[0];
rp_data.object_instance = object_instance; data.application_data_len = len;
rp_data.object_property = object_property;
rp_data.array_index = array_index;
rp_data.application_data = &Temp_Buf[0];
rp_data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len += rp_ack_encode_apdu(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
&rp_data); &data);
fprintf(stderr,"Sending Read Property Ack!\n"); fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} }
@@ -418,25 +403,21 @@ void ReadPropertyHandler(
{ {
len = bacfile_encode_property_apdu( len = bacfile_encode_property_apdu(
&Temp_Buf[0], &Temp_Buf[0],
object_instance, data.object_instance,
object_property, data.object_property,
array_index, data.array_index,
&error_class, &error_class,
&error_code); &error_code);
if (len > 0) if (len > 0)
{ {
// encode the APDU portion of the packet // encode the APDU portion of the packet
rp_data.object_type = object_type; data.application_data = &Temp_Buf[0];
rp_data.object_instance = object_instance; data.application_data_len = len;
rp_data.object_property = object_property;
rp_data.array_index = array_index;
rp_data.application_data = &Temp_Buf[0];
rp_data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len += rp_ack_encode_apdu(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
&rp_data); &data);
fprintf(stderr,"Sending Read Property Ack!\n"); fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} }
@@ -615,6 +596,7 @@ void AtomicReadFileHandler(
int len = 0; int len = 0;
int pdu_len = 0; int pdu_len = 0;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
bool send = false;
bool error = false; bool error = false;
int bytes_sent = 0; int bytes_sent = 0;
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
@@ -645,6 +627,7 @@ void AtomicReadFileHandler(
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_OTHER); ABORT_REASON_OTHER);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr,"Sending Abort!\n");
send = true;
} }
else if (service_data->segmented_message) else if (service_data->segmented_message)
{ {
@@ -653,6 +636,7 @@ void AtomicReadFileHandler(
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr,"Sending Abort!\n");
send = true;
} }
else else
{ {
@@ -660,9 +644,6 @@ void AtomicReadFileHandler(
{ {
data.fileData = (uint8_t *)&buffer[0]; data.fileData = (uint8_t *)&buffer[0];
data.fileDataLength = sizeof(buffer); data.fileDataLength = sizeof(buffer);
// FIXME: this may not be a valid way to check this, as the
// file length may be smaller than the request
// or smaller than the size of the buffer.
if (data.type.stream.requestedOctetCount < data.fileDataLength) if (data.type.stream.requestedOctetCount < data.fileDataLength)
{ {
if (bacfile_read_data(&data)) if (bacfile_read_data(&data))
@@ -671,9 +652,13 @@ void AtomicReadFileHandler(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
&data); &data);
send = true;
} }
else else
{
send = true;
error = true; error = true;
}
} }
else else
{ {
@@ -681,17 +666,15 @@ void AtomicReadFileHandler(
&Tx_Buf[pdu_len], &Tx_Buf[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, fprintf(stderr,"Sending Abort!\n");
"Sending Abort! %u octets requested." send = true;
" Only able to send %u octets.\n",
data.type.stream.requestedOctetCount,
data.fileDataLength);
} }
} }
else else
{ {
error_class = ERROR_CLASS_SERVICES; error_class = ERROR_CLASS_SERVICES;
error_code = ERROR_CODE_INVALID_FILE_ACCESS_METHOD; error_code = ERROR_CODE_INVALID_FILE_ACCESS_METHOD;
send = true;
error = true; error = true;
} }
} }
@@ -704,13 +687,17 @@ void AtomicReadFileHandler(
error_class, error_class,
error_code); error_code);
fprintf(stderr,"Sending Error!\n"); fprintf(stderr,"Sending Error!\n");
send = true;
}
if (send)
{
bytes_sent = datalink_send_pdu(
src, // destination address
&Tx_Buf[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno));
} }
bytes_sent = datalink_send_pdu(
src, // destination address
&Tx_Buf[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno));
return; return;
} }
@@ -718,7 +705,8 @@ void AtomicReadFileHandler(
// We performed an AtomicReadFile Request, // We performed an AtomicReadFile Request,
// and here is the data from the server // and here is the data from the server
// Note: it does not have to be the same file=instance // Note: it does not have to be the same file=instance
// that someone can read from us. // that someone can read from us. It is common to
// use the description as the file name.
void AtomicReadFileAckHandler( void AtomicReadFileAckHandler(
uint8_t *service_request, uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
+30 -52
View File
@@ -35,17 +35,13 @@
#include "bacenum.h" #include "bacenum.h"
#include "bacdcode.h" #include "bacdcode.h"
#include "bacdef.h" #include "bacdef.h"
#include "device.h"
#include "rp.h" #include "rp.h"
// encode service // encode service
int rp_encode_apdu( int rp_encode_apdu(
uint8_t *apdu, uint8_t *apdu,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_OBJECT_TYPE object_type, BACNET_READ_PROPERTY_DATA *data)
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
int32_t array_index)
{ {
int len = 0; // length of each encoding int len = 0; // length of each encoding
int apdu_len = 0; // total length of the apdu, return value int apdu_len = 0; // total length of the apdu, return value
@@ -53,21 +49,21 @@ int rp_encode_apdu(
if (apdu) if (apdu)
{ {
apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
apdu[1] = encode_max_segs_max_apdu(0, Device_Max_APDU_Length_Accepted()); apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU);
apdu[2] = invoke_id; apdu[2] = invoke_id;
apdu[3] = SERVICE_CONFIRMED_READ_PROPERTY; // service choice apdu[3] = SERVICE_CONFIRMED_READ_PROPERTY; // service choice
apdu_len = 4; apdu_len = 4;
len = encode_context_object_id(&apdu[apdu_len], 0, len = encode_context_object_id(&apdu[apdu_len], 0,
object_type, object_instance); data->object_type, data->object_instance);
apdu_len += len; apdu_len += len;
len = encode_context_enumerated(&apdu[apdu_len], 1, len = encode_context_enumerated(&apdu[apdu_len], 1,
object_property); data->object_property);
apdu_len += len; apdu_len += len;
/* optional array index */ /* optional array index */
if (array_index != BACNET_ARRAY_ALL) if (data->array_index != BACNET_ARRAY_ALL)
{ {
len = encode_context_unsigned(&apdu[apdu_len], 2, len = encode_context_unsigned(&apdu[apdu_len], 2,
array_index); data->array_index);
apdu_len += len; apdu_len += len;
} }
} }
@@ -79,10 +75,7 @@ int rp_encode_apdu(
int rp_decode_service_request( int rp_decode_service_request(
uint8_t *apdu, uint8_t *apdu,
unsigned apdu_len, unsigned apdu_len,
BACNET_OBJECT_TYPE *object_type, BACNET_READ_PROPERTY_DATA *data)
uint32_t *object_instance,
BACNET_PROPERTY_ID *object_property,
int32_t *array_index)
{ {
unsigned len = 0; unsigned len = 0;
uint8_t tag_number = 0; uint8_t tag_number = 0;
@@ -92,21 +85,20 @@ int rp_decode_service_request(
unsigned array_value = 0; // for decoding unsigned array_value = 0; // for decoding
// check for value pointers // check for value pointers
if (apdu_len && object_type && object_instance && if (apdu_len && data)
object_property && array_index)
{ {
// 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; return -1;
len += decode_object_id(&apdu[len], &type, object_instance); len += decode_object_id(&apdu[len], &type, &data->object_instance);
*object_type = type; data->object_type = type;
// Tag 1: Property ID // Tag 1: Property ID
len += decode_tag_number_and_value(&apdu[len], len += decode_tag_number_and_value(&apdu[len],
&tag_number, &len_value_type); &tag_number, &len_value_type);
if (tag_number != 1) if (tag_number != 1)
return -1; return -1;
len += decode_enumerated(&apdu[len], len_value_type, &property); len += decode_enumerated(&apdu[len], len_value_type, &property);
*object_property = property; data->object_property = property;
// Tag 2: Optional Array Index // Tag 2: Optional Array Index
if (len < apdu_len) if (len < apdu_len)
{ {
@@ -116,13 +108,13 @@ int rp_decode_service_request(
{ {
len += decode_unsigned(&apdu[len], len_value_type, len += decode_unsigned(&apdu[len], len_value_type,
&array_value); &array_value);
*array_index = array_value; data->array_index = array_value;
} }
else else
*array_index = BACNET_ARRAY_ALL; data->array_index = BACNET_ARRAY_ALL;
} }
else else
*array_index = BACNET_ARRAY_ALL; data->array_index = BACNET_ARRAY_ALL;
} }
return (int)len; return (int)len;
@@ -132,10 +124,7 @@ int rp_decode_apdu(
uint8_t *apdu, uint8_t *apdu,
unsigned apdu_len, unsigned apdu_len,
uint8_t *invoke_id, uint8_t *invoke_id,
BACNET_OBJECT_TYPE *object_type, BACNET_READ_PROPERTY_DATA *data)
uint32_t *object_instance,
BACNET_PROPERTY_ID *object_property,
int32_t *array_index)
{ {
int len = 0; int len = 0;
unsigned offset = 0; unsigned offset = 0;
@@ -156,10 +145,7 @@ int rp_decode_apdu(
len = rp_decode_service_request( len = rp_decode_service_request(
&apdu[offset], &apdu[offset],
apdu_len - offset, apdu_len - offset,
object_type, data);
object_instance,
object_property,
array_index);
} }
return len; return len;
@@ -251,7 +237,7 @@ int rp_ack_decode_service_request(
len++; len++;
// don't decode the application tag number or its data here // don't decode the application tag number or its data here
data->application_data = &apdu[len]; data->application_data = &apdu[len];
data->application_data_len = apdu_len - len; data->application_data_len = apdu_len - len - 1 /*closing tag*/;
} }
else else
return -1; return -1;
@@ -354,22 +340,17 @@ void testReadProperty(Test * pTest)
int apdu_len = 0; int apdu_len = 0;
uint8_t invoke_id = 128; uint8_t invoke_id = 128;
uint8_t test_invoke_id = 0; uint8_t test_invoke_id = 0;
BACNET_OBJECT_TYPE object_type = OBJECT_CALENDAR; BACNET_READ_PROPERTY_DATA data;
BACNET_OBJECT_TYPE test_object_type = 0; BACNET_READ_PROPERTY_DATA test_data;
uint32_t object_instance = 1;
uint32_t test_object_instance = 0; data.object_type = OBJECT_DEVICE;
BACNET_PROPERTY_ID object_property = PROP_ARCHIVE; data.object_instance = 1;
BACNET_PROPERTY_ID test_object_property = 0; data.object_property = PROP_OBJECT_IDENTIFIER;
int32_t array_index = 2; data.array_index = BACNET_ARRAY_ALL;
int32_t test_array_index = 0;
len = rp_encode_apdu( len = rp_encode_apdu(
&apdu[0], &apdu[0],
invoke_id, invoke_id,
object_type, &data);
object_instance,
object_property,
array_index);
ct_test(pTest, len != 0); ct_test(pTest, len != 0);
apdu_len = len; apdu_len = len;
@@ -377,15 +358,12 @@ void testReadProperty(Test * pTest)
&apdu[0], &apdu[0],
apdu_len, apdu_len,
&test_invoke_id, &test_invoke_id,
&test_object_type, &test_data);
&test_object_instance,
&test_object_property,
&test_array_index);
ct_test(pTest, len != -1); ct_test(pTest, len != -1);
ct_test(pTest, test_object_type == object_type); ct_test(pTest, test_data.object_type == data.object_type);
ct_test(pTest, test_object_instance == object_instance); ct_test(pTest, test_data.object_instance == data.object_instance);
ct_test(pTest, test_object_property == object_property); ct_test(pTest, test_data.object_property == data.object_property);
ct_test(pTest, test_array_index == array_index); ct_test(pTest, test_data.array_index == data.array_index);
return; return;
} }
+3 -12
View File
@@ -51,28 +51,19 @@ typedef struct BACnet_Read_Property_Data
int rp_encode_apdu( int rp_encode_apdu(
uint8_t *apdu, uint8_t *apdu,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_OBJECT_TYPE object_type, BACNET_READ_PROPERTY_DATA *data);
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
int32_t array_index);
// decode the service request only // decode the service request only
int rp_decode_service_request( int rp_decode_service_request(
uint8_t *apdu, uint8_t *apdu,
unsigned apdu_len, unsigned apdu_len,
BACNET_OBJECT_TYPE *object_type, BACNET_READ_PROPERTY_DATA *data);
uint32_t *object_instance,
BACNET_PROPERTY_ID *object_property,
int32_t *array_index);
int rp_decode_apdu( int rp_decode_apdu(
uint8_t *apdu, uint8_t *apdu,
unsigned apdu_len, unsigned apdu_len,
uint8_t *invoke_id, uint8_t *invoke_id,
BACNET_OBJECT_TYPE *object_type, BACNET_READ_PROPERTY_DATA *data);
uint32_t *object_instance,
BACNET_PROPERTY_ID *object_property,
int32_t *array_index);
int rp_ack_encode_apdu( int rp_ack_encode_apdu(
uint8_t *apdu, uint8_t *apdu,
-1
View File
@@ -8,7 +8,6 @@ CFLAGS = -Wall -I. -Itest -DTEST -DTEST_READ_PROPERTY -g
SRCS = bacdcode.c \ SRCS = bacdcode.c \
bigend.c \ bigend.c \
device.c \
rp.c \ rp.c \
test/ctest.c test/ctest.c
+10
View File
@@ -19,6 +19,16 @@ make -f mstp.mak
./mstp >> test.log ./mstp >> test.log
make -f mstp.mak clean make -f mstp.mak clean
make -f iam.mak clean
make -f iam.mak
./iam >> test.log
make -f iam.mak clean
make -f whois.mak clean
make -f whois.mak
./whois >> test.log
make -f whois.mak clean
make -f bacdcode.mak clean make -f bacdcode.mak clean
make -f bacdcode.mak make -f bacdcode.mak
./bacdcode >> test.log ./bacdcode >> test.log