def run(self):
     while self.keep_running:
         try:
             serialData = read_value_from_serial()
             if serialData is not None:
                 self.disQueue.put(serialData)
                 if isinstance(serialData, LogPhyRes):
                     if self.pcap_file != None:
                         self.pcap_file.write(PCAPFormatter.build_record_data(serialData.get_raw_data()))
                         self.pcap_file.flush()
                     if self.is_pipe_enabled:
                         if self.is_pipe_connected is False:
                             try:
                                 self.pipe = os.open(PIPE_FILENAME, os.O_WRONLY | os.O_NONBLOCK)
                                 os.write(self.pipe, PCAPFormatter.build_global_header_data())
                                 self.is_pipe_connected = True
                             except OSError as e:
                                 if e.errno == errno.ENXIO:
                                     print("Wireshark not listening yet ...")
                         else:
                             try:
                                 os.write(self.pipe, PCAPFormatter.build_record_data(serialData.get_raw_data()))
                             except OSError as e:
                                 if e.errno == errno.EPIPE:
                                     print("Wireshark stopped listening, waiting for reconnection ...")
                                     self.is_pipe_connected = False
         except Exception as inst:
             printError(inst)
예제 #2
0
 def run(self):
     while self.keep_running:
         try:
             serialData = read_value_from_serial()
             if serialData is not None:
                 self.disQueue.put(serialData)
                 if isinstance(serialData, LogPhyRes):
                     if self.pcap_file != None:
                         self.pcap_file.write(
                             PCAPFormatter.build_record_data(
                                 serialData.get_raw_data()))
                         self.pcap_file.flush()
                     if self.is_pipe_enabled:
                         if self.is_pipe_connected is False:
                             try:
                                 self.pipe = os.open(
                                     PIPE_FILENAME,
                                     os.O_WRONLY | os.O_NONBLOCK)
                                 os.write(
                                     self.pipe,
                                     PCAPFormatter.build_global_header_data(
                                     ))
                                 self.is_pipe_connected = True
                             except OSError as e:
                                 if e.errno == errno.ENXIO:
                                     print(
                                         "Wireshark not listening yet ...")
                         else:
                             try:
                                 os.write(
                                     self.pipe,
                                     PCAPFormatter.build_record_data(
                                         serialData.get_raw_data()))
                             except OSError as e:
                                 if e.errno == errno.EPIPE:
                                     print(
                                         "Wireshark stopped listening, waiting for reconnection ..."
                                     )
                                     self.is_pipe_connected = False
         except Exception as inst:
             printError(inst)
def main():
    global serial_port, settings
    keep_running = True
    # Some variables we need
    init()
    dateTime = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

    # Setup the console parser
    parser = argparse.ArgumentParser(description = "DASH7 logger for the OSS-7 stack. You can exit the logger using Ctrl-c, it takes some time.")
    parser.add_argument('serial', default="COM12", metavar="serial port", help="serial port (eg COM7 or /dev/ttyUSB0)", nargs='?')
    parser.add_argument('-b', '--baud' , default=115200, metavar="baudrate", type=int, help="set the baud rate (default: 9600)")
    parser.add_argument('-v', '--version', action='version', version='DASH7 Logger 0.5', help="show the current version")
    parser.add_argument('-f', '--file', metavar="file", help="write to a pcap file", nargs='?', default=None, const=dateTime)
    parser.add_argument('-p', '--pipe', help="stream live pcap data to a named pipe", action="store_true", default=False) # TODO print filename
    parser.add_argument('-l', '--list', help="Lists available serial ports", action="store_true", default=False)
    general_options = parser.add_argument_group('general logging')
    general_options.add_argument('--string', help="Disable string logs", action="store_false", default=True)
    general_options.add_argument('--data', help="Disable data logs", action="store_false", default=True)
    general_options.add_argument('--trace', help="Disable trace logs", action="store_false", default=True)
    stack_options = parser.add_argument_group('stack logging')
    stack_options.add_argument('--phy', help="Disable logs for phy", action="store_false", default=True)
    stack_options.add_argument('--dll', help="Disable logs for dll", action="store_false", default=True)
    stack_options.add_argument('--mac', help="Disable logs for mac", action="store_false", default=True)
    stack_options.add_argument('--nwl', help="Disable logs for nwl", action="store_false", default=True)
    stack_options.add_argument('--trans', help="Disable logs for trans", action="store_false", default=True)
    stack_options.add_argument('--fwk', help="Disable logs for fwk", action="store_false", default=True)
    stack_options.add_argument('--stack', help="Disable all stack logs", action="store_false", default=True)
    special_options = parser.add_argument_group('special logging')
    special_options.add_argument('--dllres', help="Disable DLL RES logs", action="store_false", default=True)
    special_options.add_argument('--phyres', help="Disable PHY RES Logs", action="store_false", default=True)
    special_options.add_argument('--display', help="Format the data of PHY RES", choices=['hex', 'bin', 'txt', 'dec'], default='hex')
    settings = vars(parser.parse_args())

    # We only want to list the serial ports, then exit
    if settings["list"]:
        list_serial_ports()
        sys.exit()

    # Setup the serial port
    if settings["serial"] is None:
        printError("You didn't specify a serial port!")
        sys.exit()

    serial_port = serial.Serial(settings['serial'], settings['baud'])
    empty_serial_buffer()

    # Array containing all the threads
    threads = []
    # Only write a file if we have a file defined
    pcap_file = None
    if settings["file"] != None:
        # TODO check if file already exists
        pcap_file = open(settings["file"], 'w')
        pcap_file.write(PCAPFormatter.build_global_header_data())
        pcap_file.flush()

    if settings["pipe"]:
        if not os.path.exists(PIPE_FILENAME):
            os.mkfifo(PIPE_FILENAME)

    threads.append(parse_d7(pcap_file, settings["pipe"], displayQueue))
    threads.append(display_d7(displayQueue))

    try:
        for t in threads:
            t.start()
    except Exception as inst:
        printError("Error unable to start thread")
        printError(inst)

    while keep_running:
        try:
            # Sleep a very short time, we are just waiting for a keyboard intterupt really
            time.sleep(0.0001)
        except KeyboardInterrupt:
            print("\nCtrl-c received! Sending Kill to the threads...")
            for t in threads:
                t.keep_running = False
            keep_running = False

    print("The logger is stopping, please wait")
    sys.exit()
예제 #4
0
def main():
    global serial_port, settings
    keep_running = True
    # Some variables we need
    init()
    dateTime = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

    # Setup the console parser
    parser = argparse.ArgumentParser(
        description=
        "DASH7 logger for the OSS-7 stack. You can exit the logger using Ctrl-c, it takes some time."
    )
    parser.add_argument('serial',
                        default="COM12",
                        metavar="serial port",
                        help="serial port (eg COM7 or /dev/ttyUSB0)",
                        nargs='?')
    parser.add_argument('-b',
                        '--baud',
                        default=115200,
                        metavar="baudrate",
                        type=int,
                        help="set the baud rate (default: 9600)")
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='DASH7 Logger 0.5',
                        help="show the current version")
    parser.add_argument('-f',
                        '--file',
                        metavar="file",
                        help="write to a pcap file",
                        nargs='?',
                        default=None,
                        const=dateTime)
    parser.add_argument('-p',
                        '--pipe',
                        help="stream live pcap data to a named pipe",
                        action="store_true",
                        default=False)  # TODO print filename
    parser.add_argument('-l',
                        '--list',
                        help="Lists available serial ports",
                        action="store_true",
                        default=False)
    general_options = parser.add_argument_group('general logging')
    general_options.add_argument('--string',
                                 help="Disable string logs",
                                 action="store_false",
                                 default=True)
    general_options.add_argument('--data',
                                 help="Disable data logs",
                                 action="store_false",
                                 default=True)
    general_options.add_argument('--trace',
                                 help="Disable trace logs",
                                 action="store_false",
                                 default=True)
    stack_options = parser.add_argument_group('stack logging')
    stack_options.add_argument('--phy',
                               help="Disable logs for phy",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--dll',
                               help="Disable logs for dll",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--mac',
                               help="Disable logs for mac",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--nwl',
                               help="Disable logs for nwl",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--trans',
                               help="Disable logs for trans",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--fwk',
                               help="Disable logs for fwk",
                               action="store_false",
                               default=True)
    stack_options.add_argument('--stack',
                               help="Disable all stack logs",
                               action="store_false",
                               default=True)
    special_options = parser.add_argument_group('special logging')
    special_options.add_argument('--dllres',
                                 help="Disable DLL RES logs",
                                 action="store_false",
                                 default=True)
    special_options.add_argument('--phyres',
                                 help="Disable PHY RES Logs",
                                 action="store_false",
                                 default=True)
    special_options.add_argument('--display',
                                 help="Format the data of PHY RES",
                                 choices=['hex', 'bin', 'txt', 'dec'],
                                 default='hex')
    settings = vars(parser.parse_args())

    # We only want to list the serial ports, then exit
    if settings["list"]:
        list_serial_ports()
        sys.exit()

    # Setup the serial port
    if settings["serial"] is None:
        printError("You didn't specify a serial port!")
        sys.exit()

    serial_port = serial.Serial(settings['serial'], settings['baud'])
    empty_serial_buffer()

    # Array containing all the threads
    threads = []
    # Only write a file if we have a file defined
    pcap_file = None
    if settings["file"] != None:
        # TODO check if file already exists
        pcap_file = open(settings["file"], 'w')
        pcap_file.write(PCAPFormatter.build_global_header_data())
        pcap_file.flush()

    if settings["pipe"]:
        if not os.path.exists(PIPE_FILENAME):
            os.mkfifo(PIPE_FILENAME)

    threads.append(parse_d7(pcap_file, settings["pipe"], displayQueue))
    threads.append(display_d7(displayQueue))

    try:
        for t in threads:
            t.start()
    except Exception as inst:
        printError("Error unable to start thread")
        printError(inst)

    while keep_running:
        try:
            # Sleep a very short time, we are just waiting for a keyboard intterupt really
            time.sleep(0.0001)
        except KeyboardInterrupt:
            print("\nCtrl-c received! Sending Kill to the threads...")
            for t in threads:
                t.keep_running = False
            keep_running = False

    print("The logger is stopping, please wait")
    sys.exit()