ofdbus/dbus/tsrc/testapps/exes1/src/dbus_match_rule.c
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 <pthread.h>
       
    24 #include <fcntl.h>
       
    25 #include <unistd.h>
       
    26 #include <sys/stat.h>
       
    27 
       
    28 #define LOG_FILE "c:\\logs\\dbus_match_rule_log1.txt"
       
    29 #include "std_log_result.h"
       
    30 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    31 
       
    32 void create_xml(int result)
       
    33 {
       
    34 	if(result)
       
    35 		assert_failed = 1;
       
    36 	
       
    37 	testResultXml("dbus_match_rule");
       
    38 	close_log_file();
       
    39 }
       
    40 
       
    41 int handle_error(DBusError* error)
       
    42 	{
       
    43 	std_log(LOG_FILENAME_LINE, "%s", error->name);
       
    44 	std_log(LOG_FILENAME_LINE, "%s", error->message);
       
    45 	dbus_error_free(error);
       
    46 	create_xml(1);
       
    47 	return 1; 
       
    48 	} 
       
    49 
       
    50 int send_message(DBusConnection* connection, char* path, char* iface, char* member)
       
    51 {
       
    52 	DBusMessage* msg;
       
    53 	char* str = "DBus Testing.";
       
    54 	
       
    55 	msg = dbus_message_new_signal(path, iface, member);
       
    56 	
       
    57 	if(!msg) 
       
    58 	{ 
       
    59 		std_log(LOG_FILENAME_LINE, "msg is NULL");
       
    60 		create_xml(1);
       
    61 		return 1;
       
    62 	}
       
    63 	 
       
    64 	if(!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
       
    65 	{
       
    66 		std_log(LOG_FILENAME_LINE, "Fail to append args");
       
    67 		create_xml(1);
       
    68 		return 1;
       
    69 	}
       
    70 	 
       
    71 	dbus_connection_send(connection, msg, NULL);
       
    72 	dbus_connection_flush(connection);	 
       
    73 	dbus_message_unref(msg);
       
    74 	return 0;
       
    75 }
       
    76 
       
    77 int main()
       
    78 {
       
    79 	DBusConnection* connection;
       
    80 	DBusError error;
       
    81 	DBusMessage* msg;
       
    82 	char* str;
       
    83 	const char *retStr;
       
    84 	DBusObjectPathVTable vtable ={
       
    85 	  NULL,
       
    86 	  NULL,
       
    87 	  NULL,
       
    88 	};
       
    89 	char buf[180] = ""; 
       
    90 	FILE* fp = NULL;
       
    91 	int fd;
       
    92 
       
    93 	const char* fifopath = "C:\\mkfifo1.file";
       
    94 	int err;
       
    95 	err = mkfifo (fifopath, 0666);
       
    96 	   if(err != 0)
       
    97 	   {
       
    98 	       // probably file already exists, delete the file
       
    99 	       unlink(fifopath); 
       
   100 	       // try once more..
       
   101 	       err = mkfifo (fifopath, 0666);
       
   102 	       if(err != 0)
       
   103 	       {
       
   104 			create_xml(1);
       
   105 	            return 1;
       
   106 	       }
       
   107 	   }
       
   108 
       
   109 #if defined(__WINSCW__) || defined(__WINS__)
       
   110 	fp = popen("Z:\\sys\\bin\\match_rule_server.exe", "r");
       
   111 #else
       
   112 	fp = popen("C:\\sys\\bin\\match_rule_server.exe", "r");
       
   113 #endif
       
   114 	if(!fp)
       
   115 		std_log(LOG_FILENAME_LINE, "fp is NULL"); 
       
   116 
       
   117 	// checkpoint 1. server up and registered to the dbus.
       
   118 	fd = open(fifopath, O_RDONLY);
       
   119 	if(fd > 0)
       
   120 	{
       
   121 		err = read (fd, buf, 20);  
       
   122 		close(fd);
       
   123 	}
       
   124 	else
       
   125 		{
       
   126 		std_log(LOG_FILENAME_LINE, "Error in FIFO open().");
       
   127 		create_xml(1);
       
   128 		return 1;
       
   129 		}
       
   130 	// unlink(fifopath);
       
   131 	if(strcmp("done1", buf))
       
   132 		{
       
   133 		std_log(LOG_FILENAME_LINE, "done is not returned from server.");
       
   134 		create_xml(1);
       
   135 		return 1;
       
   136 		}
       
   137 	
       
   138 	dbus_error_init(&error);
       
   139 	connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
       
   140 	 
       
   141 	if(!connection || dbus_error_is_set(&error))
       
   142 		return handle_error(&error);
       
   143 	  
       
   144 	send_message(connection, "/Test/Signal/Object", "Test.Signal.Send2", "first");
       
   145 	
       
   146 	std_log(LOG_FILENAME_LINE, "First part over");
       
   147 	
       
   148 	dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send3'",&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 	
       
   162 	if(!dbus_bus_request_name (connection, "test.Signal.Send1", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error) == -1)
       
   163 	{
       
   164 		std_log(LOG_FILENAME_LINE, "Requesting name fail");	
       
   165 		create_xml(1);
       
   166 		return 1;
       
   167 	}
       
   168 	
       
   169 	std_log(LOG_FILENAME_LINE, "Starting while loop");
       
   170 	while(TRUE)  
       
   171 	{	
       
   172 		dbus_connection_read_write(connection, 0);
       
   173 		
       
   174 		msg = dbus_connection_pop_message(connection);
       
   175 			
       
   176 		if(msg == NULL)
       
   177 		{
       
   178 			continue; 
       
   179 		} 
       
   180 		
       
   181 		std_log(LOG_FILENAME_LINE, "Message Detected");
       
   182 	
       
   183 		if(dbus_message_is_signal(msg, "Test.Signal.Send3", "second"))
       
   184 		{
       
   185 			if(!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
       
   186 				{
       
   187 					dbus_error_free(&error);
       
   188 					create_xml(1);
       
   189 					return 1;
       
   190 				}  
       
   191 			std_log(LOG_FILENAME_LINE, "Got MSG : %s",  str);
       
   192 			break; 
       
   193 		}    
       
   194 	
       
   195 		dbus_message_unref(msg); 
       
   196 	} 
       
   197 	
       
   198 	// Checkpoint 2. match rule on server removed
       
   199 	fd = open(fifopath, O_RDONLY);
       
   200     if (fd > 0)
       
   201         {
       
   202         err = read(fd, buf, 20);
       
   203         close(fd);
       
   204         }
       
   205     else
       
   206         {
       
   207         std_log(LOG_FILENAME_LINE, "2. Error in FIFO open().");
       
   208         create_xml(1);
       
   209         return 1;
       
   210         }
       
   211       unlink(fifopath);
       
   212     if (strcmp("done2", buf))
       
   213         {
       
   214         std_log(LOG_FILENAME_LINE, "2. done is not returned from server.");
       
   215         create_xml(1);
       
   216         return 1;
       
   217         }
       
   218 	send_message(connection, "/Test/Signal/Object", "Test.Signal.Send2", "third");
       
   219 	send_message(connection, "/Test/Signal/Object", "Test.Signal.Send2", "first");	
       
   220 	send_message(connection, "/Test/Signal/Object", "Test.Signal.Send2", "third");
       
   221 
       
   222 	while(TRUE)
       
   223 	{
       
   224 		dbus_connection_read_write(connection, 0);
       
   225 		msg = dbus_connection_pop_message(connection);
       
   226 		if(!msg)
       
   227 			continue;
       
   228 		// discard spurious connection messages
       
   229         retStr = dbus_message_get_destination(msg);
       
   230         if(!strncmp(":", retStr, 1))
       
   231             continue;
       
   232 		if(!dbus_message_has_destination(msg, "test.Signal.Send1"))
       
   233 		{
       
   234 			std_log(LOG_FILENAME_LINE, "dbus_message_has_destination() failed to check Destination.");
       
   235 			create_xml(1);
       
   236 			return 1;
       
   237 		}
       
   238 		
       
   239 		if(dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_ERROR)
       
   240 		{
       
   241 			std_log(LOG_FILENAME_LINE, "Error is returned.");
       
   242 			if(!dbus_message_has_member(msg, "error"))
       
   243 				std_log(LOG_FILENAME_LINE, "dbus_message_has_member() failed to check error.");
       
   244 			create_xml(1);
       
   245 			return 1;
       
   246 		}
       
   247 		else if(dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_CALL)
       
   248 			{
       
   249 			std_log(LOG_FILENAME_LINE, "Method call is returned.");
       
   250 			if(!dbus_message_has_member(msg, "success"))
       
   251 			{
       
   252 				std_log(LOG_FILENAME_LINE, "dbus_message_has_member() failed to check error.");
       
   253 				create_xml(1);
       
   254 				return 1;
       
   255 			}
       
   256 			break; 
       
   257 			}
       
   258 		
       
   259 	}
       
   260 	dbus_connection_close(connection);
       
   261 	dbus_connection_unref(connection);
       
   262 	dbus_shutdown();
       
   263 	if(fp) 
       
   264 		pclose(fp);
       
   265 	
       
   266 	std_log(LOG_FILENAME_LINE, "Test Successful");
       
   267 	create_xml(0);	
       
   268 	return 0; 
       
   269 }