https://github.com/nohomey/open-ioctl
Opens device file in non-blocking ioctl mode only
https://github.com/nohomey/open-ioctl
device-drivers nodejs nonblock open-files typescript
Last synced: 21 days ago
JSON representation
Opens device file in non-blocking ioctl mode only
- Host: GitHub
- URL: https://github.com/nohomey/open-ioctl
- Owner: NoHomey
- License: mit
- Created: 2016-07-09T07:46:09.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-11-17T15:50:08.000Z (over 5 years ago)
- Last Synced: 2025-02-14T12:18:04.918Z (over 1 year ago)
- Topics: device-drivers, nodejs, nonblock, open-files, typescript
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# open-ioctl
Opens device file in non-blocking ioctl mode only
[](https://badge.fury.io/js/open-ioctl)
[](https://github.com/NoHomey/open-ioctl)
[](https://semaphoreci.com/nohomey/open-ioctl)
[](https://codeclimate.com/github/NoHomey/open-ioctl)
[](https://codeclimate.com/github/NoHomey/open-ioctl/coverage)
[](https://codeclimate.com/github/NoHomey/open-ioctl)


# Installation
Install with npm:
```bash
$ npm install open-ioctl
```
# Description
Opening ioctl only device drivers made easy.
Opens device file in non-blocking ioctl mode. File is opend with flags 3 | O_NONBLOCK.
Flag 3 means that only ioctl calls can be made for comunication with the device driver (remember read/write operations are expensive this is why open-ioctl was made in first place to make it easer for performance and command oriented device drivers).
Flag O_NONBLOCK means that ioctl calls will not put process and thread from which they came to sleep.
# Warning
## O_NONBLOCK
O_NONBLOCK: flag is different compared to O_ASYNC (which is recomended to use only in rear an special cases, also you have no access to it in node).
O_NONBLOCK means that all syscalls respectivly ioctl calls will not put the process and thread from which they were made to sleep while the result is awaited. This is perfect for in node process call so no even loop block will occure.
O_ASYNC can't be used on regular device files only terminals, sockets and pipes also it's not exported in fs.constants from the 'fs' module with a reason! For more information see: Posix open(2), open(3).
# Usage
## openIoctl(path, callback)
- path \
- callback \
### path is based on /dev
The callback gets two arguments (err, fd).
- err \
- fd \
```javascript
const { openIoctl } = require('open-ioctl');
const { close } = require('fs');
openIoctl('test_dev', function(err, fd) { // /dev/test_dev
if(err) {
console.log(err);
} else {
console.log(fd);
/* ioctl calls */
close(fd, function(err) {
if(err) {
console.log(err);
}
});
}
});
```
## openIoctlSync(path)
- path \
Synchronous version of openIoctl(). Returns an integer representing the file descriptor \.
```javascript
const { openIoctlSync } = require('open-ioctl');
const { closeSync } = require('fs');
var fd = openIoctlSync('test/test_dev'); // /dev/test/test_dev
console.log(fd);
/* ioctl calls */
closeSync(fd);
```
## open device file located outside of /dev
```javascript
const { openIoctlSync } = require('open-ioctl');
const { closeSync } = require('fs');
var fd = openIoctlSync('../proc/some_dev'); // /proc/some_dev
console.log(fd);
/* ioctl calls */
closeSync(fd);
```
# For full working example code visit: (https://github.com/NoHomey/nodejs-ioctl-example)
# Written in TypeScript and TypeScript Ready!