/* ********************************************************************************************** * File: pio_packet_queue.cpp * Description: * Authors: Liu WenTan * Date: 2022-07-15 * Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. *********************************************************************************************** */ #include #include "../../sdk/include/utils.h" #include "pio_packet_queue.h" static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len) { memcpy(ptr + offset, data, data_len); return 0; } int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len) { return packet_copy_data_offset(ptr, 0, pkt_data, pkt_len); } void pio_packet_enqueue(struct pio_packet_queue *q, struct pio_packet *p) { if (nullptr == p) return; /* more packets in queue */ if (q->top != nullptr) { p->prev = nullptr; p->next = q->top; q->top->prev = p; q->top = p; /* only packet */ } else { p->prev = nullptr; p->next = nullptr; q->top = p; q->bot = p; } q->len++; } struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *q) { struct pio_packet *p = nullptr; /* if the queue is empty there are no packets left. */ if (q->len == 0) { return nullptr; } q->len--; /* pull the bottom packet from the queue */ p = q->bot; /* more packets in queue */ if (q->bot->prev != nullptr) { q->bot = q->bot->prev; q->bot->next = nullptr; /* just the one we remove, so now empty */ } else { q->top = nullptr; q->bot = nullptr; } p->next = nullptr; p->prev = nullptr; return p; } void release_pio_packet_queue(struct pio_packet_queue *q) { if (nullptr == q) { return; } while (q->len != 0) { struct pio_packet *p = pio_packet_dequeue(q); q->len--; FREE(p); } }