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.
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;
50 val = vconf_keynode_get_int(key);
52 /* invoke the each callback with value */
53 DD_LIST_FOREACH(device_cb_list[type], elem, 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;
66 val = vconf_keynode_get_int(key);
68 /* invoke the each callback with value */
69 DD_LIST_FOREACH(device_cb_list[type], elem, 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;
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(device_cb_list[type], elem, 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;
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(device_cb_list[type], elem, 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;
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(device_cb_list[type], elem, 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 _E("fail to get dbus connection : %s", err->message); //LCOV_EXCL_LINE
179 g_clear_error(&err); //LCOV_EXCL_LINE System Error
183 /* subscribe signal */
184 id = g_dbus_connection_signal_subscribe(conn,
187 signal, /* null to match on all signals */
189 NULL, /* null to match on all kinds of arguments */
190 G_DBUS_SIGNAL_FLAGS_NONE,
195 _E("fail to connect %s signal", signal); //LCOV_EXCL_LINE
205 static int unregister_signal(int *sig_id)
208 GDBusConnection *conn;
213 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
215 _E("fail to get dbus connection : %s", err->message); //LCOV_EXCL_LINE
216 g_clear_error(&err); //LCOV_EXCL_LINE System Error
220 /* unsubscribe signal */
221 g_dbus_connection_signal_unsubscribe(conn, *sig_id);
227 static int register_request(device_callback_e 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. */
246 return register_signal(DEVICED_BUS_NAME,
248 DEVICED_INTERFACE_LED,
249 SIGNAL_FLASH_STATE, flash_state_cb, &flash_sigid);
257 static int release_request(device_callback_e 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,
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,
272 case DEVICE_CALLBACK_FLASH_BRIGHTNESS:
275 return unregister_signal(&flash_sigid);
283 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
285 struct device_cb_info *cb_info;
289 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
290 return DEVICE_ERROR_INVALID_PARAMETER;
293 return DEVICE_ERROR_INVALID_PARAMETER;
295 /* check if it is the first request */
296 n = DD_LIST_LENGTH(device_cb_list[type]);
298 ret = register_request(type);
300 return DEVICE_ERROR_OPERATION_FAILED;
303 /* check for the same request */
304 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
305 if (cb_info->cb == cb)
306 return DEVICE_ERROR_ALREADY_IN_PROGRESS;
309 /* add device changed callback to list (local) */
310 cb_info = malloc(sizeof(struct device_cb_info));
312 return DEVICE_ERROR_OPERATION_FAILED;
315 cb_info->data = data;
317 DD_LIST_APPEND(device_cb_list[type], cb_info);
319 return DEVICE_ERROR_NONE;
322 int device_remove_callback(device_callback_e type, device_changed_cb cb)
324 struct device_cb_info *cb_info;
328 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
329 return DEVICE_ERROR_INVALID_PARAMETER;
332 return DEVICE_ERROR_INVALID_PARAMETER;
334 /* search for the same element with callback */
335 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
336 if (cb_info->cb == cb)
341 return DEVICE_ERROR_INVALID_PARAMETER;
343 /* remove device callback from list (local) */
344 DD_LIST_REMOVE(device_cb_list[type], cb_info);
347 /* check if this callback is last element */
348 n = DD_LIST_LENGTH(device_cb_list[type]);
350 ret = release_request(type);
352 return DEVICE_ERROR_OPERATION_FAILED;
355 return DEVICE_ERROR_NONE;