Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 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> |
| 24 | |
| 25 | #include "MtpServer.h" |
| 26 | #include "MtpStorage.h" |
| 27 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 28 | using namespace android; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 29 | |
| 30 | static void enable_usb_function(const char* name, bool enable) { |
| 31 | char path[PATH_MAX]; |
| 32 | |
| 33 | snprintf(path, sizeof(path), "/sys/class/usb_composite/%s/enable", name); |
| 34 | int fd = open(path, O_RDWR); |
| 35 | if (fd < 0) { |
| 36 | fprintf(stderr, "could not open %s in enable_usb_function\n", path); |
| 37 | exit(1); |
| 38 | } |
| 39 | write(fd, enable ? "1" : "0", 2); |
| 40 | close(fd); |
| 41 | } |
| 42 | |
| 43 | int main() { |
| 44 | // first disable UMS and enable MTP USB functions |
| 45 | enable_usb_function("usb_mass_storage", false); |
| 46 | enable_usb_function("mtp", true); |
| 47 | |
| 48 | int fd = open("/dev/mtp_usb", O_RDWR); |
| 49 | printf("open returned %d\n", fd); |
| 50 | if (fd < 0) { |
| 51 | fprintf(stderr, "could not open MTP driver\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | MtpServer server(fd, "/data/data/mtp/mtp.db"); |
| 56 | server.addStorage("/sdcard"); |
| 57 | server.scanStorage(); |
| 58 | server.run(); |
| 59 | |
| 60 | close(fd); |
| 61 | return 0; |
| 62 | } |
| 63 | |