DreamTeam/pastebin

From Noisebridge
Revision as of 23:31, 25 May 2013 by Bfb (talk | contribs)
Jump to navigation Jump to search

asynchronous serial stream of bytes

3 parts

header payload checksum

transport medium UART serial comm USB bluetooth and any mechanism which can stream bytes

packet begins with header then payload ends with payloads's checksum byte

payload section may be up to 169 bytes long

each of sync, plength, and checksum are 1 byte each header consists of 2 syncs and 1 plength bytes

so, possible packet

sync sync plength (1+1+1)
payload (169)
checksum (1)

so minumum 4 (? empty payload)

maximum 173

header

2 sync bytes signal beginning of new packet
0xaa

issue of 2 sync "special 0xaa"  appear in data
therefore plength and checksum are necessary
 to avoid in-band signal/data ambiguity mis-parsing
plength == payload length not packet length
so if larger than 169 then error
can be 0

payload

don't parse until verify checksum
checksum
 sum bytes
 take lowest 8 bits
 perform bit inverse
payload is series of data rows

data row format:

value type

there can be 0 or more data rows

each data row begins with

0 or more Extended Code Flag Byte (0x55)
 Extended Code Level (not yet implemented) given by ECFB count
1 Code bytes (which will never by 0x55/ECFB or 0xaa/sync)
0 or 1 VLength bytes (signalled by Code <= 0x7f)
1 or more Value bytes

<= 0x7f "single-byte code"

0x02 POOR_SIGNAL
0x04 ATTENTION
0x05 MEDITATION
0x16 BLINK EVENT
 unsigned 2s-complement

>= 0x80 "multi-byte code"

0x80 RAW Wave Value
 single 16-bit 2s-complement signed big-endian (-32768 to 32767)
0x
0x83 ASIC_EEG_POWER

"



...............

sync_flag_count = 0
valid_plength = False
valid_payload = False
plength = -1
have_checksum = False
payload_byte_count = 0
buffer_size = 256
payload_buffer = byte_array(buffer_size)

while (stream)

 get abyte
 if sync_flag_count < 2
   if abyte not sync
     (trace out-of-sync value, sync_flag_count)
     sync_flag_count = 0
     continue
   increment sync_flag_count 
   if sync_flag_count < 2
     continue
   (trace successful sync)
 if not valid_plength 
   plength = abyte
   valid_plength = test_plength(plength) (value from 0 to 170)
   if not valid_plength
     (trace not valid_plength)
     plength = -1
     continue
   (trace valid_plength)
 if not have_checksum
   checksum = abyte
   have_checksum = True
   (trace checksum)
   continue
 if payload_byte_count < plength
   payload_buffer.append(abyte)
   increment payload_byte_count
   trace(payload_buffer, payload_byte_count)
   continue
 if payload_byte_count > plength
   valid_payload = False
   (trace serious logic error)
   break
 valid_payload = test_payload(payload, plength, checksum) 
 if valid_payload
   (trace valid_payload)
   parse_payload(payload_buffer)
 else
   (trace not valid_payload)
 reset_flags(plength, checksum, etc)

state progression:

need_socket
receiving_signal

- loop

need_first_sync
need_second_sync
need_plength
need_checksum
need_payload
need_paysum

- loop

payload_remaining
need_datarow
need_codelevel
need_codetype
need_vlength
need_val
valid_data

-

valid_packet

-