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 test_telemetry_predicates(self): temp1 = ChTemplate(1, "Test Channel 1", "Predicate_Tester", I32Type()) temp2 = ChTemplate(2, "Test Channel 2", "Predicate_Tester", StringType()) update1 = ChData(I32Type(20), TimeType(), temp1) update2 = ChData(StringType("apple"), TimeType(), temp2) pred = predicates.telemetry_predicate() assert pred( update1 ), "If no fields are specified a ChData object should return True" assert pred( update2 ), "If no fields are specified a ChData object should return True" assert not pred( "diff object" ), "Anything that's not a ChData object should be False" assert not pred( 5), "Anything that's not a ChData object should be False" self.check_str(pred) id_pred = predicates.equal_to(1) pred = predicates.telemetry_predicate(id_pred=id_pred) assert pred(update1), "This predicate on the ID 1 should return True" assert not pred( update2), "This predicate on the ID 2 should return False" self.check_str(pred) val_pred = predicates.equal_to("apple") pred = predicates.telemetry_predicate(value_pred=val_pred) assert not pred( update1), "This predicate on the value 20 should return False" assert pred( update2 ), "This predicate on the value \"apple\" should return True" self.check_str(pred) time_pred = predicates.equal_to(0) pred = predicates.telemetry_predicate(time_pred=time_pred) assert pred(update1), "This predicate on the time 0 should return True" assert pred(update2), "This predicate on the time 0 should return True" self.check_str(pred) val_pred = predicates.within_range(10, 30) pred = predicates.telemetry_predicate(id_pred, val_pred, time_pred) assert pred( update1), "Specifying all fields should return True for update 1" assert not pred( update2), "Specifying all fields should return False for update 2" self.check_str(pred)
def get_oscillator_sequence(self, length): seq = [] for i in range(0, length): ch_temp = self.pipeline.dictionaries.channel_name["Oscillator"] val = int(round(10 * math.sin(math.radians(i)))) seq.append(ChData(I32Type(val), TimeType(), ch_temp)) return seq
def test_time_type(): """ Tests the TimeType serialization and deserialization """ TIME_SIZE = 11 in_no_err_list = [ (TimeBase["TB_NONE"].value, 1, 100, 999999), (TimeBase["TB_PROC_TIME"].value, 0xFF, 1234567, 2952), (TimeBase["TB_WORKSTATION_TIME"].value, 8, 1529430215, 12), (TimeBase["TB_SC_TIME"].value, 231, 1344230277, 123456), (TimeBase["TB_FPGA_TIME"].value, 78, 10395, 24556), (TimeBase["TB_DONT_CARE"].value, 0xB3, 12390819, 12356), ] in_err_list = [ (10, 58, 15345, 0), (TimeBase["TB_NONE"].value, 1, 3, -1), (TimeBase["TB_WORKSTATION_TIME"].value, 1, 700000, 1234567), ] val = TimeType() size = val.getSize() assert size == TIME_SIZE for (t_base, t_context, secs, usecs) in in_no_err_list: ser_deser_time_test(t_base, t_context, secs, usecs) for (t_base, t_context, secs, usecs) in in_err_list: with pytest.raises(TypeRangeException): ser_deser_time_test(t_base, t_context, secs, usecs)
def get_range(self, length): temp1 = ChTemplate(1, "Test Channel 1", "Chrono_Hist_Tester", I32Type()) chList = [] ts0 = TimeType() for item in range(length): tsi = ts0 + time.time() chList.append(ChData(I32Type(item), tsi, temp1)) return chList
def setUp(self): for t in self.threads: if t.isAlive(): t.join() self.threads.clear() count = len(self.case_list) self.api.start_test_case(self._testMethodName, count) self.case_list.append(1) self.tHistory = TestHistory() self.t0 = TimeType()
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 test_pkt_encoder(): """ Tests the encoding of the packet encoder """ config = ConfigManager() config.set("types", "msg_len", "U16") enc = PktEncoder() enc_config = PktEncoder(config) ch_temp_1 = ChTemplate(101, "test_ch", "test_comp", U32Type()) ch_temp_2 = ChTemplate(102, "test_ch2", "test_comp2", U8Type()) ch_temp_3 = ChTemplate(103, "test_ch3", "test_comp3", U16Type()) pkt_temp = PktTemplate(64, "test_pkt", [ch_temp_1, ch_temp_2, ch_temp_3]) time_obj = TimeType(2, 0, 1533758629, 123456) ch_obj_1 = ChData(U32Type(1356), time_obj, ch_temp_1) ch_obj_2 = ChData(U8Type(143), time_obj, ch_temp_2) ch_obj_3 = ChData(U16Type(1509), time_obj, ch_temp_3) pkt_obj = PktData([ch_obj_1, ch_obj_2, ch_obj_3], time_obj, pkt_temp) desc_bin = b"\x00\x00\x00\x04" id_bin = b"\x00\x40" time_bin = b"\x00\x02\x00\x5b\x6b\x4c\xa5\x00\x01\xe2\x40" ch_bin = b"\x00\x00\x05\x4c\x8F\x05\xe5" long_len_bin = b"\x00\x00\x00\x18" short_len_bin = b"\x00\x18" reg_expected = long_len_bin + desc_bin + id_bin + time_bin + ch_bin config_expected = short_len_bin + desc_bin + id_bin + time_bin + ch_bin reg_output = enc.encode_api(pkt_obj) assert (reg_output == reg_expected ), "FAIL: expected regular output to be %s, but found %s" % ( list(reg_expected), list(reg_output), ) config_output = enc_config.encode_api(pkt_obj) assert (config_output == config_expected ), "FAIL: expected configured output to be %s, but found %s" % ( list(config_expected), list(config_output), )
def __init__(self, cmd_args, cmd_temp, cmd_time=None): """ Constructor. Args: cmd_args: The arguments for the event. Should match the types of the arguments in the cmd_temp object. Should be a tuple. cmd_temp: Command Template instance for this command (this provides the opcode and argument types are stored) cmd_time: The time the event should occur. This is for sequences. Should be a TimeType object with time base=TB_DONT_CARE Returns: An initialized CmdData object """ super().__init__() self.id = cmd_temp.get_id() self.template = cmd_temp self.arg_vals = cmd_args self.args = [deepcopy(typ) for (_, _, typ) in self.template.arguments] self.arg_names = [name for (name, _, _) in self.template.arguments] if cmd_time: self.time = cmd_time else: self.time = TimeType(TimeBase["TB_DONT_CARE"].value) errors = [] for val, typ in zip(self.arg_vals, self.args): try: self.convert_arg_value(val, typ) errors.append("") except Exception as exc: errors.append(str(exc)) # If any errors occur, then raise a aggregated error if [error for error in errors if error != ""]: raise CommandArgumentsException(errors)
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
def setUp(self): self.t0 = TimeType(0, 0, 0, 0) self.t1 = TimeType(0, 0, 1, 0) self.t15 = TimeType(0, 0, 1, 500000)
def get_counter_sequence(self, length): seq = [] for i in range(0, length): ch_temp = self.pipeline.dictionaries.channel_name["Counter"] seq.append(ChData(U32Type(i), TimeType(), ch_temp)) return seq
def test_event_predicates(self): args1_def = [("name", "string", StringType()), ("age", "int", I32Type())] temp1 = EventTemplate( 1, "Test Msg 1", "Predicate Tester", args1_def, EventSeverity.ACTIVITY_LO, "", ) args1 = (StringType("John"), I32Type(35)) msg1 = EventData(args1, TimeType(), temp1) args2_def = [ ("description", "string", StringType()), ("count", "int", I32Type()), ] temp2 = EventTemplate( 2, "Test Msg 2", "Predicate Tester", args2_def, EventSeverity.ACTIVITY_HI, "", ) args2 = (StringType("Dozen"), I32Type(12)) msg2 = EventData(args2, TimeType(), temp2) pred = predicates.event_predicate() assert pred( msg1 ), "If no fields are specified an EventData object should return True" assert pred( msg2 ), "If no fields are specified an EventData object should return True" assert not pred( "diff object" ), "Anything that's not an EventData object should be False" assert not pred(5), "Anything that's not a EventData object should be False" self.check_str(pred) id_pred = predicates.equal_to(1) pred = predicates.event_predicate(id_pred=id_pred) assert pred(msg1), "This predicate on the ID 1 should return True" assert not pred(msg2), "This predicate on the ID 2 should return False" self.check_str(pred) args_pred = predicates.args_predicate([None, None]) pred = predicates.event_predicate(args_pred=args_pred) assert pred( msg1 ), "This predicate should return True, as it expects an event with 2 args" assert pred( msg2 ), "This predicate should return True, as it expects an event with 2 args" self.check_str(pred) args_pred = predicates.args_predicate(["John", 35]) pred = predicates.event_predicate(args_pred=args_pred) assert pred( msg1 ), "This predicate should return True as msg1 has args (str John, int32 35)" assert not pred( msg2 ), "This predicate should return False as msg2 has args (str Dozen, int32 12)" self.check_str(pred) severity_pred = predicates.equal_to(EventSeverity.ACTIVITY_LO) pred = predicates.event_predicate(severity_pred=severity_pred) assert severity_pred(msg1.get_severity()) assert pred( msg1 ), "This predicate should return True as msg1 has an ACTIVITY_LO severity" assert not pred( msg2 ), "This predicate should return False as msg2 has an ACTIVITY_HI severity" self.check_str(pred) time_pred = predicates.equal_to(0) pred = predicates.event_predicate(time_pred=time_pred) assert pred(msg1), "This predicate on the time 0 should return True" assert pred(msg2), "This predicate on the time 0 should return True" self.check_str(pred) pred = predicates.event_predicate(id_pred, args_pred, severity_pred, time_pred) assert pred(msg1), "Specifying all fields should return True for msg1" assert not pred(msg2), "Specifying all fields should return False for msg2" self.check_str(pred)
def test_event_encoder(): """ Tests the encoding of the event encoder """ config = ConfigManager() config.set("types", "msg_len", "U16") enc = EventEncoder() enc_config = EventEncoder(config) temp = EventTemplate( 101, "test_ch", "test_comp", [("a1", "a1", U32Type()), ("a2", "a2", U32Type())], EventSeverity["DIAGNOSTIC"], "%d %d", ) time_obj = TimeType(2, 0, 1533758629, 123456) event_obj = EventData((U32Type(42), U32Type(10)), time_obj, temp) desc_bin = b"\x00\x00\x00\x02" id_bin = b"\x00\x00\x00\x65" time_bin = b"\x00\x02\x00\x5b\x6b\x4c\xa5\x00\x01\xe2\x40" arg_bin = b"\x00\x00\x00\x2a\x00\x00\x00\x0a" long_len_bin = b"\x00\x00\x00\x1b" short_len_bin = b"\x00\x1b" reg_expected = long_len_bin + desc_bin + id_bin + time_bin + arg_bin config_expected = short_len_bin + desc_bin + id_bin + time_bin + arg_bin reg_output = enc.encode_api(event_obj) assert (reg_output == reg_expected ), "FAIL: expected regular output to be %s, but found %s" % ( list(reg_expected), list(reg_output), ) config_output = enc_config.encode_api(event_obj) assert (config_output == config_expected ), "FAIL: expected configured output to be %s, but found %s" % ( list(config_expected), list(config_output), ) temp = EventTemplate( 102, "test_ch2", "test_comp2", [("a1", "a1", U8Type()), ("a2", "a2", U16Type())], EventSeverity["DIAGNOSTIC"], "%d %d", ) time_obj = TimeType(2, 0, 1533758628, 123457) event_obj = EventData((U8Type(128), U16Type(40)), time_obj, temp) desc_bin = b"\x00\x00\x00\x02" id_bin = b"\x00\x00\x00\x66" time_bin = b"\x00\x02\x00\x5b\x6b\x4c\xa4\x00\x01\xe2\x41" arg_bin = b"\x80\x00\x28" long_len_bin = b"\x00\x00\x00\x16" short_len_bin = b"\x00\x16" reg_expected = long_len_bin + desc_bin + id_bin + time_bin + arg_bin config_expected = short_len_bin + desc_bin + id_bin + time_bin + arg_bin reg_output = enc.encode_api(event_obj) assert (reg_output == reg_expected ), "FAIL: expected regular output to be %s, but found %s" % ( list(reg_expected), list(reg_output), ) config_output = enc_config.encode_api(event_obj) assert (config_output == config_expected ), "FAIL: expected configured output to be %s, but found %s" % ( list(config_expected), list(config_output), )
def test_ch_encoder(): """ Tests the encoding of the channel encoder """ config = ConfigManager() config.set("types", "msg_len", "U16") enc = ChEncoder() enc_config = ChEncoder(config) temp = ChTemplate(101, "test_ch", "test_comp", U32Type()) time_obj = TimeType(2, 0, 1533758629, 123456) ch_obj = ChData(U32Type(42), time_obj, temp) desc_bin = b"\x00\x00\x00\x01" id_bin = b"\x00\x00\x00\x65" time_bin = b"\x00\x02\x00\x5b\x6b\x4c\xa5\x00\x01\xe2\x40" val_bin = b"\x00\x00\x00\x2a" long_len_bin = b"\x00\x00\x00\x17" short_len_bin = b"\x00\x17" reg_expected = long_len_bin + desc_bin + id_bin + time_bin + val_bin config_expected = short_len_bin + desc_bin + id_bin + time_bin + val_bin reg_output = enc.encode_api(ch_obj) assert (reg_output == reg_expected ), "FAIL: expected regular output to be %s, but found %s" % ( list(reg_expected), list(reg_output), ) config_output = enc_config.encode_api(ch_obj) assert (config_output == config_expected ), "FAIL: expected configured output to be %s, but found %s" % ( list(config_expected), list(config_output), ) temp = ChTemplate(102, "test_ch2", "test_comp2", U16Type()) time_obj = TimeType(2, 0, 1533758628, 123457) ch_obj = ChData(U16Type(40), time_obj, temp) desc_bin = b"\x00\x00\x00\x01" id_bin = b"\x00\x00\x00\x66" time_bin = b"\x00\x02\x00\x5b\x6b\x4c\xa4\x00\x01\xe2\x41" val_bin = b"\x00\x28" long_len_bin = b"\x00\x00\x00\x15" short_len_bin = b"\x00\x15" reg_expected = long_len_bin + desc_bin + id_bin + time_bin + val_bin config_expected = short_len_bin + desc_bin + id_bin + time_bin + val_bin reg_output = enc.encode_api(ch_obj) assert (reg_output == reg_expected ), "FAIL: expected regular output to be %s, but found %s" % ( list(reg_expected), list(reg_output), ) config_output = enc_config.encode_api(ch_obj) assert (config_output == config_expected ), "FAIL: expected configured output to be %s, but found %s" % ( list(config_expected), list(config_output), )
def get_severity_event(self, severity="DIAGNOSTIC"): name = "Severity" + severity temp = self.pipeline.dictionaries.event_name[name] event = EventData(tuple(), TimeType(), temp) return event
binary_data = (len_bin + desc_bin + id_bin + time_bin + val_bin) return binary_data if __name__ == "__main__": # Unit Tests config = ConfigManager() config.set('types', 'msg_len', 'U16') enc = ChEncoder() enc_config = ChEncoder("GUI", config) temp = ChTemplate(101, "test_ch", "test_comp", U32Type()) time_obj = TimeType(2, 0, 1533758629, 123456) ch_obj = ChData(U32Type(42), time_obj, temp) desc_bin = "\x00\x00\x00\x01" id_bin = "\x00\x00\x00\x65" time_bin = "\x00\x02\x00\x5b\x6b\x4c\xa5\x00\x01\xe2\x40" val_bin = "\x00\x00\x00\x2a" long_len_bin = "\x00\x00\x00\x17" short_len_bin = "\x00\x17" reg_expected = (long_len_bin + desc_bin + id_bin + time_bin + val_bin) config_expected = (short_len_bin + desc_bin + id_bin + time_bin + val_bin) reg_output = enc.encode_api(ch_obj)
def __init__(self): self.command_count = 0 self.t0 = TimeType() StandardPipeline.__init__(self)