예제 #1
0
 def load(self, data):
     reader = BufferReader(data)
     self.stateType = reader.getUInt16()
     self.id = reader.getUInt16()
     self.persistenceMode = reader.getUInt8()
     reserved = reader.getUInt8()
     self.payload = reader.getRemainingBytes()
예제 #2
0
    def _parse(self, reader: BufferReader):
        self.fileNameHash = reader.getUInt32()
        self.lineNr = reader.getUInt16()
        self.logLevel = reader.getUInt8()

        flags = reader.getUInt8()
        self.newLine = (flags & (1 << 0)) != 0
 def _deserialize(self, reader: BufferReader):
     self.header.deserialize(reader)
     self.numArgs = reader.getUInt8()
     self.argBufs = []
     for i in range(0, self.numArgs):
         argSize = reader.getUInt8()
         self.argBufs.append(reader.getBytes(argSize))
예제 #4
0
 def _deserialize(self, reader: BufferReader):
     self.protocol = reader.getUInt8()
     self.dfuVersion = reader.getUInt16()
     self.major = reader.getUInt8()
     self.minor = reader.getUInt8()
     self.patch = reader.getUInt8()
     self.preReleaseVersion = reader.getUInt8()
     self.buildType = BootloaderBuildType(reader.getUInt8())
예제 #5
0
	def _deserialize(self, reader: BufferReader):
		self.fileNameHash = reader.getUInt32()
		self.lineNr = reader.getUInt16()
		self.logLevel = reader.getUInt8()

		flags = reader.getUInt8()
		self.newLine = (flags & (1 << 0)) != 0
		self.reverse = (flags & (1 << 1)) != 0
예제 #6
0
    def _parse(self, reader: BufferReader):
        self.protocol = reader.getUInt8()
        self.maxApps = reader.getUInt8()
        self.maxAppSize = reader.getUInt16()
        self.maxChunkSize = reader.getUInt16()
        self.maxRamUsage = reader.getUInt16()
        self.sdkVersion.parse(reader)

        self.appsStatus = []
        for i in range(0, self.maxApps):
            statusPacket = MicroappStatusPacket()
            statusPacket.parse(reader)
            self.appsStatus.append(statusPacket)
예제 #7
0
def _parseErrorPacket(packet, reader: BufferReader):
    packet.crownstoneId     = reader.getUInt8()
    packet.errorsBitmask    = AdvCrownstoneErrorBitmask(reader.getUInt32())
    packet.errorTimestamp   = reader.getUInt32()
    packet.flags            = AdvFlags(reader.getUInt8())
    packet.temperature      = reader.getInt8()
    partialTimestamp        = reader.getUInt16()
    packet.uniqueIdentifier = partialTimestamp

    if packet.flags.timeIsSet:
        packet.timestamp = reconstructTimestamp(time.time(), partialTimestamp)
    else:
        packet.timestamp = partialTimestamp  # this is now a counter
	def load(self, data):
		"""
		Parses data buffer to set member variables.

		data : list of bytes

		Raises exception when parsing fails.
		"""
		streamBuf = BufferReader(data)
		self.timestamp = streamBuf.getUInt32()
		self.switchCommand = streamBuf.getUInt8()
		self.switchState = SwitchState(streamBuf.getUInt8())
		self.source = CommandSourcePacket(streamBuf.getBytes(CommandSourcePacket.size()))
    def load(self, data):
        """
		Parses data buffer to set member variables.

		data : list of bytes

		Raises exception when parsing fails.
		"""
        streamBuf = BufferReader(data)
        byte1 = streamBuf.getUInt8()
        sourceTypeval = (byte1 >> 5) & 7
        self.sourceType = CommandSourceType(
            sourceTypeval)  # Throws exception of value is not in enum
        self.viaMesh = (byte1 & (1 << 0)) != 0
        self.sourceId = streamBuf.getUInt8()
    def load(self, data) :
        # TODO: remove minSize, and use: self.payload = payload.getRemainingBytes()
        # TODO: raise error instead of setting self.valid.
        minSize = 7

        if len(data) >= minSize:
            payload = BufferReader(data)

            self.protocol = payload.getUInt8()
            self.commandTypeUInt16 = payload.getUInt16()
            resultNumber  = payload.getUInt16()

            if ControlType.has_value(self.commandTypeUInt16) and ResultValue.has_value(resultNumber):
                self.commandType = ControlType(self.commandTypeUInt16)
                self.resultCode  = ResultValue(resultNumber)
                self.size        = payload.getUInt16()

                totalSize = minSize + self.size
                if len(data) >= totalSize:
                    if self.size == 0:
                        return

                    for i in range(minSize, totalSize):
                        self.payload.append(data[i])
                else:
                    self.valid = False
            else:
                self.valid = False
        else:
            self.valid = False
예제 #11
0
	def _parse(self, reader: BufferReader):
		data1 = reader.getUInt8()
		data2 = reader.getUInt8()
		self.raw = [data1, data2]

		# 1 bit hasData
		self.hasData = (data1 & 0x01) == 1
		data1 = data1 >> 1

		# 2 bits checksum
		self.checksum = data1 & 0x03
		data1 = data1 >> 2

		# 1 bit enabled
		self.enabled = (data1 & 0x01) == 1
		data1 = data1 >> 1

		# 2 bits boot
		self.boot = data1 & 0x03
		data1 = data1 >> 2

		# 1 bit memory
		self.memory = data1 & 0x01
		data1 = data1 >> 1

		# 9 bits reserved
		self.reserved = data1
def parseExternalStatePacket(reader: BufferReader):
    packet = AdvExternalCrownstoneState()
    _parseStatePacket(packet, reader)
    # position 13 on the state packet is the global flags, it is used in the external state for rssi
    packet.rssiOfExternalCrownstone = reader.getInt8()
    packet.validation = reader.getUInt8()
    return packet
예제 #13
0
	def parse(self, buffer: list):
		"""
		Parses data between size field and CRC.

		:returns True on success.
		"""
		reader = BufferReader(buffer)
		try:
			self.protocolMajor = reader.getUInt8()
			self.protocolMinor = reader.getUInt8()
			self.messageType   = reader.getUInt8()
			self.payload       = reader.getRemainingBytes()
			return True
		except CrownstoneError as e:
			_LOGGER.warning(F"Parse error: {e}")
			return False
 def _deserialize(self, reader: BufferReader):
     self.commandProtocolVersion = reader.getUInt8()
     self.masterVersion = reader.getUInt16()
     self.masterCrc = reader.getUInt32()
     self.freeSpace = reader.getUInt16()
     self.summaries.clear()
     while reader.getRemainingByteCount():
         self.summaries.append(FilterSummaryPacket(reader))
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
예제 #16
0
    def fromData(self, data):
        payload = BufferReader(data)

        firstByte = payload.getUInt8()
        if not BehaviourType.has_value(firstByte):
            self.valid = False
            return self

        self.behaviourType = BehaviourType(firstByte)
        self.intensity = payload.getUInt8()
        self.profileIndex = payload.getUInt8()
        self.activeDays = ActiveDays().fromData(payload.getUInt8())
        self.fromTime = BehaviourTime().fromData(
            payload.getBytes(5))  # 4 5 6 7 8
        self.untilTime = BehaviourTime().fromData(
            payload.getBytes(5))  # 9 10 11 12 13

        if self.fromTime.valid == False or self.untilTime.valid == False:
            self.valid = False
            return self

        if self.behaviourType == BehaviourType.behaviour:
            if payload.length >= 14 + 13:
                self.presence = BehaviourPresence().fromData(
                    payload.getBytes(
                        13))  # 14 15 16 17 18 19 20 21 22 23 24 25 26
                if not self.presence.valid:
                    self.valid = False
                    return self
            else:
                self.valid = False
                return self

        if self.behaviourType == BehaviourType.smartTimer:
            if payload.length >= 14 + 13 + 17:
                presence = BehaviourPresence().fromData(payload.getBytes(17))
                if not presence.valid:
                    self.valid = False
                    return self

                self.endCondition = presence

            else:
                self.valid = False
                return self
def parseMicroappServiceData(reader: BufferReader):
    packet = AdvMicroappData()

    packet.flags = AdvMicroappFlags(reader.getUInt8())
    packet.microappUuid = reader.getUInt16()
    packet.microappData = reader.getBytes(8)
    packet.crownstoneId = reader.getUInt8()
    packet.partialTimestamp = reader.getUInt16()
    packet.validation = reader.getUInt8()

    packet.uniqueIdentifier = packet.partialTimestamp
    if packet.flags.timeIsSet:
        packet.timestamp = reconstructTimestamp(time.time(),
                                                packet.partialTimestamp)
    else:
        packet.timestamp = packet.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
예제 #19
0
    def load(self, data):
        if len(data) == 16:
            payload = BufferReader(data)

            self.validation = payload.getUInt32()
            self.protocol = payload.getUInt8()
            self.sessionNonce = payload.getBytes(5)
            self.validationKey = payload.getBytes(4)
        else:
            self.valid = False
예제 #20
0
    def __init__(self, data):
        if isinstance(data, BufferReader):
            reader = data
        else:
            reader = BufferReader(data)

        # Parse buffer
        self.sphereId = reader.getUInt8()

        self.status = UartCrownstoneStatusPacket(reader)
예제 #21
0
def parseOpCode6(data):
    reader = BufferReader(data)
    dataType = reader.getUInt8()

    if dataType == 0:
        return parseSetupState(reader)
    elif dataType == 5:
        return parseHubData(reader)
    else:
        raise CrownstoneException(CrownstoneError.UNKNOWN_SERVICE_DATA,
                                  "Could not parse this dataType")
    def __init__(self, buffer: BufferReader or list or bytearray):
        if isinstance(buffer, BufferReader):
            reader = buffer
        else:
            reader = BufferReader(buffer)

        self.flags = reader.getUInt8()

        # Parse flags
        self.encryptionRequired = self.flags & (1 << 0) != 0
        self.hasBeenSetUp       = self.flags & (1 << 1) != 0
        self.hubMode            = self.flags & (1 << 2) != 0
        self.hasError           = self.flags & (1 << 3) != 0
예제 #23
0
    def __init__(self, data):
        """
        The parsed data ends up in the self.payload. These will be one of the Adv* classes defined in the ./containers/ folder.
        """
        self.opCode = 0
        self.deviceType = DeviceType.UNDEFINED
        self.operationMode = CrownstoneOperationMode.UNKNOWN
        self.payload = None
        self.decrypted = False

        self._data = data

        reader = BufferReader(self._data)
        self.opCode = reader.getUInt8()
        if self.opCode == 7:
            self.operationMode = CrownstoneOperationMode.NORMAL
        elif self.opCode == 6:
            self.operationMode = CrownstoneOperationMode.SETUP

        deviceType = reader.getUInt8()
        if DeviceType.has_value(deviceType):
            self.deviceType = DeviceType(deviceType)
	def load(self, data):
		"""
		Parses data buffer to set member variables.

		data : list of bytes

		Raises exception when parsing fails.
		"""
		streamBuf = BufferReader(data)
		count = streamBuf.getUInt8()
		self.list = []
		for i in range(0, count):
			self.list.append(SwitchHistoryItemPacket(streamBuf.getBytes(SwitchHistoryItemPacket.size())))
	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())
예제 #26
0
    def _parse(self, reader: BufferReader):
        self.buildVersion = reader.getUInt32()
        self.sdkVersion.parse(reader)
        self.checksum = reader.getUInt16()
        self.checksumHeader = reader.getUInt16()
        self.tests.parse(reader)
        self.functionTrying = reader.getUInt8()
        self.functionFailed = reader.getUInt8()

        self.functionsPassed = []
        functionsPassed = reader.getUInt32()
        for i in range(0, 32):
            if functionsPassed & (1 << i):
                self.functionsPassed.append(i)
예제 #27
0
def parseSetupState(reader: BufferReader):
    packet = AdvCrownstoneSetupState()

    packet.switchState = SwitchState(reader.getUInt8())
    packet.flags = AdvFlags(reader.getUInt8())
    packet.temperature = reader.getInt8()
    powerFactor = reader.getInt8()
    realPower = reader.getInt16()

    packet.powerFactor = float(powerFactor) / 127.0

    # we cannot have a 0 for a power factor.To avoid division by 0, we set it to be either 0.01 or -0.01
    if 0 <= packet.powerFactor < 0.01:
        packet.powerFactor = 0.01
    elif -0.01 < packet.powerFactor < 0:
        packet.powerFactor = -0.01

    packet.powerUsageReal = float(realPower) / 8.0
    packet.powerUsageApparent = packet.powerUsageReal / packet.powerFactor
    packet.errorBitmask = reader.getInt32()
    packet.uniqueIdentifier = reader.getUInt8()

    return packet
    def fromData(self, data):
        if len(data) != 13:
            self.valid = False
            return self

        payload = BufferReader(data)
        firstByte = payload.getUInt8()
        if not BehaviourPresenceType.has_value(firstByte):
            self.valid = False
            return self

        self.behaviourType = BehaviourPresenceType(firstByte)
        self.locationIds = self.unpackMask(payload.getUInt64())
        self.delayInSeconds = payload.getUInt32()

        return self
    def fromBuffer(self, buf: list):
        reader = BufferReader(buf)

        self.sdkVersionMajor = reader.getUInt8()
        self.sdkVersionMinor = reader.getUInt8()
        self.size = reader.getUInt16()

        self.checksum = reader.getUInt16()
        self.checksumHeader = reader.getUInt16()

        self.appBuildVersion = reader.getUInt32()

        self.startOffset = reader.getUInt16()
        self.reserved = reader.getUInt16()

        self.reserved2 = reader.getUInt32()
예제 #30
0
    def fromData(self, data):
        if len(data) != 5:
            self.valid = False
            return self

        payload = BufferReader(data)

        firstByte = payload.getUInt8()
        if not BehaviourTimeType.has_value(firstByte):
            self.valid = False
            return self

        self.timeType = BehaviourTimeType(firstByte)
        self.offset = payload.getInt32()
        self.valid = True

        return self