os/ossrv/ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-service.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0
     2
#include "test-utils.h"
sl@0
     3
sl@0
     4
static DBusLoop *loop;
sl@0
     5
static dbus_bool_t already_quit = FALSE;
sl@0
     6
static dbus_bool_t hello_from_self_reply_received = FALSE;
sl@0
     7
sl@0
     8
static void
sl@0
     9
quit (void)
sl@0
    10
{
sl@0
    11
  if (!already_quit)
sl@0
    12
    {
sl@0
    13
      _dbus_loop_quit (loop);
sl@0
    14
      already_quit = TRUE;
sl@0
    15
    }
sl@0
    16
}
sl@0
    17
sl@0
    18
static void
sl@0
    19
die (const char *message)
sl@0
    20
{
sl@0
    21
  fprintf (stderr, "*** test-service: %s", message);
sl@0
    22
  exit (1);
sl@0
    23
}
sl@0
    24
sl@0
    25
static void
sl@0
    26
check_hello_from_self_reply (DBusPendingCall *pcall, 
sl@0
    27
                             void *user_data)
sl@0
    28
{
sl@0
    29
  DBusMessage *reply;
sl@0
    30
  DBusMessage *echo_message, *echo_reply = NULL;
sl@0
    31
  DBusError error;
sl@0
    32
  DBusConnection *connection;
sl@0
    33
  
sl@0
    34
  int type;
sl@0
    35
  
sl@0
    36
  dbus_error_init (&error);
sl@0
    37
 
sl@0
    38
  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
sl@0
    39
  if (connection == NULL)
sl@0
    40
    {
sl@0
    41
      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
sl@0
    42
               error.message);
sl@0
    43
      dbus_error_free (&error);
sl@0
    44
      die("no memory");
sl@0
    45
    }
sl@0
    46
sl@0
    47
  
sl@0
    48
  echo_message = (DBusMessage *)user_data;
sl@0
    49
    
sl@0
    50
  reply = dbus_pending_call_steal_reply (pcall);
sl@0
    51
    
sl@0
    52
  type = dbus_message_get_type (reply);
sl@0
    53
    
sl@0
    54
  if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
    55
    {
sl@0
    56
      const char *s;
sl@0
    57
      printf ("Reply from HelloFromSelf received\n");
sl@0
    58
     
sl@0
    59
      if (!dbus_message_get_args (echo_message,
sl@0
    60
                              &error,
sl@0
    61
                              DBUS_TYPE_STRING, &s,
sl@0
    62
                              DBUS_TYPE_INVALID))
sl@0
    63
        {
sl@0
    64
            echo_reply = dbus_message_new_error (echo_message,
sl@0
    65
                                      error.name,
sl@0
    66
                                      error.message);
sl@0
    67
sl@0
    68
            if (echo_reply == NULL)
sl@0
    69
              die ("No memory\n");
sl@0
    70
sl@0
    71
        } 
sl@0
    72
      else
sl@0
    73
        {  
sl@0
    74
          echo_reply = dbus_message_new_method_return (echo_message);
sl@0
    75
          if (echo_reply == NULL)
sl@0
    76
            die ("No memory\n");
sl@0
    77
  
sl@0
    78
          if (!dbus_message_append_args (echo_reply,
sl@0
    79
                                 DBUS_TYPE_STRING, &s,
sl@0
    80
                                 DBUS_TYPE_INVALID))
sl@0
    81
            die ("No memory");
sl@0
    82
        }
sl@0
    83
        
sl@0
    84
      if (!dbus_connection_send (connection, echo_reply, NULL))
sl@0
    85
        die ("No memory\n");
sl@0
    86
      
sl@0
    87
      dbus_message_unref (echo_reply);
sl@0
    88
    }
sl@0
    89
  else if (type == DBUS_MESSAGE_TYPE_ERROR)
sl@0
    90
    {
sl@0
    91
      dbus_set_error_from_message (&error, reply);
sl@0
    92
      printf ("Error type in reply: %s\n", error.message);
sl@0
    93
sl@0
    94
      if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
sl@0
    95
        {
sl@0
    96
            echo_reply = dbus_message_new_error (echo_reply,
sl@0
    97
                                      error.name,
sl@0
    98
                                      error.message);
sl@0
    99
sl@0
   100
            if (echo_reply == NULL)
sl@0
   101
              die ("No memory\n");
sl@0
   102
sl@0
   103
            if (!dbus_connection_send (connection, echo_reply, NULL))
sl@0
   104
              die ("No memory\n");
sl@0
   105
sl@0
   106
            dbus_message_unref (echo_reply);
sl@0
   107
        }
sl@0
   108
      dbus_error_free (&error);
sl@0
   109
    }
sl@0
   110
  else
sl@0
   111
     _dbus_assert_not_reached ("Unexpected message received\n");
sl@0
   112
sl@0
   113
  hello_from_self_reply_received = TRUE;
sl@0
   114
  
sl@0
   115
  dbus_message_unref (reply);
sl@0
   116
  dbus_message_unref (echo_message);
sl@0
   117
  dbus_pending_call_unref (pcall);
sl@0
   118
  dbus_connection_unref (connection);
sl@0
   119
}
sl@0
   120
sl@0
   121
static DBusHandlerResult
sl@0
   122
handle_run_hello_from_self (DBusConnection     *connection,
sl@0
   123
                                               DBusMessage        *message)
sl@0
   124
{
sl@0
   125
  DBusError error;
sl@0
   126
  DBusMessage *reply, *self_message;
sl@0
   127
  DBusPendingCall *pcall;
sl@0
   128
  char *s;
sl@0
   129
sl@0
   130
  _dbus_verbose ("sending reply to Echo method\n");
sl@0
   131
  
sl@0
   132
  dbus_error_init (&error);
sl@0
   133
  
sl@0
   134
  if (!dbus_message_get_args (message,
sl@0
   135
                              &error,
sl@0
   136
                              DBUS_TYPE_STRING, &s,
sl@0
   137
                              DBUS_TYPE_INVALID))
sl@0
   138
    {
sl@0
   139
      reply = dbus_message_new_error (message,
sl@0
   140
                                      error.name,
sl@0
   141
                                      error.message);
sl@0
   142
sl@0
   143
      if (reply == NULL)
sl@0
   144
        die ("No memory\n");
sl@0
   145
sl@0
   146
      if (!dbus_connection_send (connection, reply, NULL))
sl@0
   147
        die ("No memory\n");
sl@0
   148
sl@0
   149
      dbus_message_unref (reply);
sl@0
   150
sl@0
   151
      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sl@0
   152
    }
sl@0
   153
    printf ("Sending HelloFromSelf\n");
sl@0
   154
sl@0
   155
 _dbus_verbose ("*** Sending message to self\n");
sl@0
   156
 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
sl@0
   157
                                          "/org/freedesktop/TestSuite",
sl@0
   158
                                          "org.freedesktop.TestSuite",
sl@0
   159
                                          "HelloFromSelf");
sl@0
   160
  
sl@0
   161
  if (self_message == NULL)
sl@0
   162
    die ("No memory");
sl@0
   163
  
sl@0
   164
  if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
sl@0
   165
    die("No memory");
sl@0
   166
  
sl@0
   167
  dbus_message_ref (message);
sl@0
   168
  if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
sl@0
   169
    die("No memory");
sl@0
   170
    
sl@0
   171
  printf ("Sent HelloFromSelf\n");
sl@0
   172
  return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   173
}
sl@0
   174
sl@0
   175
static DBusHandlerResult
sl@0
   176
handle_echo (DBusConnection     *connection,
sl@0
   177
             DBusMessage        *message)
sl@0
   178
{
sl@0
   179
  DBusError error;
sl@0
   180
  DBusMessage *reply;
sl@0
   181
  char *s;
sl@0
   182
sl@0
   183
  _dbus_verbose ("sending reply to Echo method\n");
sl@0
   184
  
sl@0
   185
  dbus_error_init (&error);
sl@0
   186
  
sl@0
   187
  if (!dbus_message_get_args (message,
sl@0
   188
                              &error,
sl@0
   189
                              DBUS_TYPE_STRING, &s,
sl@0
   190
                              DBUS_TYPE_INVALID))
sl@0
   191
    {
sl@0
   192
      reply = dbus_message_new_error (message,
sl@0
   193
                                      error.name,
sl@0
   194
                                      error.message);
sl@0
   195
sl@0
   196
      if (reply == NULL)
sl@0
   197
        die ("No memory\n");
sl@0
   198
sl@0
   199
      if (!dbus_connection_send (connection, reply, NULL))
sl@0
   200
        die ("No memory\n");
sl@0
   201
sl@0
   202
      dbus_message_unref (reply);
sl@0
   203
sl@0
   204
      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sl@0
   205
    }
sl@0
   206
sl@0
   207
  reply = dbus_message_new_method_return (message);
sl@0
   208
  if (reply == NULL)
sl@0
   209
    die ("No memory\n");
sl@0
   210
  
sl@0
   211
  if (!dbus_message_append_args (reply,
sl@0
   212
                                 DBUS_TYPE_STRING, &s,
sl@0
   213
                                 DBUS_TYPE_INVALID))
sl@0
   214
    die ("No memory");
sl@0
   215
  
sl@0
   216
  if (!dbus_connection_send (connection, reply, NULL))
sl@0
   217
    die ("No memory\n");
sl@0
   218
sl@0
   219
  fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
sl@0
   220
  
sl@0
   221
  dbus_message_unref (reply);
sl@0
   222
    
sl@0
   223
  return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   224
}
sl@0
   225
sl@0
   226
static void
sl@0
   227
path_unregistered_func (DBusConnection  *connection,
sl@0
   228
                        void            *user_data)
sl@0
   229
{
sl@0
   230
  /* connection was finalized */
sl@0
   231
}
sl@0
   232
sl@0
   233
static DBusHandlerResult
sl@0
   234
path_message_func (DBusConnection  *connection,
sl@0
   235
                   DBusMessage     *message,
sl@0
   236
                   void            *user_data)
sl@0
   237
{
sl@0
   238
  if (dbus_message_is_method_call (message,
sl@0
   239
                                   "org.freedesktop.TestSuite",
sl@0
   240
                                   "Echo"))
sl@0
   241
    return handle_echo (connection, message);
sl@0
   242
  else if (dbus_message_is_method_call (message,
sl@0
   243
                                        "org.freedesktop.TestSuite",
sl@0
   244
                                        "Exit"))
sl@0
   245
    {
sl@0
   246
      quit ();
sl@0
   247
      return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   248
    }
sl@0
   249
  else if (dbus_message_is_method_call (message,
sl@0
   250
                                        "org.freedesktop.TestSuite",
sl@0
   251
                                        "EmitFoo"))
sl@0
   252
    {
sl@0
   253
      /* Emit the Foo signal */
sl@0
   254
      DBusMessage *signal;
sl@0
   255
      double v_DOUBLE;
sl@0
   256
sl@0
   257
      _dbus_verbose ("emitting signal Foo\n");
sl@0
   258
      
sl@0
   259
      signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
sl@0
   260
                                        "org.freedesktop.TestSuite",
sl@0
   261
                                        "Foo");
sl@0
   262
      if (signal == NULL)
sl@0
   263
        die ("No memory\n");
sl@0
   264
sl@0
   265
      v_DOUBLE = 42.6;
sl@0
   266
      if (!dbus_message_append_args (signal,
sl@0
   267
                                     DBUS_TYPE_DOUBLE, &v_DOUBLE,
sl@0
   268
                                     DBUS_TYPE_INVALID))
sl@0
   269
        die ("No memory");
sl@0
   270
  
sl@0
   271
      if (!dbus_connection_send (connection, signal, NULL))
sl@0
   272
        die ("No memory\n");
sl@0
   273
      
sl@0
   274
      return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   275
    }
sl@0
   276
    
sl@0
   277
  else if (dbus_message_is_method_call (message,
sl@0
   278
                                   "org.freedesktop.TestSuite",
sl@0
   279
                                   "RunHelloFromSelf"))
sl@0
   280
    {
sl@0
   281
      return handle_run_hello_from_self (connection, message);
sl@0
   282
    }
sl@0
   283
  else if (dbus_message_is_method_call (message,
sl@0
   284
                                        "org.freedesktop.TestSuite",
sl@0
   285
                                        "HelloFromSelf"))
sl@0
   286
    {
sl@0
   287
        DBusMessage *reply;
sl@0
   288
        printf ("Received the HelloFromSelf message\n");
sl@0
   289
        
sl@0
   290
        reply = dbus_message_new_method_return (message);
sl@0
   291
        if (reply == NULL)
sl@0
   292
          die ("No memory");
sl@0
   293
        
sl@0
   294
        if (!dbus_connection_send (connection, reply, NULL))
sl@0
   295
          die ("No memory");
sl@0
   296
sl@0
   297
        return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   298
    }
sl@0
   299
  else
sl@0
   300
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sl@0
   301
}
sl@0
   302
sl@0
   303
static DBusObjectPathVTable
sl@0
   304
echo_vtable = {
sl@0
   305
  path_unregistered_func,
sl@0
   306
  path_message_func,
sl@0
   307
  NULL,
sl@0
   308
};
sl@0
   309
sl@0
   310
sl@0
   311
static const char* echo_path = "/org/freedesktop/TestSuite" ;
sl@0
   312
sl@0
   313
static DBusHandlerResult
sl@0
   314
filter_func (DBusConnection     *connection,
sl@0
   315
             DBusMessage        *message,
sl@0
   316
             void               *user_data)
sl@0
   317
{
sl@0
   318
  if (dbus_message_is_signal (message,
sl@0
   319
                              DBUS_INTERFACE_LOCAL,
sl@0
   320
                              "Disconnected"))
sl@0
   321
    {
sl@0
   322
      quit ();
sl@0
   323
      return DBUS_HANDLER_RESULT_HANDLED;
sl@0
   324
    }
sl@0
   325
  else
sl@0
   326
    {
sl@0
   327
      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sl@0
   328
    }
sl@0
   329
}
sl@0
   330
sl@0
   331
int
sl@0
   332
main ()
sl@0
   333
{
sl@0
   334
  DBusError error;
sl@0
   335
  int result,bus_get = 1;
sl@0
   336
  DBusConnection *connection;
sl@0
   337
  
sl@0
   338
  dbus_error_init (&error);
sl@0
   339
  connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
sl@0
   340
 if(connection == NULL)
sl@0
   341
 {
sl@0
   342
 		
sl@0
   343
 		dbus_error_free(&error);
sl@0
   344
 		/*test-service.exe spawned by dbus_daemon_tets_main was returning NULL for dbus_bus_get(), work around for this is to use below API */
sl@0
   345
		connection =  dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
sl@0
   346
 		bus_get = 0;
sl@0
   347
 }
sl@0
   348
  if (connection == NULL)
sl@0
   349
    {
sl@0
   350
      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
sl@0
   351
               error.message);
sl@0
   352
      dbus_error_free (&error);
sl@0
   353
      return 1;
sl@0
   354
    }
sl@0
   355
sl@0
   356
if( !bus_get)
sl@0
   357
{
sl@0
   358
sl@0
   359
	if (!dbus_bus_register (connection, &error))
sl@0
   360
    {
sl@0
   361
     
sl@0
   362
      //_dbus_connection_close_possibly_shared (connection);
sl@0
   363
      dbus_connection_unref (connection);  
sl@0
   364
      return -1;
sl@0
   365
    }
sl@0
   366
}
sl@0
   367
  loop = _dbus_loop_new ();
sl@0
   368
  if (loop == NULL)
sl@0
   369
    die ("No memory\n");
sl@0
   370
  
sl@0
   371
  if (!test_connection_setup (loop, connection))
sl@0
   372
    die ("No memory\n");
sl@0
   373
sl@0
   374
  if (!dbus_connection_add_filter (connection,
sl@0
   375
                                   filter_func, NULL, NULL))
sl@0
   376
    die ("No memory");
sl@0
   377
sl@0
   378
  if (!dbus_connection_register_object_path (connection,
sl@0
   379
                                             echo_path,
sl@0
   380
                                             &echo_vtable,
sl@0
   381
                                             (void*) 0xdeadbeef))
sl@0
   382
    die ("No memory");
sl@0
   383
sl@0
   384
  {
sl@0
   385
    void *d;
sl@0
   386
    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
sl@0
   387
      die ("No memory");
sl@0
   388
    if (d != (void*) 0xdeadbeef)
sl@0
   389
      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
sl@0
   390
  }
sl@0
   391
  
sl@0
   392
  result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
sl@0
   393
                                  0, &error);
sl@0
   394
  if (dbus_error_is_set (&error))
sl@0
   395
    {
sl@0
   396
      fprintf (stderr, "Error %s\n", error.message);
sl@0
   397
      _dbus_verbose ("*** Failed to acquire service: %s\n",
sl@0
   398
                     error.message);
sl@0
   399
      dbus_error_free (&error);
sl@0
   400
      exit (1);
sl@0
   401
    }
sl@0
   402
  
sl@0
   403
  _dbus_verbose ("*** Test service entering main loop\n");
sl@0
   404
  _dbus_loop_run (loop);
sl@0
   405
  
sl@0
   406
  test_connection_shutdown (loop, connection);
sl@0
   407
sl@0
   408
  dbus_connection_remove_filter (connection, filter_func, NULL);
sl@0
   409
  
sl@0
   410
  if( !bus_get)
sl@0
   411
  		dbus_connection_close(connection);
sl@0
   412
  			
sl@0
   413
  dbus_connection_unref (connection);
sl@0
   414
sl@0
   415
  _dbus_loop_unref (loop);
sl@0
   416
  loop = NULL;
sl@0
   417
  
sl@0
   418
  dbus_free(loop);
sl@0
   419
  dbus_shutdown ();
sl@0
   420
sl@0
   421
  _dbus_verbose ("*** Test service exiting\n");
sl@0
   422
  
sl@0
   423
  return 0;
sl@0
   424
}