Skip to content

Instantly share code, notes, and snippets.

@igotit-anything
Created January 27, 2020 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igotit-anything/24265dcd18213a98ba1385ab24af9de1 to your computer and use it in GitHub Desktop.
Save igotit-anything/24265dcd18213a98ba1385ab24af9de1 to your computer and use it in GitHub Desktop.
//////// cy_class_bmft5_pack.h
#ifndef cy_class_bmft5_pack_h
#define cy_class_bmft5_pack_h
#include <stdint.h>
#include <string.h> // memcpy,NULL, ..
#include <stdlib.h> // malloc
#define SIZE_BLOCK_BMFT5 244
typedef void(*cy_pf_v_pui8_ui16)(uint8_t *, uint16_t ) ;
class CBMFT5_Pack
{
public:
CBMFT5_Pack(uint16_t size_block
, uint16_t size_packet
, cy_pf_v_pui8_ui16 fp_use_block
);
~CBMFT5_Pack(void);
private:
uint16_t Size_Block; // BMFT4 header 4byte + PacketArea
uint16_t Size_Packet;
uint16_t Size_BlockPacketArea;
uint16_t IndexFirst_BlockPacketArea; //
uint8_t NPE; // Number of Packet Edge in current Block PacketArea.
uint16_t IPF; // Index of Packet Edge in Block.
uint8_t BlockCount ;
// uint8_t * Block; //dynamic creation in construct
uint8_t Block[ SIZE_BLOCK_BMFT5];
cy_pf_v_pui8_ui16 cb_use_block;
void proc_pack_packet_smaller_packetarea_block(uint8_t * p_packet);
void proc_pack_packet_larger_packetarea_block(uint8_t * p_packet);
void proc_pack_packet_equal_packetarea_block(uint8_t * p_packet);
void Use_Block(void);
public:
void proc_pack(uint8_t * p_packet);
};
#endif
//////// cy_class_bmft5_pack.cpp
#include "cy_class_bmft5_pack.h"
CBMFT5_Pack::CBMFT5_Pack(uint16_t size_block
, uint16_t size_packet
, cy_pf_v_pui8_ui16 fp_use_block
)
{
Size_Block = size_block;
Size_Packet = size_packet;
Size_BlockPacketArea = (Size_Block - 5); // 5 means BMFT5 header size.
IndexFirst_BlockPacketArea = 5; //
NPE = 0;
IPF = IndexFirst_BlockPacketArea;
cb_use_block = fp_use_block;
BlockCount=0;
Block[0] =16; // 16~31 is BMF.
// Block = (uint8_t *)malloc(Size_Block);
}
CBMFT5_Pack::~CBMFT5_Pack(void)
{
/*
if(Block != NULL)
{
free((void*)Block);
Block = NULL;
}
*/
}
void CBMFT5_Pack::proc_pack_packet_smaller_packetarea_block(uint8_t * p_packet)
{
static uint16_t remain_size_BlockPacketArea = Size_BlockPacketArea;
static uint16_t block_index_packet_write = IndexFirst_BlockPacketArea; // init as first position.
remain_size_BlockPacketArea = (Size_Block - block_index_packet_write) ; // renewal remain size of PacketArea.
if(remain_size_BlockPacketArea > Size_Packet)
{
memcpy(&Block[block_index_packet_write],&p_packet[0],Size_Packet);
NPE++; //increment +1 when if only p_packet[0] allocated
if(NPE == 1) IPF = block_index_packet_write;
block_index_packet_write += Size_Packet; // for next write
}
else if(remain_size_BlockPacketArea == Size_Packet)
{
memcpy(&Block[block_index_packet_write],&p_packet[0],Size_Packet);
NPE++; //increment +1 when if only p_packet[0] allocated
if(NPE == 1) IPF = block_index_packet_write;
Use_Block(); // when Block Full
block_index_packet_write = IndexFirst_BlockPacketArea; // for next write
}
else if(remain_size_BlockPacketArea < Size_Packet)
{
memcpy(&Block[block_index_packet_write],&p_packet[0],remain_size_BlockPacketArea);
NPE++; //increment +1 when if only p_packet[0] allocated
if(NPE == 1) IPF = block_index_packet_write;
Use_Block(); // when Block Full
block_index_packet_write = IndexFirst_BlockPacketArea; // for next write
memcpy(&Block[block_index_packet_write],&p_packet[remain_size_BlockPacketArea],(Size_Packet - remain_size_BlockPacketArea));
block_index_packet_write += (Size_Packet - remain_size_BlockPacketArea); // for next write
}
}
/*
in this case the NPE(Number of Packet Edge is only 0 or 1)
*/
void CBMFT5_Pack::proc_pack_packet_larger_packetarea_block(uint8_t * p_packet)
{
// static uint16_t remain_size_packet = Size_Packet; // un written size of packet
static uint16_t block_index_packet_write = IndexFirst_BlockPacketArea; // init as first position.
uint16_t packet_index_write = 0;
uint16_t num_division_packet;
uint16_t size_tail_packet;
uint16_t size_front_packet;
uint16_t idx_divsion_packet;
if(block_index_packet_write > IndexFirst_BlockPacketArea )
{
size_front_packet = (Size_Block - block_index_packet_write);
num_division_packet = (Size_Packet-size_front_packet)/Size_BlockPacketArea;
size_tail_packet = (Size_Packet-size_front_packet)%Size_BlockPacketArea;
/// write front packet to Block backend.
memcpy(&Block[block_index_packet_write],&p_packet[0],size_front_packet);
NPE = 1;
IPF = block_index_packet_write;
Use_Block();
block_index_packet_write = IndexFirst_BlockPacketArea; // next write position.
packet_index_write = size_front_packet;// next write position.
/// write middle packet
for(idx_divsion_packet = 0 ; idx_divsion_packet < num_division_packet; idx_divsion_packet++)
{
memcpy(&Block[block_index_packet_write],&p_packet[packet_index_write],Size_BlockPacketArea);
Use_Block();
packet_index_write += Size_BlockPacketArea; // next write position.
}
/// write tail packet
if(size_tail_packet > 0)
{
memcpy(&Block[block_index_packet_write],&p_packet[packet_index_write],size_tail_packet);
block_index_packet_write = IndexFirst_BlockPacketArea + size_tail_packet; // for next write new packet received.
}
else if(size_tail_packet == 0)
{
block_index_packet_write = IndexFirst_BlockPacketArea; // for next write new packet received.
}
} //if(block_index_packet_write > IndexFirst_BlockPacketArea )
else // block_index_packet_write == IndexFirst_BlockPacketArea, no need to write front
{
num_division_packet = (Size_Packet/Size_BlockPacketArea);
size_tail_packet = (Size_Packet%Size_BlockPacketArea);
block_index_packet_write = IndexFirst_BlockPacketArea;
packet_index_write = 0;
/// write middle packet
for(idx_divsion_packet = 0 ; idx_divsion_packet < num_division_packet; idx_divsion_packet++)
{
memcpy(&Block[block_index_packet_write],&p_packet[packet_index_write],Size_BlockPacketArea);
Use_Block();
packet_index_write += Size_BlockPacketArea; // next write position.
}
/// write tail packet
if(size_tail_packet > 0)
{
memcpy(&Block[IndexFirst_BlockPacketArea],&p_packet[packet_index_write],size_tail_packet);
block_index_packet_write = IndexFirst_BlockPacketArea + size_tail_packet; // for next write new packet received.
}
else if(size_tail_packet == 0)
{
block_index_packet_write = IndexFirst_BlockPacketArea; // for next write new packet received.
}
}//else block_index_packet_write == IndexFirst_BlockPacketArea
}
void CBMFT5_Pack::proc_pack_packet_equal_packetarea_block(uint8_t * p_packet)
{
memcpy(&Block[IndexFirst_BlockPacketArea], p_packet, Size_Packet);
NPE = 1;
IPF = IndexFirst_BlockPacketArea;
Use_Block();
}
void CBMFT5_Pack::Use_Block(void)
{
Block[1] = NPE;
Block[2] = BlockCount++;
BlockCount &= 31;
memcpy(&Block[3], (uint8_t*)&IPF, 2); // Block[3],[4]
if(cb_use_block != NULL)
cb_use_block(Block, Size_Block);
// init para
NPE = 0;
IPF = IndexFirst_BlockPacketArea;
}
void CBMFT5_Pack::proc_pack(uint8_t * p_packet)
{
if(Size_Packet < Size_BlockPacketArea)
{
proc_pack_packet_smaller_packetarea_block(p_packet);
}
else if(Size_Packet > Size_BlockPacketArea)
{
proc_pack_packet_larger_packetarea_block(p_packet);
}
else
{
proc_pack_packet_equal_packetarea_block(p_packet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment