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