31
|
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 <unistd.h>
|
|
25 |
|
|
26 |
#define LOG_FILE "c:\\logs\\dbus_N_messages_log1.txt"
|
|
27 |
#include "std_log_result.h"
|
|
28 |
#define LOG_FILENAME_LINE __FILE__, __LINE__
|
|
29 |
|
|
30 |
#define MAX_THREAD 25
|
|
31 |
|
|
32 |
typedef struct{
|
|
33 |
pthread_mutex_t mutex;
|
|
34 |
pthread_cond_t cond;
|
|
35 |
DBusConnection* connection;
|
|
36 |
int ret;
|
|
37 |
}threadData;
|
|
38 |
|
|
39 |
void create_xml(int result)
|
|
40 |
{
|
|
41 |
if(result)
|
|
42 |
assert_failed = 1;
|
|
43 |
|
|
44 |
testResultXml("dbus_N_messages");
|
|
45 |
close_log_file();
|
|
46 |
}
|
|
47 |
|
|
48 |
int handle_error(DBusError* error)
|
|
49 |
{
|
|
50 |
std_log(LOG_FILENAME_LINE, "%s", error->name);
|
|
51 |
std_log(LOG_FILENAME_LINE, "%s", error->message);
|
|
52 |
dbus_error_free(error);
|
|
53 |
create_xml(1);
|
|
54 |
return 1;
|
|
55 |
}
|
|
56 |
|
|
57 |
static void* send_msg(void* data)
|
|
58 |
{
|
|
59 |
threadData* thrData = (threadData*)data;
|
|
60 |
static int cnt = 1;
|
|
61 |
dbus_int32_t no = 5;
|
|
62 |
DBusPendingCall* pending;
|
|
63 |
DBusMessage* msg1;
|
|
64 |
DBusMessage* msg;
|
|
65 |
DBusError error;
|
|
66 |
FILE* fp;
|
|
67 |
|
|
68 |
pthread_mutex_lock(&thrData->mutex);
|
|
69 |
|
|
70 |
msg = dbus_message_new_method_call("Test.Method.Call", "/Test/Method/Object", "test.Method.Call", "simple");
|
|
71 |
|
|
72 |
dbus_message_append_args(msg, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
|
|
73 |
|
|
74 |
pthread_cond_wait(&thrData->cond, &thrData->mutex);
|
|
75 |
|
|
76 |
// send message and get a handle for a reply
|
|
77 |
if (!dbus_connection_send_with_reply (thrData->connection, msg, &pending, -1)) { // -1 is default timeout
|
|
78 |
thrData->ret = 2;
|
|
79 |
}
|
|
80 |
|
|
81 |
if (NULL == pending) {
|
|
82 |
thrData->ret = 2;
|
|
83 |
}
|
|
84 |
dbus_connection_flush(thrData->connection);
|
|
85 |
|
|
86 |
// free message
|
|
87 |
dbus_message_unref(msg);
|
|
88 |
|
|
89 |
// block until we recieve a reply
|
|
90 |
dbus_pending_call_block(pending);
|
|
91 |
|
|
92 |
// get the reply message
|
|
93 |
msg1 = dbus_pending_call_steal_reply(pending);
|
|
94 |
if (NULL == msg1) {
|
|
95 |
thrData->ret = 2;
|
|
96 |
|
|
97 |
}
|
|
98 |
// free the pending message handle
|
|
99 |
dbus_pending_call_unref(pending);
|
|
100 |
|
|
101 |
dbus_error_init(&error);
|
|
102 |
dbus_message_get_args(msg1, &error, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
|
|
103 |
|
|
104 |
fp = fopen("C:\\new.txt", "a+");
|
|
105 |
fprintf(fp, "%d\n", no);
|
|
106 |
fclose(fp);
|
|
107 |
|
|
108 |
if(no == 9090)
|
|
109 |
{
|
|
110 |
thrData->ret++;
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
// free reply and close connection
|
|
115 |
dbus_message_unref(msg1);
|
|
116 |
pthread_mutex_unlock(&thrData->mutex);
|
|
117 |
return NULL;
|
|
118 |
}
|
|
119 |
|
|
120 |
|
|
121 |
int main()
|
|
122 |
{
|
|
123 |
DBusError error;
|
|
124 |
int cnt;
|
|
125 |
threadData thrData;
|
|
126 |
|
|
127 |
pthread_t thread[MAX_THREAD];
|
|
128 |
int thrVal[MAX_THREAD]={0};
|
|
129 |
void* thrValPtr[MAX_THREAD];
|
|
130 |
|
|
131 |
for(cnt=0; cnt<MAX_THREAD; cnt++)
|
|
132 |
thrValPtr[cnt] = (void*)&thrVal[cnt];
|
|
133 |
|
|
134 |
dbus_error_init(&error);
|
|
135 |
thrData.connection = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
|
|
136 |
if(!thrData.connection || dbus_error_is_set(&error))
|
|
137 |
return handle_error(&error);
|
|
138 |
|
|
139 |
pthread_mutex_init(&thrData.mutex, NULL);
|
|
140 |
pthread_cond_init(&thrData.cond, NULL);
|
|
141 |
thrData.ret = 0;
|
|
142 |
|
|
143 |
for(cnt=0; cnt<MAX_THREAD; cnt++)
|
|
144 |
pthread_create(&thread[cnt], NULL, &send_msg, &thrData);
|
|
145 |
|
|
146 |
sleep(1);
|
|
147 |
|
|
148 |
pthread_cond_broadcast(&thrData.cond);
|
|
149 |
|
|
150 |
for(cnt=0; cnt<MAX_THREAD; cnt++)
|
|
151 |
pthread_join(thread[cnt], &thrValPtr[cnt]);
|
|
152 |
|
|
153 |
if(thrData.ret != MAX_THREAD)
|
|
154 |
{
|
|
155 |
std_log(LOG_FILENAME_LINE, "No. of threads crashed %d", (MAX_THREAD - thrData.ret));
|
|
156 |
create_xml(1);
|
|
157 |
return 1;
|
|
158 |
}
|
|
159 |
|
|
160 |
dbus_connection_close(thrData.connection);
|
|
161 |
dbus_connection_unref(thrData.connection);
|
|
162 |
dbus_shutdown();
|
|
163 |
|
|
164 |
std_log(LOG_FILENAME_LINE, "Test Successful");
|
|
165 |
create_xml(0);
|
|
166 |
return 0;
|
|
167 |
}
|