Fix the issue that changed callback is not working
[platform/core/appfw/notification-service.git] / bluetooth_notification_client.c
1 #include <Ecore.h>
2 #include <notification.h>
3 #include <unistd.h>
4
5 #include <dbus/dbus.h>
6 #include <dbus/dbus-glib.h>
7 #include <dbus/dbus-glib-lowlevel.h>
8 #include <bundle.h>
9 #include <dlog.h>
10 #include <libwlmessage.h>
11
12 typedef enum {
13            BT_AGENT_ACCEPT,
14            BT_AGENT_REJECT,
15            BT_AGENT_CANCEL,
16            BT_CORE_AGENT_TIMEOUT,
17 } bt_agent_accept_type_t;
18
19
20 typedef void (*bt_notification)(DBusGProxy *proxy);
21
22 static DBusGProxy*
23 __bluetooth_create_agent_proxy(DBusGConnection *sys_conn, const char *path)
24 {
25         return dbus_g_proxy_new_for_name (sys_conn,
26                                           "org.projectx.bt",
27                                           path,
28                                           "org.bluez.Agent1");
29 }
30
31 static DBusGProxy*
32 __bluetooth_create_obex_proxy(DBusGConnection *sys_conn)
33 {
34         return dbus_g_proxy_new_for_name(sys_conn,
35                                          "org.bluez.frwk_agent",
36                                          "/org/obex/ops_agent",
37                                          "org.openobex.Agent");
38 }
39
40 static void
41 __notify_passkey_confirm_request_accept_cb( DBusGProxy* agent_proxy)
42 {
43         dbus_g_proxy_call_no_reply( agent_proxy, "ReplyConfirmation",
44                                    G_TYPE_UINT, BT_AGENT_ACCEPT,
45                                    G_TYPE_INVALID, G_TYPE_INVALID);
46
47 }
48
49 static void
50 __notify_passkey_confirm_request_cancel_cb(DBusGProxy* agent_proxy)
51 {
52
53         dbus_g_proxy_call_no_reply( agent_proxy, "ReplyConfirmation",
54                                     G_TYPE_UINT, BT_AGENT_CANCEL,
55                                     G_TYPE_INVALID, G_TYPE_INVALID);
56
57 }
58
59 static void
60 __notify_push_authorize_request_accept_cb(DBusGProxy* obex_proxy)
61 {
62
63         dbus_g_proxy_call_no_reply( obex_proxy, "ReplyAuthorize",
64                                     G_TYPE_UINT, BT_AGENT_ACCEPT,
65                                     G_TYPE_INVALID, G_TYPE_INVALID);
66
67 }
68
69 static void
70 __notify_push_authorize_request_cancel_cb(DBusGProxy* obex_proxy)
71 {
72
73         dbus_g_proxy_call_no_reply( obex_proxy, "ReplyAuthorize",
74                                     G_TYPE_UINT, BT_AGENT_CANCEL,
75                                     G_TYPE_INVALID, G_TYPE_INVALID);
76
77 }
78
79 static void
80 __notify_authorize_request_accept_cb(DBusGProxy* agent_proxy)
81 {
82
83          dbus_g_proxy_call_no_reply( agent_proxy, "ReplyAuthorize",
84                                      G_TYPE_UINT, BT_AGENT_ACCEPT,
85                                      G_TYPE_INVALID, G_TYPE_INVALID);
86 }
87
88 static void
89 __notify_authorize_request_cancel_cb(DBusGProxy* agent_proxy)
90 {
91
92          dbus_g_proxy_call_no_reply( agent_proxy, "ReplyAuthorize",
93                                      G_TYPE_UINT, BT_AGENT_CANCEL,
94                                      G_TYPE_INVALID, G_TYPE_INVALID);
95
96 }
97
98 static int
99 __display_notification(bt_notification cb_1, bt_notification cb_2, DBusGProxy *proxy)
100 {
101
102          notification_error_e err = NOTIFICATION_ERROR_NONE;
103          int bt_yesno = 1;
104          char line[4];
105
106          struct wlmessage *wlmessage = wlmessage_create();
107          wlmessage_set_message(wlmessage, "Do you confirm ?");
108          wlmessage_add_button(wlmessage, 1, "Yes");
109          wlmessage_add_button(wlmessage, 0, "No");
110          wlmessage_set_default_button(wlmessage, 1);
111          bt_yesno = wlmessage_show(wlmessage, NULL);
112          wlmessage_destroy(wlmessage);
113
114          if (bt_yesno == 1) {
115                  LOGD("user accepts to pair with device ");
116                  (cb_1) (proxy);
117          } else if (bt_yesno == 0) {
118                  LOGD("user rejects to pair with device ");
119                  (cb_2) (proxy);
120         }
121
122 #if 0
123          fprintf(stdout, "Do you confirm yes or no ? ");
124          while ( bt_yesno != 0){
125                  if (!fgets(line, sizeof(line), stdin))
126                          continue;
127                  if (strncmp("yes", line, 3) == 0) {
128                          LOGD("user accepts to pair with device ");
129                          (cb_1) (proxy);
130                          bt_yesno = 0;
131                  } else if (strncmp("no", line, 2) == 0) {
132                          LOGD("user rejects to pair with device ");
133                          (cb_2) (proxy);
134                          bt_yesno = 0;
135                  } else {
136                          fprintf(stdout," yes or no ?\n");
137                  }
138          }
139 #endif
140          err = notification_delete_all_by_type("bluetooth-frwk-bt-service", NOTIFICATION_TYPE_NOTI);
141          if (err != NOTIFICATION_ERROR_NONE) {
142                   LOGE("Unable to remove notifications");
143          }
144
145 }
146
147 static void __noti_changed_cb(void *data, notification_type_e type)
148 {
149          notification_h noti = NULL;
150          notification_list_h notification_list = NULL;
151          notification_list_h get_list = NULL;
152          int count = 0, group_id = 0, priv_id = 0, show_noti = 0, num = 1;
153          char *pkgname = NULL;
154          char *title = NULL;
155          char *str_count = NULL;
156          char *content= NULL;
157          bundle *user_data = NULL;
158          DBusGConnection *sys_conn;
159          DBusGProxy *agent_proxy;
160          DBusGProxy *obex_proxy;
161
162
163          const char *device_name = NULL;
164          const char *passkey = NULL;
165          const char *file = NULL;
166          const char *agent_path;
167          const char *event_type = NULL;
168
169          notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &notification_list);
170          if (notification_list) {
171                 get_list = notification_list_get_head(notification_list);
172                 noti = notification_list_get_data(get_list);
173                 notification_get_id(noti, &group_id, &priv_id);
174                 notification_get_pkgname(noti, &pkgname);
175                 if(pkgname == NULL){
176                        notification_get_application(noti, &pkgname);
177                 }
178
179                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &str_count);
180                 if (!str_count) {
181                         count = 0;
182                 } else {
183                         count = atoi(str_count);
184                 }
185                 notification_get_title(noti, &title, NULL);
186                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT , &content);
187                 notification_get_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,NULL, &user_data);
188
189                 fprintf(stdout, "NOTIFICATION: %s - %s - %s - %i - %i \n", pkgname, title, content, count, num);
190
191                 event_type = bundle_get_val(user_data, "event-type");
192
193                 if (!event_type) {
194                         LOGD("Not a bluetooth-related notification...");
195                         return;
196                 }
197
198                 if(!strcasecmp(event_type, "pin-request")) {
199                         /* Not implemented */
200                         fprintf(stdout," Not implemented\n");
201
202                 } else if (!strcasecmp(event_type, "passkey-confirm-request")){
203                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
204                         passkey = (gchar*) bundle_get_val(user_data, "passkey");
205                         agent_path = bundle_get_val(user_data, "agent-path");
206
207                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
208                         if (sys_conn == NULL) {
209                                 fprintf(stdout,"ERROR: Can't get on system bus");
210                                 return;
211                         }
212
213                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
214                         if (!agent_proxy){
215                                 fprintf(stdout,"create new agent_proxy failed\n");
216                                 return;
217                         }
218
219                          __display_notification(__notify_passkey_confirm_request_accept_cb, __notify_passkey_confirm_request_cancel_cb,agent_proxy);
220                 } else if (!strcasecmp(event_type, "passkey-request")) {
221                         /* Not implemented */
222                         fprintf(stdout," Not implemented\n");
223
224                 } else if (!strcasecmp(event_type, "passkey-display-request")) {
225                         /* Not implemented */
226                         fprintf(stdout," Not implemented\n");
227
228                 } else if (!strcasecmp(event_type, "authorize-request")) {
229                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
230                         agent_path = bundle_get_val(user_data, "agent-path");
231
232                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
233                         if (sys_conn == NULL) {
234                                 fprintf(stdout,"ERROR: Can't get on system bus");
235                                 return;
236                         }
237
238                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
239                         if (!agent_proxy){
240                                 fprintf(stdout,"create new agent_proxy failed\n");
241                                 return;
242                         }
243
244                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
245                 } else if (!strcasecmp(event_type, "app-confirm-request")) {
246                         /* Not implemented */
247                         fprintf(stdout," Not implemented\n");
248
249                 } else if (!strcasecmp(event_type, "push-authorize-request")) {
250                         file = (gchar*) bundle_get_val(user_data, "file");
251                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
252
253                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
254                         if (sys_conn == NULL) {
255                                 fprintf(stdout,"ERROR: Can't get on system bus");
256                                 return;
257                         }
258
259                         obex_proxy = __bluetooth_create_obex_proxy(sys_conn);
260                         if (!obex_proxy){
261                                 fprintf(stdout,"create new obex_proxy failed\n");
262                                 return;
263                         }
264
265                         __display_notification( __notify_push_authorize_request_accept_cb, __notify_push_authorize_request_cancel_cb,obex_proxy);
266                 } else if (!strcasecmp(event_type, "confirm-overwrite-request")) {
267                         /* Not implemented */
268                         fprintf(stdout," Not implemented\n");
269
270                 } else if (!strcasecmp(event_type, "keyboard-passkey-request")) {
271                         /* Not implemented */
272                         fprintf(stdout," Not implemented\n");
273
274                 } else if (!strcasecmp(event_type, "bt-information")) {
275                         /* Not implemented */
276                         fprintf(stdout," Not implemented\n");
277
278                 } else if (!strcasecmp(event_type, "exchange-request")) {
279                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
280                         agent_path = bundle_get_val(user_data, "agent-path");
281
282                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
283                         if (sys_conn == NULL) {
284                                 fprintf(stdout,"ERROR: Can't get on system bus");
285                                 return;
286                         }
287
288                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
289                         if (!agent_proxy){
290                                 fprintf(stdout,"create new agent_proxy failed\n");
291                                 return;
292                         }
293
294                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
295                 } else if (!strcasecmp(event_type, "phonebook-request")) {
296                         device_name = bundle_get_val(user_data, "device-name");
297                         agent_path = bundle_get_val(user_data, "agent-path");
298
299                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
300                         if (sys_conn == NULL) {
301                                 fprintf(stdout,"ERROR: Can't get on system bus");
302                                 return;
303                         }
304
305                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
306                         if (!agent_proxy){
307                                 fprintf(stdout,"create new agent_proxy failed\n");
308                                 return;
309                         }
310
311                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
312                 } else if (!strcasecmp(event_type, "message-request")) {
313                         device_name = bundle_get_val(user_data, "device-name");
314                         agent_path = bundle_get_val(user_data, "agent-path");
315
316                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
317                         if (sys_conn == NULL) {
318                                 fprintf(stdout,"ERROR: Can't get on system bus");
319                                 return;
320                         }
321
322                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
323                         if (!agent_proxy){
324                                 fprintf(stdout,"create new agent_proxy failed\n");
325                                 return;
326                         }
327
328                         __display_notification( __notify_authorize_request_accept_cb,  __notify_authorize_request_cancel_cb,agent_proxy);
329                 }
330         }
331         if (notification_list != NULL) {
332                  notification_free_list(notification_list);
333                  notification_list = NULL;
334         }
335
336         return;
337 }
338
339 int
340 main(int argc, char **argv)
341 {
342     if (!ecore_init()) {
343         LOGE("ERROR: Cannot init Ecore!\n");
344         return -1;
345     }
346
347     notification_resister_changed_cb(__noti_changed_cb, NULL);
348     ecore_main_loop_begin();
349
350  shutdown:
351     ecore_shutdown();
352     return 1;
353 }
354