def encode(self, pad=0, header=True): """Encodes this cFS command to binary. If pad is specified, it indicates the maximum size of the encoded command in bytes. If the encoded command is less than pad, the remaining bytes are set to zero. The optional header argument controls whether or not a header is prepended to the encoded command and offers several levels of granularity. If False or None, no header is prepended. If True, a CCSDS header is computed based on the `APID` found in the command's corresponding :class:`CmdDefn`. To control the CCSDS sequence count (`seqcount`) only set header to an instance of :class:`CcsdsHeader` with the desired `seqcount`. For complete control over the header contents, set header to a bytearray. NOTE: This method sets the cFS command checksum byte to zero in accordance with the current cFS ground system tools and cFE source code. That is, as far as we can tell, the checksum is not used. """ checksum = 0 secondary = struct.pack('BB', checksum, self.defn.opcode) offset = len(secondary) size = max(offset + self.defn.argsize, pad) encoded = bytearray(size) encoded[0:offset] = secondary index = 0 for defn in self.defn.argdefns: if defn.fixed: value = defn.value else: value = self.args[index] index += 1 encoded[ defn.slice(offset) ] = defn.encode(value) if header is True: header = ccsds.CcsdsHeader() if isinstance(header, ccsds.CcsdsHeader): header.type = 1 header.apid = self.defn.ccsds.apid if self.defn.ccsds else 0 header.shflag = 1 header.seqflags = 0b11 print 'len(encoded) =', len(encoded) header.length = len(encoded) - 1 header = header._data if isinstance(header, bytearray): encoded = header + encoded return encoded
def testCcsdsHeaderDecode(): header = ccsds.CcsdsHeader([0x18, 0x2A, 0xC4, 0xD2, 0x16, 0x2E]) assert header.version == 0 assert header.type == 1 assert header.shflag == 1 assert header.apid == 42 assert header.raw.seqflags == 0b11 assert header.seqcount == 1234 assert header.length == 5678
def testCcsdsHeaderDefaults(): header = ccsds.CcsdsHeader() assert header.version == 0 assert header.type == 0 assert header.shflag == 0 assert header.apid == 0 assert header.raw.seqflags == 0b11 assert header.seqcount == 0 assert header.length == 0
def testCcsdsHeaderEncode(): header = ccsds.CcsdsHeader() header.version = 0 header.type = 1 header.shflag = 1 header.apid = 42 header.seqflags = 0b11 header.seqcount = 1234 header.length = 5678 assert header._data == bytearray([0x18, 0x2A, 0xC4, 0xD2, 0x16, 0x2E])