update esppilight lib
content of the library is not filtered on 433mhz only
This commit is contained in:
1 parent
a275e48ee1
commit
67dd7ac078
1030 files changed
+203335
-116
No files matched your search
@@ -0,0 +1,6 @@
|
||||
project(openzwave_platform_unix)
|
||||
|
||||
file(GLOB ${PROJECT_NAME}_sources "${PROJECT_SOURCE_DIR}/*.cpp")
|
||||
file(GLOB ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/*.h")
|
||||
|
||||
set(${PROJECT_NAME}_files ${${PROJECT_NAME}_sources} ${${PROJECT_NAME}_headers} CACHE INTERNAL "")
|
||||
@@ -0,0 +1,262 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// EventImpl.cpp
|
||||
//
|
||||
// POSIX implementation of a cross-platform event
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "../../Defs.h"
|
||||
#include "EventImpl.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::EventImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
EventImpl::EventImpl
|
||||
(
|
||||
):
|
||||
m_manualReset( true ),
|
||||
m_isSignaled( false ),
|
||||
m_waitingThreads( 0 )
|
||||
{
|
||||
pthread_mutexattr_t ma;
|
||||
pthread_mutexattr_init( &ma );
|
||||
pthread_mutexattr_settype( &ma, PTHREAD_MUTEX_ERRORCHECK );
|
||||
pthread_mutex_init( &m_lock, &ma );
|
||||
pthread_mutexattr_destroy( &ma );
|
||||
|
||||
pthread_condattr_t ca;
|
||||
pthread_condattr_init( &ca );
|
||||
pthread_condattr_setpshared( &ca, PTHREAD_PROCESS_PRIVATE );
|
||||
pthread_cond_init( &m_condition, &ca );
|
||||
pthread_condattr_destroy( &ca );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::~EventImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
EventImpl::~EventImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
pthread_mutex_destroy( &m_lock );
|
||||
pthread_cond_destroy( &m_condition );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::Set>
|
||||
// Set the event to signalled
|
||||
//-----------------------------------------------------------------------------
|
||||
void EventImpl::Set
|
||||
(
|
||||
)
|
||||
{
|
||||
int err = pthread_mutex_lock( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Set lock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
if( m_manualReset )
|
||||
{
|
||||
m_isSignaled = true;
|
||||
err = pthread_cond_broadcast( &m_condition );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Set cond broadcast error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( !m_waitingThreads )
|
||||
{
|
||||
m_isSignaled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = pthread_cond_signal( &m_condition );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Set cond signal error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
err = pthread_mutex_unlock( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Set unlock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::Reset>
|
||||
// Set the event to not signalled
|
||||
//-----------------------------------------------------------------------------
|
||||
void EventImpl::Reset
|
||||
(
|
||||
)
|
||||
{
|
||||
int err = pthread_mutex_lock ( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Reset lock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
m_isSignaled = false;
|
||||
err = pthread_mutex_unlock( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Reset unlock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::IsSignalled>
|
||||
// Test whether the event is set
|
||||
//-----------------------------------------------------------------------------
|
||||
bool EventImpl::IsSignalled
|
||||
(
|
||||
)
|
||||
{
|
||||
return m_isSignaled;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <EventImpl::Wait>
|
||||
// Wait for the event to become signalled
|
||||
//-----------------------------------------------------------------------------
|
||||
bool EventImpl::Wait
|
||||
(
|
||||
int32 const _timeout /* milliseconds */
|
||||
)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
int err = pthread_mutex_lock( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Wait lock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
if( m_isSignaled )
|
||||
{
|
||||
if ( !m_manualReset )
|
||||
{
|
||||
m_isSignaled = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++m_waitingThreads;
|
||||
if( _timeout == 0 )
|
||||
{
|
||||
result = m_isSignaled;
|
||||
}
|
||||
else if( _timeout > 0 )
|
||||
{
|
||||
struct timeval now;
|
||||
struct timespec abstime;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
abstime.tv_sec = now.tv_sec + (_timeout / 1000);
|
||||
|
||||
// Now add the remainder of our timeout to the microseconds part of 'now'
|
||||
now.tv_usec += (_timeout % 1000) * 1000;
|
||||
|
||||
// Careful now! Did it wrap?
|
||||
while( now.tv_usec >= ( 1000 * 1000 ) )
|
||||
{
|
||||
// Yes it did so bump our seconds and subtract
|
||||
now.tv_usec -= (1000 * 1000);
|
||||
abstime.tv_sec++;
|
||||
}
|
||||
|
||||
abstime.tv_nsec = now.tv_usec * 1000;
|
||||
|
||||
while( !m_isSignaled )
|
||||
{
|
||||
int oldstate;
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
|
||||
|
||||
err = pthread_cond_timedwait( &m_condition, &m_lock, &abstime );
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
|
||||
|
||||
if( err == ETIMEDOUT )
|
||||
{
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
if( err == 0 )
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Wait cond timedwait error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while( !m_isSignaled )
|
||||
{
|
||||
int oldstate;
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
|
||||
|
||||
err = pthread_cond_wait( &m_condition, &m_lock );
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
|
||||
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Wait cond wait error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
--m_waitingThreads;
|
||||
}
|
||||
|
||||
err = pthread_mutex_unlock( &m_lock );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "EventImpl::Wait unlock error %d (%d)\n", errno, err );
|
||||
assert( 0 );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// EventImpl.h
|
||||
//
|
||||
// POSIX implementation of a cross-platform event
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _EventImpl_H
|
||||
#define _EventImpl_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class EventImpl
|
||||
{
|
||||
private:
|
||||
friend class Event;
|
||||
friend class SocketImpl;
|
||||
friend class Wait;
|
||||
|
||||
EventImpl();
|
||||
~EventImpl();
|
||||
|
||||
void Set();
|
||||
void Reset();
|
||||
|
||||
bool Wait( int32 _timeout ); // The wait method is to be used only by the Wait::Multiple method
|
||||
bool IsSignalled();
|
||||
|
||||
pthread_mutex_t m_lock;
|
||||
pthread_cond_t m_condition;
|
||||
bool m_manualReset;
|
||||
bool m_isSignaled;
|
||||
unsigned int m_waitingThreads;
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_EventImpl_H
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// FileOpsImpl.cpp
|
||||
//
|
||||
// Unix implementation of file operations
|
||||
//
|
||||
// Copyright (c) 2012, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <dirent.h>
|
||||
#include "FileOpsImpl.h"
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <FileOpsImpl::FileOpsImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
FileOpsImpl::FileOpsImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <FileOpsImpl::~FileOpsImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
FileOpsImpl::~FileOpsImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <FileOpsImpl::FolderExists>
|
||||
// Determine if a folder exists
|
||||
//-----------------------------------------------------------------------------
|
||||
bool FileOpsImpl::FolderExists
|
||||
(
|
||||
string _folderName
|
||||
)
|
||||
{
|
||||
DIR *dirp = opendir( _folderName.c_str() );
|
||||
if( dirp != NULL )
|
||||
{
|
||||
closedir( dirp );
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// FileOpsImpl.h
|
||||
//
|
||||
// Unix implementation of file operations
|
||||
//
|
||||
// Copyright (c) 2012, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _FileOpsImpl_H
|
||||
#define _FileOpsImpl_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <string>
|
||||
#include "../../Defs.h"
|
||||
#include "../FileOps.h"
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class FileOpsImpl
|
||||
{
|
||||
friend class FileOps;
|
||||
|
||||
private:
|
||||
FileOpsImpl();
|
||||
~FileOpsImpl();
|
||||
|
||||
bool FolderExists( string _filename );
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_FileOpsImpl_H
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// LogImpl.cpp
|
||||
//
|
||||
// Unix implementation of message and error logging
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include "../../Defs.h"
|
||||
#include "LogImpl.h"
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::LogImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
LogImpl::LogImpl
|
||||
(
|
||||
string const& _filename,
|
||||
bool const _bAppendLog,
|
||||
bool const _bConsoleOutput,
|
||||
LogLevel const _saveLevel,
|
||||
LogLevel const _queueLevel,
|
||||
LogLevel const _dumpTrigger
|
||||
):
|
||||
m_filename( _filename ), // name of log file
|
||||
m_bConsoleOutput( _bConsoleOutput ), // true to provide a copy of output to console
|
||||
m_bAppendLog( _bAppendLog ), // true to append (and not overwrite) any existing log
|
||||
m_saveLevel( _saveLevel ), // level of messages to log to file
|
||||
m_queueLevel( _queueLevel ), // level of messages to log to queue
|
||||
m_dumpTrigger( _dumpTrigger ) // dump queued messages when this level is seen
|
||||
{
|
||||
if ( !m_bAppendLog )
|
||||
{
|
||||
this->pFile = fopen( m_filename.c_str(), "w" );
|
||||
} else {
|
||||
this->pFile = fopen( m_filename.c_str(), "a" );
|
||||
}
|
||||
if( this->pFile == NULL )
|
||||
{
|
||||
std::cerr << "Could Not Open OZW Log File." << std::endl;
|
||||
}
|
||||
setlinebuf(stdout); // To prevent buffering and lock contention issues
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::~LogImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
LogImpl::~LogImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
fclose( this->pFile );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::Write>
|
||||
// Write to the log
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::Write
|
||||
(
|
||||
LogLevel _logLevel,
|
||||
uint8 const _nodeId,
|
||||
char const* _format,
|
||||
va_list _args
|
||||
)
|
||||
{
|
||||
// create a timestamp string
|
||||
string timeStr = GetTimeStampString();
|
||||
string nodeStr = GetNodeString( _nodeId );
|
||||
string loglevelStr = GetLogLevelString(_logLevel);
|
||||
|
||||
// handle this message
|
||||
if( (_logLevel <= m_queueLevel) || (_logLevel == LogLevel_Internal) ) // we're going to do something with this message...
|
||||
{
|
||||
char lineBuf[1024] = {0};
|
||||
//int lineLen = 0;
|
||||
if( _format != NULL && _format[0] != '\0' )
|
||||
{
|
||||
va_list saveargs;
|
||||
va_copy( saveargs, _args );
|
||||
|
||||
vsnprintf( lineBuf, sizeof(lineBuf), _format, _args );
|
||||
va_end( saveargs );
|
||||
}
|
||||
|
||||
// should this message be saved to file (and possibly written to console?)
|
||||
if( (_logLevel <= m_saveLevel) || (_logLevel == LogLevel_Internal) )
|
||||
{
|
||||
std::string outBuf;
|
||||
|
||||
if ( this->pFile != NULL || m_bConsoleOutput )
|
||||
{
|
||||
if( _logLevel != LogLevel_Internal ) // don't add a second timestamp to display of queued messages
|
||||
{
|
||||
outBuf.append(timeStr);
|
||||
outBuf.append(loglevelStr);
|
||||
outBuf.append(nodeStr);
|
||||
outBuf.append(lineBuf);
|
||||
outBuf.append("\n");
|
||||
|
||||
}
|
||||
|
||||
// print message to file (and possibly screen)
|
||||
if( this->pFile != NULL )
|
||||
{
|
||||
fputs( outBuf.c_str(), pFile );
|
||||
}
|
||||
if( m_bConsoleOutput )
|
||||
{
|
||||
fputs( outBuf.c_str(), stdout );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( _logLevel != LogLevel_Internal )
|
||||
{
|
||||
char queueBuf[1024];
|
||||
string threadStr = GetThreadId();
|
||||
snprintf( queueBuf, sizeof(queueBuf), "%s%s%s", timeStr.c_str(), threadStr.c_str(), lineBuf );
|
||||
Queue( queueBuf );
|
||||
}
|
||||
}
|
||||
|
||||
// now check to see if the _dumpTrigger has been hit
|
||||
if( (_logLevel <= m_dumpTrigger) && (_logLevel != LogLevel_Internal) && (_logLevel != LogLevel_Always) )
|
||||
QueueDump();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::Queue>
|
||||
// Write to the log queue
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::Queue
|
||||
(
|
||||
char const* _buffer
|
||||
)
|
||||
{
|
||||
string bufStr = _buffer;
|
||||
m_logQueue.push_back( bufStr );
|
||||
|
||||
// rudimentary queue size management
|
||||
if( m_logQueue.size() > 500 )
|
||||
{
|
||||
m_logQueue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::QueueDump>
|
||||
// Dump the LogQueue to output device
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::QueueDump
|
||||
(
|
||||
)
|
||||
{
|
||||
Log::Write( LogLevel_Always, "" );
|
||||
Log::Write( LogLevel_Always, "Dumping queued log messages");
|
||||
Log::Write( LogLevel_Always, "" );
|
||||
list<string>::iterator it = m_logQueue.begin();
|
||||
while( it != m_logQueue.end() )
|
||||
{
|
||||
string strTemp = *it;
|
||||
Log::Write( LogLevel_Internal, strTemp.c_str() );
|
||||
it++;
|
||||
}
|
||||
m_logQueue.clear();
|
||||
Log::Write( LogLevel_Always, "" );
|
||||
Log::Write( LogLevel_Always, "End of queued log message dump");
|
||||
Log::Write( LogLevel_Always, "" );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::Clear>
|
||||
// Clear the LogQueue
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::QueueClear
|
||||
(
|
||||
)
|
||||
{
|
||||
m_logQueue.clear();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::SetLoggingState>
|
||||
// Sets the various log state variables
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::SetLoggingState
|
||||
(
|
||||
LogLevel _saveLevel,
|
||||
LogLevel _queueLevel,
|
||||
LogLevel _dumpTrigger
|
||||
)
|
||||
{
|
||||
m_saveLevel = _saveLevel;
|
||||
m_queueLevel = _queueLevel;
|
||||
m_dumpTrigger = _dumpTrigger;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::GetTimeStampString>
|
||||
// Generate a string with formatted current time
|
||||
//-----------------------------------------------------------------------------
|
||||
string LogImpl::GetTimeStampString
|
||||
(
|
||||
)
|
||||
{
|
||||
struct timeval tv;
|
||||
struct tm tm;
|
||||
char fmt[64], buf[64], line[256];
|
||||
gettimeofday(&tv, NULL);
|
||||
#ifdef _WIN32
|
||||
struct tm *tm1;
|
||||
if((tm1 = gmtime(&tv.tv_sec)) != 0) {
|
||||
memcpy(&tm, tm1, sizeof(struct tm));
|
||||
#else
|
||||
if((gmtime_r(&tv.tv_sec, &tm)) != 0) {
|
||||
#endif
|
||||
strftime(fmt, sizeof(fmt), "%b %d %H:%M:%S", &tm);
|
||||
snprintf(buf, sizeof(buf), "%s:%03u", fmt, (unsigned int)tv.tv_usec);
|
||||
}
|
||||
sprintf(line, "[%22.22s] %s: [Z-Wave]: ", buf, "pilight-daemon");
|
||||
|
||||
string str = line;
|
||||
return str;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::GetNodeString>
|
||||
// Generate a string with formatted node id
|
||||
//-----------------------------------------------------------------------------
|
||||
string LogImpl::GetNodeString
|
||||
(
|
||||
uint8 const _nodeId
|
||||
)
|
||||
{
|
||||
if( _nodeId == 0 )
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
if( _nodeId == 255 ) // should make distinction between broadcast and controller better for SwitchAll broadcast
|
||||
{
|
||||
return "contrlr, ";
|
||||
}
|
||||
else
|
||||
{
|
||||
char buf[20];
|
||||
snprintf( buf, sizeof(buf), "Node%03d, ", _nodeId );
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::GetThreadId>
|
||||
// Generate a string with formatted thread id
|
||||
//-----------------------------------------------------------------------------
|
||||
string LogImpl::GetThreadId
|
||||
(
|
||||
)
|
||||
{
|
||||
char buf[20];
|
||||
snprintf( buf, sizeof(buf), "%08lx ", (long unsigned int)pthread_self() );
|
||||
string str = buf;
|
||||
return str;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::SetLogFileName>
|
||||
// Provide a new log file name (applicable to future writes)
|
||||
//-----------------------------------------------------------------------------
|
||||
void LogImpl::SetLogFileName
|
||||
(
|
||||
const string &_filename
|
||||
)
|
||||
{
|
||||
m_filename = _filename;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <LogImpl::GetLogLevelString>
|
||||
// Provide a new log file name (applicable to future writes)
|
||||
//-----------------------------------------------------------------------------
|
||||
string LogImpl::GetLogLevelString
|
||||
(
|
||||
LogLevel _level
|
||||
)
|
||||
{
|
||||
if ((_level >= LogLevel_None) && (_level <= LogLevel_Internal)) {
|
||||
char buf[20];
|
||||
snprintf( buf, sizeof(buf), "%s, ", LogLevelString[_level] );
|
||||
return buf;
|
||||
}
|
||||
else
|
||||
return "Unknown, ";
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// LogImpl.h
|
||||
//
|
||||
// Unix implementation of message and error logging
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _LogImpl_H
|
||||
#define _LogImpl_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <list>
|
||||
#include "../Log.h"
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class LogImpl : public i_LogImpl
|
||||
{
|
||||
private:
|
||||
friend class Log;
|
||||
|
||||
LogImpl( string const& _filename, bool const _bAppendLog, bool const _bConsoleOutput, LogLevel const _saveLevel, LogLevel const _queueLevel, LogLevel const _dumpTrigger );
|
||||
~LogImpl();
|
||||
|
||||
void Write( LogLevel _level, uint8 const _nodeId, char const* _format, va_list _args );
|
||||
void Queue( char const* _buffer );
|
||||
void QueueDump();
|
||||
void QueueClear();
|
||||
void SetLoggingState( LogLevel _saveLevel, LogLevel _queueLevel, LogLevel _dumpTrigger );
|
||||
void SetLogFileName( const string &_filename );
|
||||
|
||||
string GetTimeStampString();
|
||||
string GetNodeString( uint8 const _nodeId );
|
||||
string GetThreadId();
|
||||
string GetLogLevelString(LogLevel _level);
|
||||
|
||||
string m_filename; /**< filename specified by user (default is ozw_log.txt) */
|
||||
bool m_bConsoleOutput; /**< if true, send log output to console as well as to the file */
|
||||
bool m_bAppendLog; /**< if true, the log file should be appended to any with the same name */
|
||||
list<string> m_logQueue; /**< list of queued log messages */
|
||||
LogLevel m_saveLevel;
|
||||
LogLevel m_queueLevel;
|
||||
LogLevel m_dumpTrigger;
|
||||
FILE* pFile;
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_LogImpl_H
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
//----------------------------------------------------------------------------
|
||||
//
|
||||
// MutexImpl.cpp
|
||||
//
|
||||
// POSIX implementation of the cross-platform mutex
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "../../Defs.h"
|
||||
#include "../Log.h"
|
||||
#include "MutexImpl.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <MutexImpl::MutexImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
MutexImpl::MutexImpl
|
||||
(
|
||||
):
|
||||
m_lockCount( 0 )
|
||||
{
|
||||
pthread_mutexattr_t ma;
|
||||
|
||||
pthread_mutexattr_init ( &ma );
|
||||
pthread_mutexattr_settype( &ma, PTHREAD_MUTEX_RECURSIVE );
|
||||
int err = pthread_mutex_init( &m_criticalSection, &ma );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "MutexImpl::MutexImpl error %d (%d)\n", errno, err );
|
||||
}
|
||||
pthread_mutexattr_destroy( &ma );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <MutexImpl::~MutexImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
MutexImpl::~MutexImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
pthread_mutex_destroy( &m_criticalSection );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <MutexImpl::Lock>
|
||||
// Lock the mutex
|
||||
//-----------------------------------------------------------------------------
|
||||
bool MutexImpl::Lock
|
||||
(
|
||||
bool const _bWait
|
||||
)
|
||||
{
|
||||
if( _bWait )
|
||||
{
|
||||
// We will wait for the lock
|
||||
int err = pthread_mutex_lock( &m_criticalSection );
|
||||
if( err == 0 )
|
||||
{
|
||||
++m_lockCount;
|
||||
return true;
|
||||
}
|
||||
fprintf(stderr, "MutexImpl::Lock error %d (%d)\n", errno, err);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Returns immediately, even if the lock was not available.
|
||||
if( pthread_mutex_trylock( &m_criticalSection ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
++m_lockCount;
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <MutexImpl::Unlock>
|
||||
// Release our lock on the mutex
|
||||
//-----------------------------------------------------------------------------
|
||||
void MutexImpl::Unlock
|
||||
(
|
||||
)
|
||||
{
|
||||
if( !m_lockCount )
|
||||
{
|
||||
// No locks - we have a mismatched lock/release pair
|
||||
assert(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
--m_lockCount;
|
||||
int err = pthread_mutex_unlock( &m_criticalSection );
|
||||
if( err != 0 )
|
||||
{
|
||||
fprintf(stderr, "MutexImpl::Unlock error %d (%d)\n", errno, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <MutexImpl::IsSignalled>
|
||||
// Test whether the mutex is free
|
||||
//-----------------------------------------------------------------------------
|
||||
bool MutexImpl::IsSignalled
|
||||
(
|
||||
)
|
||||
{
|
||||
return( 0 == m_lockCount );
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
//----------------------------------------------------------------------------
|
||||
//
|
||||
// MutexImpl.h
|
||||
//
|
||||
// POSIX implementation of the cross-platform mutex
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _MutexImpl_H
|
||||
#define _MutexImpl_H
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class MutexImpl
|
||||
{
|
||||
private:
|
||||
friend class Mutex;
|
||||
|
||||
MutexImpl();
|
||||
~MutexImpl();
|
||||
|
||||
bool Lock( bool const _bWait = true );
|
||||
void Unlock();
|
||||
|
||||
bool IsSignalled();
|
||||
|
||||
uint32 m_lockCount; // Keep track of the locks (there can be more than one if they occur on the same thread.
|
||||
pthread_mutex_t m_criticalSection;
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_MutexIF_H
|
||||
|
||||
@@ -0,0 +1,373 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// SerialControllerImpl.cpp
|
||||
//
|
||||
// POSIX implementation of a cross-platform serial port
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <unistd.h>
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include "../../Defs.h"
|
||||
#include "../Thread.h"
|
||||
#include "../Event.h"
|
||||
#include "SerialControllerImpl.h"
|
||||
#include "../Log.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include "../../libudev.h"
|
||||
#endif
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::SerialControllerImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
SerialControllerImpl::SerialControllerImpl
|
||||
(
|
||||
SerialController* _owner
|
||||
):
|
||||
m_owner( _owner ),
|
||||
m_hSerialController( -1 )
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::~SerialControllerImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
SerialControllerImpl::~SerialControllerImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
flock(m_hSerialController, LOCK_UN);
|
||||
if(m_hSerialController >= 0)
|
||||
close( m_hSerialController );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::Open>
|
||||
// Open the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
bool SerialControllerImpl::Open
|
||||
(
|
||||
)
|
||||
{
|
||||
// Try to init the serial port
|
||||
if( !Init( 1 ) )
|
||||
{
|
||||
// Failed. We bail to allow the app a chance to take over, rather than retry
|
||||
// automatically. Automatic retries only occur after a successful init.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start the read thread
|
||||
m_pThread = new Thread( "SerialController" );
|
||||
m_pThread->Start( SerialReadThreadEntryPoint, this );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::Close>
|
||||
// Close the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
void SerialControllerImpl::Close
|
||||
(
|
||||
)
|
||||
{
|
||||
if( m_pThread )
|
||||
{
|
||||
m_pThread->Stop();
|
||||
m_pThread->Release();
|
||||
m_pThread = NULL;
|
||||
}
|
||||
close( m_hSerialController );
|
||||
m_hSerialController = -1;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialReadThreadEntryPoint>
|
||||
// Entry point of the thread for receiving data from the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
void SerialControllerImpl::SerialReadThreadEntryPoint
|
||||
(
|
||||
Event* _exitEvent,
|
||||
void* _context
|
||||
)
|
||||
{
|
||||
SerialControllerImpl* impl = (SerialControllerImpl*)_context;
|
||||
if( impl )
|
||||
{
|
||||
impl->ReadThreadProc( _exitEvent );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::ReadThreadProc>
|
||||
// Handle receiving data
|
||||
//-----------------------------------------------------------------------------
|
||||
void SerialControllerImpl::ReadThreadProc
|
||||
(
|
||||
Event* _exitEvent
|
||||
)
|
||||
{
|
||||
uint32 attempts = 0;
|
||||
while( true )
|
||||
{
|
||||
// Init must have been called successfully during Open, so we
|
||||
// don't do it again until the end of the loop
|
||||
if( -1 != m_hSerialController )
|
||||
{
|
||||
// Enter read loop. Call will only return if
|
||||
// an exit is requested or an error occurs
|
||||
Read();
|
||||
|
||||
// Reset the attempts, so we get a rapid retry for temporary errors
|
||||
attempts = 0;
|
||||
}
|
||||
|
||||
if( attempts < 25 )
|
||||
{
|
||||
// Retry every 5 seconds for the first two minutes...
|
||||
if( Wait::Single( _exitEvent, 5000 ) >= 0 )
|
||||
{
|
||||
// Exit signalled.
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// ...retry every 30 seconds after that
|
||||
if( Wait::Single( _exitEvent, 30000 ) >= 0 )
|
||||
{
|
||||
// Exit signalled.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Init( ++attempts );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::Init>
|
||||
// Initialize the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
bool SerialControllerImpl::Init
|
||||
(
|
||||
uint32 const _attempts
|
||||
)
|
||||
{
|
||||
|
||||
string device = m_owner->m_serialControllerName;
|
||||
|
||||
Log::Write( LogLevel_Info, "Trying to open serial port %s (attempt %d)", device.c_str(), _attempts );
|
||||
|
||||
m_hSerialController = open( device.c_str(), O_RDWR | O_NOCTTY, 0 );
|
||||
|
||||
if( -1 == m_hSerialController )
|
||||
{
|
||||
//Error
|
||||
Log::Write( LogLevel_Error, "ERROR: Cannot open serial port %s. Error code %d", device.c_str(), errno );
|
||||
goto SerialOpenFailure;
|
||||
}
|
||||
|
||||
if( flock( m_hSerialController, LOCK_EX | LOCK_NB) == -1 )
|
||||
{
|
||||
Log::Write( LogLevel_Error, "ERROR: Cannot get exclusive lock for serial port %s. Error code %d", device.c_str(), errno );
|
||||
}
|
||||
|
||||
int bits;
|
||||
bits = 0;
|
||||
ioctl( m_hSerialController, TIOCMSET, &bits );
|
||||
|
||||
// Configure the serial device parameters
|
||||
// Build on the current configuration
|
||||
struct termios tios;
|
||||
|
||||
bzero( &tios, sizeof(tios) );
|
||||
tcgetattr( m_hSerialController, &tios );
|
||||
switch( m_owner->m_parity )
|
||||
{
|
||||
case SerialController::Parity_None:
|
||||
tios.c_iflag = IGNPAR;
|
||||
break;
|
||||
case SerialController::Parity_Odd:
|
||||
tios.c_iflag = INPCK;
|
||||
tios.c_cflag = PARENB | PARODD;
|
||||
break;
|
||||
default:
|
||||
Log::Write( LogLevel_Error, "ERROR: Parity not supported" );
|
||||
goto SerialOpenFailure;
|
||||
}
|
||||
switch( m_owner->m_stopBits )
|
||||
{
|
||||
case SerialController::StopBits_One:
|
||||
break; // default
|
||||
case SerialController::StopBits_Two:
|
||||
tios.c_cflag |= CSTOPB;
|
||||
break;
|
||||
default:
|
||||
Log::Write( LogLevel_Error, "ERROR: Stopbits not supported" );
|
||||
goto SerialOpenFailure;
|
||||
}
|
||||
tios.c_iflag |= IGNBRK;
|
||||
tios.c_cflag |= CS8 | CREAD | CLOCAL;
|
||||
tios.c_oflag = 0;
|
||||
tios.c_lflag = 0;
|
||||
for( int i = 0; i < NCCS; i++ )
|
||||
tios.c_cc[i] = 0;
|
||||
tios.c_cc[VMIN] = 0;
|
||||
tios.c_cc[VTIME] = 1;
|
||||
switch( m_owner->m_baud )
|
||||
{
|
||||
case 300:
|
||||
cfsetspeed( &tios, B300 );
|
||||
break;
|
||||
case 1200:
|
||||
cfsetspeed( &tios, B1200 );
|
||||
break;
|
||||
case 2400:
|
||||
cfsetspeed( &tios, B2400 );
|
||||
break;
|
||||
case 4800:
|
||||
cfsetspeed( &tios, B4800 );
|
||||
break;
|
||||
case 9600:
|
||||
cfsetspeed( &tios, B9600 );
|
||||
break;
|
||||
case 19200:
|
||||
cfsetspeed( &tios, B19200 );
|
||||
break;
|
||||
case 38400:
|
||||
cfsetspeed( &tios, B38400 );
|
||||
break;
|
||||
case 57600:
|
||||
cfsetspeed( &tios, B57600 );
|
||||
break;
|
||||
#ifdef DARWIN
|
||||
case 76800:
|
||||
cfsetspeed( &tios, B76800 );
|
||||
break;
|
||||
#endif
|
||||
case 115200:
|
||||
cfsetspeed( &tios, B115200 );
|
||||
break;
|
||||
case 230400:
|
||||
cfsetspeed( &tios, B230400 );
|
||||
break;
|
||||
default:
|
||||
Log::Write( LogLevel_Error, "Baud rate not supported" );
|
||||
goto SerialOpenFailure;
|
||||
}
|
||||
if ( tcsetattr( m_hSerialController, TCSANOW, &tios ) == -1 )
|
||||
{
|
||||
// Error. Clean up and exit
|
||||
Log::Write( LogLevel_Error, "ERROR: Failed to set serial port parameters" );
|
||||
goto SerialOpenFailure;
|
||||
}
|
||||
|
||||
tcflush( m_hSerialController, TCIOFLUSH );
|
||||
|
||||
// Open successful
|
||||
Log::Write( LogLevel_Info, "Serial port %s opened (attempt %d)", device.c_str(), _attempts );
|
||||
return true;
|
||||
|
||||
SerialOpenFailure:
|
||||
Log::Write( LogLevel_Error, "ERROR: Failed to open serial port %s", device.c_str() );
|
||||
if(m_hSerialController >= 0)
|
||||
{
|
||||
close( m_hSerialController );
|
||||
m_hSerialController = -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::Read>
|
||||
// Read data from the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
void SerialControllerImpl::Read
|
||||
(
|
||||
)
|
||||
{
|
||||
uint8 buffer[256];
|
||||
|
||||
while( 1 )
|
||||
{
|
||||
int32 bytesRead;
|
||||
int err;
|
||||
|
||||
do
|
||||
{
|
||||
bytesRead = read( m_hSerialController, buffer, sizeof(buffer) );
|
||||
if( bytesRead > 0 )
|
||||
m_owner->Put( buffer, bytesRead );
|
||||
} while( bytesRead > 0 );
|
||||
|
||||
do
|
||||
{
|
||||
struct timeval *whenp;
|
||||
fd_set rds, eds;
|
||||
int oldstate;
|
||||
|
||||
FD_ZERO( &rds );
|
||||
FD_SET( m_hSerialController, &rds );
|
||||
FD_ZERO( &eds );
|
||||
FD_SET( m_hSerialController, &eds );
|
||||
whenp = NULL;
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
|
||||
err = select( m_hSerialController + 1, &rds, NULL, &eds, whenp );
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
|
||||
} while( err <= 0 );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <SerialControllerImpl::Write>
|
||||
// Send data to the serial port
|
||||
//-----------------------------------------------------------------------------
|
||||
uint32 SerialControllerImpl::Write
|
||||
(
|
||||
uint8* _buffer,
|
||||
uint32 _length
|
||||
)
|
||||
{
|
||||
if( -1 == m_hSerialController )
|
||||
{
|
||||
//Error
|
||||
Log::Write( LogLevel_Error, "ERROR: Serial port must be opened before writing" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Write the data
|
||||
uint32 bytesWritten;
|
||||
bytesWritten = write( m_hSerialController, _buffer, _length);
|
||||
return bytesWritten;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// SerialControllerImpl.h
|
||||
//
|
||||
// POSIX implementation of a cross-platform serial port
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _SerialControllerImpl_H
|
||||
#define _SerialControllerImpl_H
|
||||
|
||||
#include <strings.h>
|
||||
#include <termios.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "../../Defs.h"
|
||||
#include "../SerialController.h"
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class SerialControllerImpl
|
||||
{
|
||||
public:
|
||||
void ReadThreadProc( Event* _exitEvent );
|
||||
|
||||
private:
|
||||
friend class SerialController;
|
||||
|
||||
SerialControllerImpl( SerialController* _owner );
|
||||
~SerialControllerImpl();
|
||||
|
||||
bool Open();
|
||||
void Close();
|
||||
|
||||
uint32 Write( uint8* _buffer, uint32 _length );
|
||||
|
||||
bool Init( uint32 const _attempts );
|
||||
void Read();
|
||||
|
||||
SerialController* m_owner;
|
||||
int m_hSerialController;
|
||||
Thread* m_pThread;
|
||||
|
||||
static void SerialReadThreadEntryPoint( Event* _exitEvent, void* _content );
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_SerialControllerImpl_H
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
//----------------------------------------------------------------------------
|
||||
//
|
||||
// ThreadImpl.h
|
||||
//
|
||||
// POSIX implementation of a cross-platform thread
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <unistd.h>
|
||||
#include "../../Defs.h"
|
||||
#include "../Event.h"
|
||||
#include "../Thread.h"
|
||||
#include "ThreadImpl.h"
|
||||
|
||||
#ifdef DARWIN
|
||||
#define pthread_yield pthread_yield_np
|
||||
#endif
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::ThreadImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
ThreadImpl::ThreadImpl
|
||||
(
|
||||
Thread* _owner,
|
||||
string const& _tname
|
||||
):
|
||||
m_owner( _owner ),
|
||||
// m_hThread( NULL ), /* p_thread_t isn't a pointer in Linux, so can't do this */
|
||||
m_bIsRunning( false ),
|
||||
m_name( _tname )
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::~ThreadImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
ThreadImpl::~ThreadImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::Start>
|
||||
// Start a function running on this thread
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ThreadImpl::Start
|
||||
(
|
||||
Thread::pfnThreadProc_t _pfnThreadProc,
|
||||
Event* _exitEvent,
|
||||
void* _pContext
|
||||
)
|
||||
{
|
||||
pthread_attr_t ta;
|
||||
|
||||
pthread_attr_init( &ta );
|
||||
pthread_attr_setstacksize ( &ta, 0 );
|
||||
pthread_attr_setdetachstate ( &ta, PTHREAD_CREATE_JOINABLE );
|
||||
|
||||
// Create a thread to run the specified function
|
||||
m_pfnThreadProc = _pfnThreadProc;
|
||||
m_pContext = _pContext;
|
||||
m_exitEvent = _exitEvent;
|
||||
m_exitEvent->Reset();
|
||||
|
||||
pthread_create ( &m_hThread, &ta, ThreadImpl::ThreadProc, this );
|
||||
pthread_detach( m_hThread );
|
||||
//fprintf(stderr, "thread %s starting %08x\n", m_name.c_str(), m_hThread);
|
||||
//fflush(stderr);
|
||||
|
||||
pthread_attr_destroy ( &ta );
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::Terminate>
|
||||
// End this thread
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ThreadImpl::Terminate
|
||||
(
|
||||
)
|
||||
{
|
||||
// void* data = NULL;
|
||||
|
||||
//fprintf(stderr, "thread %s stopping %08x running %d\n", m_name.c_str(), m_hThread, m_bIsRunning );
|
||||
//fflush(stderr);
|
||||
if( !m_bIsRunning )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// This will kill an app that doesn't catch and ignore it.
|
||||
// We need to find another way to interrupt select.
|
||||
// thread_kill( m_hThread, SIGALRM );
|
||||
|
||||
//m_hThread = NULL;
|
||||
m_bIsRunning = false;
|
||||
|
||||
// pthread_cancel( m_hThread );
|
||||
// pthread_join( m_hThread, &data );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::msSleep>
|
||||
// Cause thread to sleep for the specified number of milliseconds
|
||||
//-----------------------------------------------------------------------------
|
||||
void ThreadImpl::Sleep
|
||||
(
|
||||
uint32 _millisecs
|
||||
)
|
||||
{
|
||||
usleep( _millisecs*1000 );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::IsSignalled>
|
||||
// Test whether the thread has completed
|
||||
//-----------------------------------------------------------------------------
|
||||
bool ThreadImpl::IsSignalled
|
||||
(
|
||||
)
|
||||
{
|
||||
return !m_bIsRunning;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::ThreadProc>
|
||||
// Entry point for running a function on this thread
|
||||
//-----------------------------------------------------------------------------
|
||||
void *ThreadImpl::ThreadProc
|
||||
(
|
||||
void* _pArg
|
||||
)
|
||||
{
|
||||
ThreadImpl* pImpl = (ThreadImpl*)_pArg;
|
||||
//fprintf(stderr, "thread %s run begin %08x running %d\n", pImpl->m_name.c_str(), pImpl->m_hThread, pImpl->m_bIsRunning );
|
||||
//fflush(stderr);
|
||||
pImpl->Run();
|
||||
//fprintf(stderr, "thread %s run end %08x running %d\n", pImpl->m_name.c_str(), pImpl->m_hThread, pImpl->m_bIsRunning );
|
||||
//fflush(stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <ThreadImpl::Run>
|
||||
// Entry point for running a function on this thread
|
||||
//-----------------------------------------------------------------------------
|
||||
void ThreadImpl::Run
|
||||
(
|
||||
)
|
||||
{
|
||||
m_bIsRunning = true;
|
||||
m_pfnThreadProc( m_exitEvent, m_pContext );
|
||||
m_bIsRunning = false;
|
||||
|
||||
// Let any watchers know that the thread has finished running
|
||||
m_owner->Notify();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//----------------------------------------------------------------------------
|
||||
//
|
||||
// ThreadImpl.h
|
||||
//
|
||||
// POSIX implementation of a cross-platform thread
|
||||
//
|
||||
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _ThreadImpl_H
|
||||
#define _ThreadImpl_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include <string>
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
class Thread;
|
||||
class Event;
|
||||
|
||||
class ThreadImpl
|
||||
{
|
||||
private:
|
||||
friend class Thread;
|
||||
|
||||
ThreadImpl( Thread* _owner, string const& _tname );
|
||||
~ThreadImpl();
|
||||
|
||||
bool Start( Thread::pfnThreadProc_t _pfnThreadProc, Event* _exitEvent, void* _context );
|
||||
void Sleep( uint32 _millisecs );
|
||||
bool IsSignalled();
|
||||
bool Terminate();
|
||||
|
||||
void Run();
|
||||
static void* ThreadProc( void *parg);
|
||||
|
||||
Thread* m_owner;
|
||||
Event* m_exitEvent;
|
||||
pthread_t m_hThread;
|
||||
Thread::pfnThreadProc_t m_pfnThreadProc;
|
||||
void* m_pContext;
|
||||
bool m_bIsRunning;
|
||||
string m_name;
|
||||
};
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_ThreadImpl_H
|
||||
@@ -0,0 +1,141 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// TimeStampImpl.cpp
|
||||
//
|
||||
// OSX implementation of a TimeStamp
|
||||
//
|
||||
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include "../../Defs.h"
|
||||
#include "TimeStampImpl.h"
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::TimeStampImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
TimeStampImpl::TimeStampImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
SetTime(0);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::~TimeStampImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
TimeStampImpl::~TimeStampImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::SetTime>
|
||||
// Sets the timestamp to now, plus an offset in milliseconds
|
||||
//-----------------------------------------------------------------------------
|
||||
void TimeStampImpl::SetTime
|
||||
(
|
||||
int32 _milliseconds // = 0
|
||||
)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
m_stamp.tv_sec = now.tv_sec + (_milliseconds / 1000);
|
||||
|
||||
// Now add the remainder of our timeout to the microseconds part of 'now'
|
||||
now.tv_usec += ((_milliseconds % 1000) * 1000);
|
||||
|
||||
// Careful now! Did it wrap?
|
||||
if(now.tv_usec >= 1000000)
|
||||
{
|
||||
// Yes it did so bump our seconds and modulo
|
||||
now.tv_usec %= 1000000;
|
||||
m_stamp.tv_sec++;
|
||||
}
|
||||
|
||||
m_stamp.tv_nsec = now.tv_usec * 1000;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::TimeRemaining>
|
||||
// Gets the difference between now and the timestamp time in milliseconds
|
||||
//-----------------------------------------------------------------------------
|
||||
int32 TimeStampImpl::TimeRemaining
|
||||
(
|
||||
)
|
||||
{
|
||||
int32 diff;
|
||||
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
// Seconds
|
||||
diff = (int32)((m_stamp.tv_sec - now.tv_sec)*1000);
|
||||
|
||||
// Milliseconds
|
||||
diff += (((m_stamp.tv_nsec/1000)-now.tv_usec)/1000);
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::GetAsString>
|
||||
// Return a string representation
|
||||
//-----------------------------------------------------------------------------
|
||||
string TimeStampImpl::GetAsString
|
||||
(
|
||||
)
|
||||
{
|
||||
char str[100];
|
||||
struct tm *tm;
|
||||
tm = localtime( &m_stamp.tv_sec );
|
||||
|
||||
snprintf( str, sizeof(str), "%04d-%02d-%02d %02d:%02d:%02d:%03d ",
|
||||
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(m_stamp.tv_nsec / (1000*1000)) );
|
||||
return str;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <TimeStampImpl::operator->
|
||||
// Overload the subtract operator to get the difference between two
|
||||
// timestamps in milliseconds
|
||||
//-----------------------------------------------------------------------------
|
||||
int32 TimeStampImpl::operator-
|
||||
(
|
||||
TimeStampImpl const& _other
|
||||
)
|
||||
{
|
||||
// Seconds
|
||||
int32 diff = (int32)((m_stamp.tv_sec-_other.m_stamp.tv_sec)*1000);
|
||||
|
||||
// Milliseconds
|
||||
diff += ((m_stamp.tv_nsec - _other.m_stamp.tv_nsec)/1000000);
|
||||
|
||||
return diff;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// TimeStampImpl.h
|
||||
//
|
||||
// OSX implementation of a TimeStamp
|
||||
//
|
||||
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _TimeStampImpl_H
|
||||
#define _TimeStampImpl_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include "../../Defs.h"
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
/** \brief Windows implementation of a timestamp.
|
||||
*/
|
||||
class TimeStampImpl
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* Creates a TimeStampImpl object.
|
||||
*/
|
||||
TimeStampImpl();
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destroys the TimeStampImpl object.
|
||||
*/
|
||||
~TimeStampImpl();
|
||||
|
||||
/**
|
||||
* SetTime. Sets the timestamp to now, plus the offset in milliseconds.
|
||||
* \param _milliseconds positive or negative offset from
|
||||
* now in milliseconds.
|
||||
*/
|
||||
void SetTime( int32 _milliseconds );
|
||||
|
||||
/**
|
||||
* TimeRemaining. Gets the difference between now and the timestamp
|
||||
* time in milliseconds.
|
||||
* \return milliseconds remaining until we reach the timestamp. The
|
||||
* return value is negative if the timestamp is in the past.
|
||||
*/
|
||||
int32 TimeRemaining();
|
||||
|
||||
/**
|
||||
* Return as as string
|
||||
*/
|
||||
string GetAsString();
|
||||
|
||||
/**
|
||||
* Overload the subtract operator to get the difference between
|
||||
* two timestamps in milliseconds.
|
||||
*/
|
||||
int32 operator- ( TimeStampImpl const& _other );
|
||||
|
||||
private:
|
||||
TimeStampImpl( TimeStampImpl const& ); // prevent copy
|
||||
TimeStampImpl& operator = ( TimeStampImpl const& ); // prevent assignment
|
||||
|
||||
struct timespec m_stamp;
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_TimeStampImpl_H
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// WaitImpl.cpp
|
||||
//
|
||||
// POSIX implementation of an abstract base class for objects we
|
||||
// want to be able to wait for.
|
||||
//
|
||||
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "../../Defs.h"
|
||||
#include "../Wait.h"
|
||||
#include "WaitImpl.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
using namespace OpenZWave;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <WaitImpl::WaitImpl>
|
||||
// Constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
WaitImpl::WaitImpl
|
||||
(
|
||||
Wait* _owner
|
||||
):
|
||||
m_owner( _owner )
|
||||
{
|
||||
pthread_mutexattr_t ma;
|
||||
pthread_mutexattr_init ( &ma );
|
||||
pthread_mutexattr_settype( &ma, PTHREAD_MUTEX_RECURSIVE );
|
||||
pthread_mutex_init( &m_criticalSection, &ma );
|
||||
pthread_mutexattr_destroy( &ma );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <WaitImpl::~WaitImpl>
|
||||
// Destructor
|
||||
//-----------------------------------------------------------------------------
|
||||
WaitImpl::~WaitImpl
|
||||
(
|
||||
)
|
||||
{
|
||||
pthread_mutex_destroy( &m_criticalSection );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <WaitImpl::AddWatcher>
|
||||
// Add a watcher to our object.
|
||||
//-----------------------------------------------------------------------------
|
||||
void WaitImpl::AddWatcher
|
||||
(
|
||||
Wait::pfnWaitNotification_t _callback,
|
||||
void* _context
|
||||
)
|
||||
{
|
||||
// Add the watcher to our list
|
||||
Watcher watcher;
|
||||
watcher.m_callback = _callback;
|
||||
watcher.m_context = _context;
|
||||
|
||||
if( pthread_mutex_lock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::AddWatcher lock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
m_watchers.push_back( watcher );
|
||||
if( pthread_mutex_unlock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::AddWatcher unlock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
|
||||
// If the object is already in a signalled state, notify the watcher immediately
|
||||
if( m_owner->IsSignalled() )
|
||||
{
|
||||
_callback( _context );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <WaitImpl::RemoveWatcher>
|
||||
// Remove a watcher from our object.
|
||||
//-----------------------------------------------------------------------------
|
||||
bool WaitImpl::RemoveWatcher
|
||||
(
|
||||
Wait::pfnWaitNotification_t _callback,
|
||||
void* _context
|
||||
)
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
if( pthread_mutex_lock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::RemoveWatcher lock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
|
||||
for( list<Watcher>::iterator it=m_watchers.begin(); it!=m_watchers.end(); ++it )
|
||||
{
|
||||
Watcher const& watcher = *it;
|
||||
if( ( watcher.m_callback == _callback ) && ( watcher.m_context == _context ) )
|
||||
{
|
||||
m_watchers.erase( it );
|
||||
res = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( pthread_mutex_unlock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::RemoveWatcher unlock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// <WaitImpl::Notify>
|
||||
// Notify all the watchers that the object has become signalled
|
||||
//-----------------------------------------------------------------------------
|
||||
void WaitImpl::Notify
|
||||
(
|
||||
)
|
||||
{
|
||||
if( pthread_mutex_lock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::Notify lock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
for( list<Watcher>::iterator it=m_watchers.begin(); it!=m_watchers.end(); ++it )
|
||||
{
|
||||
Watcher const& watcher = *it;
|
||||
watcher.m_callback( watcher.m_context );
|
||||
}
|
||||
if( pthread_mutex_unlock( &m_criticalSection ) != 0 )
|
||||
{
|
||||
fprintf(stderr, "WaitImpl::Notify unlock error %d\n", errno );
|
||||
assert( 0 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// WaitImpl.h
|
||||
//
|
||||
// POSIX implementation of a base class for objects we
|
||||
// want to be able to wait for.
|
||||
//
|
||||
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// SOFTWARE NOTICE AND LICENSE
|
||||
//
|
||||
// This file is part of OpenZWave.
|
||||
//
|
||||
// OpenZWave is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
//
|
||||
// OpenZWave 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 Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _WaitImpl_H
|
||||
#define _WaitImpl_H
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef _WIN32
|
||||
#include "../../libs/pthreadw32/pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include <list>
|
||||
#include "../../Defs.h"
|
||||
#include "../Ref.h"
|
||||
#include "../Wait.h"
|
||||
|
||||
namespace OpenZWave
|
||||
{
|
||||
/** \brief Windows specific implementation of Wait objects.
|
||||
*/
|
||||
class WaitImpl
|
||||
{
|
||||
private:
|
||||
friend class Wait;
|
||||
|
||||
WaitImpl( Wait* _owner );
|
||||
virtual ~WaitImpl();
|
||||
|
||||
void AddWatcher( Wait::pfnWaitNotification_t _callback, void* _context );
|
||||
bool RemoveWatcher( Wait::pfnWaitNotification_t _callback, void* _context );
|
||||
void Notify();
|
||||
|
||||
static int32 Multiple( Wait** _objects, uint32 _numObjects, int32 _timeout = -1 );
|
||||
|
||||
WaitImpl( Wait const& ); // prevent copy
|
||||
WaitImpl& operator = ( WaitImpl const& ); // prevent assignment
|
||||
|
||||
struct Watcher
|
||||
{
|
||||
Wait::pfnWaitNotification_t m_callback;
|
||||
void* m_context;
|
||||
};
|
||||
|
||||
list<Watcher> m_watchers;
|
||||
Wait* m_owner;
|
||||
pthread_mutex_t m_criticalSection;
|
||||
};
|
||||
|
||||
} // namespace OpenZWave
|
||||
|
||||
#endif //_WaitImpl_H
|
||||
|
||||
Reference in new issue
Block a user