blob: 9062494f8c740662e05cb4fe9f9feeb9e79cbad5 [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"
28#include "MtpStorage.h"
Mike Lockwood64a44222010-05-16 16:39:32 -040029#include "f_mtp.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040030
Mike Lockwood7850ef92010-05-14 10:10:36 -040031using namespace android;
Mike Lockwood16864ba2010-05-11 17:16:59 -040032
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070033static bool enable_usb_function(const char* name, bool enable) {
Mike Lockwood16864ba2010-05-11 17:16:59 -040034 char path[PATH_MAX];
35
36 snprintf(path, sizeof(path), "/sys/class/usb_composite/%s/enable", name);
37 int fd = open(path, O_RDWR);
38 if (fd < 0) {
39 fprintf(stderr, "could not open %s in enable_usb_function\n", path);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070040 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -040041 }
42 write(fd, enable ? "1" : "0", 2);
43 close(fd);
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070044 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -040045}
46
Mike Lockwood64a44222010-05-16 16:39:32 -040047int main(int argc, char* argv[]) {
48 bool usePTP = false;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070049 const char* storagePath = "/sdcard";
Mike Lockwood64a44222010-05-16 16:39:32 -040050
51 for (int i = 1; i < argc; i++) {
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070052 const char* arg = argv[i];
53 if (!strcmp(arg, "-p"))
Mike Lockwood64a44222010-05-16 16:39:32 -040054 usePTP = true;
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070055 else if (arg[0] == '/')
56 storagePath = arg;
Mike Lockwood64a44222010-05-16 16:39:32 -040057 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040058
59 int fd = open("/dev/mtp_usb", O_RDWR);
60 printf("open returned %d\n", fd);
61 if (fd < 0) {
62 fprintf(stderr, "could not open MTP driver\n");
63 return -1;
64 }
65
Mike Lockwood64a44222010-05-16 16:39:32 -040066 if (usePTP) {
67 // set driver mode to PTP
68 int ret = ioctl(fd, MTP_SET_INTERFACE_MODE, MTP_INTERFACE_MODE_PTP);
69 if (ret) {
70 fprintf(stderr, "MTP_SET_INTERFACE_MODE failed\n");
71 return -1;
72 }
73 }
74
75 // disable UMS and enable MTP USB functions
76 enable_usb_function("usb_mass_storage", false);
77 enable_usb_function("mtp", true);
78
Mike Lockwood16864ba2010-05-11 17:16:59 -040079 MtpServer server(fd, "/data/data/mtp/mtp.db");
Mike Lockwood78c1e5e2010-06-15 10:28:47 -070080 server.addStorage(storagePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -040081 server.scanStorage();
82 server.run();
83
84 close(fd);
85 return 0;
86}
87