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/libgdbus.h>
25 #include <libsyscommon/list.h>
30 #include "display-internal.h"
33 #define SIGNAL_FLASH_STATE "ChangeFlashState"
35 struct device_cb_info {
40 static GList *device_cb_list[DEVICE_CALLBACK_MAX];
41 static int flash_sigid;
43 //LCOV_EXCL_START Not called Callback
44 static void battery_capacity_cb(keynode_t *key, void *data)
46 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
47 struct device_cb_info *cb_info;
48 GList *elem, *elem_next;
51 val = vconf_keynode_get_int(key);
53 /* invoke the each callback with value */
54 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
55 cb_info->cb(type, (void*)val, cb_info->data);
59 //LCOV_EXCL_START Not called Callback
60 static void battery_charging_cb(keynode_t *key, void *data)
62 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
63 struct device_cb_info *cb_info;
64 GList *elem, *elem_next;
67 val = vconf_keynode_get_int(key);
69 /* invoke the each callback with value */
70 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
71 cb_info->cb(type, (void*)val, cb_info->data);
75 //LCOV_EXCL_START Not called Callback
76 static void battery_level_cb(keynode_t *key, void *data)
78 static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
79 struct device_cb_info *cb_info;
80 GList *elem, *elem_next;
83 val = vconf_keynode_get_int(key);
85 if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
86 status = DEVICE_BATTERY_LEVEL_EMPTY;
87 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
88 status = DEVICE_BATTERY_LEVEL_CRITICAL;
89 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
90 status = DEVICE_BATTERY_LEVEL_LOW;
91 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
92 status = DEVICE_BATTERY_LEVEL_HIGH;
93 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
94 status = DEVICE_BATTERY_LEVEL_FULL;
98 /* invoke the each callback with value */
99 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
100 cb_info->cb(type, (void*)status, cb_info->data);
104 //LCOV_EXCL_START Not called Callback
105 static void display_changed_cb(keynode_t *key, void *data)
107 static device_callback_e type = DEVICE_CALLBACK_DISPLAY_STATE;
108 struct device_cb_info *cb_info;
109 GList *elem, *elem_next;
110 display_state_e state;
113 val = vconf_keynode_get_int(key);
116 case 1: state = DISPLAY_STATE_NORMAL;
118 case 2: state = DISPLAY_STATE_SCREEN_DIM;
120 case 3: state = DISPLAY_STATE_SCREEN_OFF;
126 /* invoke the each callback with value */
127 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
128 cb_info->cb(type, (void*)state, cb_info->data);
132 //LCOV_EXCL_START Not called Callback
133 static void flash_state_cb(GDBusConnection *conn,
136 const gchar *interface,
138 GVariant *parameters,
141 static int type = DEVICE_CALLBACK_FLASH_BRIGHTNESS;
142 struct device_cb_info *cb_info;
143 GList *elem, *elem_next;
146 if (strncmp(signal, SIGNAL_FLASH_STATE,
147 sizeof(SIGNAL_FLASH_STATE)) != 0) {
148 _E("wrong parameter : signal(%s)", signal);
152 /* get camera value */
153 g_variant_get(parameters, "(i)", &val);
154 _D("%s - %d", signal, val);
156 /* invoke the each callback with value */
157 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
158 cb_info->cb(type, (void*)val, cb_info->data);
162 static int register_signal(const char *bus_name,
163 const char *object_path,
164 const char *interface_name,
166 GDBusSignalCallback callback,
170 GDBusConnection *conn;
173 #if !GLIB_CHECK_VERSION(2, 35, 0)
177 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
179 //LCOV_EXCL_START System Error
180 _E("fail to get dbus connection : %s", err->message);
186 /* subscribe signal */
187 id = g_dbus_connection_signal_subscribe(conn,
190 signal, /* null to match on all signals */
192 NULL, /* null to match on all kinds of arguments */
193 G_DBUS_SIGNAL_FLAGS_NONE,
198 //LCOV_EXCL_START System Error
199 _E("fail to connect %s signal", signal);
210 static int unregister_signal(int *sig_id)
213 GDBusConnection *conn;
218 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
220 //LCOV_EXCL_START System Error
221 _E("fail to get dbus connection : %s", err->message);
227 /* unsubscribe signal */
228 g_dbus_connection_signal_unsubscribe(conn, *sig_id);
234 static int register_request(device_callback_e type)
237 case DEVICE_CALLBACK_BATTERY_CAPACITY:
238 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
239 battery_capacity_cb, NULL);
240 case DEVICE_CALLBACK_BATTERY_LEVEL:
241 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
242 battery_level_cb, NULL);
243 case DEVICE_CALLBACK_BATTERY_CHARGING:
244 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
245 battery_charging_cb, NULL);
246 case DEVICE_CALLBACK_DISPLAY_STATE:
247 return vconf_notify_key_changed(VCONFKEY_PM_STATE,
248 display_changed_cb, NULL);
249 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
250 /* sig_id begins with 1. */
253 return register_signal(DEVICED_BUS_NAME,
255 DEVICED_INTERFACE_LED,
256 SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid);
264 static int release_request(device_callback_e type)
267 case DEVICE_CALLBACK_BATTERY_CAPACITY:
268 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
269 battery_capacity_cb);
270 case DEVICE_CALLBACK_BATTERY_LEVEL:
271 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
273 case DEVICE_CALLBACK_BATTERY_CHARGING:
274 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
275 battery_charging_cb);
276 case DEVICE_CALLBACK_DISPLAY_STATE:
277 return vconf_ignore_key_changed(VCONFKEY_PM_STATE,
279 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
282 return unregister_signal(&flash_sigid);
290 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
292 struct device_cb_info *cb_info;
293 GList *elem, *elem_next;
296 if (!is_feature_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE)
297 return DEVICE_ERROR_NOT_SUPPORTED;
299 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
300 return DEVICE_ERROR_INVALID_PARAMETER;
303 return DEVICE_ERROR_INVALID_PARAMETER;
305 /* check if it is the first request */
306 n = SYS_G_LIST_LENGTH(device_cb_list[type]);
308 ret_request = register_request(type);
310 return DEVICE_ERROR_OPERATION_FAILED;
313 /* check for the same request */
314 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
315 if (cb_info->cb == cb)
316 return DEVICE_ERROR_ALREADY_IN_PROGRESS;
319 /* add device changed callback to list (local) */
320 cb_info = malloc(sizeof(struct device_cb_info));
322 return DEVICE_ERROR_OPERATION_FAILED;
325 cb_info->data = data;
327 SYS_G_LIST_APPEND(device_cb_list[type], cb_info);
329 return DEVICE_ERROR_NONE;
332 int device_remove_callback(device_callback_e type, device_changed_cb cb)
334 struct device_cb_info *cb_info;
335 GList *elem, *elem_next;
338 if (!is_feature_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE)
339 return DEVICE_ERROR_NOT_SUPPORTED;
341 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
342 return DEVICE_ERROR_INVALID_PARAMETER;
345 return DEVICE_ERROR_INVALID_PARAMETER;
347 /* search for the same element with callback */
348 SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
349 if (cb_info->cb == cb)
354 return DEVICE_ERROR_INVALID_PARAMETER;
356 /* remove device callback from list (local) */
357 SYS_G_LIST_REMOVE(device_cb_list[type], cb_info);
360 /* check if this callback is last element */
361 n = SYS_G_LIST_LENGTH(device_cb_list[type]);
363 ret_request = release_request(type);
365 return DEVICE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE System Error
368 return DEVICE_ERROR_NONE;