def header(self, value): #self._header need to contain header class instance if (type(value) == type(Header())): self._header = value else: raise Exception( 'Invalid Header : object is not an instance of Header class')
def __getattr__(self, name): if (name in self._names): return self.TypeItem(self._values[self._names.index(name)], self._p_values[self._names.index(name)], self._m_classes[self._names.index(name)]) else: raise Exception('Message Type {0} not defined'.format(name))
def destination(self, destination): if (not isinstance(destination, int) or destination < 0 or destination > 255): raise Exception('Invalid header destination set value: {0}'.format( destination)) else: self._destination = destination
def __getattr__(self, name): if (name in self._names): return self.DestinationItem( self._values[self._names.index(name)], self._p_values[self._names.index(name)]) else: raise Exception('Message Destination {0} not defined'.format(name))
def __getattr__(cls, name): if (name in cls._clist): return cls._clist.index(name) elif (name.endswith('_P')): return pack(_PACK_FORMAT, cls.__getattr__(name[:-2])) else: raise Exception('MSG_PLUG_TYPE {0} not existing'.format(name))
def data(self, value): if (self._header == None): raise Exception( 'Invalid Header : header has to be define ahead of data') else: #self._data need to contain data class instance #header type must match data type self.header.type = TYPE.get_value_from_class(type(value)) self._data = value
def header(self): if (self._header == None): raise Exception('Invalid Header : header not define') else: return self._header
def data(self): if (self._data == None): raise Exception('Invalid Data : data not define') else: return self._data
def timestamp(self, timestamp): if (timestamp < 0): raise Exception( 'Invalid header timestamp set value: {0}'.format(timestamp)) else: self._timestamp = timestamp
def timestamp(self): if (self._timestamp == None): raise Exception('Invalid header data : timestamp not define') else: return self._timestamp
def type(self): if (self._type == None): raise Exception('Invalid header data : type not define') else: return self._type
def source(self, source): if (not isinstance(source, int) or source < 0 or source > 255): raise Exception( 'Invalid header source set value: {0}'.format(source)) else: self._source = source
def source(self): if (self._source == None): raise Exception('Invalid header data : source not define') else: return self._source
def destination(self): if (self._destination == None): raise Exception('Invalid header data : destination not define') else: return self._destination
def _get_X_from_Y(self, value, X_table, Y_table): if (value in Y_table): return X_table[Y_table.index(value)] else: raise Exception( 'Message Type search item not defined'.format(value))
def value(self): if (self._value == None): raise Exception('Invalid value : value not define') else: return self._value
def type(self, type): if (not type in TYPE.values): raise Exception('Invalid header type set value: {0}'.format(type)) else: self._type = type
def _decode(self, msgbuf): ''' Decode a buffer as a MAVLink message ''' try: magic, mlen, seq, srcSystem, srcComponent, msgId = struct.unpack( 'cBBBBB', msgbuf[:6]) except struct.error as emsg: raise Exception('Unable to unpack MAVLink header: %s' % emsg) if ord(magic) != 254: raise Exception("invalid MAVLink prefix '%s'" % magic) if mlen != len(msgbuf) - 8: raise Exception( 'invalid MAVLink message length. Got %u expected %u, msgId=%u' % (len(msgbuf) - 8, mlen, msgId)) if not msgId in mavlink.mavlink_map: raise Exception('unknown MAVLink message ID %u' % msgId) # decode the payload type = mavlink.mavlink_map[msgId] fmt = type.format order_map = type.orders len_map = type.lengths crc_extra = type.crc_extra # decode the checksum try: crc, = struct.unpack('<H', msgbuf[-2:]) except struct.error as emsg: raise Exception('Unable to unpack MAVLink CRC: %s' % emsg) crcbuf = msgbuf[1:-2] crcbuf = crcbuf + struct.pack('B', crc_extra) crc2 = x25crc(crcbuf) if crc != crc2.crc: raise Exception( 'invalid MAVLink CRC in msgID %u 0x%04x should be 0x%04x' % (msgId, crc, crc2.crc)) try: t = struct.unpack(fmt, msgbuf[6:-2]) except struct.error as emsg: raise Exception( 'Unable to unpack MAVLink payload type=%s fmt=%s payloadLength=%u: %s' % (type, fmt, len(msgbuf[6:-2]), emsg)) tlist = list(t) # handle sorted fields if True: t = tlist[:] if sum(len_map) == len(len_map): # message has no arrays in it for i in range(0, len(tlist)): tlist[i] = t[order_map[i]] else: # message has some arrays tlist = [] for i in range(0, len(order_map)): order = order_map[i] L = len_map[order] tip = sum(len_map[:order]) field = t[tip] if L == 1 or isinstance(field, str): tlist.append(field) else: tlist.append(t[tip:(tip + L)]) # terminate any strings for i in range(0, len(tlist)): if isinstance(tlist[i], str): tlist[i] = str(mavlink.MAVString(tlist[i])) t = tuple(tlist) # construct the message object try: m = type(*t) except Exception as emsg: raise Exception( 'Unable to instantiate MAVLink message of type %s : %s' % (type, emsg)) m._msgbuf = msgbuf m._payload = msgbuf[6:-2] m._crc = crc m._header = mavlink.MAVLink_header(msgId, mlen, seq, srcSystem, srcComponent) self._value = m return m