diff --git a/firmware/src/common/sa_common.h b/firmware/src/common/sa_common.h index c07aa5f..f2eb804 100644 --- a/firmware/src/common/sa_common.h +++ b/firmware/src/common/sa_common.h @@ -117,8 +117,10 @@ INLINE void zepto_memcpy( void* dest, const void* src, uint16_t cnt ) // debug helpers #ifdef SA_DEBUG +#if !defined DEBUG_PRINTING #define DEBUG_PRINTING #endif +#endif #ifdef DEBUG_PRINTING #include diff --git a/firmware/src/common/sagdp_protocol.c b/firmware/src/common/sagdp_protocol.c index af541b5..c05f50e 100644 --- a/firmware/src/common/sagdp_protocol.c +++ b/firmware/src/common/sagdp_protocol.c @@ -786,6 +786,7 @@ uint8_t handler_sagdp_receive_up( sa_time_val* currt, waiting_for* wf, sasp_nonc { // if ACK is requested, send it ZEPTO_DEBUG_ASSERT(0); + return SAGDP_RET_SYS_CORRUPTED; // just as long as not implemented } else { @@ -819,6 +820,7 @@ uint8_t handler_sagdp_receive_up( sa_time_val* currt, waiting_for* wf, sasp_nonc { // if ACK is requested, send it ZEPTO_DEBUG_ASSERT(0); + return SAGDP_RET_SYS_CORRUPTED; // just as long as not implemented } else { diff --git a/firmware/src/hal/hal_commlayer.h b/firmware/src/hal/hal_commlayer.h index 48bb4f4..7470fc1 100755 --- a/firmware/src/hal/hal_commlayer.h +++ b/firmware/src/hal/hal_commlayer.h @@ -43,13 +43,10 @@ uint8_t send_message( MEMORY_HANDLE mem_h ); uint8_t try_get_message( MEMORY_HANDLE mem_h ); void keep_transmitter_on( bool keep_on ); -#ifdef __cplusplus -} -#endif - //uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ); -//uint8_t wait_for_communication_event( uint16_t timeout ); #ifdef USED_AS_MASTER +uint8_t wait_for_communication_event( unsigned int timeout ); +uint8_t try_get_message_within_master( MEMORY_HANDLE mem_h ); #ifdef USED_AS_MASTER_COMMSTACK uint8_t send_to_central_unit( MEMORY_HANDLE mem_h ); #elif defined USED_AS_MASTER_CORE @@ -59,6 +56,10 @@ uint8_t send_to_commm_stack( MEMORY_HANDLE mem_h ); #endif #endif +#ifdef __cplusplus +} +#endif + #endif // __SA_COMMLAYER_H__ \ No newline at end of file diff --git a/firmware/src/hal/platforms/desktop/hal_commlayer.c b/firmware/src/hal/platforms/desktop/hal_commlayer.c index ed1b4f9..9c8db06 100644 --- a/firmware/src/hal/platforms/desktop/hal_commlayer.c +++ b/firmware/src/hal/platforms/desktop/hal_commlayer.c @@ -215,9 +215,14 @@ uint8_t try_get_message( MEMORY_HANDLE mem_h ) #if (defined USED_AS_MASTER) && ( (defined USED_AS_MASTER_COMMSTACK) || (defined USED_AS_MASTER_CORE) ) -const char* inet_addr_as_string_with_cl = "127.0.0.1"; +#ifdef _MSC_VER +SOCKET sock_with_cl; +SOCKET sock_with_cl_accepted; +#else int sock_with_cl; int sock_with_cl_accepted; +#endif +const char* inet_addr_as_string_with_cl = "127.0.0.1"; struct sockaddr_in sa_self_with_cl, sa_other_with_cl; #ifdef USED_AS_MASTER_COMMSTACK @@ -524,10 +529,10 @@ uint8_t wait_for_communication_event( unsigned int timeout ) #ifdef USED_AS_MASTER_COMMSTACK FD_SET(sock, &rfds); FD_SET(sock_with_cl, &rfds); - fd_cnt = sock > sock_with_cl ? sock + 1 : sock_with_cl + 1; + fd_cnt = (int)(sock > sock_with_cl ? sock + 1 : sock_with_cl + 1); #else FD_SET(sock_with_cl, &rfds); - fd_cnt = sock_with_cl + 1; + fd_cnt = (int)(sock_with_cl + 1); #endif #else FD_SET(sock, &rfds); diff --git a/tests/emulator/sa_client.c b/tests/emulator/sa_client.c deleted file mode 100644 index 2c7234b..0000000 --- a/tests/emulator/sa_client.c +++ /dev/null @@ -1,707 +0,0 @@ -/******************************************************************************* -Copyright (C) 2015 OLogN Technologies AG - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*******************************************************************************/ - - -#define MODEL_IN_EFFECT 1 -//#define MODEL_IN_EFFECT 2 - - -#include "sa_common.h" -#include "sa_commlayer.h" -#include "sa_hal_time_provider.h" -#include "sa_timer.h" -#include "saoudp_protocol.h" -#include "sasp_protocol.h" -#include "sagdp_protocol.h" -#if MODEL_IN_EFFECT == 1 -#include "yoctovm_protocol.h" -#elif MODEL_IN_EFFECT == 2 -#include "saccp_protocol.h" -#include "sa_test_control_prog.h" -#else -#error #error Unexpected value of MODEL_IN_EFFECT -#endif -#include "test_generator.h" -#include - - -uint8_t pid[ SASP_NONCE_SIZE ]; -uint8_t nonce[ SASP_NONCE_SIZE ]; - - -int8_t main_loop() -{ -#ifdef ENABLE_COUNTER_SYSTEM - INIT_COUNTER_SYSTEM -#endif // ENABLE_COUNTER_SYSTEM - - - printf("starting CLIENT...\n"); - printf("==================\n\n"); - - tester_initTestSystem(); -/* bool run_simultaniously = true; - if ( run_simultaniously ) - { - requestSyncExec(); - }*/ - - // TODO: actual key loading, etc - uint8_t sasp_key[16]; - memcpy( sasp_key, "16-byte fake key", 16 ); - - timeout_action tact; - tact.action = 0; - - // in this preliminary implementation all memory segments are kept separately - // All memory objects are listed below - // TODO: revise memory management - uint8_t timer_val = 0; - uint16_t wake_time; - // TODO: revise time/timer management - -#if MODEL_IN_EFFECT == 2 - DefaultTestingControlProgramState control_prog_state; - default_test_control_program_init( &control_prog_state ); -#endif - - uint8_t ret_code; - - // test setup values - bool wait_for_incoming_chain_with_timer = false; - uint16_t wake_time_to_start_new_chain = 0; - - uint8_t wait_to_continue_processing = 0; - uint16_t wake_time_continue_processing; - - // do necessary initialization - SAGDP_DATA sagdp_data; - sagdp_init( &sagdp_data ); - SASP_DATA sasp_data; - sasp_init_at_lifestart( &sasp_data ); - - - - -#if MODEL_IN_EFFECT == 1 - ret_code = master_start( MEMORY_HANDLE_MAIN_LOOP ); -#elif MODEL_IN_EFFECT == 2 - ret_code = default_test_control_program_start_new( &control_prog_state, MEMORY_HANDLE_MAIN_LOOP ); -#else -#error #error Unexpected value of MODEL_IN_EFFECT -#endif - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto entry; - - // MAIN LOOP - for (;;) - { -getmsg: -#if MODEL_IN_EFFECT == 1 - if ( wait_to_continue_processing && getTime() >= wake_time_continue_processing ) - { -printf( "Processing continued...\n" ); - 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; - } -#elif MODEL_IN_EFFECT == 2 -#else -#error #error Unexpected value of MODEL_IN_EFFECT -#endif - - // 1. Get message from comm peer - printf("Waiting for a packet from server...\n"); -// ret_code = getMessage( sizeInOut, rwBuff, BUF_SIZE); - if ( tester_shouldInsertIncomingPacket( MEMORY_HANDLE_MAIN_LOOP ) ) - { - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - INCREMENT_COUNTER( 87, "MAIN LOOP, incoming packet inserted" ); - ret_code = COMMLAYER_RET_OK; - } - else - { - ret_code = try_get_message( MEMORY_HANDLE_MAIN_LOOP ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - INCREMENT_COUNTER_IF( 96, "MAIN LOOP, packet received [1]", ret_code == COMMLAYER_RET_OK ); - } - - // sync sending/receiving - if ( ret_code == COMMLAYER_RET_OK ) - { - if ( tester_shouldDropIncomingPacket() ) - { - INCREMENT_COUNTER( 88, "MAIN LOOP, incoming packet dropped [1]" ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto getmsg; - } - - if ( tester_releaseOutgoingPacket( MEMORY_HANDLE_TEST_SUPPORT ) ) // we are on the safe side here as buffer out is not yet used - { - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - INCREMENT_COUNTER( 89, "MAIN LOOP, packet sent sync with receiving [1]" ); - send_message( MEMORY_HANDLE_TEST_SUPPORT ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - assert( ugly_hook_get_request_size( MEMORY_HANDLE_TEST_SUPPORT ) == 0 && ugly_hook_get_response_size( MEMORY_HANDLE_TEST_SUPPORT ) == 0 ); - } - } - - while ( ret_code == COMMLAYER_RET_PENDING ) - { - waitForTimeQuantum(); -#if MODEL_IN_EFFECT == 1 - if ( wait_to_continue_processing && getTime() >= wake_time_continue_processing ) - { -printf( "Processing continued...\n" ); - 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; - } -#elif MODEL_IN_EFFECT == 2 -#else -#error #error Unexpected value of MODEL_IN_EFFECT -#endif -// if ( timer_val != 0 && getTime() >= wake_time ) - if ( sagdp_data.event_type ) //TODO: temporary solution - { - printf( "no reply received; the last message (if any) will be resent by timer\n" ); - // TODO: to think: why do we use here handler_sagdp_receive_request_resend_lsp() and not handlerSAGDP_timer() - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_timer( &tact, NULL, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - if ( ret_code == SAGDP_RET_OK ) - { - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto trygetmsg; - } -// wake_time = 0; - else if ( ret_code == SAGDP_RET_NEED_NONCE ) - { - ret_code = handler_sasp_get_packet_id( nonce, SASP_NONCE_SIZE, &sasp_data ); - assert( ret_code == SASP_RET_NONCE ); - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_timer( &tact, nonce, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); -printf( "ret_code = %d\n", ret_code ); - assert( ret_code != SAGDP_RET_NEED_NONCE && ret_code != SAGDP_RET_OK ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - // sync sending/receiving: release presently hold packet (if any), and cause this packet to be hold - { - uint16_t sz; - if ( tester_releaseOutgoingPacket( MEMORY_HANDLE_TEST_SUPPORT ) ) // we are on the safe side here as buffer out is not yet used - { - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - send_message( MEMORY_HANDLE_TEST_SUPPORT ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - tester_requestHoldingPacket(); - INCREMENT_COUNTER( 90, "MAIN LOOP, packet sent sync with receiving [2] (because of timer)" ); - } - } - goto saspsend; - break; - } - else - { - printf( "ret_code = %d\n", ret_code ); - assert( 0 ); - } - } - 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; -#if MODEL_IN_EFFECT == 1 - ret_code = master_start( MEMORY_HANDLE_MAIN_LOOP ); -#elif MODEL_IN_EFFECT == 2 -#else -#error #error Unexpected value of MODEL_IN_EFFECT -#endif - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto entry; - break; - } -trygetmsg: - ret_code = try_get_message( 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 ); - assert( ret_code != COMMLAYER_RET_OK || ugly_hook_get_request_size( MEMORY_HANDLE_MAIN_LOOP ) != 0 ); - - // sync sending/receiving - if ( ret_code == COMMLAYER_RET_OK ) - { -/*+*/ if ( tester_releaseOutgoingPacket( MEMORY_HANDLE_TEST_SUPPORT ) ) // we are on the safe side here as buffer out is not yet used - { - INCREMENT_COUNTER( 93, "MAIN LOOP, packet sent sync with receiving [3]" ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - send_message( MEMORY_HANDLE_TEST_SUPPORT ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - }/*+*/ - - if ( tester_shouldDropIncomingPacket() ) - { - INCREMENT_COUNTER( 92, "MAIN LOOP, incoming packet dropped [2]" ); - goto getmsg; - } - } - } - if ( ret_code != COMMLAYER_RET_OK ) - { - printf("\n\nWAITING FOR ESTABLISHING COMMUNICATION WITH SERVER...\n\n"); - if (!communication_initialize()) // regardles of errors... quick and dirty solution so far - return -1; - goto getmsg; - } - - tester_registerIncomingPacket( MEMORY_HANDLE_MAIN_LOOP ); - printf("Message from server received\n"); - printf( "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 ) ); - - - // 2.1. Pass to SAoUDP - ret_code = handler_saoudp_receive( MEMORY_HANDLE_MAIN_LOOP ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - - switch ( ret_code ) - { - case SAOUDP_RET_OK: - { - // regular processing will be done below in the next block - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - - // 2.2. Pass to SASP - ret_code = handler_sasp_receive( sasp_key, pid, MEMORY_HANDLE_MAIN_LOOP, &sasp_data ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - printf( "SASP1: 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 ) - { - case SASP_RET_IGNORE: - { - printf( "BAD MESSAGE_RECEIVED\n" ); - goto getmsg; - break; - } - case SASP_RET_TO_LOWER_ERROR: - { - goto saoudp_send; - break; - } - case SASP_RET_TO_HIGHER_NEW: - { - // regular processing will be done below in the next block - break; - } -/* case SASP_RET_TO_HIGHER_REPEATED: - { - printf( "NONCE_LAST_SENT has been reset; the last message (if any) will be resent\n" ); - ret_code = handlerSAGDP_receiveRepeatedUP( &tact, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data, msgLastSent ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto saspsend; - break; - }*/ - case SASP_RET_TO_HIGHER_LAST_SEND_FAILED: - { - printf( "NONCE_LAST_SENT has been reset; the last message (if any) will be resent\n" ); - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_request_resend_lsp( &tact, NULL, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - if ( ret_code == SAGDP_RET_TO_LOWER_NONE ) - { - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - continue; - } - else if ( ret_code == SAGDP_RET_NEED_NONCE ) - { - ret_code = handler_sasp_get_packet_id( nonce, SASP_NONCE_SIZE, &sasp_data ); - assert( ret_code == SASP_RET_NONCE ); - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_request_resend_lsp( &tact, nonce, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - assert( ret_code != SAGDP_RET_NEED_NONCE && ret_code != SAGDP_RET_TO_LOWER_NONE ); - } - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - goto saspsend; - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - - // 3. pass to SAGDP a new packet - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_up( &tact, NULL, pid, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - if ( ret_code == SAGDP_RET_NEED_NONCE ) - { - ret_code = handler_sasp_get_packet_id( nonce, SASP_NONCE_SIZE, &sasp_data ); - assert( ret_code == SASP_RET_NONCE ); - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_up( &tact, nonce, pid, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - assert( ret_code != SAGDP_RET_NEED_NONCE ); - } - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - printf( "SAGDP1: 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 ) - { -#ifdef USED_AS_MASTER - case SAGDP_RET_OK: - { - printf( "master received unexpected packet. ignored\n" ); - goto getmsg; - break; - } -#else - case SAGDP_RET_SYS_CORRUPTED: - { - // TODO: reinitialize all - goto getmsg; - break; - } -#endif - case SAGDP_RET_TO_HIGHER: - { - // regular processing will be done below, but we need to jump over - break; - } -#if 0 - case SAGDP_RET_TO_HIGHER_ERROR: - { - sagdp_init( &sagdp_data ); - // TODO: reinit the rest of stack (where applicable) -#if MODEL_IN_EFFECT == 1 - ret_code = master_error( MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4*/ ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); -#elif MODEL_IN_EFFECT == 2 -#else -#error Unexpected value of MODEL_IN_EFFECT -#endif - goto entry; - break; - } -#endif // 0 - case SAGDP_RET_TO_LOWER_REPEATED: - { - goto saspsend; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - -#if MODEL_IN_EFFECT == 1 - -processcmd: - // 4. Process received command (yoctovm) - ret_code = master_process( &wait_to_continue_processing, MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4*/ ); -/* if ( ret_code == YOCTOVM_RESET_STACK ) - { - sagdp_init( &sagdp_data ); - // 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 ); -entry: - wait_for_incoming_chain_with_timer = false; - if ( ret_code == YOCTOVM_WAIT_TO_CONTINUE ) - printf( "YOCTO: ret: %d; waiting to continue ...\n", ret_code ); - 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 ) - { - case YOCTOVM_PASS_LOWER: - { - // test generation: sometimes master can start a new chain at not in-chain reason - bool restart_chain = tester_get_rand_val() % 8 == 0; - if ( restart_chain ) - { - sagdp_init( &sagdp_data ); - ret_code = master_start( MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4*/ ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - assert( ret_code == YOCTOVM_PASS_LOWER ); - } - // regular processing will be done below in the next block - break; - } - case YOCTOVM_PASS_LOWER_THEN_IDLE: - { - bool start_now = tester_get_rand_val() % 3 == 0; - wake_time_to_start_new_chain = start_now ? getTime() : getTime() + tester_get_rand_val() % 8; - wait_for_incoming_chain_with_timer = true; - break; - } - case YOCTOVM_FAILED: - sagdp_init( &sagdp_data ); - // NOTE: no 'break' is here as the rest is the same as for YOCTOVM_OK - case YOCTOVM_OK: - { - // here, in general, two main options are present: - // (1) to start a new chain immediately, or - // (2) to wait, during certain period of time, for an incoming chain, and then, if no packet is received, to start a new chain -// bool start_now = get_rand_val() % 3 == 0; - bool start_now = true; - if ( start_now ) - { - PRINTF( " === YOCTOVM_OK, forced chain restart ===\n" ); - ret_code = master_start( MEMORY_HANDLE_MAIN_LOOP/*, BUF_SIZE / 4*/ ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - assert( ret_code == YOCTOVM_PASS_LOWER ); - // one more trick: wait for some time to ensure that slave will start its own chain, and then send "our own" chain start -/* bool mutual = get_rand_val() % 5 == 0; - if ( mutual ) - justWait( 5 );*/ - } - else - { - PRINTF( " === YOCTOVM_OK, delayed chain restart ===\n" ); - wake_time_to_start_new_chain = getTime() + tester_get_rand_val() % 8; - wait_for_incoming_chain_with_timer = true; - goto getmsg; - } - 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; -printf( "Processing in progress... (period = %d, time = %d)\n", wait_to_continue_processing, wake_time_continue_processing ); - goto getmsg; - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - -#elif MODEL_IN_EFFECT == 2 - -processcmd: - // 4. Process received command (yoctovm) - ret_code = handler_saccp_receive( MEMORY_HANDLE_MAIN_LOOP, /*sasp_nonce_type chain_id*/NULL, &control_prog_state ); //master_process( &wait_to_continue_processing, MEMORY_HANDLE_MAIN_LOOP ); -/* if ( ret_code == YOCTOVM_RESET_STACK ) - { - sagdp_init( &sagdp_data ); - // 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 ); - switch ( ret_code ) - { - case SACCP_RET_CHAIN_DONE: - { - ret_code = handler_sacpp_start_new_chain( MEMORY_HANDLE_MAIN_LOOP, &control_prog_state ); - break; - } - case SACCP_RET_CHAIN_CONTINUED: - { - ret_code = handler_sacpp_continue_chain( MEMORY_HANDLE_MAIN_LOOP, &control_prog_state ); - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); -entry: - -#else -#error Unexpected value of MODEL_IN_EFFECT -#endif - - // 5. SAGDP - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_hlp( &tact, NULL, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - if ( ret_code == SAGDP_RET_NEED_NONCE ) - { - ret_code = handler_sasp_get_packet_id( nonce, SASP_NONCE_SIZE, &sasp_data ); - assert( ret_code == SASP_RET_NONCE ); - sa_get_time( &(tact.tv) ); tact.action = 0; - ret_code = handler_sagdp_receive_hlp( &tact, nonce, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); - assert( ret_code != SAGDP_RET_NEED_NONCE ); - } - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - printf( "SAGDP2: 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 ) - { - case SAGDP_RET_SYS_CORRUPTED: - { - // TODO: reinit the system - PRINTF( "Internal error. System is to be reinitialized\n" ); - break; - } - case SAGDP_RET_TO_LOWER_NEW: - { - // regular processing will be done below in the next block - wake_time = getTime() + timer_val; - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - - // SASP -saspsend: - ret_code = handler_sasp_send( sasp_key, nonce, MEMORY_HANDLE_MAIN_LOOP, &sasp_data ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - printf( "SASP2: 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 ) - { - case SASP_RET_TO_LOWER_REGULAR: - { - // regular processing will be done below in the next block - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - - // SAoUDP -saoudp_send: - ret_code = handler_saoudp_send( MEMORY_HANDLE_MAIN_LOOP ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - - switch ( ret_code ) - { - case SAOUDP_RET_OK: - { - // regular processing will be done below in the next block - break; - } - default: - { - // unexpected ret_code - printf( "Unexpected ret_code %d\n", ret_code ); - assert( 0 ); - break; - } - } - -// allowSyncExec(); - tester_registerOutgoingPacket( MEMORY_HANDLE_MAIN_LOOP ); - - bool syncSendReceive; - bool is_packet_to_send = true; - if ( tester_holdPacketOnRequest( MEMORY_HANDLE_MAIN_LOOP ) ) - { - INCREMENT_COUNTER( 95, "MAIN LOOP, holdPacketOnRequest() called" ); - syncSendReceive = false; - is_packet_to_send = false; - } - else - syncSendReceive = tester_get_rand_val() % 4 == 0 && !tester_isOutgoingPacketOnHold(); - - if ( syncSendReceive ) - { - INCREMENT_COUNTER( 94, "MAIN LOOP, holdOutgoingPacket() called" ); - tester_holdOutgoingPacket( MEMORY_HANDLE_MAIN_LOOP ); - } - else - { - if ( tester_shouldInsertOutgoingPacket( MEMORY_HANDLE_TEST_SUPPORT ) ) - { - INCREMENT_COUNTER( 80, "MAIN LOOP, packet inserted" ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - send_message( MEMORY_HANDLE_TEST_SUPPORT ); - zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); - } - - if ( !tester_shouldDropOutgoingPacket() ) - { - if ( is_packet_to_send ) - { - ret_code = send_message( MEMORY_HANDLE_MAIN_LOOP ); - zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); - if (ret_code != COMMLAYER_RET_OK ) - { - return -1; - } - INCREMENT_COUNTER( 82, "MAIN LOOP, packet sent" ); - printf("\nMessage sent to comm peer\n"); - } - } - else - { - INCREMENT_COUNTER( 81, "MAIN LOOP, packet dropped" ); - printf("\nMessage lost on the way...\n"); - } - } - - } - - return 0; -} - -int main(int argc, char *argv[]) -{ - zepto_mem_man_init_memory_management(); - if ( !communication_initialize() ) - return -1; - - int8_t ret = main_loop(); - - communication_terminate(); - - return ret; -} diff --git a/tests/emulator/sa_client_central_unit.c b/tests/emulator/sa_client_central_unit.c index 149d25d..718b128 100644 --- a/tests/emulator/sa_client_central_unit.c +++ b/tests/emulator/sa_client_central_unit.c @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -35,7 +35,7 @@ int main_loop() INIT_COUNTER_SYSTEM #endif // ENABLE_COUNTER_SYSTEM - + ZEPTO_DEBUG_PRINTF_1("starting CLIENT...\n"); ZEPTO_DEBUG_PRINTF_1("==================\n\n"); @@ -45,11 +45,11 @@ int main_loop() uint8_t ret_code; // test setup values - bool wait_for_incoming_chain_with_timer = false; - uint16_t wake_time_to_start_new_chain; +// 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; +// uint16_t wake_time_continue_processing; // Try to initialize connection @@ -78,7 +78,7 @@ int main_loop() ZEPTO_DEBUG_PRINTF_3( "%c [0x%x]\n", bu[k], bu[k] ); return 0;*/ #else - uint8_t buff_base[] = {0x2, 0x0, 0x8, 0x1, 0x1, 0x2, 0x0, 0x1, '-', '-', '>' }; + uint8_t buff_base[] = {0x2, 0x0, 0x8, 0x1, 0x1, 0x2, 0x0, 0x1, '-', '-', '>' }; uint8_t buff[128]; buff[0] = 1; // first in the chain memcpy( buff+1, buff_base, sizeof(buff_base) ); diff --git a/tests/emulator/sa_client_comm_stack.c b/tests/emulator/sa_client_comm_stack.c index 21aa0b7..c6e9a27 100644 --- a/tests/emulator/sa_client_comm_stack.c +++ b/tests/emulator/sa_client_comm_stack.c @@ -64,8 +64,8 @@ int main_loop() uint8_t ret_code; - uint8_t wait_to_continue_processing = 0; - uint16_t wake_time_continue_processing; +// uint8_t wait_to_continue_processing = 0; +// uint16_t wake_time_continue_processing; // do necessary initialization /* SAGDP_DATA sagdp_data; @@ -451,7 +451,6 @@ int main_loop() } } - sendmsg: tester_registerOutgoingPacket( MEMORY_HANDLE_MAIN_LOOP ); bool syncSendReceive; diff --git a/tests/emulator/sa_test_control_prog.c b/tests/emulator/sa_test_control_prog.c index c9ce051..d6b35d7 100644 --- a/tests/emulator/sa_test_control_prog.c +++ b/tests/emulator/sa_test_control_prog.c @@ -20,7 +20,7 @@ Copyright (C) 2015 OLogN Technologies AG #include "../../firmware/src/common/sagdp_protocol.h" // for packet flags #include "../../firmware/src/common/saccp_protocol_constants.h" -#include "test-generator.h" +#include "test_generator.h" #include // for sprintf() in fake implementation #define CHAIN_MAX_SIZE 9 @@ -79,9 +79,10 @@ uint8_t default_test_control_program_start_new( void* control_prog_state, MEMORY zepto_parser_encode_and_append_uint16( reply, ps->chain_ini_size ); zepto_parser_encode_and_append_uint16( reply, ps->reply_to_id ); zepto_parser_encode_and_append_uint16( reply, ps->self_id ); + uint8_t varln = 6 - ps->self_id % 7; // 0:6 + zepto_write_uint8( reply, varln + 1 ); char tail[256]; - uint16_t varln = 6 - ps->self_id % 7; // 0:6 uint8_t i; for ( i=0;ichain_ini_size ); zepto_parser_encode_and_append_uint16( reply, ps->reply_to_id ); zepto_parser_encode_and_append_uint16( reply, ps->self_id ); + uint8_t varln = 6 - ps->self_id % 7; // 0:6 + zepto_write_uint8( reply, varln + 1 ); char tail[256]; - uint16_t varln = 6 - ps->self_id % 7; // 0:6 uint8_t i; for ( i=0;ireply_to_id = zepto_parse_encoded_uint16( received ); ps->self_id = zepto_parse_encoded_uint16( received ); char tail[256]; - uint16_t tail_sz = zepto_parsing_remaining_bytes( received ); +// uint16_t tail_sz = zepto_parsing_remaining_bytes( received ); + uint8_t tail_sz = zepto_parse_uint8( received ); zepto_parse_read_block( received, (uint8_t*)tail, tail_sz ); tail[ tail_sz ] = 0; diff --git a/tests/emulator/sa_test_control_prog.h b/tests/emulator/sa_test_control_prog.h index 6508d3d..a62302e 100644 --- a/tests/emulator/sa_test_control_prog.h +++ b/tests/emulator/sa_test_control_prog.h @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -30,7 +30,7 @@ Copyright (C) 2015 OLogN Technologies AG #define CONTROL_PROG_PASS_LOWER_THEN_IDLE 4 /*#define CONTROL_PROG_WAIT_TO_CONTINUE 4*/ -typedef struct _DefaultTestingControlProgramState +typedef struct _DefaultTestingControlProgramState { uint8_t state; //'0' means 'be ready to process incoming command', '1' means 'prepare reply' uint16_t last_sent_id; @@ -44,9 +44,9 @@ typedef struct _DefaultTestingControlProgramState } DefaultTestingControlProgramState; uint8_t default_test_control_program_init( void* control_prog_state ); -uint8_t default_test_control_program_accept_reply( void* control_prog_state, uint8_t packet_status, parser_obj* received, uint16_t sz ); +//uint8_t default_test_control_program_accept_reply( void* control_prog_state, uint8_t packet_status, parser_obj* received, uint16_t sz ); uint8_t default_test_control_program_accept_reply_continue( void* control_prog_state, MEMORY_HANDLE reply ); -//uint8_t default_test_control_program_start_new( void* control_prog_state, MEMORY_HANDLE reply ); +uint8_t default_test_control_program_start_new( void* control_prog_state, MEMORY_HANDLE reply ); uint8_t default_test_control_program_accept_reply( MEMORY_HANDLE mem_h, sasp_nonce_type chain_id, void* control_prog_state ); #endif // __SA_TEST_CONTROL_PROG_H__ diff --git a/tests/emulator/sa_unit_tests_aes_eax.c b/tests/emulator/sa_unit_tests_aes_eax.c index c8fc29b..319733c 100644 --- a/tests/emulator/sa_unit_tests_aes_eax.c +++ b/tests/emulator/sa_unit_tests_aes_eax.c @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -15,10 +15,10 @@ Copyright (C) 2015 OLogN Technologies AG 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *******************************************************************************/ -#include "sa_common.h" -#include "sa_aes_128.h" -#include "sa_eax_128.h" -#include "sa_unit_tests_aes_eax.h" +#include "sa-common.h" +#include "sa-aes-128.h" +#include "sa-eax-128.h" +#include "sa-unit-tests-aes-eax.h" void sa_aes_print_msg(const char* name, const uint8_t* msg, uint16_t msg_sz) { diff --git a/tests/emulator/saccp_protocol_client_side.c b/tests/emulator/saccp_protocol_client_side.c index f19ec32..7633501 100644 --- a/tests/emulator/saccp_protocol_client_side.c +++ b/tests/emulator/saccp_protocol_client_side.c @@ -165,5 +165,7 @@ uint8_t handler_saccp_receive( MEMORY_HANDLE mem_h ) } } + ZEPTO_DEBUG_ASSERT( NULL == "Error: we should not be here anyway\n" ); + return SACCP_RET_FAILED; } diff --git a/tests/emulator/saccp_protocol_client_side.h b/tests/emulator/saccp_protocol_client_side.h index beac7c3..a488c8e 100644 --- a/tests/emulator/saccp_protocol_client_side.h +++ b/tests/emulator/saccp_protocol_client_side.h @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -48,6 +48,7 @@ uint8_t handler_sacpp_continue_chain( MEMORY_HANDLE mem_h, void* control_prog_st #define SACCP_RET_PASS_TO_CENTRAL_UNIT 1 // packet must be sent to a communication peer uint8_t handler_saccp_receive( MEMORY_HANDLE mem_h ); +uint8_t handler_saccp_prepare_to_send( MEMORY_HANDLE mem_h ); #endif // MASTER_ENABLE_ALT_TEST_MODE diff --git a/tests/emulator/sha256.c b/tests/emulator/sha256.c index 1d53be3..44ff80a 100644 --- a/tests/emulator/sha256.c +++ b/tests/emulator/sha256.c @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -15,7 +15,7 @@ Copyright (C) 2015 OLogN Technologies AG 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *******************************************************************************/ -#include "../../firmware/src/common/sa_common.h" +#include "../../firmware/src/common/sa-common.h" #define SHA256_BYTES_TO_UINT32( num, bytes ) {num = (bytes)[0]; num <<= 8; num += (bytes)[1]; num <<= 8; num += (bytes)[2]; num <<= 8; num += (bytes)[3];} #define SHA256_UINT32_TO_BYTES( num, bytes ) {(bytes)[3] = (uint8_t)(num); (bytes)[2] = (uint8_t)((num)>>8); (bytes)[1] = (uint8_t)((num)>>16); (bytes)[0] = (uint8_t)((num)>>24);} diff --git a/tests/emulator/sha256.h b/tests/emulator/sha256.h index 2a14769..d2b9129 100644 --- a/tests/emulator/sha256.h +++ b/tests/emulator/sha256.h @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -19,7 +19,7 @@ Copyright (C) 2015 OLogN Technologies AG #define __SHA256_H___ -#include "../../firmware/src/common/sa_common.h" +#include "../../firmware/src/common/sa-common.h" void sha256(const uint8_t* msg, uint16_t len, uint8_t* hash); diff --git a/tests/emulator/test_generator.c b/tests/emulator/test_generator.c index 07401c4..17ea151 100644 --- a/tests/emulator/test_generator.c +++ b/tests/emulator/test_generator.c @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, @@ -15,7 +15,6 @@ Copyright (C) 2015 OLogN Technologies AG 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *******************************************************************************/ -#include "../../firmware/src/common/sa_common.h" #include "../../firmware/src/common/sa_common.h" #include "../../firmware/src/hal/hal_commlayer.h" #include // for get_rand_val() diff --git a/tests/emulator/test_generator.h b/tests/emulator/test_generator.h index cb21a04..5915758 100644 --- a/tests/emulator/test_generator.h +++ b/tests/emulator/test_generator.h @@ -2,7 +2,7 @@ Copyright (C) 2015 OLogN Technologies AG This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as + it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful,