blob: a6468fc7370735f97cd31eec8922fb08240f9d1a [file] [log] [blame]
The Android Open Source Project8ac3a132009-01-20 14:04:01 -08001
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
24static boolean host_connected = false;
25static boolean ums_enabled = false;
26
27int ums_bootstrap(void)
28{
29 return 0;
30}
31
32void ums_enabled_set(boolean enabled)
33{
34 ums_enabled = enabled;
35 send_msg(enabled ? VOLD_EVT_UMS_ENABLED : VOLD_EVT_UMS_DISABLED);
36}
37
38boolean ums_enabled_get()
39{
40 return ums_enabled;
41}
42
43void ums_hostconnected_set(boolean connected)
44{
45 LOG_VOL("ums_hostconnected_set(%d):\n", connected);
46 host_connected = connected;
47
48 if (!connected)
49 ums_enabled_set(false);
50 send_msg(connected ? VOLD_EVT_UMS_CONNECTED : VOLD_EVT_UMS_DISCONNECTED);
51}
52
53int ums_enable(char *dev_fspath, char *lun_syspath)
54{
55 LOG_VOL("ums_enable(%s, %s):\n", dev_fspath, lun_syspath);
56
57 int fd;
58 char filename[255];
59
60 sprintf(filename, "/sys/%s/file", lun_syspath);
61 if ((fd = open(filename, O_WRONLY)) < 0) {
62 LOGE("Unable to open '%s' (%s)\n", filename, strerror(errno));
63 return -errno;
64 }
65
66 if (write(fd, dev_fspath, strlen(dev_fspath)) < 0) {
67 LOGE("Unable to write to ums lunfile (%s)\n", strerror(errno));
68 close(fd);
69 return -errno;
70 }
71
72 close(fd);
73 return 0;
74}
75
76int ums_disable(char *lun_syspath)
77{
78 LOG_VOL("ums_disable(%s):\n", lun_syspath);
79
80 int fd;
81 char filename[255];
82
83 sprintf(filename, "/sys/%s/file", lun_syspath);
84 if ((fd = open(filename, O_WRONLY)) < 0) {
85 LOGE("Unable to open '%s' (%s)\n", filename, strerror(errno));
86 return -errno;
87 }
88
89 char ch = 0;
90
91 if (write(fd, &ch, 1) < 0) {
92 LOGE("Unable to write to ums lunfile (%s)\n", strerror(errno));
93 close(fd);
94 return -errno;
95 }
96
97 close(fd);
98 return 0;
99}
100
101boolean ums_hostconnected_get(void)
102{
103 return host_connected;
104}
105
106int ums_send_status(void)
107{
108 int rc;
109
110 LOG_VOL("ums_send_status():\n");
111
112 rc = send_msg(ums_enabled_get() ? VOLD_EVT_UMS_ENABLED :
113 VOLD_EVT_UMS_DISABLED);
114 if (rc < 0)
115 return rc;
116
117 rc = send_msg(ums_hostconnected_get() ? VOLD_EVT_UMS_CONNECTED :
118 VOLD_EVT_UMS_DISCONNECTED);
119
120 return rc;
121}