Change DD_LIST_FOREACH to DD_LIST_FOREACH_SAFE to protect crash
[platform/core/api/device.git] / src / callback.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <vconf.h>
23 #include <gio/gio.h>
24
25 #include "callback.h"
26 #include "battery.h"
27 #include "display.h"
28 #include "common.h"
29 #include "dbus.h"
30 #include "list.h"
31
32 #define SIGNAL_FLASH_STATE  "ChangeFlashState"
33
34 struct device_cb_info {
35         device_changed_cb cb;
36         void *data;
37 };
38
39 static dd_list *device_cb_list[DEVICE_CALLBACK_MAX];
40 static int flash_sigid;
41
42 //LCOV_EXCL_START Not called Callback
43 static void battery_capacity_cb(keynode_t *key, void *data)
44 {
45         static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
46         struct device_cb_info *cb_info;
47         dd_list *elem, *elem_next;
48         int val;
49
50         val = vconf_keynode_get_int(key);
51
52         /* invoke the each callback with value */
53         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
54                 cb_info->cb(type, (void*)val, cb_info->data);
55 }
56 //LCOV_EXCL_STOP
57
58 //LCOV_EXCL_START Not called Callback
59 static void battery_charging_cb(keynode_t *key, void *data)
60 {
61         static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
62         struct device_cb_info *cb_info;
63         dd_list *elem, *elem_next;
64         int val;
65
66         val = vconf_keynode_get_int(key);
67
68         /* invoke the each callback with value */
69         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
70                 cb_info->cb(type, (void*)val, cb_info->data);
71 }
72 //LCOV_EXCL_STOP
73
74 //LCOV_EXCL_START Not called Callback
75 static void battery_level_cb(keynode_t *key, void *data)
76 {
77         static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
78         struct device_cb_info *cb_info;
79         dd_list *elem, *elem_next;
80         int val, status;
81
82         val = vconf_keynode_get_int(key);
83
84         if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
85                 status = DEVICE_BATTERY_LEVEL_EMPTY;
86         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
87                 status = DEVICE_BATTERY_LEVEL_CRITICAL;
88         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
89                 status = DEVICE_BATTERY_LEVEL_LOW;
90         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
91                 status = DEVICE_BATTERY_LEVEL_HIGH;
92         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
93                 status = DEVICE_BATTERY_LEVEL_FULL;
94         else
95                 status = -1;
96
97         /* invoke the each callback with value */
98         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
99                 cb_info->cb(type, (void*)status, cb_info->data);
100 }
101 //LCOV_EXCL_STOP
102
103 //LCOV_EXCL_START Not called Callback
104 static void display_changed_cb(keynode_t *key, void *data)
105 {
106         static device_callback_e type = DEVICE_CALLBACK_DISPLAY_STATE;
107         struct device_cb_info *cb_info;
108         dd_list *elem, *elem_next;
109         display_state_e state;
110         int val;
111
112         val = vconf_keynode_get_int(key);
113
114         switch (val) {
115         case 1: state = DISPLAY_STATE_NORMAL;
116                         break;
117         case 2: state = DISPLAY_STATE_SCREEN_DIM;
118                         break;
119         case 3: state = DISPLAY_STATE_SCREEN_OFF;
120                         break;
121         default: state = -1;
122                         break;
123         }
124
125         /* invoke the each callback with value */
126         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
127                 cb_info->cb(type, (void*)state, cb_info->data);
128 }
129 //LCOV_EXCL_STOP
130
131 //LCOV_EXCL_START Not called Callback
132 static void flash_state_cb(GDBusConnection *conn,
133                 const gchar *sender,
134                 const gchar *object,
135                 const gchar *interface,
136                 const gchar *signal,
137                 GVariant *parameters,
138                 gpointer user_data)
139 {
140         static int type = DEVICE_CALLBACK_FLASH_BRIGHTNESS;
141         struct device_cb_info *cb_info;
142         dd_list *elem, *elem_next;
143         int val;
144
145         if (strncmp(signal, SIGNAL_FLASH_STATE,
146                                 sizeof(SIGNAL_FLASH_STATE)) != 0) {
147                 _E("wrong parameter : signal(%s)", signal);
148                 return;
149         }
150
151         /* get camera value */
152         g_variant_get(parameters, "(i)", &val);
153         _D("%s - %d", signal, val);
154
155         /* invoke the each callback with value */
156         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
157                 cb_info->cb(type, (void*)val, cb_info->data);
158 }
159 //LCOV_EXCL_STOP
160
161 static int register_signal(const char *bus_name,
162                 const char *object_path,
163                 const char *interface_name,
164                 const char *signal,
165                 GDBusSignalCallback callback,
166                 int *sig_id)
167 {
168         GError *err = NULL;
169         GDBusConnection *conn;
170         int id;
171
172 #if !GLIB_CHECK_VERSION(2, 35, 0)
173         g_type_init();
174 #endif
175
176         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
177         if (!conn) {
178                 _E("fail to get dbus connection : %s", err->message); //LCOV_EXCL_LINE
179                 g_clear_error(&err); //LCOV_EXCL_LINE System Error
180                 return -EPERM;
181         }
182
183         /* subscribe signal */
184         id = g_dbus_connection_signal_subscribe(conn,
185                         bus_name,
186                         interface_name,
187                         signal,         /* null to match on all signals */
188                         object_path,
189                         NULL,           /* null to match on all kinds of arguments */
190                         G_DBUS_SIGNAL_FLAGS_NONE,
191                         callback,
192                         NULL,
193                         NULL);
194         if (id == 0) {
195                 _E("fail to connect %s signal", signal); //LCOV_EXCL_LINE
196                 return -EPERM;
197         }
198
199         if (sig_id)
200                 *sig_id = id;
201
202         return 0;
203 }
204
205 static int unregister_signal(int *sig_id)
206 {
207         GError *err = NULL;
208         GDBusConnection *conn;
209
210         if (!sig_id)
211                 return -EINVAL;
212
213         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
214         if (!conn) {
215                 _E("fail to get dbus connection : %s", err->message); //LCOV_EXCL_LINE
216                 g_clear_error(&err); //LCOV_EXCL_LINE System Error
217                 return -EPERM;
218         }
219
220         /* unsubscribe signal */
221         g_dbus_connection_signal_unsubscribe(conn, *sig_id);
222         *sig_id = 0;
223
224         return 0;
225 }
226
227 static int register_request(device_callback_e type)
228 {
229         switch (type) {
230         case DEVICE_CALLBACK_BATTERY_CAPACITY:
231                 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
232                                 battery_capacity_cb, NULL);
233         case DEVICE_CALLBACK_BATTERY_LEVEL:
234                 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
235                                 battery_level_cb, NULL);
236         case DEVICE_CALLBACK_BATTERY_CHARGING:
237                 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
238                                 battery_charging_cb, NULL);
239         case DEVICE_CALLBACK_DISPLAY_STATE:
240                 return vconf_notify_key_changed(VCONFKEY_PM_STATE,
241                                 display_changed_cb, NULL);
242         case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
243                 /* sig_id begins with 1. */
244                 if (flash_sigid)
245                         return -EEXIST;
246                 return register_signal(DEVICED_BUS_NAME,
247                                 DEVICED_PATH_LED,
248                                 DEVICED_INTERFACE_LED,
249                                 SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid);
250         default:
251                 break;
252         }
253
254         return -EINVAL;
255 }
256
257 static int release_request(device_callback_e type)
258 {
259         switch (type) {
260         case DEVICE_CALLBACK_BATTERY_CAPACITY:
261                 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
262                                 battery_capacity_cb);
263         case DEVICE_CALLBACK_BATTERY_LEVEL:
264                 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
265                                 battery_level_cb);
266         case DEVICE_CALLBACK_BATTERY_CHARGING:
267                 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
268                                 battery_charging_cb);
269         case DEVICE_CALLBACK_DISPLAY_STATE:
270                 return vconf_ignore_key_changed(VCONFKEY_PM_STATE,
271                                 display_changed_cb);
272         case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
273                 if (!flash_sigid)
274                         return -ENOENT;
275                 return unregister_signal(&flash_sigid);
276         default:
277                 break;
278         }
279
280         return -EINVAL;
281 }
282
283 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
284 {
285         struct device_cb_info *cb_info;
286         dd_list *elem, *elem_next;
287         int ret, n;
288
289         if (type < 0 || type >= DEVICE_CALLBACK_MAX)
290                 return DEVICE_ERROR_INVALID_PARAMETER;
291
292         if (!cb)
293                 return DEVICE_ERROR_INVALID_PARAMETER;
294
295         /* check if it is the first request */
296         n = DD_LIST_LENGTH(device_cb_list[type]);
297         if (n == 0) {
298                 ret = register_request(type);
299                 if (ret < 0)
300                         return DEVICE_ERROR_OPERATION_FAILED;
301         }
302
303         /* check for the same request */
304         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
305                 if (cb_info->cb == cb)
306                         return DEVICE_ERROR_ALREADY_IN_PROGRESS;
307         }
308
309         /* add device changed callback to list (local) */
310         cb_info = malloc(sizeof(struct device_cb_info));
311         if (!cb_info)
312                 return DEVICE_ERROR_OPERATION_FAILED;
313
314         cb_info->cb = cb;
315         cb_info->data = data;
316
317         DD_LIST_APPEND(device_cb_list[type], cb_info);
318
319         return DEVICE_ERROR_NONE;
320 }
321
322 int device_remove_callback(device_callback_e type, device_changed_cb cb)
323 {
324         struct device_cb_info *cb_info;
325         dd_list *elem, *elem_next;
326         int ret, n;
327
328         if (type < 0 || type >= DEVICE_CALLBACK_MAX)
329                 return DEVICE_ERROR_INVALID_PARAMETER;
330
331         if (!cb)
332                 return DEVICE_ERROR_INVALID_PARAMETER;
333
334         /* search for the same element with callback */
335         DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
336                 if (cb_info->cb == cb)
337                         break;
338         }
339
340         if (!cb_info)
341                 return DEVICE_ERROR_INVALID_PARAMETER;
342
343         /* remove device callback from list (local) */
344         DD_LIST_REMOVE(device_cb_list[type], cb_info);
345         free(cb_info);
346
347         /* check if this callback is last element */
348         n = DD_LIST_LENGTH(device_cb_list[type]);
349         if (n == 0) {
350                 ret = release_request(type);
351                 if (ret < 0)
352                         return DEVICE_ERROR_OPERATION_FAILED;
353         }
354
355         return DEVICE_ERROR_NONE;
356 }