예제 #1
0
//---------------------------------------------------------------------------
void PacketFile::reserve(int32_t count, bool useCheckSum)
{
	//---------------------------------------------------
	// If we already have packets, reserve does nothing.
	// Otherwise, reserve sets up the file.  Must be
	// called before any writing to a newly created file.
	if(numPackets)
	{
		return;
	}
	usesCheckSum = useCheckSum;
	numPackets = count;
	int32_t firstPacketOffset = TABLE_ENTRY(numPackets);
	writeLong(PACKET_FILE_VERSION);
	writeLong(firstPacketOffset);
	//----------------------------
	// initialize the seek table
	while(count-- > 0)
		writeLong(SetPacketType(firstPacketOffset, STORAGE_TYPE_NUL));
	//-------------------------------------------------------------
	// If we called this, chances are we are writing a packet file
	// from start to finish.  It is MUCH faster if this table is
	// updated in memory and flushed when the file is closed.
	if(!seekTable)
	{
		seekTable = (int32_t*)systemHeap->Malloc(numPackets * sizeof(int32_t));
		if(seekTable != nullptr)
		{
			seek(sizeof(int32_t) * 2);							//File Version & File Length
			read(puint8_t(seekTable), (numPackets * sizeof(int32_t)));
		}
	}
}
예제 #2
0
//---------------------------------------------------------------------------
void PacketFile::atClose(void)
{
	if(isOpen() && fileMode != READ)								// update filesize
	{
		int32_t endPtr = getLength();
		//seek(sizeof(int32_t));								//Move Past Version Marker
		//writeLong(endPtr);								//Write File length
		int32_t tableEntry;
		currentPacket = numPackets;
		if(!seekTable)
		{
			while(--currentPacket >= 0)
			{
				seek(TABLE_ENTRY(currentPacket));
				tableEntry = readLong();
				if(GetPacketType(tableEntry) == STORAGE_TYPE_NUL)
				{
					seek(TABLE_ENTRY(currentPacket));
					writeLong(SetPacketType(endPtr, STORAGE_TYPE_NUL));
				}
				else
				{
					endPtr = GetPacketOffset(tableEntry);
				}
			}
		}
		else
		{
			while(--currentPacket >= 0)
			{
				tableEntry = seekTable[currentPacket];
				if(GetPacketType(tableEntry) == STORAGE_TYPE_NUL)
				{
					seekTable[currentPacket] = SetPacketType(endPtr, STORAGE_TYPE_NUL);
				}
				else
				{
					endPtr = GetPacketOffset(tableEntry);
				}
			}
		}
		//-----------------------------------------------------
		// If seekTable was being used, write it back to file
		if(seekTable)
		{
			seek(sizeof(int32_t) * 2);							//File Version & File Length
			write(puint8_t(seekTable), (numPackets * sizeof(int32_t)));
		}
		//------------------------------------------------------
		// Is we were using a checkSum, calc it and write it to
		// the beginning of the file.
		if(usesCheckSum)
		{
			int32_t checkSum = checkSumFile();
			seek(0);
			writeLong(checkSum);
		}
	}
	clear();
}
예제 #3
0
//---------------------------------------------------------------------------
int32_t PacketFile::afterOpen(void)
{
	if(!numPackets && getLength() >= 12)
	{
		int32_t firstPacketOffset;
		int32_t firstCheck = readLong();
		if(firstCheck == PACKET_FILE_VERSION && !usesCheckSum)
		{
		}
		else
		{
			//---------------------------------------
			// This is probably a checksum.  Check it
			int32_t checkSum = checkSumFile();
			if(checkSum != firstCheck)
				return PACKET_OUT_OF_RANGE;
		}
		firstPacketOffset = readLong();
		numPackets = (firstPacketOffset / sizeof(int32_t)) - 2;
	}
	currentPacket = -1;
	if(fileMode == READ || fileMode == RDWRITE)
	{
		if(numPackets && !seekTable)
		{
			seekTable = (int32_t*)systemHeap->Malloc(numPackets * sizeof(int32_t));
			gosASSERT(seekTable != nullptr);
			seek(sizeof(int32_t) * 2);												//File Version & File Length
			read(puint8_t(seekTable), (numPackets * sizeof(int32_t)));
		}
	}
	return(NO_ERROR);
}
예제 #4
0
bool RS232::SendBufAgain()
{
	mpSerial->write (puint8_t(&mHeaderOut), sizeof(SerialHeader)) ;
	mpSerial->write (mTabOut, mHeaderOut.size) ;
	mWaitAck = true ;	// we want ACK
	mTimeAck = millis () ;
	return true ;
}
예제 #5
0
void RS232::SendAck(uint8_t status)
{
	SerialHeader header ;
	header.begin = SERIAL_MSG_BEGIN ;
	header.size = 0 ;
	header.type = status ;
	header.crc =  0 ;
	mpSerial->write (puint8_t(&header), sizeof(SerialHeader)) ;
	Serial.println ("RS ACK sent");
	
}