forked from cheery/node-udev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udev.cc
265 lines (224 loc) · 8.17 KB
/
udev.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <v8.h>
#include <nan.h>
#include <libudev.h>
using namespace v8;
static struct udev *udev;
static void PushProperties(Local<Object> obj, struct udev_device* dev) {
struct udev_list_entry* sysattrs;
struct udev_list_entry* entry;
sysattrs = udev_device_get_properties_list_entry(dev);
udev_list_entry_foreach(entry, sysattrs) {
const char *name, *value;
name = udev_list_entry_get_name(entry);
value = udev_list_entry_get_value(entry);
if (value != NULL) {
obj->Set(Nan::New<String>(name).ToLocalChecked(), Nan::New<String>(value).ToLocalChecked());
} else {
obj->Set(Nan::New<String>(name).ToLocalChecked(), Nan::Null());
}
}
}
static void PushSystemAttributes(Local<Object> obj, struct udev_device* dev) {
struct udev_list_entry* sysattrs;
struct udev_list_entry* entry;
sysattrs = udev_device_get_sysattr_list_entry(dev);
udev_list_entry_foreach(entry, sysattrs) {
const char *name, *value;
name = udev_list_entry_get_name(entry);
value = udev_device_get_sysattr_value(dev, name);
if (value != NULL) {
obj->Set(Nan::New<String>(name).ToLocalChecked(), Nan::New<String>(value).ToLocalChecked());
} else {
obj->Set(Nan::New<String>(name).ToLocalChecked(), Nan::Null());
}
}
}
class Monitor : public node::ObjectWrap {
struct poll_struct {
Nan::Persistent<Object> monitor;
};
uv_poll_t* poll_handle;
udev_monitor* mon;
int fd;
static void on_handle_event(uv_poll_t* handle, int status, int events) {
Nan::HandleScope scope;
poll_struct* data = (poll_struct*)handle->data;
Local<Object> monitor = Nan::New(data->monitor);
Monitor* wrapper = ObjectWrap::Unwrap<Monitor>(monitor);
udev_device* dev = udev_monitor_receive_device(wrapper->mon);
Local<Object> obj = Nan::New<Object>();
obj->Set(Nan::New<String>("syspath").ToLocalChecked(), Nan::New<String>(udev_device_get_syspath(dev)).ToLocalChecked());
PushProperties(obj, dev);
Local<Function> emit = monitor->Get(Nan::New<String>("emit").ToLocalChecked()).As<Function>();
Local<Value> emitArgs[2];
emitArgs[0] = Nan::New<String>(udev_device_get_action(dev)).ToLocalChecked();
emitArgs[1] = obj;
emit->Call(monitor, 2, emitArgs);
udev_device_unref(dev);
};
static NAN_METHOD(New) {
Nan::HandleScope scope;
uv_poll_t* handle;
Monitor* obj = new Monitor();
obj->Wrap(info.This());
obj->mon = udev_monitor_new_from_netlink(udev, "udev");
obj->fd = udev_monitor_get_fd(obj->mon);
obj->poll_handle = handle = new uv_poll_t;
udev_monitor_enable_receiving(obj->mon);
poll_struct* data = new poll_struct;
//NanAssignPersistent(data->monitor, info.This());
data->monitor.Reset(info.This());
handle->data = data;
uv_poll_init(uv_default_loop(), obj->poll_handle, obj->fd);
uv_poll_start(obj->poll_handle, UV_READABLE, on_handle_event);
//NanReturnThis();
return;
}
static void on_handle_close(uv_handle_t *handle) {
poll_struct* data = (poll_struct*)handle->data;
//NanDisposePersistent(data->monitor);
data->monitor.Reset();
delete data;
delete handle;
}
static NAN_METHOD(Close) {
Nan::HandleScope scope;
Monitor* obj = ObjectWrap::Unwrap<Monitor>(info.This());
uv_poll_stop(obj->poll_handle);
uv_close((uv_handle_t*)obj->poll_handle, on_handle_close);
udev_monitor_unref(obj->mon);
return;
}
public:
static void Init(Handle<Object> target) {
// I do not remember why the functiontemplate was tugged into a persistent.
static Nan::Persistent<FunctionTemplate> constructor;
//Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New<String>("Monitor").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
//NODE_SET_PROTOTYPE_METHOD(tpl, "close", Close);
Nan::SetPrototypeMethod(tpl, "close", Close);
//NanAssignPersistent(constructor, tpl);
constructor.Reset(tpl);
//target->Set(
Nan::Set(
target,
Nan::New<String>("Monitor").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked()
);
}
};
NAN_METHOD(List) {
Nan::HandleScope scope;
//NanScope();
Local<Array> list = Nan::New<Array>();
struct udev_enumerate* enumerate;
struct udev_list_entry* devices;
struct udev_list_entry* entry;
struct udev_device *dev;
enumerate = udev_enumerate_new(udev);
// add match etc. stuff.
if(info[0]->IsString()) {
v8::Local<v8::String> subsystem = info[0]->ToString();
udev_enumerate_add_match_subsystem(enumerate, *Nan::Utf8String(subsystem));
}
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
int i = 0;
udev_list_entry_foreach(entry, devices) {
const char *path;
path = udev_list_entry_get_name(entry);
dev = udev_device_new_from_syspath(udev, path);
Local<Object> obj = Nan::New<Object>();
PushProperties(obj, dev);
//obj->Set(
Nan::Set(
obj,
Nan::New<String>("syspath").ToLocalChecked(),
Nan::New<String>(path).ToLocalChecked()
);
//list->Set(i++, obj);
Nan::Set(list, i++, obj);
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
//NanReturnValue(list);
info.GetReturnValue().Set(list);
}
NAN_METHOD(GetNodeParentBySyspath) {
Nan::HandleScope scope;
//NanScope();
struct udev_device *dev;
struct udev_device *parentDev;
if(!info[0]->IsString()) {
Nan::ThrowTypeError("first argument must be a string");
}
v8::Local<v8::String> string = info[0]->ToString();
dev = udev_device_new_from_syspath(udev, *Nan::Utf8String(string));
if (dev == NULL) {
Nan::ThrowError("device not found");
}
parentDev = udev_device_get_parent(dev);
if(parentDev == NULL) {
udev_device_unref(dev);
}
Local<Object> obj = Nan::New<Object>();
PushProperties(obj, parentDev);
const char *path;
path = udev_device_get_syspath(parentDev);
//obj->Set(
Nan::Set(
obj,
Nan::New<String>("syspath").ToLocalChecked(),
Nan::New<String>(path).ToLocalChecked()
);
udev_device_unref(dev);
//NanReturnValue(obj);
info.GetReturnValue().Set(obj);
}
NAN_METHOD(GetSysattrBySyspath) {
Nan::HandleScope scope;
//NanScope();
struct udev_device *dev;
if(!info[0]->IsString()) {
Nan::ThrowTypeError("first argument must be a string");
//NanReturnNull();
}
v8::Local<v8::String> string = info[0]->ToString();
dev = udev_device_new_from_syspath(udev, *Nan::Utf8String(string));
if (dev == NULL) {
Nan::ThrowError("device not found");
}
Local<Object> obj = Nan::New<Object>();
PushSystemAttributes(obj, dev);
obj->Set(Nan::New<String>("syspath").ToLocalChecked(), string);
udev_device_unref(dev);
//NanReturnValue(obj);
info.GetReturnValue().Set(obj);
}
static void Init(Handle<Object> target) {
udev = udev_new();
if (!udev) {
Nan::ThrowError("Can't create udev\n");
}
Nan::Set(
target,
Nan::New<String>("list").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(List)).ToLocalChecked()
);
Nan::Set(
target,
Nan::New<String>("getNodeParentBySyspath").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GetNodeParentBySyspath)).ToLocalChecked()
);
//target->Set(
Nan::Set(
target,
Nan::New<String>("getSysattrBySyspath").ToLocalChecked(),
//Nan::New<FunctionTemplate>(GetSysattrBySyspath)->GetFunction());
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GetSysattrBySyspath)).ToLocalChecked()
);
Monitor::Init(target);
}
NODE_MODULE(udev, Init)