Skip to content

Commit 38c36b0

Browse files
authored
Merge pull request #156 from bryanburgers/api
Document the API
2 parents b9d03ac + b059692 commit 38c36b0

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed

README.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,291 @@ environment variables manually (the actual bus address might be different):
6060
process.env.DISPLAY = ':0';
6161
process.env.DBUS_SESSION_BUS_ADDRESS = 'unix:path=/run/dbus/system_bus_socket';
6262

63+
64+
## API
65+
66+
67+
### DBus
68+
69+
The root object of this module.
70+
71+
#### `new DBus()`
72+
73+
Create a new DBus instance.
74+
75+
```
76+
var DBus = require('dbus')
77+
var dbus = new DBus()
78+
```
79+
80+
#### `DBus.prototype.getBus(busName)`
81+
82+
* busName `<string>`
83+
84+
Connect to a bus. `busName` must be either `"system"` to connect to the system
85+
bus or `"session"` to connect to the session bus.
86+
87+
Returns a `Bus`.
88+
89+
```
90+
var bus = dbus.getBus('session');
91+
```
92+
93+
#### `DBus.prototype.registerService(busName, serviceName)`
94+
95+
* busName `<string>`
96+
* serviceName `<string>`
97+
98+
Register a service on a specific bus. This allows the caller to create a DBus
99+
service.
100+
101+
`busName` must be either `"system"` to create the service on the system bus, or
102+
`"session"` to create the service on the session bus. _Note: the system bus
103+
often has security requirements that need to be met before the service can be
104+
registered._
105+
106+
Returns a `Service`.
107+
108+
```
109+
var service = dbus.registerService('session', 'com.example.Library');
110+
```
111+
112+
113+
### Bus
114+
115+
An active connection to one of DBus' buses.
116+
117+
#### `Bus.prototype.getInterface(serviceName, objectPath, interfaceName, callback)`
118+
119+
* serviceName `<string>` - The well-known name of the service that owns the object.
120+
* objectPath `<string>` - The path of the object.
121+
* interfaceName `<string>` - Which of the object's interfaces to retrieve.
122+
* callback `<function>`
123+
124+
Get an existing object's interface from a well-known service.
125+
126+
Once retrieved, `callback` will be called with either an error or with an
127+
`Interface`.
128+
129+
```
130+
bus.getInterface('com.example.Library', '/com/example/Library/authors/DAdams', 'com.example.Library.Author1', function(err, interface) {
131+
if (err) {
132+
...
133+
}
134+
135+
// Do something with the interface
136+
});
137+
```
138+
139+
140+
### Interface
141+
142+
#### `Interface.prototype.getProperty(propertyName, callback)`
143+
144+
* propertyName `<string>` - The name of the property to get.
145+
* callback `<function>`
146+
147+
Get the value of a property.
148+
149+
Once retrieved `callback` will be called with either an error or with the value
150+
of the property.
151+
152+
```
153+
interface.getProperty('Name', function(err, name) {
154+
});
155+
```
156+
157+
#### `Interface.prototype.setProperty(propertyName, value, callback)`
158+
159+
* propertyName `<string>` - The name of the property to get.
160+
* value `<any>` - The value of the property to set.
161+
* callback `<function>`
162+
163+
Set the value of a property.
164+
165+
Once set `callback` will be called with either an error or nothing.
166+
167+
```
168+
interface.setProperty('Name', 'Douglas Adams', function(err) {
169+
});
170+
```
171+
172+
#### `Interface.prototype.getProperties(callback)`
173+
174+
* callback `<function>`
175+
176+
Get the value of all of the properties of the interface.
177+
178+
Once retrieved `callback` will be called with either an error or with an object
179+
where the keys are the names of the properties, and the values are the values
180+
of those properties.
181+
182+
```
183+
interface.getProperties(function(err, properties) {
184+
console.log(properties.Name);
185+
});
186+
```
187+
188+
#### `Interface.prototype[methodName](...args, [options], callback)`
189+
190+
* methodName `<string>` - The name of the method on the interface to call.
191+
* ...args `<any>` - The arguments that must be passed to the method.
192+
* options `<object>` - The options that can be set. This is optional.
193+
* options.timeout `<number>` - The number of milliseconds to wait before the
194+
request is timed out. This defaults to `-1`: don't time out.
195+
* callback `<function>`
196+
197+
Call a method on the interface.
198+
199+
Once executed, `callback` will be called with either an error or with the
200+
result of the method call.
201+
202+
```
203+
interface.AddBook("The Hitchhiker's Guide to the Galaxy", { timeout: 1000 }, function(err, result) {
204+
})
205+
```
206+
207+
208+
### Service
209+
210+
A dbus service created by the application.
211+
212+
#### `Service.prototype.createObject(objectPath)`
213+
214+
* objectPath `<string>` - The path of the object. E.g., `/com/example/ObjectName`
215+
216+
Create an object that is exposed over DBus.
217+
218+
Returns a `ServiceObject`.
219+
220+
```
221+
var object = service.createObject('/com/example/Library/authors/DAdams');
222+
```
223+
224+
#### `Service.prototype.removeObject(object)`
225+
226+
* object `<ServiceObject>` - the service object that has been created
227+
228+
Remove (or unexpose) an object that has been created.
229+
230+
```
231+
service.removeObject(object);
232+
```
233+
234+
### ServiceObject
235+
236+
An object that is exposed over DBus.
237+
238+
#### `ServiceObject.prototype.createInterface(interfaceName)`
239+
240+
* interfaceName `<string>` - The name of the interface.
241+
242+
Create an interface on an object.
243+
244+
Returns a `ServiceInterface`.
245+
246+
```
247+
var interface = object.createInterface('com.example.Library.Author1');
248+
```
249+
250+
251+
### ServiceInterface
252+
253+
An interface for an object that is exposed over DBus.
254+
255+
#### `ServiceInterface.prototype.addMethod(method, opts, handler)`
256+
257+
* method `<string>` - The name of the method
258+
* opts `<object>` - Options for the method
259+
* opts.in - The signature for parameters
260+
* opts.out - The signature for what the method returns
261+
* handler `<function>` - The method handler
262+
263+
Add a method that can be called over DBus.
264+
265+
```
266+
interface.addMethod('AddBook', {
267+
in: [DBus.Define(String), DBus.Define(Number)],
268+
out: [DBus.Define(Number)]
269+
}, function(name, quality, callback) {
270+
doSomeAsyncOperation(name, quality, function(err, result) {
271+
if (err) {
272+
return callback(err);
273+
}
274+
275+
callback(result);
276+
});
277+
});
278+
```
279+
280+
#### `ServiceInterface.prototype.addProperty(name, opts)`
281+
282+
* name `<string>` - The name of the property
283+
* opts `<object>`
284+
* opts.type - The type of the property
285+
* opts.getter - The function to retrieve the value
286+
* opts.setter - The function to set the value (optional)
287+
288+
Add a property that can be get, and/or optionally set, over DBus.
289+
290+
```
291+
interface.addProperty('BooksWritten', {
292+
type: DBus.Define(Number),
293+
getter: function(callback) {
294+
getNumberOfBooksForAuthor(function(err, bookCount) {
295+
if(err) {
296+
return callback(err);
297+
}
298+
callback(bookCount);
299+
});
300+
}
301+
}
302+
303+
var name = 'Douglas Adams';
304+
interface.addProperty('Name', {
305+
type: Dbus.Define(String),
306+
getter: function(callback) {
307+
callback(name);
308+
}
309+
setter: function(value, done) {
310+
name = value;
311+
done();
312+
}
313+
}
314+
```
315+
316+
#### `ServiceInterface.prototype.addSignal(name, opts)`
317+
318+
* name `<string>` - The name of the signal
319+
* opts `<object>`
320+
* types
321+
322+
Create a DBus signal.
323+
324+
```
325+
interface.addSignal('bookCreated', {
326+
types: [DBus.Define(Object)]
327+
});
328+
```
329+
330+
#### `ServiceInterface.prototype.emitSignal(name, ...values)`
331+
332+
* name `<string>` - The name of the signal
333+
* values `<any>` - The values to emit
334+
335+
Emit a signal
336+
337+
```
338+
interface.emit('bookCreated', { name: "The Hitchhiker's Guide to the Galaxy" })
339+
```
340+
341+
#### `ServiceInterface.prototype.update()`
342+
343+
Save interface updates after making changes. After changes to the interface are
344+
made (via `addMethod`, `addProperty`, and `addSignal`), `update` must be called
345+
to ensure that other DBus clients can see the changes that were made.
346+
347+
63348
## License
64349

65350
(The MIT License)

0 commit comments

Comments
 (0)