示例#1
0
def main():
    s = socket()
    s.connect(('192.168.1.97', 9761))
    output = Container(
                      source=64,
                      dest=1,
                      sequence=33,
                      command=0x10,
                      payload_10 = None)
    

    while True:
        print "-"*60
        print fecha()
        print "-"*60
        paquete = MaraFrame.build(output)
        #print paquete, type(paquete)
        s.send(str(paquete))
        data = s.recv(1024)
        try:
            data = MaraFrame.parse(data)
        except FieldError as e:
            print "Error al decodificar la trama", e
            continue
        MaraFrame.pretty_print(data, 
                               show_header=False, 
                               show_bcc=False)
        sleep(.8)
        output.sequence += 1
        if output.sequence > MAX_SEQ:
            output.sequence = MIN_SEQ
示例#2
0
def main():
    s = socket()
    s.connect(('192.168.1.97', 9761))
    output = Container(
                      source=64,
                      dest=1,
                      sequence=33,
                      command=0x10,
                      payload_10 = None)


    while True:
        print "-"*60
        print fecha()
        print "-"*60
        paquete = MaraFrame.build(output)
        #print paquete, type(paquete)
        s.send(str(paquete))
        data = s.recv(1024)
        try:
            data = MaraFrame.parse(data)
        except FieldError as e:
            print "Error al decodificar la trama", e
            continue
        MaraFrame.pretty_print(data,
                               show_header=False,
                               show_bcc=False)
        sleep(.8)
        output.sequence += 1
        if output.sequence > MAX_SEQ:
            output.sequence = MIN_SEQ
示例#3
0
    def buildFrame10(cls, dis=[], ais=[], svs=[], events=[], through_construct=True):
        data = copy(cls.BASE_CONTAINER)
        data.payload_10.dis = dis
        data.payload_10.candis = len(dis) * 2 + 1
        data.payload_10.ais = ais
        data.payload_10.canais = len(ais) * 2 + 1
        data.payload_10.event = events
        data.payload_10.canevs = (len(events) * 10) + 1
        data.payload_10.varsys = svs
        data.payload_10.canvarsys = len(svs) * 2 + 1

        if through_construct:
            build = MaraFrame.build(data)
            data = MaraFrame.parse(build)
        return data
示例#4
0
文件: utils.py 项目: D3f0/txscada
def get_structs_from_logs(logdir, count=0, frame_count=0, older_first=False,
                          filter_func=None):
    """Uses get_ascii_from_logs to get ascii frames and then it parses them
    using construct.
    Notice that parsing may fail and/or """
    from protocols.constructs import MaraFrame
    from protocols.constructs.structs import hexstr2buffer
    from construct import FieldError
    count = 0
    filters = build_filter_funcs(filter_func)
    for ascii_frame in get_ascii_from_logs(logdir,
                                           count=count,
                                           frame_count=0,  # Cutting at upper level
                                           older_first=older_first):
        buffer = hexstr2buffer(ascii_frame)
        try:
            container = MaraFrame.parse(buffer)
        except (ValueError, FieldError):
            continue
        if not filters(container):
            continue

        yield container
        count += 1
        if frame_count and count > frame_count:
            break
示例#5
0
    def test_send_peh_upon_connection(self):
        '''To test client protocol we isloate it from the ClientFactory'''
        with patch.object(datetime, 'datetime', Mock(wraps=datetime.datetime)) as patched:
            fixed_date = datetime.datetime(2014, 1, 1, 12, 0, 0)
            patched.now.return_value = fixed_date

            factory = ClientFactory()
            factory.comaster = self.comaster

            factory.protocol = MaraClientProtocol
            proto = factory.buildProtocol(('127.0.0.1', 0))
            proto.construct = MaraFrame

            # Disable unnesesary behaviour
            def stop():
                proto.stop()
                reactor.stop()
            proto.sendPoll = MagicMock(side_effect=stop)

            transport = proto_helpers.StringTransport()
            proto.makeConnection(transport)

            bytes_sent_to_device = transport.value()
            result = MaraFrame.parse(bytes_sent_to_device)
            self.assertEqual(result.dest, 0xFF)
            self.assertEqual(result.source, 2)

            # We don't need to check BCC since it's already coded into MaraFrame
            self.assertEqual(result.peh, fixed_date)

            reactor.run()
            # Shuld have stopped
            self.assertEqual(self.comaster.update_peh_timestamp.call_count, 1)
            self.assertEqual(self.comaster.update_peh_timestamp.call_args[0][0],
                             fixed_date)
示例#6
0
def get_structs_from_logs(logdir,
                          count=0,
                          frame_count=0,
                          older_first=False,
                          filter_func=None):
    """Uses get_ascii_from_logs to get ascii frames and then it parses them
    using construct.
    Notice that parsing may fail and/or """
    from protocols.constructs import MaraFrame
    from protocols.constructs.structs import hexstr2buffer
    from construct import FieldError
    count = 0
    filters = build_filter_funcs(filter_func)
    for ascii_frame in get_ascii_from_logs(
            logdir,
            count=count,
            frame_count=0,  # Cutting at upper level
            older_first=older_first):
        buffer = hexstr2buffer(ascii_frame)
        try:
            container = MaraFrame.parse(buffer)
        except (ValueError, FieldError):
            continue
        if not filters(container):
            continue

        yield container
        count += 1
        if frame_count and count > frame_count:
            break
示例#7
0
 def dataReceived(self, data):
     try:
         self.input = MaraFrame.parse(data)
         self.maraPackageReceived()
     except FieldError:
         # If the server has no data, it does not matter
         self.input = None
         self.logger.warn("Error de pareso: %s" % upperhexstr(data))
示例#8
0
文件: models.py 项目: D3f0/txscada
    def _process_str_frame(self, a_text_frame, **flags):
        """
        Creates crecords from frame into a COMaster entity tree.
        This **should** not be used in poll function. It's a helper for
        commandline for easy recovery of not saved frames.
        Accepts a frame per line.
        @return True on success, False otherwise
        """
        from protocols.constructs.structs import hexstr2buffer
        from protocols.constructs import MaraFrame

        buff = hexstr2buffer(a_text_frame)
        frame = MaraFrame.parse(buff)
        success = True
        try:
            self.process_frame(frame)
        except Exception:
            success = False
        return success
示例#9
0
 def setUp(self):
     buffer = hexstr2buffer(FRAME)
     payload = MaraFrame.parse(buffer).palyload_10
     ied = IEDFactory()