@iotile/iotile-device
Version:
A typescript library for interfacing with IOTile BLE devices
119 lines • 5.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var iotile_common_1 = require("@iotile/iotile-common");
var advertisements_1 = require("./advertisements");
var IOTileAdvertisementService = /** @class */ (function () {
function IOTileAdvertisementService() {
}
IOTileAdvertisementService.prototype.processAdvertisement = function (connectionID, rssi, rawAdvert) {
var advert;
if (rawAdvert instanceof ArrayBuffer) {
advert = advertisements_1.Advertisement.FromAndroid(rawAdvert);
}
else {
advert = advertisements_1.Advertisement.FromIOS(rawAdvert);
}
/**
* Do a series of checks to make sure that we are looking at a valid IOTile advertisement.
*
* We currently support the following advertisements:
*
* v1: We recieve manufacturer data for arch systems and a fixed 128bit service UUID.
* v2: We recieve service data for arch systems and an assigned 16bit service UUID.
*/
if (advert.containsService(advertisements_1.IOTileV1ServiceUUID)) {
var manuData = advert.getManufacturerData(advertisements_1.ArchManufacturerCode);
if (manuData == null)
return null;
if (manuData.byteLength < 6)
return null; //6 is the number of bytes in the initial advertisement packet (not including anything in the scan response)
return this.processValidAdvertisementV1(connectionID, rssi, advert);
}
else if (advert.containsService(advertisements_1.IOTileV2ServiceUUID)) {
var serviceData = advert.getServiceData(parseInt(advertisements_1.IOTileV2ServiceUUID, 16));
if (serviceData == null)
return null;
if (serviceData.byteLength < 9)
return null; //9 is the number of bytes in the advertisement packet that we will need
return this.processValidAdvertisementV2(connectionID, rssi, advert);
}
else {
return null;
}
};
IOTileAdvertisementService.prototype.processValidAdvertisementV1 = function (connectionID, rssi, advert) {
var manuData = advert.getManufacturerData(advertisements_1.ArchManufacturerCode);
if (manuData == null)
throw new iotile_common_1.ArgumentError("Missing manufactururer data in processValidAdvertisementV1");
if (manuData.byteLength < 6)
throw new iotile_common_1.ArgumentError("Manufacturer data too short, length=" + manuData.byteLength);
var _a = iotile_common_1.unpackArrayBuffer("LH", manuData.slice(0, 6)), uuid = _a[0], rawFlags = _a[1];
var slug = iotile_common_1.deviceIDToSlug(uuid);
var flags = parseFlagsV1(rawFlags);
return {
batteryVoltage: 0,
deviceID: uuid,
slug: slug,
connectionID: connectionID,
rssi: rssi,
flags: flags
};
};
IOTileAdvertisementService.prototype.processValidAdvertisementV2 = function (connectionID, rssi, advert) {
var serviceData = advert.getServiceData(parseInt(advertisements_1.IOTileV2ServiceUUID, 16));
if (serviceData == null)
throw new iotile_common_1.ArgumentError("Missing service data in processValidAdvertisementV2");
if (serviceData.byteLength < 9)
throw new iotile_common_1.ArgumentError("Service data too short, length=" + serviceData.byteLength);
/**
* We skip the 4 bytes betweein the uuid and the flags since this contains information
* relevant for decrypting broadcast information but not for what we need here.
*/
var _a = iotile_common_1.unpackArrayBuffer("L3xBLBxHL", serviceData.slice(0, 20)), uuid = _a[0], rawFlags = _a[1], timestamp = _a[2], battery = _a[3], broadcastStreamPacked = _a[4], broadcastValue = _a[5];
var slug = iotile_common_1.deviceIDToSlug(uuid);
var flags = parseFlagsV2(rawFlags);
var broadcastStream = broadcastStreamPacked & ((1 << 15) - 1);
return {
batteryVoltage: battery,
deviceID: uuid,
slug: slug,
connectionID: connectionID,
rssi: rssi,
flags: flags,
broadcastStream: broadcastStream,
broadcastValue: broadcastValue,
timestamp: timestamp
};
};
return IOTileAdvertisementService;
}());
exports.IOTileAdvertisementService = IOTileAdvertisementService;
/**
* Parse a 16-bit integer with flags from an advertising packet into an IOTileAdvertisementFlagsV1 object
*/
function parseFlagsV1(flags) {
return {
hasData: ((flags & (1 << 0)) !== 0),
lowVoltage: ((flags & (1 << 1)) !== 0),
otherConnected: ((flags & (1 << 2)) !== 0),
robustReports: ((flags & (1 << 3)) !== 0),
fastWrites: ((flags & (1 << 4)) !== 0)
};
}
/**
* Parse a 16-bit integer with flags from an advertising packet into an IOTileAdvertisementFlagsV2 object
*/
function parseFlagsV2(flags) {
return {
hasData: ((flags & (1 << 0)) !== 0),
lowVoltage: ((flags & (1 << 1)) !== 0),
otherConnected: ((flags & (1 << 2)) !== 0),
dataIsEncrypted: ((flags & (1 << 3)) !== 0),
keyIsDeviceKey: ((flags & (1 << 4)) !== 0),
keyIsUserKey: ((flags & (1 << 5)) !== 0),
isSynchronized: ((flags & (1 << 6)) !== 0),
robustReports: true,
fastWrites: true
};
}
//# sourceMappingURL=iotile-advert-serv.js.map