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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
define('serial_service', [
'content/public/renderer/service_provider',
'device/serial/serial.mojom',
'mojo/public/js/bindings/core',
'mojo/public/js/bindings/router',
], function(serviceProvider, serialMojom, core, routerModule) {
/**
* A Javascript client for the serial service and connection Mojo services.
*
* This provides a thick client around the Mojo services, exposing a JS-style
* interface to serial connections and information about serial devices. This
* converts parameters and result between the Apps serial API types and the
* Mojo types.
*/
var service = new serialMojom.SerialServiceProxy(new routerModule.Router(
serviceProvider.connectToService(serialMojom.SerialServiceProxy.NAME_)));
function getDevices() {
return service.getDevices().then(function(response) {
return $Array.map(response.devices, function(device) {
var result = {path: device.path || ''};
if (device.has_vendor_id)
result.vendorId = device.vendor_id;
if (device.has_product_id)
result.productId = device.product_id;
if (device.display_name)
result.displayName = device.display_name;
return result;
});
});
}
var DEFAULT_CLIENT_OPTIONS = {
persistent: false,
name: '',
receiveTimeout: 0,
sendTimeout: 0,
bufferSize: 4096,
};
var DATA_BITS_TO_MOJO = {
undefined: serialMojom.DataBits.NONE,
'seven': serialMojom.DataBits.SEVEN,
'eight': serialMojom.DataBits.EIGHT,
};
var STOP_BITS_TO_MOJO = {
undefined: serialMojom.StopBits.NONE,
'one': serialMojom.StopBits.ONE,
'two': serialMojom.StopBits.TWO,
};
var PARITY_BIT_TO_MOJO = {
undefined: serialMojom.ParityBit.NONE,
'no': serialMojom.ParityBit.NO,
'odd': serialMojom.ParityBit.ODD,
'even': serialMojom.ParityBit.EVEN,
};
function invertMap(input) {
var output = {};
for (var key in input) {
if (key == 'undefined')
output[input[key]] = undefined;
else
output[input[key]] = key;
}
return output;
}
var DATA_BITS_FROM_MOJO = invertMap(DATA_BITS_TO_MOJO);
var STOP_BITS_FROM_MOJO = invertMap(STOP_BITS_TO_MOJO);
var PARITY_BIT_FROM_MOJO = invertMap(PARITY_BIT_TO_MOJO);
function getServiceOptions(options) {
var out = {};
if (options.dataBits)
out.data_bits = DATA_BITS_TO_MOJO[options.dataBits];
if (options.stopBits)
out.stop_bits = STOP_BITS_TO_MOJO[options.stopBits];
if (options.parityBit)
out.parity_bit = PARITY_BIT_TO_MOJO[options.parityBit];
if ('ctsFlowControl' in options) {
out.has_cts_flow_control = true;
out.cts_flow_control = options.ctsFlowControl;
}
if ('bitrate' in options)
out.bitrate = options.bitrate;
return out;
}
function convertServiceInfo(result) {
if (!result.info)
throw new Error('Failed to get ConnectionInfo.');
return {
ctsFlowControl: !!result.info.cts_flow_control,
bitrate: result.info.bitrate || undefined,
dataBits: DATA_BITS_FROM_MOJO[result.info.data_bits],
stopBits: STOP_BITS_FROM_MOJO[result.info.stop_bits],
parityBit: PARITY_BIT_FROM_MOJO[result.info.parity_bit],
};
}
function Connection(remoteConnection, router, id, options) {
this.remoteConnection_ = remoteConnection;
this.router_ = router;
this.id_ = id;
getConnections().then(function(connections) {
connections[this.id_] = this;
}.bind(this));
this.paused_ = false;
this.options_ = {};
for (var key in DEFAULT_CLIENT_OPTIONS) {
this.options_[key] = DEFAULT_CLIENT_OPTIONS[key];
}
this.setClientOptions_(options);
}
Connection.create = function(path, options) {
options = options || {};
var serviceOptions = getServiceOptions(options);
var pipe = core.createMessagePipe();
// Note: These two are created and closed because the service implementation
// requires that we provide valid message pipes for the data source and
// sink. Currently the client handles are immediately closed; the real
// implementation will come later.
var sendPipe = core.createMessagePipe();
var receivePipe = core.createMessagePipe();
service.connect(path,
serviceOptions,
pipe.handle0,
sendPipe.handle0,
receivePipe.handle0);
core.close(sendPipe.handle1);
core.close(receivePipe.handle1);
var router = new routerModule.Router(pipe.handle1);
var connection = new serialMojom.ConnectionProxy(router);
return connection.getInfo().then(convertServiceInfo).then(
function(info) {
return Promise.all([info, allocateConnectionId()]);
}).catch(function(e) {
router.close();
throw e;
}).then(function(results) {
var info = results[0];
var id = results[1];
var serialConnectionClient = new Connection(
connection, router, id, options);
var clientInfo = serialConnectionClient.getClientInfo_();
for (var key in clientInfo) {
info[key] = clientInfo[key];
}
return {
connection: serialConnectionClient,
info: info,
};
});
};
Connection.prototype.close = function() {
this.router_.close();
return getConnections().then(function(connections) {
delete connections[this.id_]
return true;
}.bind(this));
};
Connection.prototype.getClientInfo_ = function() {
var info = {
connectionId: this.id_,
paused: this.paused_,
}
for (var key in this.options_) {
info[key] = this.options_[key];
}
return info;
};
Connection.prototype.getInfo = function() {
var info = this.getClientInfo_();
return this.remoteConnection_.getInfo().then(convertServiceInfo).then(
function(result) {
for (var key in result) {
info[key] = result[key];
}
return info;
}).catch(function() {
return info;
});
};
Connection.prototype.setClientOptions_ = function(options) {
if ('name' in options)
this.options_.name = options.name;
if ('receiveTimeout' in options)
this.options_.receiveTimeout = options.receiveTimeout;
if ('sendTimeout' in options)
this.options_.sendTimeout = options.sendTimeout;
if ('bufferSize' in options)
this.options_.bufferSize = options.bufferSize;
};
Connection.prototype.setOptions = function(options) {
this.setClientOptions_(options);
var serviceOptions = getServiceOptions(options);
if ($Object.keys(serviceOptions).length == 0)
return true;
return this.remoteConnection_.setOptions(serviceOptions).then(
function(result) {
return !!result.success;
}).catch(function() {
return false;
});
};
Connection.prototype.getControlSignals = function() {
return this.remoteConnection_.getControlSignals().then(function(result) {
if (!result.signals)
throw new Error('Failed to get control signals.');
var signals = result.signals;
return {
dcd: !!signals.dcd,
cts: !!signals.cts,
ri: !!signals.ri,
dsr: !!signals.dsr,
};
});
};
Connection.prototype.setControlSignals = function(signals) {
var controlSignals = {};
if ('dtr' in signals) {
controlSignals.has_dtr = true;
controlSignals.dtr = signals.dtr;
}
if ('rts' in signals) {
controlSignals.has_rts = true;
controlSignals.rts = signals.rts;
}
return this.remoteConnection_.setControlSignals(controlSignals).then(
function(result) {
return !!result.success;
});
};
Connection.prototype.flush = function() {
return this.remoteConnection_.flush().then(function(result) {
return !!result.success;
});
};
Connection.prototype.setPaused = function(paused) {
this.paused_ = paused;
};
var connections_ = {};
var nextConnectionId_ = 0;
// Wrap all access to |connections_| through getConnections to avoid adding
// any synchronous dependencies on it. This will likely be important when
// supporting persistent connections by stashing them.
function getConnections() {
return Promise.resolve(connections_);
}
function getConnection(id) {
return getConnections().then(function(connections) {
if (!connections[id])
throw new Error ('Serial connection not found.');
return connections[id];
});
}
function allocateConnectionId() {
return Promise.resolve(nextConnectionId_++);
}
return {
getDevices: getDevices,
createConnection: Connection.create,
getConnection: getConnection,
getConnections: getConnections,
};
});
|