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 | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 33 | #include "MtpStorage.h" |
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 | |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 37 | // MtpServer fields |
| 38 | static jfieldID field_MtpServer_nativeContext; |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 39 | |
| 40 | // MtpStorage fields |
| 41 | static jfieldID field_MtpStorage_storageId; |
| 42 | static jfieldID field_MtpStorage_path; |
| 43 | static jfieldID field_MtpStorage_description; |
| 44 | static jfieldID field_MtpStorage_reserveSpace; |
Mike Lockwood | 5169054 | 2011-05-09 20:16:05 -0700 | [diff] [blame] | 45 | static jfieldID field_MtpStorage_removable; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 46 | static jfieldID field_MtpStorage_maxFileSize; |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 47 | |
| 48 | static Mutex sMutex; |
| 49 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 50 | // ---------------------------------------------------------------------------- |
| 51 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 52 | // in android_mtp_MtpDatabase.cpp |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 53 | extern MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 54 | |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 55 | static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) { |
| 56 | return (MtpServer*)env->GetIntField(thiz, field_MtpServer_nativeContext); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 59 | static void |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 60 | android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jboolean usePtp) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 61 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 62 | int fd = open("/dev/mtp_usb", O_RDWR); |
| 63 | if (fd >= 0) { |
| 64 | MtpServer* server = new MtpServer(fd, getMtpDatabase(env, javaDatabase), |
| 65 | usePtp, AID_MEDIA_RW, 0664, 0775); |
| 66 | env->SetIntField(thiz, field_MtpServer_nativeContext, (int)server); |
| 67 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 68 | ALOGE("could not open MTP driver, errno: %d", errno); |
Mike Lockwood | 071b2b6 | 2011-01-25 09:29:27 -0800 | [diff] [blame] | 69 | } |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void |
| 73 | android_mtp_MtpServer_run(JNIEnv *env, jobject thiz) |
| 74 | { |
| 75 | MtpServer* server = getMtpServer(env, thiz); |
| 76 | if (server) |
| 77 | server->run(); |
| 78 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 79 | ALOGE("server is null in run"); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void |
| 83 | android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz) |
| 84 | { |
| 85 | Mutex::Autolock autoLock(sMutex); |
| 86 | |
| 87 | MtpServer* server = getMtpServer(env, thiz); |
| 88 | if (server) { |
| 89 | delete server; |
| 90 | env->SetIntField(thiz, field_MtpServer_nativeContext, 0); |
| 91 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 92 | ALOGE("server is null in cleanup"); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 93 | } |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 96 | static void |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 97 | android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle) |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 98 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 99 | Mutex::Autolock autoLock(sMutex); |
| 100 | |
| 101 | MtpServer* server = getMtpServer(env, thiz); |
| 102 | if (server) |
| 103 | server->sendObjectAdded(handle); |
| 104 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 105 | ALOGE("server is null in send_object_added"); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 109 | android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle) |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 110 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 111 | Mutex::Autolock autoLock(sMutex); |
| 112 | |
| 113 | MtpServer* server = getMtpServer(env, thiz); |
| 114 | if (server) |
| 115 | server->sendObjectRemoved(handle); |
| 116 | else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 117 | ALOGE("server is null in send_object_removed"); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Mike Lockwood | eabe8bf | 2010-08-31 14:35:23 -0400 | [diff] [blame] | 120 | static void |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 121 | android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage) |
Mike Lockwood | 66e57f6 | 2011-02-18 13:24:01 -0500 | [diff] [blame] | 122 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 123 | Mutex::Autolock autoLock(sMutex); |
| 124 | |
| 125 | MtpServer* server = getMtpServer(env, thiz); |
| 126 | if (server) { |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 127 | jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId); |
| 128 | jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path); |
| 129 | jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description); |
| 130 | jlong reserveSpace = env->GetLongField(jstorage, field_MtpStorage_reserveSpace); |
Mike Lockwood | 5169054 | 2011-05-09 20:16:05 -0700 | [diff] [blame] | 131 | jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 132 | jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 133 | |
| 134 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 135 | if (pathStr != NULL) { |
| 136 | const char *descriptionStr = env->GetStringUTFChars(description, NULL); |
| 137 | if (descriptionStr != NULL) { |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 138 | MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr, |
| 139 | reserveSpace, removable, maxFileSize); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 140 | server->addStorage(storage); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 141 | env->ReleaseStringUTFChars(path, pathStr); |
| 142 | env->ReleaseStringUTFChars(description, descriptionStr); |
| 143 | } else { |
| 144 | env->ReleaseStringUTFChars(path, pathStr); |
| 145 | } |
| 146 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 147 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 148 | ALOGE("server is null in add_storage"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 149 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void |
| 153 | android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId) |
| 154 | { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 155 | Mutex::Autolock autoLock(sMutex); |
| 156 | |
| 157 | MtpServer* server = getMtpServer(env, thiz); |
| 158 | if (server) { |
| 159 | MtpStorage* storage = server->getStorage(storageId); |
| 160 | if (storage) { |
| 161 | server->removeStorage(storage); |
| 162 | delete storage; |
| 163 | } |
| 164 | } else |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 165 | ALOGE("server is null in remove_storage"); |
Mike Lockwood | 66e57f6 | 2011-02-18 13:24:01 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 168 | // ---------------------------------------------------------------------------- |
| 169 | |
| 170 | static JNINativeMethod gMethods[] = { |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 171 | {"native_setup", "(Landroid/mtp/MtpDatabase;Z)V", |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 172 | (void *)android_mtp_MtpServer_setup}, |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 173 | {"native_run", "()V", (void *)android_mtp_MtpServer_run}, |
| 174 | {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup}, |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 175 | {"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added}, |
| 176 | {"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed}, |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 177 | {"native_add_storage", "(Landroid/mtp/MtpStorage;)V", |
| 178 | (void *)android_mtp_MtpServer_add_storage}, |
| 179 | {"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage}, |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 180 | }; |
| 181 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 182 | static const char* const kClassPathName = "android/mtp/MtpServer"; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 183 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 184 | int register_android_mtp_MtpServer(JNIEnv *env) |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 185 | { |
| 186 | jclass clazz; |
| 187 | |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 188 | clazz = env->FindClass("android/mtp/MtpStorage"); |
| 189 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 190 | ALOGE("Can't find android/mtp/MtpStorage"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | field_MtpStorage_storageId = env->GetFieldID(clazz, "mStorageId", "I"); |
| 194 | if (field_MtpStorage_storageId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 195 | ALOGE("Can't find MtpStorage.mStorageId"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | field_MtpStorage_path = env->GetFieldID(clazz, "mPath", "Ljava/lang/String;"); |
| 199 | if (field_MtpStorage_path == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 200 | ALOGE("Can't find MtpStorage.mPath"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | field_MtpStorage_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;"); |
| 204 | if (field_MtpStorage_description == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 205 | ALOGE("Can't find MtpStorage.mDescription"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | field_MtpStorage_reserveSpace = env->GetFieldID(clazz, "mReserveSpace", "J"); |
| 209 | if (field_MtpStorage_reserveSpace == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 210 | ALOGE("Can't find MtpStorage.mReserveSpace"); |
Mike Lockwood | 5169054 | 2011-05-09 20:16:05 -0700 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | field_MtpStorage_removable = env->GetFieldID(clazz, "mRemovable", "Z"); |
| 214 | if (field_MtpStorage_removable == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 215 | ALOGE("Can't find MtpStorage.mRemovable"); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 216 | return -1; |
| 217 | } |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 218 | field_MtpStorage_maxFileSize = env->GetFieldID(clazz, "mMaxFileSize", "J"); |
| 219 | if (field_MtpStorage_maxFileSize == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 220 | ALOGE("Can't find MtpStorage.mMaxFileSize"); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 221 | return -1; |
| 222 | } |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 223 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 224 | clazz = env->FindClass("android/mtp/MtpServer"); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 225 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 226 | ALOGE("Can't find android/mtp/MtpServer"); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 227 | return -1; |
| 228 | } |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 229 | field_MtpServer_nativeContext = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 230 | if (field_MtpServer_nativeContext == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 231 | ALOGE("Can't find MtpServer.mNativeContext"); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 232 | return -1; |
| 233 | } |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 234 | |
| 235 | return AndroidRuntime::registerNativeMethods(env, |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 236 | "android/mtp/MtpServer", gMethods, NELEM(gMethods)); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 237 | } |