#include <string.h>
#include <assert.h>
#include "hacs_platform.h"
#include "hacs_pstore.h"
#include "stm32f4xx_hal.h"
#include "hacs_crc.h"

static uint32_t current_bank_addr;
static uint32_t alternate_bank_addr;
static uint8_t ram_buf[ALIGN_TO_WORD(HACS_PSTORE_BANK_SIZE)];

int hacs_pstore_init()
{
  hacs_pstore_header_t *header0 = (hacs_pstore_header_t *)HACS_PSTORE_0_ADDR;
  hacs_pstore_header_t *header1 = (hacs_pstore_header_t *)HACS_PSTORE_1_ADDR;
  uint32_t l_version0 = header0->layout_version;
  uint32_t l_version1 = header1->layout_version;
  
  // Determine the current bank and the alternate bank.
  // First, check if one of the banks is erased, by looking at the layout_version field.
  // If neither is erased or both are erased, proceed to compare the bank_version count.
  if (l_version0 == 0xFFFFFFFF && l_version1 != 0xFFFFFFFF) {
    current_bank_addr = HACS_PSTORE_1_ADDR;
    alternate_bank_addr = HACS_PSTORE_0_ADDR;
  } else if (l_version1 == 0xFFFFFFFF && l_version0 != 0xFFFFFFFF) {
    current_bank_addr = HACS_PSTORE_0_ADDR;
    alternate_bank_addr = HACS_PSTORE_1_ADDR;
  } else {
    uint32_t b_version0 = header0->bank_version;
    uint32_t b_version1 = header1->bank_version;
示例#2
0
/*! 
  \brief Moves the packets from the ring buffer to a given buffer.
  \param pWanAdapter Pointer to WAN_ADAPTER structure associated with this instance.
  \param Buffer Pointer to the destination, user allocated, buffer.
  \param BuffSize Size of the buffer.
  \return It returns the number of bytes correctly written to the destination buffer
*/
DWORD WanPacketRemovePacketsFromRingBuffer(PWAN_ADAPTER pWanAdapter, PUCHAR Buffer, DWORD BuffSize)
{
	DWORD Copied;
	struct bpf_hdr *Header;
	Copied = 0;
	DWORD ToCopy;
	DWORD Increment;

	ResetEvent(pWanAdapter->hReadEvent);
	
	while (BuffSize > Copied)
	{
		if ( pWanAdapter->Free < pWanAdapter->Size )  
		{  //there are some packets in the selected (aka LocalData) buffer
			Header = (struct bpf_hdr*)(pWanAdapter->Buffer + pWanAdapter->C);

			if (Header->bh_caplen + sizeof (struct bpf_hdr) > BuffSize - Copied)  
			{  //if the packet does not fit into the user buffer, we've ended copying packets
				return Copied;
			}
				
			*((struct bpf_hdr*)(Buffer + Copied)) = *Header;
			
			Copied += sizeof(struct bpf_hdr);
			pWanAdapter->C += sizeof(struct bpf_hdr);

			if ( pWanAdapter->C == pWanAdapter->Size )
				pWanAdapter->C = 0;

			if ( pWanAdapter->Size - pWanAdapter->C < (DWORD)Header->bh_caplen )
			{
				//the packet is fragmented in the buffer (i.e. it skips the buffer boundary)
				ToCopy = pWanAdapter->Size - pWanAdapter->C;
				CopyMemory(Buffer + Copied,pWanAdapter->Buffer + pWanAdapter->C, ToCopy);
				CopyMemory(Buffer + Copied + ToCopy, pWanAdapter->Buffer + 0, Header->bh_caplen - ToCopy);
				pWanAdapter->C = Header->bh_caplen - ToCopy;
			}
			else
			{
				//the packet is not fragmented
				CopyMemory(Buffer + Copied ,pWanAdapter->Buffer + pWanAdapter->C ,Header->bh_caplen);
				pWanAdapter->C += Header->bh_caplen;
		//		if (c==size)  inutile, contemplato nell "header atomico"
		//			c=0;
			}

			Copied += ALIGN_TO_WORD(Header->bh_caplen);

			Increment = Header->bh_caplen + sizeof(struct bpf_hdr);
			if ( pWanAdapter->Size - pWanAdapter->C < sizeof(struct bpf_hdr) )
			{   //the next packet would be saved at the end of the buffer, but the NewHeader struct would be fragmented
				//so the producer (--> the consumer) skips to the beginning of the buffer
				Increment += pWanAdapter->Size - pWanAdapter->C;
				pWanAdapter->C = 0;
			}
			pWanAdapter->Free += Increment;
		}
		else
			return Copied;
	}
	return Copied;
}