Exemplo n.º 1
0
    def parse(self, decryptionKey=None):
        if decryptionKey is not None:
            self.decrypt(decryptionKey)

        reader = BufferReader(self._data)
        reader.skip(2)

        if self.opCode == 7:
            try:
                self.payload = parseOpcode7(reader.getRemainingBytes())
            except CrownstoneException:
                self.payload = AdvUnknownData()
                self.payload.data = self._data
                raise CrownstoneException(
                    CrownstoneError.INVALID_SERVICE_DATA,
                    "Protocol not supported. Unknown data type. Could be because decryption failed."
                )
        elif self.opCode == 6:
            self.payload = parseOpCode6(reader.getRemainingBytes())
        else:
            self.payload = AdvUnknownData()
            self.payload.data = self._data
            raise CrownstoneException(
                CrownstoneError.INVALID_SERVICE_DATA,
                "Protocol not supported. Unknown opcode.")
def parseHubData(reader: BufferReader):
    packet = AdvHubState()

    packet.crownstoneId = reader.getUInt8()
    packet.flags = AdvHubFlags(reader.getUInt8())
    packet.hubData = reader.getBytes(9)
    partialTimestamp = reader.getUInt16()
    reader.skip()
    packet.validation = reader.getUInt8()

    packet.uniqueIdentifier = partialTimestamp
    if packet.flags.timeIsSet:
        packet.timestamp = reconstructTimestamp(time.time(), partialTimestamp)
    else:
        packet.timestamp = partialTimestamp  # this is now a counter

    return packet
def parseAlternativeState(reader: BufferReader):
    packet = AdvAlternativeState()
    packet.crownstoneId             = reader.getUInt8()
    packet.switchState              = SwitchState(reader.getUInt8())
    packet.flags                    = AdvFlags(reader.getUInt8())
    packet.behaviourMasterHash      = reader.getUInt16()
    packet.assetFilterMasterVersion = reader.getUInt16()
    packet.assetFilterMasterCRC     = reader.getUInt32()
    partialTimestamp                = reader.getUInt16()
    reader.skip()
    packet.validation               = reader.getUInt8()
    packet.uniqueIdentifier         = partialTimestamp
    
    if packet.flags.timeIsSet:
        packet.timestamp = reconstructTimestamp(time.time(), partialTimestamp)
    else:
        packet.timestamp = partialTimestamp # this is now a counter

    return packet
	def load(self, data):
		"""
		Parses data buffer to set member variables.

		data : list of bytes

		Raises exception when parsing fails.
		"""
		streamBuf = BufferReader(data)

		samplesTypeVal = streamBuf.getUInt8()
		self.samplesType = PowerSamplesType(samplesTypeVal) # Throws exception of value is not in enum
		self.index = streamBuf.getUInt8()
		self.count = streamBuf.getUInt16()
		self.timestamp = streamBuf.getUInt32()
		self.delayUs = streamBuf.getUInt16()
		self.sampleIntervalUs = streamBuf.getUInt16()
		streamBuf.skip(2)
		self.offset = streamBuf.getInt16()
		self.multiplier = streamBuf.getFloat()
		self.samples = []
		for i in range(0, self.count):
			self.samples.append(streamBuf.getInt16())
Exemplo n.º 5
0
    def decrypt(self, keyHexString):
        if len(keyHexString) >= 16 and len(self._data) == 18:
            if self.operationMode == CrownstoneOperationMode.NORMAL:
                reader = BufferReader(self._data)
                encryptedData = reader.skip(2).getRemainingBytes()
                result = EncryptionHandler.decryptECB(encryptedData,
                                                      keyHexString)

                for i in range(0, len(encryptedData)):
                    # the first 2 bytes are opcode and device type
                    self._data[i + 2] = result[i]

                self.decrypted = True
        else:
            raise CrownstoneException(
                CrownstoneError.COULD_NOT_DECRYPT,
                "ServiceData decryption failed. Invalid key or invalid data.")