os/ossrv/ofdbus/dbus/tsrc/testapps/exes/signal/src/signal.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include<stdio.h>  
    20 #include <dbus/dbus.h>
    21 #include <stdlib.h>
    22 #include <string.h>
    23 #include <fcntl.h>
    24 #include <unistd.h>
    25 #include <sys/stat.h>
    26 
    27 #define MAX_SIGNALS 3
    28 
    29 #define LOG_FILE "c:\\logs\\signal_log1.txt"
    30 #include "std_log_result.h"
    31 #define LOG_FILENAME_LINE __FILE__, __LINE__
    32 
    33 void create_xml(int result)
    34 	{
    35 	if(result)
    36 		assert_failed = 1;
    37 	
    38 	testResultXml("signal");
    39     close_log_file();
    40 	}
    41 
    42 int handle_error(DBusError* error)
    43 	{
    44 	std_log(LOG_FILENAME_LINE,"%s", error->name);
    45 	std_log(LOG_FILENAME_LINE,"%s", error->message);
    46 	dbus_error_free(error);
    47 	create_xml(1);
    48 	return 1; 
    49 	} 
    50 
    51 
    52 int main()
    53 {
    54 	DBusConnection* connection;
    55 	DBusError error;
    56 	DBusMessage* msg;
    57 	dbus_int32_t arg_int32 = 1010;
    58 	DBusObjectPathVTable vtable =
    59 	{
    60 		NULL,NULL,NULL
    61 	};
    62 	char* str = "DBus Testing. hjgfyh34348#$!%^45678901730952698376092869876DBus Testing. hjgfyh34348#$!%^~!@#$%^&*()_+`-=<>?:{},./;'[]45678901730952698376092869876";
    63 	FILE* fp[MAX_SIGNALS]={NULL};
    64 	char buf[20] = "";
    65 	int cnt=0;
    66 	char exe_param[100];
    67 	char names[][40]={"test.Signal.Send3 ",
    68 					"test.Signal.Send4 ",
    69 					"test.Signal.Send5 "};
    70 	char obj_path[][40]={"/Test/Signal/Object3",
    71 					"/Test/Signal/Object4",
    72 					"/Test/Signal/Object5"};
    73 	int fd;
    74 	const char* fifopath = "C:\\signalfifo.file";
    75 	int err;
    76 	err = mkfifo(fifopath, 0666);
    77 	if (err != 0)
    78 	    {
    79 	    // probably file already exists, delete the file
    80         unlink(fifopath);
    81         // try once more..
    82         err = mkfifo(fifopath, 0666);
    83         if (err != 0)
    84             {
    85             create_xml(1);
    86             return 1;
    87             }
    88         }
    89 	for(cnt=0;cnt< MAX_SIGNALS;cnt++)
    90 	{
    91 	#if defined(__WINSCW__) || defined(__WINS__)
    92 		strcpy(exe_param,"Z:\\sys\\bin\\Simple_Server.exe ");
    93 	#else
    94 		strcpy(exe_param,"C:\\sys\\bin\\Simple_Server.exe ");
    95 	#endif
    96 		strcat(exe_param, names[cnt]);
    97 		strcat(exe_param, obj_path[cnt]);
    98 		fp[cnt] = popen(exe_param, "r");
    99 			if(!fp[cnt])
   100 				{
   101 				std_log(LOG_FILENAME_LINE, "fp %d is NULL", cnt); 
   102 				create_xml(1);
   103 				return 1;
   104 				}
   105 		//wait till the server is up
   106 		fd = open(fifopath, O_RDONLY);
   107 	    if (fd> 0)
   108 	        {
   109 	        err = read(fd, buf, 20);
   110 	        close(fd);
   111 	        }
   112 	    else
   113 	        {
   114 	        std_log(LOG_FILENAME_LINE, "Error in FIFO open().");
   115 	        create_xml(1);
   116 	        return 1;
   117 	        }
   118 	    if (strcmp("done", buf))
   119 	        {
   120 	        std_log(LOG_FILENAME_LINE,"done is not returned from server.");
   121 	        create_xml(1);
   122 	        return 1;
   123 	        } 
   124 	}
   125 	
   126 	unlink(fifopath); 	
   127 	dbus_error_init(&error);
   128 	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
   129 	 
   130 	if(!connection || dbus_error_is_set(&error))
   131 		return handle_error(&error);
   132 	  
   133 	msg = dbus_message_new_signal("/Test/Signal/Object", "Test.Signal.Send", "first");
   134 	
   135 	if(!msg) 
   136 	{
   137 		std_log(LOG_FILENAME_LINE,"msg is NULL");
   138 		create_xml(1);
   139 		return 1;
   140 	}
   141 	 
   142 	if(!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
   143 	{
   144 		std_log(LOG_FILENAME_LINE, "Fail to append args");
   145 		create_xml(1);
   146 		return 1;
   147 	}
   148 	dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send1'",&error);
   149 	
   150 	if(dbus_error_is_set(&error))
   151 		return handle_error(&error);
   152 	
   153 	std_log(LOG_FILENAME_LINE, "Registering path");
   154 	if(!dbus_connection_register_object_path (connection, "/Test/Signal/Object1", &vtable, NULL))
   155 	{ 
   156 		std_log(LOG_FILENAME_LINE, "Registering path fail");
   157 		create_xml(1);
   158 		return 1;
   159 	}
   160 	std_log(LOG_FILENAME_LINE, "Requesting name");	
   161 	if(!dbus_bus_request_name (connection, "test.Signal.Send1", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error) == -1)
   162 	{
   163 		std_log(LOG_FILENAME_LINE, "Requesting name fail");	
   164 		create_xml(1);
   165 		return 1;
   166 	}
   167 	
   168 	dbus_connection_send(connection, msg, NULL);
   169 	dbus_connection_flush(connection);
   170 	 
   171 	dbus_message_unref(msg);
   172 	
   173 	str = "";
   174 	std_log(LOG_FILENAME_LINE, "First part over");
   175 	
   176 	cnt=0;
   177 	while(TRUE)  
   178 	{	
   179 		dbus_connection_read_write(connection, 0);
   180 		
   181 		msg = dbus_connection_pop_message(connection);
   182 			
   183 		if(msg == NULL)
   184 		{
   185 			continue; 
   186 		} 
   187 		
   188 		std_log(LOG_FILENAME_LINE, "Message Detected");
   189 	
   190 		if(dbus_message_is_signal(msg, "Test.Signal.Send1", "second"))
   191 		{
   192 			if(!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
   193 				{
   194 					std_log(LOG_FILENAME_LINE, "Error while retriving arguments :: %s", error.name);
   195 					dbus_error_free(&error);
   196 					create_xml(1);
   197 					return 1;
   198 				}  
   199 			std_log(LOG_FILENAME_LINE, "Got %d MSG : %s", cnt, str);
   200 			cnt++;
   201 			if(cnt==MAX_SIGNALS)
   202 				break; 
   203 		}  
   204 	
   205 		dbus_message_unref(msg);
   206 	}
   207 		 
   208 	dbus_connection_unref(connection);
   209 	for(cnt=0;cnt<MAX_SIGNALS;cnt++)
   210 		if(fp[cnt])
   211 			pclose(fp[cnt]);
   212 	
   213 	std_log(LOG_FILENAME_LINE, "Test Successful"); 
   214 	create_xml(0);
   215 	return 0;
   216 }