Example #1
0
    def __init__(self, stream, callback, proto_name, proto, **kwargs):

        self._logger = getLogger("comm.channel")

        transmitter = "arm"
        follow = False

        for arg in kwargs:
            if arg == "transmitter":
                if kwargs[arg] != None:
                    transmitter = kwargs[arg]
            elif arg == "follow":
                if kwargs[arg] != None:
                    follow = kwargs[arg]
            else:
                self._logger.warning("unexpected '%s' argument" %arg)

        self._stream = stream
        self._callback = callback
        self._proto_name = proto_name.lower()
        self._proto = proto

        for packet_name in self._proto['packets']:
            packet = self._proto['packets'][packet_name]
            if packet['transmitter'] == transmitter or packet['transmitter'] == 'both' or transmitter == 'both':
                self.__setattr__(packet_name, self._create_method(packet_name, packet))

        def recv(id, args):
            if id == -1:
                callback(None, args)
                return
            else:
                know_packet = False
                for packet_name in self._proto['packets']:
                    packet = self._proto['packets'][packet_name]
                    if packet['id'] == id:
                        know_packet = True
                        break
                if not know_packet:
                    self._logger.warning("unknow packet id %d" %id)
                    return
                if packet['transmitter'] == transmitter and packet['transmitter'] != 'both' and transmitter != 'both':
                    self._logger.info("ignoring %s message" %packet['transmitter'])
                    return
                if len(packet['args']) != len(args) \
                        and len(packet['args']) + 2 != len(args):
                    self._logger.warning("packet with id %d expected " \
                            "%d arguments, %d was given"
                            %(id, len(packet['args']), len(args)))
                    return
                arguments = dict(zip(packet['args'], args))
                if len(args) == len(packet['args']) + 2:
                    arguments['timestamp'] = args[-2]
                    arguments['microseconds'] = args[-1]
                self._callback(packet_name, arguments)

        thread = decode(stream, recv, follow)
Example #2
0
File: channel.py Project: alub/ATP
    def __init__(self, stream, callback, proto_name, proto, **kwargs):

        self._logger = getLogger("comm.channel")

        transmitter = "arm"
        follow = False

        for arg in kwargs:
            if arg == "transmitter":
                if kwargs[arg] != None:
                    transmitter = kwargs[arg]
            elif arg == "follow":
                if kwargs[arg] != None:
                    follow = kwargs[arg]
            else:
                self._logger.warning("unexpected '%s' argument" % arg)

        self._stream = stream
        self._callback = callback
        self._proto_name = proto_name.lower()
        self._proto = proto

        for packet_name in self._proto['packets']:
            packet = self._proto['packets'][packet_name]
            if packet['transmitter'] == transmitter or packet[
                    'transmitter'] == 'both' or transmitter == 'both':
                self.__setattr__(packet_name,
                                 self._create_method(packet_name, packet))

        def recv(id, args):
            if id == -1:
                callback(None, args)
                return
            else:
                know_packet = False
                for packet_name in self._proto['packets']:
                    packet = self._proto['packets'][packet_name]
                    if packet['id'] == id:
                        know_packet = True
                        break
                if not know_packet:
                    self._logger.warning("unknow packet id %d" % id)
                    return
                if packet['transmitter'] == transmitter and packet[
                        'transmitter'] != 'both' and transmitter != 'both':
                    self._logger.info("ignoring %s message" %
                                      packet['transmitter'])
                    return
                if len(packet['args']) != len(args) \
                        and len(packet['args']) + 2 != len(args):
                    self._logger.warning("packet with id %d expected " \
                            "%d arguments, %d was given"
                            %(id, len(packet['args']), len(args)))
                    return
                arguments = dict(zip(packet['args'], args))
                if len(args) == len(packet['args']) + 2:
                    arguments['timestamp'] = args[-2]
                    arguments['microseconds'] = args[-1]
                self._callback(packet_name, arguments)

        thread = decode(stream, recv, follow)
Example #3
0
    def __init__(self, stream, callback, **kwargs):

        self._logger = getLogger("comm.channel")

        transmitter = "arm"
        follow = False
        proto = None
        self._symmetrical = False

        for arg in kwargs:
            if arg == "transmitter":
                if kwargs[arg] != None:
                    transmitter = kwargs[arg]
            elif arg == "follow":
                if kwargs[arg] != None:
                    follow = kwargs[arg]
            elif arg == "proto":
                if kwargs[arg] != None:
                    proto = kwargs[arg].capitalize()
            elif arg == "symmetrical":
                if kwargs[arg] != None:
                    self._symmetrical = kwargs[arg]
            else:
                self._logger.warning("unexpected '%s' argument" %arg)

        self._stream = stream

        import protos
        self._protos = protos.load()
        self._proto = None
        self._proto_name = None

        if proto:
            try:
                self._proto = self._protos[proto]
            except KeyError:
                pass

            if not self._proto:
                raise Exception("Unknow protocol '%s', " \
                        "available protocols: %s" \
                        %(proto, ','.join(self._protos.keys())))

            for packet_name in self._proto['packets']:
                packet = self._proto['packets'][packet_name]
                if packet['transmitter'] == transmitter or packet['transmitter'] == 'both' or transmitter == 'both':
                    self.__setattr__(packet_name, self._create_method(packet_name, packet))
            self._proto_name = proto.lower()

        def recv(id, args):
            if id == -1:
                callback(None, args)
                return
            if self._proto == None:
                if id == 255:
                    if len(args) != 1:
                        self._logger.warning("invalid arguments count for id %d" %id)
                        return
                    board = args[0]
                    for proto_name in self._protos:
                        proto = self._protos[proto_name]
                        if proto["id"] == board:
                            self._proto = proto
                            self._proto_name = proto_name.lower()
                            self.logger.info("Loading proto '%s'" %proto_name)
                            recv(id, args)
                    if self._proto == None:
                        self._logger.warning("unknow board %d" %board)
                else:
                    self._logger.info("no protocol loaded, can't decode id %d" %id)
            else:
                know_packet = False
                for packet_name in self._proto['packets']:
                    packet = self._proto['packets'][packet_name]
                    if packet['id'] == id:
                        know_packet = True
                        break
                if not know_packet:
                    self._logger.warning("unknow packet id %d" %id)
                    return
                if packet['transmitter'] == transmitter and packet['transmitter'] != 'both' and transmitter != 'both':
                    self._logger.info("ignoring %s message" %packet['transmitter'])
                    return
                if len(packet['args']) != len(args) \
                        and len(packet['args']) + 2 != len(args):
                    self._logger.warning("packet with id %d expected " \
                            "%d arguments, %d was given"
                            %(id, len(packet['args']), len(args)))
                    return
                arguments = dict(zip(packet['args'], args))
                if len(args) == len(packet['args']) + 2:
                    arguments['timestamp'] = args[-2]
                    arguments['microseconds'] = args[-1]
                if self._symmetrical:
                    symmetrical(self._proto_name, packet_name, arguments)
                callback(packet_name, arguments)

        thread = decode(stream, recv, follow)
Example #4
0
#!/usr/bin/env python3

from atp import decode
import sys
import fcntl
import os
import argparse


def print_packet(id, args):
    print("[%d]" % id)
    for i in args:
        print("\t", i)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Decode ATP packets')
    parser.add_argument('-f',
                        '--follow',
                        dest='follow',
                        action='store_true',
                        help='output appended data as the file grows')
    args = parser.parse_args()
    decode(sys.stdin.buffer, print_packet, args.follow)
Example #5
0
#!/usr/bin/env python3

from atp import decode
import sys
import fcntl
import os
import argparse


def print_packet(id, args):
    print("[%d]" %id)
    for i in args:
        print("\t", i)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Decode ATP packets')
    parser.add_argument('-f', '--follow', dest='follow', action='store_true', help='output appended data as the file grows')
    args = parser.parse_args()
    decode(sys.stdin.buffer, print_packet, args.follow)