def ser_deser_time_test(t_base, t_context, secs, usecs): """ Test serialization/deserialization of TimeType objects. This test function creates a time type object with the given parameters and then serializes it and deserializes it. Also prints it for visual inspection of the formatted output. Args: t_base (int): Time base for the new time type object t_context (int): Time context for the new time type object secs (int): Seconds value for the new time type object usecs (int): Seconds value for the new time type object should_err (int): True if error expected, else False Returns: True if test passed, False otherwise """ val = TimeType(t_base, t_context, secs, usecs) buff = val.serialize() val2 = TimeType() val2.deserialize(buff, 0) assert val2.timeBase.value == t_base assert val2.timeContext == t_context assert val2.seconds == secs assert val2.useconds == usecs
def decode_api(self, data): """ Decodes the given data and returns the result. This function allows for non-registered code to call the same decoding code as is used to parse data passed to the data_callback function. Args: data: (bytearray) Binary packetized telemetry data to decode Returns: Parsed version of the input data in the form of a PktData object or None if the data is not decodable """ ptr = 0 # Decode Pkt ID here... id_obj = U16Type() id_obj.deserialize(data, ptr) ptr += id_obj.getSize() pkt_id = id_obj.val # Decode time... pkt_time = TimeType() pkt_time.deserialize(data, ptr) ptr += pkt_time.getSize() if pkt_id not in self.__dict: # Don't crash if can't find pkt. Just notify and keep going print("Packet decode error: id %d not in dictionary. Time=%s" % (pkt_id, pkt_time.to_readable())) print("Full pkt = \n") for i in data: print("0x%02x" % ord(i)) return None # Retrieve the template instance for this channel pkt_temp = self.__dict[pkt_id] ch_temps = pkt_temp.get_ch_list() ch_data_objs = [] for ch_temp in ch_temps: val_obj = self.decode_ch_val(data, ptr, ch_temp) ptr += val_obj.getSize() ch_data_objs.append(ChData(val_obj, pkt_time, ch_temp)) return PktData(ch_data_objs, pkt_time, pkt_temp)
def decode_api(self, data): ''' Decodes the given data and returns the result. This function allows for non-registered code to call the same decoding code as is used to parse data passed to the data_callback function. Args: data: Binary telemetry channel data to decode Returns: Parsed version of the channel telemetry data in the form of a ChData object or None if the data is not decodable ''' ptr = 0 # Decode Ch ID here... id_obj = U32Type() id_obj.deserialize(data, ptr) ptr += id_obj.getSize() ch_id = id_obj.val # Decode time... ch_time = TimeType() ch_time.deserialize(data, ptr) ptr += ch_time.getSize() if ch_id in self.__dict: # Retrieve the template instance for this channel ch_temp = self.__dict[ch_id] val_obj = self.decode_ch_val(data, ptr, ch_temp) return ChData(val_obj, ch_time, ch_temp) else: print("Channel decode error: id %d not in dictionary" % ch_id) return None