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 | * |
| 16 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 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 | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 29 | |
| 30 | #include "mtp_MtpServer.hpp" |
| 31 | #include "MtpServer.h" |
| 32 | #include "MtpStorage.h" |
| 33 | #include "MtpDebug.h" |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 34 | #include "MtpMessage.hpp" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 35 | |
| 36 | #include <string> |
| 37 | |
| 38 | void twmtp_MtpServer::start() |
| 39 | { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 40 | usePtp = false; |
| 41 | MyMtpDatabase* mtpdb = new MyMtpDatabase(); |
Ethan Yonker | abb5c4e | 2014-12-29 09:44:05 -0600 | [diff] [blame] | 42 | /* Sleep for a bit before we open the MTP USB device because some |
| 43 | * devices are not ready due to the kernel not responding to our |
| 44 | * sysfs requests right away. |
| 45 | */ |
| 46 | usleep(800000); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 47 | #ifdef USB_MTP_DEVICE |
Ethan Yonker | a1f3805 | 2014-09-11 08:28:51 -0500 | [diff] [blame] | 48 | #define STRINGIFY(x) #x |
| 49 | #define EXPAND(x) STRINGIFY(x) |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 50 | const char* mtp_device = EXPAND(USB_MTP_DEVICE); |
Ethan Yonker | a1f3805 | 2014-09-11 08:28:51 -0500 | [diff] [blame] | 51 | MTPI("Using '%s' for MTP device.\n", EXPAND(USB_MTP_DEVICE)); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 52 | #else |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 53 | const char* mtp_device = "/dev/mtp_usb"; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 54 | #endif |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 55 | int fd = open(mtp_device, O_RDWR); |
| 56 | if (fd < 0) { |
Ethan Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 57 | MTPE("could not open MTP driver, errno: %d\n", errno); |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 58 | return; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 59 | } |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 60 | MTPD("fd: %d\n", fd); |
| 61 | server = new MtpServer(mtpdb, usePtp, 0, 0664, 0775); |
| 62 | refserver = server; |
| 63 | MTPI("created new mtpserver object\n"); |
| 64 | add_storage(); |
| 65 | MTPD("Starting add / remove mtppipe monitor thread\n"); |
| 66 | pthread_t thread; |
| 67 | ThreadPtr mtpptr = &twmtp_MtpServer::mtppipe_thread; |
| 68 | PThreadPtr p = *(PThreadPtr*)&mtpptr; |
| 69 | pthread_create(&thread, NULL, p, this); |
| 70 | // This loop restarts the MTP process if the device is unplugged and replugged in |
| 71 | while (true) { |
| 72 | server->run(fd); |
| 73 | fd = open(mtp_device, O_RDWR); |
| 74 | usleep(800000); |
| 75 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 78 | void twmtp_MtpServer::set_storages(storages* mtpstorages) { |
| 79 | stores = mtpstorages; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void twmtp_MtpServer::cleanup() |
| 83 | { |
| 84 | android::Mutex sMutex; |
| 85 | android::Mutex::Autolock autoLock(sMutex); |
| 86 | |
| 87 | if (server) { |
| 88 | delete server; |
| 89 | } else { |
| 90 | MTPD("server is null in cleanup"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void twmtp_MtpServer::send_object_added(int handle) |
| 95 | { |
| 96 | android::Mutex sMutex; |
| 97 | android::Mutex::Autolock autoLock(sMutex); |
| 98 | |
| 99 | if (server) |
| 100 | server->sendObjectAdded(handle); |
| 101 | else |
| 102 | MTPD("server is null in send_object_added"); |
| 103 | } |
| 104 | |
| 105 | void twmtp_MtpServer::send_object_removed(int handle) |
| 106 | { |
| 107 | android::Mutex sMutex; |
| 108 | android::Mutex::Autolock autoLock(sMutex); |
| 109 | |
| 110 | if (server) |
| 111 | server->sendObjectRemoved(handle); |
| 112 | else |
| 113 | MTPD("server is null in send_object_removed"); |
| 114 | } |
| 115 | |
| 116 | void twmtp_MtpServer::add_storage() |
| 117 | { |
| 118 | android::Mutex sMutex; |
| 119 | android::Mutex::Autolock autoLock(sMutex); |
| 120 | |
Ethan Yonker | 1b03920 | 2015-01-30 10:08:48 -0600 | [diff] [blame] | 121 | 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] | 122 | for (unsigned int i = 0; i < stores->size(); ++i) { |
| 123 | std::string pathStr = stores->at(i)->mount; |
| 124 | |
| 125 | if (!pathStr.empty()) { |
| 126 | std::string descriptionStr = stores->at(i)->display; |
| 127 | int storageID = stores->at(i)->mtpid; |
| 128 | long reserveSpace = 1; |
| 129 | bool removable = false; |
bigbiff | 0c53203 | 2014-12-21 13:41:26 -0500 | [diff] [blame] | 130 | uint64_t maxFileSize = stores->at(i)->maxFileSize; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 131 | if (descriptionStr != "") { |
| 132 | MtpStorage* storage = new MtpStorage(storageID, &pathStr[0], &descriptionStr[0], reserveSpace, removable, maxFileSize, refserver); |
| 133 | server->addStorage(storage); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void twmtp_MtpServer::remove_storage(int storageId) |
| 140 | { |
| 141 | android::Mutex sMutex; |
| 142 | android::Mutex::Autolock autoLock(sMutex); |
| 143 | |
| 144 | if (server) { |
| 145 | MtpStorage* storage = server->getStorage(storageId); |
| 146 | if (storage) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 147 | MTPD("twmtp_MtpServer::remove_storage calling removeStorage\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 148 | server->removeStorage(storage); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 149 | } |
| 150 | } else |
| 151 | MTPD("server is null in remove_storage"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 152 | MTPD("twmtp_MtpServer::remove_storage DONE\n"); |
| 153 | } |
| 154 | |
| 155 | int twmtp_MtpServer::mtppipe_thread(void) |
| 156 | { |
| 157 | if (mtp_read_pipe == -1) { |
| 158 | MTPD("mtppipe_thread exiting because mtp_read_pipe not set\n"); |
| 159 | return 0; |
| 160 | } |
| 161 | MTPD("Starting twmtp_MtpServer::mtppipe_thread\n"); |
| 162 | int read_count; |
| 163 | struct mtpmsg mtp_message; |
| 164 | while (1) { |
| 165 | read_count = ::read(mtp_read_pipe, &mtp_message, sizeof(mtp_message)); |
| 166 | MTPD("read %i from mtppipe\n", read_count); |
| 167 | if (read_count == sizeof(mtp_message)) { |
| 168 | if (mtp_message.message_type == MTP_MESSAGE_ADD_STORAGE) { |
| 169 | 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] | 170 | if (mtp_message.storage_id) { |
| 171 | long reserveSpace = 1; |
| 172 | bool removable = false; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 173 | MtpStorage* storage = new MtpStorage(mtp_message.storage_id, &mtp_message.path[0], &mtp_message.display[0], reserveSpace, removable, mtp_message.maxFileSize, refserver); |
Ethan Yonker | 4bfabab | 2014-12-29 09:15:37 -0600 | [diff] [blame] | 174 | server->addStorage(storage); |
| 175 | MTPD("mtppipe done adding storage\n"); |
| 176 | } else { |
| 177 | MTPE("Invalid storage ID %i specified\n", mtp_message.storage_id); |
| 178 | } |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 179 | } else if (mtp_message.message_type == MTP_MESSAGE_REMOVE_STORAGE) { |
| 180 | MTPI("mtppipe remove storage %i\n", mtp_message.storage_id); |
| 181 | remove_storage(mtp_message.storage_id); |
| 182 | MTPD("mtppipe done removing storage\n"); |
| 183 | } else { |
| 184 | MTPE("Unknown mtppipe message value: %i\n", mtp_message.message_type); |
| 185 | } |
| 186 | } else { |
| 187 | MTPE("twmtp_MtpServer::mtppipe_thread unexpected read_count %i\n", read_count); |
| 188 | close(mtp_read_pipe); |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | MTPD("twmtp_MtpServer::mtppipe_thread closing\n"); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void twmtp_MtpServer::set_read_pipe(int pipe) |
| 197 | { |
| 198 | mtp_read_pipe = pipe; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 199 | } |