blob: ce890d15d753fc456c877232fd6906f84d97dff5 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001/*
2 * Copyright (C) 2010 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 *
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050016 * Additional Copyright (C) 2018 TeamWin
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040017 */
18
19#include <utils/Log.h>
20
21#include <stdio.h>
22#include <assert.h>
23#include <limits.h>
24#include <unistd.h>
25#include <fcntl.h>
26#include <vector>
27#include <utils/threads.h>
Ethan Yonker726a0202014-12-16 20:01:38 -060028#include <pthread.h>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050029#include <cutils/properties.h>
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040030
31#include "mtp_MtpServer.hpp"
32#include "MtpServer.h"
33#include "MtpStorage.h"
34#include "MtpDebug.h"
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050035#include "MtpDescriptors.h"
Ethan Yonker726a0202014-12-16 20:01:38 -060036#include "MtpMessage.hpp"
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050037#include "mtp_MtpDatabase.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040038
39#include <string>
40
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050041void twmtp_MtpServer::set_device_info() {
42 char property[512];
43 property_get("ro.build.product", property, "unknown manufacturer");
44 mtpinfo.deviceInfoManufacturer = MtpStringBuffer(property);
45 property_get("ro.product.model", property, "unknown model");
46 mtpinfo.deviceInfoModel = MtpStringBuffer(property);
47 mtpinfo.deviceInfoDeviceVersion = MtpStringBuffer("None");
48 property_get("ro.serialno", property, "unknown serial number");
49 mtpinfo.deviceInfoSerialNumber = MtpStringBuffer(property);
50}
51
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052void twmtp_MtpServer::start()
53{
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050054 int controlFd = 0;
55
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040056 usePtp = false;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050057 IMtpDatabase* mtpdb = new IMtpDatabase();
58 MTPD("launching server\n");
59 /* Sleep for a bit before we open the MTP USB device because some
60 * devices are not ready due to the kernel not responding to our
61 * sysfs requests right away.
62 */
63 usleep(800000);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040064#ifdef USB_MTP_DEVICE
Ethan Yonkera1f38052014-09-11 08:28:51 -050065#define STRINGIFY(x) #x
66#define EXPAND(x) STRINGIFY(x)
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050067 const char* mtp_device = EXPAND(USB_MTP_DEVICE);
68 MTPI("Using '%s' for MTP device.\n", EXPAND(USB_MTP_DEVICE));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040069#else
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050070 const char* mtp_device = "/dev/mtp_usb";
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040071#endif
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050072 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
73 if (ffs_ok) {
74 MTPD("Opening FFS EPO\n");
75 controlFd = open(FFS_MTP_EP0, O_RDWR);
76 } else {
77 controlFd = open(mtp_device, O_WRONLY);
78 }
79 if (controlFd < 0) {
Ethan Yonker20fd25c2014-09-03 14:04:43 -050080 MTPE("could not open MTP driver, errno: %d\n", errno);
Ethan Yonker1b039202015-01-30 10:08:48 -060081 return;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040082 }
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050083 MTPD("MTP fd: %d\n", controlFd);
84
85 server = new MtpServer(mtpdb,\
86 controlFd,\
87 usePtp,\
88 mtpinfo.deviceInfoManufacturer, \
89 mtpinfo.deviceInfoModel, \
90 mtpinfo.deviceInfoDeviceVersion, \
91 mtpinfo.deviceInfoSerialNumber);
Ethan Yonker1b039202015-01-30 10:08:48 -060092 refserver = server;
93 MTPI("created new mtpserver object\n");
94 add_storage();
95 MTPD("Starting add / remove mtppipe monitor thread\n");
96 pthread_t thread;
97 ThreadPtr mtpptr = &twmtp_MtpServer::mtppipe_thread;
98 PThreadPtr p = *(PThreadPtr*)&mtpptr;
99 pthread_create(&thread, NULL, p, this);
100 // This loop restarts the MTP process if the device is unplugged and replugged in
101 while (true) {
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500102 server->run();
Ethan Yonker1b039202015-01-30 10:08:48 -0600103 usleep(800000);
104 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400105}
106
Ethan Yonker1b039202015-01-30 10:08:48 -0600107void twmtp_MtpServer::set_storages(storages* mtpstorages) {
108 stores = mtpstorages;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400109}
110
111void twmtp_MtpServer::cleanup()
112{
113 android::Mutex sMutex;
114 android::Mutex::Autolock autoLock(sMutex);
115
116 if (server) {
117 delete server;
118 } else {
119 MTPD("server is null in cleanup");
120 }
121}
122
123void twmtp_MtpServer::send_object_added(int handle)
124{
125 android::Mutex sMutex;
126 android::Mutex::Autolock autoLock(sMutex);
127
128 if (server)
129 server->sendObjectAdded(handle);
130 else
131 MTPD("server is null in send_object_added");
132}
133
134void twmtp_MtpServer::send_object_removed(int handle)
135{
136 android::Mutex sMutex;
137 android::Mutex::Autolock autoLock(sMutex);
138
139 if (server)
140 server->sendObjectRemoved(handle);
141 else
142 MTPD("server is null in send_object_removed");
143}
144
145void twmtp_MtpServer::add_storage()
146{
147 android::Mutex sMutex;
148 android::Mutex::Autolock autoLock(sMutex);
149
Ethan Yonker1b039202015-01-30 10:08:48 -0600150 MTPD("twmtp_MtpServer::add_storage count of storage devices: %i\n", stores->size());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400151 for (unsigned int i = 0; i < stores->size(); ++i) {
152 std::string pathStr = stores->at(i)->mount;
153
154 if (!pathStr.empty()) {
155 std::string descriptionStr = stores->at(i)->display;
156 int storageID = stores->at(i)->mtpid;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400157 bool removable = false;
bigbiff0c532032014-12-21 13:41:26 -0500158 uint64_t maxFileSize = stores->at(i)->maxFileSize;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400159 if (descriptionStr != "") {
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500160 MtpStorage* storage = new MtpStorage(storageID, &pathStr[0], &descriptionStr[0], removable, maxFileSize, refserver);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400161 server->addStorage(storage);
162 }
163 }
164 }
165}
166
167void twmtp_MtpServer::remove_storage(int storageId)
168{
169 android::Mutex sMutex;
170 android::Mutex::Autolock autoLock(sMutex);
171
172 if (server) {
173 MtpStorage* storage = server->getStorage(storageId);
174 if (storage) {
Ethan Yonker726a0202014-12-16 20:01:38 -0600175 MTPD("twmtp_MtpServer::remove_storage calling removeStorage\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400176 server->removeStorage(storage);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400177 }
178 } else
179 MTPD("server is null in remove_storage");
Ethan Yonker726a0202014-12-16 20:01:38 -0600180 MTPD("twmtp_MtpServer::remove_storage DONE\n");
181}
182
183int twmtp_MtpServer::mtppipe_thread(void)
184{
185 if (mtp_read_pipe == -1) {
186 MTPD("mtppipe_thread exiting because mtp_read_pipe not set\n");
187 return 0;
188 }
189 MTPD("Starting twmtp_MtpServer::mtppipe_thread\n");
190 int read_count;
191 struct mtpmsg mtp_message;
192 while (1) {
193 read_count = ::read(mtp_read_pipe, &mtp_message, sizeof(mtp_message));
194 MTPD("read %i from mtppipe\n", read_count);
195 if (read_count == sizeof(mtp_message)) {
196 if (mtp_message.message_type == MTP_MESSAGE_ADD_STORAGE) {
197 MTPI("mtppipe add storage %i '%s'\n", mtp_message.storage_id, mtp_message.path);
Ethan Yonker4bfabab2014-12-29 09:15:37 -0600198 if (mtp_message.storage_id) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -0600199 bool removable = false;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500200 MtpStorage* storage = new MtpStorage(mtp_message.storage_id, &mtp_message.path[0], &mtp_message.display[0], removable, mtp_message.maxFileSize, refserver);
Ethan Yonker4bfabab2014-12-29 09:15:37 -0600201 server->addStorage(storage);
202 MTPD("mtppipe done adding storage\n");
203 } else {
204 MTPE("Invalid storage ID %i specified\n", mtp_message.storage_id);
205 }
Ethan Yonker726a0202014-12-16 20:01:38 -0600206 } else if (mtp_message.message_type == MTP_MESSAGE_REMOVE_STORAGE) {
207 MTPI("mtppipe remove storage %i\n", mtp_message.storage_id);
208 remove_storage(mtp_message.storage_id);
209 MTPD("mtppipe done removing storage\n");
210 } else {
211 MTPE("Unknown mtppipe message value: %i\n", mtp_message.message_type);
212 }
213 } else {
214 MTPE("twmtp_MtpServer::mtppipe_thread unexpected read_count %i\n", read_count);
215 close(mtp_read_pipe);
216 break;
217 }
218 }
219 MTPD("twmtp_MtpServer::mtppipe_thread closing\n");
220 return 0;
221}
222
223void twmtp_MtpServer::set_read_pipe(int pipe)
224{
225 mtp_read_pipe = pipe;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400226}