1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/dbus/tsrc/testapps/exes1/src/dbus_signal.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,219 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include<stdio.h>
1.23 +#include <dbus/dbus.h>
1.24 +#include <stdlib.h>
1.25 +#include <string.h>
1.26 +#include <pthread.h>
1.27 +#include <fcntl.h>
1.28 +#include <unistd.h>
1.29 +#include <sys/stat.h>
1.30 +#define LOG_FILE "c:\\logs\\dbus_signal_log1.txt"
1.31 +#include "std_log_result.h"
1.32 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.33 +
1.34 +#define MAX_SIGNALS 3
1.35 +
1.36 +void create_xml(int result)
1.37 +{
1.38 + if(result)
1.39 + assert_failed = 1;
1.40 +
1.41 + testResultXml("dbus_signal");
1.42 + close_log_file();
1.43 +}
1.44 +
1.45 +int handle_error(DBusError* error)
1.46 + {
1.47 + std_log(LOG_FILENAME_LINE, "%s", error->name);
1.48 + std_log(LOG_FILENAME_LINE, "%s", error->message);
1.49 + dbus_error_free(error);
1.50 + create_xml(1);
1.51 + return 1;
1.52 + }
1.53 +
1.54 +
1.55 +int main()
1.56 +{
1.57 + DBusConnection* connection;
1.58 + DBusError error;
1.59 + DBusMessage* msg;
1.60 + dbus_int32_t arg_int32 = 1010;
1.61 + DBusObjectPathVTable vtable =
1.62 + {
1.63 + NULL,NULL,NULL
1.64 + };
1.65 + char* str = "DBus Testing. hjgfyh34348#$!%^45678901730952698376092869876DBus Testing. hjgfyh34348#$!%^~!@#$%^&*()_+`-=<>?:{},./;'[]45678901730952698376092869876";
1.66 + FILE* fp[MAX_SIGNALS]={NULL};
1.67 + char buf[20] = "";
1.68 + int cnt=0;
1.69 + char exe_param[100];
1.70 + char names[][40]={"test.Signal.Send3 ",
1.71 + "test.Signal.Send4 ",
1.72 + "test.Signal.Send5 "};
1.73 + char obj_path[][40]={"/Test/Signal/Object3",
1.74 + "/Test/Signal/Object4",
1.75 + "/Test/Signal/Object5"};
1.76 + int fd;
1.77 + const char* fifopath = "C:\\signalfifo.file";
1.78 + int err;
1.79 + err = mkfifo(fifopath, 0666);
1.80 + if (err != 0)
1.81 + {
1.82 + // probably file already exists, delete the file
1.83 + unlink(fifopath);
1.84 + // try once more..
1.85 + err = mkfifo(fifopath, 0666);
1.86 + if (err != 0)
1.87 + {
1.88 + create_xml(1);
1.89 + return 1;
1.90 + }
1.91 + }
1.92 +
1.93 + for(cnt=0;cnt< MAX_SIGNALS;cnt++)
1.94 + {
1.95 +#if defined(__WINSCW__) || defined(__WINS__)
1.96 + strcpy(exe_param,"Z:\\sys\\bin\\Simple_Server.exe ");
1.97 +#else
1.98 + strcpy(exe_param,"C:\\sys\\bin\\Simple_Server.exe ");
1.99 +#endif
1.100 + strcat(exe_param, names[cnt]);
1.101 + strcat(exe_param, obj_path[cnt]);
1.102 + fp[cnt] = popen(exe_param, "r");
1.103 + if(!fp[cnt])
1.104 + {
1.105 + std_log(LOG_FILENAME_LINE, "fp %d is NULL", cnt);
1.106 + create_xml(1);
1.107 + return 1;
1.108 + }
1.109 + //wait till the server is up
1.110 + fd = open(fifopath, O_RDONLY);
1.111 + if (fd> 0)
1.112 + {
1.113 + err = read(fd, buf, 20);
1.114 + close(fd);
1.115 + }
1.116 + else
1.117 + {
1.118 + std_log(LOG_FILENAME_LINE, "Error in FIFO open().");
1.119 + create_xml(1);
1.120 + return 1;
1.121 + }
1.122 + if (strcmp("done", buf))
1.123 + {
1.124 + std_log(LOG_FILENAME_LINE,"done is not returned from server.");
1.125 + create_xml(1);
1.126 + return 1;
1.127 + }
1.128 + }
1.129 + unlink(fifopath);
1.130 + dbus_error_init(&error);
1.131 + connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
1.132 +
1.133 + if(!connection || dbus_error_is_set(&error))
1.134 + return handle_error(&error);
1.135 +
1.136 + msg = dbus_message_new_signal("/Test/Signal/Object", "Test.Signal.Send", "first");
1.137 +
1.138 +// sleep(2);
1.139 + if(!msg)
1.140 + {
1.141 + std_log(LOG_FILENAME_LINE, "msg is NULL");
1.142 + create_xml(1);
1.143 + return 1;
1.144 + }
1.145 +
1.146 + if(!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
1.147 + {
1.148 + std_log(LOG_FILENAME_LINE, "Fail to append args");
1.149 + create_xml(1);
1.150 + return 1;
1.151 + }
1.152 + dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send1'",&error);
1.153 +
1.154 + if(dbus_error_is_set(&error))
1.155 + return handle_error(&error);
1.156 +
1.157 + std_log(LOG_FILENAME_LINE, "Registering path");
1.158 + if(!dbus_connection_register_object_path (connection, "/Test/Signal/Object1", &vtable, NULL))
1.159 + {
1.160 + std_log(LOG_FILENAME_LINE, "Registering path fail");
1.161 + create_xml(1);
1.162 + return 1;
1.163 + }
1.164 + std_log(LOG_FILENAME_LINE, "Requesting name");
1.165 + if(!dbus_bus_request_name (connection, "test.Signal.Send1", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error) == -1)
1.166 + {
1.167 + std_log(LOG_FILENAME_LINE, "Requesting name fail");
1.168 + create_xml(1);
1.169 + return 1;
1.170 + }
1.171 +
1.172 + dbus_connection_send(connection, msg, NULL);
1.173 + dbus_connection_flush(connection);
1.174 +
1.175 + dbus_message_unref(msg);
1.176 +
1.177 + str = "";
1.178 + std_log(LOG_FILENAME_LINE, "First part over");
1.179 +
1.180 + cnt=0;
1.181 + while(TRUE)
1.182 + {
1.183 + dbus_connection_read_write(connection, 0);
1.184 +
1.185 + msg = dbus_connection_pop_message(connection);
1.186 +
1.187 + if(msg == NULL)
1.188 + {
1.189 + continue;
1.190 + }
1.191 +
1.192 + std_log(LOG_FILENAME_LINE, "Message Detected");
1.193 +
1.194 + if(dbus_message_is_signal(msg, "Test.Signal.Send1", "second"))
1.195 + {
1.196 + if(!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
1.197 + {
1.198 + std_log(LOG_FILENAME_LINE, "Error while retriving arguments :: %s", error.name);
1.199 + dbus_error_free(&error);
1.200 + create_xml(1);
1.201 + return 1;
1.202 + }
1.203 + std_log(LOG_FILENAME_LINE, "Got %d MSG : %s", cnt, str);
1.204 + cnt++;
1.205 + if(cnt==MAX_SIGNALS)
1.206 + break;
1.207 + }
1.208 +
1.209 + dbus_message_unref(msg);
1.210 + }
1.211 +
1.212 + dbus_connection_close(connection);
1.213 + dbus_connection_unref(connection);
1.214 + dbus_shutdown();
1.215 + for(cnt=0;cnt<MAX_SIGNALS;cnt++)
1.216 + if(fp[cnt])
1.217 + pclose(fp[cnt]);
1.218 +
1.219 + std_log(LOG_FILENAME_LINE, "Test Successful");
1.220 + create_xml(0);
1.221 + return 0;
1.222 +}