From 9153783bc3aa348db536f99f3b53766cd9b8205b Mon Sep 17 00:00:00 2001 From: divanchykhin Date: Mon, 1 Jun 2015 08:56:35 +0300 Subject: [PATCH] Emulator actual embodiment: update of hal-related code --- .../hal/{sa-eeprom.h => hal-eeprom.h} | 20 ++- tests/emulator/hal/hal-waiting.h | 43 +++++++ tests/emulator/hal/sa-commlayer.h | 3 +- tests/emulator/hal/sa-hal-time-provider.h | 7 +- .../emulator/hal/win-lnx-common/hal-eeprom.c | 116 ++++++++++++++++++ .../hal/win-lnx-common/hal-time-convertions.h | 27 ++++ .../hal/win-lnx-common/sa-commlayer.c | 70 ++++++++++- 7 files changed, 264 insertions(+), 22 deletions(-) rename tests/emulator/hal/{sa-eeprom.h => hal-eeprom.h} (61%) create mode 100644 tests/emulator/hal/hal-waiting.h create mode 100644 tests/emulator/hal/win-lnx-common/hal-eeprom.c create mode 100644 tests/emulator/hal/win-lnx-common/hal-time-convertions.h diff --git a/tests/emulator/hal/sa-eeprom.h b/tests/emulator/hal/hal-eeprom.h similarity index 61% rename from tests/emulator/hal/sa-eeprom.h rename to tests/emulator/hal/hal-eeprom.h index f62992b..9b0e771 100644 --- a/tests/emulator/hal/sa-eeprom.h +++ b/tests/emulator/hal/hal-eeprom.h @@ -15,22 +15,16 @@ Copyright (C) 2015 OLogN Technologies AG 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *******************************************************************************/ -#if !defined __SA_EEPROM_H__ -#define __SA_EEPROM_H__ +#if !defined __HAL_EEPROM_H__ +#define __HAL_EEPROM_H__ #include "../sa-common.h" -// data IDs (for communication with eeprom -#define DATA_SASP_NONCE_LW_ID 0 // Nonce Lower Watermark -#define DATA_SASP_NONCE_LS_ID 1 // Nonce to use For Sending -// ...to be continued - -#define DATA_CONTINUE_LIFE_ID 0Xff // FAKE data used at simulator startup: if not present, a new life (whatever it means) is started - // calls -void eeprom_write( uint8_t id, uint8_t* data, uint8_t size); -void eeprom_read_fixed_size( uint8_t id, uint8_t* data, uint8_t size); -uint8_t eeprom_read_size( uint8_t id ); +bool hal_init_eeprom_access(); +bool hal_eeprom_write( const uint8_t* data, uint16_t size, uint16_t address ); +bool hal_eeprom_read( uint8_t* data, uint16_t size, uint16_t address); +void hal_eeprom_flush(); -#endif // __SA_EEPROM_H__ \ No newline at end of file +#endif // __HAL_EEPROM_H__ \ No newline at end of file diff --git a/tests/emulator/hal/hal-waiting.h b/tests/emulator/hal/hal-waiting.h new file mode 100644 index 0000000..3c4a5c2 --- /dev/null +++ b/tests/emulator/hal/hal-waiting.h @@ -0,0 +1,43 @@ +/******************************************************************************* +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. +*******************************************************************************/ + +#if !defined __SA_HAL_WAITING_H__ +#define __SA_HAL_WAITING_H__ + +#include "../sa-common.h" +#include "sa-hal-time-provider.h" + +typedef struct _waiting_for +{ + sa_time_val wait_time; + uint8_t wait_packet; + uint8_t wait_i2c; + uint8_t wait_legs; + uint16_t leg_mask; // [in] + uint16_t leg_values_waited; // [in] + uint16_t leg_value_received; // [out] +} waiting_for; + +#define WAIT_RESULTED_IN_FAILURE 0 +#define WAIT_RESULTED_IN_TIMEOUT 1 +#define WAIT_RESULTED_IN_PACKET 2 +#define WAIT_RESULTED_IN_I2C 3 +#define WAIT_RESULTED_IN_PINS 4 + +uint8_t hal_wait_for( waiting_for* wf ); + +#endif // __SA_HAL_WAITING_H__ diff --git a/tests/emulator/hal/sa-commlayer.h b/tests/emulator/hal/sa-commlayer.h index 91ba556..b766edf 100644 --- a/tests/emulator/hal/sa-commlayer.h +++ b/tests/emulator/hal/sa-commlayer.h @@ -41,7 +41,8 @@ void communication_terminate(); uint8_t sendMessage( MEMORY_HANDLE mem_h ); -uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ); +//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 #ifdef USED_AS_MASTER_COMMSTACK uint8_t send_to_central_unit( MEMORY_HANDLE mem_h ); diff --git a/tests/emulator/hal/sa-hal-time-provider.h b/tests/emulator/hal/sa-hal-time-provider.h index 35dcbcc..3a1495c 100644 --- a/tests/emulator/hal/sa-hal-time-provider.h +++ b/tests/emulator/hal/sa-hal-time-provider.h @@ -19,6 +19,7 @@ Copyright (C) 2015 OLogN Technologies AG #define __SA_HAL_TIME_PROVIDER_H__ #include "../sa-common.h" +#include typedef struct _sa_time_struct { @@ -29,10 +30,10 @@ typedef struct _sa_time_struct // use typedef below instead typedef sa_time_struct sa_time_val; -void sa_get_time( sa_time_val* t ); +#define TIME_MILLISECONDS16_TO_TIMEVAL( mslow, timeval ) HAL_TIME_MILLISECONDS16_TO_TIMEVAL( mslow, timeval ) +#define TIME_MILLISECONDS32_TO_TIMEVAL( mslow, mshigh, timeval ) HAL_TIME_MILLISECONDS32_TO_TIMEVAL( mslow, mshigh, timeval ) - -unsigned short getTime(); // TODO: get rid of it ASAP +void sa_get_time( sa_time_val* t ); // PLATFORM-SPECIFIC IMPLEMENTATION diff --git a/tests/emulator/hal/win-lnx-common/hal-eeprom.c b/tests/emulator/hal/win-lnx-common/hal-eeprom.c new file mode 100644 index 0000000..0904050 --- /dev/null +++ b/tests/emulator/hal/win-lnx-common/hal-eeprom.c @@ -0,0 +1,116 @@ +/******************************************************************************* +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. +*******************************************************************************/ + +#include "../hal-eeprom.h" +#include +#include +#include +#ifdef _MSC_VER +#include +#include +#endif + + +// Interface is implemented based on file IO +// File has a standard name + +//FILE* f = NULL; +int efile = -1; +#ifdef _MSC_VER +HANDLE hfile = INVALID_HANDLE_VALUE; +#endif + +bool hal_init_eeprom_access() +{ +// f = fopen( MASTER_SLAVE_BIT == 1 ? "sa-eeprom-master": "sa-eeprom-slave", "rw+b" ); + efile = open( MASTER_SLAVE_BIT == 1 ? "sa-eeprom-master": "sa-eeprom-slave.dat", O_RDWR | O_CREAT | O_BINARY, S_IWRITE | S_IREAD ); +#ifdef _MSC_VER + hfile = (HANDLE) _get_osfhandle (efile); + if ( hfile == INVALID_HANDLE_VALUE ) + return false; +#endif + return efile != -1; +} + +bool hal_eeprom_write( const uint8_t* data, uint16_t size, uint16_t address ) +{ + int res; + res = lseek( efile, address, SEEK_SET); + if ( res == -1 ) + return false; + res = write( efile, data, size ); + if ( res != size ) + return false; + return true; +} + +bool hal_eeprom_read( uint8_t* data, uint16_t size, uint16_t address) +{ + int res; + res = lseek( efile, address, SEEK_SET); + if ( res == -1 ) + return false; + res = read( efile, data, size ); + if ( res != size ) + return false; + return true; +} + +void hal_eeprom_flush() +{ +#ifdef _MSC_VER + FlushFileBuffers(hfile); +#else + fsync( efile ); +#endif +} + +/* +void eeprom_write( uint8_t id, uint8_t* data, uint8_t size) +{ + char filename[256]; + prepareFileName( filename, id ); + FILE* f = fopen( filename, "wb" ); + assert( f ); + int written = fwrite( data, 1, size, f ); + assert( written == size ); + fclose( f ); +} + +void eeprom_read_fixed_size( uint8_t id, uint8_t* data, uint8_t size) +{ + char filename[256]; + prepareFileName( filename, id ); + FILE* f = fopen( filename, "rb" ); + assert( f ); + int retrieved = fread ( data, 1, size, f ); + assert( retrieved == size ); + fclose( f ); +} + +uint8_t eeprom_read_size( uint8_t id ) +{ + char filename[256]; + prepareFileName( filename, id ); + FILE* f = fopen( filename, "wb" ); + assert( f ); + fseek ( f, 0, SEEK_END ); + int size = ftell( f ); + fclose( f ); + return size; +} +*/ \ No newline at end of file diff --git a/tests/emulator/hal/win-lnx-common/hal-time-convertions.h b/tests/emulator/hal/win-lnx-common/hal-time-convertions.h new file mode 100644 index 0000000..17f93f2 --- /dev/null +++ b/tests/emulator/hal/win-lnx-common/hal-time-convertions.h @@ -0,0 +1,27 @@ +/******************************************************************************* +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. +*******************************************************************************/ + +#if !defined __HAL_TIME_CONVERTIONS_H__ +#define __HAL_TIME_CONVERTIONS_H__ + +// present implementation assumes that resolution of h/w timer is 1 ms +// macros below must be reimplemented if this is not the case +#define HAL_TIME_MILLISECONDS16_TO_TIMEVAL( mslow, timeval ) {(timeval).low_t = (mslow); (timeval).high_t = 0;} +#define HAL_TIME_MILLISECONDS32_TO_TIMEVAL( mslow, mshigh, timeval ) {(timeval).low_t = (mslow); (timeval).high_t = mshigh;} + + +#endif // __HAL_TIME_CONVERTIONS_H__ \ No newline at end of file diff --git a/tests/emulator/hal/win-lnx-common/sa-commlayer.c b/tests/emulator/hal/win-lnx-common/sa-commlayer.c index 05abbe5..655e473 100644 --- a/tests/emulator/hal/win-lnx-common/sa-commlayer.c +++ b/tests/emulator/hal/win-lnx-common/sa-commlayer.c @@ -16,6 +16,7 @@ Copyright (C) 2015 OLogN Technologies AG *******************************************************************************/ #include "../sa-commlayer.h" +#include "../hal-waiting.h" #include @@ -506,7 +507,8 @@ void communication_terminate() #endif // USED_AS_MASTER -uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) +//uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) +uint8_t wait_for_communication_event( unsigned int timeout ) { printf( "wait_for_communication_event()\n" ); fd_set rfds; @@ -556,10 +558,10 @@ uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) { if ( FD_ISSET(sock, &rfds) ) { - uint8_t ret_code = tryGetMessage( mem_h ); +/* uint8_t ret_code = tryGetMessage( mem_h ); if ( ret_code == COMMLAYER_RET_FAILED ) return ret_code; - assert( ret_code == COMMLAYER_RET_OK ); + assert( ret_code == COMMLAYER_RET_OK );*/ return COMMLAYER_RET_FROM_DEV; } #ifdef USED_AS_MASTER @@ -567,10 +569,10 @@ uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) { // assert( rfds.fd_array[0] == sock_with_cl ); assert( FD_ISSET(sock_with_cl, &rfds) ); - uint8_t ret_code = try_get_message_within_master( mem_h ); +/* 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 ); + assert( ret_code == COMMLAYER_RET_OK );*/ return COMMLAYER_RET_FROM_CENTRAL_UNIT; } #endif // USED_AS_MASTER @@ -581,3 +583,61 @@ uint8_t wait_for_communication_event( MEMORY_HANDLE mem_h, uint16_t timeout ) } } +uint8_t wait_for_timeout( unsigned int timeout) +{ + struct timeval tv; + int retval; + tv.tv_sec = timeout / 1000; + tv.tv_usec = ((long)timeout % 1000) * 1000; + + retval = select(0, NULL, NULL, NULL, &tv); + + if (retval == -1) + { +#ifdef _MSC_VER + int error = WSAGetLastError(); + printf( "error %d\n", error ); +#else + perror("select()"); +// int error = errno; +// if ( error == EAGAIN || error == EWOULDBLOCK ) +#endif + assert(0); + return COMMLAYER_RET_FAILED; + } + else + { + return COMMLAYER_RET_TIMEOUT; + } +} + +uint8_t hal_wait_for( waiting_for* wf ) +{ + unsigned int timeout = wf->wait_time.high_t; + timeout <<= 16; + timeout += wf->wait_time.low_t; + uint8_t ret_code; + assert( wf->wait_legs == 0 ); // not implemented + assert( wf->wait_i2c == 0 ); // not implemented + if ( wf->wait_packet ) + { + ret_code = wait_for_communication_event( timeout ); + switch ( ret_code ) + { + case COMMLAYER_RET_FROM_DEV: return WAIT_RESULTED_IN_PACKET; break; + case COMMLAYER_RET_TIMEOUT: return WAIT_RESULTED_IN_TIMEOUT; break; + case COMMLAYER_RET_FAILED: return WAIT_RESULTED_IN_FAILURE; break; + default: return WAIT_RESULTED_IN_FAILURE; + } + } + else + { + ret_code = wait_for_timeout( timeout ); + switch ( ret_code ) + { + case COMMLAYER_RET_TIMEOUT: return WAIT_RESULTED_IN_TIMEOUT; break; + default: return WAIT_RESULTED_IN_FAILURE; + } + } +} +