From 5175ad00e1d648a89efdf8d397b31bf84fd203c8 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Mon, 6 Jan 2003 22:09:16 +0000 Subject: 2003-01-07 Anders Carlsson * dbus/dbus-marshal.c: (_dbus_marshal_string), (_dbus_demarshal_string), (_dbus_marshal_test): * dbus/dbus-marshal.h: Document these functions. * dbus/dbus-message.c: (dbus_message_get_name), (dbus_message_append_int32), (dbus_message_append_uint32), (dbus_message_append_double), (dbus_message_append_string), (dbus_message_append_byte_array): * dbus/dbus-message.h: Add functions for adding message fields of different types. * dbus/dbus-protocol.h: Add the different types. --- ChangeLog | 17 ++++++++ dbus/dbus-marshal.c | 103 ++++++++++++++++++++++++++++++++++++++++++----- dbus/dbus-marshal.h | 64 ++++++++++++++--------------- dbus/dbus-message.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-message.h | 13 ++++++ dbus/dbus-protocol.h | 11 +++++ 6 files changed, 276 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index 21a18331..976e0d47 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2003-01-07 Anders Carlsson + + * dbus/dbus-marshal.c: (_dbus_marshal_string), + (_dbus_demarshal_string), (_dbus_marshal_test): + * dbus/dbus-marshal.h: + * dbus/dbus-message.c: (dbus_message_get_name), + Document these functions. + + (dbus_message_append_int32), (dbus_message_append_uint32), + (dbus_message_append_double), (dbus_message_append_string), + (dbus_message_append_byte_array): + * dbus/dbus-message.h: + Add functions for adding message fields of different types. + + * dbus/dbus-protocol.h: + Add the different types. + 2003-01-05 Havoc Pennington * bus/connection.c: implement routines for handling connections, diff --git a/dbus/dbus-marshal.c b/dbus/dbus-marshal.c index da726356..6e4f4143 100644 --- a/dbus/dbus-marshal.c +++ b/dbus/dbus-marshal.c @@ -124,6 +124,14 @@ unpack_int32 (int byte_order, * @{ */ +/** + * Marshals a double value. + * + * @param str the string to append the marshalled value to + * @param byte_order the byte order to use + * @param value the value + * @returns #TRUE on success + */ dbus_bool_t _dbus_marshal_double (DBusString *str, int byte_order, @@ -140,6 +148,14 @@ _dbus_marshal_double (DBusString *str, return _dbus_string_append_len (str, (const char *)&value, sizeof (double)); } +/** + * Marshals a 32 bit signed integer value. + * + * @param str the string to append the marshalled value to + * @param byte_order the byte order to use + * @param value the value + * @returns #TRUE on success + */ dbus_bool_t _dbus_marshal_int32 (DBusString *str, int byte_order, @@ -156,6 +172,14 @@ _dbus_marshal_int32 (DBusString *str, return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_int32_t)); } +/** + * Marshals a 32 bit unsigned integer value. + * + * @param str the string to append the marshalled value to + * @param byte_order the byte order to use + * @param value the value + * @returns #TRUE on success + */ dbus_bool_t _dbus_marshal_uint32 (DBusString *str, int byte_order, @@ -172,10 +196,18 @@ _dbus_marshal_uint32 (DBusString *str, return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t)); } +/** + * Marshals a UTF-8 string + * + * @param str the string to append the marshalled value to + * @param byte_order the byte order to use + * @param value the string + * @returns #TRUE on success + */ dbus_bool_t -_dbus_marshal_utf8_string (DBusString *str, - int byte_order, - const char *value) +_dbus_marshal_string (DBusString *str, + int byte_order, + const char *value) { int len; @@ -187,6 +219,15 @@ _dbus_marshal_utf8_string (DBusString *str, return _dbus_string_append_len (str, value, len + 1); } +/** + * Marshals a byte array + * + * @param str the string to append the marshalled value to + * @param byte_order the byte order to use + * @param value the byte array + * @param len the length of the byte array + * @returns #TRUE on success + */ dbus_bool_t _dbus_marshal_byte_array (DBusString *str, int byte_order, @@ -199,6 +240,15 @@ _dbus_marshal_byte_array (DBusString *str, return _dbus_string_append_len (str, value, len); } +/** + * Demarshals a double. + * + * @param str the string containing the data + * @param byte_order the byte order + * @param pos the position in the string + * @param new_pos the new position of the string + * @returns the demarshaled double. + */ double _dbus_demarshal_double (DBusString *str, int byte_order, @@ -223,6 +273,15 @@ _dbus_demarshal_double (DBusString *str, return retval; } +/** + * Demarshals a 32 bit signed integer. + * + * @param str the string containing the data + * @param byte_order the byte order + * @param pos the position in the string + * @param new_pos the new position of the string + * @returns the demarshaled integer. + */ dbus_int32_t _dbus_demarshal_int32 (DBusString *str, int byte_order, @@ -241,6 +300,15 @@ _dbus_demarshal_int32 (DBusString *str, return unpack_int32 (byte_order, buffer); } +/** + * Demarshals a 32 bit unsigned integer. + * + * @param str the string containing the data + * @param byte_order the byte order + * @param pos the position in the string + * @param new_pos the new position of the string + * @returns the demarshaled integer. + */ dbus_uint32_t _dbus_demarshal_uint32 (DBusString *str, int byte_order, @@ -259,11 +327,24 @@ _dbus_demarshal_uint32 (DBusString *str, return unpack_uint32 (byte_order, buffer); } +/** + * Demarshals an UTF-8 string. + * + * @todo Should we check the string to make sure + * that it's valid UTF-8, and maybe "fix" the string + * if it's broken? + * + * @param str the string containing the data + * @param byte_order the byte order + * @param pos the position in the string + * @param new_pos the new position of the string + * @returns the demarshaled string. + */ char * -_dbus_demarshal_utf8_string (DBusString *str, - int byte_order, - int pos, - int *new_pos) +_dbus_demarshal_string (DBusString *str, + int byte_order, + int pos, + int *new_pos) { int len; char *retval; @@ -417,16 +498,16 @@ _dbus_marshal_test (void) /* Marshal strings */ tmp1 = "This is the dbus test string"; - if (!_dbus_marshal_utf8_string (&str, DBUS_BIG_ENDIAN, tmp1)) + if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1)) _dbus_assert_not_reached ("could not marshal string"); - tmp2 = _dbus_demarshal_utf8_string (&str, DBUS_BIG_ENDIAN, pos, &pos); + tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos); _dbus_assert (strcmp (tmp1, tmp2) == 0); dbus_free (tmp2); tmp1 = "This is the dbus test string"; - if (!_dbus_marshal_utf8_string (&str, DBUS_LITTLE_ENDIAN, tmp1)) + if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1)) _dbus_assert_not_reached ("could not marshal string"); - tmp2 = _dbus_demarshal_utf8_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos); + tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos); _dbus_assert (strcmp (tmp1, tmp2) == 0); dbus_free (tmp2); diff --git a/dbus/dbus-marshal.h b/dbus/dbus-marshal.h index 88a3b1a9..6a9d752f 100644 --- a/dbus/dbus-marshal.h +++ b/dbus/dbus-marshal.h @@ -39,40 +39,40 @@ #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN #endif -dbus_bool_t _dbus_marshal_double (DBusString *str, - int byte_order, - double value); -dbus_bool_t _dbus_marshal_int32 (DBusString *str, - int byte_order, - dbus_int32_t value); -dbus_bool_t _dbus_marshal_uint32 (DBusString *str, - int byte_order, - dbus_uint32_t value); -dbus_bool_t _dbus_marshal_utf8_string (DBusString *str, - int byte_order, - const char *value); -dbus_bool_t _dbus_marshal_byte_array (DBusString *str, - int byte_order, - const unsigned char *value, - int len); +dbus_bool_t _dbus_marshal_double (DBusString *str, + int byte_order, + double value); +dbus_bool_t _dbus_marshal_int32 (DBusString *str, + int byte_order, + dbus_int32_t value); +dbus_bool_t _dbus_marshal_uint32 (DBusString *str, + int byte_order, + dbus_uint32_t value); +dbus_bool_t _dbus_marshal_string (DBusString *str, + int byte_order, + const char *value); +dbus_bool_t _dbus_marshal_byte_array (DBusString *str, + int byte_order, + const unsigned char *value, + int len); +double _dbus_demarshal_double (DBusString *str, + int byte_order, + int pos, + int *new_pos); +dbus_int32_t _dbus_demarshal_int32 (DBusString *str, + int byte_order, + int pos, + int *new_pos); +dbus_uint32_t _dbus_demarshal_uint32 (DBusString *str, + int byte_order, + int pos, + int *new_pos); +char * _dbus_demarshal_string (DBusString *str, + int byte_order, + int pos, + int *new_pos); -double _dbus_demarshal_double (DBusString *str, - int byte_order, - int pos, - int *new_pos); -dbus_int32_t _dbus_demarshal_int32 (DBusString *str, - int byte_order, - int pos, - int *new_pos); -dbus_uint32_t _dbus_demarshal_uint32 (DBusString *str, - int byte_order, - int pos, - int *new_pos); -char * _dbus_demarshal_utf8_string (DBusString *str, - int byte_order, - int pos, - int *new_pos); diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index aed943f7..b7f66644 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -22,6 +22,7 @@ */ #include "dbus-internals.h" +#include "dbus-marshal.h" #include "dbus-message.h" #include "dbus-message-internal.h" #include "dbus-memory.h" @@ -202,6 +203,7 @@ dbus_message_unref (DBusMessage *message) /** * Gets the name of a message. + * * @param message the message * @returns the message name (should not be freed) */ @@ -213,6 +215,115 @@ dbus_message_get_name (DBusMessage *message) return NULL; } +/** + * Appends a 32 bit signed integer to the message. + * + * @param message the message + * @param value the integer value + * @returns #TRUE on success + */ +dbus_bool_t +dbus_message_append_int32 (DBusMessage *message, + dbus_int32_t value) +{ + _dbus_assert (message != NULL); + _dbus_assert (!message->locked); + + if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32)) + return FALSE; + + return _dbus_marshal_int32 (&message->body, + DBUS_COMPILER_BYTE_ORDER, value); +} + +/** + * Appends a 32 bit unsigned integer to the message. + * + * @param message the message + * @param value the integer value + * @returns #TRUE on success + */ +dbus_bool_t +dbus_message_append_uint32 (DBusMessage *message, + dbus_uint32_t value) +{ + _dbus_assert (message != NULL); + _dbus_assert (!message->locked); + + if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32)) + return FALSE; + + return _dbus_marshal_uint32 (&message->body, + DBUS_COMPILER_BYTE_ORDER, value); +} + +/** + * Appends a double value to the message. + * + * @param message the message + * @param value the double value + * @returns #TRUE on success + */ +dbus_bool_t +dbus_message_append_double (DBusMessage *message, + double value) +{ + _dbus_assert (message != NULL); + _dbus_assert (!message->locked); + + if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32)) + return FALSE; + + return _dbus_marshal_double (&message->body, + DBUS_COMPILER_BYTE_ORDER, value); +} + +/** + * Appends a UTF-8 string to the message. + * + * @param message the message + * @param value the string + * @returns #TRUE on success + */ +dbus_bool_t +dbus_message_append_string (DBusMessage *message, + const char *value) +{ + _dbus_assert (message != NULL); + _dbus_assert (!message->locked); + _dbus_assert (value != NULL); + + if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UTF8_STRING)) + return FALSE; + + return _dbus_marshal_string (&message->body, + DBUS_COMPILER_BYTE_ORDER, value); +} + +/** + * Appends a byte array to the message. + * + * @param message the message + * @param value the array + * @param len the length of the array + * @returns #TRUE on success + */ +dbus_bool_t +dbus_message_append_byte_array (DBusMessage *message, + unsigned const char *value, + int len) +{ + _dbus_assert (message != NULL); + _dbus_assert (!message->locked); + _dbus_assert (value != NULL); + + if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY)) + return FALSE; + + return _dbus_marshal_byte_array (&message->body, + DBUS_COMPILER_BYTE_ORDER, value, len); +} + /** @} */ /** diff --git a/dbus/dbus-message.h b/dbus/dbus-message.h index ae8b84e1..f427780f 100644 --- a/dbus/dbus-message.h +++ b/dbus/dbus-message.h @@ -41,6 +41,19 @@ void dbus_message_unref (DBusMessage *message); const char* dbus_message_get_name (DBusMessage *message); +dbus_bool_t dbus_message_append_int32 (DBusMessage *message, + dbus_int32_t value); +dbus_bool_t dbus_message_append_uint32 (DBusMessage *message, + dbus_uint32_t value); +dbus_bool_t dbus_message_append_double (DBusMessage *message, + double value); +dbus_bool_t dbus_message_append_string (DBusMessage *message, + const char *value); +dbus_bool_t dbus_message_append_byte_array (DBusMessage *message, + unsigned const char *value, + int len); + + DBUS_END_DECLS; #endif /* DBUS_MESSAGE_H */ diff --git a/dbus/dbus-protocol.h b/dbus/dbus-protocol.h index 0e009241..e1a17b54 100644 --- a/dbus/dbus-protocol.h +++ b/dbus/dbus-protocol.h @@ -36,6 +36,17 @@ extern "C" { #define DBUS_LITTLE_ENDIAN ('l') /* LSB first */ #define DBUS_BIG_ENDIAN ('B') /* MSB first */ +/* Data types */ +#define DBUS_TYPE_INVALID 0 +#define DBUS_TYPE_INT32 1 +#define DBUS_TYPE_UINT32 2 +#define DBUS_TYPE_DOUBLE 3 +#define DBUS_TYPE_INT32_ARRAY 4 +#define DBUS_TYPE_UINT32_ARRAY 5 +#define DBUS_TYPE_DOUBLE_ARRAY 6 +#define DBUS_TYPE_BYTE_ARRAY 7 +#define DBUS_TYPE_UTF8_STRING 8 + #ifdef __cplusplus } #endif -- cgit