From 07a1077b2dcfcc0dd53d68e574c12fbe135659e0 Mon Sep 17 00:00:00 2001 From: skarg Date: Fri, 22 Sep 2006 21:16:20 +0000 Subject: [PATCH] correcting the npdu encoding for handlers. --- bacnet-stack/cov.c | 11 +++++++---- bacnet-stack/demo/handler/h_rp.c | 20 ++++++++++++-------- bacnet-stack/iam.c | 4 ++-- bacnet-stack/npdu.c | 6 +++--- bacnet-stack/npdu.h | 2 +- bacnet-stack/ports/linux/arcnet.c | 7 +++---- bacnet-stack/ports/linux/ethernet.c | 7 +++---- bacnet-stack/ports/rtos32/dlmstp.c | 19 ++++++++++--------- bacnet-stack/ports/rtos32/ethernet.c | 7 +++---- 9 files changed, 44 insertions(+), 39 deletions(-) diff --git a/bacnet-stack/cov.c b/bacnet-stack/cov.c index 75172cbb..3e12b299 100644 --- a/bacnet-stack/cov.c +++ b/bacnet-stack/cov.c @@ -275,7 +275,8 @@ int cov_notify_decode_service_request(uint8_t * apdu, } int ucov_notify_send(uint8_t * buffer, BACNET_COV_DATA * data) -{ +{ + int len = 0; int pdu_len = 0; BACNET_ADDRESS dest; int bytes_sent = 0; @@ -283,10 +284,12 @@ int ucov_notify_send(uint8_t * buffer, BACNET_COV_DATA * data) /* unconfirmed is a broadcast */ datalink_get_broadcast_address(&dest); + /* encode the NPDU portion of the packet */ + npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); + pdu_len = npdu_encode_pdu(&buffer[0], &dest, NULL, &npdu_data); /* encode the APDU portion of the packet */ - pdu_len = ucov_notify_encode_apdu(&buffer[0], data); - /* encode the NPDU portion of the packet */ - npdu_encode_unconfirmed_apdu(&npdu_data, MESSAGE_PRIORITY_NORMAL); + len = ucov_notify_encode_apdu(&buffer[pdu_len], data); + pdu_len += len; /* send the data */ bytes_sent = datalink_send_pdu(&dest, &npdu_data, &buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_rp.c b/bacnet-stack/demo/handler/h_rp.c index 98d2c2b9..eb54e549 100644 --- a/bacnet-stack/demo/handler/h_rp.c +++ b/bacnet-stack/demo/handler/h_rp.c @@ -59,33 +59,37 @@ void handler_read_property(uint8_t * service_request, int len = 0; int pdu_len = 0; BACNET_NPDU_DATA npdu_data; - bool send = false; - bool error = false; + bool error = true; 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; len = rp_decode_service_request(service_request, service_len, &data); + /* 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_encode_pdu(&Handler_Transmit_Buffer[0], src, + &my_address, &npdu_data); + #if PRINT_ENABLED if (len <= 0) fprintf(stderr, "Unable to decode Read-Property Request!\n"); #endif /* bad decoding - send an abort */ if (len < 0) { - pdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[0], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER); #if PRINT_ENABLED fprintf(stderr, "Sending Abort!\n"); #endif - send = true; } else if (service_data->segmented_message) { - pdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[0], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); #if PRINT_ENABLED fprintf(stderr, "Sending Abort!\n"); #endif - send = true; } else { switch (data.object_type) { case OBJECT_DEVICE: @@ -99,8 +103,8 @@ void handler_read_property(uint8_t * service_request, data.application_data = &Temp_Buf[0]; data.application_data_len = len; /* FIXME: probably need a length limitation sent with encode */ - pdu_len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer[0], + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); #if PRINT_ENABLED fprintf(stderr, diff --git a/bacnet-stack/iam.c b/bacnet-stack/iam.c index da917279..79388f30 100755 --- a/bacnet-stack/iam.c +++ b/bacnet-stack/iam.c @@ -145,8 +145,8 @@ int iam_send(uint8_t * buffer) /* I-Am is a global broadcast */ datalink_get_broadcast_address(&dest); /* encode the NPDU portion of the packet */ - npdu_encode_apdu(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - len = npdu_encode_pdu(&buffer[0], &dest, NULL, &npdu_data); + npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); + pdu_len = npdu_encode_pdu(&buffer[0], &dest, NULL, &npdu_data); /* encode the APDU portion of the packet */ len = iam_encode_apdu(&buffer[pdu_len], Device_Object_Instance_Number(), diff --git a/bacnet-stack/npdu.c b/bacnet-stack/npdu.c index 9bb2edc1..5284507e 100644 --- a/bacnet-stack/npdu.c +++ b/bacnet-stack/npdu.c @@ -189,7 +189,7 @@ int npdu_encode_pdu(uint8_t * npdu, /* Configure the NPDU portion of the packet for an APDU */ /* This function does not handle the network messages, just APDUs. */ -void npdu_encode_apdu(BACNET_NPDU_DATA * npdu_data, +void npdu_encode_npdu_data(BACNET_NPDU_DATA * npdu_data, bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority) { @@ -388,7 +388,7 @@ void testNPDU2(Test * pTest) for (i = 0; i < src.len; i++) { src.adr[i] = 0x40; } - npdu_encode_apdu(&npdu_data, true, priority); + npdu_encode_npdu_data(&npdu_data, true, priority); len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data); ct_test(pTest, len != 0); /* can we get the info back? */ @@ -452,7 +452,7 @@ void testNPDU1(Test * pTest) for (i = 0; i < MAX_MAC_LEN; i++) { src.adr[i] = 0; } - npdu_encode_apdu(&npdu_data, false, priority); + npdu_encode_npdu_data(&npdu_data, false, priority); len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data); ct_test(pTest, len != 0); /* can we get the info back? */ diff --git a/bacnet-stack/npdu.h b/bacnet-stack/npdu.h index 5ac23f4a..4e0196e6 100644 --- a/bacnet-stack/npdu.h +++ b/bacnet-stack/npdu.h @@ -62,7 +62,7 @@ extern "C" { BACNET_ADDRESS * dest, BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data); - void npdu_encode_apdu(BACNET_NPDU_DATA * npdu, + void npdu_encode_npdu_data(BACNET_NPDU_DATA * npdu, bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority); diff --git a/bacnet-stack/ports/linux/arcnet.c b/bacnet-stack/ports/linux/arcnet.c index cac94243..2569834c 100644 --- a/bacnet-stack/ports/linux/arcnet.c +++ b/bacnet-stack/ports/linux/arcnet.c @@ -194,9 +194,9 @@ int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ int bytes = 0; uint8_t mtu[512] = { 0 }; int mtu_len = 0; - int npdu_len = 0; struct archdr *pkt = (struct archdr *) mtu; + (void)npdu_data; src.mac[0] = ARCNET_MAC_Address; src.mac_len = 1; @@ -223,14 +223,13 @@ int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ pkt->soft.raw[1] = 0x82; /* DSAP for BACnet */ pkt->soft.raw[2] = 0x82; /* SSAP for BACnet */ pkt->soft.raw[3] = 0x03; /* LLC Control byte in header */ - npdu_len = npdu_encode_pdu(&pkt->soft.raw[4], dest, &src, npdu_data); /* packet length */ - mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + npdu_len + pdu_len; + mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + pdu_len; if (mtu_len > 512) { fprintf(stderr, "arcnet: PDU is too big to send!\n"); return -4; } - memcpy(&pkt->soft.raw[4 + npdu_len], pdu, pdu_len); + memcpy(&pkt->soft.raw[4], pdu, pdu_len); /* Send the packet */ bytes = sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0, diff --git a/bacnet-stack/ports/linux/ethernet.c b/bacnet-stack/ports/linux/ethernet.c index f5d756b7..f9bbf5f3 100644 --- a/bacnet-stack/ports/linux/ethernet.c +++ b/bacnet-stack/ports/linux/ethernet.c @@ -188,8 +188,8 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ BACNET_ADDRESS src = { 0 }; /* source address for npdu */ uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */ int mtu_len = 0; - int npdu_len = 0; + (void)ndpu_data; /* load the BACnet address for NPDU data */ for (i = 0; i < 6; i++) { src.mac[i] = Ethernet_MAC_Address[i]; @@ -224,8 +224,7 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ mtu[14] = 0x82; /* DSAP for BACnet */ mtu[15] = 0x82; /* SSAP for BACnet */ mtu[16] = 0x03; /* Control byte in header */ - npdu_len = npdu_encode_pdu(&mtu[17], dest, &src, npdu_data); - mtu_len = 17 + npdu_len; + mtu_len = 17; if ((mtu_len + pdu_len) > MAX_MPDU) { fprintf(stderr, "ethernet: PDU is too big to send!\n"); return -4; @@ -233,7 +232,7 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; /* packet length - only the logical portion, not the address */ - encode_unsigned16(&mtu[12], 3 + npdu_len + pdu_len); + encode_unsigned16(&mtu[12], 3 + pdu_len); /* Send the packet */ bytes = diff --git a/bacnet-stack/ports/rtos32/dlmstp.c b/bacnet-stack/ports/rtos32/dlmstp.c index 29793470..53489804 100644 --- a/bacnet-stack/ports/rtos32/dlmstp.c +++ b/bacnet-stack/ports/rtos32/dlmstp.c @@ -64,10 +64,10 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ unsigned pdu_len) { /* number of bytes of data */ int bytes_sent = 0; - unsigned npdu_len = 0; uint8_t frame_type = 0; uint8_t destination = 0; /* destination address */ - BACNET_ADDRESS src; + BACNET_ADDRESS src; + unsigned mtu_len = 0; if (MSTP_Port.TxReady == false) { if (npdu_data->confirmed_message) @@ -84,22 +84,23 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ fprintf(stderr, "mstp: invalid destination MAC address!\n"); #endif return -2; - } - dlmstp_get_my_address(&src); - npdu_len = npdu_encode_pdu(&PDU_Buffer[0], dest, &src, npdu_data); - if ((8 /* header len */ + npdu_len + pdu_len) > MAX_MPDU) { + } + /* header len */ + mtu_len = 8; + if ((mtu_len + pdu_len) > MAX_MPDU) { #if PRINT_ENABLED fprintf(stderr, "mstp: PDU is too big to send!\n"); #endif return -4; - } - memmove(&PDU_Buffer[npdu_len], pdu, pdu_len); + } + memmove(&PDU_Buffer[mtu_len], pdu, pdu_len); + mtu_len += pdu_len; bytes_sent = MSTP_Create_Frame( (uint8_t *) & MSTP_Port.TxBuffer[0], sizeof(MSTP_Port.TxBuffer), MSTP_Port.TxFrameType, destination, - MSTP_Port.This_Station, &PDU_Buffer[0], npdu_len + pdu_len); + MSTP_Port.This_Station, &PDU_Buffer[0], mtu_len); MSTP_Port.TxLength = bytes_sent; MSTP_Port.TxReady = true; } diff --git a/bacnet-stack/ports/rtos32/ethernet.c b/bacnet-stack/ports/rtos32/ethernet.c index 4e3a2537..e57ebae2 100644 --- a/bacnet-stack/ports/rtos32/ethernet.c +++ b/bacnet-stack/ports/rtos32/ethernet.c @@ -94,9 +94,9 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ int bytes = 0; uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; - int npdu_len = 0; int i = 0; + (void)npdu_data; /* don't waste time if the socket is not valid */ if (Ethernet_Socket < 0) { fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); @@ -134,8 +134,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ mtu[mtu_len++] = 0x03; /* Control byte in header */ - npdu_len = npdu_encode_pdu(&mtu[17], dest, src, npdu_data); - mtu_len = 17 + npdu_len; + mtu_len = 17; if ((mtu_len + pdu_len) > MAX_MPDU) { fprintf(stderr, "ethernet: PDU is too big to send!\n"); return -4; @@ -143,7 +142,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; /* packet length - only the logical portion, not the address */ - encode_unsigned16(&mtu[12], 3 + npdu_len + pdu_len); + encode_unsigned16(&mtu[12], 3 + pdu_len); /* Send the packet */ bytes = send(Ethernet_Socket, (const char *) &mtu, mtu_len, 0);