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