Skip to content

Commit

Permalink
Merge pull request #145 from NordicSemiconductor/fix/handle-jlink-loo…
Browse files Browse the repository at this point in the history
…kup-edge-cases

Handle edge cases in J-Link serial number lookup
  • Loading branch information
mrodem authored Nov 23, 2017
2 parents 50cb0a7 + 84ee25a commit 0700689
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/api/registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ import { findParentIdPrefixes, findJlinkIds } from './jlink';
// comName. This can happen if the COM port number on the system goes beyond
// COM256, or if the port numbering is manually reset. In these cases the
// registry may contain outdated relations between comNames and J-Link IDs,
// and we do not know which one is correct. We return all IDs found so that
// the application can display a warning. The largest ID is returned first.
// and we do not know which one is correct. We return all IDs found and
// leave it up to the caller to pick the right one.

function findJlinkIdsFromRegistry(comName) {
return findParentIdPrefixes(comName)
Expand Down
9 changes: 6 additions & 3 deletions lib/api/registry/reg.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ function regCmd(args) {

proc.on('close', code => {
if (code !== 0) {
reject(`Error when calling reg.exe: ${buffer}`);
} else {
resolve(buffer);
// The reg.exe command exits with code 1 if a query returns
// zero results. However, this could also indicate that there
// is an error, so logging it for traceability reasons.
console.log(`The reg.exe command exited with code ${code}. ` +
`Arguments: ${JSON.stringify(args)}. Output: ${buffer}.`);
}
resolve(buffer);
});

proc.stdout.on('data', data => {
Expand Down
74 changes: 55 additions & 19 deletions lib/windows/app/actions/serialPortActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

import SerialPort from 'serialport';
import nrfjprogjs from 'pc-nrfjprog-js';
import { findJlinkIds } from '../../../api/registry';
import { logger } from '../../../api/logging';

Expand Down Expand Up @@ -80,34 +81,69 @@ function selectPortAction(port) {
};
}

function warnAboutMultipleJlinkIds(port, jlinkIds) {
logger.warn(`Multiple J-Link IDs were found in registry for ${port.comName}: ` +
`${JSON.stringify(jlinkIds)}. Using ${jlinkIds[0]}.`);
function getConnectedDevices() {
return new Promise((resolve, reject) => {
nrfjprogjs.getConnectedDevices((error, devices) => {
if (error) {
reject(error);
} else {
resolve(devices);
}
});
});
}

function warnAboutNoJlinkId(port) {
logger.warn(`No J-Link ID was found in registry for ${port.comName}.`);
function findConnectedJlinkId(jlinkIds) {
return getConnectedDevices()
.then(devices => {
const connectedJlinkId = jlinkIds.find(jlinkId => {
const jlinkIdNumber = parseInt(jlinkId, 10);
const device = devices.find(dev => dev.serialNumber === jlinkIdNumber);
return !!device;
});
if (!connectedJlinkId) {
throw new Error(`Got multiple J-Link IDs (${JSON.stringify(jlinkIds)}), ` +
'but none are connected.');
}
return connectedJlinkId;
});
}

function getPortWithSerialNumber(port) {
return findJlinkIds(port.comName)
.then(jlinkIds => {
if (jlinkIds.length > 1) {
warnAboutMultipleJlinkIds(port, jlinkIds);
} else if (jlinkIds.length === 0) {
warnAboutNoJlinkId(port);
return port;
}
return Object.assign({}, port, {
serialNumber: jlinkIds[0],
return new Promise(resolve => {
findJlinkIds(port.comName)
.then(jlinkIds => {
if (jlinkIds.length === 1) {
return jlinkIds[0];
} else if (jlinkIds.length > 1) {
logger.warn(`Found multiple J-Link IDs in registry for ${port.comName}: ` +
`${JSON.stringify(jlinkIds)}. This may indicate that your registry ` +
'contains invalid data. Using nrfjprog to detect which one is ' +
'connected.');
return findConnectedJlinkId(jlinkIds);
}
throw new Error('No J-Link ID found.');
})
.then(jlinkId => {
resolve(Object.assign({}, port, {
serialNumber: jlinkId,
}));
})
.catch(error => {
logger.warn(`J-Link serial number lookup failed for ${port.comName}: ` +
`${error.message}`);
resolve(port);
});
});
});
}

function getPortsWithSerialNumber(ports) {
const promises = ports.map(port => (
port.vendorId === SEGGER_VENDOR_ID ? getPortWithSerialNumber(port) : port),
);
const promises = ports.map(port => {
if (port.vendorId === SEGGER_VENDOR_ID) {
return getPortWithSerialNumber(port);
}
return Promise.resolve(port);
});
return Promise.all(promises);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nrfconnect",
"version": "2.3.0-alpha.1",
"version": "2.3.0-alpha.2",
"description": "nRF Connect for PC",
"repository": {
"type": "git",
Expand Down

0 comments on commit 0700689

Please sign in to comment.