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.
22 #include <system_settings.h>
28 const char *dir_path[STORAGE_DIRECTORY_MAX] = {
29 [STORAGE_DIRECTORY_IMAGES] = "Images",
30 [STORAGE_DIRECTORY_SOUNDS] = "Sounds",
31 [STORAGE_DIRECTORY_VIDEOS] = "Videos",
32 [STORAGE_DIRECTORY_CAMERA] = "Camera",
33 [STORAGE_DIRECTORY_DOWNLOADS] = "Downloads",
34 [STORAGE_DIRECTORY_MUSIC] = "Music",
35 [STORAGE_DIRECTORY_DOCUMENTS] = "Documents",
36 [STORAGE_DIRECTORY_OTHERS] = "Others",
37 [STORAGE_DIRECTORY_SYSTEM_RINGTONES] = "",
40 static dd_list *st_head;
42 void add_device(const struct storage_ops *st)
44 DD_LIST_APPEND(st_head, st);
47 void remove_device(const struct storage_ops *st)
49 DD_LIST_REMOVE(st_head, st);
52 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
54 const struct storage_ops *st;
56 int storage_id = 0, ret;
59 _E("Invalid parameter");
60 return STORAGE_ERROR_INVALID_PARAMETER;
63 DD_LIST_FOREACH(st_head, elem, st) {
64 ret = callback(storage_id, st->type, st->get_state(),
65 st->root(), user_data);
66 /* if the return value is false, will be stop to iterate */
72 return STORAGE_ERROR_NONE;
75 API int storage_get_root_directory(int storage_id, char **path)
77 const struct storage_ops *st;
80 _E("Invalid parameger");
81 return STORAGE_ERROR_INVALID_PARAMETER;
84 st = DD_LIST_NTH(st_head, storage_id);
86 _E("Not supported storage : id(%d)", storage_id);
87 return STORAGE_ERROR_NOT_SUPPORTED;
90 *path = strdup(st->root());
92 _E("Failed to copy the root string : %d", errno);
93 return STORAGE_ERROR_OUT_OF_MEMORY;
96 return STORAGE_ERROR_NONE;
99 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
101 const struct storage_ops *st;
108 _E("Invalid parameger");
109 return STORAGE_ERROR_INVALID_PARAMETER;
112 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
113 _E("Invalid parameter");
114 return STORAGE_ERROR_INVALID_PARAMETER;
117 st = DD_LIST_NTH(st_head, storage_id);
119 _E("Not supported storage : id(%d)", storage_id);
120 return STORAGE_ERROR_NOT_SUPPORTED;
123 if (st->type != STORAGE_TYPE_INTERNAL
124 && type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
125 _E("Not support directory : id(%d) type(%d)", storage_id, type);
126 return STORAGE_ERROR_NOT_SUPPORTED;
130 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
131 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
133 _E("Failed to get ringtone path : %d", ret);
134 return STORAGE_ERROR_OPERATION_FAILED;
136 end = strrchr(temp2, '/');
139 snprintf(temp, PATH_MAX, "%s", temp2);
142 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
144 *path = strdup(temp);
146 _E("Failed to copy the directory(%d) string : %d", type, errno);
147 return STORAGE_ERROR_OUT_OF_MEMORY;
150 return STORAGE_ERROR_NONE;
153 API int storage_get_type(int storage_id, storage_type_e *type)
155 const struct storage_ops *st;
158 _E("Invalid parameger");
159 return STORAGE_ERROR_INVALID_PARAMETER;
162 st = DD_LIST_NTH(st_head, storage_id);
164 _E("Not supported storage : id(%d)", storage_id);
165 return STORAGE_ERROR_NOT_SUPPORTED;
170 return STORAGE_ERROR_NONE;
173 API int storage_get_state(int storage_id, storage_state_e *state)
175 const struct storage_ops *st;
178 _E("Invalid parameger");
179 return STORAGE_ERROR_INVALID_PARAMETER;
182 st = DD_LIST_NTH(st_head, storage_id);
184 _E("Not supported storage : id(%d)", storage_id);
185 return STORAGE_ERROR_NOT_SUPPORTED;
188 *state = st->get_state();
190 return STORAGE_ERROR_NONE;
193 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
195 const struct storage_ops *st;
196 struct storage_cb_info info;
200 _E("Invalid parameger");
201 return STORAGE_ERROR_INVALID_PARAMETER;
204 st = DD_LIST_NTH(st_head, storage_id);
206 _E("Not supported storage : id(%d)", storage_id);
207 return STORAGE_ERROR_NOT_SUPPORTED;
210 /* do not register changed callback in case of internal memory */
211 if (st->type == STORAGE_TYPE_INTERNAL)
212 return STORAGE_ERROR_NONE;
214 info.id = storage_id;
215 info.state_cb = callback;
216 info.user_data = user_data;
218 ret = st->register_cb(STORAGE_CALLBACK_STATE, &info);
220 _E("Failed to register callback : id(%d)", storage_id);
221 return STORAGE_ERROR_OPERATION_FAILED;
224 return STORAGE_ERROR_NONE;
227 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
229 const struct storage_ops *st;
230 struct storage_cb_info info;
234 _E("Invalid parameger");
235 return STORAGE_ERROR_INVALID_PARAMETER;
238 st = DD_LIST_NTH(st_head, storage_id);
240 _E("Not supported storage : id(%d)", storage_id);
241 return STORAGE_ERROR_NOT_SUPPORTED;
244 /* in case of internal memory, it does not register changed callback */
245 if (st->type == STORAGE_TYPE_INTERNAL)
246 return STORAGE_ERROR_NONE;
248 info.id = storage_id;
249 info.state_cb = callback;
251 ret = st->unregister_cb(STORAGE_CALLBACK_STATE, &info);
253 _E("Failed to unregister callback : id(%d)", storage_id);
254 return STORAGE_ERROR_OPERATION_FAILED;
257 return STORAGE_ERROR_NONE;
260 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
262 const struct storage_ops *st;
263 unsigned long long total;
267 _E("Invalid parameger");
268 return STORAGE_ERROR_INVALID_PARAMETER;
271 st = DD_LIST_NTH(st_head, storage_id);
273 _E("Not supported storage : id(%d)", storage_id);
274 return STORAGE_ERROR_NOT_SUPPORTED;
277 ret = st->get_space(&total, NULL);
279 _E("Failed to get total memory : id(%d)", storage_id);
280 return STORAGE_ERROR_OPERATION_FAILED;
285 return STORAGE_ERROR_NONE;
288 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
290 const struct storage_ops *st;
291 unsigned long long avail;
295 _E("Invalid parameger");
296 return STORAGE_ERROR_INVALID_PARAMETER;
299 st = DD_LIST_NTH(st_head, storage_id);
301 _E("Not supported storage : id(%d)", storage_id);
302 return STORAGE_ERROR_NOT_SUPPORTED;
305 ret = st->get_space(NULL, &avail);
307 _E("Failed to get available memory : id(%d)", storage_id);
308 return STORAGE_ERROR_OPERATION_FAILED;
313 return STORAGE_ERROR_NONE;