blob: 767cf2e695af88949910fdebbb03c00589772353 [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"
18#include "cutils/log.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/types.h>
23#include <fcntl.h>
Mike Lockwood64a44222010-05-16 16:39:32 -040024#include <sys/ioctl.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040025
26#include "MtpServer.h"
27#include "MtpStorage.h"
Mike Lockwood64a44222010-05-16 16:39:32 -040028#include "f_mtp.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040029
Mike Lockwood7850ef92010-05-14 10:10:36 -040030using namespace android;
Mike Lockwood16864ba2010-05-11 17:16:59 -040031
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070032static bool enable_usb_function(const char* name, bool enable) {
Mike Lockwood16864ba2010-05-11 17:16:59 -040033 char path[PATH_MAX];
34
35 snprintf(path, sizeof(path), "/sys/class/usb_composite/%s/enable", name);
36 int fd = open(path, O_RDWR);
37 if (fd < 0) {
38 fprintf(stderr, "could not open %s in enable_usb_function\n", path);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070039 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -040040 }
41 write(fd, enable ? "1" : "0", 2);
42 close(fd);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070043 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -040044}
45
Mike Lockwood64a44222010-05-16 16:39:32 -040046int main(int argc, char* argv[]) {
47 bool usePTP = false;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070048 const char* storagePath = "/sdcard";
Mike Lockwood64a44222010-05-16 16:39:32 -040049
50 for (int i = 1; i < argc; i++) {
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070051 const char* arg = argv[i];
52 if (!strcmp(arg, "-p"))
Mike Lockwood64a44222010-05-16 16:39:32 -040053 usePTP = true;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070054 else if (arg[0] == '/')
55 storagePath = arg;
Mike Lockwood64a44222010-05-16 16:39:32 -040056 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040057
58 int fd = open("/dev/mtp_usb", O_RDWR);
59 printf("open returned %d\n", fd);
60 if (fd < 0) {
61 fprintf(stderr, "could not open MTP driver\n");
62 return -1;
63 }
64
Mike Lockwood64a44222010-05-16 16:39:32 -040065 if (usePTP) {
66 // set driver mode to PTP
67 int ret = ioctl(fd, MTP_SET_INTERFACE_MODE, MTP_INTERFACE_MODE_PTP);
68 if (ret) {
69 fprintf(stderr, "MTP_SET_INTERFACE_MODE failed\n");
70 return -1;
71 }
72 }
73
74 // disable UMS and enable MTP USB functions
75 enable_usb_function("usb_mass_storage", false);
76 enable_usb_function("mtp", true);
77
Mike Lockwood16864ba2010-05-11 17:16:59 -040078 MtpServer server(fd, "/data/data/mtp/mtp.db");
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070079 server.addStorage(storagePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -040080 server.scanStorage();
81 server.run();
82
83 close(fd);
84 return 0;
85}
86