tizen 2.3 release
[framework/system/deviced.git] / src / tima / noti.c
1 /*
2  * TIMA Notification
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <assert.h>
21 #include <E_DBus.h>
22
23 #include "core/log.h"
24 #include "core/common.h"
25
26 #define SYSTEM_POPUP_BUS_NAME   "org.tizen.system.popup"
27 #define SYSTEM_POPUP_DBUS_PATH  "/Org/Tizen/System/Popup"
28
29 #define SYSTEM_POPUP_PATH_TIMA  SYSTEM_POPUP_DBUS_PATH"/Tima"
30 #define SYSTEM_POPUP_IFACE_TIMA SYSTEM_POPUP_BUS_NAME".Tima"
31
32 #define METHOD_PKM_NOTI_ON      "PKMDetectionNotiOn"
33 #define METHOD_PKM_NOTI_OFF     "PKMDetectionNotiOff"
34 #define METHOD_LKM_NOTI_ON      "LKMPreventionNotiOn"
35 #define METHOD_LKM_NOTI_OFF     "LKMPreventionNotiOff"
36
37 #define RETRY_MAX               10
38 #define DBUS_REPLY_TIMEOUT      (120 * 1000)
39
40 static int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
41 {
42         char *ch;
43         int i, int_type;
44
45         if (!sig || !param)
46                 return 0;
47
48         for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
49                 switch (*ch) {
50                 case 's':
51                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
52                         break;
53                 case 'i':
54                         int_type = atoi(param[i]);
55                         dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
56                         break;
57                 default:
58                         _E("ERROR: %s %c", sig, *ch);
59                         return -EINVAL;
60                 }
61         }
62         return 0;
63 }
64
65 DBusMessage *call_dbus_method(const char *dest, const char *path,
66                 const char *interface, const char *method,
67                 const char *sig, char *param[])
68 {
69         DBusConnection *conn;
70         DBusMessage *msg = NULL;
71         DBusMessageIter iter;
72         DBusMessage *ret;
73         DBusError err;
74         int r;
75
76         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
77         if (!conn) {
78                 ret = NULL;
79                 goto out;
80         }
81
82         msg = dbus_message_new_method_call(dest, path, interface, method);
83         if (!msg) {
84                 ret = NULL;
85                 goto out;
86         }
87
88         dbus_message_iter_init_append(msg, &iter);
89         r = append_variant(&iter, sig, param);
90         if (r < 0) {
91                 ret = NULL;
92                 goto out;
93         }
94
95         /*This function is for synchronous dbus method call */
96         dbus_error_init(&err);
97         ret = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
98         dbus_error_free(&err);
99
100 out:
101         dbus_message_unref(msg);
102
103         return ret;
104 }
105
106 int request_to_launch_by_dbus(char *bus, char *path, char *iface,
107                 char *method, char *ptype, char *param[])
108 {
109         DBusMessage *msg;
110         DBusError err;
111         int i, r, ret_val;
112
113         i = 0;
114         do {
115                 msg = call_dbus_method(bus, path, iface, method, ptype, param);
116                 if (msg)
117                         break;
118                 i++;
119         } while (i < RETRY_MAX);
120         if (!msg) {
121                 _E("fail to call dbus method");
122                 return -ECONNREFUSED;
123         }
124
125         dbus_error_init(&err);
126
127         r = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
128         if (!r) {
129                 _E("no message : [%s:%s]", err.name, err.message);
130                 ret_val = -EBADMSG;
131         }
132
133         dbus_message_unref(msg);
134         dbus_error_free(&err);
135
136         return ret_val;
137 }
138
139 /* This function returns notification ID (ID >= 0) */
140 int tima_lkm_prevention_noti_on(void)
141 {
142         return request_to_launch_by_dbus(SYSTEM_POPUP_BUS_NAME,
143                         SYSTEM_POPUP_PATH_TIMA, SYSTEM_POPUP_IFACE_TIMA,
144                         METHOD_LKM_NOTI_ON, NULL, NULL);
145 }
146
147 /* This function needs the notification ID to remove
148  * This function return 0 if success */
149 int tima_lkm_prevention_noti_off(int noti_id)
150 {
151         char *param[1];
152         char str[8];
153         snprintf(str, sizeof(str), "%d", noti_id);
154         param[0] = str;
155
156         return request_to_launch_by_dbus(SYSTEM_POPUP_BUS_NAME,
157                         SYSTEM_POPUP_PATH_TIMA, SYSTEM_POPUP_IFACE_TIMA,
158                         METHOD_LKM_NOTI_OFF, "i", param);
159 }
160
161 /* This function returns notification ID (ID >= 0) */
162 int tima_pkm_detection_noti_on(void)
163 {
164         return request_to_launch_by_dbus(SYSTEM_POPUP_BUS_NAME,
165                         SYSTEM_POPUP_PATH_TIMA, SYSTEM_POPUP_IFACE_TIMA,
166                         METHOD_PKM_NOTI_ON, NULL, NULL);
167 }
168
169 /* This function needs the notification ID to remove
170  * This function return 0 if success */
171 int tima_pkm_detection_noti_off(int noti_id)
172 {
173         char *param[1];
174         char str[8];
175         snprintf(str, sizeof(str), "%d", noti_id);
176         param[0] = str;
177
178         return request_to_launch_by_dbus(SYSTEM_POPUP_BUS_NAME,
179                         SYSTEM_POPUP_PATH_TIMA, SYSTEM_POPUP_IFACE_TIMA,
180                         METHOD_PKM_NOTI_OFF, "i", param);
181 }