EdXian / packet-generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

packet-generator

OS

  • Windows
  • Linux/Ubuntu

Pre-installed python library

  • python3.0 is required
  • pyqt5 - pip install pyqt5

Byte-oriented protocol

  • USART
  • TCP/UDP
  • SPI

XML file

Edit a xml file: you can create a new one or modify by the example xml file. The packet generator will generate code based on a xml file.

Do not use any reserved keyword in xml file, otherwise conflicts will be caused

macros

The following is an example for how to define macros in your template.

    <macro name = "PARAM">
        <var name = "PACK_LEN" val = "200" >  </var>
        <var name = "BOOT_MODE" val = "0" >  </var>
        <var name = "CRC_CHECK_LENGTH" val = "128" >  </var>
        <var name = "BL_FLASH_IO" val = "FLASH_0" >  </var>        
        <var name = "BL_USART_IO" val = "USART_0" >  </var>
        <var name = "IS_BOOT_MODE" val = "(pin_mode == BOOT_MODE)" >  </var>
        <var name = "BL_ACK" val = "0XA5" >  </var>
        <var name = "BL_NACK" val = "0X7F" >  </var>                        
    </macro>

enum

    <enum name = "meditation_mode">
        <var name = "meditation_on" val = "0"> </var>
        <var name = "meditation_lv1" val = "1"> </var>
        <var name = "meditation_lv2" val = "2"> </var>
        <var name = "meditation_lv3" val = "3"> </var>
        <var name = "meditation_off" val = "4"> </var>
        <var name = "//reserved" val = "5"> </var>
    </enum>

unions

    <union name = "joint" anonymous_type = "uint8_t">
        <struct>
            <var type="uint8_t" name="header_leg" bit ="1"> </var>
            <var type="uint8_t" name="pillow" bit ="1"> </var>
            <var type="uint8_t" name="neck" bit ="1"> </var>
            <var type="uint8_t" name="back" bit ="1"> </var>
            <var type="uint8_t" name="lumbar" bit ="1"> </var>
            <var type="uint8_t" name="buttocks" bit ="1"> </var>
            <var type="uint8_t" name="reserved" bit ="1"> </var>
        </struct>
    </union>

struct

    <struct name = "header" encode = "false" config = "true">
        <var type="uint8_t" name="header"> </var>
        <var type="uint8_t" name="len"> </var>
    	<var type="uint8_t" name="packet_type"> </var>
    </struct>   

GUI

  1. click load button to load an xml file.
  2. Use default checksum(sum32), little-endian, align 1, align word.
  3. click generate
  4. copy source file and header file in to your project and name them packet.c/packet.h
  5. ensure that the .c/.h file are included in your project.

communication

The communication can be splited into two actio- encoding and decoding

encoding

when trying to send a message from a master to a slave , you should encode a message firstly and then send to the bus. The following snippets shows how to encode a message and send it as a pack.

packet_encode(NULL,&pack,sizeof(packet_t));
uart_send(&pack,sizeof(pack));

decoding

After receieving a byte, a slave is attempt to decode a pack until success. if a pack can be deocded by slave, the slave will know what message sended by a master.

parse_state_t parser;
parser_init(&parser);
...
uart_recv(&buf,1);
msg_id = packet_parser(buf2,buf,&parser);
if(msg_id){
    message_decode(buf2, msg_id, &parser);
}


uint8_t message_decode(uint8_t* buf_,uint8_t msg_id, parse_state_t* ps){
	
	switch (msg_id)
	{
		case 1:
		start = ps->start_idx;
		memcpy(&pack2,&buf_[ps->start_idx],sizeof(packet_t));
		memset(buf_,0,200);
		parser_init(ps);
		break;
		
		case 2:
		
		memset(buf,0,200);
		break;
		
		case 3:
		
		memset(buf,0,200);
		break;
		
	}
}


  1. message_decode is a weak function, the user can redefine this function and implement it.
  2. msg_id is an uint8_t-type variable. The slave gets a message when the paket_parser return a non-zero value (msg_id is not zero).
  3. msg_id is a unique value for each of message. The id value is devfined as xxx_ID in header file.

Structure of a Packet

The following figure shows the general structure of a packet. A packet consists of header, payload and checksum.

header:

header provides the basic information of a packet which includes: head : the start of packet which indicates the beginning of a packet. len : the length of following bytes. packet_type: the type of pack device_type: the type of device msg_id : reserved byte sub_id : reserved byte

payload:

Depends on packet type (i.e. packet_type).

checksum:

To check the validation of a packet.

About


Languages

Language:Python 91.3%Language:C 8.7%