[gnome-color-manager] trivial: add gcm_buffer_xxx functions to do the BE and LE buffer conversions



commit 65de4a733949fb35696c8fdcec012e597f1f209f
Author: Richard Hughes <richard hughsie com>
Date:   Thu Jul 22 11:37:42 2010 +0100

    trivial: add gcm_buffer_xxx functions to do the BE and LE buffer conversions

 libcolor-glib/Makefile.am       |    2 +
 libcolor-glib/gcm-buffer.c      |   93 +++++++++++++++++++++++++++++++++++++++
 libcolor-glib/gcm-buffer.h      |   41 +++++++++++++++++
 libcolor-glib/gcm-self-test.c   |   18 ++++++++
 libcolor-glib/gcm-sensor-huey.c |   17 ++-----
 5 files changed, 158 insertions(+), 13 deletions(-)
---
diff --git a/libcolor-glib/Makefile.am b/libcolor-glib/Makefile.am
index e5dd770..597b2cb 100644
--- a/libcolor-glib/Makefile.am
+++ b/libcolor-glib/Makefile.am
@@ -72,6 +72,8 @@ libcolor_glib_la_SOURCES =					\
 	gcm-edid.h						\
 	gcm-dmi.c						\
 	gcm-dmi.h						\
+	gcm-buffer.c						\
+	gcm-buffer.h						\
 	gcm-profile.c						\
 	gcm-profile.h						\
 	gcm-clut.c						\
diff --git a/libcolor-glib/gcm-buffer.c b/libcolor-glib/gcm-buffer.c
new file mode 100644
index 0000000..0b330c4
--- /dev/null
+++ b/libcolor-glib/gcm-buffer.c
@@ -0,0 +1,93 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU Lesser General Public License Version 2.1
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+/**
+ * SECTION:gcm-buffer
+ * @short_description: buffer functionality
+ *
+ * Functions to manipulate buffer.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include <gcm-buffer.h>
+
+/**
+ * gcm_buffer_write_uint16_be:
+ * @data: the writable data buffer
+ * @value: the value to write
+ *
+ * Writes a big endian value into a data buffer.
+ * NOTE: No validation is done on the buffer to ensure it's big enough.
+ **/
+void
+gcm_buffer_write_uint16_be (guchar *buffer, guint16 value)
+{
+	buffer[0] = (value >> 8) & 0xff;
+	buffer[1] = (value >> 0) & 0xff;
+}
+
+/**
+ * gcm_buffer_write_uint16_le:
+ * @data: the writable data buffer
+ * @value: the value to write
+ *
+ * Writes a little endian value into a data buffer.
+ * NOTE: No validation is done on the buffer to ensure it's big enough.
+ **/
+void
+gcm_buffer_write_uint16_le (guchar *buffer, guint16 value)
+{
+	buffer[0] = (value >> 0) & 0xff;
+	buffer[1] = (value >> 8) & 0xff;
+}
+
+/**
+ * gcm_buffer_read_uint16_be:
+ * @data: the writable data buffer
+ *
+ * Reads a big endian value from a data buffer.
+ * NOTE: No validation is done on the buffer to ensure it's valid.
+ *
+ * Return value: the value to read.
+ **/
+guint16
+gcm_buffer_read_uint16_be (const guchar *buffer)
+{
+	return GUINT16_FROM_BE (*(guint16*)buffer);
+}
+
+/**
+ * gcm_buffer_read_uint16_le:
+ * @data: the writable data buffer
+ *
+ * Reads a big endian value from a data buffer.
+ * NOTE: No validation is done on the buffer to ensure it's valid.
+ *
+ * Return value: the value to read.
+ **/
+guint16
+gcm_buffer_read_uint16_le (const guchar *buffer)
+{
+	return GUINT16_FROM_LE (*(guint16*)buffer);
+}
diff --git a/libcolor-glib/gcm-buffer.h b/libcolor-glib/gcm-buffer.h
new file mode 100644
index 0000000..3de670b
--- /dev/null
+++ b/libcolor-glib/gcm-buffer.h
@@ -0,0 +1,41 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#if !defined (__LIBCOLOR_GLIB_H_INSIDE__) && !defined (LIBCOLOR_GLIB_COMPILATION)
+#error "Only <libcolor-glib.h> can be included directly."
+#endif
+
+#ifndef __GCM_BUFFER_H__
+#define __GCM_BUFFER_H__
+
+#define __GCM_BUFFER_H_INSIDE__
+
+guint16		 gcm_buffer_read_uint16_be		(const guchar		*buffer);
+guint16		 gcm_buffer_read_uint16_le		(const guchar		*buffer);
+void		 gcm_buffer_write_uint16_be		(guchar			*buffer,
+							 guint16		 value);
+void		 gcm_buffer_write_uint16_le		(guchar			*buffer,
+							 guint16		 value);
+
+#undef __GCM_BUFFER_H_INSIDE__
+
+#endif /* __GCM_BUFFER_H__ */
+
diff --git a/libcolor-glib/gcm-self-test.c b/libcolor-glib/gcm-self-test.c
index 107435d..f584d7c 100644
--- a/libcolor-glib/gcm-self-test.c
+++ b/libcolor-glib/gcm-self-test.c
@@ -38,6 +38,7 @@
 #include "gcm-dmi.h"
 #include "gcm-image.h"
 #include "gcm-usb.h"
+#include "gcm-buffer.h"
 
 static void
 gcm_test_common_func (void)
@@ -671,6 +672,22 @@ gcm_test_usb_func (void)
 	g_object_unref (usb);
 }
 
+static void
+gcm_test_buffer_func (void)
+{
+	guchar buffer[4];
+
+	gcm_buffer_write_uint16_be (buffer, 255);
+	g_assert_cmpint (buffer[0], ==, 0x00);
+	g_assert_cmpint (buffer[1], ==, 0xff);
+	g_assert_cmpint (gcm_buffer_read_uint16_be (buffer), ==, 255);
+
+	gcm_buffer_write_uint16_le (buffer, 8192);
+	g_assert_cmpint (buffer[0], ==, 0x00);
+	g_assert_cmpint (buffer[1], ==, 0x20);
+	g_assert_cmpint (gcm_buffer_read_uint16_le (buffer), ==, 8192);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -691,6 +708,7 @@ main (int argc, char **argv)
 	g_test_add_func ("/libcolor-glib/dmi", gcm_test_dmi_func);
 	g_test_add_func ("/libcolor-glib/profile_store", gcm_test_profile_store_func);
 	g_test_add_func ("/libcolor-glib/usb", gcm_test_usb_func);
+	g_test_add_func ("/libcolor-glib/buffer", gcm_test_buffer_func);
 	if (g_test_thorough ()) {
 		g_test_add_func ("/libcolor-glib/brightness", gcm_test_brightness_func);
 		g_test_add_func ("/libcolor-glib/image", gcm_test_image_func);
diff --git a/libcolor-glib/gcm-sensor-huey.c b/libcolor-glib/gcm-sensor-huey.c
index d785b70..263545f 100644
--- a/libcolor-glib/gcm-sensor-huey.c
+++ b/libcolor-glib/gcm-sensor-huey.c
@@ -33,6 +33,7 @@
 
 #include "egg-debug.h"
 
+#include "gcm-buffer.h"
 #include "gcm-usb.h"
 #include "gcm-common.h"
 #include "gcm-sensor-huey.h"
@@ -598,16 +599,6 @@ gcm_sensor_huey_set_leds (GcmSensor *sensor, guint8 value, GError **error)
 	return gcm_sensor_huey_send_data (sensor_huey, payload, 8, reply, 8, &reply_read, error);
 }
 
-/**
- * gcm_sensor_huey_write_uint16:
- **/
-static void
-gcm_sensor_huey_write_uint16 (guchar *data, guint16 value)
-{
-	data[0] = (value >> 8) & 0xff;
-	data[1] = (value >> 0) & 0xff;
-}
-
 typedef struct {
 	guint16	R;
 	guint16	G;
@@ -626,9 +617,9 @@ gcm_sensor_huey_sample_for_threshold (GcmSensorHuey *sensor_huey, GcmSensorHueyM
 	gsize reply_read;
 
 	/* these are 16 bit gain values */
-	gcm_sensor_huey_write_uint16 (request + 1, threshold->R);
-	gcm_sensor_huey_write_uint16 (request + 3, threshold->G);
-	gcm_sensor_huey_write_uint16 (request + 5, threshold->B);
+	gcm_buffer_write_uint16_be (request + 1, threshold->R);
+	gcm_buffer_write_uint16_be (request + 3, threshold->G);
+	gcm_buffer_write_uint16_be (request + 5, threshold->B);
 
 	/* measure, and get red */
 	ret = gcm_sensor_huey_send_data (sensor_huey, request, 8, reply, 8, &reply_read, error);



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