Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -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 "MtpServerJNI" |
| 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <assert.h> |
| 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <utils/threads.h> |
| 26 | |
| 27 | #include "jni.h" |
| 28 | #include "JNIHelp.h" |
| 29 | #include "android_runtime/AndroidRuntime.h" |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 30 | #include "private/android_filesystem_config.h" |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 31 | |
| 32 | #include "MtpServer.h" |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 33 | #include "MtpSqliteDatabase.h" // REMOVE |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 34 | |
Mike Lockwood | 81ea83d | 2010-06-30 17:49:41 -0400 | [diff] [blame] | 35 | using namespace android; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | static jfieldID field_context; |
| 40 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 41 | // in android_media_MtpDatabase.cpp |
| 42 | extern MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | static bool ExceptionCheck(void* env) |
| 47 | { |
| 48 | return ((JNIEnv *)env)->ExceptionCheck(); |
| 49 | } |
| 50 | |
| 51 | class MtpThread : public Thread { |
| 52 | private: |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 53 | MtpDatabase* mDatabase; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 54 | String8 mStoragePath; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 55 | bool mDone; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 56 | |
| 57 | public: |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 58 | MtpThread(MtpDatabase* database, const char* storagePath) |
| 59 | : mDatabase(database), mStoragePath(storagePath), mDone(false) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
| 63 | virtual bool threadLoop() { |
| 64 | int fd = open("/dev/mtp_usb", O_RDWR); |
| 65 | printf("open returned %d\n", fd); |
| 66 | if (fd < 0) { |
| 67 | LOGE("could not open MTP driver\n"); |
| 68 | return false; |
| 69 | } |
| 70 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 71 | MtpServer* server = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 72 | server->addStorage(mStoragePath); |
| 73 | |
| 74 | // temporary |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 75 | LOGD("MtpThread server->run"); |
| 76 | server->run(); |
| 77 | close(fd); |
| 78 | delete server; |
| 79 | |
| 80 | bool done = mDone; |
| 81 | if (done) |
| 82 | delete this; |
| 83 | LOGD("threadLoop returning %s", (done ? "false" : "true")); |
| 84 | return !done; |
| 85 | } |
| 86 | |
| 87 | void setDone() { mDone = true; } |
| 88 | }; |
| 89 | |
| 90 | static void |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 91 | android_media_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jstring storagePath) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 92 | { |
| 93 | LOGD("setup\n"); |
| 94 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 95 | MtpDatabase* database = getMtpDatabase(env, javaDatabase); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 96 | const char *storagePathStr = env->GetStringUTFChars(storagePath, NULL); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 97 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 98 | MtpThread* thread = new MtpThread(database, storagePathStr); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 99 | env->SetIntField(thiz, field_context, (int)thread); |
| 100 | |
| 101 | env->ReleaseStringUTFChars(storagePath, storagePathStr); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void |
| 105 | android_media_MtpServer_finalize(JNIEnv *env, jobject thiz) |
| 106 | { |
| 107 | LOGD("finalize\n"); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | android_media_MtpServer_start(JNIEnv *env, jobject thiz) |
| 113 | { |
| 114 | LOGD("start\n"); |
| 115 | MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context); |
| 116 | thread->run("MtpThread"); |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | android_media_MtpServer_stop(JNIEnv *env, jobject thiz) |
| 121 | { |
| 122 | LOGD("stop\n"); |
| 123 | MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context); |
| 124 | if (thread) { |
| 125 | thread->setDone(); |
| 126 | env->SetIntField(thiz, field_context, 0); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | static JNINativeMethod gMethods[] = { |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame^] | 133 | {"native_setup", "(Landroid/media/MtpDatabase;Ljava/lang/String;)V", |
| 134 | (void *)android_media_MtpServer_setup}, |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 135 | {"native_finalize", "()V", (void *)android_media_MtpServer_finalize}, |
| 136 | {"native_start", "()V", (void *)android_media_MtpServer_start}, |
| 137 | {"native_stop", "()V", (void *)android_media_MtpServer_stop}, |
| 138 | }; |
| 139 | |
| 140 | static const char* const kClassPathName = "android/media/MtpServer"; |
| 141 | |
| 142 | int register_android_media_MtpServer(JNIEnv *env) |
| 143 | { |
| 144 | jclass clazz; |
| 145 | |
| 146 | LOGD("register_android_media_MtpServer\n"); |
| 147 | |
| 148 | clazz = env->FindClass("android/media/MtpServer"); |
| 149 | if (clazz == NULL) { |
| 150 | LOGE("Can't find android/media/MtpServer"); |
| 151 | return -1; |
| 152 | } |
| 153 | field_context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 154 | if (field_context == NULL) { |
| 155 | LOGE("Can't find MtpServer.mNativeContext"); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | return AndroidRuntime::registerNativeMethods(env, |
| 160 | "android/media/MtpServer", gMethods, NELEM(gMethods)); |
| 161 | } |