예제 #1
0
def test_timeout():
    # Create an agent that throws an exception when it receives
    # a payload command packet.
    a = Agent()
    a.bind_udp_sockets()
    a.service_handler["Payload Command"] = Agent.raise_exception

    # Set a timeout that is << delay.
    Agent.TIMEOUT = 0.005

    # Run agent.
    t = threading.Thread(target=Agent.run, args=(a,))
    t.daemon = True
    t.start()

    # Delay
    time.sleep(0.02)

    # Send a payload command packet -- SHUTDOWN
    p = Packet()
    p.service = Supernova.service_id("Payload Command")
    p.dest_node = Supernova.get_my_id()
    Send.send_to_self(p)   

    # Wait for and then assert that thread has exited.
    t.join(0.01)
    assert not t.is_alive()
예제 #2
0
def test_startup_and_shutdown():
    # Create an agent that throws an exception when it receives
    # a payload command packet.
    a = Agent()
    a.bind_udp_sockets()
    a.service_handler["Payload Command"] = Agent.raise_exception

    # Run agent.
    t = threading.Thread(target=Agent.run, args=(a,))
    t.daemon = True
    t.start()

    # Send an ACK packet
    p = Packet()
    p.service = Supernova.service_id("Payload Command")
    p.dest_node = Supernova.get_my_id()
    p.ack = 1
    Send.send_to_self(p)   

    # Wait for and then assert that thread has *not* exited.
    t.join(0.01)
    assert t.is_alive()

    # Send a payload command packet -- SHUTDOWN
    p = Packet()
    p.service = Supernova.service_id("Payload Command")
    p.dest_node = Supernova.get_my_id()
    Send.send_to_self(p)   

    # Wait for and then assert that thread has exited.
    t.join(0.01)
    assert not t.is_alive()
예제 #3
0
def test_socket_bind_error():
    # Block socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((Supernova.payload_ip(Supernova.get_my_id()),
               Supernova.service_recv_port("Payload Command", Supernova.get_my_id())) )

    a = Agent()
    a.bind_udp_sockets() # will fail gracefully
    a.close()

    sock.close()
예제 #4
0
def main():
    """ Main entry point for executable. """

    a = Agent()
    Agent.DEBUG = True

    cmd_handler = PayloadCommandHandler()

    # Register some callbacks
    a.service_handler["Payload Command"] = cmd_handler.dispatch;
    a.service_handler["Telemetry Packet"] = Agent.print_it;

    print("Binding UDP sockets")
    a.bind_udp_sockets()

    print("Waiting for bus")
    a.run()
예제 #5
0
class BbbSoftware:

    # Timeout after waiting for any network event
    MAIN_LOOP_TIMEOUT = 1.0 # seconds

    def __init__(self):
        self.agent = None
        self.payload_cmds = None

        # Event object to wake the flight control loop
        self.event = threading.Event()

        # Initialize components of state machine
        self.hardware = Hardware()

        # Initialize state
        self.state = State.INITIAL

        # Initialize stats
        self.stats = Stats()

    def on_telemetry(self, packet):
        # Decode as telemetry packet
        tp = TelemetryPacket(packet)
        tp.deserialize()
        # Update newest data
        self.hardware.telemetry = tp
        # Wake the main control thread
        self.event.set()

    def thread_agent(self):
        """
        This is the entry point for the thread that handles
        all communication with the Supernova bus.
        It communicates back to the main ConnOps loop via
        multithreaded Event objects.
        """
        while True:
            try:
                # Set up the command handlers
                self.agent = Agent()
                self.agent.service_handler["Telemetry Packet"] = self.on_telemetry

                # Run
                self.agent.bind_udp_sockets()
                self.agent.run() # should never exit

            except Exception as ex:
                # NOTE: It is an error to ever reach this line.
                # Catch and swallow all exceptions.
                1 + 1

            # NOTE: It is an error to ever reach this line.
            self.agent_errors = self.agent_errors + 1

    def main(self):
        # Launch the thread that communicates with the Supernova bus.
        agent_thread = threading.Thread(target=self.thread_agent)
        agent_thread.daemon = True
        agent_thread.start()

        # Run the flight state machine program
        while True:
            self.event.wait(BbbSoftware.MAIN_LOOP_TIMEOUT)
            self.state = Transitions.next(self.state)
예제 #6
0
class Tk1Main:

    DEBUG = False

    KERNEL_MOD_PATH = "../UVCStill/uvcstill.ko"

    def __init__(self):
        """ Constructor """

        self.cmds = self.create_payload_cmd_handler()
        self.agent = Agent()
        self.agent.service_handler["Payload Command"] = self.cmds.dispatch

        self.capture_proc = None


    def create_payload_cmd_handler(self):
        """
        Create the payload command handler.

        This establishes the mapping of our command IDs to the
        code that handles them.  Adding a new payload command
        requires linking to it here.
        """
        handler = PayloadCommandHandler()

        handler.handlers.update( {
            PayloadCommandId.ABORT_CAPTURE : self.do_abort_capture,
            PayloadCommandId.CAPTURE_360   : self.do_capture_360,
            PayloadCommandId.CAPTURE_180   : self.do_capture_180,
            PayloadCommandId.CAPTURE_CUSTOM: self.do_capture_custom,
            PayloadCommandId.CAMERA_POWER_ON  : self.do_cameras_on,
            PayloadCommandId.CAMERA_POWER_OFF : self.do_cameras_off,
            PayloadCommandId.RELOAD_KERNEL_MODULE : self.reload_kernel_module,
        } )

        if Tk1Main.DEBUG: PayloadCommandHandler.DEBUG = True

        return handler


    def main(self):
        """
        Start up the Pumpkin Supernova agent and wait for commands.
        """
        
        if Tk1Main.DEBUG: print("Binding UDP sockets")
        self.agent.bind_udp_sockets()
        if Tk1Main.DEBUG: print("Waiting for bus")
        self.agent.run()


    def do_abort_capture(self, packet):
        """
        Immediately terminate any camera captures that are in progress.
        """

        if self.capture_proc and not self.capture_proc.poll():
            if Tk1Main.DEBUG: print("Aborting capture")
            # Send a SIGTERM to capture process
            self.capture_proc.send_signal(signal.SIGTERM)
        else:
            if Tk1Main.DEBUG: print("No capture to abort")



    def do_capture_180(self, packet):
        """
        Capture a 180-degree sequence.
        """

        if (packet.data_len <= 8):
            # Ignore bad packets.
            return

        (num_frames, start_time) = struct.unpack("ll", packet.data)

        self.capture(4, num_frames, start_time)


    def do_capture_360(self, packet):
        """
        Capture a 360-degree sequence.
        """

        if (packet.data_len <= 8):
            # Ignore bad packets.
            return

        (num_frames, start_time) = struct.unpack("ll", packet.data)

        self.capture(8, num_frames, start_time)


    def do_capture_custom(self, packet):
        """
        Capture a custom sequence.
        """

        if (packet.data_len <= 14):
            # Ignore bad packets.
            return

        (num_cameras, num_frames,  start_time, width, height) 
             = struct.unpack("hhlll", packet.data)

        self.capture(num_cameras, num_frames, start_time, widht, height)