-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbus_sender_exp2.c
176 lines (118 loc) · 3.36 KB
/
dbus_sender_exp2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <glib.h>
#define ESLAB_INTERFACE "/org/eslab/injung"
#define ESLAB_PATH "org.eslab.injung"
static DBusConnectino *Conn = NULL; //Only 1 Connection
static char eslabPath[100];
static char eslabInterface[100];
/*
DBusHandlerResult replyGet(DBusConnection *connection, DBusMessage *message, void *iface_user_data)
{
DBusError err;
dbus_error_init(&err);
dbus_message_get_args(message, &err,
DBUS_TYPE_STRING, &reply
}
*/
static DBusHandlerResult dbus_response(DBusConnection *connection, DBusMessage *message, void *user_data)
{
if(dbus_message_is_signal(message, eslabInterface, "GetReply")){
return replyGet(connection, message, user_data);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
void pid_to_char(*output)
{
pid_t pid = getpid();
unsigned int value = (unsigned int) pid;
unsigned int temp;
output[5] = NULL;
for(int i=4; i>=0; i--)
{
temp = value % 10;
output[i] = ((char)(temp)) + 65;
value = value / 10;
}
printf("Convert: from %u to %s \n", (unsigned int)pid, output);
}
DBusConnection *DbusInit()
{
DBusConnection *connection;
DBusError error;
dbus_error_init(&error);
char total_address[100];
const char *address1 = "type='signal', interface='org.eslab.';
char address2[6];
const char *address3 = "'";
printf(" Start dbus initialzing \n");
pid_to_char(address2);
sprintf(total_address, "%s%s%s", address1, address2, address3);
printf("Total address: %s\n", total_address);
sprintf(eslabPath, "/org/eslab/%s", address2);
sprintf(eslabInterface, "org.eslab.%s", address2);
connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
if(dbus_error_is_set(&error))
{
printf("Error connecting to the daemon bus: %s", error.message);
dbus_error_free(&error);
}
dbus_connection_set_exit_on_disconnect(connection, FALSE);
/*
//if(!eslab_DbusConnection(connection)){
// printf("Failed to initialize connection \n");
//
//}
*/
dbus_bus_add_match(connection, total_address, NULL); //set bus position
dbus_connection_flush(connection);
dbus_connection_add_filter(connection, dbus_response, NULL, NULL); //register dbus message handling fucntion "dbus_response"
printf("Dbus initializing complete\n");
return connection;
}
int main()
{
Conn = DbusInit();
char *str;
char *reply_str;
// Dbus "Get" Start !!!!!!!
DBusMessage *msg;
DBusMessage *reply;
DBusError error;
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.eslab.injung", //Target for the method Call
"/", //object to call on
"org.eslab.injung", //interface to call on
"Get");
if(msg == NULL){
printf("Message NULL\n");
}
str = "Hello Are you there?";
dbus_message_append_args(msg,
DBUS_TYPE_STRING, &(str),
DBUS_TYPE_INVALID);
//send message and get a reply
if( Conn == NULL){
printf("why null?\n");
Conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
}
reply = dbus_connection_send_with_reply_and_block(Conn, msg, 500, &error);
if(reply == NULL)
{
printf("Get Null Reply\n");
printF("Error: %s\n", error.message);
}
dbus_error_free(&error);
dbus_message_unref(msg);
dbus_message_get_args(reply, NULL,
DBUS_TYPE,STRING, &(reply_str),
DBUS_TYPE_INVALID);
dbus_message_unref(reply);
printf("receive: %s\n", reply_str);
printf("End!\n");
return 0;
}