From 7b18a105db637d1ea625bd41f35e52df41165fa3 Mon Sep 17 00:00:00 2001
From: Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
Date: Tue, 16 Feb 2016 15:55:06 +0100
Subject: Add readout and trigger API

---
 uca-net-client.c   | 29 ++++++++++++++++++++++-------
 uca-net-protocol.h | 10 ++++++++++
 uca-net-server.c   | 45 +++++++++++++++++++++++++++++++++++++--------
 ucad.c             | 24 ++++++++++++++++++++++++
 4 files changed, 93 insertions(+), 15 deletions(-)

diff --git a/uca-net-client.c b/uca-net-client.c
index c92eedd..471d039 100644
--- a/uca-net-client.c
+++ b/uca-net-client.c
@@ -134,22 +134,37 @@ uca_net_client_set_property (GSocketConnection *connection, const gchar *name, c
     return handle_default_reply (connection, UCA_NET_MESSAGE_SET_PROPERTY, error);
 }
 
-void
-uca_net_client_start_recording (GSocketConnection *connection, GError **error)
+static void
+default_handshake (GSocketConnection *connection, UcaNetMessageType type, GError **error)
 {
-    if (!send_default_message (connection, UCA_NET_MESSAGE_START_RECORDING, error))
+    if (!send_default_message (connection, type, error))
         return;
 
-    handle_default_reply (connection, UCA_NET_MESSAGE_START_RECORDING, error);
+    handle_default_reply (connection, type, error);
+}
+
+void
+uca_net_client_start_recording (GSocketConnection *connection, GError **error)
+{
+    default_handshake (connection, UCA_NET_MESSAGE_START_RECORDING, error);
 }
 
 void
 uca_net_client_stop_recording (GSocketConnection *connection, GError **error)
 {
-    if (!send_default_message (connection, UCA_NET_MESSAGE_STOP_RECORDING, error))
-        return;
+    default_handshake (connection, UCA_NET_MESSAGE_STOP_RECORDING, error);
+}
 
-    handle_default_reply (connection, UCA_NET_MESSAGE_STOP_RECORDING, error);
+void
+uca_net_client_start_readout (GSocketConnection *connection, GError **error)
+{
+    default_handshake (connection, UCA_NET_MESSAGE_START_READOUT, error);
+}
+
+void
+uca_net_client_stop_readout (GSocketConnection *connection, GError **error)
+{
+    default_handshake (connection, UCA_NET_MESSAGE_STOP_READOUT, error);
 }
 
 gboolean
diff --git a/uca-net-protocol.h b/uca-net-protocol.h
index 3df8c37..0b73c57 100644
--- a/uca-net-protocol.h
+++ b/uca-net-protocol.h
@@ -8,6 +8,9 @@ typedef enum {
     UCA_NET_MESSAGE_SET_PROPERTY,
     UCA_NET_MESSAGE_START_RECORDING,
     UCA_NET_MESSAGE_STOP_RECORDING,
+    UCA_NET_MESSAGE_START_READOUT,
+    UCA_NET_MESSAGE_STOP_READOUT,
+    UCA_NET_MESSAGE_TRIGGER,
     UCA_NET_MESSAGE_GRAB,
     UCA_NET_MESSAGE_CLOSE_CONNECTION,
 } UcaNetMessageType;
@@ -57,6 +60,9 @@ typedef struct {
     void     (*set_property)    (gpointer user_data, const gchar *name, const gchar *value, GError **error);
     void     (*start_recording) (gpointer user_data, GError **error);
     void     (*stop_recording)  (gpointer user_data, GError **error);
+    void     (*start_readout)   (gpointer user_data, GError **error);
+    void     (*stop_readout)    (gpointer user_data, GError **error);
+    void     (*trigger)         (gpointer user_data, GError **error);
     gboolean (*grab)            (gpointer data, gpointer user_data, GError **error);
 } UcaNetHandlers;
 
@@ -72,6 +78,10 @@ void        uca_net_client_start_recording (GSocketConnection   *connection,
                                             GError             **error);
 void        uca_net_client_stop_recording  (GSocketConnection   *connection,
                                             GError             **error);
+void        uca_net_client_start_readout   (GSocketConnection   *connection,
+                                            GError             **error);
+void        uca_net_client_stop_readout    (GSocketConnection   *connection,
+                                            GError             **error);
 gboolean    uca_net_client_grab            (GSocketConnection   *connection,
                                             gpointer             data,
                                             gsize                size,
diff --git a/uca-net-server.c b/uca-net-server.c
index f038cd6..853319c 100644
--- a/uca-net-server.c
+++ b/uca-net-server.c
@@ -51,25 +51,45 @@ uca_net_server_handle_set_property (GOutputStream *output, UcaNetMessageSetPrope
 }
 
 static void
-uca_net_server_handle_start_recording (GOutputStream *output, GError **stream_error)
+handle_default (GOutputStream *output, UcaNetMessageType type, 
+                void (*handler) (gpointer user_data, GError **error), GError **stream_error)
 {
-    UcaNetDefaultReply reply = { .type = UCA_NET_MESSAGE_START_RECORDING };
+    UcaNetDefaultReply reply = { .type = type };
     GError *error = NULL;
 
-    handlers.start_recording (handlers.user_data, &error);
+    handler (handlers.user_data, &error);
     prepare_error_reply (error, &reply.error);
     send_reply (output, &reply, sizeof (reply), stream_error);
 }
 
+static void
+uca_net_server_handle_start_recording (GOutputStream *output, GError **stream_error)
+{
+    handle_default (output, UCA_NET_MESSAGE_START_RECORDING, handlers.start_recording, stream_error);
+}
+
 static void
 uca_net_server_handle_stop_recording (GOutputStream *output, GError **stream_error)
 {
-    UcaNetDefaultReply reply = { .type = UCA_NET_MESSAGE_STOP_RECORDING };
-    GError *error = NULL;
+    handle_default (output, UCA_NET_MESSAGE_STOP_RECORDING, handlers.start_recording, stream_error);
+}
 
-    handlers.stop_recording (handlers.user_data, &error);
-    prepare_error_reply (error, &reply.error);
-    send_reply (output, &reply, sizeof (reply), stream_error);
+static void
+uca_net_server_handle_start_readout (GOutputStream *output, GError **stream_error)
+{
+    handle_default (output, UCA_NET_MESSAGE_START_READOUT, handlers.start_readout, stream_error);
+}
+
+static void
+uca_net_server_handle_stop_readout (GOutputStream *output, GError **stream_error)
+{
+    handle_default (output, UCA_NET_MESSAGE_STOP_READOUT, handlers.stop_readout, stream_error);
+}
+
+static void
+uca_net_server_handle_trigger (GOutputStream *output, GError **stream_error)
+{
+    handle_default (output, UCA_NET_MESSAGE_TRIGGER, handlers.trigger, stream_error);
 }
 
 static void
@@ -153,6 +173,15 @@ uca_net_server_handle (GSocketConnection *connection)
             case UCA_NET_MESSAGE_STOP_RECORDING:
                 uca_net_server_handle_stop_recording (output, &error);
                 break;
+            case UCA_NET_MESSAGE_START_READOUT:
+                uca_net_server_handle_start_readout (output, &error);
+                break;
+            case UCA_NET_MESSAGE_STOP_READOUT:
+                uca_net_server_handle_stop_readout (output, &error);
+                break;
+            case UCA_NET_MESSAGE_TRIGGER:
+                uca_net_server_handle_trigger (output, &error);
+                break;
             case UCA_NET_MESSAGE_GRAB:
                 uca_net_server_handle_grab (output, (UcaNetMessageGrabRequest *) buffer, &error);
                 break;
diff --git a/ucad.c b/ucad.c
index de3d79b..6223058 100644
--- a/ucad.c
+++ b/ucad.c
@@ -74,6 +74,9 @@ handle_get_property_request (gpointer user_data, const gchar *name, gchar *value
     camera = user_data;
     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (camera), name);
 
+    if (pspec == NULL)
+        return;
+
     g_value_init (&prop_value, g_type_is_a (pspec->value_type, G_TYPE_ENUM) ? G_TYPE_INT : pspec->value_type);
     g_object_get_property (G_OBJECT (camera), name, &prop_value);
 
@@ -114,6 +117,24 @@ handle_stop_recording_request (gpointer user_data, GError **error)
     uca_camera_stop_recording (UCA_CAMERA (user_data), error);
 }
 
+static void
+handle_start_readout_request (gpointer user_data, GError **error)
+{
+    uca_camera_start_recording (UCA_CAMERA (user_data), error);
+}
+
+static void
+handle_stop_readout_request (gpointer user_data, GError **error)
+{
+    uca_camera_stop_recording (UCA_CAMERA (user_data), error);
+}
+
+static void
+handle_trigger_request (gpointer user_data, GError **error)
+{
+    uca_camera_trigger (UCA_CAMERA (user_data), error);
+}
+
 static gboolean
 handle_grab_request (gpointer data, gpointer user_data, GError **error)
 {
@@ -133,6 +154,9 @@ run_callback (GSocketService *service, GSocketConnection *connection, GObject *s
         .set_property = handle_set_property_request,
         .start_recording = handle_start_recording_request,
         .stop_recording = handle_stop_recording_request,
+        .start_readout = handle_start_readout_request,
+        .stop_readout = handle_stop_readout_request,
+        .trigger = handle_trigger_request,
         .grab = handle_grab_request,
     };
 
-- 
cgit v1.2.3