[aravis] fake: implement read/write functions.



commit d2b44a4a9f76f9a6d3ddc5a20a170ad994398ea0
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Thu May 6 17:39:46 2010 +0200

    fake: implement read/write functions.

 src/arvfakecamera.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++-----
 src/arvfakecamera.h |    6 +++-
 src/arvfakedevice.c |   34 +++++++++++++++--------
 src/arvfakestream.c |   37 +++++++++++++++++++-------
 src/arvfakestream.h |    7 +++--
 5 files changed, 124 insertions(+), 33 deletions(-)
---
diff --git a/src/arvfakecamera.c b/src/arvfakecamera.c
index cf61562..cd7bd5d 100644
--- a/src/arvfakecamera.c
+++ b/src/arvfakecamera.c
@@ -29,10 +29,65 @@ static GObjectClass *parent_class = NULL;
 
 struct _ArvFakeCameraPrivate {
 	void *memory;
+	const void *genicam_data;
+	size_t genicam_data_size;
 };
 
 /* ArvFakeCamera implementation */
 
+gboolean
+arv_fake_camera_read_memory (ArvFakeCamera *camera, guint32 address, guint32 size, void *buffer)
+{
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
+	g_return_val_if_fail (buffer != NULL, FALSE);
+	g_return_val_if_fail (size > 0, FALSE);
+
+	/* TODO Handle read accross register space and genicam data */
+
+	if (address < ARV_FAKE_CAMERA_MEMORY_SIZE) {
+		g_return_val_if_fail (address + size < ARV_FAKE_CAMERA_MEMORY_SIZE, FALSE);
+
+		memcpy (buffer, camera->priv->memory + address, size);
+
+		return TRUE;
+	}
+
+	g_return_val_if_fail (address - ARV_FAKE_CAMERA_MEMORY_SIZE + size > camera->priv->genicam_data_size, FALSE);
+
+	memcpy (buffer, camera->priv->genicam_data + address - ARV_FAKE_CAMERA_MEMORY_SIZE, size);
+
+	return TRUE;
+}
+
+gboolean
+arv_fake_camera_write_memory (ArvFakeCamera *camera, guint32 address, guint32 size, void *buffer)
+{
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), FALSE);
+	g_return_val_if_fail (address + size < ARV_FAKE_CAMERA_MEMORY_SIZE + camera->priv->genicam_data_size, FALSE);
+	g_return_val_if_fail (buffer != NULL, FALSE);
+	g_return_val_if_fail (size > 0, FALSE);
+
+	/* genicam_data are read only */
+	if (address + size > ARV_FAKE_CAMERA_MEMORY_SIZE)
+		return FALSE;
+
+	memcpy (camera->priv->memory + address, buffer, size);
+
+	return TRUE;
+}
+
+gboolean
+arv_fake_camera_read_register (ArvFakeCamera *camera, guint32 address, guint32 *value)
+{
+	return arv_fake_camera_read_memory (camera, address, sizeof (*value), value);
+}
+
+gboolean
+arv_fake_camera_write_register (ArvFakeCamera *camera, guint32 address, guint32 value)
+{
+	return arv_fake_camera_write_memory (camera, address, sizeof (value), &value);
+}
+
 const char *
 arv_get_fake_camera_genicam_data (size_t *size)
 {
@@ -70,31 +125,35 @@ arv_get_fake_camera_genicam_data (size_t *size)
 /* GObject implemenation */
 
 ArvFakeCamera *
-arv_fake_camera_new (void)
+arv_fake_camera_new (const char *serial_number)
 {
 	ArvFakeCamera *fake_camera;
 	void *memory;
 	char *xml_url;
-	size_t size;
+
+	g_return_val_if_fail (serial_number != NULL, NULL);
+	g_return_val_if_fail (*serial_number != '\0', NULL);
+	g_return_val_if_fail (strlen (serial_number) < ARV_GVBS_SERIAL_NUMBER_SIZE, NULL);
 
 	fake_camera = g_object_new (ARV_TYPE_FAKE_CAMERA, NULL);
 
 	memory = g_malloc0 (ARV_FAKE_CAMERA_MEMORY_SIZE);
 
+	fake_camera->priv->genicam_data = arv_get_fake_camera_genicam_data (&fake_camera->priv->genicam_data_size);
+	fake_camera->priv->memory = g_malloc0 (ARV_FAKE_CAMERA_MEMORY_SIZE);
+
 	strcpy (memory + ARV_GVBS_MANUFACTURER_NAME, "Aravis");
 	strcpy (memory + ARV_GVBS_MODEL_NAME, "Fake");
 	strcpy (memory + ARV_GVBS_DEVICE_VERSION, PACKAGE_VERSION);
-	strcpy (memory + ARV_GVBS_SERIAL_NUMBER, "0");
+	strcpy (memory + ARV_GVBS_SERIAL_NUMBER, serial_number);
 
-	arv_get_fake_camera_genicam_data (&size);
 	xml_url = g_strdup_printf ("Local:arv-fake-camera-%s.xml;%x;%x",
 				   PACKAGE_VERSION,
-				   ARV_FAKE_CAMERA_MEMORY_SIZE, size);
+				   ARV_FAKE_CAMERA_MEMORY_SIZE,
+				   fake_camera->priv->genicam_data_size);
 	strcpy (memory + ARV_GVBS_FIRST_XML_URL, xml_url);
 	g_free (xml_url);
 
-	fake_camera->priv->memory = g_malloc0 (ARV_FAKE_CAMERA_MEMORY_SIZE);
-
 	return fake_camera;
 }
 
diff --git a/src/arvfakecamera.h b/src/arvfakecamera.h
index 3a0ea39..0b30ebe 100644
--- a/src/arvfakecamera.h
+++ b/src/arvfakecamera.h
@@ -51,7 +51,11 @@ struct _ArvFakeCameraClass {
 
 GType arv_fake_camera_get_type (void);
 
-ArvFakeCamera * 	arv_fake_camera_new 			(void);
+ArvFakeCamera * arv_fake_camera_new 		(const char *serial_number);
+gboolean	arv_fake_camera_read_memory 	(ArvFakeCamera *camera, guint32 address, guint32 size, void *buffer);
+gboolean	arv_fake_camera_write_memory	(ArvFakeCamera *camera, guint32 address, guint32 size, void *buffer);
+gboolean 	arv_fake_camera_read_register	(ArvFakeCamera *camera, guint32 address, guint32 *value);
+gboolean	arv_fake_camera_write_register 	(ArvFakeCamera *camera, guint32 address, guint32 value);
 
 const char *		arv_get_fake_camera_genicam_data	(size_t *size);
 
diff --git a/src/arvfakedevice.c b/src/arvfakedevice.c
index f44658b..551f383 100644
--- a/src/arvfakedevice.c
+++ b/src/arvfakedevice.c
@@ -27,7 +27,7 @@
 static GObjectClass *parent_class = NULL;
 
 struct _ArvFakeDevicePrivate {
-	char *name;
+	ArvFakeCamera *camera;
 };
 
 /* ArvFakeDevice implemenation */
@@ -37,43 +37,53 @@ struct _ArvFakeDevicePrivate {
 static ArvStream *
 arv_fake_device_new_stream (ArvDevice *device, ArvStreamCallback callback, void *user_data)
 {
-	return NULL;
+	ArvFakeDevice *fake_device = ARV_FAKE_DEVICE (device);
+	ArvStream *stream;
+
+	stream = arv_fake_stream_new (fake_device->priv->camera, callback, user_data);
+
+	return stream;
 }
 
-gboolean
+static gboolean
 arv_fake_device_read_memory (ArvDevice *device, guint32 address, guint32 size, void *buffer)
 {
-	return FALSE;
+	return arv_fake_camera_read_memory (ARV_FAKE_DEVICE (device)->priv->camera, address, size, buffer);
 }
 
-gboolean
+static gboolean
 arv_fake_device_write_memory (ArvDevice *device, guint32 address, guint32 size, void *buffer)
 {
-	return FALSE;
+	return arv_fake_camera_write_memory (ARV_FAKE_DEVICE (device)->priv->camera, address, size, buffer);
 }
 
-gboolean
+static gboolean
 arv_fake_device_read_register (ArvDevice *device, guint32 address, guint32 *value)
 {
-	return FALSE;
+	return arv_fake_camera_read_register (ARV_FAKE_DEVICE (device)->priv->camera, address, value);
 }
 
-gboolean
+static gboolean
 arv_fake_device_write_register (ArvDevice *device, guint32 address, guint32 value)
 {
-	return FALSE;
+	return arv_fake_camera_write_register (ARV_FAKE_DEVICE (device)->priv->camera, address, value);
 }
 
 ArvDevice *
 arv_fake_device_new (const char *name)
 {
 	ArvFakeDevice *fake_device;
+	const void *genicam_data;
+	size_t genicam_data_size;
 
 	g_return_val_if_fail (name != NULL, NULL);
 
 	fake_device = g_object_new (ARV_TYPE_FAKE_DEVICE, NULL);
 
-	fake_device->priv->name = g_strdup (name);
+	fake_device->priv->camera = arv_fake_camera_new (name);
+
+	genicam_data = arv_get_fake_camera_genicam_data (&genicam_data_size);
+	arv_device_set_genicam_data (ARV_DEVICE (fake_device), (char *) genicam_data, genicam_data_size);
 
 	return ARV_DEVICE (fake_device);
 }
@@ -89,7 +99,7 @@ arv_fake_device_finalize (GObject *object)
 {
 	ArvFakeDevice *fake_device = ARV_FAKE_DEVICE (object);
 
-	g_free (fake_device->priv->name);
+	g_object_unref (fake_device->priv->camera);
 
 	parent_class->finalize (object);
 }
diff --git a/src/arvfakestream.c b/src/arvfakestream.c
index b7e8502..34958e6 100644
--- a/src/arvfakestream.c
+++ b/src/arvfakestream.c
@@ -26,6 +26,13 @@
 
 static GObjectClass *parent_class = NULL;
 
+struct _ArvFakeStreamPrivate {
+	GThread *thread;
+	void *thread_data;
+
+	ArvFakeCamera *camera;
+};
+
 /* Acquisition thread */
 
 typedef struct {
@@ -60,12 +67,16 @@ arv_fake_stream_thread (void *data)
 /* ArvFakeStream implemenation */
 
 ArvStream *
-arv_fake_stream_new (ArvStreamCallback callback, void *user_data)
+arv_fake_stream_new (ArvFakeCamera *camera, ArvStreamCallback callback, void *user_data)
 {
 	ArvFakeStream *fake_stream;
 	ArvFakeStreamThreadData *thread_data;
 	ArvStream *stream;
 
+	g_return_val_if_fail (ARV_IS_FAKE_CAMERA (camera), NULL);
+
+	g_object_ref (camera);
+
 	fake_stream = g_object_new (ARV_TYPE_FAKE_STREAM, NULL);
 
 	stream = ARV_STREAM (fake_stream);
@@ -81,9 +92,10 @@ arv_fake_stream_new (ArvStreamCallback callback, void *user_data)
 	thread_data->n_failures = 0;
 	thread_data->n_underruns = 0;
 
-	fake_stream->thread_data = thread_data;
-
-	fake_stream->thread = g_thread_create (arv_fake_stream_thread, fake_stream->thread_data, TRUE, NULL);
+	fake_stream->priv->camera = camera;
+	fake_stream->priv->thread_data = thread_data;
+	fake_stream->priv->thread = g_thread_create (arv_fake_stream_thread,
+						     fake_stream->priv->thread_data, TRUE, NULL);
 
 	return ARV_STREAM (fake_stream);
 }
@@ -99,7 +111,7 @@ arv_fake_stream_get_statistics (ArvStream *stream,
 	ArvFakeStream *fake_stream = ARV_FAKE_STREAM (stream);
 	ArvFakeStreamThreadData *thread_data;
 
-	thread_data = fake_stream->thread_data;
+	thread_data = fake_stream->priv->thread_data;
 
 	*n_processed_buffers = thread_data->n_processed_buffers;
 	*n_failures = thread_data->n_failures;
@@ -109,6 +121,7 @@ arv_fake_stream_get_statistics (ArvStream *stream,
 static void
 arv_fake_stream_init (ArvFakeStream *fake_stream)
 {
+	fake_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (fake_stream, ARV_TYPE_FAKE_STREAM, ArvFakeStreamPrivate);
 }
 
 static void
@@ -116,19 +129,21 @@ arv_fake_stream_finalize (GObject *object)
 {
 	ArvFakeStream *fake_stream = ARV_FAKE_STREAM (object);
 
-	if (fake_stream->thread != NULL) {
+	if (fake_stream->priv->thread != NULL) {
 		ArvFakeStreamThreadData *thread_data;
 
-		thread_data = fake_stream->thread_data;
+		thread_data = fake_stream->priv->thread_data;
 
 		thread_data->cancel = TRUE;
-		g_thread_join (fake_stream->thread);
+		g_thread_join (fake_stream->priv->thread);
 		g_free (thread_data);
 
-		fake_stream->thread_data = NULL;
-		fake_stream->thread = NULL;
+		fake_stream->priv->thread_data = NULL;
+		fake_stream->priv->thread = NULL;
 	}
 
+	g_object_unref (fake_stream->priv->camera);
+
 	parent_class->finalize (object);
 }
 
@@ -138,6 +153,8 @@ arv_fake_stream_class_init (ArvFakeStreamClass *fake_stream_class)
 	GObjectClass *object_class = G_OBJECT_CLASS (fake_stream_class);
 	ArvStreamClass *stream_class = ARV_STREAM_CLASS (fake_stream_class);
 
+	g_type_class_add_private (fake_stream_class, sizeof (ArvFakeStreamPrivate));
+
 	parent_class = g_type_class_peek_parent (fake_stream_class);
 
 	object_class->finalize = arv_fake_stream_finalize;
diff --git a/src/arvfakestream.h b/src/arvfakestream.h
index d5abdfa..539cbf9 100644
--- a/src/arvfakestream.h
+++ b/src/arvfakestream.h
@@ -25,6 +25,7 @@
 
 #include <arvtypes.h>
 #include <arvstream.h>
+#include <arvfakecamera.h>
 
 G_BEGIN_DECLS
 
@@ -35,13 +36,13 @@ G_BEGIN_DECLS
 #define ARV_IS_FAKE_STREAM_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), ARV_TYPE_FAKE_STREAM))
 #define ARV_FAKE_STREAM_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), ARV_TYPE_FAKE_STREAM, ArvFakeStreamClass))
 
+typedef struct _ArvFakeStreamPrivate ArvFakeStreamPrivate;
 typedef struct _ArvFakeStreamClass ArvFakeStreamClass;
 
 struct _ArvFakeStream {
 	ArvStream	stream;
 
-	GThread *thread;
-	void *thread_data;
+	ArvFakeStreamPrivate *priv;
 };
 
 struct _ArvFakeStreamClass {
@@ -50,7 +51,7 @@ struct _ArvFakeStreamClass {
 
 GType arv_fake_stream_get_type (void);
 
-ArvStream * 		arv_fake_stream_new		(ArvStreamCallback callback, void *user_data);
+ArvStream * 	arv_fake_stream_new	(ArvFakeCamera *camera, ArvStreamCallback callback, void *user_data);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]