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