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