ofdbus/dbus/tsrc/testapps/exes/req_name/src/req_name.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 <errno.h>
       
    26 
       
    27 #define LOG_FILE "c:\\logs\\req_name_log1.txt"
       
    28 #include "std_log_result.h"
       
    29 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    30 
       
    31 void create_xml(int result)
       
    32 	{
       
    33 	if(result)
       
    34 		assert_failed = 1;
       
    35 	
       
    36 	testResultXml("req_name");
       
    37     close_log_file();
       
    38 	}
       
    39 
       
    40 int handle_error(DBusError* error)
       
    41 	{
       
    42 	std_log(LOG_FILENAME_LINE,"%s", error->name);
       
    43 	std_log(LOG_FILENAME_LINE,"%s", error->message);
       
    44 	dbus_error_free(error);
       
    45 	create_xml(1);
       
    46 	return 1; 
       
    47 	} 
       
    48 
       
    49 int main()
       
    50 {
       
    51 	DBusConnection* connection;
       
    52 	DBusError error;
       
    53 	DBusError error1;
       
    54 	int result;
       
    55 	int fd;
       
    56 	const char* fifopath = "C:\\mkfifo2.file";
       
    57 	int err;
       
    58 	
       
    59 	FILE* fp;
       
    60 	char buf[20];
       
    61 	
       
    62 	dbus_error_init(&error);
       
    63 	dbus_error_init(&error1);
       
    64 	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
       
    65 	if(!connection || dbus_error_is_set(&error))
       
    66 		return handle_error(&error);
       
    67 	
       
    68 	result = dbus_bus_request_name(connection, "Test.Method.Call", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error1);
       
    69 	if(result == -1)
       
    70 		std_log(LOG_FILENAME_LINE, "Error Set :: %s\n%s", error1.name,error1.message);
       
    71 	if(result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
       
    72 	{
       
    73 		std_log(LOG_FILENAME_LINE, "Test Fail");
       
    74 		create_xml(1);
       
    75 		return 1;
       
    76 	}
       
    77 		
       
    78 	result = dbus_bus_request_name(connection, "Test.Method.Call2", DBUS_NAME_FLAG_REPLACE_EXISTING, &error1);
       
    79 	if(result == -1)
       
    80 		std_log(LOG_FILENAME_LINE, "Error Set :: %s\n%s", error1.name,error1.message);
       
    81 	if(result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
       
    82 	{
       
    83 		std_log(LOG_FILENAME_LINE, "Test Fail");
       
    84 		return 1;
       
    85 	}
       
    86 	
       
    87 	result = dbus_bus_request_name(connection, "Test.Method.Call2", DBUS_NAME_FLAG_REPLACE_EXISTING, &error1);
       
    88 	if(result == -1)
       
    89 		std_log(LOG_FILENAME_LINE, "Error Set :: %s\n%s", error1.name,error1.message);
       
    90 	if(result != DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER )
       
    91 	{
       
    92 		std_log(LOG_FILENAME_LINE, "Test Fail");
       
    93 		create_xml(1);
       
    94 		return 1;
       
    95 	}
       
    96 	
       
    97 	//for FIFO
       
    98 	
       
    99 	err = mkfifo (fifopath, 0666);
       
   100 	   if(err != 0)
       
   101 	   {
       
   102 	       // probably file already exists, delete the file
       
   103 	       unlink(fifopath); 
       
   104 	       // try once more..
       
   105 	       err = mkfifo (fifopath, 0666);
       
   106 	       if(err != 0)
       
   107 	       {
       
   108 	       		std_log(LOG_FILENAME_LINE, "Failed to make FIFO");
       
   109 	       		create_xml(1);
       
   110 	            return 1;
       
   111 	       }
       
   112 	   }
       
   113 	
       
   114 	#if defined(__WINSCW__) || defined(__WINS__)
       
   115 	fp = popen("z:\\sys\\bin\\request_name.exe","r");
       
   116 	#else
       
   117 	fp = popen("c:\\sys\\bin\\request_name.exe","r");
       
   118 	#endif
       
   119 	if(!fp)
       
   120 		{
       
   121 		std_log(LOG_FILENAME_LINE, "fp is NULL.");
       
   122 		create_xml(1);
       
   123 		return 1;
       
   124 		}	
       
   125 	fd = open(fifopath, O_RDONLY);
       
   126 	if(fd > 0)
       
   127 	{
       
   128 		err = read (fd, buf, 20); 
       
   129 		if(!strcmp("Error Occured", buf))
       
   130 			{
       
   131 			std_log(LOG_FILENAME_LINE, "Error while requesting name in request_name.exe.");
       
   132 			create_xml(1);
       
   133 			return 1;
       
   134 			}
       
   135 		close(fd);
       
   136 	}
       
   137 	else
       
   138 		{
       
   139 		std_log(LOG_FILENAME_LINE, "Error in FIFO open().");
       
   140 		create_xml(1);
       
   141 		return 1;
       
   142 		}
       
   143 	unlink(fifopath);
       
   144 	if(strcmp("done", buf))
       
   145 		{
       
   146 		create_xml(1);
       
   147 		return 1;
       
   148 		}
       
   149 	
       
   150 	result = dbus_bus_request_name(connection, "Test.Method.Call3", DBUS_NAME_FLAG_DO_NOT_QUEUE , &error1);
       
   151 	if(result == -1)
       
   152 		std_log(LOG_FILENAME_LINE, "Error Set :: %s\n%s", error1.name,error1.message);
       
   153 	if(result != DBUS_REQUEST_NAME_REPLY_EXISTS )
       
   154 	{
       
   155 		std_log(LOG_FILENAME_LINE, "Test Fail");
       
   156 		create_xml(1);
       
   157 		return 1;
       
   158 	}	
       
   159 	
       
   160 	dbus_connection_unref(connection);
       
   161 	std_log(LOG_FILENAME_LINE, "Test Successful");
       
   162 	create_xml(0);
       
   163 	return 0;
       
   164 }