p26001-store-acs-soc-fw-app/util/Device.cpp
2026-09-28 21:33:04 +08:00

104 lines
1.9 KiB
C++

/*
*******************************************************************************
* 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;
}
}