Add Bluetooth-client-notification to test bluetooth pairing
[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[256];
104
105          fprintf(stdout, "Do you confirm yes or no ? ");
106          while ( bt_yesno != 0){
107                  gets(line);
108                  if ( strcmp(line,"yes") == 0){
109                          (cb_1) (proxy);
110                          bt_yesno = 0;
111                  } else if ( strcmp(line,"no") == 0){
112                          (cb_2) (proxy);
113                          bt_yesno = 0;
114                  } else {
115                          fprintf(stdout," yes or no :");
116                  }
117          }
118          err = notification_delete_all_by_type("bluetooth-frwk-bt-service", NOTIFICATION_TYPE_NOTI);
119          if (err != NOTIFICATION_ERROR_NONE) {
120                   fprintf(stdout, "Unable to remove notifications");
121          }
122
123 }
124
125 static int __noti_changed_cb(void *data, notification_type_e type)
126 {
127          notification_h noti = NULL;
128          notification_list_h notification_list = NULL;
129          notification_list_h get_list = NULL;
130          int count = 0, group_id = 0, priv_id = 0, show_noti = 0, num = 1;
131          char *pkgname = NULL;
132          char *title = NULL;
133          char *str_count = NULL;
134          char *content= NULL;
135          bundle *user_data = NULL;
136          DBusGConnection *sys_conn;
137          DBusGProxy *agent_proxy;
138          DBusGProxy *obex_proxy;
139
140
141          const char *device_name = NULL;
142          const char *passkey = NULL;
143          const char *file = NULL;
144          const char *agent_path;
145          const char *event_type = NULL;
146
147          notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &notification_list);
148          if (notification_list) {
149                 get_list = notification_list_get_head(notification_list);
150                 noti = notification_list_get_data(get_list);
151                 notification_get_id(noti, &group_id, &priv_id);
152                 notification_get_pkgname(noti, &pkgname);
153                 if(pkgname == NULL){
154                        notification_get_application(noti, &pkgname);
155                 }
156
157                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &str_count);
158                 if (!str_count) {
159                         count = 0;
160                 } else {
161                         count = atoi(str_count);
162                 }
163                 notification_get_title(noti, &title, NULL);
164                 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT , &content);
165                 notification_get_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH,NULL, &user_data);
166
167                 fprintf(stdout, "NOTIFICATION: %s - %s - %s - %i - %i \n", pkgname, title, content, count, num);
168
169                 event_type = bundle_get_val(user_data, "event-type");
170
171                 if(!strcasecmp(event_type, "pin-request")) {
172                         /* Not implemented */
173                         fprintf(stdout," Not implemented\n");
174
175                 } else if (!strcasecmp(event_type, "passkey-confirm-request")){
176                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
177                         passkey = (gchar*) bundle_get_val(user_data, "passkey");
178                         agent_path = bundle_get_val(user_data, "agent-path");
179
180                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
181                         if (sys_conn == NULL) {
182                                 fprintf(stdout,"ERROR: Can't get on system bus");
183                                 return EXIT_FAILURE;
184                         }
185
186                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
187                         if (!agent_proxy){
188                                 fprintf(stdout,"create new agent_proxy failed\n");
189                                 return EXIT_FAILURE;
190                         }
191
192                          __display_notification(__notify_passkey_confirm_request_accept_cb, __notify_passkey_confirm_request_cancel_cb,agent_proxy);
193                 } else if (!strcasecmp(event_type, "passkey-request")) {
194                         /* Not implemented */
195                         fprintf(stdout," Not implemented\n");
196
197                 } else if (!strcasecmp(event_type, "passkey-display-request")) {
198                         /* Not implemented */
199                         fprintf(stdout," Not implemented\n");
200
201                 } else if (!strcasecmp(event_type, "authorize-request")) {
202                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
203                         agent_path = bundle_get_val(user_data, "agent-path");
204
205                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
206                         if (sys_conn == NULL) {
207                                 fprintf(stdout,"ERROR: Can't get on system bus");
208                                 return EXIT_FAILURE;
209                         }
210
211                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
212                         if (!agent_proxy){
213                                 fprintf(stdout,"create new agent_proxy failed\n");
214                                 return EXIT_FAILURE;
215                         }
216
217                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
218                 } else if (!strcasecmp(event_type, "app-confirm-request")) {
219                         /* Not implemented */
220                         fprintf(stdout," Not implemented\n");
221
222                 } else if (!strcasecmp(event_type, "push-authorize-request")) {
223                         file = (gchar*) bundle_get_val(user_data, "file");
224                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
225
226                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
227                         if (sys_conn == NULL) {
228                                 fprintf(stdout,"ERROR: Can't get on system bus");
229                                 return EXIT_FAILURE;
230                         }
231
232                         obex_proxy = __bluetooth_create_obex_proxy(sys_conn);
233                         if (!obex_proxy){
234                                 fprintf(stdout,"create new obex_proxy failed\n");
235                                 return EXIT_FAILURE;
236                         }
237
238                         __display_notification( __notify_push_authorize_request_accept_cb, __notify_push_authorize_request_cancel_cb,obex_proxy);
239                 } else if (!strcasecmp(event_type, "confirm-overwrite-request")) {
240                         /* Not implemented */
241                         fprintf(stdout," Not implemented\n");
242
243                 } else if (!strcasecmp(event_type, "keyboard-passkey-request")) {
244                         /* Not implemented */
245                         fprintf(stdout," Not implemented\n");
246
247                 } else if (!strcasecmp(event_type, "bt-information")) {
248                         /* Not implemented */
249                         fprintf(stdout," Not implemented\n");
250
251                 } else if (!strcasecmp(event_type, "exchange-request")) {
252                         device_name = (gchar*) bundle_get_val(user_data, "device-name");
253                         agent_path = bundle_get_val(user_data, "agent-path");
254
255                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
256                         if (sys_conn == NULL) {
257                                 fprintf(stdout,"ERROR: Can't get on system bus");
258                                 return EXIT_FAILURE;
259                         }
260
261                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
262                         if (!agent_proxy){
263                                 fprintf(stdout,"create new agent_proxy failed\n");
264                                 return EXIT_FAILURE;
265                         }
266
267                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
268                 } else if (!strcasecmp(event_type, "phonebook-request")) {
269                         device_name = bundle_get_val(user_data, "device-name");
270                         agent_path = bundle_get_val(user_data, "agent-path");
271
272                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
273                         if (sys_conn == NULL) {
274                                 fprintf(stdout,"ERROR: Can't get on system bus");
275                                 return EXIT_FAILURE;
276                         }
277
278                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
279                         if (!agent_proxy){
280                                 fprintf(stdout,"create new agent_proxy failed\n");
281                                 return EXIT_FAILURE;
282                         }
283
284                         __display_notification( __notify_authorize_request_accept_cb, __notify_authorize_request_cancel_cb,agent_proxy);
285                 } else if (!strcasecmp(event_type, "message-request")) {
286                         device_name = bundle_get_val(user_data, "device-name");
287                         agent_path = bundle_get_val(user_data, "agent-path");
288
289                         sys_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
290                         if (sys_conn == NULL) {
291                                 fprintf(stdout,"ERROR: Can't get on system bus");
292                                 return EXIT_FAILURE;
293                         }
294
295                         agent_proxy = __bluetooth_create_agent_proxy(sys_conn, agent_path);
296                         if (!agent_proxy){
297                                 fprintf(stdout,"create new agent_proxy failed\n");
298                                 return EXIT_FAILURE;
299                         }
300
301                         __display_notification( __notify_authorize_request_accept_cb,  __notify_authorize_request_cancel_cb,agent_proxy);
302                 }
303         }
304         if (notification_list != NULL) {
305                  notification_free_list(notification_list);
306                  notification_list = NULL;
307         }
308
309         return EXIT_SUCCESS;
310 }
311
312 int
313 main(int argc, char **argv)
314 {
315     if (!ecore_init()) {
316         fprintf(stderr, "ERROR: Cannot init Ecore!\n");
317         return -1;
318     }
319
320     notification_resister_changed_cb(__noti_changed_cb, NULL);
321     ecore_main_loop_begin();
322
323  shutdown:
324     ecore_shutdown();
325     return 0;
326 }
327