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.
31 struct device_cb_info {
36 static dd_list *device_cb_list[DEVICE_CALLBACK_MAX];
38 static void battery_capacity_cb(keynode_t *key, void *data)
40 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
41 struct device_cb_info *cb_info;
45 val = vconf_keynode_get_int(key);
47 /* invoke the each callback with value */
48 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
49 cb_info->cb(type, (void*)val, cb_info->data);
52 static void battery_charging_cb(keynode_t *key, void *data)
54 static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
55 struct device_cb_info *cb_info;
59 val = vconf_keynode_get_int(key);
61 /* invoke the each callback with value */
62 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
63 cb_info->cb(type, (void*)val, cb_info->data);
66 static void battery_level_cb(keynode_t *key, void *data)
68 static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
69 struct device_cb_info *cb_info;
73 val = vconf_keynode_get_int(key);
75 if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
76 status = DEVICE_BATTERY_LEVEL_EMPTY;
77 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
78 status = DEVICE_BATTERY_LEVEL_CRITICAL;
79 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
80 status = DEVICE_BATTERY_LEVEL_LOW;
81 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
82 status = DEVICE_BATTERY_LEVEL_HIGH;
83 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
84 status = DEVICE_BATTERY_LEVEL_FULL;
88 /* invoke the each callback with value */
89 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
90 cb_info->cb(type, (void*)status, cb_info->data);
93 static void display_changed_cb(keynode_t *key, void *data)
95 static device_callback_e type = DEVICE_CALLBACK_DISPLAY_STATE;
96 struct device_cb_info *cb_info;
98 display_state_e state;
101 val = vconf_keynode_get_int(key);
104 case 1: state = DISPLAY_STATE_NORMAL;
106 case 2: state = DISPLAY_STATE_SCREEN_DIM;
108 case 3: state = DISPLAY_STATE_SCREEN_OFF;
114 /* invoke the each callback with value */
115 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
116 cb_info->cb(type, (void*)state, cb_info->data);
119 static int register_request(device_callback_e type)
122 case DEVICE_CALLBACK_BATTERY_CAPACITY:
123 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
124 battery_capacity_cb, NULL);
125 case DEVICE_CALLBACK_BATTERY_LEVEL:
126 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
127 battery_level_cb, NULL);
128 case DEVICE_CALLBACK_BATTERY_CHARGING:
129 return vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
130 battery_charging_cb, NULL);
131 case DEVICE_CALLBACK_DISPLAY_STATE:
132 return vconf_notify_key_changed(VCONFKEY_PM_STATE,
133 display_changed_cb, NULL);
141 static int release_request(device_callback_e type)
144 case DEVICE_CALLBACK_BATTERY_CAPACITY:
145 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
146 battery_capacity_cb);
147 case DEVICE_CALLBACK_BATTERY_LEVEL:
148 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
150 case DEVICE_CALLBACK_BATTERY_CHARGING:
151 return vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
152 battery_charging_cb);
153 case DEVICE_CALLBACK_DISPLAY_STATE:
154 return vconf_ignore_key_changed(VCONFKEY_PM_STATE,
163 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
165 struct device_cb_info *cb_info;
169 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
170 return DEVICE_ERROR_INVALID_PARAMETER;
173 return DEVICE_ERROR_INVALID_PARAMETER;
175 /* check if it is the first request */
176 n = DD_LIST_LENGTH(device_cb_list[type]);
178 ret = register_request(type);
180 return DEVICE_ERROR_OPERATION_FAILED;
183 /* check for the same request */
184 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
185 if (cb_info->cb == cb)
186 return DEVICE_ERROR_ALREADY_IN_PROGRESS;
189 /* add device changed callback to list (local) */
190 cb_info = malloc(sizeof(struct device_cb_info));
192 return DEVICE_ERROR_OPERATION_FAILED;
195 cb_info->data = data;
197 DD_LIST_APPEND(device_cb_list[type], cb_info);
199 return DEVICE_ERROR_NONE;
202 int device_remove_callback(device_callback_e type, device_changed_cb cb)
204 struct device_cb_info *cb_info;
208 if (type < 0 || type >= DEVICE_CALLBACK_MAX)
209 return DEVICE_ERROR_INVALID_PARAMETER;
212 return DEVICE_ERROR_INVALID_PARAMETER;
214 /* search for the same element with callback */
215 DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
216 if (cb_info->cb == cb)
221 return DEVICE_ERROR_INVALID_PARAMETER;
223 /* remove device callback from list (local) */
224 DD_LIST_REMOVE(device_cb_list[type], cb_info);
227 /* check if this callback is last element */
228 n = DD_LIST_LENGTH(device_cb_list[type]);
230 ret = release_request(type);
232 return DEVICE_ERROR_OPERATION_FAILED;
235 return DEVICE_ERROR_NONE;