forked from cheery/node-udev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
udev-ext-media.cc
309 lines (230 loc) · 9.14 KB
/
udev-ext-media.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <v8.h>
#include <node.h>
#include <iostream>
#include <string.h>
#include <libudev.h>
using namespace v8;
static struct udev *udev;
/**
* @param {Boolean} strict - True, only added DVDs will be recognized
*/
static const char* GetMediaType(struct udev_device* dev, bool strict=false){
//Check if the device is probably an external storage device (but not SATA)
const char *id_bus = udev_device_get_property_value(dev, "ID_BUS");
const char *id_fs_type = udev_device_get_property_value(dev, "ID_FS_TYPE");
const char *id_usb_driver = udev_device_get_property_value(dev, "ID_USB_DRIVER");
const char *id_model = udev_device_get_property_value(dev, "ID_MODEL");
const char *id_type = udev_device_get_property_value(dev, "ID_TYPE");
bool isSata = id_bus != NULL && strcmp(id_bus, "ata") == 0;
bool isUsb = id_fs_type != NULL
&& id_usb_driver != NULL
&& strcmp(id_usb_driver, "usb-storage") == 0;
bool isMMC = id_fs_type != NULL
&& id_model != NULL
&& strcmp(id_model, "SD_MMC") == 0;
bool isDVD = id_type != NULL
&& strcmp(id_type, "cd") == 0
&& (strict ? id_fs_type != NULL : true);
bool isUnknown = id_fs_type != NULL;
if(isDVD){
return "dvd";
}
//DVD is also ata driver, so needs to be after isDVD check
if(isSata){
return NULL;
}
if(isMMC){
return "sdcard";
}
if(isUsb){
return "usb";
}
if(isUnknown){
return "unknown";
}
return NULL;
}
static const char* ConvertChangeAction(struct udev_device* dev){
const char *action = udev_device_get_property_value(dev, "ACTION");
if(action == NULL){
return NULL;
}
//If action is add or remove, do nothing and return that action instead
if(strcmp(action, "change") != 0){
return action;
}
//Otherwise try to convert the change either to add or remove
const char* media_type = GetMediaType(dev);
//If there is no media-type, then there is something totally wrong
if(media_type == NULL){
return NULL;
}
const char *id_fs_type = udev_device_get_property_value(dev, "ID_FS_TYPE");
const char *disk_eject_request = udev_device_get_property_value(dev, "DISK_EJECT_REQUEST");
//DVD and change
if(strcmp(media_type, "dvd") == 0){
//Add
if(id_fs_type != NULL){
return "add";
}
//Remove
if(disk_eject_request != NULL && strcmp(disk_eject_request, "1") == 0){
return "remove";
}
}
return NULL;
}
static void PushProperties(Local<Object> obj, struct udev_device* dev) {
struct udev_list_entry* sysattrs;
struct udev_list_entry* entry;
//Retrieve all properties of the given device
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(String::New(name), String::New(value));
} else {
obj->Set(String::New(name), Null());
}
}
//Add an additional property to distinct the media type
const char* media_type = GetMediaType(dev);
if(media_type != NULL){
obj->Set(String::New("MEDIA_TYPE"), String::New(media_type));
}
}
class Monitor : public node::ObjectWrap {
struct poll_struct {
Persistent<Object> monitor;
};
public:
static void Init(Handle<Object> target) {
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("Monitor"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->PrototypeTemplate()->Set(String::NewSymbol("close"),
FunctionTemplate::New(Close)->GetFunction());
Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("Monitor"), constructor);
};
private:
static void on_handle_close(uv_handle_t *handle) {
poll_struct* data = (poll_struct*)handle->data;
data->monitor.Dispose();
delete data;
delete handle;
}
static void on_handle_event(uv_poll_t* handle, int status, int events) {
HandleScope scope;
poll_struct* data = (poll_struct*)handle->data;
Monitor* wrapper = ObjectWrap::Unwrap<Monitor>(data->monitor);
udev_device* dev = udev_monitor_receive_device(wrapper->mon);
const char *media_type = GetMediaType(dev);
TryCatch tc;
//Only emit if there is an actual device
if(media_type != NULL){
//Convert Change Actions correctly, also only emit, if there was an actual action
const char* action = ConvertChangeAction(dev);
if(action){
Local<Object> obj = Object::New();
obj->Set(String::NewSymbol("syspath"), String::New(udev_device_get_syspath(dev)));
PushProperties(obj, dev);
Local<Value> emit_v = data->monitor->Get(String::NewSymbol("emit"));
Local<Function> emit = Local<Function>::Cast(emit_v);
Local<Value> emitArgs[2];
obj->Set(String::New("ACTION"), String::New(action));
//TODO: Probably add some additional routines to transform values
//Emit add or remove event
emitArgs[0] = String::NewSymbol(action);
emitArgs[1] = obj;
emit->Call(data->monitor, 2, emitArgs);
//Experimental unified emit for all types
//emitArgs[0] = String::NewSymbol("device_event");
//emitArgs[1] = obj;
//emit->Call(data->monitor, 2, emitArgs);
}
}
udev_device_unref(dev);
if (tc.HasCaught()) node::FatalException(tc);
};
static Handle<Value> New(const Arguments& args) {
HandleScope scope;
//For handling udev-events
uv_poll_t* handle;
//Convenient Monitor wrapper for managing the C udev monior
Monitor* obj = new Monitor();
//Create the monitor struct, generated by libudev and connect to event-source "udev"
obj->mon = udev_monitor_new_from_netlink(udev, "udev");
//Experimental: Adding type-matching for kernel filter
udev_monitor_filter_add_match_subsystem_devtype(obj->mon, "block", "partition");
udev_monitor_filter_add_match_subsystem_devtype(obj->mon, "block", "disk");
//Starting the event polling
udev_monitor_enable_receiving(obj->mon);
obj->fd = udev_monitor_get_fd(obj->mon);
obj->poll_handle = handle = new uv_poll_t;
obj->Wrap(args.This());
poll_struct* data = new poll_struct;
data->monitor = Persistent<Object>::New(args.This());
//Additional parameters for this handle
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);
return args.This();
};
static Handle<Value> Close(const Arguments& args) {
HandleScope scope;
Monitor* obj = ObjectWrap::Unwrap<Monitor>(args.This());
uv_poll_stop(obj->poll_handle);
uv_close((uv_handle_t*)obj->poll_handle, on_handle_close);
udev_monitor_unref(obj->mon);
return scope.Close(Undefined());
};
uv_poll_t* poll_handle;
udev_monitor* mon;
int fd;
};
static Handle<Value> List(const Arguments& args) {
HandleScope scope;
Local<Array> list = Array::New();
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.
udev_enumerate_add_match_subsystem(enumerate, "block");
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);
//Be strict and do not accept any dvds without id_fs_type
const char *media_type = GetMediaType(dev, true);
//Only take non-sata devices
if(media_type != NULL){
//Create new javascript object and push
Local<Object> obj = Object::New();
PushProperties(obj, dev);
obj->Set(String::NewSymbol("syspath"), String::New(path));
list->Set(i++, obj);
}
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
return scope.Close(list);
}
static void Init(Handle<Object> target) {
udev = udev_new();
if (!udev) {
ThrowException(String::New("Can't create udev\n"));
}
//Publish function "list"
target->Set(String::NewSymbol("list"),
FunctionTemplate::New(List)->GetFunction());
Monitor::Init(target);
}
NODE_MODULE(udev_ext_media, Init)