os/ossrv/ofdbus/dbus/tsrc/testapps/exes1/src/dbus_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 <pthread.h>
    24 #include <fcntl.h>
    25 #include <unistd.h>
    26 #include <sys/stat.h>
    27 #define LOG_FILE "c:\\logs\\dbus_signal_log1.txt"
    28 #include "std_log_result.h"
    29 #define LOG_FILENAME_LINE __FILE__, __LINE__
    30 
    31 #define MAX_SIGNALS 3
    32 
    33 void create_xml(int result)
    34 {
    35 	if(result)
    36 		assert_failed = 1;
    37 	
    38 	testResultXml("dbus_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 	
    90 	for(cnt=0;cnt< MAX_SIGNALS;cnt++)
    91 	{
    92 #if defined(__WINSCW__) || defined(__WINS__)
    93 		strcpy(exe_param,"Z:\\sys\\bin\\Simple_Server.exe ");
    94 #else
    95 		strcpy(exe_param,"C:\\sys\\bin\\Simple_Server.exe ");
    96 #endif
    97 		strcat(exe_param, names[cnt]);
    98 		strcat(exe_param, obj_path[cnt]);
    99 		fp[cnt] = popen(exe_param, "r");
   100 			if(!fp[cnt])
   101 				{
   102 				std_log(LOG_FILENAME_LINE, "fp %d is NULL", cnt); 
   103 				create_xml(1);
   104 				return 1;
   105 				}
   106 		//wait till the server is up
   107 		fd = open(fifopath, O_RDONLY);
   108 	    if (fd> 0)
   109 	        {
   110 	        err = read(fd, buf, 20);
   111 	        close(fd);
   112 	        }
   113 	    else
   114 	        {
   115 	        std_log(LOG_FILENAME_LINE, "Error in FIFO open().");
   116 	        create_xml(1);
   117 	        return 1;
   118 	        }
   119 	    if (strcmp("done", buf))
   120 	        {
   121 	        std_log(LOG_FILENAME_LINE,"done is not returned from server.");
   122 	        create_xml(1);
   123 	        return 1;
   124 	        }
   125 	}
   126 	unlink(fifopath);
   127 	dbus_error_init(&error);
   128 	connection = dbus_bus_get_private(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 //	sleep(2);
   136 	if(!msg) 
   137 	{
   138 		std_log(LOG_FILENAME_LINE, "msg is NULL");
   139 		create_xml(1);
   140 		return 1;
   141 	}
   142 	 
   143 	if(!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
   144 	{
   145 		std_log(LOG_FILENAME_LINE, "Fail to append args");
   146 		create_xml(1);
   147 		return 1;
   148 	}
   149    dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send1'",&error);
   150     
   151     if(dbus_error_is_set(&error))
   152         return handle_error(&error);
   153     
   154     std_log(LOG_FILENAME_LINE, "Registering path");
   155     if(!dbus_connection_register_object_path (connection, "/Test/Signal/Object1", &vtable, NULL))
   156     { 
   157         std_log(LOG_FILENAME_LINE, "Registering path fail");
   158         create_xml(1);
   159         return 1;
   160     }
   161     std_log(LOG_FILENAME_LINE, "Requesting name");  
   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 	dbus_connection_send(connection, msg, NULL);
   170 	dbus_connection_flush(connection);
   171 	 
   172 	dbus_message_unref(msg);
   173 	
   174 	str = "";
   175 	std_log(LOG_FILENAME_LINE, "First part over");
   176 
   177 	cnt=0;
   178 	while(TRUE)  
   179 	{	
   180 		dbus_connection_read_write(connection, 0);
   181 		
   182 		msg = dbus_connection_pop_message(connection);
   183 			
   184 		if(msg == NULL)
   185 		{
   186 			continue; 
   187 		} 
   188 		
   189 		std_log(LOG_FILENAME_LINE, "Message Detected");
   190 
   191 		if(dbus_message_is_signal(msg, "Test.Signal.Send1", "second"))
   192 		{
   193 			if(!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
   194 				{
   195 					std_log(LOG_FILENAME_LINE, "Error while retriving arguments :: %s", error.name);
   196 					dbus_error_free(&error);
   197 					create_xml(1);
   198 					return 1;
   199 				}  
   200 			std_log(LOG_FILENAME_LINE, "Got %d MSG : %s", cnt, str);
   201 			cnt++;
   202 			if(cnt==MAX_SIGNALS)
   203 				break; 
   204 		}  
   205 
   206 		dbus_message_unref(msg);
   207 	}
   208 		 
   209 	dbus_connection_close(connection);
   210 	dbus_connection_unref(connection);
   211 	dbus_shutdown();
   212 	for(cnt=0;cnt<MAX_SIGNALS;cnt++)
   213 		if(fp[cnt])
   214 			pclose(fp[cnt]);
   215 
   216 	std_log(LOG_FILENAME_LINE, "Test Successful"); 
   217 	create_xml(0);
   218 	return 0;
   219 }