-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Low level access to device file. Minimizes frame loss.
Use ppoll to wait until data is available and then read. It eliminates situations where read() was stuck and required forcing thread cancel. So the main cause of lost frames is eliminated. Some frames still get lost but much less frequently.
- Loading branch information
Showing
8 changed files
with
156 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _KMICKI_HIDDEV_HIDDEVFILE_ | ||
#define _KMICKI_HIDDEV_HIDDEVFILE_ | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <sys/select.h> | ||
#include "poll.h" | ||
|
||
namespace kmicki::hiddev | ||
{ | ||
class HidDevFile | ||
{ | ||
public: | ||
HidDevFile() = delete; | ||
HidDevFile(std::string const& _filePath, int readTimeoutUs, bool const& open = true); | ||
|
||
bool Open(); | ||
int Read(std::vector<char> & data); | ||
bool Close(); | ||
bool IsOpen(); | ||
|
||
private: | ||
pollfd fileDescriptors[1]; | ||
int & file; | ||
std::string filePath; | ||
timespec timeout; | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// for ppoll() | ||
#ifndef _GNU_SOURCE | ||
#define _GNU_SOURCE | ||
#endif | ||
|
||
#include "hiddev/hiddevfile.h" | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <poll.h> | ||
|
||
namespace kmicki::hiddev | ||
{ | ||
static const int cUsToTimeout = 1000; | ||
|
||
HidDevFile::HidDevFile(std::string const& _filePath, int readTimeoutUs, bool const& open) | ||
: filePath(_filePath), fileDescriptors{{-1,POLLIN,0}}, | ||
timeout{0,readTimeoutUs*cUsToTimeout}, file(fileDescriptors[0].fd) | ||
{ | ||
if(open) | ||
Open(); | ||
} | ||
|
||
bool HidDevFile::Open() | ||
{ | ||
file = open(filePath.c_str(),O_RDONLY); | ||
return file >= 0; | ||
} | ||
|
||
bool HidDevFile::Close() | ||
{ | ||
auto result = close(file); | ||
file = -1; | ||
return result == 0; | ||
} | ||
|
||
bool HidDevFile::IsOpen() | ||
{ | ||
return file >= 0 && (fcntl(file, F_GETFD) != -1 || errno != EBADF); | ||
} | ||
|
||
int HidDevFile::Read(std::vector<char> & data) | ||
{ | ||
if(file < 0) | ||
return false; | ||
|
||
auto retval = ppoll(fileDescriptors,1,&timeout,nullptr); | ||
|
||
if(retval == 0) | ||
return 0; | ||
|
||
if(retval < 0) | ||
return retval; | ||
|
||
int readCnt = 0; | ||
|
||
do { | ||
auto readCntLoc = read(file, data.data()+readCnt,data.size()-readCnt); | ||
if(readCntLoc < 0) | ||
return readCntLoc; | ||
if(readCntLoc == 0) | ||
return (readCnt==0)?-1:readCnt; | ||
readCnt += readCntLoc; | ||
} | ||
while(readCnt < data.size()); | ||
|
||
return readCnt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters