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