Skip to content

Commit

Permalink
Emulator actual embodiment: enabling async packet receiving/packet pr…
Browse files Browse the repository at this point in the history
…ocessing on app level (testing environment)
  • Loading branch information
divanchykhin committed Apr 7, 2015
1 parent 426dd56 commit 23ff152
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 27 deletions.
35 changes: 31 additions & 4 deletions tests/emulator/sa_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ int main_loop()
uint8_t ret_code;

// test setup values
bool wait_for_incoming_chain_with_timer;
bool wait_for_incoming_chain_with_timer = false;
uint16_t wake_time_to_start_new_chain;

uint8_t wait_to_continue_processing = 0;
uint16_t wake_time_continue_processing;

// do necessary initialization
sagdp_init( data_buff + DADA_OFFSET_SAGDP );
SASP_initAtLifeStart( data_buff + DADA_OFFSET_SASP );
Expand Down Expand Up @@ -165,7 +168,7 @@ int main_loop()
goto saspsend;
break;
}
else if ( wait_for_incoming_chain_with_timer && getTime() >= wake_time )
else if ( wait_for_incoming_chain_with_timer && getTime() >= wake_time_to_start_new_chain )
{
INCREMENT_COUNTER( 91, "MAIN LOOP, waiting for incoming chain by timer done; starting own chain" );
wait_for_incoming_chain_with_timer = false;
Expand All @@ -174,6 +177,19 @@ int main_loop()
goto entry;
break;
}
else if ( wait_to_continue_processing && getTime() >= wake_time_continue_processing )
{
INCREMENT_COUNTER( 98, "MAIN LOOP, continuing processing" );
wait_to_continue_processing = 0;
#ifdef USED_AS_MASTER
ret_code = master_process_continue( MEMORY_HANDLE_MAIN_LOOP );
#else
ret_code = slave_process_continue( MEMORY_HANDLE_MAIN_LOOP );
#endif
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
goto entry;
break;
}
ret_code = tryGetMessage( MEMORY_HANDLE_MAIN_LOOP );
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
INCREMENT_COUNTER_IF( 97, "MAIN LOOP, packet received [2]", ret_code == COMMLAYER_RET_OK );
Expand Down Expand Up @@ -333,16 +349,20 @@ int main_loop()

processcmd:
// 4. Process received command (yoctovm)
ret_code = master_continue( MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4, stack, stackSize*/ );
ret_code = master_process( &wait_to_continue_processing, MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4, stack, stackSize*/ );
/* if ( ret_code == YOCTOVM_RESET_STACK )
{
sagdp_init( data_buff + DADA_OFFSET_SAGDP );
// TODO: reinit the rest of stack (where applicable)
ret_code = master_start( sizeInOut, rwBuff, rwBuff + BUF_SIZE / 4 );
}*/
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %d\n", ret_code, ugly_hook_get_request_size( MEMORY_HANDLE_MAIN_LOOP ), ugly_hook_get_response_size( MEMORY_HANDLE_MAIN_LOOP ) );
entry:
wait_for_incoming_chain_with_timer = false;
if ( ret_code == YOCTOVM_WAIT_TO_CONTINUE )
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %dwaiting to continue ...\n" );
else
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %d\n", ret_code, ugly_hook_get_request_size( MEMORY_HANDLE_MAIN_LOOP ), ugly_hook_get_response_size( MEMORY_HANDLE_MAIN_LOOP ) );

switch ( ret_code )
{
Expand Down Expand Up @@ -397,6 +417,13 @@ int main_loop()
}
break;
}
case YOCTOVM_WAIT_TO_CONTINUE:
{
if ( wait_to_continue_processing == 0 ) wait_to_continue_processing = 1;
wake_time_continue_processing = getTime() + wait_to_continue_processing;
goto getmsg;
break;
}
default:
{
// unexpected ret_code
Expand Down
32 changes: 30 additions & 2 deletions tests/emulator/sa_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ int main_loop()
bool wait_for_incoming_chain_with_timer;
uint16_t wake_time_to_start_new_chain;

uint8_t wait_to_continue_processing = 0;
uint16_t wake_time_continue_processing;

// do necessary initialization
sagdp_init( data_buff + DADA_OFFSET_SAGDP );

Expand Down Expand Up @@ -120,6 +123,19 @@ int main_loop()
goto alt_entry;
break;
}
else if ( wait_to_continue_processing && getTime() >= wake_time_continue_processing )
{
INCREMENT_COUNTER( 98, "MAIN LOOP, continuing processing" );
wait_to_continue_processing = 0;
#ifdef USED_AS_MASTER
ret_code = master_process_continue( MEMORY_HANDLE_MAIN_LOOP );
#else
ret_code = slave_process_continue( MEMORY_HANDLE_MAIN_LOOP );
#endif
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
goto entry;
break;
}
ret_code = tryGetMessage( MEMORY_HANDLE_MAIN_LOOP );
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
INCREMENT_COUNTER_IF( 92, "MAIN LOOP, packet received [2]", ret_code == COMMLAYER_RET_OK );
Expand Down Expand Up @@ -262,7 +278,7 @@ int main_loop()

processcmd:
// 4. Process received command (yoctovm)
ret_code = slave_process( MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4, stack, stackSize*/ );
ret_code = slave_process( &wait_to_continue_processing, MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4, stack, stackSize*/ );
/* if ( ret_code == YOCTOVM_RESET_STACK )
{
sagdp_init( data_buff + DADA_OFFSET_SAGDP );
Expand All @@ -271,7 +287,12 @@ int main_loop()
ret_code = master_start( sizeInOut, rwBuff, rwBuff + BUF_SIZE / 4 );
}*/
zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP );
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %d\n", ret_code, ugly_hook_get_request_size( MEMORY_HANDLE_MAIN_LOOP ), ugly_hook_get_response_size( MEMORY_HANDLE_MAIN_LOOP ) );
entry:
wait_for_incoming_chain_with_timer = false;
if ( ret_code == YOCTOVM_WAIT_TO_CONTINUE )
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %dwaiting to continue ...\n" );
else
printf( "YOCTO: ret: %d; rq_size: %d, rsp_size: %d\n", ret_code, ugly_hook_get_request_size( MEMORY_HANDLE_MAIN_LOOP ), ugly_hook_get_response_size( MEMORY_HANDLE_MAIN_LOOP ) );

switch ( ret_code )
{
Expand Down Expand Up @@ -328,6 +349,13 @@ int main_loop()
}
break;
}
case YOCTOVM_WAIT_TO_CONTINUE:
{
if ( wait_to_continue_processing == 0 ) wait_to_continue_processing = 1;
wake_time_continue_processing = getTime() + wait_to_continue_processing;
goto getmsg;
break;
}
default:
{
// unexpected ret_code
Expand Down
87 changes: 68 additions & 19 deletions tests/emulator/yoctovm_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ uint16_t currChainIdBase[2] = { 0, ( MASTER_SLAVE_BIT << 15 ) };
// End of Pure Testing Block


// currently processed chain
uint8_t first_byte;
uint16_t chain_id[2];
uint16_t chain_ini_size;
uint16_t reply_to_id;
uint16_t self_id;

uint8_t slave_process( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )


uint8_t slave_process( uint8_t* wait_to_process_time, REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )
{
// buffer assumes to contain an input message (including First Byte)
// FAKE structure of the buffer: First_Byte | fake_body (9-16 bytes)
Expand All @@ -51,26 +59,27 @@ uint8_t slave_process( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* s
// replying_to is a copy of self_id of the received packet (or 0 for the first packet ion the chain)
// rand_part is filled with some number of '-' ended by '>'

*wait_to_process_time = 0;

INCREMENT_COUNTER( 0, "slave_process(), packet received" );

// init parser object
parser_obj po;
zepto_parser_init( &po, mem_h );

uint16_t msg_size = zepto_parsing_remaining_bytes( &po ); // all these bytes + (potentially) {padding_size + padding} will be written
uint8_t first_byte = zepto_parse_uint8( &po );
first_byte = zepto_parse_uint8( &po );
if ( ( first_byte & ( SAGDP_P_STATUS_FIRST | SAGDP_P_STATUS_TERMINATING ) ) == SAGDP_P_STATUS_ERROR_MSG )
{
PRINTF( "slave_process(): ERROR MESSAGE RECEIVED IN YOCTO\n" );
assert(0);
}

uint16_t chain_id[2];
chain_id[0] = zepto_parse_encoded_uint16( &po );
chain_id[1] = zepto_parse_encoded_uint16( &po );
uint16_t chain_ini_size = zepto_parse_encoded_uint16( &po );
uint16_t reply_to_id = zepto_parse_encoded_uint16( &po );
uint16_t self_id = zepto_parse_encoded_uint16( &po );
chain_ini_size = zepto_parse_encoded_uint16( &po );
reply_to_id = zepto_parse_encoded_uint16( &po );
self_id = zepto_parse_encoded_uint16( &po );
char tail[256];
uint16_t tail_sz = zepto_parsing_remaining_bytes( &po );
zepto_parse_read_block( &po, (uint8_t*)tail, tail_sz );
Expand Down Expand Up @@ -120,20 +129,40 @@ uint8_t slave_process( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* s
self_id++;
last_sent_id = self_id;

// scenario decision
if ( 1 )
{
// just go through
*wait_to_process_time = 0;
return slave_process_continue( mem_h );
}
else
{
// request to wait
*wait_to_process_time = 0;
return YOCTOVM_WAIT_TO_CONTINUE;
}
}

uint8_t slave_process_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )
{
zepto_response_to_request( mem_h );

zepto_write_uint8( mem_h, first_byte );
zepto_write_encoded_uint16( mem_h, chain_id[0] );
zepto_write_encoded_uint16( mem_h, chain_id[1] );
zepto_write_encoded_uint16( mem_h, chain_ini_size );
zepto_write_encoded_uint16( mem_h, reply_to_id );
zepto_write_encoded_uint16( mem_h, self_id );

char tail[256];
uint16_t varln = 6 - self_id % 7; // 0:6
for ( uint8_t i=0;i<varln; i++ )
tail[ i] = '-';
tail[ varln ] = '>';
tail[ varln + 1 ] = 0;
zepto_write_block( mem_h, (uint8_t*)tail, varln + 1 );
msg_size = 11+varln+1;
uint16_t msg_size = 11+varln+1;

// print outgoing packet
PRINTF( "Yocto: Packet sent : [%d bytes] [%d][0x%04x][0x%04x][0x%04x][0x%04x][0x%04x]%s\n", msg_size, first_byte, chain_id[0], chain_id[1], chain_ini_size, reply_to_id, self_id, tail );
Expand Down Expand Up @@ -161,7 +190,7 @@ uint8_t master_start( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* st
// Initial number of packets in the chain is currently entered manually by a tester
PRINT_COUNTERS();

uint8_t first_byte = SAGDP_P_STATUS_FIRST;
first_byte = SAGDP_P_STATUS_FIRST;

#ifdef MANUAL_TEST_DATA_ENTERING

Expand All @@ -175,17 +204,16 @@ uint8_t master_start( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* st

#else // MANUAL_TEST_DATA_ENTERING

uint16_t chain_ini_size = tester_get_rand_val() % ( CHAIN_MAX_SIZE - 2 ) + 2;
chain_ini_size = tester_get_rand_val() % ( CHAIN_MAX_SIZE - 2 ) + 2;

#endif // MANUAL_TEST_DATA_ENTERING

currChainID[0] = ++( currChainIdBase[0] );
currChainID[1] = currChainIdBase[1];
uint16_t chain_id[2];
chain_id[0] = currChainID[0];
chain_id[1] = currChainID[1];
uint16_t reply_to_id = 0;
uint16_t self_id = 1;
reply_to_id = 0;
self_id = 1;
last_sent_id = self_id;

// prepare outgoing packet
Expand Down Expand Up @@ -217,9 +245,10 @@ uint8_t master_start( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* st
return YOCTOVM_PASS_LOWER;
}

uint8_t master_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )
uint8_t master_process( uint8_t* wait_to_process_time, REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )
{
// by now master_continue() does the same as yocto_process
*wait_to_process_time = 0;

INCREMENT_COUNTER( 4, "master_continue(), packet received" );

Expand All @@ -228,7 +257,7 @@ uint8_t master_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t*
zepto_parser_init( &po, mem_h );

uint16_t msg_size = zepto_parsing_remaining_bytes( &po ); // all these bytes + (potentially) {padding_size + padding} will be written
uint8_t first_byte = zepto_parse_uint8( &po );
first_byte = zepto_parse_uint8( &po );
if ( ( first_byte & ( SAGDP_P_STATUS_FIRST | SAGDP_P_STATUS_TERMINATING ) ) == SAGDP_P_STATUS_ERROR_MSG )
{
PRINTF( "master_continue(): ERROR MESSAGE RECEIVED IN YOCTO\n" );
Expand All @@ -238,12 +267,11 @@ uint8_t master_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t*
assert(0);
}

uint16_t chain_id[2];
chain_id[0] = zepto_parse_encoded_uint16( &po );
chain_id[1] = zepto_parse_encoded_uint16( &po );
uint16_t chain_ini_size = zepto_parse_encoded_uint16( &po );
uint16_t reply_to_id = zepto_parse_encoded_uint16( &po );
uint16_t self_id = zepto_parse_encoded_uint16( &po );
chain_ini_size = zepto_parse_encoded_uint16( &po );
reply_to_id = zepto_parse_encoded_uint16( &po );
self_id = zepto_parse_encoded_uint16( &po );
char tail[256];
uint16_t tail_sz = zepto_parsing_remaining_bytes( &po );
zepto_parse_read_block( &po, (uint8_t*)tail, tail_sz );
Expand Down Expand Up @@ -296,20 +324,41 @@ uint8_t master_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t*
self_id++;
last_sent_id = self_id;


// scenario decision
if ( 1 )
{
// just go through
*wait_to_process_time = 0;
return master_process_continue( mem_h );
}
else
{
// request to wait
*wait_to_process_time = 0;
return YOCTOVM_WAIT_TO_CONTINUE;
}
}

uint8_t master_process_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ )
{
zepto_response_to_request( mem_h );

zepto_write_uint8( mem_h, first_byte );
zepto_write_encoded_uint16( mem_h, chain_id[0] );
zepto_write_encoded_uint16( mem_h, chain_id[1] );
zepto_write_encoded_uint16( mem_h, chain_ini_size );
zepto_write_encoded_uint16( mem_h, reply_to_id );
zepto_write_encoded_uint16( mem_h, self_id );

char tail[256];
uint16_t varln = 6 - self_id % 7; // 0:6
for ( uint8_t i=0;i<varln; i++ )
tail[ i] = '-';
tail[ varln ] = '>';
tail[ varln + 1 ] = 0;
zepto_write_block( mem_h, (uint8_t*)tail, varln + 1 );
msg_size = 11+varln+1;
uint16_t msg_size = 11+varln+1;

// print outgoing packet
PRINTF( "Yocto: Packet sent : [%d bytes] [%d][0x%04x][0x%04x][0x%04x][0x%04x][0x%04x]%s\n", msg_size, first_byte, chain_id[0], chain_id[1], chain_ini_size, reply_to_id, self_id, tail );
Expand Down
7 changes: 5 additions & 2 deletions tests/emulator/yoctovm_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ Copyright (C) 2015 OLogN Technologies AG
#define YOCTOVM_OK 1 // if terminating packet received
#define YOCTOVM_PASS_LOWER 2
#define YOCTOVM_PASS_LOWER_THEN_IDLE 3 // ret code for testing; same as YOCTOVM_PASS_LOWER but notifies main loop that the chain is over
#define YOCTOVM_WAIT_TO_CONTINUE 4
//#define YOCTOVM_RESET_STACK 4

uint8_t slave_process( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t slave_process( uint8_t* wait_to_process_time, REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t slave_process_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t master_start( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t master_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t master_process( uint8_t* wait_to_process_time, REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t master_process_continue( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );
uint8_t master_error( REQUEST_REPLY_HANDLE mem_h/*, int buffOutSize, uint8_t* stack, int stackSize*/ );

// Pure Testing Block
Expand Down

0 comments on commit 23ff152

Please sign in to comment.