====================================================================================================
INCLUDE FILES
==================================================================================================*/
#include "TC_Tcmd.h"
#include "TC_Utility.h"
#include "TC_Utils.h"
#include "TC_SensorMonitor.h"
#include <stdio.h>
#include <string.h>
#include <sys/times.h>
#include <sys/select.h>
/*==================================================================================================
FUNCTION DEFINITIONS
==================================================================================================*/
static int event_exist(const char* filename );
/*==================================================================================================
FUNCTION:TC_Monitor_SensorHandler()
DESCRIPTION: This handler is responsible for monitoring key input value.
ARGUMENTS PASSED:
TC_TCMD_T* tcmd - Pointer to TCMD data structure
RETURN VALUE:
void
IMPORTANT NOTES:
None
==================================================================================================*/
void TC_Monitor_SensorHandler(TC_TCMD_T* tcmd)
{
UINT32 length=0;
int fd[2];// save file descripter
int index=0;// as fd[FD_ARRAY_MAX] index to know the opened descripter;
int i=0,ret=0;
int flag; //as a flag to know which fd in fd_set
fd_set fds;
struct input_event lsensor_data;
struct input_event gsensor_data[3];
//read buffer
char read_buffer[READ_LEN];
//return buffer
char return_buffer[50];
memset(read_buffer, 0, sizeof(read_buffer));
memset(return_buffer, 0, sizeof(return_buffer));
/* parse parameter */
length = tcmd->cmd_header.length;
TCMD_TRACE ("TC_SensorMonitor.c receive Opcode 0x%04x was found, length 0x%04x\n", tcmd->cmd_header.opcode,length);
/*START */
if( (ret=system(GSENSOR_START))<0 )
{
TCMD_TRACE( "echo on gsensor failed\n" );
TC_SendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);
return;
}
if( (ret=system(LSENSOR_START))<0 )
{
TCMD_TRACE( "echo off lsensor failed\n" );
TC_SendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);
return;
}
//LSENSOR : open lsensor device ;
if( event_exist(LSENSOR_NAME ) == 0 )
{
fd[0] = open( LSENSOR_NAME , O_RDONLY );
if( fd[0] < 0 )
{
TCMD_TRACE("Error opening the file:%s \n", LSENSOR_NAME );
TC_SendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);
return;
}
else
{
TCMD_TRACE(" open device %s successfully,fd = %d\n", LSENSOR_NAME , fd[0]);
}
}
//GSENSOR : open gsensor device;
if( event_exist(GSENSOR_NAME ) == 0 )
{
fd[1] = open( GSENSOR_NAME , O_RDONLY );
if( fd[1] < 0 )
{
close( fd[0] );
TCMD_TRACE("Error opening the file:%s \n", GSENSOR_NAME );
TC_SendResponse(tcmd,TC_CMD_RES_GEN_FAIL,TC_RSP_FLAG_FAIL,0, NULL);
return;
}
else
{
TCMD_TRACE(" open device %s successfully,fd = %d\n", GSENSOR_NAME , fd[1]);
}
}
//use function select to block
while(1)
{
FD_ZERO( &fds );
FD_SET( fd[0], &fds );
FD_SET( fd[1], &fds );
ret = select( fd[1]+1, &fds, NULL, NULL, NULL);
if( ret > 0 )
{
// which file descripter is selected
for( i=0; i<2; i++ )
{
if( FD_ISSET(fd[i],&fds)==1 )
{
flag = i;
TCMD_TRACE( "flag is %d\n", flag );
break;
}
}
// read file
if( flag == 0 ) //lsensor contents
{
ret = read( fd[0], &lsensor_data, sizeof(lsensor_data) );
if( ret > 0 )
sprintf( return_buffer,"Lsensor:type-%d;code -%d; value- %d", lsensor_data.type, lsensor_data.code, lsensor_data.value);
TCMD_TRACE("read length :%d return_buffer:%s\n", ret,return_buffer);
break;
}
else// gsensor
{
ret = read( fd[1], gsensor_data, sizeof(gsensor_data) );
if( ret > 0 )
{
TCMD_TRACE( "read length is %d\n",ret );
sprintf( return_buffer,"Gsensor:x-%d y-%d z-%d ", gsensor_data[0].value, gsensor_data[1].value, gsensor_data[2].value);//gsensor contents
}
else
{
TCMD_TRACE( " file descripter: %d ,read length is %d ,error is %s", fd[1], ret, strerror(errno) );
}
break;
}
}//end ret>0
else
{
TCMD_TRACE( "select failed :%s", strerror(errno) );
}
}//end while
// close file
TCMD_TRACE("close all device\n");
for( i=0; i< 2; i++)
{
close(fd[i]);
}
// response
TCMD_TRACE("return buffer is : %s \n" ,return_buffer);
TC_SendResponse(tcmd,TC_CMD_RES_GEN_SUCC,TC_RSP_FLAG_NONE,strlen(return_buffer), return_buffer);
return;
}
static int event_exist(const char* filename )
{
int status = -1;
status = access( filename , F_OK);
if( status < 0 )
{
printf( "%s does not exist \n", filename);
return -1;
}
else
{
printf( "%s exist \n", filename);
return 0;
}
}
|