advlib-ble-manufacturers
Version:
Wireless advertising packet decoding library for Bluetooth Low Energy manufacturer-specific data. We believe in an open Internet of Things.
85 lines (67 loc) • 2.25 kB
JavaScript
/**
* Copyright reelyActive 2015-2020
* We believe in an open Internet of Things
*/
const SIGNATURE_SEPARATOR = '/';
/**
* Convert the given hexadecimal string or Buffer to a Buffer.
* @param {Object} data A hexadecimal-string or Buffer.
* @return {Object} The data as a Buffer, or null if invalid data format.
*/
function convertToBuffer(data) {
if(Buffer.isBuffer(data)) {
return data;
}
if(typeof data === 'string') {
data = data.toLowerCase();
let isHex = /[0-9a-f]+/.test(data);
if(isHex) {
return Buffer.from(data, 'hex');
}
}
return null;
}
/**
* Convert the given data to a hexadecimal string of the given number of bytes.
* @param {Object} data The data to convert (Number, String or Buffer).
* @param {Number} length The target length of the string in bytes.
* @param {boolean} reverseEndianness Optionally reverse the endianness.
* @return {String} The data as a hexadecimal string.
*/
function convertToHexString(data, length, reverseEndianness) {
let hexString = null;
if(Number.isInteger(data) && (data >= 0)) {
hexString = data.toString(16).padStart(length * 2, '0');
}
if(Buffer.isBuffer(data)) {
hexString = data.toString('hex').padStart(length * 2, '0');
}
if(typeof data === 'string') {
hexString = data.padStart(length * 2, '0');
}
if(hexString && (reverseEndianness === true)) {
return hexString.match(/.{2}/g).reverse().join('');
}
return hexString;
}
/**
* Convert the given signed 8.8 Buffer to a floating point Number.
* @param {Object} data The signed 8.8 Bufferdata to convert.
* @return {Number} The floating point representation as a Number.
*/
function parseSigned88(data) {
return data.readInt8() + (data.readUInt8(1) / 256);
}
/**
* Convert a ZigZag-encoded integer to a signed integer.
* @param {Number} data The ZigZag-encoded integer.
* @return {Number} The decoded signed integer.
*/
function decodeZigZag(data) {
return (data >> 1) ^ -(data & 1);
}
module.exports.SIGNATURE_SEPARATOR = SIGNATURE_SEPARATOR;
module.exports.convertToBuffer = convertToBuffer;
module.exports.convertToHexString = convertToHexString;
module.exports.parseSigned88 = parseSigned88;
module.exports.decodeZigZag = decodeZigZag;