def setPayload(self, payload): if len(payload) > 9: raise MessageError( 'Could not set payload (payload too long).') self.payload = [] for byte in payload: self.payload += byte
def setAdvOptions2(self, num): if (num > 0xFF) or (num < 0x00): raise MessageError('Could not set adv options 2 ' \ '(out of range).') if len(self.payload) == 4: self.payload.append('\x00') self.payload[4] = chr(num)
def decode(self, raw): if len(raw) < 5: raise MessageError('Could not decode (message is incomplete).') sync, length, type_ = struct.unpack('BBB', raw[:3]) if sync != MESSAGE_TX_SYNC: raise MessageError('Could not decode (expected TX sync).') if length > 9: raise MessageError('Could not decode (payload too long).') if len(raw) < (length + 4): raise MessageError('Could not decode (message is incomplete).') self.setType(type_) self.setPayload(raw[3:length + 3]) if self.getChecksum() != ord(raw[length + 3]): raise MessageError('Could not decode (bad checksum).', internal='CHECKSUM') return self.getSize()
def getHandler(self, raw=None): if raw: self.decode(raw) msg = None if self.type_ == MESSAGE_CHANNEL_UNASSIGN: msg = ChannelUnassignMessage() elif self.type_ == MESSAGE_CHANNEL_ASSIGN: msg = ChannelAssignMessage() elif self.type_ == MESSAGE_CHANNEL_ID: msg = ChannelIDMessage() elif self.type_ == MESSAGE_CHANNEL_PERIOD: msg = ChannelPeriodMessage() elif self.type_ == MESSAGE_CHANNEL_SEARCH_TIMEOUT: msg = ChannelSearchTimeoutMessage() elif self.type_ == MESSAGE_CHANNEL_FREQUENCY: msg = ChannelFrequencyMessage() elif self.type_ == MESSAGE_CHANNEL_TX_POWER: msg = ChannelTXPowerMessage() elif self.type_ == MESSAGE_NETWORK_KEY: msg = NetworkKeyMessage() elif self.type_ == MESSAGE_TX_POWER: msg = TXPowerMessage() elif self.type_ == MESSAGE_SYSTEM_RESET: msg = SystemResetMessage() elif self.type_ == MESSAGE_CHANNEL_OPEN: msg = ChannelOpenMessage() elif self.type_ == MESSAGE_CHANNEL_CLOSE: msg = ChannelCloseMessage() elif self.type_ == MESSAGE_CHANNEL_REQUEST: msg = ChannelRequestMessage() elif self.type_ == MESSAGE_CHANNEL_BROADCAST_DATA: msg = ChannelBroadcastDataMessage() elif self.type_ == MESSAGE_CHANNEL_ACKNOWLEDGED_DATA: msg = ChannelAcknowledgedDataMessage() elif self.type_ == MESSAGE_CHANNEL_BURST_DATA: msg = ChannelBurstDataMessage() elif self.type_ == MESSAGE_CHANNEL_EVENT: msg = ChannelEventMessage() elif self.type_ == MESSAGE_CHANNEL_STATUS: msg = ChannelStatusMessage() elif self.type_ == MESSAGE_VERSION: msg = VersionMessage() elif self.type_ == MESSAGE_CAPABILITIES: msg = CapabilitiesMessage() elif self.type_ == MESSAGE_SERIAL_NUMBER: msg = SerialNumberMessage() else: raise MessageError('Could not find message handler ' \ '(unknown message type).') msg.setPayload(self.getPayload()) return msg
def setType(self, type_): if (type_ > 0xFF) or (type_ < 0x00): raise MessageError('Could not set type (type out of range).') self.type_ = type_
def setSerialNumber(self, serial): if (len(serial) != 4): raise MessageError('Could not set serial number ' \ '(expected 4 bytes).') self.setPayload(serial)
def setAdvOptions(self, num): if (num > 0xFF) or (num < 0x00): raise MessageError('Could not set adv options ' \ '(out of range).') self.payload[3] = chr(num)
def setMaxNetworks(self, num): if (num > 0xFF) or (num < 0x00): raise MessageError('Could not set max networks ' \ '(out of range).') self.payload[1] = chr(num)
def setMaxChannels(self, num): if (num > 0xFF) or (num < 0x00): raise MessageError('Could not set max channels ' \ '(out of range).') self.payload[0] = chr(num)
def setVersion(self, version): if (len(version) != 9): raise MessageError('Could not set ANT version ' \ '(expected 9 bytes).') self.setPayload(version)
def setStatus(self, status): if (status > 0xFF) or (status < 0x00): raise MessageError('Could not set channel status ' \ '(out of range).') self.payload[1] = chr(status)
def setMessageCode(self, message_code): if (message_code > 0xFF) or (message_code < 0x00): raise MessageError('Could not set message code ' \ '(out of range).') self.payload[2] = chr(message_code)
def setMessageID(self, message_id): if (message_id > 0xFF) or (message_id < 0x00): raise MessageError('Could not set message ID ' \ '(out of range).') self.payload[1] = chr(message_id)
def setChannelNumber(self, number): if (number > 0xFF) or (number < 0x00): raise MessageError('Could not set channel number ' \ '(out of range).') self.payload[0] = chr(number)