bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1 | /* |
| 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 bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 16 | * Additional Copyright (C) 2018 TeamWin |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 17 | */ |
| 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 Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 28 | #include <pthread.h> |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 29 | #include <cutils/properties.h> |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 30 | |
| 31 | #include "mtp_MtpServer.hpp" |
| 32 | #include "MtpServer.h" |
| 33 | #include "MtpStorage.h" |
| 34 | #include "MtpDebug.h" |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 35 | #include "MtpDescriptors.h" |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 36 | #include "MtpMessage.hpp" |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 37 | #include "mtp_MtpDatabase.hpp" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 38 | |
| 39 | #include <string> |
| 40 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 41 | void 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 bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 52 | void twmtp_MtpServer::start() |
| 53 | { |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 54 | int controlFd = 0; |
| 55 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 56 | usePtp = false; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 57 | 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 bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 64 | #ifdef USB_MTP_DEVICE |
Ethan Yonker | a1f3805 | 2014-09-11 08:28:51 -0500 | [diff] [blame] | 65 | #define STRINGIFY(x) #x |
| 66 | #define EXPAND(x) STRINGIFY(x) |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 67 | const char* mtp_device = EXPAND(USB_MTP_DEVICE); |
| 68 | MTPI("Using '%s' for MTP device.\n", EXPAND(USB_MTP_DEVICE)); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 69 | #else |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 70 | const char* mtp_device = "/dev/mtp_usb"; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 71 | #endif |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 72 | 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 Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 80 | MTPE("could not open MTP driver, errno: %d\n", errno); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 81 | return; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 82 | } |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 83 | 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 Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 92 | 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 bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 102 | server->run(); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 103 | usleep(800000); |
| 104 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 107 | void twmtp_MtpServer::set_storages(storages* mtpstorages) { |
| 108 | stores = mtpstorages; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void 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 | |
| 123 | void 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 | |
| 134 | void 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 | |
| 145 | void twmtp_MtpServer::add_storage() |
| 146 | { |
| 147 | android::Mutex sMutex; |
| 148 | android::Mutex::Autolock autoLock(sMutex); |
| 149 | |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 150 | MTPD("twmtp_MtpServer::add_storage count of storage devices: %i\n", stores->size()); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 151 | 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 bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 157 | bool removable = false; |
bigbiff | 0c53203 | 2014-12-21 13:41:26 -0500 | [diff] [blame] | 158 | uint64_t maxFileSize = stores->at(i)->maxFileSize; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 159 | if (descriptionStr != "") { |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 160 | MtpStorage* storage = new MtpStorage(storageID, &pathStr[0], &descriptionStr[0], removable, maxFileSize, refserver); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 161 | server->addStorage(storage); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void 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 Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 175 | MTPD("twmtp_MtpServer::remove_storage calling removeStorage\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 176 | server->removeStorage(storage); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 177 | } |
| 178 | } else |
| 179 | MTPD("server is null in remove_storage"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 180 | MTPD("twmtp_MtpServer::remove_storage DONE\n"); |
| 181 | } |
| 182 | |
| 183 | int 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 Yonker | 4bfabab | 2014-12-29 09:15:37 -0600 | [diff] [blame] | 198 | if (mtp_message.storage_id) { |
Ethan Yonker | 4bfabab | 2014-12-29 09:15:37 -0600 | [diff] [blame] | 199 | bool removable = false; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 200 | MtpStorage* storage = new MtpStorage(mtp_message.storage_id, &mtp_message.path[0], &mtp_message.display[0], removable, mtp_message.maxFileSize, refserver); |
Ethan Yonker | 4bfabab | 2014-12-29 09:15:37 -0600 | [diff] [blame] | 201 | 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 Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 206 | } 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 | |
| 223 | void twmtp_MtpServer::set_read_pipe(int pipe) |
| 224 | { |
| 225 | mtp_read_pipe = pipe; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 226 | } |