diff --git a/bacnet-stack/include/ptransfer.h b/bacnet-stack/include/ptransfer.h new file mode 100644 index 00000000..21b7a7b3 --- /dev/null +++ b/bacnet-stack/include/ptransfer.h @@ -0,0 +1,93 @@ +/*####COPYRIGHTBEGIN#### + ------------------------------------------- + Copyright (C) 2009 Steve Karg + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + The Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA. + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile + this file and link it with other works to produce a work based + on this file, this file does not by itself cause the resulting + work to be covered by the GNU General Public License. However + the source code for this file must still be made available in + accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + ------------------------------------------- +####COPYRIGHTEND####*/ +#ifndef PRIVATE_TRANSFER_H +#define PRIVATE_TRANSFER_H + +#include +#include + +typedef struct BACnet_Private_Transfer_Data { + uint16_t vendorID; + uint32_t serviceNumber; + uint8_t *serviceParameters; + int serviceParametersLen; +} BACNET_PRIVATE_TRANSFER_DATA; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + int ptransfer_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_PRIVATE_TRANSFER_DATA * private_data); + int ptransfer_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_PRIVATE_TRANSFER_DATA * private_data); + + int ptransfer_error_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code, + BACNET_PRIVATE_TRANSFER_DATA *private_data); + int ptransfer_error_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code, + BACNET_PRIVATE_TRANSFER_DATA * private_data); + + int ptransfer_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_PRIVATE_TRANSFER_DATA *private_data); +/* ptransfer_ack_decode_service_request() is the same as + ptransfer_decode_service_request */ + +#ifdef TEST +#include "ctest.h" + void test_Private_Transfer_Request( + Test * pTest); + void test_Private_Transfer_Ack( + Test * pTest); + void test_Private_Transfer_Error( + Test * pTest); +#endif + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif diff --git a/bacnet-stack/src/ptransfer.c b/bacnet-stack/src/ptransfer.c new file mode 100644 index 00000000..72ed01be --- /dev/null +++ b/bacnet-stack/src/ptransfer.c @@ -0,0 +1,541 @@ +/*####COPYRIGHTBEGIN#### + ------------------------------------------- + Copyright (C) 2009 Steve Karg + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + The Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA. + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile + this file and link it with other works to produce a work based + on this file, this file does not by itself cause the resulting + work to be covered by the GNU General Public License. However + the source code for this file must still be made available in + accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + ------------------------------------------- +####COPYRIGHTEND####*/ +#include +#include "bacenum.h" +#include "bacdcode.h" +#include "bacdef.h" +#include "ptransfer.h" + +/* encode service */ +int ptransfer_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_PRIVATE_TRANSFER_DATA * private_data) +{ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ + + if (apdu) { + apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; + apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU); + apdu[2] = invoke_id; + apdu[3] = SERVICE_CONFIRMED_PRIVATE_TRANSFER; + apdu_len = 4; +/* + ConfirmedPrivateTransfer-Request ::= SEQUENCE { + vendorID [0] Unsigned, + serviceNumber [1] Unsigned, + serviceParameters [2] ABSTRACT-SYNTAX.&Type OPTIONAL + } +*/ + len = encode_context_unsigned(&apdu[apdu_len], 0, + private_data->vendorID); + apdu_len += len; + len = encode_context_unsigned(&apdu[apdu_len], 1, + private_data->serviceNumber); + apdu_len += len; + len = encode_opening_tag(&apdu[apdu_len], 2); + apdu_len += len; + for (len = 0; len < private_data->serviceParametersLen; len++) { + apdu[apdu_len] = private_data->serviceParameters[len]; + apdu_len++; + } + len = encode_closing_tag(&apdu[apdu_len], 2); + apdu_len += len; + } + + return apdu_len; +} + +/* decode the service request only */ +int ptransfer_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_PRIVATE_TRANSFER_DATA * private_data) +{ + unsigned len = 0; /* return value */ + unsigned decode_len = 0; /* return value */ + uint32_t unsigned_value = 0; + + /* check for value pointers */ + if (apdu_len && private_data) { + /* Tag 0: vendorID */ + decode_len = decode_context_unsigned(&apdu[len], 0, &unsigned_value); + if (decode_len < 0) { + return -1; + } + len = decode_len; + private_data->vendorID = unsigned_value; + /* Tag 1: serviceNumber */ + decode_len = decode_context_unsigned(&apdu[len], 1, &unsigned_value); + if (decode_len < 0) { + return -1; + } + len += decode_len; + private_data->serviceNumber = unsigned_value; + /* Tag 2: serviceParameters */ + if (decode_is_opening_tag_number(&apdu[len], 2)) { + /* a tag number of 2 is not extended so only one octet */ + len++; + /* don't decode the serviceParameters here */ + private_data->serviceParameters = &apdu[len]; + private_data->serviceParametersLen = + apdu_len - len - 1 /*closing tag */ ; + /* len includes the data and the closing tag */ + len = apdu_len; + } else { + return -1; + } + } + + return (int) len; +} + +int ptransfer_error_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code, + BACNET_PRIVATE_TRANSFER_DATA *private_data) +{ + int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of the part of the encoding */ + + if (apdu) { + apdu[0] = PDU_TYPE_ERROR; + apdu[1] = invoke_id; + apdu[2] = SERVICE_CONFIRMED_PRIVATE_TRANSFER; + apdu_len = 3; + /* service parameters */ +/* + ConfirmedPrivateTransfer-Error ::= SEQUENCE { + errorType [0] Error, + vendorID [1] Unsigned, + serviceNumber [2] Unsigned, + errorParameters [3] ABSTRACT-SYNTAX.&Type OPTIONAL + } +*/ + len = encode_opening_tag(&apdu[apdu_len], 0); + apdu_len += len; + len = encode_application_enumerated(&apdu[apdu_len], error_class); + apdu_len += len; + len = encode_application_enumerated(&apdu[apdu_len], error_code); + apdu_len += len; + len = encode_closing_tag(&apdu[apdu_len], 0); + apdu_len += len; + len = encode_context_unsigned(&apdu[apdu_len], 1, + private_data->vendorID); + apdu_len += len; + len = encode_context_unsigned(&apdu[apdu_len], 2, + private_data->serviceNumber); + apdu_len += len; + len = encode_opening_tag(&apdu[apdu_len], 3); + apdu_len += len; + for (len = 0; len < private_data->serviceParametersLen; len++) { + apdu[apdu_len] = private_data->serviceParameters[len]; + apdu_len++; + } + len = encode_closing_tag(&apdu[apdu_len], 3); + apdu_len += len; + } + + return apdu_len; +} + +/* decode the service request only */ +int ptransfer_error_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code, + BACNET_PRIVATE_TRANSFER_DATA * private_data) +{ + unsigned len = 0; /* return value */ + unsigned decode_len = 0; /* return value */ + uint8_t tag_number = 0; + uint32_t len_value_type = 0; + uint32_t unsigned_value = 0; + + /* check for value pointers */ + if (apdu_len && private_data) { + /* Tag 0: Error */ + if (decode_is_opening_tag_number(&apdu[len], 0)) { + /* a tag number of 0 is not extended so only one octet */ + len++; + /* error class */ + decode_len = + decode_tag_number_and_value(&apdu[len], &tag_number, + &len_value_type); + len += decode_len; + if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) { + return 0; + } + decode_len = + decode_enumerated(&apdu[len], len_value_type, &unsigned_value); + len += decode_len; + if (error_class) { + *error_class = (BACNET_ERROR_CLASS) unsigned_value; + } + /* error code */ + decode_len = + decode_tag_number_and_value(&apdu[len], &tag_number, + &len_value_type); + len += decode_len; + if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) { + return 0; + } + decode_len = + decode_enumerated(&apdu[len], len_value_type, &unsigned_value); + len += decode_len; + if (error_code) { + *error_code = (BACNET_ERROR_CODE) unsigned_value; + } + if (decode_is_closing_tag_number(&apdu[len], 0)) { + /* a tag number of 0 is not extended so only one octet */ + len++; + } else { + return 0; + } + } + /* Tag 1: vendorID */ + decode_len = decode_context_unsigned(&apdu[len], 1, &unsigned_value); + if (decode_len < 0) { + return -1; + } + len += decode_len; + private_data->vendorID = unsigned_value; + /* Tag 2: serviceNumber */ + decode_len = decode_context_unsigned(&apdu[len], 2, &unsigned_value); + if (decode_len < 0) { + return -1; + } + len += decode_len; + private_data->serviceNumber = unsigned_value; + /* Tag 3: serviceParameters */ + if (decode_is_opening_tag_number(&apdu[len], 3)) { + /* a tag number of 2 is not extended so only one octet */ + len++; + /* don't decode the serviceParameters here */ + private_data->serviceParameters = &apdu[len]; + private_data->serviceParametersLen = apdu_len - len - 1 /*closing tag */ ; + } else { + return -1; + } + /* we could check for a closing tag of 3 */ + } + + return (int) len; +} + +int ptransfer_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_PRIVATE_TRANSFER_DATA *private_data) +{ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ + + if (apdu) { + apdu[0] = PDU_TYPE_COMPLEX_ACK; /* complex ACK service */ + apdu[1] = invoke_id; /* original invoke id from request */ + apdu[2] = SERVICE_CONFIRMED_PRIVATE_TRANSFER; /* service choice */ + apdu_len = 3; + /* service ack follows */ +/* + ConfirmedPrivateTransfer-ACK ::= SEQUENCE { + vendorID [0] Unsigned, + serviceNumber [1] Unsigned, + resultBlock [2] ABSTRACT-SYNTAX.&Type OPTIONAL + } +*/ + len = encode_context_unsigned(&apdu[apdu_len], 0, + private_data->vendorID); + apdu_len += len; + len = encode_context_unsigned(&apdu[apdu_len], 1, + private_data->serviceNumber); + apdu_len += len; + len = encode_opening_tag(&apdu[apdu_len], 2); + apdu_len += len; + for (len = 0; len < private_data->serviceParametersLen; len++) { + apdu[apdu_len] = private_data->serviceParameters[len]; + apdu_len++; + } + len = encode_closing_tag(&apdu[apdu_len], 2); + apdu_len += len; + } + + return apdu_len; +} + +/* ptransfer_ack_decode_service_request() is the same as + ptransfer_decode_service_request */ + +#ifdef TEST +#include +#include +#include "ctest.h" + +int ptransfer_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_PRIVATE_TRANSFER_DATA *private_data) +{ + int len = 0; + unsigned offset = 0; + + if (!apdu) + return -1; + /* optional checking - most likely was already done prior to this call */ + if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) + return -1; + /* apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU); */ + /* invoke id - filled in by net layer */ + *invoke_id = apdu[2]; + if (apdu[3] != SERVICE_CONFIRMED_PRIVATE_TRANSFER) + return -1; + offset = 4; + + if (apdu_len > offset) { + len = ptransfer_decode_service_request( + &apdu[offset], apdu_len - offset, private_data); + } + + return len; +} + +int ptransfer_ack_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ + uint8_t * invoke_id, + BACNET_PRIVATE_TRANSFER_DATA *private_data) +{ + int len = 0; + int offset = 0; + + if (!apdu) + return -1; + /* optional checking - most likely was already done prior to this call */ + if (apdu[0] != PDU_TYPE_COMPLEX_ACK) + return -1; + *invoke_id = apdu[1]; + if (apdu[2] != SERVICE_CONFIRMED_PRIVATE_TRANSFER) + return -1; + offset = 3; + if (apdu_len > offset) { + len = + ptransfer_decode_service_request( + &apdu[offset], apdu_len - offset, private_data); + } + + return len; +} + +int ptransfer_error_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ + uint8_t * invoke_id, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code, + BACNET_PRIVATE_TRANSFER_DATA *private_data) +{ + int len = 0; + int offset = 0; + + if (!apdu) + return -1; + /* optional checking - most likely was already done prior to this call */ + if (apdu[0] != PDU_TYPE_ERROR) + return -1; + *invoke_id = apdu[1]; + if (apdu[2] != SERVICE_CONFIRMED_PRIVATE_TRANSFER) + return -1; + offset = 3; + if (apdu_len > offset) { + len = + ptransfer_error_decode_service_request( + &apdu[offset], apdu_len - offset, + error_class, error_code, private_data); + } + + return len; +} + +void test_Private_Transfer_Ack( + Test * pTest) +{ + uint8_t apdu[480] = { 0 }; + int len = 0; + int apdu_len = 0; + uint8_t invoke_id = 128; + uint8_t test_invoke_id = 0; +#define MAX_PD_CHUNK 32 + uint8_t private_data_chunk[MAX_PD_CHUNK] = "I Love You, Patricia!"; + BACNET_PRIVATE_TRANSFER_DATA private_data; + BACNET_PRIVATE_TRANSFER_DATA test_data; + + private_data.vendorID = BACNET_VENDOR_ID; + private_data.serviceNumber = 1; + private_data.serviceParameters = &private_data_chunk[0]; + private_data.serviceParametersLen = MAX_PD_CHUNK; + + len = ptransfer_ack_encode_apdu(&apdu[0], invoke_id, &private_data); + ct_test(pTest, len != 0); + ct_test(pTest, len != -1); + apdu_len = len; + len = ptransfer_ack_decode_apdu( + &apdu[0], apdu_len, + &test_invoke_id, &test_data); + ct_test(pTest, len != -1); + ct_test(pTest, test_invoke_id == invoke_id); + ct_test(pTest, test_data.vendorID == private_data.vendorID); + ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber); + ct_test(pTest, + test_data.serviceParametersLen == private_data.serviceParametersLen); + for (len = 0; len < MAX_PD_CHUNK; len++) { + ct_test(pTest, + test_data.serviceParameters[len] == + private_data.serviceParameters[len]); + } +} + +void test_Private_Transfer_Error( + Test * pTest) +{ + uint8_t apdu[480] = { 0 }; + int len = 0; + int apdu_len = 0; + uint8_t invoke_id = 128; + uint8_t test_invoke_id = 0; + BACNET_ERROR_CLASS error_class = ERROR_CLASS_RESOURCES; + BACNET_ERROR_CODE error_code = ERROR_CODE_OPERATIONAL_PROBLEM; + BACNET_ERROR_CLASS test_error_class = 0; + BACNET_ERROR_CODE test_error_code = 0; +#define MAX_PD_CHUNK 32 + uint8_t private_data_chunk[MAX_PD_CHUNK] = "I Love You, Patricia!"; + BACNET_PRIVATE_TRANSFER_DATA private_data; + BACNET_PRIVATE_TRANSFER_DATA test_data; + + private_data.vendorID = BACNET_VENDOR_ID; + private_data.serviceNumber = 1; + private_data.serviceParameters = &private_data_chunk[0]; + private_data.serviceParametersLen = MAX_PD_CHUNK; + + len = ptransfer_error_encode_apdu(&apdu[0], invoke_id, + error_class, error_code, &private_data); + ct_test(pTest, len != 0); + ct_test(pTest, len != -1); + apdu_len = len; + len = ptransfer_error_decode_apdu( + &apdu[0], apdu_len, + &test_invoke_id, + &test_error_class, &test_error_code, + &test_data); + ct_test(pTest, len != -1); + ct_test(pTest, test_invoke_id == invoke_id); + ct_test(pTest, test_data.vendorID == private_data.vendorID); + ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber); + ct_test(pTest, test_error_class == error_class); + ct_test(pTest, test_error_code == error_code); + ct_test(pTest, + test_data.serviceParametersLen == private_data.serviceParametersLen); + for (len = 0; len < MAX_PD_CHUNK; len++) { + ct_test(pTest, + test_data.serviceParameters[len] == + private_data.serviceParameters[len]); + } +} + +void test_Private_Transfer_Request( + Test * pTest) +{ + uint8_t apdu[480] = { 0 }; + int len = 0; + int apdu_len = 0; + uint8_t invoke_id = 128; + uint8_t test_invoke_id = 0; +#define MAX_PD_CHUNK 32 + uint8_t private_data_chunk[MAX_PD_CHUNK] = "I Love You, Patricia!"; + BACNET_PRIVATE_TRANSFER_DATA private_data; + BACNET_PRIVATE_TRANSFER_DATA test_data; + + private_data.vendorID = BACNET_VENDOR_ID; + private_data.serviceNumber = 1; + private_data.serviceParameters = &private_data_chunk[0]; + private_data.serviceParametersLen = MAX_PD_CHUNK; + len = ptransfer_encode_apdu(&apdu[0], invoke_id, &private_data); + ct_test(pTest, len != 0); + apdu_len = len; + len = ptransfer_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data); + ct_test(pTest, len != -1); + ct_test(pTest, test_data.vendorID == private_data.vendorID); + ct_test(pTest, test_data.serviceNumber == private_data.serviceNumber); + ct_test(pTest, + test_data.serviceParametersLen == private_data.serviceParametersLen); + for (len = 0; len < MAX_PD_CHUNK; len++) { + ct_test(pTest, + test_data.serviceParameters[len] == + private_data.serviceParameters[len]); + } + + return; +} + +#ifdef TEST_PRIVATE_TRANSFER +int main( + void) +{ + Test *pTest; + bool rc; + + pTest = ct_create("BACnet PrivateTransfer", NULL); + /* individual tests */ + rc = ct_addTestFunction(pTest, test_Private_Transfer_Request); + assert(rc); + rc = ct_addTestFunction(pTest, test_Private_Transfer_Ack); + assert(rc); + rc = ct_addTestFunction(pTest, test_Private_Transfer_Error); + assert(rc); + + ct_setStream(pTest, stdout); + ct_run(pTest); + (void) ct_report(pTest); + ct_destroy(pTest); + + return 0; +} +#endif /* TEST_READ_PROPERTY */ +#endif /* TEST */ + + diff --git a/bacnet-stack/test.mak b/bacnet-stack/test.mak index fe0caeb0..36e30221 100644 --- a/bacnet-stack/test.mak +++ b/bacnet-stack/test.mak @@ -149,6 +149,12 @@ npdu: logfile test/npdu.mak ( ./test/npdu >> ${LOGFILE} ) ( cd test ; make -f npdu.mak clean ) +ptransfer: logfile test/ptransfer.mak + ( cd test ; make -f ptransfer.mak clean ) + ( cd test ; make -f ptransfer.mak ) + ( ./test/ptransfer >> ${LOGFILE} ) + ( cd test ; make -f ptransfer.mak clean ) + rd: logfile test/rd.mak ( cd test ; make -f rd.mak clean ) ( cd test ; make -f rd.mak ) diff --git a/bacnet-stack/test/ptransfer.mak b/bacnet-stack/test/ptransfer.mak new file mode 100644 index 00000000..673da572 --- /dev/null +++ b/bacnet-stack/test/ptransfer.mak @@ -0,0 +1,35 @@ +#Makefile to build test case +CC = gcc +SRC_DIR = ../src +INCLUDES = -I../include -I. +DEFINES = -DBIG_ENDIAN=0 -DTEST -DTEST_PRIVATE_TRANSFER + +CFLAGS = -Wall $(INCLUDES) $(DEFINES) -g + +SRCS = $(SRC_DIR)/bacdcode.c \ + $(SRC_DIR)/bacint.c \ + $(SRC_DIR)/bacstr.c \ + $(SRC_DIR)/bacreal.c \ + $(SRC_DIR)/ptransfer.c \ + ctest.c + +TARGET = ptransfer + +all: ${TARGET} + +OBJS = ${SRCS:.c=.o} + +${TARGET}: ${OBJS} + ${CC} -o $@ ${OBJS} + +.c.o: + ${CC} -c ${CFLAGS} $*.c -o $@ + +depend: + rm -f .depend + ${CC} -MM ${CFLAGS} *.c >> .depend + +clean: + rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini + +include: .depend