1
2 /*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <fcntl.h>
19 #include <errno.h>
20
21 #include "vold.h"
22 #include "ums.h"
23
24 #define DEBUG_UMS 0
25
26 static boolean host_connected = false;
27 static boolean ums_enabled = false;
28
ums_bootstrap(void)29 int ums_bootstrap(void)
30 {
31 return 0;
32 }
33
ums_enabled_set(boolean enabled)34 void ums_enabled_set(boolean enabled)
35 {
36 ums_enabled = enabled;
37 send_msg(enabled ? VOLD_EVT_UMS_ENABLED : VOLD_EVT_UMS_DISABLED);
38 }
39
ums_enabled_get()40 boolean ums_enabled_get()
41 {
42 return ums_enabled;
43 }
44
ums_hostconnected_set(boolean connected)45 void ums_hostconnected_set(boolean connected)
46 {
47 #if DEBUG_UMS
48 LOG_VOL("ums_hostconnected_set(%d):", connected);
49 #endif
50 host_connected = connected;
51
52 if (!connected)
53 ums_enabled_set(false);
54 send_msg(connected ? VOLD_EVT_UMS_CONNECTED : VOLD_EVT_UMS_DISCONNECTED);
55 }
56
ums_enable(char * dev_fspath,char * lun_syspath)57 int ums_enable(char *dev_fspath, char *lun_syspath)
58 {
59 LOG_VOL("ums_enable(%s, %s):", dev_fspath, lun_syspath);
60
61 int fd;
62 char filename[255];
63
64 sprintf(filename, "/sys/%s/file", lun_syspath);
65 if ((fd = open(filename, O_WRONLY)) < 0) {
66 LOGE("Unable to open '%s' (%s)", filename, strerror(errno));
67 return -errno;
68 }
69
70 if (write(fd, dev_fspath, strlen(dev_fspath)) < 0) {
71 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
72 close(fd);
73 return -errno;
74 }
75
76 close(fd);
77 return 0;
78 }
79
ums_disable(char * lun_syspath)80 int ums_disable(char *lun_syspath)
81 {
82 #if DEBUG_UMS
83 LOG_VOL("ums_disable(%s):", lun_syspath);
84 #endif
85
86 int fd;
87 char filename[255];
88
89 sprintf(filename, "/sys/%s/file", lun_syspath);
90 if ((fd = open(filename, O_WRONLY)) < 0) {
91 LOGE("Unable to open '%s' (%s)", filename, strerror(errno));
92 return -errno;
93 }
94
95 char ch = 0;
96
97 if (write(fd, &ch, 1) < 0) {
98 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
99 close(fd);
100 return -errno;
101 }
102
103 close(fd);
104 return 0;
105 }
106
ums_hostconnected_get(void)107 boolean ums_hostconnected_get(void)
108 {
109 return host_connected;
110 }
111
ums_send_status(void)112 int ums_send_status(void)
113 {
114 int rc;
115
116 #if DEBUG_UMS
117 LOG_VOL("ums_send_status():");
118 #endif
119
120 rc = send_msg(ums_enabled_get() ? VOLD_EVT_UMS_ENABLED :
121 VOLD_EVT_UMS_DISABLED);
122 if (rc < 0)
123 return rc;
124
125 rc = send_msg(ums_hostconnected_get() ? VOLD_EVT_UMS_CONNECTED :
126 VOLD_EVT_UMS_DISCONNECTED);
127
128 return rc;
129 }
130