blob: a2cb8263040f978ae13df9eafd6c46d4df41be90 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -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
17#define LOG_TAG "mtp_usb"
Mike Lockwoodb14e5882010-06-29 18:11:52 -040018
19#include "MtpDebug.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040020
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <fcntl.h>
Mike Lockwood64a44222010-05-16 16:39:32 -040025#include <sys/ioctl.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040026
27#include "MtpServer.h"
Mike Lockwood1865a5d2010-07-03 00:44:05 -040028#include "MtpSqliteDatabase.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040029#include "MtpStorage.h"
Mike Lockwood64a44222010-05-16 16:39:32 -040030#include "f_mtp.h"
Mike Lockwood8e2a2802010-07-02 15:15:07 -040031#include "private/android_filesystem_config.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040032
Mike Lockwood7850ef92010-05-14 10:10:36 -040033using namespace android;
Mike Lockwood16864ba2010-05-11 17:16:59 -040034
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070035static bool enable_usb_function(const char* name, bool enable) {
Mike Lockwood16864ba2010-05-11 17:16:59 -040036 char path[PATH_MAX];
37
38 snprintf(path, sizeof(path), "/sys/class/usb_composite/%s/enable", name);
39 int fd = open(path, O_RDWR);
40 if (fd < 0) {
41 fprintf(stderr, "could not open %s in enable_usb_function\n", path);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070042 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -040043 }
44 write(fd, enable ? "1" : "0", 2);
45 close(fd);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070046 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -040047}
48
Mike Lockwood64a44222010-05-16 16:39:32 -040049int main(int argc, char* argv[]) {
50 bool usePTP = false;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070051 const char* storagePath = "/sdcard";
Mike Lockwood64a44222010-05-16 16:39:32 -040052
53 for (int i = 1; i < argc; i++) {
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070054 const char* arg = argv[i];
55 if (!strcmp(arg, "-p"))
Mike Lockwood64a44222010-05-16 16:39:32 -040056 usePTP = true;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070057 else if (arg[0] == '/')
58 storagePath = arg;
Mike Lockwood64a44222010-05-16 16:39:32 -040059 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040060
61 int fd = open("/dev/mtp_usb", O_RDWR);
62 printf("open returned %d\n", fd);
63 if (fd < 0) {
64 fprintf(stderr, "could not open MTP driver\n");
65 return -1;
66 }
67
Mike Lockwood64a44222010-05-16 16:39:32 -040068 if (usePTP) {
69 // set driver mode to PTP
70 int ret = ioctl(fd, MTP_SET_INTERFACE_MODE, MTP_INTERFACE_MODE_PTP);
71 if (ret) {
72 fprintf(stderr, "MTP_SET_INTERFACE_MODE failed\n");
73 return -1;
74 }
75 }
76
77 // disable UMS and enable MTP USB functions
78 enable_usb_function("usb_mass_storage", false);
79 enable_usb_function("mtp", true);
80
Mike Lockwood1865a5d2010-07-03 00:44:05 -040081 MtpSqliteDatabase* database = new MtpSqliteDatabase();
82 database->open("/data/data/mtp/mtp.db", true);
83 MtpServer server(fd, database, AID_SDCARD_RW, 0664, 0775);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070084 server.addStorage(storagePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -040085 server.scanStorage();
86 server.run();
87
88 close(fd);
89 return 0;
90}
91