Skip to content

Commit 918d0d5

Browse files
committed
Initial implementation
- Added some examples
1 parent 1fd56c6 commit 918d0d5

10 files changed

+228
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
build

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
build

binding.gyp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'ioctl',
5+
'sources': [ 'src/ioctl.cpp' ],
6+
'include_dirs': [
7+
'<!(node -e "require(\'nan\')")'
8+
]
9+
}
10+
]
11+
}

examples/fionread.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var dgram = require('dgram');
2+
var ioctl = require('../');
3+
4+
var FIONREAD = 0x541B;
5+
6+
var s = dgram.createSocket('udp4');
7+
s.bind(1234, function(err) {
8+
if (err) {
9+
throw err;
10+
}
11+
12+
var s1 = dgram.createSocket('udp4');
13+
var message = new Buffer("Some bytes");
14+
s1.send(message, 0, message.length, 1234, "localhost", function(err, bytes) {
15+
var length = new Buffer(4);
16+
var ret = ioctl(s._handle.fd, FIONREAD, length);
17+
console.log('Pending bytes: ' + length.readInt32LE(0));
18+
s1.close();
19+
s.close();
20+
});
21+
});

examples/hda_pversion.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var fs = require('fs');
2+
var ioctl = require('../');
3+
var ref = require('ref');
4+
var ArrayType = require('ref-array');
5+
var StructType = require('ref-struct');
6+
7+
var SND_HW = '/dev/snd/hwC0D0';
8+
var HDA_IOCTL_PVERSION = 0x80dc4801;
9+
10+
// struct snd_hwdep_info {
11+
// unsigned int device; /* WR: device number */
12+
// int card; /* R: card number */
13+
// unsigned char id[64]; /* ID (user selectable) */
14+
// unsigned char name[80]; /* hwdep name */
15+
// int iface; /* hwdep interface */
16+
// unsigned char reserved[64]; /* reserved for future */
17+
// };
18+
19+
// define the "snd_hwdep_info" struct type
20+
var snd_hwdep_info = StructType({
21+
device : ref.types.uint32,
22+
card : ref.types.int32,
23+
id : ArrayType(ref.types.uchar, 64),
24+
name : ArrayType(ref.types.uchar, 80),
25+
iface : ref.types.int32,
26+
reserved : ArrayType(ref.types.uchar, 64)
27+
});
28+
29+
var info = new snd_hwdep_info();
30+
31+
fs.open(SND_HW, 'r', function(err, fd) {
32+
if (err) {
33+
throw err;
34+
}
35+
36+
var ret = ioctl(fd, HDA_IOCTL_PVERSION, info.ref());
37+
console.log('device: ' + info.device);
38+
console.log('card: ' + info.card);
39+
console.log('id: ' + info.id.buffer.toString());
40+
console.log('name: ' + info.name.buffer.toString());
41+
console.log('iface: ' + info.iface);
42+
console.log('reserved: ' + info.reserved.buffer.toString());
43+
fs.close(fd);
44+
});

examples/termio.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var fs = require('fs');
2+
var ioctl = require('../');
3+
var ref = require('ref');
4+
var ArrayType = require('ref-array');
5+
var StructType = require('ref-struct');
6+
7+
var TTY = '/dev/tty1';
8+
var TCGETA = 0x5405;
9+
var TIOCEXCL = 0x540C;
10+
11+
// #define NCC 8
12+
// struct termio {
13+
// unsigned short c_iflag; /* input mode flags */
14+
// unsigned short c_oflag; /* output mode flags */
15+
// unsigned short c_cflag; /* control mode flags */
16+
// unsigned short c_lflag; /* local mode flags */
17+
// unsigned char c_line; /* line discipline */
18+
// unsigned char c_cc[NCC]; /* control characters */
19+
// };
20+
21+
// define the "snd_hwdep_info" struct type
22+
var termio = StructType({
23+
c_iflag : ref.types.ushort,
24+
c_oflag : ref.types.ushort,
25+
c_cflag : ref.types.ushort,
26+
c_lflag : ref.types.ushort,
27+
c_line : ref.types.uchar,
28+
c_cc : ArrayType(ref.types.uchar, 8)
29+
});
30+
31+
fs.open(TTY, 'r+', function(err, fd) {
32+
if (err) {
33+
throw err;
34+
}
35+
36+
var info = new termio();
37+
var ret = ioctl(fd, TCGETA, info.ref());
38+
console.log('TCGETA ret: ' + ret);
39+
console.log('c_iflag: ' + info.c_iflag);
40+
console.log('c_oflag: ' + info.c_oflag);
41+
console.log('c_cflag: ' + info.c_cflag);
42+
console.log('c_lflag: ' + info.c_lflag);
43+
console.log('c_line: ' + info.c_line);
44+
console.log('c_cc: ' + info.c_cc.buffer.toString());
45+
ret = ioctl(fd, TIOCEXCL);
46+
console.log('TIOCEXCL ret: ' + ret);
47+
fs.close(fd);
48+
});

examples/watchdog.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var fs = require('fs');
2+
var ioctl = require('../');
3+
var ref = require('ref');
4+
var ArrayType = require('ref-array');
5+
var StructType = require('ref-struct');
6+
7+
var WATCHDOG_DEVICE = '/dev/watchdog';
8+
var WDIOC_GETSUPPORT = 2150127360;
9+
10+
// struct watchdog_info {
11+
// __u32 options; /* Options the card/driver supports */
12+
// __u32 firmware_version; /* Firmware version of the card */
13+
// __u8 identity[32]; /* Identity of the board */
14+
// };
15+
16+
// define the "snd_hwdep_info" struct type
17+
var watchdog_info = StructType({
18+
options : ref.types.uint32,
19+
firmware_version : ref.types.uint32,
20+
identity : ArrayType(ref.types.uchar, 32)
21+
});
22+
23+
var info = new watchdog_info();
24+
25+
fs.open(WATCHDOG_DEVICE, 'r', function(err, fd) {
26+
if (err) {
27+
throw err;
28+
}
29+
30+
var ret = ioctl(fd, WDIOC_GETSUPPORT, info.ref());
31+
console.log('options: ' + info.options);
32+
console.log('firmware_version: ' + info.firmware_version);
33+
console.log('identity: ' + info.identity.buffer.toString());
34+
fs.close(fd);
35+
});

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./build/Release/ioctl.node').ioctl;

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ioctl",
3+
"version": "0.0.0",
4+
"description": "nodejs ioctl wrapper",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"ioctl",
11+
"syscall"
12+
],
13+
"author": "Santiago Gimeno",
14+
"license": "ISC",
15+
"dependencies": {
16+
"bindings": "^1.1.1",
17+
"nan": "^0.8.0"
18+
}
19+
}

src/ioctl.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <sys/ioctl.h>
2+
#include <v8.h>
3+
#include <node.h>
4+
#include "nan.h"
5+
6+
using namespace v8;
7+
using namespace node;
8+
9+
NAN_METHOD(Ioctl) {
10+
NanScope();
11+
12+
int length = args.Length();
13+
14+
assert((length == 2) || (length == 3));
15+
16+
void* argp = NULL;
17+
18+
if (!args[0]->IsInt32()) {
19+
return ThrowException(Exception::TypeError(String::New("Argument 0 Must be an Integer")));
20+
}
21+
22+
if (!args[1]->IsUint32()) {
23+
return ThrowException(Exception::TypeError(String::New("Argument 1 Must be an Integer")));
24+
}
25+
26+
if (length == 3) {
27+
if (!Buffer::HasInstance(args[2])) {
28+
return ThrowException(Exception::TypeError(String::New("Argument 2 Must be a Buffer")));
29+
}
30+
31+
argp = Buffer::Data(args[2]);
32+
}
33+
34+
int fd = args[0]->Int32Value();
35+
int request = args[1]->Uint32Value();
36+
int res = ioctl(fd, request, argp);
37+
38+
NanReturnValue(Number::New(res));
39+
}
40+
41+
void InitAll(Handle<Object> exports) {
42+
exports->Set(NanSymbol("ioctl"), FunctionTemplate::New(Ioctl)->GetFunction());
43+
}
44+
45+
NODE_MODULE(ioctl, InitAll)

0 commit comments

Comments
 (0)