UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

67 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const iotile_common_1 = require("@iotile/iotile-common"); function parseUTF8String(data) { let bytes = new Uint8Array(data); let i = 0; let s = ''; while (i < bytes.length) { var c = bytes[i++]; if (c > 127) { if (c > 191 && c < 224) { if (i >= bytes.length) throw new Error('UTF-8 decode: incomplete 2-byte sequence'); c = (c & 31) << 6 | bytes[i++] & 63; } else if (c > 223 && c < 240) { if (i + 1 >= bytes.length) throw new Error('UTF-8 decode: incomplete 3-byte sequence'); c = (c & 15) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63; } else if (c > 239 && c < 248) { if (i + 2 >= bytes.length) throw new Error('UTF-8 decode: incomplete 4-byte sequence'); c = (c & 7) << 18 | (bytes[i++] & 63) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63; } else throw new Error('UTF-8 decode: unknown multibyte start 0x' + c.toString(16) + ' at index ' + (i - 1)); } if (c <= 0xffff) { s += String.fromCharCode(c); } else if (c <= 0x10ffff) { c -= 0x10000; s += String.fromCharCode(c >> 10 | 0xd800); s += String.fromCharCode(c & 0x3FF | 0xdc00); } else { throw new Error('UTF-8 decode: code point 0x' + c.toString(16) + ' exceeds UTF-16 reach'); } } return s; } exports.parseUTF8String = parseUTF8String; function parseBinaryUUID(data, reverse) { if (data.byteLength !== 16) throw new Error(`A binary 128-bit uuid must be exactly 16 bytes long, length was ${data.byteLength}`); if (reverse) { let reversedData = new Uint8Array(16); let origData = new Uint8Array(data); for (let i = 0; i < 16; ++i) { reversedData[15 - i] = origData[i]; } data = reversedData.buffer; } let view = new DataView(data); let timeLow = iotile_common_1.numberToHexString(view.getUint32(0, false), 8); let timeMid = iotile_common_1.numberToHexString(view.getUint16(4, false), 4); let timeHigh = iotile_common_1.numberToHexString(view.getUint16(6, false), 4); let clockResHigh = iotile_common_1.numberToHexString(view.getUint8(8), 2); let clockResLow = iotile_common_1.numberToHexString(view.getUint8(9), 2); let node1 = iotile_common_1.numberToHexString(view.getUint16(10, false), 4); let node2 = iotile_common_1.numberToHexString(view.getUint32(12, false), 8); let guid = `${timeLow}-${timeMid}-${timeHigh}-${clockResHigh}${clockResLow}-${node1}${node2}`; return guid.toUpperCase(); } exports.parseBinaryUUID = parseBinaryUUID; //# sourceMappingURL=utilities.js.map