os/ossrv/ofdbus/dbus/tsrc/testapps/exes1/src/file_writecontact0.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 
    24 #define LOG_FILE "c:\\logs\\file_writecontact0_log1.txt"
    25 #include "std_log_result.h"
    26 #define LOG_FILENAME_LINE __FILE__, __LINE__
    27 
    28 void create_xml(int result)
    29 {
    30 	if(result)
    31 		assert_failed = 1;
    32 	
    33 	testResultXml("file_writecontact0");
    34 	close_log_file();
    35 }
    36 
    37 int main()
    38 {
    39  		DBusConnection* connection;
    40 	DBusError error;
    41 	DBusMessage* msg;
    42 	DBusMessage* msg1;
    43 	DBusMessageIter append_iter;
    44 	DBusMessageIter return_iter;
    45 	DBusMessageIter sub_iter;
    46 	DBusPendingCall* pending;
    47 	char error_name[40];
    48 	char error_msg[40];
    49 	dbus_int32_t return_value;
    50 	char ret_msg[20]; 
    51 	int i;
    52 	
    53 	
    54 	char* arg_str = "DBus Testing";
    55 	char* arg_obj_path = "\Test\Object\Path";
    56 	int size;
    57 	struct data_contact
    58 	{
    59 		char *name;
    60 		char *ph_no;
    61 		dbus_int32_t cnt;
    62 	}data;
    63 	size = sizeof(struct data_contact);
    64 	std_log(LOG_FILENAME_LINE, "size of struct is %d",size);
    65 	  
    66 	 
    67 	
    68 	data.name = "DBus";
    69 	data.ph_no = "+91234455";
    70 	//cnt=0;
    71 	
    72 	
    73 	dbus_error_init(&error);
    74 	
    75 	connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
    76 	if(!connection)
    77 	{
    78 		
    79 		std_log(LOG_FILENAME_LINE, "error_name",error.name);
    80 		std_log(LOG_FILENAME_LINE, "error_msg",error.message);
    81 		create_xml(1);
    82 		return 1;
    83 	}  
    84 	msg = dbus_message_new_method_call("Test.Method.Call1", "/Test/Method/Object", "test.Method.Call", "file_writecontact");
    85 	if(msg == NULL)
    86 		{ 
    87 			std_log(LOG_FILENAME_LINE, "message error");
    88 		create_xml(1);
    89 		return 1;
    90 		}
    91 		std_log(LOG_FILENAME_LINE, "message created successfully");
    92 	 
    93 	
    94 	dbus_message_iter_init_append(msg, &append_iter);
    95 	
    96 	if(!dbus_message_iter_append_basic(&append_iter, DBUS_TYPE_STRING, &arg_str))
    97 		{
    98 		std_log(LOG_FILENAME_LINE, "No memory");
    99 		create_xml(1);
   100 		return 1;
   101 		}
   102 	
   103 	// structure 
   104 	
   105 		 
   106 	for(i=0; i<=50; i++)  
   107 	{
   108 	if(!dbus_message_iter_open_container(&append_iter, DBUS_TYPE_STRUCT, NULL, &sub_iter))
   109 		{
   110 		std_log(LOG_FILENAME_LINE, "No memory");
   111 		create_xml(1); 
   112 		return 1;
   113 		}	     
   114 		data.cnt = i; 
   115 		if(!dbus_message_iter_append_basic(&sub_iter, DBUS_TYPE_INT32, &data.cnt))
   116 		{ 
   117 			std_log(LOG_FILENAME_LINE, "No memory");
   118 			create_xml(1);
   119 			return 1;  
   120 		} 
   121 		if(!dbus_message_iter_append_basic(&sub_iter, DBUS_TYPE_STRING, &data.name))
   122 		{ 
   123 			std_log(LOG_FILENAME_LINE, "No memory");
   124 			create_xml(1);
   125 			return 1;  
   126 		} 
   127 		if(!dbus_message_iter_append_basic(&sub_iter, DBUS_TYPE_STRING, &data.ph_no))
   128 		{ 
   129 			std_log(LOG_FILENAME_LINE, "No memory");
   130 			create_xml(1);
   131 			return 1;  
   132 		} 
   133 		dbus_message_iter_close_container(&append_iter, &sub_iter);  // for 80 structure
   134 	}
   135 	
   136 	
   137 	// send message and get a handle for a reply
   138 	   if (!dbus_connection_send_with_reply (connection, msg, &pending, -1)) { // -1 is default timeout
   139 	   	std_log(LOG_FILENAME_LINE, "message send error");
   140 	   		create_xml(1);
   141 	   		exit(1);
   142 	   }   
   143 	   if (NULL == pending) {
   144 	   	std_log(LOG_FILENAME_LINE, "pending is null");
   145 	   		create_xml(1);
   146 	      exit(1);
   147 	   }
   148 	   dbus_connection_flush(connection);
   149 	   
   150 	   // free message
   151 	   dbus_message_unref(msg); 
   152 	  
   153 	   // block until we recieve a reply
   154 	   dbus_pending_call_block(pending);
   155 	
   156 	   // get the reply message
   157 	   msg1 = dbus_pending_call_steal_reply(pending);
   158 	   if (NULL == msg1) {
   159 	   	std_log(LOG_FILENAME_LINE, "Reply error"); 
   160 	   create_xml(1);
   161 	 
   162 	      exit(1);
   163 	   } 
   164 	   
   165 	   // free the pending message handle
   166 	   dbus_pending_call_unref(pending);
   167 		 
   168 		dbus_error_init (&error);
   169 		dbus_message_iter_init(msg1, &return_iter);
   170 
   171 		dbus_message_iter_get_basic(&return_iter, &return_value);
   172 		std_log(LOG_FILENAME_LINE, "reply = %d",return_value);
   173 		 
   174 			if(return_value != 50)
   175 			{
   176 			std_log(LOG_FILENAME_LINE, "return value is not what is sent");
   177 			create_xml(1);
   178 			return 1;
   179 			} 
   180 			dbus_message_unref(msg);  
   181 	   
   182 	   dbus_connection_close(connection);
   183 	   dbus_connection_unref(connection);
   184 	   dbus_shutdown();
   185 	   std_log(LOG_FILENAME_LINE, "SUCCESS");
   186  		   create_xml(0);
   187 		    return 0;  
   188 
   189 }