os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-utils.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     2 #include "test-utils.h"
     3 
     4 typedef struct
     5 {
     6   DBusLoop *loop;
     7   DBusConnection *connection;
     8 
     9 } CData;
    10 
    11 static dbus_bool_t
    12 connection_watch_callback (DBusWatch     *watch,
    13                            unsigned int   condition,
    14                            void          *data)
    15 {
    16   return dbus_watch_handle (watch, condition);
    17 }
    18 
    19 static dbus_bool_t
    20 add_watch (DBusWatch *watch,
    21 	   void      *data)
    22 {
    23   CData *cd = data;
    24 
    25   return _dbus_loop_add_watch (cd->loop,
    26                                watch,
    27                                connection_watch_callback,
    28                                cd, NULL);
    29 }
    30 
    31 static void
    32 remove_watch (DBusWatch *watch,
    33 	      void      *data)
    34 {
    35   CData *cd = data;
    36   
    37   _dbus_loop_remove_watch (cd->loop,
    38                            watch, connection_watch_callback, cd);  
    39 }
    40 
    41 static void
    42 connection_timeout_callback (DBusTimeout   *timeout,
    43                              void          *data)
    44 {
    45   /* Can return FALSE on OOM but we just let it fire again later */
    46   dbus_timeout_handle (timeout);
    47 }
    48 
    49 static dbus_bool_t
    50 add_timeout (DBusTimeout *timeout,
    51 	     void        *data)
    52 {
    53   CData *cd = data;
    54 
    55   return _dbus_loop_add_timeout (cd->loop,
    56                                  timeout, connection_timeout_callback, cd, NULL);
    57 }
    58 
    59 static void
    60 remove_timeout (DBusTimeout *timeout,
    61 		void        *data)
    62 {
    63   CData *cd = data;
    64 
    65   _dbus_loop_remove_timeout (cd->loop,
    66                              timeout, connection_timeout_callback, cd);
    67 }
    68 
    69 static void
    70 dispatch_status_function (DBusConnection    *connection,
    71                           DBusDispatchStatus new_status,
    72                           void              *data)
    73 {
    74   DBusLoop *loop = data;
    75   
    76   if (new_status != DBUS_DISPATCH_COMPLETE)
    77     {
    78       while (!_dbus_loop_queue_dispatch (loop, connection))
    79         _dbus_wait_for_memory ();
    80     }
    81 }
    82 
    83 static void
    84 cdata_free (void *data)
    85 {
    86   CData *cd = data;
    87 
    88   dbus_connection_unref (cd->connection);
    89   _dbus_loop_unref (cd->loop);
    90   
    91   dbus_free (cd);
    92 }
    93 
    94 static CData*
    95 cdata_new (DBusLoop       *loop,
    96            DBusConnection *connection)
    97 {
    98   CData *cd;
    99 
   100   cd = dbus_new0 (CData, 1);
   101   if (cd == NULL)
   102     return NULL;
   103 
   104   cd->loop = loop;
   105   cd->connection = connection;
   106 
   107   dbus_connection_ref (cd->connection);
   108   _dbus_loop_ref (cd->loop);
   109 
   110   return cd;
   111 }
   112 
   113 dbus_bool_t
   114 test_connection_setup (DBusLoop       *loop,
   115                        DBusConnection *connection)
   116 {
   117   CData *cd;
   118 
   119   cd = NULL;
   120   
   121   dbus_connection_set_dispatch_status_function (connection, dispatch_status_function,
   122                                                 loop, NULL);
   123   
   124   cd = cdata_new (loop, connection);
   125   if (cd == NULL)
   126     goto nomem;
   127 
   128   /* Because dbus-mainloop.c checks dbus_timeout_get_enabled(),
   129    * dbus_watch_get_enabled() directly, we don't have to provide
   130    * "toggled" callbacks.
   131    */
   132   
   133   if (!dbus_connection_set_watch_functions (connection,
   134                                             add_watch,
   135                                             remove_watch,
   136                                             NULL,
   137                                             cd, cdata_free))
   138     goto nomem;
   139 
   140 
   141   cd = cdata_new (loop, connection);
   142   if (cd == NULL)
   143     goto nomem;
   144   
   145   if (!dbus_connection_set_timeout_functions (connection,
   146                                               add_timeout,
   147                                               remove_timeout,
   148                                               NULL,
   149                                               cd, cdata_free))
   150     goto nomem;
   151 
   152   if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
   153     {
   154       if (!_dbus_loop_queue_dispatch (loop, connection))
   155         goto nomem;
   156     }
   157   
   158   return TRUE;
   159   
   160  nomem:
   161   if (cd)
   162     cdata_free (cd);
   163   
   164   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
   165   dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL);
   166   dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL);
   167   
   168   return FALSE;
   169 }
   170 
   171 void
   172 test_connection_shutdown (DBusLoop       *loop,
   173                           DBusConnection *connection)
   174 {
   175   if (!dbus_connection_set_watch_functions (connection,
   176                                             NULL,
   177                                             NULL,
   178                                             NULL,
   179                                             NULL, NULL))
   180     _dbus_assert_not_reached ("setting watch functions to NULL failed");
   181   
   182   if (!dbus_connection_set_timeout_functions (connection,
   183                                               NULL,
   184                                               NULL,
   185                                               NULL,
   186                                               NULL, NULL))
   187     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
   188 
   189   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
   190 }