blob: 96a1e94532cd0a37995409fce6967011a171beba [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{
Ethan Yonker20fd25c2014-09-03 14:04:43 -050040 if (setup() == 0) {
41 add_storage();
Ethan Yonker726a0202014-12-16 20:01:38 -060042 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 Yonker20fd25c2014-09-03 14:04:43 -050047 server->run();
48 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040049}
50
51void twmtp_MtpServer::set_storages(storages* mtpstorages) {
52 stores = mtpstorages;
53}
54
Ethan Yonker20fd25c2014-09-03 14:04:43 -050055int twmtp_MtpServer::setup()
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040056{
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040057 usePtp = false;
58 MyMtpDatabase* mtpdb = new MyMtpDatabase();
59#ifdef USB_MTP_DEVICE
Ethan Yonkera1f38052014-09-11 08:28:51 -050060#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 bigbiffc7eee6f2014-09-02 18:59:01 -040064#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 Yonker20fd25c2014-09-03 14:04:43 -050073 MTPE("could not open MTP driver, errno: %d\n", errno);
74 return -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040075 }
Ethan Yonker20fd25c2014-09-03 14:04:43 -050076 return 0;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040077}
78
79void twmtp_MtpServer::run()
80{
81 MTPD("running in twmtp\n");
82 server->run();
83}
84
85void 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
97void 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
108void 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
119void 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;
bigbiff0c532032014-12-21 13:41:26 -0500133 uint64_t maxFileSize = stores->at(i)->maxFileSize;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400134 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
142void 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 Yonker726a0202014-12-16 20:01:38 -0600150 MTPD("twmtp_MtpServer::remove_storage calling removeStorage\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400151 server->removeStorage(storage);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400152 }
153 } else
154 MTPD("server is null in remove_storage");
Ethan Yonker726a0202014-12-16 20:01:38 -0600155 MTPD("twmtp_MtpServer::remove_storage DONE\n");
156}
157
158int 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);
Ethan Yonker4bfabab2014-12-29 09:15:37 -0600173 if (mtp_message.storage_id) {
174 long reserveSpace = 1;
175 bool removable = false;
176 MtpStorage* storage = new MtpStorage(mtp_message.storage_id, mtp_message.path, mtp_message.display, reserveSpace, removable, mtp_message.maxFileSize, refserver);
177 server->addStorage(storage);
178 MTPD("mtppipe done adding storage\n");
179 } else {
180 MTPE("Invalid storage ID %i specified\n", mtp_message.storage_id);
181 }
Ethan Yonker726a0202014-12-16 20:01:38 -0600182 } else if (mtp_message.message_type == MTP_MESSAGE_REMOVE_STORAGE) {
183 MTPI("mtppipe remove storage %i\n", mtp_message.storage_id);
184 remove_storage(mtp_message.storage_id);
185 MTPD("mtppipe done removing storage\n");
186 } else {
187 MTPE("Unknown mtppipe message value: %i\n", mtp_message.message_type);
188 }
189 } else {
190 MTPE("twmtp_MtpServer::mtppipe_thread unexpected read_count %i\n", read_count);
191 close(mtp_read_pipe);
192 break;
193 }
194 }
195 MTPD("twmtp_MtpServer::mtppipe_thread closing\n");
196 return 0;
197}
198
199void twmtp_MtpServer::set_read_pipe(int pipe)
200{
201 mtp_read_pipe = pipe;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400202}