2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <libsyscommon/dbus-system.h>
32 #define SIGNAL_FLASH_STATE "ChangeFlashState"
34 struct device_cb_info {
39 static dd_list *device_cb_list[DEVICE_CALLBACK_MAX];
40 static int flash_sigid;
42 //LCOV_EXCL_START Not called Callback
43 static void battery_capacity_cb(keynode_t *key, void *data)
45 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
46 struct device_cb_info *cb_info;
47 dd_list *elem, *elem_next;
50 val = vconf_keynode_get_int(key);
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);
58 //LCOV_EXCL_START Not called Callback
59 static void battery_charging_cb(keynode_t *key, void *data)
61 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
62 struct device_cb_info *cb_info;
63 dd_list *elem, *elem_next;
66 val = vconf_keynode_get_int(key);
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);
74 //LCOV_EXCL_START Not called Callback
75 static void battery_level_cb(keynode_t *key, void *data)
77 static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
78 struct device_cb_info *cb_info;
79 dd_list *elem, *elem_next;
82 val = vconf_keynode_get_int(key);
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;
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);
103 //LCOV_EXCL_START Not called Callback
104 static void display_changed_cb(keynode_t *key, void *data)
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;
112 val = vconf_keynode_get_int(key);
115 case 1: state = DISPLAY_STATE_NORMAL;
117 case 2: state = DISPLAY_STATE_SCREEN_DIM;
119 case 3: state = DISPLAY_STATE_SCREEN_OFF;
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);
131 //LCOV_EXCL_START Not called Callback
132 static void flash_state_cb(GDBusConnection *conn,
135 const gchar *interface,
137 GVariant *parameters,
140 static int type = DEVICE_CALLBACK_FLASH_BRIGHTNESS;
141 struct device_cb_info *cb_info;
142 dd_list *elem, *elem_next;
145 if (strncmp(signal, SIGNAL_FLASH_STATE,
146 sizeof(SIGNAL_FLASH_STATE)) != 0) {
147 _E("wrong parameter : signal(%s)", signal);
151 /* get camera value */
152 g_variant_get(parameters, "(i)", &val);
153 _D("%s - %d", signal, val);
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);
161 static int register_signal(const char *bus_name,
162 const char *object_path,
163 const char *interface_name,
165 GDBusSignalCallback callback,
169 GDBusConnection *conn;
172 #if !GLIB_CHECK_VERSION(2, 35, 0)
176 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
178 //LCOV_EXCL_START System Error
179 _E("fail to get dbus connection : %s", err->message);
185 /* subscribe signal */
186 id = g_dbus_connection_signal_subscribe(conn,
189 signal, /* null to match on all signals */
191 NULL, /* null to match on all kinds of arguments */
192 G_DBUS_SIGNAL_FLAGS_NONE,
197 //LCOV_EXCL_START System Error
198 _E("fail to connect %s signal", signal);
209 static int unregister_signal(int *sig_id)
212 GDBusConnection *conn;
217 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
219 //LCOV_EXCL_START System Error
220 _E("fail to get dbus connection : %s", err->message);
226 /* unsubscribe signal */
227 g_dbus_connection_signal_unsubscribe(conn, *sig_id);
233 static int register_request(device_callback_e type)
236 case DEVICE_CALLBACK_BATTERY_CAPACITY:
237 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
238 battery_capacity_cb, NULL);
239 case DEVICE_CALLBACK_BATTERY_LEVEL:
240 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
241 battery_level_cb, NULL);
242 case DEVICE_CALLBACK_BATTERY_CHARGING:
243 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
244 battery_charging_cb, NULL);
245 case DEVICE_CALLBACK_DISPLAY_STATE:
246 return vconf_notify_key_changed(VCONFKEY_PM_STATE,
247 display_changed_cb, NULL);
248 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
249 /* sig_id begins with 1. */
252 return register_signal(DEVICED_BUS_NAME,
254 DEVICED_INTERFACE_LED,
255 SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid);
263 static int release_request(device_callback_e type)
266 case DEVICE_CALLBACK_BATTERY_CAPACITY:
267 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
268 battery_capacity_cb);
269 case DEVICE_CALLBACK_BATTERY_LEVEL:
270 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
272 case DEVICE_CALLBACK_BATTERY_CHARGING:
273 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
274 battery_charging_cb);
275 case DEVICE_CALLBACK_DISPLAY_STATE:
276 return vconf_ignore_key_changed(VCONFKEY_PM_STATE,
278 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
281 return unregister_signal(&flash_sigid);
289 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
291 struct device_cb_info *cb_info;
292 dd_list *elem, *elem_next;
295 if (!is_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE)
296 return DEVICE_ERROR_NOT_SUPPORTED;
298 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
299 return DEVICE_ERROR_INVALID_PARAMETER;
302 return DEVICE_ERROR_INVALID_PARAMETER;
304 /* check if it is the first request */
305 n = DD_LIST_LENGTH(device_cb_list[type]);
307 ret = register_request(type);
309 return DEVICE_ERROR_OPERATION_FAILED;
312 /* check for the same request */
313 DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
314 if (cb_info->cb == cb)
315 return DEVICE_ERROR_ALREADY_IN_PROGRESS;
318 /* add device changed callback to list (local) */
319 cb_info = malloc(sizeof(struct device_cb_info));
321 return DEVICE_ERROR_OPERATION_FAILED;
324 cb_info->data = data;
326 DD_LIST_APPEND(device_cb_list[type], cb_info);
328 return DEVICE_ERROR_NONE;
331 int device_remove_callback(device_callback_e type, device_changed_cb cb)
333 struct device_cb_info *cb_info;
334 dd_list *elem, *elem_next;
337 if (!is_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE)
338 return DEVICE_ERROR_NOT_SUPPORTED;
340 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
341 return DEVICE_ERROR_INVALID_PARAMETER;
344 return DEVICE_ERROR_INVALID_PARAMETER;
346 /* search for the same element with callback */
347 DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
348 if (cb_info->cb == cb)
353 return DEVICE_ERROR_INVALID_PARAMETER;
355 /* remove device callback from list (local) */
356 DD_LIST_REMOVE(device_cb_list[type], cb_info);
359 /* check if this callback is last element */
360 n = DD_LIST_LENGTH(device_cb_list[type]);
362 ret = release_request(type);
364 return DEVICE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE System Error
367 return DEVICE_ERROR_NONE;