Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jan 26, 2024
1 parent 04655ed commit 3cd6ec9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/cellular-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const CellularDevice = base => class extends base {
*
* @param {Object} [options] Options.
* @param {Number} [options.timeout] Timeout (milliseconds).
* @return {Promise<Object>}
* @return {Promise<String>}
*/
async getIccid({ timeout = globalOptions.requestTimeout } = {}) {
const r = await this.sendRequest(Request.CELLULAR_GET_ICCID, null /* msg */, { timeout });
Expand Down
2 changes: 1 addition & 1 deletion src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class Device extends DeviceBase {
*
* @return {Promise<Array>} List of asssets available on the device.
*/
async getAssetInfo({ timeout = globalOptions.timeout }) {
async getAssetInfo({ timeout = globalOptions.timeout } = {}) {
if (this.isInDfuMode) {
throw new StateError('Cannot get information when the device is in DFU mode');
}
Expand Down
4 changes: 2 additions & 2 deletions src/device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe('Device', () => {
sinon.stub(device, 'sendProtobufRequest').resolves(expectedAssetInfo);
sinon.stub(device, 'isInDfuMode').value(false);

const result = await device.getAssetInfo({});
const result = await device.getAssetInfo();

expect(device.sendProtobufRequest).to.have.property('callCount', 1);
expect(result).to.eql(expectedAssetInfo);
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('Device', () => {
sinon.stub(device, 'sendProtobufRequest').resolves(expectedAssetInfo);
sinon.stub(device, 'isInDfuMode').value(true);

await expect(device.getAssetInfo({})).to.be.eventually.rejectedWith(StateError, 'Cannot get information when the device is in DFU mode');
await expect(device.getAssetInfo()).to.be.eventually.rejectedWith(StateError, 'Cannot get information when the device is in DFU mode');
});

describe('Device', () => {
Expand Down
43 changes: 17 additions & 26 deletions src/network-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,39 @@ const InterfaceConfigurationSource = fromProtobufEnum(proto.InterfaceConfigurati
});

/**
* Converts a given IP address and version into a dotted-decimal format.
*
* @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6'
* @param {string} version 'v4' or 'v6;
* @returns {string} address in dotted-decimal format
*/
function convertIPAddress(addr, version) {
if (!addr) {
return null;
}
return version === 'v4' ? Address4.fromInteger(addr).address : Address6.fromByteArray(addr).address;
}

/**
* Converts a given interface IP address object and version into a dotted-decimal format.
* Converts a given interface IP address object with version into a dotted-decimal format.
*
* @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6'
* @param {string} version 'v4' or 'v6;
* @returns {string} address in dotted-decimal format
*/
function convertInterfaceAddress(ifaceAddr, version) {
let res = null;
if (ifaceAddr && ifaceAddr.address && ifaceAddr.address[version] && ifaceAddr.address[version].address) {
res = convertIPAddress(ifaceAddr.address[version].address, version);
let prefixLength = null;
if (ifaceAddr && ifaceAddr.address && ifaceAddr.address[version]) {
const addrObj = ifaceAddr.address[version];
res = convertIPAddress(addrObj, version);
}
if (ifaceAddr && ifaceAddr.prefixLength) {
prefixLength = ifaceAddr.prefixLength;
}
const prefixLength = ifaceAddr.prefixLength;
return res ? `${res}/${prefixLength}` : null;
}

/**
* Converts a given dns IP address and version into a dotted-decimal format.
* Converts a given IP address object with version into a dotted-decimal address string.
*
* @param {int32 or buffer} addr Address sent as int32 for 'v4' and as a buffer for 'v6'
* @param {string} version 'v4' or 'v6;
* @returns {string} address in dotted-decimal format
*/
function convertDnsAddress(dnsAddr, version) {
if (dnsAddr && dnsAddr[version] && dnsAddr[version].address) {
return convertIPAddress(dnsAddr[version].address, version);
} else {
return null;
function convertIPAddress(addr, version) {
if (addr && addr.address) {
const val = addr.address;
const res = version === 'v4' ? Address4.fromInteger(val) : Address6.fromByteArray(val);
return res.address;
}
return null;
}

/**
Expand Down Expand Up @@ -189,7 +180,7 @@ const NetworkDevice = base => class extends base {
addresses: ipv4Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v4')),
gateway: convertIPAddress(ipv4Config.gateway, 'v4'),
peer: convertIPAddress(ipv4Config.peer, 'v4'),
dns: ipv4Config.dns.map((addr) => convertDnsAddress(addr, 'v4')),
dns: ipv4Config.dns.map((addr) => convertIPAddress(addr, 'v4')),
source: InterfaceConfigurationSource.fromProtobuf(ipv4Config.source)
};
}
Expand All @@ -198,7 +189,7 @@ const NetworkDevice = base => class extends base {
result.ipv6Config = {
addresses: ipv6Config.addresses.map((addr) => convertInterfaceAddress(addr, 'v6')),
gateway: convertIPAddress(ipv6Config.gateway, 'v6'),
dns: ipv6Config.dns.map((addr) => convertDnsAddress(addr, 'v6')),
dns: ipv6Config.dns.map((addr) => convertIPAddress(addr, 'v6')),
source: InterfaceConfigurationSource.fromProtobuf(ipv6Config.source)
};
}
Expand Down
8 changes: 2 additions & 6 deletions src/network-device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ describe('NetworkDevice', () => {
'ipv4Config': {
'dns': [
{
'v4': {
'address': 3232257567
}
'address': 3232257567
}
],
'addresses': [
Expand All @@ -58,9 +56,7 @@ describe('NetworkDevice', () => {
'ipv6Config': {
'dns': [
{
'v6': {
'address': Buffer.from([2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 15])
}
'address': Buffer.from([2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 15])
}
],
'addresses': [
Expand Down

0 comments on commit 3cd6ec9

Please sign in to comment.