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