Skip to content

Commit

Permalink
Emulator actual embodiment: update of hal-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
divanchykhin committed Jun 1, 2015
1 parent 6cae114 commit 9153783
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 22 deletions.
20 changes: 7 additions & 13 deletions tests/emulator/hal/sa-eeprom.h → tests/emulator/hal/hal-eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
#endif // __HAL_EEPROM_H__
43 changes: 43 additions & 0 deletions tests/emulator/hal/hal-waiting.h
Original file line number Diff line number Diff line change
@@ -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__
3 changes: 2 additions & 1 deletion tests/emulator/hal/sa-commlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
7 changes: 4 additions & 3 deletions tests/emulator/hal/sa-hal-time-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Copyright (C) 2015 OLogN Technologies AG
#define __SA_HAL_TIME_PROVIDER_H__

#include "../sa-common.h"
#include <hal-time-convertions.h>

typedef struct _sa_time_struct
{
Expand All @@ -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



Expand Down
116 changes: 116 additions & 0 deletions tests/emulator/hal/win-lnx-common/hal-eeprom.c
Original file line number Diff line number Diff line change
@@ -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 <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#ifdef _MSC_VER
#include <windows.h>
#include <io.h>
#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;
}
*/
27 changes: 27 additions & 0 deletions tests/emulator/hal/win-lnx-common/hal-time-convertions.h
Original file line number Diff line number Diff line change
@@ -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__
70 changes: 65 additions & 5 deletions tests/emulator/hal/win-lnx-common/sa-commlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Copyright (C) 2015 OLogN Technologies AG
*******************************************************************************/

#include "../sa-commlayer.h"
#include "../hal-waiting.h"
#include <stdio.h>


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -556,21 +558,21 @@ 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
else
{
// 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
Expand All @@ -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;
}
}
}

0 comments on commit 9153783

Please sign in to comment.