Git init
[framework/api/application.git] / src / storage_usbhost.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/vfs.h>
22
23 #include <aul.h>
24 #include <dlog.h>
25 #include <vconf.h>
26
27 #include <app_storage.h>
28 #include <app_storage_private.h>
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33
34 #define LOG_TAG "TIZEN_N_STORAGE"
35
36 #define USBHOST_PATH "/opt/storage/usb"
37
38 static int storage_usbhost_get_state()
39 {
40         int usbhost_state = 0;
41
42         vconf_get_int(VCONFKEY_SYSMAN_USB_HOST_STATUS, &usbhost_state);
43
44         switch (usbhost_state)
45         {
46                 case VCONFKEY_SYSMEN_USB_HOST_DISCONNECTED:
47                         return STORAGE_STATE_REMOVED;
48
49                 case VCONFKEY_SYSMEN_USB_HOST_CONNECTED:
50                         return STORAGE_STATE_MOUNTED;
51
52                 default:
53                         return STORAGE_STATE_REMOVED;
54         }
55 }
56
57 static void storage_usbhost_state_cb_broker(keynode_t* key, void* data)
58 {
59         storage_state_e state;
60
61         state = storage_usbhost_get_state();
62
63         storage_dispatch_state_event(state, data);
64 }
65
66 static int storage_usbhost_set_state_cb(void *data)
67 {
68         vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_HOST_STATUS, storage_usbhost_state_cb_broker, data);
69         return 0;
70 }
71
72 static void storage_usbhost_unset_state_cb()
73 {
74         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_HOST_STATUS, storage_usbhost_state_cb_broker);
75 }
76
77
78 static int storage_usbhost_get_space(unsigned long long *total, unsigned long long *available)
79 {
80         storage_state_e state;
81         state = storage_usbhost_get_state();
82
83         if (state < STORAGE_STATE_MOUNTED)
84         {
85                 if (total != NULL)
86                 {
87                         *total = 0;
88                 }
89
90                 if (available != NULL)
91                 {
92                         *available = 0;
93                 }
94
95                 return 0;
96         }
97         else
98         {
99                 return storage_statfs(USBHOST_PATH, total, available);
100         }
101 }
102
103
104 storage_device_h storage_usbhost_device()
105 {
106         storage_device_h device;
107
108         device = calloc(1, sizeof(struct storage_device_s));
109
110         if (device == NULL)
111         {
112                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, STORAGE_ERROR_OUT_OF_MEMORY);
113                 return NULL;
114         }
115
116         device->type = STORAGE_TYPE_EXTERNAL;
117         device->path = USBHOST_PATH;
118         device->get_state = storage_usbhost_get_state;
119         device->set_state_cb = storage_usbhost_set_state_cb;
120         device->unset_state_cb = storage_usbhost_unset_state_cb;
121         device->get_space = storage_usbhost_get_space;
122
123         return device;
124 }
125