blob: 9df564ffc5f05a20b1f01d9d9440b5327897d0cd [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>
28
29#include "mtp_MtpServer.hpp"
30#include "MtpServer.h"
31#include "MtpStorage.h"
32#include "MtpDebug.h"
33
34#include <string>
35
36void twmtp_MtpServer::start()
37{
Ethan Yonker20fd25c2014-09-03 14:04:43 -050038 if (setup() == 0) {
39 add_storage();
40 server->run();
41 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042}
43
44void twmtp_MtpServer::set_storages(storages* mtpstorages) {
45 stores = mtpstorages;
46}
47
Ethan Yonker20fd25c2014-09-03 14:04:43 -050048int twmtp_MtpServer::setup()
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040049{
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040050 usePtp = false;
51 MyMtpDatabase* mtpdb = new MyMtpDatabase();
52#ifdef USB_MTP_DEVICE
Ethan Yonkera1f38052014-09-11 08:28:51 -050053#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 bigbiffc7eee6f2014-09-02 18:59:01 -040057#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 Yonker20fd25c2014-09-03 14:04:43 -050066 MTPE("could not open MTP driver, errno: %d\n", errno);
67 return -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040068 }
Ethan Yonker20fd25c2014-09-03 14:04:43 -050069 return 0;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040070}
71
72void twmtp_MtpServer::run()
73{
74 MTPD("running in twmtp\n");
75 server->run();
76}
77
78void 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
90void 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
101void 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
112void 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
135void 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}