UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

810 lines (799 loc) 37.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var _a; var error_space_1 = require("./../common/error-space"); var iotile_common_1 = require("@iotile/iotile-common"); var lodash_1 = require("lodash"); /** * All known short codes for converting settings into string names. */ var SettingCodes; (function (SettingCodes) { SettingCodes[SettingCodes["IPConfig"] = 0] = "IPConfig"; SettingCodes[SettingCodes["NetworkName"] = 1] = "NetworkName"; SettingCodes[SettingCodes["NetworkKey"] = 2] = "NetworkKey"; SettingCodes[SettingCodes["SharedConnection"] = 3] = "SharedConnection"; SettingCodes[SettingCodes["InterfaceIndex"] = 4] = "InterfaceIndex"; SettingCodes[SettingCodes["ModemAPN"] = 5] = "ModemAPN"; })(SettingCodes = exports.SettingCodes || (exports.SettingCodes = {})); var NetworkInterfaces; (function (NetworkInterfaces) { NetworkInterfaces[NetworkInterfaces["Ethernet"] = 0] = "Ethernet"; NetworkInterfaces[NetworkInterfaces["Wifi"] = 1] = "Wifi"; })(NetworkInterfaces = exports.NetworkInterfaces || (exports.NetworkInterfaces = {})); /** * A simple class for holding an IPV4 address and converting between representations */ var IPV4Address = /** @class */ (function () { function IPV4Address(address) { if (address instanceof IPV4Address) { address = address.asUint32(); } if (typeof address === 'string') { var parts = address.split('.'); if (parts.length != 4) { throw (new iotile_common_1.ArgumentError("String IPV4 address did not have 4 dot separated parts: " + address)); } this._parts = parts.map(function (x) { return parseInt(x); }); } else if (typeof address === 'number') { this._parts = [(address >> 24) & 0xFF, (address >> 16) & 0xFF, (address >> 8) & 0xFF, (address >> 0) & 0xFF]; } else if (address.length === 4) { this._parts = address.map(function (x) { return parseInt(x); }); } else if (!address) { this._parts = [0, 0, 0, 0]; } else { throw new iotile_common_1.ArgumentError("Unknown type to create IPV4Address from: " + address); } } /** * Return the dotted address as a single 32 bit integer * * X.Y.Z.W returns 0xXXYYZZWW */ IPV4Address.prototype.asUint32 = function () { var x = this._parts[0]; var y = this._parts[1]; var z = this._parts[2]; var w = this._parts[3]; return iotile_common_1.toUint32((x << 24) | (y << 16) | (z << 8) | w); }; IPV4Address.prototype.toString = function () { return this._parts.join('.'); }; IPV4Address.prototype.isEqual = function (ip1, ip2) { return ip1.asUint32() === ip2.asUint32(); }; return IPV4Address; }()); exports.IPV4Address = IPV4Address; /** * A data class encapsulating a wifi or cellular network. */ var WirelessNetwork = /** @class */ (function () { /** * * @param ssid * @param authType * @param quality */ function WirelessNetwork(ssid, authType, quality) { if (quality === void 0) { quality = null; } this.ssid = ssid; this.authType = authType; this.quality = quality; this.valid = true; } /** * Rebuild a WirelessNetwork from one or more encoded chunks. */ WirelessNetwork.FromEncodedChunks = function (encodedChunks) { var valid = false; var authType = 0; var quality = null; var ssid = ''; for (var i = 0; i < encodedChunks.length; i++) { var chunk = encodedChunks[i]; var _a = iotile_common_1.unpackArrayBuffer('BBBB16s', chunk), authByte = _a[0], signalQuality = _a[1], totalLength = _a[2], validLength = _a[3], ssidChunk = _a[4]; if (i === 0) { valid = !!(authByte & (1 << 4)); authType = authByte & 15; quality = signalQuality; } if (validLength < ssidChunk.length) { ssidChunk = ssidChunk.slice(0, validLength); } ssid += ssidChunk; } if (!valid) { return WirelessNetwork.InvalidNetwork(); } return new WirelessNetwork(ssid, authType, quality); }; WirelessNetwork.InvalidNetwork = function () { var net = new WirelessNetwork('None', WirelessNetwork.NO_AUTH); net.valid = false; return net; }; WirelessNetwork.prototype.requiresPassword = function () { return this.authType !== WirelessNetwork.NO_AUTH; }; WirelessNetwork.prototype.toString = function () { if (!this.valid) { return 'Invalid network'; } return this.ssid + " (" + lodash_1.get(WirelessNetwork.AUTH_TABLE, this.authType, 'Unsupported auth') + ", quality=" + this.quality + ")"; }; WirelessNetwork.prototype.encode = function (nameOffset) { var authByte = (this.authType & 15) | ((this.valid << 0) << 4); var encodedName = new ArrayBuffer(0); if (this.ssid) { encodedName = iotile_common_1.stringToBuffer(this.ssid); } var totalLength = encodedName.byteLength; var remainingLength = Math.max(0, totalLength - nameOffset); var containedLength = Math.min(remainingLength, 16); var quality = this.quality; if (quality === null) { quality = 0; } var nameChunk = encodedName.slice(nameOffset, containedLength); if (nameChunk.byteLength < 16) { nameChunk = iotile_common_1.padArrayBuffer(nameChunk, 16); } return iotile_common_1.packArrayBuffer('BBBB16s', authByte, quality, totalLength, containedLength, nameChunk); }; WirelessNetwork.NO_AUTH = 0; WirelessNetwork.WPA2_PSK_AUTH = 1; WirelessNetwork.WPA2_ENTERPRISE = 2; WirelessNetwork.WEP_AUTH = 3; WirelessNetwork.UNKNOWN_AUTH = 4; WirelessNetwork.CELLULAR = 5; WirelessNetwork.AUTH_TABLE = (_a = {}, _a[WirelessNetwork.NO_AUTH] = 'Open', _a[WirelessNetwork.WPA2_PSK_AUTH] = 'WPA2 PSK', _a[WirelessNetwork.WPA2_ENTERPRISE] = 'WPA2 Enterprise', _a[WirelessNetwork.WEP_AUTH] = 'WEP', _a[WirelessNetwork.UNKNOWN_AUTH] = 'Unknown Auth', _a[WirelessNetwork.CELLULAR] = 'Cellular', _a); return WirelessNetwork; }()); exports.WirelessNetwork = WirelessNetwork; var NetworkInterfaceInfo = /** @class */ (function () { function NetworkInterfaceInfo(ifaceId, configured, ifaceType, carrier) { this.id = ifaceId; this.configured = configured; this.autoconnect = false; this.valid = true; this.static = false; this.sharing = false; this.error = 0; this.type = ifaceType; this.carrier = carrier; this._ip4Addr = new IPV4Address("0.0.0.0"); this._ip4Gateway = new IPV4Address("0.0.0.0"); this._ip4Netmask = new IPV4Address("0.0.0.0"); this._ip4DNS = []; } Object.defineProperty(NetworkInterfaceInfo.prototype, "address", { get: function () { return this._ip4Addr; }, enumerable: true, configurable: true }); Object.defineProperty(NetworkInterfaceInfo.prototype, "gateway", { get: function () { return this._ip4Gateway; }, enumerable: true, configurable: true }); Object.defineProperty(NetworkInterfaceInfo.prototype, "netmask", { get: function () { return this._ip4Netmask; }, enumerable: true, configurable: true }); Object.defineProperty(NetworkInterfaceInfo.prototype, "dnsServers", { get: function () { return this._ip4DNS; }, enumerable: true, configurable: true }); /** * Build a NetworkInterfaceInfo object from an RPC response * @param {Arraybuffer} data The 20 byte packed binary data produced * by a call to asRPCPayload */ NetworkInterfaceInfo.FromRPC = function (data) { if (data.byteLength !== 20) { throw new iotile_common_1.ArgumentError("Invalid rpc payload with the wrong length\", expected=20, found=" + data.byteLength); } var _a = iotile_common_1.unpackArrayBuffer('BBBxLLLL', data), ifaceByte = _a[0], flags = _a[1], error = _a[2], ip4Addr = _a[3], ip4Mask = _a[4], ip4Gateway = _a[5], dns = _a[6]; var isStatic = !!(ifaceByte & (1 << 7)); var ifaceId = ifaceByte & (63); var _b = NetworkInterfaceInfo.ParseFlags(flags), ifaceType = _b[0], configured = _b[1], media = _b[2], sharing = _b[3], autoconf = _b[4], valid = _b[5]; var status = new NetworkInterfaceInfo(ifaceId, configured, ifaceType, media); status.sharing = sharing; status.error = error; status.autoconnect = autoconf; status.valid = valid; if (new IPV4Address(ip4Addr).toString() !== '0.0.0.0') { status.setIp4Info(ip4Addr, ip4Gateway, ip4Mask); } if (new IPV4Address(dns).toString() !== '0.0.0.0') { status.addDNS(dns); } status.static = isStatic; return status; }; NetworkInterfaceInfo.ParseFlags = function (flags) { var ifaceType = flags & 7; var autoconf = !!(flags & (1 << 3)); var configured = !!(flags & (1 << 4)); var media = !!(flags & (1 << 5)); var sharing = !!(flags & (1 << 6)); var valid = !!(flags & (1 << 7)); return [ifaceType, configured, media, sharing, autoconf, valid]; }; /** * Configure the ip address information for this interface. */ NetworkInterfaceInfo.prototype.setIp4Info = function (addr, gateway, netmask) { this._ip4Addr = new IPV4Address(addr); this._ip4Gateway = new IPV4Address(gateway); this._ip4Netmask = new IPV4Address(netmask); }; NetworkInterfaceInfo.prototype.addDNS = function (addr) { this._ip4DNS.push(new IPV4Address(addr)); }; NetworkInterfaceInfo.prototype.toString = function () { if (!this.valid) { return "Invalid Interface " + this.id; } var out = "\n Interface " + this.id + "\n Basic Info:\n - Type: " + this.type + "\n - Configured: " + this.configured + "\n - Cable Plugged/Has Coverage: " + this.carrier + "\n - Hotspot: " + this.sharing + "\n - Autoconnect: " + this.autoconnect + "\n - Error Code: " + this.error + "\n IP Info:\n - Static IP: " + this.static + "\n - IP Address: " + this._ip4Addr.toString() + "\n - Netmask: " + this._ip4Netmask.toString() + "\n - Gateway: " + this._ip4Gateway.toString() + "\n DNS Servers:\n "; for (var _i = 0, _a = this._ip4DNS; _i < _a.length; _i++) { var server = _a[_i]; out += " - " + server.toString() + "\n"; } return out; }; NetworkInterfaceInfo.prototype.invalidInterface = function (ifaceId) { var info = new NetworkInterfaceInfo(ifaceId, false, 0, false); info.valid = false; return info; }; /** * Pack this interface status as a an RPC response payload */ NetworkInterfaceInfo.prototype.asRPCPayload = function () { var ifaceByte = this.id | (this.static << 7); var flags = this._packFlags(); var dns = new IPV4Address('0.0.0.0'); if (this._ip4DNS.length > 0) { dns = this._ip4DNS[0]; } return iotile_common_1.packArrayBuffer('BBBxLLLL', ifaceByte, flags, this.error, this._ip4Addr.asUint32(), this._ip4Netmask.asUint32(), this._ip4Gateway.asUint32(), dns.asUint32()); }; NetworkInterfaceInfo.prototype._packFlags = function () { return (this.type & 7) | (this.valid << 7) | (this.sharing << 6) | (this.carrier << 5) | (this.configured << 4) | (this.autoconnect << 3); }; return NetworkInterfaceInfo; }()); exports.NetworkInterfaceInfo = NetworkInterfaceInfo; /** * Class to configure the network for a raspberry pi based access point */ var NetworkConfig = /** @class */ (function () { function NetworkConfig(adapter, address) { this.adapter = adapter; this.address = address; } /** * Capture and count the network interfaces on this device. * * This function must be called before any other interface related * functions are called since it captures the network devices for * future access. * * The interfaces may be accessed by index after calling this function * and their index number will always point to the same device until * this function is called again. * * @returns {Promise<number>}: The count of network devices. * * @param address */ NetworkConfig.prototype.listInterfaces = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.adapter.errorHandlingRPC(this.address, 0x8000, "", "LL", [])]; }); }); }; /** * Get basic information about a network interface. * * This information includes the type of interface, whether it is currently * connected to a media / ethernet cable and what its IP information is. * * @returns {Promise<string>} InterfaceInfo show-as string: An InterfaceInfo structure with the interface info. * * @param {number} index The index of the interface we wish to query. * This must be < the count returned by list_interfaces. */ NetworkConfig.prototype.interfaceInfo = function (index) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.adapter.typedRPC(this.address, 0x8001, 'L', 'V', [index])]; case 1: res = (_a.sent())[0]; return [2 /*return*/, NetworkInterfaceInfo.FromRPC(iotile_common_1.stringToBuffer(res))]; } }); }); }; /** * List all visible networks for a given interface. * * @returns {Promise<string[]>} A list of visible WirelessNetwork objects * * @param {number} index A network interface index. */ NetworkConfig.prototype.listNetworks = function (index) { return __awaiter(this, void 0, void 0, function () { var count, networks, i, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this._captureNetworkList(index)]; case 1: count = _c.sent(); networks = []; i = 0; _c.label = 2; case 2: if (!(i < count)) return [3 /*break*/, 5]; _b = (_a = networks).push; return [4 /*yield*/, this.networkInfo(i)]; case 3: _b.apply(_a, [_c.sent()]); _c.label = 4; case 4: i++; return [3 /*break*/, 2]; case 5: return [2 /*return*/, networks]; } }); }); }; /** * Get the active network for a given interface. * * @returns {Promise<string>} WirelessNetwork show-as string: The active wireless network. * * @param {number} index A network interface index. */ NetworkConfig.prototype.activeNetwork = function (index) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._captureNetworkList(index)]; case 1: _a.sent(); return [2 /*return*/, this.networkInfo(-1)]; } }); }); }; /** * Get information on a given wireless network. You must have previously called list_networks to capture network information from a given interface and then you can iterate over those results by calling this function. If the network name is longer than 20 bytes, you can call this function with a nonzero offset in order to retrieve the higher parts of the name. The same header information is repeated with each call, not just when offset=0. The response contains: - Network auth type and flags - Signal quality - Total name length - Valid name length in this segment - up to 16 bytes of the access point ssid or carrier name depending on whether the network is wifi or cellular. To enquire about the active network for the interface, pass -1 as the index. * @param {number} index The index of the network to enquire about. Pass -1 to * ask about the active network */ NetworkConfig.prototype.networkInfo = function (index) { return __awaiter(this, void 0, void 0, function () { var _a, chunks, accumLength, totalLength, chunkLength, chunk; return __generator(this, function (_b) { switch (_b.label) { case 0: chunks = []; accumLength = 0; totalLength = null; _b.label = 1; case 1: if (!(totalLength === null || accumLength < totalLength)) return [3 /*break*/, 3]; return [4 /*yield*/, this._networkInfoChunk(index, accumLength)]; case 2: chunk = _b.sent(); _a = iotile_common_1.unpackArrayBuffer('xxBB', chunk.slice(0, 4)), totalLength = _a[0], chunkLength = _a[1]; accumLength += chunkLength; chunks.push(chunk); return [3 /*break*/, 1]; case 3: return [2 /*return*/, WirelessNetwork.FromEncodedChunks(chunks)]; } }); }); }; /** * Setup a basic wifi connection. You need to enter the network ssid and password (if there is one). If you want a default dhcp based connection those are the only settings you need. Otherwise, you can specify a static IP address by passing static_ip, dns, netmask and gateway. If you want to create a wifi hotspot, you can pass shared=True and the network name and password are used to create a wifi hotspot. If you combine shared=True with a static IP then you can set the IP of the node and the netmask it will use to allocate IPs for other computers connecting to its hotspot. The dns and gateway options are ignored if shared=True. * @param {number} iface The index of the interface that we wish to configure as a wifi network. This should be a wifi type interface. * @param {string} networkName The ssid of the network that you wish to join * @param {string} password An optional password for the network * @param {string} staticIp An optional static ipv4 address in X.Y.Z.W format * @param {string} dns An optional dns server in X.Y.Z.W format, only used if combined with static_ip. * @param {string} netmask An optional netmask in X.Y.Z.W format, only used if combined with static_ip. * @param {string} gateway An optional default gateway for routing traffic. Only used if combined with static_ip. * @param {boolean} shared Whether to setup the interface as a hotspot or not. */ NetworkConfig.prototype.configWifi = function (iface, networkName, password, staticIp, dns, netmask, gateway, shared) { if (password === void 0) { password = ""; } if (staticIp === void 0) { staticIp = null; } if (dns === void 0) { dns = null; } if (netmask === void 0) { netmask = null; } if (gateway === void 0) { gateway = null; } if (shared === void 0) { shared = false; } return __awaiter(this, void 0, void 0, function () { var ipv4Config; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.beginConnection()]; case 1: _a.sent(); return [4 /*yield*/, this.pushConfigSetting(SettingCodes.InterfaceIndex, iface)]; case 2: _a.sent(); return [4 /*yield*/, this.pushConfigSetting(SettingCodes.NetworkName, networkName)]; case 3: _a.sent(); if (!password) return [3 /*break*/, 5]; return [4 /*yield*/, this.pushConfigSetting(SettingCodes.NetworkKey, password)]; case 4: _a.sent(); _a.label = 5; case 5: ipv4Config = new NetworkInterfaceInfo(iface, true, 0, true); ipv4Config.autoconnect = true; if (staticIp) { ipv4Config.static = true; if (!shared) { if (dns === null || netmask === null || gateway === null) { throw new error_space_1.WifiConfigError("You specified a static IP address but did not also specify a DNS server,\n netmask and gateway.", staticIp, dns, netmask, gateway); } ipv4Config.setIp4Info(staticIp, gateway, netmask); ipv4Config.addDNS(dns); } else { if (netmask === null) { throw new error_space_1.WifiConfigError("You specified a static IP address as a hotspot but did not include a netmask.", staticIp, dns, netmask, gateway); } ipv4Config.setIp4Info(staticIp, '0.0.0.0', netmask); } } ipv4Config.sharing = shared; return [4 /*yield*/, this.pushConfigSetting(SettingCodes.IPConfig, ipv4Config)]; case 6: _a.sent(); return [4 /*yield*/, this.pushConfigSetting(SettingCodes.SharedConnection, shared)]; case 7: _a.sent(); return [4 /*yield*/, this.finishConnection(iface)]; case 8: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Set up the ethernet configuration of a device. Currently only designed for static ip allocation. * * @param {number} iface The index of the interface that we wish to configure as an ethernet network. This should be a wifi type interface. * @param {string} staticIp An optional static ipv4 address in X.Y.Z.W format * @param {string} dns An optional dns server in X.Y.Z.W format, only used if combined with static_ip. * @param {string} netmask An optional netmask in X.Y.Z.W format, only used if combined with static_ip. * @param {string} gateway An optional default gateway for routing traffic. Only used if combined with static_ip. */ NetworkConfig.prototype.configEthernet = function (iface, staticIp, dns, netmask, gateway) { if (staticIp === void 0) { staticIp = null; } if (dns === void 0) { dns = null; } if (netmask === void 0) { netmask = null; } if (gateway === void 0) { gateway = null; } return __awaiter(this, void 0, void 0, function () { var ipv4Config; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.beginConnection()]; case 1: _a.sent(); return [4 /*yield*/, this.pushConfigSetting(SettingCodes.InterfaceIndex, iface)]; case 2: _a.sent(); ipv4Config = new NetworkInterfaceInfo(iface, true, 1, true); ipv4Config.autoconnect = true; if (staticIp) { ipv4Config.static = true; if (netmask === null || gateway === null) { throw new error_space_1.EthernetConfigError("You specified a static IP address but did not also specify a \n netmask and gateway.", staticIp, dns, netmask, gateway); } ipv4Config.setIp4Info(staticIp, gateway, netmask); if (dns) { ipv4Config.addDNS(dns); } } ipv4Config.sharing = false; return [4 /*yield*/, this.pushConfigSetting(SettingCodes.IPConfig, ipv4Config)]; case 3: _a.sent(); return [4 /*yield*/, this.pushConfigSetting(SettingCodes.SharedConnection, false)]; case 4: _a.sent(); return [4 /*yield*/, this.finishConnection(iface)]; case 5: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Begin pushing settings for a new configuration. */ NetworkConfig.prototype.beginConnection = function () { return this.adapter.typedRPC(this.address, 0x8005, "", "", []); }; /** * Finish pushing settings for a new configuration. */ NetworkConfig.prototype.finishConnection = function (index) { return __awaiter(this, void 0, void 0, function () { var err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.adapter.errorHandlingRPC(this.address, 0x8008, "L", "L", [index])]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: err_1 = _a.sent(); if (err_1.name === 'RPCError') { err_1.message = "Unable to configure connection"; } throw err_1; case 3: return [2 /*return*/]; } }); }); }; /** * Push a config setting by short id code. */ NetworkConfig.prototype.pushConfigSetting = function (settingCode, value) { return __awaiter(this, void 0, void 0, function () { var autocommit, flags, initialChunk, validLength, initialData, err_2, remaining, commit; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(value instanceof ArrayBuffer)) { value = this.encodeValue(value); } autocommit = value.byteLength < 16; flags = autocommit << 0; initialChunk = value.slice(0, 16); validLength = initialChunk.byteLength; initialData = iotile_common_1.padArrayBuffer(initialChunk, 16); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.adapter.errorHandlingRPC(this.address, 0x8006, "HBB16s", "L", [settingCode, flags, validLength, initialData])]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: err_2 = _a.sent(); if (err_2.name === 'RPCError') { err_2.message = "Error starting connection setting value. initialData=" + initialData + "; settingCode=" + settingCode; } throw err_2; case 4: remaining = value.slice(16, value.byteLength); _a.label = 5; case 5: if (!(remaining.byteLength > 0)) return [3 /*break*/, 7]; commit = remaining.byteLength <= 18; return [4 /*yield*/, this._pushSettingValueChunk(remaining.slice(0, 18), commit)]; case 6: _a.sent(); remaining = remaining.slice(18, remaining.byteLength); return [3 /*break*/, 5]; case 7: return [2 /*return*/]; } }); }); }; NetworkConfig.prototype.encodeValue = function (value) { if (typeof value === 'boolean') { return iotile_common_1.packArrayBuffer('B', value); } if (typeof value === 'number') { return iotile_common_1.packArrayBuffer('l', value); } if (typeof value === 'string') { return iotile_common_1.packArrayBuffer(value.length + 's', value); } if (value instanceof NetworkInterfaceInfo) { return value.asRPCPayload(); } throw new iotile_common_1.ArgumentError("Unknown value type, cannot encode, type=" + typeof value + ", value=" + value); }; NetworkConfig.prototype._pushSettingValueChunk = function (data, commit) { if (commit === void 0) { commit = false; } return __awaiter(this, void 0, void 0, function () { var flags, validLength, err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: flags = commit << 0; if (data.byteLength > 18) { throw new iotile_common_1.ArgumentError('Attempting to send too much data in a single rpc'); } validLength = data.byteLength; if (data.byteLength < 18) { data = iotile_common_1.padArrayBuffer(data, 18); } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.adapter.errorHandlingRPC(this.address, 0x8007, 'BB18s', 'L', [flags, validLength, data])]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: err_3 = _a.sent(); if (err_3.name === 'RPCError') { err_3.message = "Error pushing chunk of conection setting value. data=" + data + ", commit=" + commit; } throw err_3; case 4: return [2 /*return*/]; } }); }); }; /** * Get information on a given wireless network. You must have previously called list_networks to capture network information from a given interface and then you can iterate over those results by calling this function. If the network name is longer than 20 bytes, you can call this function with a nonzero offset in order to retrieve the higher parts of the name. The same header information is repeated with each call, not just when offset=0. The response contains: - Network auth type and flags - Signal quality - Total name length - Valid name length in this segment - up to 16 bytes of the access point ssid or carrier name depending on whether the network is wifi or cellular. To enquire about the active network for the interface, pass -1 as the index. * @param index * @param offset */ NetworkConfig.prototype._networkInfoChunk = function (index, offset) { return __awaiter(this, void 0, void 0, function () { var info; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.adapter.typedRPC(this.address, 0x8003, 'lB', '20s', [index, offset])]; case 1: info = (_a.sent())[0]; return [2 /*return*/, iotile_common_1.stringToBuffer(info)]; } }); }); }; NetworkConfig.prototype._captureNetworkList = function (index) { return __awaiter(this, void 0, void 0, function () { var count, err_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.adapter.errorHandlingRPC(this.address, 0x8002, 'L', 'LL', [index])]; case 1: count = (_a.sent())[0]; return [2 /*return*/, count]; case 2: err_4 = _a.sent(); if (err_4.name === 'RPCError') { err_4.message = "Error listing networks for interface, interface_index=" + index; } throw err_4; case 3: return [2 /*return*/]; } }); }); }; return NetworkConfig; }()); exports.NetworkConfig = NetworkConfig; //# sourceMappingURL=iotile-network.js.map