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> |
| 28 | |
| 29 | #include "mtp_MtpServer.hpp" |
| 30 | #include "MtpServer.h" |
| 31 | #include "MtpStorage.h" |
| 32 | #include "MtpDebug.h" |
| 33 | |
| 34 | #include <string> |
| 35 | |
| 36 | void twmtp_MtpServer::start() |
| 37 | { |
Ethan Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 38 | if (setup() == 0) { |
| 39 | add_storage(); |
| 40 | server->run(); |
| 41 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void twmtp_MtpServer::set_storages(storages* mtpstorages) { |
| 45 | stores = mtpstorages; |
| 46 | } |
| 47 | |
Ethan Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 48 | int twmtp_MtpServer::setup() |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 49 | { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 50 | usePtp = false; |
| 51 | MyMtpDatabase* mtpdb = new MyMtpDatabase(); |
| 52 | #ifdef USB_MTP_DEVICE |
Ethan Yonker | a1f3805 | 2014-09-11 08:28:51 -0500 | [diff] [blame] | 53 | #define STRINGIFY(x) #x |
| 54 | #define EXPAND(x) STRINGIFY(x) |
| 55 | MTPI("Using '%s' for MTP device.\n", EXPAND(USB_MTP_DEVICE)); |
| 56 | int fd = open(EXPAND(USB_MTP_DEVICE), O_RDWR); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 57 | #else |
| 58 | int fd = open("/dev/mtp_usb", O_RDWR); |
| 59 | #endif |
| 60 | if (fd >= 0) { |
| 61 | MTPD("fd: %d\n", fd); |
| 62 | server = new MtpServer(fd, mtpdb, usePtp, 0, 0664, 0775); |
| 63 | refserver = server; |
| 64 | MTPI("created new mtpserver object\n"); |
| 65 | } else { |
Ethan Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 66 | MTPE("could not open MTP driver, errno: %d\n", errno); |
| 67 | return -1; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 68 | } |
Ethan Yonker | 20fd25c | 2014-09-03 14:04:43 -0500 | [diff] [blame] | 69 | return 0; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void twmtp_MtpServer::run() |
| 73 | { |
| 74 | MTPD("running in twmtp\n"); |
| 75 | server->run(); |
| 76 | } |
| 77 | |
| 78 | void twmtp_MtpServer::cleanup() |
| 79 | { |
| 80 | android::Mutex sMutex; |
| 81 | android::Mutex::Autolock autoLock(sMutex); |
| 82 | |
| 83 | if (server) { |
| 84 | delete server; |
| 85 | } else { |
| 86 | MTPD("server is null in cleanup"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void twmtp_MtpServer::send_object_added(int handle) |
| 91 | { |
| 92 | android::Mutex sMutex; |
| 93 | android::Mutex::Autolock autoLock(sMutex); |
| 94 | |
| 95 | if (server) |
| 96 | server->sendObjectAdded(handle); |
| 97 | else |
| 98 | MTPD("server is null in send_object_added"); |
| 99 | } |
| 100 | |
| 101 | void twmtp_MtpServer::send_object_removed(int handle) |
| 102 | { |
| 103 | android::Mutex sMutex; |
| 104 | android::Mutex::Autolock autoLock(sMutex); |
| 105 | |
| 106 | if (server) |
| 107 | server->sendObjectRemoved(handle); |
| 108 | else |
| 109 | MTPD("server is null in send_object_removed"); |
| 110 | } |
| 111 | |
| 112 | void twmtp_MtpServer::add_storage() |
| 113 | { |
| 114 | android::Mutex sMutex; |
| 115 | android::Mutex::Autolock autoLock(sMutex); |
| 116 | |
| 117 | MTPI("adding internal storage\n"); |
| 118 | for (unsigned int i = 0; i < stores->size(); ++i) { |
| 119 | std::string pathStr = stores->at(i)->mount; |
| 120 | |
| 121 | if (!pathStr.empty()) { |
| 122 | std::string descriptionStr = stores->at(i)->display; |
| 123 | int storageID = stores->at(i)->mtpid; |
| 124 | long reserveSpace = 1; |
| 125 | bool removable = false; |
| 126 | long maxFileSize = 1000000000L; |
| 127 | if (descriptionStr != "") { |
| 128 | MtpStorage* storage = new MtpStorage(storageID, &pathStr[0], &descriptionStr[0], reserveSpace, removable, maxFileSize, refserver); |
| 129 | server->addStorage(storage); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void twmtp_MtpServer::remove_storage(int storageId) |
| 136 | { |
| 137 | android::Mutex sMutex; |
| 138 | android::Mutex::Autolock autoLock(sMutex); |
| 139 | |
| 140 | if (server) { |
| 141 | MtpStorage* storage = server->getStorage(storageId); |
| 142 | if (storage) { |
| 143 | server->removeStorage(storage); |
| 144 | delete storage; |
| 145 | } |
| 146 | } else |
| 147 | MTPD("server is null in remove_storage"); |
| 148 | } |