diff --git a/tests/emulator/sa-commlayer.cpp b/tests/emulator/sa-commlayer.cpp index e1e0378..9b98516 100644 --- a/tests/emulator/sa-commlayer.cpp +++ b/tests/emulator/sa-commlayer.cpp @@ -128,7 +128,16 @@ bool communicationInitializeAsClient() return true; } -void communicationTerminate() +bool communication_initialize() +{ +#ifdef USED_AS_MASTER + return communicationInitializeAsClient(); +#else // USED_AS_MASTER + return communicationInitializeAsServer(); +#endif // USED_AS_MASTER +} + +void communication_terminate() { CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; @@ -441,7 +450,7 @@ uint8_t buffer_out[ BUFSIZE ]; -bool communicationInitialize() +bool communication_preinitialize() { #ifdef _MSC_VER // do Windows magic @@ -453,8 +462,14 @@ bool communicationInitialize() printf("WSAStartup failed with error: %d\n", iResult); return false; } + return true; +#else + return true; #endif +} +bool _communication_initialize() +{ //Zero out socket address memset(&sa_self, 0, sizeof sa_self); memset(&sa_other, 0, sizeof sa_other); @@ -481,7 +496,12 @@ bool communicationInitialize() if (-1 == bind(sock, (struct sockaddr *)&sa_self, sizeof(sa_self))) { - printf( "error bind failed\n" ); +#ifdef _MSC_VER + int error = WSAGetLastError(); +#else + int error = errno; +#endif + printf( "bind sock failed; error %d\n", error ); CLOSE_SOCKET(sock); return false; } @@ -495,17 +515,7 @@ bool communicationInitialize() return true; } -bool communicationInitializeAsServer() -{ - return communicationInitialize(); -} - -bool communicationInitializeAsClient() -{ - return communicationInitialize(); -} - -void communicationTerminate() +void _communication_terminate() { CLOSE_SOCKET(sock); } @@ -564,103 +574,237 @@ uint8_t tryGetMessage( MEMORY_HANDLE mem_h ) } +#ifdef USED_AS_MASTER - -int xxx(void) -{ - printf("STARTING SERVER...\n"); - printf("==================\n\n"); - - // do Windows magic - WSADATA wsaData; - int iResult; - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (iResult != 0) { - printf("WSAStartup failed with error: %d\n", iResult); - return 1; - } - - // declarations - int bytes_sent; - int recsize; - char buffer_out[1024]; - char buffer_in[1024]; +const char* inet_addr_as_string_with_cl = "127.0.0.1"; +int sock_with_cl; +struct sockaddr_in sa_self_with_cl, sa_other_with_cl; - int sock; - struct sockaddr_in sa_self, sa_other; +#ifdef USED_AS_MASTER_COMMSTACK +uint16_t self_port_num_with_cl = 7665; +uint16_t other_port_num_with_cl = 7655; +#else // USED_AS_MASTER_COMMSTACK +uint16_t self_port_num_with_cl = 7655; +uint16_t other_port_num_with_cl = 7665; +#endif // USED_AS_MASTER_COMMSTACK - // data initializing +uint8_t buffer_in_with_cl[ BUFSIZE ]; +uint8_t buffer_out_with_cl[ BUFSIZE ]; +bool communication_with_comm_layer_initialize() +{ //Zero out socket address - memset(&sa_self, 0, sizeof sa_self); - memset(&sa_other, 0, sizeof sa_other); + memset(&sa_self_with_cl, 0, sizeof sa_self_with_cl); + memset(&sa_other_with_cl, 0, sizeof sa_other_with_cl); //create an internet, datagram, socket using UDP - sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (-1 == sock) /* if socket failed to initialize, exit */ + sock_with_cl = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (-1 == sock_with_cl) /* if socket failed to initialize, exit */ { printf("Error Creating Socket\n"); - exit(EXIT_FAILURE); + return false; } //The address is ipv4 - sa_other.sin_family = AF_INET; - sa_self.sin_family = AF_INET; + sa_other_with_cl.sin_family = AF_INET; + sa_self_with_cl.sin_family = AF_INET; //ip_v4 adresses is a uint32_t, convert a string representation of the octets to the appropriate value - sa_self.sin_addr.s_addr = inet_addr("127.0.0.1"); - sa_other.sin_addr.s_addr = inet_addr("127.0.0.1"); + sa_self_with_cl.sin_addr.s_addr = inet_addr( inet_addr_as_string_with_cl ); + sa_other_with_cl.sin_addr.s_addr = inet_addr( inet_addr_as_string_with_cl ); //sockets are unsigned shorts, htons(x) ensures x is in network byte order, set the port to 7654 - sa_self.sin_port = htons(7667); - sa_other.sin_port = htons(7654); + sa_self_with_cl.sin_port = htons( self_port_num_with_cl ); + sa_other_with_cl.sin_port = htons( other_port_num_with_cl ); - if (-1 == bind(sock, (struct sockaddr *)&sa_self, sizeof(sa_self))) + if (-1 == bind(sock_with_cl, (struct sockaddr *)&sa_self_with_cl, sizeof(sa_self_with_cl))) { - perror("error bind failed"); - closesocket(sock); - exit(EXIT_FAILURE); +#ifdef _MSC_VER + int error = WSAGetLastError(); +#else + int error = errno; +#endif + printf( "bind sock_with_cl failed; error %d\n", error ); + CLOSE_SOCKET(sock_with_cl); + return false; } - socklen_t fromlen = sizeof(sa_other); - -#ifdef _WIN32 +#ifdef _MSC_VER unsigned long ul = 1; - ioctlsocket(sock, FIONBIO, &ul); + ioctlsocket(sock_with_cl, FIONBIO, &ul); #else - fcntl(sock,F_SETFL,O_NONBLOCK); + fcntl(sock_with_cl,F_SETFL,O_NONBLOCK); +#endif + return true; +} + +void communication_with_comm_layer_terminate() +{ + CLOSE_SOCKET(sock_with_cl); +} + +bool communication_initialize() +{ + return communication_preinitialize() +#ifdef USED_AS_MASTER_COMMSTACK + && _communication_initialize() +#endif + && communication_with_comm_layer_initialize(); +} + +void communication_terminate() +{ + _communication_terminate(); + communication_with_comm_layer_terminate(); +} + +uint8_t try_get_message_within_master( MEMORY_HANDLE mem_h ) +{ + socklen_t fromlen = sizeof(sa_other_with_cl); + int recsize = recvfrom(sock_with_cl, (char *)buffer_in_with_cl, sizeof(buffer_in_with_cl), 0, (struct sockaddr *)&sa_other_with_cl, &fromlen); + if (recsize < 0) + { +#ifdef _MSC_VER + int error = WSAGetLastError(); + if ( error == WSAEWOULDBLOCK ) +#else + int error = errno; + if ( error == EAGAIN || error == EWOULDBLOCK ) #endif + { + return COMMLAYER_RET_PENDING; + } + else + { + printf( "unexpected error %d received while getting message\n", error ); + return COMMLAYER_RET_FAILED; + } + } + else + { + zepto_write_block( mem_h, buffer_in_with_cl, recsize ); + return COMMLAYER_RET_OK; + } + +} - for ( int i=0;;i++) +uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) +{ + printf( "wait_for_communication_event()\n" ); + fd_set rfds; + struct timeval tv; + int retval; + + /* Watch stdin (fd 0) to see when it has input. */ + FD_ZERO(&rfds); + FD_SET(sock_with_cl, &rfds); +#ifdef USED_AS_MASTER_COMMSTACK + FD_SET(sock, &rfds); +#endif + + /* Wait */ + tv.tv_sec = 0; + tv.tv_usec = 1000000; + + retval = select(2, &rfds, NULL, NULL, &tv); + /* Don't rely on the value of tv now! */ + + if (retval == -1) + { +#ifdef _MSC_VER + int error = WSAGetLastError(); +// if ( error == WSAEWOULDBLOCK ) + printf( "error %d\n", error ); +#else + perror("select()"); +// int error = errno; +// if ( error == EAGAIN || error == EWOULDBLOCK ) +#endif + assert(0); + return COMMLAYER_RET_FAILED; + } + else if (retval) { - printf("recv test....\n"); - recsize = recvfrom(sock, (char *)buffer_in, sizeof(buffer_in), 0, (struct sockaddr *)&sa_other, &fromlen); - if (recsize < 0) + assert( retval == rfds.fd_count ); +// assert(0); + if ( rfds.fd_array[0] == sock ) { -// fprintf(stderr, "%s\n", strerror(errno)); -// exit(EXIT_FAILURE); - Sleep(100); - continue; + uint8_t ret_code = tryGetMessage( mem_h ); + if ( ret_code == COMMLAYER_RET_FAILED ) + return ret_code; + assert( ret_code == COMMLAYER_RET_OK ); + return COMMLAYER_RET_FROM_DEV; } - printf("recsize: %d\n ", recsize); - Sleep(1); - printf("datagram: %.*s\n", (int)recsize, buffer_in); -// printf( "received from port: %d\n", sa_from.sin_port ); - - // echo back - - //sendto(int socket, char data, int dataLength, flags, destinationAddress, int destinationStructureLength) -// int sz_to_send = strlen(buffer); - buffer_in[recsize] = 0; - sprintf( buffer_out, "[%d]%s", i, buffer_in ); - bytes_sent = sendto(sock, buffer_out, strlen(buffer_out)+1, 0, (struct sockaddr*)&sa_other, sizeof sa_other); - if (bytes_sent < 0) { - printf("Error sending packet: %s\n", strerror(errno)); - exit(EXIT_FAILURE); + else + { + assert( rfds.fd_array[0] == sock_with_cl ); + uint8_t ret_code = try_get_message_within_master( mem_h ); + if ( ret_code == COMMLAYER_RET_FAILED ) + return ret_code; + assert( ret_code == COMMLAYER_RET_OK ); + return COMMLAYER_RET_FROM_CENTRAL_UNIT; } } + else + { + return COMMLAYER_RET_TIMEOUT; + } +} + +uint8_t send_within_master( MEMORY_HANDLE mem_h ) +{ + printf( "send_within_master() called...\n" ); + parser_obj po; + zepto_parser_init( &po, mem_h ); + uint16_t sz = zepto_parsing_remaining_bytes( &po ); + assert( sz <= BUFSIZE ); + zepto_parse_read_block( &po, buffer_out_with_cl, sz ); + + + int bytes_sent = sendto(sock_with_cl, (char*)buffer_out_with_cl, sz, 0, (struct sockaddr*)&sa_other_with_cl, sizeof sa_other_with_cl); + if (bytes_sent < 0) + { +#ifdef _MSC_VER + int error = WSAGetLastError(); + printf( "Error %d sending packet\n", error ); +#else + printf("Error sending packet: %s\n", strerror(errno)); +#endif + return COMMLAYER_RET_FAILED; + } + return COMMLAYER_RET_OK; +} + + +#ifdef USED_AS_MASTER_COMMSTACK + +uint8_t send_to_central_unit( MEMORY_HANDLE mem_h ) +{ + return send_within_master( mem_h ); +} + +#else // USED_AS_MASTER_COMMSTACK + +uint8_t send_to_commm_stack( MEMORY_HANDLE mem_h ) +{ + return send_within_master( mem_h ); +} + +#endif // USED_AS_MASTER_COMMSTACK + +#else // USED_AS_MASTER + +bool communication_initialize() +{ + return communication_preinitialize() && _communication_initialize(); +} + +void communication_terminate() +{ + _communication_terminate(); } +#endif // USED_AS_MASTER #elif diff --git a/tests/emulator/sa-commlayer.h b/tests/emulator/sa-commlayer.h index 718e27e..677c12a 100644 --- a/tests/emulator/sa-commlayer.h +++ b/tests/emulator/sa-commlayer.h @@ -20,22 +20,35 @@ Copyright (C) 2015 OLogN Technologies AG #include "zepto-mem-mngmt.h" +bool communication_initialize(); +void communication_terminate(); + // RET codes -#define COMMLAYER_RET_FAILED 0 // not authenticated, etc -#define COMMLAYER_RET_OK 1 // new packet +#define COMMLAYER_RET_FAILED 0 +#define COMMLAYER_RET_OK 1 #define COMMLAYER_RET_PENDING 2 +#define COMMLAYER_RET_FROM_CENTRAL_UNIT 10 +#define COMMLAYER_RET_FROM_DEV 11 +#define COMMLAYER_RET_TIMEOUT 12 + +#define COMMLAYER_RET_FROM_COMMM_STACK 10 + -bool communicationInitializeAsServer(); -bool communicationInitializeAsClient(); -void communicationTerminate(); -uint8_t sendMessage( uint16_t* msgSize, const uint8_t * buff ); -uint8_t getMessage( uint16_t* msgSize, uint8_t * buff, int maxSize ); // returns when a packet received -uint8_t tryGetMessage(uint16_t* msgSize, uint8_t * buff, int maxSize); // returns immediately, but a packet reception is not guaranteed uint8_t sendMessage( MEMORY_HANDLE mem_h ); -uint8_t getMessage( MEMORY_HANDLE mem_h ); // returns when a packet received uint8_t tryGetMessage( MEMORY_HANDLE mem_h ); // returns immediately, but a packet reception is not guaranteed +uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ); +#ifdef USED_AS_MASTER +#ifdef USED_AS_MASTER_COMMSTACK +uint8_t send_to_central_unit( MEMORY_HANDLE mem_h ); +#else +uint8_t send_to_commm_stack( MEMORY_HANDLE mem_h ); +#endif +#endif + + + #endif // __SA_COMMLAYER_H__ \ No newline at end of file diff --git a/tests/emulator/sa-common.h b/tests/emulator/sa-common.h index d47cde8..04305e4 100644 --- a/tests/emulator/sa-common.h +++ b/tests/emulator/sa-common.h @@ -36,7 +36,19 @@ Copyright (C) 2015 OLogN Technologies AG #define int8_t char #define uint16_t unsigned short #define int16_t short - +/* +inline void memset( void* dest, uint8_t val, uint8_t cnt ) +{ + for ( uint8_t i=0; i -#define BUF_SIZE 512 -//uint8_t data_buff[BUF_SIZE]; -//uint8_t msgLastSent[BUF_SIZE]; uint8_t pid[ SASP_NONCE_SIZE ]; uint8_t nonce[ SASP_NONCE_SIZE ]; @@ -69,9 +66,6 @@ int main_loop() // in this preliminary implementation all memory segments are kept separately // All memory objects are listed below // TODO: revise memory management -/* uint8_t miscBuff[BUF_SIZE]; - uint8_t* stack = miscBuff; // first two bytes are used for sizeInOut - uint16_t stackSize = BUF_SIZE / 4 - 2;*/ uint8_t timer_val = 0; uint16_t wake_time; // TODO: revise time/timer management @@ -85,7 +79,7 @@ int main_loop() // test setup values bool wait_for_incoming_chain_with_timer = false; - uint16_t wake_time_to_start_new_chain; + uint16_t wake_time_to_start_new_chain = 0; uint8_t wait_to_continue_processing = 0; uint16_t wake_time_continue_processing; @@ -97,7 +91,7 @@ int main_loop() SASP_initAtLifeStart( &sasp_data ); // Try to open a named pipe; wait for it, if necessary. - if ( !communicationInitializeAsClient() ) + if ( !communication_initialize() ) return -1; @@ -271,7 +265,7 @@ printf( "Processing continued...\n" ); if ( ret_code != COMMLAYER_RET_OK ) { printf("\n\nWAITING FOR ESTABLISHING COMMUNICATION WITH SERVER...\n\n"); - if (!communicationInitializeAsClient()) // regardles of errors... quick and dirty solution so far + if (!communication_initialize()) // regardles of errors... quick and dirty solution so far return -1; goto getmsg; } @@ -399,6 +393,7 @@ printf( "Processing continued...\n" ); // 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 ); @@ -413,6 +408,7 @@ printf( "Processing continued...\n" ); goto entry; break; } +#endif // 0 case SAGDP_RET_TO_LOWER_REPEATED: { goto saspsend; @@ -685,7 +681,7 @@ printf( "Processing in progress... (period = %d, time = %d)\n", wait_to_continue } - communicationTerminate(); + communication_terminate(); return 0; } diff --git a/tests/emulator/sa_client_central_unit.cpp b/tests/emulator/sa_client_central_unit.cpp new file mode 100644 index 0000000..d35c4ef --- /dev/null +++ b/tests/emulator/sa_client_central_unit.cpp @@ -0,0 +1,146 @@ +/******************************************************************************* +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 "saccp_protocol.h" +#include "sa_test_control_prog.h" +#include "test-generator.h" +#include + + +int main_loop() +{ +#ifdef ENABLE_COUNTER_SYSTEM + INIT_COUNTER_SYSTEM +#endif // ENABLE_COUNTER_SYSTEM + + + printf("starting CLIENT...\n"); + printf("==================\n\n"); + + tester_initTestSystem(); + + +#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; + + uint8_t wait_to_continue_processing = 0; + uint16_t wake_time_continue_processing; + + + // Try to initialize connection + if ( !communication_initialize() ) + return -1; + + + + ret_code = default_test_control_program_start_new( &control_prog_state, MEMORY_HANDLE_MAIN_LOOP ); + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + goto send_command; + + // MAIN LOOP + for (;;) + { +wait_for_comm_event: + ret_code = wait_for_communication_event( MEMORY_HANDLE_MAIN_LOOP, 1000 ); // TODO: recalculation + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + + switch ( ret_code ) + { + case COMMLAYER_RET_FROM_COMMM_STACK: + { + // regular processing will be done below in the next block + goto process_reply; + break; + } + case COMMLAYER_RET_TIMEOUT: + { + // regular processing will be done below in the next block + printf( "no reply from comm stack received; the last message (if any) will be resent by timer\n" ); + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + goto wait_for_comm_event; + break; + } + default: + { + // unexpected ret_code + printf( "Unexpected ret_code %d\n", ret_code ); + assert( 0 ); + break; + } + } + +/*start_new_chain: + ret_code = default_test_control_program_start_new( &control_prog_state, MEMORY_HANDLE_MAIN_LOOP ); + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + goto send_command;*/ + + // 4. Process received command (yoctovm) +process_reply: + 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 ); + 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 ); + + send_command: + send_to_commm_stack( MEMORY_HANDLE_MAIN_LOOP ); + } + + communication_terminate(); + + return 0; +} + +int main(int argc, char *argv[]) +{ + zepto_mem_man_init_memory_management(); + + return main_loop(); +} diff --git a/tests/emulator/sa_client_comm_stack.cpp b/tests/emulator/sa_client_comm_stack.cpp new file mode 100644 index 0000000..155e1d2 --- /dev/null +++ b/tests/emulator/sa_client_comm_stack.cpp @@ -0,0 +1,419 @@ +/******************************************************************************* +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-timer.h" +#include "saoudp_protocol.h" +#include "sasp_protocol.h" +#include "sagdp_protocol.h" +#include "test-generator.h" +#include + +uint8_t pid[ SASP_NONCE_SIZE ]; +uint8_t nonce[ SASP_NONCE_SIZE ]; + +/* +uint8_t send_to_central_unit_error( MEMORY_HANDLE mem_h ) +{ + return send_to_central_unit( mem_h ); +} + +uint8_t send_to_central_unit_reply( MEMORY_HANDLE mem_h ) +{ + return send_to_central_unit( mem_h ); +} +*/ + +int main_loop() +{ +#ifdef ENABLE_COUNTER_SYSTEM + INIT_COUNTER_SYSTEM +#endif // ENABLE_COUNTER_SYSTEM + + + printf("starting CLIENT's COMMM STACK...\n"); + printf("================================\n\n"); + + tester_initTestSystem(); + + // TODO: actual key loading, etc + uint8_t sasp_key[16]; + memcpy( sasp_key, "16-byte fake key", 16 ); + + uint8_t timer_val = 0; + uint16_t wake_time; + // TODO: revise time/timer management + + uint8_t ret_code; + + 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_initAtLifeStart( &sasp_data ); + + // Try to initialize connection + if ( !communication_initialize() ) + return -1; + + + // MAIN LOOP + for (;;) + { +wait_for_comm_event: + ret_code = wait_for_communication_event( MEMORY_HANDLE_MAIN_LOOP, timer_val*100 ); // TODO: recalculation + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + + switch ( ret_code ) + { + case COMMLAYER_RET_FROM_CENTRAL_UNIT: + { + // regular processing will be done below in the next block + goto client_received; + break; + } + case COMMLAYER_RET_FROM_DEV: + { + // regular processing will be done below in the next block + goto saoudp_in; + break; + } + case COMMLAYER_RET_TIMEOUT: + { + // regular processing will be done below in the next block + printf( "no reply from device received; the last message (if any) will be resent by timer\n" ); + // TODO: to think: why do we use here handlerSAGDP_receiveRequestResendLSP() and not handlerSAGDP_timer() + ret_code = handlerSAGDP_receiveRequestResendLSP( &timer_val, NULL, MEMORY_HANDLE_MAIN_LOOP, &sagdp_data ); + if ( ret_code == SAGDP_RET_TO_LOWER_NONE ) + { + zepto_response_to_request( MEMORY_HANDLE_MAIN_LOOP ); + continue; + } + wake_time = 0; + 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 ); + ret_code = handlerSAGDP_receiveRequestResendLSP( &timer_val, 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 wait_for_comm_event; + break; + } + default: + { + // unexpected ret_code + printf( "Unexpected ret_code %d\n", ret_code ); + assert( 0 ); + break; + } + } + + + 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 +saoudp_in: + 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 wait_for_comm_event; + 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_LAST_SEND_FAILED: + { + printf( "NONCE_LAST_SENT has been reset; the last message (if any) will be resent\n" ); + ret_code = handlerSAGDP_receiveRequestResendLSP( &timer_val, 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 ); + ret_code = handlerSAGDP_receiveRequestResendLSP( &timer_val, 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 + ret_code = handlerSAGDP_receiveUP( &timer_val, 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 ); + ret_code = handlerSAGDP_receiveUP( &timer_val, 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 wait_for_comm_event; + break; + } +#else + case SAGDP_RET_SYS_CORRUPTED: + { + // TODO: reinitialize all + goto wait_for_comm_event; + 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) + ret_code = send_to_central_unit_error( MEMORY_HANDLE_MAIN_LOOP ); + //+++TODO: where to go? + goto wait_for_comm_event; + 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; + } + } + + ret_code = send_to_central_unit( MEMORY_HANDLE_MAIN_LOOP ); + goto wait_for_comm_event; + //+++ TODO: what? goto select? + + // 5. SAGDP +client_received: + ret_code = handlerSAGDP_receiveHLP( &timer_val, 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 ); + ret_code = handlerSAGDP_receiveHLP( &timer_val, 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; + } + } + + sendmsg: + 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 ); + sendMessage( MEMORY_HANDLE_TEST_SUPPORT ); + zepto_response_to_request( MEMORY_HANDLE_TEST_SUPPORT ); + } + + if ( !tester_shouldDropOutgoingPacket() ) + { + if ( is_packet_to_send ) + { + ret_code = sendMessage( 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"); + } + } + + } + + communication_terminate(); + + return 0; +} + +int main(int argc, char *argv[]) +{ + zepto_mem_man_init_memory_management(); + + return main_loop(); +// return 0; +} diff --git a/tests/emulator/sa_server.cpp b/tests/emulator/sa_server.cpp index 2bf4167..830fbf3 100644 --- a/tests/emulator/sa_server.cpp +++ b/tests/emulator/sa_server.cpp @@ -39,9 +39,6 @@ Copyright (C) 2015 OLogN Technologies AG #include -#define BUF_SIZE 512 -//uint8_t data_buff[BUF_SIZE]; -//uint8_t msgLastSent[BUF_SIZE]; uint8_t pid[ SASP_NONCE_SIZE ]; uint8_t nonce[ SASP_NONCE_SIZE ]; @@ -75,9 +72,6 @@ int main_loop() // in this preliminary implementation all memory segments are kept separately // All memory objects are listed below // TODO: revise memory management -/* uint8_t miscBuff[BUF_SIZE]; - uint8_t* stack = miscBuff; // first two bytes are used for sizeInOut - int stackSize = BUF_SIZE / 4 - 2;*/ uint8_t timer_val = 0; uint16_t wake_time = 0; // TODO: revise time/timer management @@ -88,11 +82,11 @@ int main_loop() uint8_t ret_code; // test setup values - bool wait_for_incoming_chain_with_timer; - uint16_t wake_time_to_start_new_chain; + bool wait_for_incoming_chain_with_timer = 0; + uint16_t wake_time_to_start_new_chain = 0; uint8_t wait_to_continue_processing = 0; - uint16_t wake_time_continue_processing; + uint16_t wake_time_continue_processing = 0; // do necessary initialization SASP_initAtLifeStart( &sasp_data ); // TODO: replace by more extensive restore-from-backup-etc @@ -102,7 +96,7 @@ int main_loop() printf("\nAwaiting client connection... \n" ); - if (!communicationInitializeAsServer()) + if (!communication_initialize()) return -1; printf("Client connected.\n"); @@ -199,7 +193,7 @@ printf( "Processing continued...\n" ); if ( ret_code != COMMLAYER_RET_OK ) { printf("\n\nWAITING FOR ESTABLISHING COMMUNICATION WITH SERVER...\n\n"); - if (!communicationInitializeAsClient()) // regardles of errors... quick and dirty solution so far + if (!communication_initialize()) // regardles of errors... quick and dirty solution so far return -1; goto getmsg; } diff --git a/tests/emulator/sa_test_control_prog.cpp b/tests/emulator/sa_test_control_prog.cpp index e35c0fd..499cf21 100644 --- a/tests/emulator/sa_test_control_prog.cpp +++ b/tests/emulator/sa_test_control_prog.cpp @@ -268,6 +268,8 @@ uint8_t default_test_control_program_accept_reply( void* control_prog_state, uin *wait_to_process_time = tester_get_rand_val() % 5; return CONTROL_PROG_WAIT_TO_CONTINUE; }*/ +#else +// return default_test_control_program_accept_reply_continue( control_prog_state, reply ); #endif } diff --git a/tests/emulator/sagdp_protocol.h b/tests/emulator/sagdp_protocol.h index a81a105..bf5a2c1 100644 --- a/tests/emulator/sagdp_protocol.h +++ b/tests/emulator/sagdp_protocol.h @@ -32,7 +32,7 @@ Copyright (C) 2015 OLogN Technologies AG #define SAGDP_RET_TO_LOWER_REPEATED 3 // repeated packet #define SAGDP_RET_TO_LOWER_NONE 4 // repeated packet #define SAGDP_RET_TO_HIGHER 5 -#define SAGDP_RET_TO_HIGHER_ERROR 6 +//#define SAGDP_RET_TO_HIGHER_ERROR 6 #define SAGDP_RET_START_OVER_FIRST_RECEIVED 7 // "first" packet is received while SAGDP is in "wait-remote" state #define SAGDP_RET_NEED_NONCE 8 // nonce is necessary before sending a packet