# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


import sys
import subprocess
import telnetlib
import logging
import distutils
import binascii
import re
    import Queue as queue
    import queue
from codecs import encode
from threading import Thread, Lock, Event
from struct import pack, unpack
from time import sleep
from os.path import abspath
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage
            return

        # mode halt | run | init
        reset_re = re.compile("type target_reset mode (\w+)")
        # Trace data (from TPIU) Hex-encoded.
        trace_re = re.compile("type target_trace data ([0-9a-f]+)")
        # State change
        state_re = re.compile("type target_state state (\S+)")
        # Generic event
        event_re = re.compile("type target_event event (\S+)")

        mreset = reset_re.match(str)
        mtrace = trace_re.match(str)
        mstate = state_re.match(str)
        mevent = event_re.match(str)

        if mreset:
            reset_type = mreset.group(1)
            self.log.debug("Got reset event type %s" % reset_type)
            if reset_type == "halt":
                avatar_msg = UpdateStateMessage(self._origin, TargetStates.STOPPED)
                self.avatar.fast_queue.put(avatar_msg)
        elif mtrace:
            # DOn't log anything here.  If we do, our IO will be exhausted by the sheer volume of trace packets.
            self.trace_queue.put(str)
        elif mstate:
            # Do a state update!
            state = mstate.group(1)
            if "halted" in state:
                self.log.debug("Target has halted")

# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


# Path: avatar2/protocols/openocd.py
import sys
import subprocess
import telnetlib
import logging
import distutils
import binascii
import re
    import Queue as queue
    import queue
from codecs import encode
from threading import Thread, Lock, Event
from struct import pack, unpack
from time import sleep
from os.path import abspath
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage

            return

        # mode halt | run | init
        reset_re = re.compile("type target_reset mode (\w+)")
        # Trace data (from TPIU) Hex-encoded.
        trace_re = re.compile("type target_trace data ([0-9a-f]+)")
        # State change
        state_re = re.compile("type target_state state (\S+)")
        # Generic event
        event_re = re.compile("type target_event event (\S+)")

        mreset = reset_re.match(str)
        mtrace = trace_re.match(str)
        mstate = state_re.match(str)
        mevent = event_re.match(str)

        if mreset:
            reset_type = mreset.group(1)
            self.log.debug("Got reset event type %s" % reset_type)
            if reset_type == "halt":
                avatar_msg = UpdateStateMessage(self._origin, TargetStates.STOPPED)
                self.avatar.fast_queue.put(avatar_msg)
        elif mtrace:
            # DOn't log anything here.  If we do, our IO will be exhausted by the sheer volume of trace packets.
            self.trace_queue.put(str)
        elif mstate:
            # Do a state update!
            state = mstate.group(1)
            if "halted" in state:
                self.log.debug("Target has halted")

                avatar_msg = UpdateStateMessage(self._origin, TargetStates.STOPPED)