Skip to content

Commit

Permalink
Uses @tcr's fixes combined with my old fixes to give a warning that i…
Browse files Browse the repository at this point in the history
…t is booting
  • Loading branch information
johnnyman727 committed Oct 27, 2015
1 parent c976c80 commit cca4365
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Tessel.list = function(opts) {
// If there were no Tessels found
if (foundTessels.length === 0) {
// Report the sadness
reject(noTessels);
return reject(noTessels);
} else if (foundTessels.length === 1) {
// Close all opened connections and resolve
controller.closeTesselConnections(foundTessels)
Expand Down
13 changes: 6 additions & 7 deletions lib/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,17 @@ TesselSeeker.prototype.start = function(opts) {
self.emit('tessel', tessel);
} else {
debug('Fetching name:', tessel.connection.host);
tessel.getName()
return tessel.getName()
.then(function() {
self.emit('tessel', tessel);
return Promise.resolve();
})
.catch(function(err) {
logs.err('unable to fetch name:', err);
return Promise.resolve();
});
}
}).catch(function() {
return Promise.resolve();
}).catch(function(err) {
debug('Error opening device', err);
if (tessel.connection.connectionType === 'USB') {
logs.warn('Detected a Tessel that may be booting.');
}
});
// Push this Promise into the pending array
pendingOpen.push(opening);
Expand Down
14 changes: 6 additions & 8 deletions lib/tessel/tessel.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ Tessel.prototype.receive = function(remote) {

Tessel.prototype.simpleExec = function(command) {
var self = this;
return new Promise(function(resolve, reject) {
// Stop processes and delete everything in the folder
return self.connection.exec(command)
.then(function(remoteProcess) {
return self.receive(remoteProcess).then(function(received) {
return resolve(received.toString());
}).catch(reject);
// Stop processes and delete everything in the folder
return self.connection.exec(command)
.then(function(remoteProcess) {
return self.receive(remoteProcess).then(function(received) {
return Promise.resolve(received.toString());
});
});
});
};

Tessel.PUSH_PATH = '/app/';
Expand Down
43 changes: 22 additions & 21 deletions lib/usb_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,8 @@ USB.Connection.prototype._receiveMessages = function() {
var transferSize = 4096;
// Start polling
self.epIn.startPoll(2, transferSize);

// When we get data, push it into the stream
self.epIn.on('data', self.push.bind(self));

// Report errors as they arise
self.epIn.on('error', function(e) {
// If this stream was already closed, just return
if (self.closed) {
return;
} else {
// Otherwise print the error
logs.err('Error reading USB message endpoint:', e);
// Return a non-zero return code
process.exit(-5);
}
});
};

USB.Connection.prototype.open = function() {
Expand Down Expand Up @@ -130,7 +116,7 @@ USB.Connection.prototype.open = function() {
self.epIn = self.intf.endpoints[0];
self.epOut = self.intf.endpoints[1];
if (!self.epIn || !self.epOut) {
return reject(new Error('Device endpoints weren not able to be loaded'), self);
return reject(new Error('Device endpoints were not able to be loaded'), self);
}

// Map desciptions
Expand All @@ -142,6 +128,15 @@ USB.Connection.prototype.open = function() {
self.serialNumber = data;
// Register this connection with daemon (keeps track of active remote processes)
Daemon.register(self);

// If the USB Pipe isn't enabled on the other end (ie it is booting)
self.epIn.on('error', function(err) {
// Close the device resources
self._close();
// Catch the error and return if we haven't already
return reject(err);
});

// Start receiving messages
self._receiveMessages();
// If all is well, resolve the promise with the valid connection
Expand Down Expand Up @@ -175,12 +170,18 @@ USB.Connection.prototype._close = function(callback) {
}

self.closed = true;
self.intf.release(true, function() {
self.device.close();
if (typeof callback === 'function') {
callback();
}
}.bind(self));
self.epIn.stopPoll(function() {
self.intf.release(true, function attempt() {
try {
self.device.close();
} catch (e) {
setTimeout(attempt, 100);
}
if (typeof callback === 'function') {
callback();
}
});
});
};

// Returns a device in DFU mode
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"uglify-js": "^2.5.0",
"uglifyify": "^3.0.1",
"url-join": "0.0.1",
"usb": "^1.0.5",
"usb": "1.0.5",
"usb-daemon-parser": "0.0.1"
},
"preferGlobal": true,
Expand Down
68 changes: 43 additions & 25 deletions test/unit/usb_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,48 @@ exports['USB.Connection.prototype._receiveMessages'] = {
test.done();
},

errorWhenStillOpen: function(test) {
errorOnOpen: function(test) {
var self = this;
test.expect(2);

this.usbConnection._receiveMessages();

this.usbConnection.epIn.emit('error', 'oh no!');

test.equal(this.err.callCount, 1);
test.equal(this.processExit.callCount, 1);
test.done();
},

errorWhenClosed: function(test) {
test.expect(2);

this.usbConnection._receiveMessages();
this.usbConnection.closed = true;
this.usbConnection.epIn.emit('error', 'oh no!');

// The immediate return prevents these from being
// called from in the error handler
test.equal(this.err.callCount, 0);
test.equal(this.processExit.callCount, 0);
test.done();
},

var errorMessage = 'bad usb things!';

self.usbConnection.epIn.stopPoll = function() {};
this.sandbox.stub(this.usbConnection, '_receiveMessages', function() {
self.usbConnection.epIn.emit('error', errorMessage);
});

var closeFunc = this.sandbox.spy(this.usbConnection, '_close');

var fakeInterface = {
claim: function() {},
setAltSetting: function(arg1, cb) {
cb();
},
endpoints: [self.usbConnection.epIn, new Emitter()],
};
this.sandbox.stub(this.usbConnection, 'device', {
open: function() {},
interface: function() {
return fakeInterface;
},
getStringDescriptor: function(arg1, cb) {
cb();
},
deviceDescriptor: {
iSerialNumber: 'blah',
}
});

this.usbConnection.open()
.then(function() {
// It should not resolve
test.fail();
})
.catch(function(err) {
// It should throw the error;
test.equal(err, errorMessage);
test.equal(closeFunc.callCount, 1);
test.done();
});
}
};

1 comment on commit cca4365

@tcr
Copy link
Member

@tcr tcr commented on cca4365 Oct 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.