Example #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()
    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
 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 _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)
    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)
    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()
    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
Example #8
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())
Example #9
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
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
Example #12
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)

		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())
    def parse(self, data: list):
        """
		Parses data.

		:returns True on success.
		"""
        try:
            reader = BufferReader(data)
            self.opCode = reader.getUInt16()
            self.payload = reader.getRemainingBytes()
            return True
        except CrownstoneError as e:
            _LOGGER.warning(F"Parse error: {e}")
            return False
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
Example #16
0
	def _parse(self, reader: BufferReader):
		self.header.parse(reader)
		self.offset = reader.getUInt16()
		self.binaryChunk = reader.getRemainingBytes()
 def _deserialize(self, reader: BufferReader):
     self.header.deserialize(reader)
     self.offset = reader.getUInt16()
     self.binaryChunk = reader.getRemainingBytes()
Example #18
0
    def _getElementValues(
            self,
            elementType: int,  # TODO: make enum
            elementSize: int,
            elementData: list) -> list:
        bufferReader = BufferReader(elementData)
        dataSize = len(elementData)
        if dataSize % elementSize != 0:
            _LOGGER.warning(
                f"Remaining data with element size of {elementSize} and element data of size {dataSize}"
            )
            return []

        vals = []
        numElements = int(dataSize / elementSize)
        _LOGGER.debug(
            f"dataSize={dataSize} elementSize={elementSize} numElements={numElements}"
        )
        for i in range(0, numElements):
            if elementType == 0:
                # Signed integer
                if elementSize == 1:
                    vals.append(bufferReader.getInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 1:
                # Unsigned integer
                if elementSize == 1:
                    vals.append(bufferReader.getUInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getUInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getUInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getUInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 2:
                # Floating point
                if elementSize == 4:
                    vals.append(bufferReader.getFloat())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []
        return vals