blob: a1db38fe96b503ef28622ee46400126aa761ee00
1 | /* |
2 | * Copyright (C) 2016 The Android Open Source Project |
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 | #include "DroidVold.h" |
18 | |
19 | #define LOG_TAG "DroidVold" |
20 | |
21 | #include <android-base/logging.h> |
22 | #include <android-base/stringprintf.h> |
23 | #include <cutils/fs.h> |
24 | #include <cutils/log.h> |
25 | |
26 | #include "VolumeManager.h" |
27 | |
28 | namespace vendor { |
29 | namespace amlogic { |
30 | namespace hardware { |
31 | namespace droidvold { |
32 | namespace V1_0 { |
33 | namespace implementation { |
34 | |
35 | DroidVold *sInstance; |
36 | |
37 | DroidVold *DroidVold::Instance() { |
38 | if (!sInstance) |
39 | sInstance = new DroidVold(); |
40 | return sInstance; |
41 | } |
42 | |
43 | DroidVold::DroidVold() { |
44 | mCallback = NULL; |
45 | } |
46 | |
47 | DroidVold::~DroidVold() { |
48 | } |
49 | |
50 | Return<void> DroidVold::setCallback(const sp<IDroidVoldCallback>& callback) { |
51 | if (VolumeManager::Instance()->getDebug()) |
52 | LOG(DEBUG) << "setCallback =" << callback.get(); |
53 | |
54 | /* |
55 | if (callback != NULL) { |
56 | mClients.push_back(callback); |
57 | } |
58 | */ |
59 | |
60 | mCallback = callback; |
61 | return Void(); |
62 | } |
63 | |
64 | Return<Result> DroidVold::reset() { |
65 | VolumeManager *vm = VolumeManager::Instance(); |
66 | vm->reset(); |
67 | return Result::OK; |
68 | } |
69 | |
70 | Return<Result> DroidVold::shutdown() { |
71 | VolumeManager *vm = VolumeManager::Instance(); |
72 | vm->shutdown(); |
73 | return Result::OK; |
74 | } |
75 | |
76 | Return<Result> DroidVold::mount(const hidl_string& id, uint32_t flag, uint32_t uid) { |
77 | // mount [volId] [flags] |
78 | if (VolumeManager::Instance()->getDebug()) |
79 | LOG(DEBUG) << "mount id=" << id << " flag=" << flag; |
80 | |
81 | VolumeManager *vm = VolumeManager::Instance(); |
82 | std::string vid = id; |
83 | |
84 | #ifdef HAS_VIRTUAL_CDROM |
85 | // is loop volume |
86 | if (flag == 0xF) { |
87 | if (vm->mountloop(vid.c_str())) { |
88 | LOG(ERROR) << " mount loop error!"; |
89 | return Result::FAIL; |
90 | } |
91 | return Result::OK; |
92 | } |
93 | #endif |
94 | |
95 | auto vol = vm->findVolume(vid); |
96 | if (vol == nullptr) { |
97 | LOG(ERROR) << "mount ,count not find volume id=" << vid; |
98 | return Result::FAIL; |
99 | } |
100 | |
101 | vol->setMountFlags(flag); |
102 | vol->setMountUserId(uid); |
103 | |
104 | int res = vol->mount(); |
105 | if (res != 0) { |
106 | LOG(ERROR) << "failed to mount volume=" << vol; |
107 | return Result::FAIL; |
108 | } |
109 | |
110 | return Result::OK; |
111 | } |
112 | |
113 | Return<Result> DroidVold::unmount(const hidl_string& id) { |
114 | // unmount [volId] |
115 | VolumeManager *vm = VolumeManager::Instance(); |
116 | std::string vid = id; |
117 | |
118 | #ifdef HAS_VIRTUAL_CDROM |
119 | if (vid.find("/mnt/loop") != std::string::npos) { |
120 | if (vm->unmountloop(true)) { |
121 | LOG(ERROR) << "unmount loop error!"; |
122 | return Result::FAIL; |
123 | } |
124 | return Result::OK; |
125 | } |
126 | #endif |
127 | |
128 | auto vol = vm->findVolume(vid); |
129 | if (vol == nullptr) { |
130 | LOG(ERROR) << "unmount, count not find volume id=" << vid; |
131 | return Result::FAIL; |
132 | } |
133 | |
134 | int res = vol->unmount(); |
135 | |
136 | if (res != 0) { |
137 | LOG(ERROR) << "failed to unmount volume=" << vol; |
138 | return Result::FAIL; |
139 | } |
140 | |
141 | return Result::OK; |
142 | } |
143 | |
144 | Return<Result> DroidVold::format(const hidl_string& id, const hidl_string& type) { |
145 | // format [volId] [fsType|auto] |
146 | VolumeManager *vm = VolumeManager::Instance(); |
147 | std::string vid = id; |
148 | std::string fsType= type; |
149 | auto vol = vm->findVolume(id); |
150 | |
151 | if (vol == nullptr) { |
152 | LOG(ERROR) << "format, count not find volume id=" << vid; |
153 | return Result::FAIL; |
154 | } |
155 | |
156 | int res = vol->format(fsType); |
157 | |
158 | if (res != 0) { |
159 | LOG(ERROR) << "failed to unmount volume=" << vol; |
160 | return Result::FAIL; |
161 | } |
162 | |
163 | return Result::OK; |
164 | } |
165 | |
166 | void DroidVold::sendBroadcast(int event, const std::string& message) { |
167 | if (VolumeManager::Instance()->getDebug()) |
168 | LOG(DEBUG) << "event=" << event << " message=" << message; |
169 | |
170 | hidl_string mss = message; |
171 | if (mCallback != NULL) |
172 | mCallback->onEvent(event, mss); |
173 | |
174 | /* |
175 | for (int i = 0; i < clientSize; i++) { |
176 | mClients[i]->onEvent(event, mss); |
177 | } |
178 | for (auto client : mClients) { |
179 | client->onEvent(event, mss); |
180 | } |
181 | */ |
182 | } |
183 | |
184 | Return<int32_t> DroidVold::getDiskFlag(const hidl_string &path) { |
185 | int32_t flag = 0; |
186 | std::string mPath = path; |
187 | |
188 | flag = VolumeManager::Instance()->getDiskFlag(mPath); |
189 | |
190 | return flag; |
191 | } |
192 | |
193 | } // namespace implementation |
194 | } // namespace V1_0 |
195 | } // namespace droidvold |
196 | } // namespace hardware |
197 | } // namespace amlogic |
198 | } // namespace vendor |
199 |