blob: 8d6038c777217a3fa85f87b0f28d92ec3a127b45 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001/*
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 Yonker726a0202014-12-16 20:01:38 -060028#include <pthread.h>
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040029
30#include "mtp_MtpServer.hpp"
31#include "MtpServer.h"
32#include "MtpStorage.h"
33#include "MtpDebug.h"
Ethan Yonker726a0202014-12-16 20:01:38 -060034#include "MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040035
36#include <string>
37
38void twmtp_MtpServer::start()
39{
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040040 usePtp = false;
41 MyMtpDatabase* mtpdb = new MyMtpDatabase();
Ethan Yonkerabb5c4e2014-12-29 09:44:05 -060042 /* 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 bigbiffc7eee6f2014-09-02 18:59:01 -040047#ifdef USB_MTP_DEVICE
Ethan Yonkera1f38052014-09-11 08:28:51 -050048#define STRINGIFY(x) #x
49#define EXPAND(x) STRINGIFY(x)
Ethan Yonker1b039202015-01-30 10:08:48 -060050 const char* mtp_device = EXPAND(USB_MTP_DEVICE);
Ethan Yonkera1f38052014-09-11 08:28:51 -050051 MTPI("Using '%s' for MTP device.\n", EXPAND(USB_MTP_DEVICE));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052#else
Ethan Yonker1b039202015-01-30 10:08:48 -060053 const char* mtp_device = "/dev/mtp_usb";
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040054#endif
Ethan Yonker1b039202015-01-30 10:08:48 -060055 int fd = open(mtp_device, O_RDWR);
56 if (fd < 0) {
Ethan Yonker20fd25c2014-09-03 14:04:43 -050057 MTPE("could not open MTP driver, errno: %d\n", errno);
Ethan Yonker1b039202015-01-30 10:08:48 -060058 return;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040059 }
Ethan Yonker1b039202015-01-30 10:08:48 -060060 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 bigbiffc7eee6f2014-09-02 18:59:01 -040076}
77
Ethan Yonker1b039202015-01-30 10:08:48 -060078void twmtp_MtpServer::set_storages(storages* mtpstorages) {
79 stores = mtpstorages;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040080}
81
82void 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
94void 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
105void 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
116void twmtp_MtpServer::add_storage()
117{
118 android::Mutex sMutex;
119 android::Mutex::Autolock autoLock(sMutex);
120
Ethan Yonker1b039202015-01-30 10:08:48 -0600121 MTPD("twmtp_MtpServer::add_storage count of storage devices: %i\n", stores->size());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400122 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;
bigbiff0c532032014-12-21 13:41:26 -0500130 uint64_t maxFileSize = stores->at(i)->maxFileSize;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400131 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
139void 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 Yonker726a0202014-12-16 20:01:38 -0600147 MTPD("twmtp_MtpServer::remove_storage calling removeStorage\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400148 server->removeStorage(storage);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400149 }
150 } else
151 MTPD("server is null in remove_storage");
Ethan Yonker726a0202014-12-16 20:01:38 -0600152 MTPD("twmtp_MtpServer::remove_storage DONE\n");
153}
154
155int 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 Yonker4bfabab2014-12-29 09:15:37 -0600170 if (mtp_message.storage_id) {
171 long reserveSpace = 1;
172 bool removable = false;
173 MtpStorage* storage = new MtpStorage(mtp_message.storage_id, mtp_message.path, mtp_message.display, reserveSpace, removable, mtp_message.maxFileSize, refserver);
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 Yonker726a0202014-12-16 20:01:38 -0600179 } 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
196void twmtp_MtpServer::set_read_pipe(int pipe)
197{
198 mtp_read_pipe = pipe;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400199}