def getPacket(self):
		"""
		Adds start token, header and CRC to payload. Escapes bytes.
		"""
		# Construct the packet for CRC calculation
		packet = []
		packet.append(self.protocolMajor)
		packet.append(self.protocolMinor)
		packet.append(int(self.messageType))
		packet += self.payload

		# Calculate the CRC of the packet
		packetCrc = crc16ccitt(packet)

		# Append the CRC to the base packet to escape the entire thing
		packet += Conversion.uint16_to_uint8_array(packetCrc)

		# Prepend the size field.
		sizeField = len(packet)
		packet = Conversion.uint16_to_uint8_array(sizeField) + packet

		# Escape everything except the start token
		packet = self.escapeCharacters(packet)

		# Prepend the start token.
		packet = [START_TOKEN] + packet
		return packet
Beispiel #2
0
 def getPacket(self):
     # TODO: make use of the base class, now there is a double implementation of the control packet.
     arr = [SUPPORTED_PROTOCOL_VERSION]
     arr += Conversion.uint16_to_uint8_array(self.type)
     arr += Conversion.uint16_to_uint8_array(
         self.length + 6
     )  # the + 2 is for the stateType uint16, +2 for the id, +2 for persistenceMode and reserved
     arr += self.payload  # this is the state type
     arr += Conversion.uint16_to_uint8_array(self.id)
     arr.append(self.persistenceMode)
     arr.append(0)
     return arr
Beispiel #3
0
    def getPacket(self):
        packet = [SUPPORTED_PROTOCOL_VERSION]
        packet += Conversion.uint16_to_uint8_array(self.type)
        packet += self.lengthAsUint8Array
        packet += self.payload

        return packet
Beispiel #4
0
def obtainTimestamp(fullTimeStamp, lsb):
    timestampBytes = Conversion.uint32_to_uint8_array(int(fullTimeStamp))
    lsbBytes = Conversion.uint16_to_uint8_array(lsb)

    restoredTimestamp = Conversion.uint8_array_to_uint32(
        [lsbBytes[0], lsbBytes[1], timestampBytes[2], timestampBytes[3]])

    return restoredTimestamp
Beispiel #5
0
    def getIBeaconConfigIdPacket(id, timestamp, interval):
        data = []
        data.append(id)
        data += Conversion.uint32_to_uint8_array(timestamp)
        data += Conversion.uint16_to_uint8_array(interval)

        return ControlPacket(
            ControlType.SET_IBEACON_CONFIG_ID).loadByteArray(data).serialize()
Beispiel #6
0
    def getSetupPacket(crownstoneId: int, sphereId: int, adminKey, memberKey,
                       basicKey, serviceDataKey, localizationKey,
                       meshDeviceKey, meshAppKey, meshNetworkKey,
                       ibeaconUUID: str, ibeaconMajor: int, ibeaconMinor: int):
        """
        :param crownstoneId:  		uint8 number
        :param sphereId:  	     	uint8 number
        :param adminKey:      		byteString (no conversion required)
        :param memberKey:     		byteString (no conversion required)
        :param basicKey:      		byteString (no conversion required)
        :param serviceDataKey: 	    byteString (no conversion required)
        :param localizationKey: 	byteString (no conversion required)
        :param meshDeviceKey: 	    byteString (no conversion required)
        :param meshAppKey: 	        byteString (no conversion required)
        :param meshNetworkKey: 	    byteString (no conversion required)
        :param ibeaconUUID: 		string  (ie. "1843423e-e175-4af0-a2e4-31e32f729a8a")
        :param ibeaconMajor:        uint16 number
        :param ibeaconMinor:        uint16 number
        :return:
        """
        data = []
        data.append(crownstoneId)
        data.append(sphereId)

        data += list(adminKey)
        data += list(memberKey)
        data += list(basicKey)
        data += list(serviceDataKey)
        data += list(localizationKey)

        MDKey = meshDeviceKey
        if type(meshDeviceKey) is str:
            MDKey = Conversion.ascii_or_hex_string_to_16_byte_array(
                meshDeviceKey)

        data += list(MDKey)
        data += list(meshAppKey)
        data += list(meshNetworkKey)

        data += Conversion.ibeaconUUIDString_to_reversed_uint8_array(
            ibeaconUUID)
        data += Conversion.uint16_to_uint8_array(ibeaconMajor)
        data += Conversion.uint16_to_uint8_array(ibeaconMinor)

        return ControlPacket(ControlType.SETUP).loadByteArray(data).serialize()
    def getPacket(self):
        # Header: 1B device ID, 2B opcode

        # construct the basePacket, which is used for CRC calculation
        uartMsg = []
        uartMsg += Conversion.uint16_to_uint8_array(self.opCode)
        uartMsg += self.payload

        return uartMsg
Beispiel #8
0
def write_uint16(outfile, val):
    for byt in Conversion.uint16_to_uint8_array(val):
        write_uint8(outfile, byt)
 def putUInt16(self, value):
     """ Append a uint16 to the buffer. """
     self.data.extend(Conversion.uint16_to_uint8_array(value))
Beispiel #10
0
 def _process(self):
     self.length = len(self.payload)
     self.lengthAsUint8Array = Conversion.uint16_to_uint8_array(
         len(self.payload))
     return self
Beispiel #11
0
 def loadUInt16(self, uint16):
     self.payload = Conversion.uint16_to_uint8_array(uint16)
     return self._process()