整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
104
util/Device.cpp
Normal file
104
util/Device.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* INCLUDE
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "Device.h"
|
||||
#include <stdarg.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Format.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace Util
|
||||
{
|
||||
/*
|
||||
*******************************************************************************
|
||||
* CLASS DEFINE
|
||||
*******************************************************************************
|
||||
*/
|
||||
Device::Device(std::string path, Mode mode): _path(path), _mode(mode), _fd(-1)
|
||||
{
|
||||
}
|
||||
|
||||
Device::~Device()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void Device::open()
|
||||
{
|
||||
int flags;
|
||||
|
||||
switch (_mode)
|
||||
{
|
||||
default:
|
||||
case Mode::RD_ONLY:
|
||||
flags = O_RDONLY;
|
||||
break;
|
||||
|
||||
case Mode::WR_ONLY:
|
||||
flags = O_WRONLY;
|
||||
break;
|
||||
|
||||
case Mode::RDWR:
|
||||
flags = O_RDWR;
|
||||
break;
|
||||
}
|
||||
|
||||
_fd = ::open(_path.c_str(), flags);
|
||||
if (_fd < 0)
|
||||
throw Poco::InvalidAccessException(Poco::format("Access %s error: %d", _path, _fd), _fd);
|
||||
}
|
||||
|
||||
int Device::write(uint8_t* data, int size)
|
||||
{
|
||||
return ::write(_fd, data, size);
|
||||
}
|
||||
|
||||
int Device::read(uint8_t* buffer, int count)
|
||||
{
|
||||
return ::read(_fd, buffer, count);
|
||||
}
|
||||
|
||||
void Device::close()
|
||||
{
|
||||
if (_fd >= 0)
|
||||
::close(_fd);
|
||||
}
|
||||
|
||||
int Device::fcntl(int cmd)
|
||||
{
|
||||
return ::fcntl(_fd, cmd);
|
||||
}
|
||||
|
||||
int Device::fcntl(int cmd, long arg)
|
||||
{
|
||||
return ::fcntl(_fd, cmd, arg);
|
||||
}
|
||||
|
||||
int Device::fcntl(int cmd, long arg, void* lock)
|
||||
{
|
||||
return ::fcntl(_fd, cmd, arg, lock);
|
||||
}
|
||||
|
||||
int Device::poll(unsigned int& revents, int timeout)
|
||||
{
|
||||
struct pollfd pollfd;
|
||||
int rc;
|
||||
|
||||
pollfd.fd = _fd;
|
||||
pollfd.events = POLLIN;
|
||||
|
||||
rc = ::poll(&pollfd, 1, timeout);
|
||||
if (rc > 0)
|
||||
revents = pollfd.revents;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue