os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-utils.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-utils.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#include "test-utils.h"
     1.6 +
     1.7 +typedef struct
     1.8 +{
     1.9 +  DBusLoop *loop;
    1.10 +  DBusConnection *connection;
    1.11 +
    1.12 +} CData;
    1.13 +
    1.14 +static dbus_bool_t
    1.15 +connection_watch_callback (DBusWatch     *watch,
    1.16 +                           unsigned int   condition,
    1.17 +                           void          *data)
    1.18 +{
    1.19 +  return dbus_watch_handle (watch, condition);
    1.20 +}
    1.21 +
    1.22 +static dbus_bool_t
    1.23 +add_watch (DBusWatch *watch,
    1.24 +	   void      *data)
    1.25 +{
    1.26 +  CData *cd = data;
    1.27 +
    1.28 +  return _dbus_loop_add_watch (cd->loop,
    1.29 +                               watch,
    1.30 +                               connection_watch_callback,
    1.31 +                               cd, NULL);
    1.32 +}
    1.33 +
    1.34 +static void
    1.35 +remove_watch (DBusWatch *watch,
    1.36 +	      void      *data)
    1.37 +{
    1.38 +  CData *cd = data;
    1.39 +  
    1.40 +  _dbus_loop_remove_watch (cd->loop,
    1.41 +                           watch, connection_watch_callback, cd);  
    1.42 +}
    1.43 +
    1.44 +static void
    1.45 +connection_timeout_callback (DBusTimeout   *timeout,
    1.46 +                             void          *data)
    1.47 +{
    1.48 +  /* Can return FALSE on OOM but we just let it fire again later */
    1.49 +  dbus_timeout_handle (timeout);
    1.50 +}
    1.51 +
    1.52 +static dbus_bool_t
    1.53 +add_timeout (DBusTimeout *timeout,
    1.54 +	     void        *data)
    1.55 +{
    1.56 +  CData *cd = data;
    1.57 +
    1.58 +  return _dbus_loop_add_timeout (cd->loop,
    1.59 +                                 timeout, connection_timeout_callback, cd, NULL);
    1.60 +}
    1.61 +
    1.62 +static void
    1.63 +remove_timeout (DBusTimeout *timeout,
    1.64 +		void        *data)
    1.65 +{
    1.66 +  CData *cd = data;
    1.67 +
    1.68 +  _dbus_loop_remove_timeout (cd->loop,
    1.69 +                             timeout, connection_timeout_callback, cd);
    1.70 +}
    1.71 +
    1.72 +static void
    1.73 +dispatch_status_function (DBusConnection    *connection,
    1.74 +                          DBusDispatchStatus new_status,
    1.75 +                          void              *data)
    1.76 +{
    1.77 +  DBusLoop *loop = data;
    1.78 +  
    1.79 +  if (new_status != DBUS_DISPATCH_COMPLETE)
    1.80 +    {
    1.81 +      while (!_dbus_loop_queue_dispatch (loop, connection))
    1.82 +        _dbus_wait_for_memory ();
    1.83 +    }
    1.84 +}
    1.85 +
    1.86 +static void
    1.87 +cdata_free (void *data)
    1.88 +{
    1.89 +  CData *cd = data;
    1.90 +
    1.91 +  dbus_connection_unref (cd->connection);
    1.92 +  _dbus_loop_unref (cd->loop);
    1.93 +  
    1.94 +  dbus_free (cd);
    1.95 +}
    1.96 +
    1.97 +static CData*
    1.98 +cdata_new (DBusLoop       *loop,
    1.99 +           DBusConnection *connection)
   1.100 +{
   1.101 +  CData *cd;
   1.102 +
   1.103 +  cd = dbus_new0 (CData, 1);
   1.104 +  if (cd == NULL)
   1.105 +    return NULL;
   1.106 +
   1.107 +  cd->loop = loop;
   1.108 +  cd->connection = connection;
   1.109 +
   1.110 +  dbus_connection_ref (cd->connection);
   1.111 +  _dbus_loop_ref (cd->loop);
   1.112 +
   1.113 +  return cd;
   1.114 +}
   1.115 +
   1.116 +dbus_bool_t
   1.117 +test_connection_setup (DBusLoop       *loop,
   1.118 +                       DBusConnection *connection)
   1.119 +{
   1.120 +  CData *cd;
   1.121 +
   1.122 +  cd = NULL;
   1.123 +  
   1.124 +  dbus_connection_set_dispatch_status_function (connection, dispatch_status_function,
   1.125 +                                                loop, NULL);
   1.126 +  
   1.127 +  cd = cdata_new (loop, connection);
   1.128 +  if (cd == NULL)
   1.129 +    goto nomem;
   1.130 +
   1.131 +  /* Because dbus-mainloop.c checks dbus_timeout_get_enabled(),
   1.132 +   * dbus_watch_get_enabled() directly, we don't have to provide
   1.133 +   * "toggled" callbacks.
   1.134 +   */
   1.135 +  
   1.136 +  if (!dbus_connection_set_watch_functions (connection,
   1.137 +                                            add_watch,
   1.138 +                                            remove_watch,
   1.139 +                                            NULL,
   1.140 +                                            cd, cdata_free))
   1.141 +    goto nomem;
   1.142 +
   1.143 +
   1.144 +  cd = cdata_new (loop, connection);
   1.145 +  if (cd == NULL)
   1.146 +    goto nomem;
   1.147 +  
   1.148 +  if (!dbus_connection_set_timeout_functions (connection,
   1.149 +                                              add_timeout,
   1.150 +                                              remove_timeout,
   1.151 +                                              NULL,
   1.152 +                                              cd, cdata_free))
   1.153 +    goto nomem;
   1.154 +
   1.155 +  if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
   1.156 +    {
   1.157 +      if (!_dbus_loop_queue_dispatch (loop, connection))
   1.158 +        goto nomem;
   1.159 +    }
   1.160 +  
   1.161 +  return TRUE;
   1.162 +  
   1.163 + nomem:
   1.164 +  if (cd)
   1.165 +    cdata_free (cd);
   1.166 +  
   1.167 +  dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
   1.168 +  dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL);
   1.169 +  dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL);
   1.170 +  
   1.171 +  return FALSE;
   1.172 +}
   1.173 +
   1.174 +void
   1.175 +test_connection_shutdown (DBusLoop       *loop,
   1.176 +                          DBusConnection *connection)
   1.177 +{
   1.178 +  if (!dbus_connection_set_watch_functions (connection,
   1.179 +                                            NULL,
   1.180 +                                            NULL,
   1.181 +                                            NULL,
   1.182 +                                            NULL, NULL))
   1.183 +    _dbus_assert_not_reached ("setting watch functions to NULL failed");
   1.184 +  
   1.185 +  if (!dbus_connection_set_timeout_functions (connection,
   1.186 +                                              NULL,
   1.187 +                                              NULL,
   1.188 +                                              NULL,
   1.189 +                                              NULL, NULL))
   1.190 +    _dbus_assert_not_reached ("setting timeout functions to NULL failed");
   1.191 +
   1.192 +  dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
   1.193 +}