From 07bf4eba3bd39fd59ce355f9d65dda81be322d8b Mon Sep 17 00:00:00 2001 From: skarg Date: Wed, 9 Sep 2015 14:54:02 +0000 Subject: [PATCH] Deprecated Ringbuf_Alloc, and replaced with Ringbuf_Data_Peek() and Ringbuf_Data_Put() functions. Ringbuf_Alloc() was not interrupt or thread safe. --- bacnet-stack/include/ringbuf.h | 4 +- bacnet-stack/ports/at91sam7s/dlmstp.c | 6 ++- bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c | 6 ++- bacnet-stack/ports/linux/dlmstp.c | 6 ++- bacnet-stack/ports/linux/dlmstp_linux.c | 6 ++- bacnet-stack/ports/stm32f10x/dlmstp.c | 11 +++-- bacnet-stack/ports/stm32f10x/stm32f10x_conf.h | 2 +- bacnet-stack/ports/xplained/bacnet.c | 9 ++-- bacnet-stack/ports/xplained/dlmstp.c | 10 +++-- bacnet-stack/src/bacstr.c | 2 +- bacnet-stack/src/ringbuf.c | 44 +++++++++++++++---- 11 files changed, 75 insertions(+), 31 deletions(-) diff --git a/bacnet-stack/include/ringbuf.h b/bacnet-stack/include/ringbuf.h index 6125fd07..4c28efb1 100644 --- a/bacnet-stack/include/ringbuf.h +++ b/bacnet-stack/include/ringbuf.h @@ -60,8 +60,10 @@ extern "C" { bool Ringbuf_Put_Front( RING_BUFFER * b, /* ring buffer structure */ uint8_t * data_element); - volatile uint8_t *Ringbuf_Alloc( + volatile uint8_t *Ringbuf_Data_Peek( RING_BUFFER * b); + bool Ringbuf_Data_Put( + RING_BUFFER * b, volatile uint8_t *data_element); /* Note: element_count must be a power of two */ void Ringbuf_Init( RING_BUFFER * b, /* ring buffer structure */ diff --git a/bacnet-stack/ports/at91sam7s/dlmstp.c b/bacnet-stack/ports/at91sam7s/dlmstp.c index 7a5e8422..e3935053 100644 --- a/bacnet-stack/ports/at91sam7s/dlmstp.c +++ b/bacnet-stack/ports/at91sam7s/dlmstp.c @@ -1232,7 +1232,7 @@ int dlmstp_send_pdu( struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1245,7 +1245,9 @@ int dlmstp_send_pdu( /* mac_len = 0 is a broadcast address */ pkt->destination_mac = MSTP_BROADCAST_ADDRESS; } - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c b/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c index 7562b7f5..c66d4a75 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c @@ -1194,7 +1194,7 @@ int dlmstp_send_pdu( struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1207,7 +1207,9 @@ int dlmstp_send_pdu( /* mac_len = 0 is a broadcast address */ pkt->destination_mac = MSTP_BROADCAST_ADDRESS; } - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; diff --git a/bacnet-stack/ports/linux/dlmstp.c b/bacnet-stack/ports/linux/dlmstp.c index bd9fefa6..70d5dbfc 100644 --- a/bacnet-stack/ports/linux/dlmstp.c +++ b/bacnet-stack/ports/linux/dlmstp.c @@ -155,7 +155,7 @@ int dlmstp_send_pdu( struct mstp_pdu_packet *pkt; unsigned i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -168,7 +168,9 @@ int dlmstp_send_pdu( /* mac_len = 0 is a broadcast address */ pkt->destination_mac = MSTP_BROADCAST_ADDRESS; } - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; diff --git a/bacnet-stack/ports/linux/dlmstp_linux.c b/bacnet-stack/ports/linux/dlmstp_linux.c index 5192de56..679bbb7e 100644 --- a/bacnet-stack/ports/linux/dlmstp_linux.c +++ b/bacnet-stack/ports/linux/dlmstp_linux.c @@ -151,7 +151,7 @@ int dlmstp_send_pdu( return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&poSharedData->PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&poSharedData->PDU_Queue); if (pkt) { pkt->data_expecting_reply = BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]); @@ -160,7 +160,9 @@ int dlmstp_send_pdu( } pkt->length = pdu_len; pkt->destination_mac = dest->mac[0]; - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; diff --git a/bacnet-stack/ports/stm32f10x/dlmstp.c b/bacnet-stack/ports/stm32f10x/dlmstp.c index fd7d47ea..b811df48 100644 --- a/bacnet-stack/ports/stm32f10x/dlmstp.c +++ b/bacnet-stack/ports/stm32f10x/dlmstp.c @@ -444,10 +444,10 @@ static void MSTP_Send_Frame( { /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - static struct mstp_tx_packet *pkt; + struct mstp_tx_packet *pkt; uint16_t i = 0; /* used to calculate CRC for data */ - pkt = (struct mstp_tx_packet *) Ringbuf_Alloc(&Transmit_Queue); + pkt = (struct mstp_tx_packet *) Ringbuf_Data_Peek(&Transmit_Queue); if (pkt) { /* create the MS/TP header */ pkt->buffer[0] = 0x55; @@ -476,6 +476,7 @@ static void MSTP_Send_Frame( pkt->length += data_len; pkt->length += 2; } + Ringbuf_Data_Put(&Transmit_Queue, (uint8_t *)pkt); } else { pkt = NULL; } @@ -1274,7 +1275,7 @@ int dlmstp_send_pdu( struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1286,7 +1287,9 @@ int dlmstp_send_pdu( } else { pkt->destination_mac = MSTP_BROADCAST_ADDRESS; } - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; diff --git a/bacnet-stack/ports/stm32f10x/stm32f10x_conf.h b/bacnet-stack/ports/stm32f10x/stm32f10x_conf.h index a89cac7c..ac874601 100644 --- a/bacnet-stack/ports/stm32f10x/stm32f10x_conf.h +++ b/bacnet-stack/ports/stm32f10x/stm32f10x_conf.h @@ -46,7 +46,7 @@ #include "stm32f10x_tim.h" #include "stm32f10x_usart.h" #include "stm32f10x_wwdg.h" -#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ +#include "stm32f10x_misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ diff --git a/bacnet-stack/ports/xplained/bacnet.c b/bacnet-stack/ports/xplained/bacnet.c index 3221bb96..5464a27b 100644 --- a/bacnet-stack/ports/xplained/bacnet.c +++ b/bacnet-stack/ports/xplained/bacnet.c @@ -113,11 +113,12 @@ void bacnet_task_timed( pdu_len = dlmstp_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 5); if (pdu_len) { - pkt = (struct mstp_rx_packet *) Ringbuf_Alloc(&Receive_Queue); + pkt = (struct mstp_rx_packet *) Ringbuf_Data_Peek(&Receive_Queue); if (pkt) { memcpy(pkt->buffer, PDUBuffer, MAX_MPDU); bacnet_address_copy(&pkt->src, &src); pkt->length = pdu_len; + Ringbuf_Data_Put(&Receive_Queue, pkt); } } } @@ -133,7 +134,7 @@ static void bacnet_test_task(void) uint32_t instance; float float_value; uint16_t adc_value; - + instance = Analog_Input_Index_To_Instance(index); if (!Analog_Input_Out_Of_Service(instance)) { adc_value = adc_result_12bit(index); @@ -194,7 +195,7 @@ void bacnet_task(void) void bacnet_init(void) { unsigned i; - + Ringbuf_Init(&Receive_Queue, (uint8_t *) & Receive_Buffer, sizeof(struct mstp_rx_packet), MSTP_RECEIVE_PACKET_COUNT); dlmstp_init(NULL); @@ -229,5 +230,5 @@ void bacnet_init(void) Analog_Input_Units_Set( Analog_Input_Index_To_Instance(i), UNITS_PERCENT); - } + } } diff --git a/bacnet-stack/ports/xplained/dlmstp.c b/bacnet-stack/ports/xplained/dlmstp.c index 8e2b3b70..3313a318 100644 --- a/bacnet-stack/ports/xplained/dlmstp.c +++ b/bacnet-stack/ports/xplained/dlmstp.c @@ -1267,7 +1267,7 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); + pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1279,7 +1279,9 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ } else { pkt->destination_mac = dest->mac[0]; } - bytes_sent = pdu_len; + if (Ringbuf_Data_Put(&PDU_Queue, pkt)) { + bytes_sent = pdu_len; + } } return bytes_sent; @@ -1294,13 +1296,13 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ * @param max_pdu [in] - the size of the PDU buffer * @param timeout [in] - the number of milliseconds to wait for a packet * - * @return Return the length of the packet + * @return Return the length of the packet */ uint16_t dlmstp_receive(BACNET_ADDRESS * src, uint8_t * pdu, uint16_t max_pdu, unsigned timeout) -{ +{ uint16_t pdu_len = 0; /* return value */ bool transmitting = false; diff --git a/bacnet-stack/src/bacstr.c b/bacnet-stack/src/bacstr.c index 45d56de7..e922b4c7 100644 --- a/bacnet-stack/src/bacstr.c +++ b/bacnet-stack/src/bacstr.c @@ -207,7 +207,7 @@ bool bitstring_same( int bytes_used = 0; uint8_t compare_mask = 0; - if (bitstring1 && bitstring1) { + if (bitstring1 && bitstring2) { if ((bitstring1->bits_used == bitstring2->bits_used) && (bitstring1->bits_used / 8 <= MAX_BITSTRING_BYTES)) { bytes_used = (int) (bitstring1->bits_used / 8); diff --git a/bacnet-stack/src/ringbuf.c b/bacnet-stack/src/ringbuf.c index b7292b43..09da334c 100644 --- a/bacnet-stack/src/ringbuf.c +++ b/bacnet-stack/src/ringbuf.c @@ -86,7 +86,7 @@ bool Ringbuf_Empty( } /**************************************************************************** -* DESCRIPTION: Looks at the data from the head of the list without removing it +* DESCRIPTION: Looks at the data from the front of the list without removing it * RETURN: pointer to the data, or NULL if nothing in the list * ALGORITHM: none * NOTES: none @@ -134,7 +134,7 @@ bool Ringbuf_Pop( } /**************************************************************************** -* DESCRIPTION: Adds an element of data to the ring buffer +* DESCRIPTION: Adds an element of data to the end of the ring buffer * RETURN: true on succesful add, false if not added * ALGORITHM: none * NOTES: none @@ -197,13 +197,13 @@ bool Ringbuf_Put_Front( } /**************************************************************************** -* DESCRIPTION: Reserves and gets the next data portion of the buffer. -* RETURN: pointer to the data, or NULL if the list is full +* DESCRIPTION: Gets a pointer to the next free data element of the buffer +* without adding it to the ring. +* RETURN: pointer to the next data chunk, or NULL if ring buffer is full. * ALGORITHM: none -* NOTES: none +* NOTES: Use Ringbuf_Data_Peek with Ringbuf_Data_Put *****************************************************************************/ -volatile uint8_t *Ringbuf_Alloc( - RING_BUFFER * b) +volatile uint8_t *Ringbuf_Data_Peek(RING_BUFFER * b) { volatile uint8_t *ring_data = NULL; /* used to help point ring data */ @@ -212,13 +212,41 @@ volatile uint8_t *Ringbuf_Alloc( if (!Ringbuf_Full(b)) { ring_data = b->buffer; ring_data += ((b->head % b->element_count) * b->element_size); - b->head++; } } return ring_data; } +/**************************************************************************** +* DESCRIPTION: Adds the previously peeked element of data to the end of the +* ring buffer +* RETURN: true if the buffer has space and the data element points to the +* same memory previously peeked. +* ALGORITHM: none +* NOTES: Use Ringbuf_Data_Peek with Ringbuf_Data_Put +*****************************************************************************/ +bool Ringbuf_Data_Put(RING_BUFFER * b, volatile uint8_t *data_element) +{ + bool status = false; + volatile uint8_t *ring_data = NULL; /* used to help point ring data */ + + if (b) { + /* limit the amount of elements that we accept */ + if (!Ringbuf_Full(b)) { + ring_data = b->buffer; + ring_data += ((b->head % b->element_count) * b->element_size); + if (ring_data == data_element) { + /* same chunk of memory - okay to signal the head */ + b->head++; + status = true; + } + } + } + + return status; +} + /**************************************************************************** * DESCRIPTION: Configures the ring buffer * RETURN: none